From 20a04bca882fe7d56085a475a6bd556fba559713 Mon Sep 17 00:00:00 2001 From: whitequark Date: Sat, 15 Dec 2018 20:58:06 +0000 Subject: [PATCH] back.pysim: implement Part. --- nmigen/back/pysim.py | 6 +++++- nmigen/hdl/ast.py | 2 +- nmigen/test/test_sim.py | 6 ++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/nmigen/back/pysim.py b/nmigen/back/pysim.py index a3c09d1..e42169f 100644 --- a/nmigen/back/pysim.py +++ b/nmigen/back/pysim.py @@ -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() diff --git a/nmigen/hdl/ast.py b/nmigen/hdl/ast.py index 42923f4..0ed355b 100644 --- a/nmigen/hdl/ast.py +++ b/nmigen/hdl/ast.py @@ -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) diff --git a/nmigen/test/test_sim.py b/nmigen/test/test_sim.py index 7cecfd7..75e53a6 100644 --- a/nmigen/test/test_sim.py +++ b/nmigen/test/test_sim.py @@ -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)) -- 2.30.2