
// rnd_lfsr.c
// Galois LFSR 16-bit pseudo random number generator in 6502 Assembler
// Andre Adrian, 2026-02-13

#include <stdio.h>
#include <stdint.h>

#define Polynom	0x002D  // x^16+x^14+x^13+x^11+1
#define Seed    0xACE1  // any number >0

uint16_t rnd_num;       // 2 bytes 16-bit pseudo random number

// Galois LFSRs
// https://en.wikipedia.org/wiki/Linear-feedback_shift_register#Galois_LFSRs
// produces pseudo random number between 1 and 65535 in rnd_num
void rnd_lfsr(void)
{
    uint16_t carry_flag = rnd_num & 0x8000;
    rnd_num <<= 1;
    if (carry_flag) {
        rnd_num ^= Polynom;
    }
}

int main() {
    rnd_num = Seed;
    int cnt = 100;
    do {
        printf(" %04X", rnd_num);
        rnd_lfsr();
    } while (--cnt > 0);
}
