diff --git a/src/lfsr.c b/src/lfsr.c new file mode 100644 index 0000000..d3378d0 --- /dev/null +++ b/src/lfsr.c @@ -0,0 +1,14 @@ +#include "lfsr.h" + +void lfsr_init(Lfsr *ctx, uint16_t init) { + ctx->lfsr = init; + ctx->bit = 0; +} + +uint16_t lfsr_next(Lfsr *ctx) { + ctx->bit = ((ctx->lfsr >> 0) ^ (ctx->lfsr >> 2) ^ (ctx->lfsr >> 3) ^ + (ctx->lfsr >> 5)) & + 1U; + ctx->lfsr = (ctx->lfsr >> 1) | (ctx->bit << 15); + return ctx->lfsr; +} diff --git a/src/lfsr.h b/src/lfsr.h new file mode 100644 index 0000000..6cd3770 --- /dev/null +++ b/src/lfsr.h @@ -0,0 +1,14 @@ +#ifndef LFSR_H +#define LFSR_H + +#include + +typedef struct Lfsr { + uint16_t lfsr; + uint16_t bit; +} Lfsr; + +void lfsr_init(Lfsr *ctx, uint16_t init); +uint16_t lfsr_next(Lfsr *ctx); + +#endif // LFSR_H