Skip to navigation

Maths (Arithmetic): GetNextSeedNumber

Name: GetNextSeedNumber [Show more] Type: Subroutine Category: Maths (Arithmetic) Summary: Set A to a seed number
Seed numbers in The Sentinel are produced using a five-byte (40-bit) linear feedback shift register (LFSR) with EOR feedback. Specifically, to generate a new seed number, we shift the LFSR left by eight places, and on each shift we insert the EOR of bits 19 and 32 into bit 0 of the register. After eight shifts, the top byte is our next seed number.
.GetNextSeedNumber STY yStoreNextSeed \ Store Y in yStoreNextSeed so it can be preserved \ across calls to the routine \ We generate a new seed number by shifting the \ five-byte linear feedback shift register in \ seedNumberLFSR(4 3 2 1 0) by eight places, inserting \ EOR feedback as we do so LDY #8 \ Set a shift counter in Y .rand1 LDA seedNumberLFSR+2 \ Apply EOR feedback to the linear feedback shift LSR A \ register by taking the middle byte seedNumberLFSR+2, LSR A \ shifting it right by three places, EOR'ing it with LSR A \ seedNumberLFSR+4 in the output end of the shift EOR seedNumberLFSR+4 \ register and rotating bit 0 of the result into the C ROR A \ flag \ \ This is the same as taking bit 3 of seedNumberLFSR+2 \ and EOR'ing it with bit 0 of seedNumberLFSR+4 into the \ C flag \ \ We now use the C flag as the next input bit into the \ shift register \ \ So this is the same as EOR'ing bits 19 and 32 of our \ 40-bit register and shifting the result into bit 0 of \ the register ROL seedNumberLFSR \ Shift seedNumberLFSR(4 3 2 1 0) to the left by one ROL seedNumberLFSR+1 \ place, inserting the C flag into bit 0 of the input ROL seedNumberLFSR+2 \ end of the shift register in seedNumberLFSR ROL seedNumberLFSR+3 ROL seedNumberLFSR+4 DEY \ Decrement the shift counter BNE rand1 \ Loop back until we have shifted eight times LDY yStoreNextSeed \ Restore the value of Y from yStoreNextSeed that we \ stored at the start of the routine, so that it's \ preserved LDA seedNumberLFSR+4 \ Set A to the output end of the shift register in \ seedNumberLFSR+4 to give us our next seed number RTS \ Return from the subroutine