/* RISC-V LFSR test harness * * I want to test my LFSR function written in assembly. * * So this is a test harness that includes a version written in C. * * It computes the 15-bit LFSR whose characteristic polynomial * is x¹⁵ + x¹⁴ + 1, whose bit sequence, as it turns out, can be * computed by a 1-dimensional cellular automaton which produces the * shape of a Sierpiński triangle. */ #include int lfsr(unsigned char (*b)[16]); __attribute__((weak)) int lfsr(unsigned char (*b)[16]) { int i = (*b)[0]; int c = (*b)[i+1] ^= (*b)[(i+1) % 15 + 1]; i++; (*b)[0] = i % 15; return c; } int main() { unsigned char buf[16] = {0, 255}; for (size_t i = 0; i < 1024; i++) { for (size_t j = 0; j < 16; j++) { if (i % 15 == 0) printf("%3d ", buf[j]); } int c = lfsr(&buf); if (i % 15 == 0) printf("(%d)\n", c); } return 0; }