### Linear feedback shift register in DigiRule assembly

Posted:

**Fri Aug 30, 2019 3:28 pm**I wrote a simple blinkenlights of both the addr and data LEDs driven by the RANDA instruction. I wasn't too happy with the number of visible patterns (even though it was pretty), so I decided to implement a simple Galois LFSR. It also suffers from "hey, there seems to be a pattern here" in the blinkenlights, but it was a fun exercise. Anyway, for fun here it is:

For reference, here's the Cryptol code I used as the source for this:

Code: Select all

```
// Some handy constants
%define statusRegister 0xfc
%define addressLEDRegister 0xfe
%define dataLEDRegister 0xff
%define taps 0xf1
%define state 0xf0
// set things up
initsp
speed 32 // slow things down
sbr 2 statusRegister // to show bits
copylr 0xb4 taps
copylr 0x01 state
:Loop
call StepLFSR
copyrr state addressLEDRegister
call StepLFSR
copyrr state dataLEDRegister
jump Loop // Loop forever
// step the LFSR - state updated
:StepLFSR
bcrss 0 state // is the bottom bit set?
jump NoXor // if so, skip the xor
// otherwise shift & xor taps
shiftrr state
copyra state
xorra taps
copyar state
return
:NoXor
shiftrr state
return
```

Code: Select all

```
init = 0b1010110011100001
fibtaps = 0xb400 // from wikipedia - produces a maximal cycle
galoisLFSR : {n} (fin n) => [n] -> [n] -> [inf][n]
galoisLFSR seed taps = elts where
elts = [seed] # [ nextState state | state <- elts ]
nextState : [n] -> [n]
nextState s = if s!0 then n ^ taps else n where n = s >> 1
```