From 62e13b6cb049ca7531ab7bf9ceeafa8550739105 Mon Sep 17 00:00:00 2001 From: "A.M. Rowsell" Date: Sun, 14 Jul 2024 08:51:06 -0400 Subject: [PATCH] Initial commit of project! --- LICENSE | 21 +++++++++++++++ Makefile | 22 ++++++++++++++++ main.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ plottemplate | 21 +++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 main.c create mode 100644 plottemplate diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9626160 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 A.M. Rowsell + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..80029b7 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +APP=main +AWK=awk +CC=gcc +CFLAGS=-std=gnu11 -O2 + +SRCS = $(wildcard *.c) +OBJS = $(SRCS:.c=.o) + +.PHONY: all clean + +all: clean $(APP) plot + +$(APP): $(OBJS) + $(CC) $(LDFLAGS) $^ $(LIBS) -o $@ + +clean: + $(RM) $(OBJS) $(APP) plot.png + +plot: $(APP) + ./$(APP) $(TAPS) | $(AWK) -v k=0 '{ print k " " $$1; k++ }' > 16bit.txt + gnuplot plot + xdg-open plot.png diff --git a/main.c b/main.c new file mode 100644 index 0000000..6aa4ada --- /dev/null +++ b/main.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include + +// 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; +} diff --git a/plottemplate b/plottemplate new file mode 100644 index 0000000..766aada --- /dev/null +++ b/plottemplate @@ -0,0 +1,21 @@ +set border linewidth 5 + +set style line 101 lt 0 lc rgb "gray20" lw 0.5 +set style line 102 lt 2 lc rgb "black" lw 1 + +set xlabel 'Sequence number' +set ylabel 'LFSR value' + +set ytics 2500 +set mytics 2 +set xrange [0:65535] +set yrange [0:65535] +set xtics 2500 rotate by 60 right +set mxtics 2 +set tics scale 1 + +set title "16-bit LFSR: Taps $" + +set terminal png size 3840,2160 font 'Fira Code Nerd Font,30' +set output "plot.png" +plot "16bit.txt" using 1:2 notitle with points lc 14 pt 6 ps 0.5