-
Notifications
You must be signed in to change notification settings - Fork 9
LFSR
Rob Stave edited this page Oct 30, 2015
·
6 revisions
One of the basic building blocks used here is the Linear Feedback Shift Register. This is used to generate random values. The one used here is a 16 bit shift register. The values at certain taps are xor'ed together and feed back into the shift register. The number of unique states should be 2^n -1. In this case 2^16 -1 = 65535.
This is the same LFSR as is described in the wikipedia.
https://en.wikipedia.org/wiki/Linear_feedback_shift_register
Internally, the code is REALLY simple Define your lfsr:
unsigned int lfsr = 1;
void clockLfsr () {
//calculate new state
boolean outputBit = bitRead(lfsr, 10) ^ bitRead(lfsr, 12) ^ bitRead(lfsr, 13) ^ bitRead(lfsr, 15);
lfsr = lfsr << 1;
lfsr |= outputBit;
}
Each time you want to clock the LFSR, you just call the above code
grab the number of bits you want by masking it off
byte reg = lfsr & B11111111;
I generally shift it 4x to get a new number. You can do 2 and 4, but if you do 3x, you actually reduce the number of total states.