/* The two-head physics as a netlist, stepped beside its transcription. * twohead_lockstep NETLIST INIT GENERATIONS (run by src/twohead_net.py lockstep) * NETLIST is the file src/twohead_net.py export writes. INIT holds G, L, the number of * window cells in the state and their addresses as 32-bit integers, then the initial state, * the organism and its description, one byte per bit. After every step the whole state of * the netlist is compared with the transcription, and at each change of kappa the new bank * and description tape are compared with the organism and its description. */ #include #include #include #include enum { KAPPA = 0, RD = 1, ARM = 2, CPY = 3, AR = 4, AA = 5, AC = 6, B = 26, W = 64 }; static int n, U, E, *bias, *fan, *pred, *nxt, *start; static unsigned char *sig; static void *rd(FILE *f, size_t sz, size_t k) { void *p = malloc(sz * k + 8); if (fread(p, sz, k, f) != k) { fprintf(stderr, "short\n"); exit(2); } return p; } /* transcription: tapes as circular buffers with a head offset */ typedef struct { unsigned char *a; long len, h; } Tape; static int get(Tape *t, long i) { return t->a[(t->h + i) % t->len]; } static void put0(Tape *t, int v) { t->a[t->h] = v; } static void turn(Tape *t, long s) { t->h = (t->h + s) % t->len; } int main(int argc, char **argv) { FILE *f = fopen(argv[1], "rb"); int hdr[3]; if (fread(hdr, 4, 3, f) != 3) return 2; n = hdr[0]; U = hdr[1]; E = hdr[2]; bias = rd(f, 4, U); fan = rd(f, 4, U); pred = rd(f, 4, E); nxt = rd(f, 4, n); fclose(f); start = malloc(4 * (U + 1)); start[0] = 0; for (int u = 0; u < U; u++) start[u + 1] = start[u] + fan[u]; f = fopen(argv[2], "rb"); int p3[3]; if (fread(p3, 4, 3, f) != 3) return 2; int G = p3[0], L = p3[1], nreal = p3[2]; long N = (long)G * B; int *real = rd(f, 4, nreal); unsigned char *x = rd(f, 1, n), *rho = rd(f, 1, N), *desc = rd(f, 1, L); fclose(f); int gens = atoi(argv[3]); sig = calloc(n + U, 1); unsigned char *y = malloc(n); long q0 = nreal, q1 = nreal + N, d0 = nreal + 2 * N, d1 = nreal + 2 * N + L; /* the transcription, from the same initial state */ unsigned char w[W] = {0}; for (int i = 0; i < nreal; i++) w[real[i]] = x[i]; Tape Q[2], D[2]; for (int j = 0; j < 2; j++) { Q[j].len = N; Q[j].h = 0; Q[j].a = malloc(N); memcpy(Q[j].a, x + (j ? q1 : q0), N); D[j].len = L; D[j].h = 0; D[j].a = malloc(L); memcpy(D[j].a, x + (j ? d1 : d0), L); } long steps = 0, mism = 0, gstart = 0; int g = 0, k0 = w[KAPPA]; while (g < gens) { /* the netlist */ memcpy(sig, x, n); for (int u = 0; u < U; u++) { int s = bias[u]; for (int e = start[u]; e < start[u + 1]; e++) { int q = pred[e]; s += q > 0 ? sig[q - 1] : -sig[-q - 1]; } sig[n + u] = s >= 0; } for (int i = 0; i < n; i++) y[i] = sig[nxt[i]]; /* the transcription */ int k = w[KAPPA], f_[B]; for (int j = 0; j < B; j++) f_[j] = get(&Q[k], j); int c0 = f_[0] * 2 + f_[1], c1 = f_[2] * 2 + f_[3]; int beta = f_[4] * 8 + f_[5] * 4 + f_[6] * 2 + f_[7]; if (beta >= 8) beta -= 16; int a0 = 0, a1 = 0, ao = 0; for (int j = 0; j < 6; j++) { a0 = a0 * 2 + f_[8 + j]; a1 = a1 * 2 + f_[14 + j]; ao = ao * 2 + f_[20 + j]; } int w0 = c0 == 1 ? 1 : c0 == 2 ? -1 : 0, w1 = c1 == 1 ? 1 : c1 == 2 ? -1 : 0; #define CELL(a) ((a) == RD ? get(&D[k], 0) : (a) == ARM ? get(&Q[1 - k], 0) : (a) == CPY ? get(&D[1 - k], 0) : w[a]) int v = w0 * CELL(a0) + w1 * CELL(a1) + beta >= 0; if (ao == ARM) put0(&Q[1 - k], v); else if (ao == CPY) put0(&D[1 - k], v); else if (ao != RD) w[ao] = v; turn(&Q[k], B); if (w[AA]) turn(&Q[1 - k], 1); if (w[AR]) turn(&D[k], 1); if (w[AC]) turn(&D[1 - k], 1); w[AA] = w[AR] = w[AC] = 0; steps++; /* compare the whole state */ int bad = 0; for (int i = 0; i < nreal && !bad; i++) bad = y[i] != w[real[i]]; for (long i = 0; i < N && !bad; i++) bad = y[q0 + i] != get(&Q[0], i) || y[q1 + i] != get(&Q[1], i); for (long i = 0; i < L && !bad; i++) bad = y[d0 + i] != get(&D[0], i) || y[d1 + i] != get(&D[1], i); if (bad && mism++ < 3) printf("state mismatch at step %ld\n", steps); memcpy(x, y, n); if (w[KAPPA] != k0) { int c = 1 - k0, okq = 1, okd = 1; for (long i = 0; i < N; i++) okq &= x[(c ? q1 : q0) + i] == rho[i]; for (long i = 0; i < L; i++) okd &= x[(c ? d1 : d0) + i] == desc[i]; printf("generation %d: %ld steps; netlist child bank == rho: %d, child description == description: %d\n", ++g, steps - gstart, okq, okd); gstart = steps; k0 = w[KAPPA]; } } printf("%ld steps in lockstep, %ld state mismatches\n", steps, mism); return mism != 0; }