-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlsfr.py
More file actions
31 lines (27 loc) · 969 Bytes
/
lsfr.py
File metadata and controls
31 lines (27 loc) · 969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
This is a python script to takes a Length, Seed, and Gates and uses those
to implement an LFSR. Prints each step in binary and the length of the
repeating series.
"""
LENGTH = 10 # The number of bits in the seed
SEED = 0b10000 # The seed value
GATES = [0,2,3,5] # The P gates for the LSFR
def main() :
global LENGTH, SEED, GATES
print(("{:0"+str(LENGTH)+"b}").format(SEED))
currentSeed = SEED
savedStates = {SEED:1}
count = 0
while(1) :
newbit = (currentSeed >> GATES[0]) & 1
for i in GATES[1:] :
newbit = newbit ^ ((currentSeed >> i) & 1)
currentSeed = (currentSeed >> 1) | (newbit << (LENGTH-1))
print(("{:0"+str(LENGTH)+"b}").format(currentSeed))
if(currentSeed in savedStates.keys()) :
break
savedStates[currentSeed] = 1
count+=1
print("The series repeats after", count, "rounds")
if __name__ == "__main__":
main()