sim._pyrtl: mask Mux selection operand.
authorMarcelina Koƛcielnicka <mwk@0x04.net>
Sat, 14 Nov 2020 15:22:34 +0000 (16:22 +0100)
committerGitHub <noreply@github.com>
Sat, 14 Nov 2020 15:22:34 +0000 (15:22 +0000)
Otherwise it behaves funny when it's eg. the result of operator ~.

nmigen/sim/_pyrtl.py
tests/test_sim.py

index f7abc3bb9df82a08280a53a7407c0b281c495e40..3258f2b6411ff1a7530e2a297baf14871a81ba62 100644 (file)
@@ -177,7 +177,7 @@ class _RHSValueCompiler(_ValueCompiler):
         elif len(value.operands) == 3:
             if value.operator == "m":
                 sel, val1, val0 = value.operands
-                return f"({self(val1)} if {self(sel)} else {self(val0)})"
+                return f"({self(val1)} if {mask(sel)} else {self(val0)})"
         raise NotImplementedError("Operator '{}' not implemented".format(value.operator)) # :nocov:
 
     def on_Slice(self, value):
index d2d255f5e94853f1d5ba06154267746b60da2d3c..bb3906c077182929f811c2de7f039bb47da92967 100644 (file)
@@ -186,6 +186,11 @@ class SimulatorUnitTestCase(FHDLTestCase):
         self.assertStatement(stmt, [C(2, 4), C(3, 4), C(0)], C(3, 4))
         self.assertStatement(stmt, [C(2, 4), C(3, 4), C(1)], C(2, 4))
 
+    def test_mux_invert(self):
+        stmt = lambda y, a, b, c: y.eq(Mux(~c, a, b))
+        self.assertStatement(stmt, [C(2, 4), C(3, 4), C(0)], C(2, 4))
+        self.assertStatement(stmt, [C(2, 4), C(3, 4), C(1)], C(3, 4))
+
     def test_abs(self):
         stmt = lambda y, a: y.eq(abs(a))
         self.assertStatement(stmt, [C(3,  unsigned(8))], C(3,  unsigned(8)))