From e0d7914d6d99ae1c7d4c030152d6d332a99afcd5 Mon Sep 17 00:00:00 2001 From: Sam Hadow Date: Sat, 13 Apr 2024 00:28:03 +0200 Subject: [PATCH] lfsr --- .gitignore | 17 +---------------- main.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 16 deletions(-) create mode 100644 main.py diff --git a/.gitignore b/.gitignore index 0d96175..4acf9a3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,21 +1,6 @@ -# ---> Rust -# Generated by Cargo -# will have compiled files and executables -debug/ -target/ - -# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries -# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html -Cargo.lock - -# These are backup files generated by rustfmt -**/*.rs.bk - -# MSVC Windows builds of rustc generate these, which store debugging information -*.pdb - # ---> Kate # Swap Files # .*.kate-swp .swp.* +*.pdf diff --git a/main.py b/main.py new file mode 100644 index 0000000..c810f4e --- /dev/null +++ b/main.py @@ -0,0 +1,39 @@ +#!/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=' ')