40 lines
1.0 KiB
Python
40 lines
1.0 KiB
Python
|
#!/usr/bin/env python3
|
||
|
|
||
|
class lfsr(object):
|
||
|
def __init__(self, state, taps):
|
||
|
self.state = state
|
||
|
self.taps = taps
|
||
|
|
||
|
# getters and setters (private attributes)
|
||
|
def get_state(self):
|
||
|
return self.__state
|
||
|
def set_state(self, value):
|
||
|
self.__state = value
|
||
|
state = property(fget=get_state, fset=set_state, doc="lfsr state (current bits value in lfsr)")
|
||
|
|
||
|
def get_taps(self):
|
||
|
return self.__taps
|
||
|
def set_taps(self, valeur):
|
||
|
self.__taps = valeur
|
||
|
taps = property(fget=get_taps, fset=set_taps, doc="lfsr taps (bit positions affecting next state)")
|
||
|
|
||
|
def shift(self):
|
||
|
'''
|
||
|
calculate next state and return the output bit
|
||
|
'''
|
||
|
feedback = sum(self.state[tap] for tap in self.taps) % 2
|
||
|
output = self.state[-1]
|
||
|
self.state = [feedback] + self.state[:-1]
|
||
|
return output
|
||
|
|
||
|
|
||
|
key=[0]*16
|
||
|
key.append(1)
|
||
|
taps = [16-0, 16-14]
|
||
|
|
||
|
lfsr17 = lfsr(key, taps)
|
||
|
|
||
|
# Generate 100 bits
|
||
|
for _ in range(100):
|
||
|
print(lfsr17.shift(), end=' ')
|