Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
Added a Python implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mfukar committed Feb 3, 2011
1 parent 124828f commit 5061ede
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lfsr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python
# @file /home/mfukar/src/lfsr/lfsr.py
# @author Michael Foukarakis
# @version 1.0
# @date Created: Thu Feb 03, 2011 08:19 GTB Standard Time
# Last Update: Thu Feb 03, 2011 16:26 GTB Standard Time
#------------------------------------------------------------------------
# Description: Implementation of a Galois LFSR
# The bitstream is provided by the generator function
# GLFSR.states(). The 'repeat' flag controls whether the
# generator stops once the LFSR overflows or whether it
# continues perpetually.
#------------------------------------------------------------------------
# History: None yet
# TODO: Nothing yet
#------------------------------------------------------------------------
class GLFSR:
def __init__(self, polynomial, seed):
self.polynomial = polynomial | 1
self.seed = seed
self.data = seed
self.mask = 1

temp_mask = polynomial
while temp_mask != 0:
if temp_mask & self.mask != 0:
temp_mask = temp_mask ^ self.mask
if temp_mask == 0: break
self.mask = self.mask << 1

def states(self, repeat=False):
while True:
self.data = self.data << 1
if self.data & self.mask != 0:
self.data = self.data ^ self.polynomial
yield 1
else:
yield 0
if repeat == False and self.data == self.seed:
return

0 comments on commit 5061ede

Please sign in to comment.