74 lines
1.7 KiB
C
74 lines
1.7 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <inttypes.h>
|
|
#include <stddef.h>
|
|
|
|
// x^16 + x^15 + x^13 + x^1 + 1 -- D4 LFSR
|
|
// 0, 7, 12, 14 works for 16-bits
|
|
// 1, 12, 14, 15 works for 16-bits
|
|
// 16,14,13,11
|
|
// 0, 12, 14, 15 works for 15-bits
|
|
// 31, 9, 7, 6, 3, 2
|
|
// 29, 28, 25, 24, 22, 0
|
|
// 0x8000019F
|
|
|
|
void createGnuplot(int tap1, int tap2, int tap3, int tap4) {
|
|
char buf[1] = " ";
|
|
FILE *template = fopen("plottemplate", "r");
|
|
FILE *plotout = fopen("plot", "w");
|
|
if(template == NULL || plotout == NULL) {
|
|
return;
|
|
}
|
|
while(1) {
|
|
*buf = fgetc(template);
|
|
if (*buf == EOF) {
|
|
fflush(plotout);
|
|
break;
|
|
}
|
|
if(*buf == '$') {
|
|
fprintf(plotout, "%d, %d, %d, %d", tap1, tap2, tap3, tap4);
|
|
} else {
|
|
fputc(*buf, plotout);
|
|
}
|
|
}
|
|
fclose(template);
|
|
fclose(plotout);
|
|
|
|
return;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
uint16_t startState = 0x0005;
|
|
uint16_t lfsr = startState;
|
|
uint16_t bit;
|
|
uint32_t count = 0;
|
|
char *c;
|
|
int tap1, tap2, tap3, tap4;
|
|
|
|
|
|
if(argc == 5) {
|
|
tap1 = strtol(argv[1], NULL, 10);
|
|
tap2 = strtol(argv[2], NULL, 10);
|
|
tap3 = strtol(argv[3], NULL, 10);
|
|
tap4 = strtol(argv[4], NULL, 10);
|
|
} else {
|
|
tap1 = 15;
|
|
tap2 = 4;
|
|
tap3 = 2;
|
|
tap4 = 1;
|
|
}
|
|
//printf("Initial state: %0x\n", lfsr);
|
|
|
|
do {
|
|
bit = ((lfsr >> tap1) ^ (lfsr >> tap2) ^ (lfsr >> tap3) ^ (lfsr >> tap4));
|
|
lfsr = (lfsr >> 1) | (bit << 15);
|
|
count++;
|
|
//count % 100000000 == 0 ? printf("0x%08x\t%032b\n", lfsr, lfsr) : 0; // comment this line for speed check
|
|
printf("%d\n", lfsr);
|
|
// *c = scanf("%s");
|
|
} while(lfsr != startState && count < 70000); //while(lfsr != startState);
|
|
//printf("Final count: %u\n", count);
|
|
createGnuplot(tap1, tap2, tap3, tap4);
|
|
|
|
return 0;
|
|
}
|