skip the entire thing if width is zero
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Mon, 22 Apr 2019 10:49:02 +0000 (11:49 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Mon, 22 Apr 2019 10:49:02 +0000 (11:49 +0100)
simplify creation of feedback: use Cat(feedback, statebits)
add test code-generation

TLB/src/LFSR2.py

index 455ff90c2c9cb94bdf9780dbdd3724496234a997..997f85b396e090a1aa4aeadbcb0107918a22463e 100644 (file)
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: LGPL-2.1-or-later
 # See Notices.txt for copyright information
-from nmigen import Signal, Module, Const
+from nmigen import Signal, Module, Const, Cat
+from nmigen.cli import verilog, rtlil
 
 
 class LFSRPolynomial(set):
@@ -81,14 +82,18 @@ class LFSR:
 
     def elaborate(self, platform):
         m = Module()
-        feedback: Value = Const(0)
+        if self.width == 0:
+            return m
+        feedback = Const(0)
         for exponent in self.polynomial:
             if exponent > 0:
-                feedback = feedback ^ self.state[exponent - 1]
-        if self.width > 1:
-            with m.If(self.enable):
-                m.d.sync += self.state[1:self.width].eq(
-                    self.state[0:self.width - 1])
-                m.d.sync += self.state[0].eq(feedback)
+                feedback ^= self.state[exponent - 1]
+        with m.If(self.enable):
+            newstate = Cat(feedback, self.state[0:self.width - 1])
+            m.d.sync += self.state.eq(newstate)
         return m
 
+if __name__ == '__main__':
+    p24 = rtlil.convert(LFSR([24, 23, 22, 17, 0]))
+    with open("lfsr2_p24.il", "w") as f:
+        f.write(p24)