back.pysim: implement Part.
authorwhitequark <whitequark@whitequark.org>
Sat, 15 Dec 2018 20:58:06 +0000 (20:58 +0000)
committerwhitequark <whitequark@whitequark.org>
Sat, 15 Dec 2018 20:58:06 +0000 (20:58 +0000)
nmigen/back/pysim.py
nmigen/hdl/ast.py
nmigen/test/test_sim.py

index a3c09d1fc4952e3f2a800f4b966c90db8bef963e..e42169fbdc59faab7fd61b402e459f7d8d8ffbc1 100644 (file)
@@ -118,7 +118,11 @@ class _RHSValueCompiler(ValueTransformer):
         return lambda state: normalize((arg(state) >> shift) & mask, shape)
 
     def on_Part(self, value):
-        raise NotImplementedError
+        shape = value.shape()
+        arg   = self(value.value)
+        shift = self(value.offset)
+        mask  = (1 << value.width) - 1
+        return lambda state: normalize((arg(state) >> shift(state)) & mask, shape)
 
     def on_Cat(self, value):
         shape  = value.shape()
index 42923f4e33783a0814a54405a1209a0108261320..0ed355b8af3a70f909bf2351ae5619c336098157 100644 (file)
@@ -405,7 +405,7 @@ class Part(Value):
         return self.value._lhs_signals()
 
     def _rhs_signals(self):
-        return self.value._rhs_signals()
+        return self.value._rhs_signals() | self.offset._rhs_signals()
 
     def __repr__(self):
         return "(part {} {} {})".format(repr(self.value), repr(self.offset), self.width)
index 7cecfd736f87896efc6a1b20db62de0781b63525..75e53a6b561d53adbdc95e9d5a8093e1733190e1 100644 (file)
@@ -130,6 +130,12 @@ class SimulatorUnitTestCase(FHDLTestCase):
         stmt2 = lambda a: a[2:4]
         self.assertOperator(stmt2, [C(0b10110100, 8)], C(0b01, 2))
 
+    def test_part(self):
+        stmt = lambda a, b: a.part(b, 3)
+        self.assertOperator(stmt, [C(0b10110100, 8), C(0)], C(0b100, 3))
+        self.assertOperator(stmt, [C(0b10110100, 8), C(2)], C(0b101, 3))
+        self.assertOperator(stmt, [C(0b10110100, 8), C(3)], C(0b110, 3))
+
     def test_cat(self):
         self.assertOperator(Cat, [C(0b10, 2), C(0b01, 2)], C(0b0110, 4))