#include #include #include /* memcmp */ #include /* strdup */ #include /* exit */ #ifndef M_PI #define M_PI 3.14159265358979323846 #endif // pull in EXTRA and MW from sim1.h #include "sim1.h" #define SCE (1<1) #define VERBOSE (verbose) // In nominal configuration, shifter input is 21 bits, output is 18 bits // Valid shift values are 0, 1, 2, and 3. static int shifter(int a, int shift, const char *label) { if (TRACE) printf("shifter in %d (%+9.6f)\n", a, F(a)); a = a >> (3-shift); if (a > (VMAX-1)) a = VMAX-1; if (a < (-VMAX )) a = -VMAX; if (VERBOSE) printf("%s result %9d (%+9.6f)\n", label, a, F(a)); return a; } static int mul(int a, int b, int shift) { long long r = (long long)(a>>MUL_SHF)*(long long)(b>>MUL_SHF); if (TRACE) printf("multiply %lld = %d * %d\n", r, a, b); return shifter(r>>(2*MW-PW-2*EXTRA), shift, "mul"); } static int add(int a, int b, int shift) { int r = a + b; if (TRACE) printf("add %d = %d + %d\n", r, a, b); return shifter(r<<2, shift, "add"); } static int sub(int a, int b, int shift) { int r = a - b; if (TRACE) printf("sub %d = %d - %d\n", r, a, b); return shifter(r<<2, shift, "sub"); } static int inv(int a, int shift) { // Lots of ugly special cases const unsigned int iscale = 8; unsigned int u = (a<0) ? -a : a; u = u >> (PW+EXTRA-2-iscale); unsigned int u0 = u; // almost just for printing unsigned int ur; for (ur = 512; u>3; ur = ur >> 1) u = u >> 1; // u is now typically 2 or 3 if (u==2) ur = ur + ur/2; ur /= 2; if (u0<2) ur=512; if (u0==0) ur=1023; // printf("LUT in %d out %d\n", u0, ur); int r = (a<0) ? -ur : ur; r = r << (PW+4+EXTRA-3-iscale); if (TRACE) printf("inv %d (%.5f) [%u] %d (%.5f)\n", a, F(a), u, r, F(r)); return shifter(r, shift, "inv"); } static int invsqrt(int a, int shift) { // Using convention that a represents -1 to 1, start by taking abs(a). // Design input range 1/64 to 1, output range 1 to 1/8. // Actual function approximated is therefore 1/(8*sqrt(a)). // Table is segmented into three ranges of two octaves each; see sqrt1.py. // Lots of ugly special cases. const unsigned int iscale = 8; unsigned int u = (a<0) ? -a : a; u = u >> (18+EXTRA-2-iscale); unsigned int u0 = u; // almost just for printing unsigned int ur; for (ur = 512; u>7; ur = ur >> 1) u = u >> 2; switch(u) { case 2: ur = ur + 3*ur/4; break; case 3: ur = ur + ur/2; break; case 4: ur = ur + ur/4; break; case 5: ur = ur + ur/4; break; default: break; } if (u0<2) ur=1023; // printf("LUT in %d out %d\n", u0, ur); int r = (a<0) ? -ur : ur; r = r << (18+4+EXTRA-3-iscale); if (TRACE) printf("invsqrt %d (%.5f) [%u] %d (%.5f)\n", a, F(a), u, r, F(r)); return shifter(r, shift, "invsqrt"); } static void set_result_ab(int a, int b) { printf("result_ab %.6f %.6f\n", F(a), F(b)); } static void set_result_cd(int a, int b) { printf("result_cd %.6f %.6f\n", F(a), F(b)); } // Brain-dead quadratic-time hard-limited setup for a "dictionary" // Simpler and more reliable than requiring some external library. #define MAX_PERSIST 20 struct persist_var { char *name; int value; } persist_list[MAX_PERSIST]; unsigned persist_count = 0; static int persist_get(const char *name) { for (unsigned u=0; u 32768) && ((check > 1.0005 || check < 0.9999)); if (fault) fail = 1; printf("plot %7d %7d %7d %7d %7d %.6f %s\n", x, s_r0, s_r1, s_r2, s, check, fault ? "BAD" : "."); } return fail; } /* Special for run2.dat */ static void file_loop(const char *fname, int given[], unsigned given_size) { char iline[80]; FILE *file2 = fopen(fname, "r"); long int r[8]; const int fs = 64; // Input file is 16 bits, simulator is 22 bits if (file2 == NULL) { perror("fopen"); return; } while (fgets(iline, sizeof(iline), file2)) { // printf("%s", iline); char *ss = iline; for (unsigned jx=0; jx<8 && ss; jx++) { r[jx] = strtol(ss, &ss, 0); // printf("%ld\n", r[jx]); } // Provision for channel remapping, not used, hurray! given[0] = r[2]*fs; given[1] = r[3]*fs; // forward given[2] = r[4]*fs; given[3] = r[5]*fs; // reverse given[4] = r[6]*fs; given[5] = r[7]*fs; // cavity for (unsigned u=0; u 0 && iline[iline_len-1] == '\n') iline[iline_len-1] = 0; // printf("%s", iline); unsigned int row; int val; if (0 == memcmp(iline, "digaree constants ", 18)) { // digaree constants 19330 1934 2368 32768 0 0 0 0 unsigned jx=DATA_LEN; char *p1 = iline + 18; do { long v = strtol(p1, &p1, 10) * SCE; printf("given[%u] = %ld\n", jx, v); given[jx++] = v; } while (jx 1) && 0 == strcmp(argv[1], "invcheck")) { invcheck(); return 0; } if ((argc > 1) && 0 == strcmp(argv[1], "invsqrtcheck")) { int rc =invsqrtcheck(); printf(rc ? "FAIL\n" : "PASS\n"); return rc; } if ((argc > 2) && 0 == strcmp(argv[1], "replay")) { return replay(argv[2]); } unsigned int u, given_size; int given[16]; char iline[80]; printf("Starting C-based bit-accurate simulator\n"); char pname[80]; unsigned dummy; char type; int val; u = 0; // init.dat must keep the s, h order matching how the sf_user module // streams data to the ALU. while (fgets(iline, sizeof(iline), stdin)) { if (*iline == '#') { } else if ((*iline == 's' || *iline == 'h') && 3 == sscanf(iline, "%c %u %d", &type, &dummy, &val)) { if (u < sizeof(given)/sizeof(given[0])) { given[u++] = SCE * val; } } else if (*iline == 'p' && 3 == sscanf(iline, "%c %20s %d", &type, pname, &val)) { persist_set(pname, SCE * val); } } given_size = u; printf("read initialization file, %u\n", given_size); if (argc > 1) { file_loop(argv[1], given, given_size); } else { for (u=0; u