From: whitequark Date: Fri, 20 Sep 2019 10:12:59 +0000 (+0000) Subject: back.pysim: fix simulation of Value.xor(). X-Git-Tag: v0.1rc1~96 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=7f6b3f93f5bbdc4eb490ce6ea6bff3790b9749d3;p=nmigen.git back.pysim: fix simulation of Value.xor(). --- diff --git a/nmigen/back/pysim.py b/nmigen/back/pysim.py index 4ff3ed5..57461ee 100644 --- a/nmigen/back/pysim.py +++ b/nmigen/back/pysim.py @@ -137,7 +137,7 @@ class _RHSValueCompiler(_ValueCompiler): return lambda state: normalize(arg(state) == mask, shape) if value.op == "r^": # Believe it or not, this is the fastest way to compute a sideways XOR in Python. - return lambda state: normalize(str(arg(state)).count("1") % 2, shape) + return lambda state: normalize(format(arg(state), "b").count("1") % 2, shape) elif len(value.operands) == 2: lhs, rhs = map(self, value.operands) if value.op == "+": diff --git a/nmigen/test/test_sim.py b/nmigen/test/test_sim.py index fae5718..7979e49 100644 --- a/nmigen/test/test_sim.py +++ b/nmigen/test/test_sim.py @@ -61,18 +61,21 @@ class SimulatorUnitTestCase(FHDLTestCase): stmt = lambda y, a: y.eq(a.any()) self.assertStatement(stmt, [C(0b00, 2)], C(0)) self.assertStatement(stmt, [C(0b01, 2)], C(1)) + self.assertStatement(stmt, [C(0b10, 2)], C(1)) self.assertStatement(stmt, [C(0b11, 2)], C(1)) def test_all(self): stmt = lambda y, a: y.eq(a.all()) self.assertStatement(stmt, [C(0b00, 2)], C(0)) self.assertStatement(stmt, [C(0b01, 2)], C(0)) + self.assertStatement(stmt, [C(0b10, 2)], C(0)) self.assertStatement(stmt, [C(0b11, 2)], C(1)) - def test_xor(self): + def test_xor_unary(self): stmt = lambda y, a: y.eq(a.xor()) self.assertStatement(stmt, [C(0b00, 2)], C(0)) self.assertStatement(stmt, [C(0b01, 2)], C(1)) + self.assertStatement(stmt, [C(0b10, 2)], C(1)) self.assertStatement(stmt, [C(0b11, 2)], C(0)) def test_add(self): @@ -100,7 +103,7 @@ class SimulatorUnitTestCase(FHDLTestCase): stmt = lambda y, a, b: y.eq(a | b) self.assertStatement(stmt, [C(0b1100, 4), C(0b1010, 4)], C(0b1110, 4)) - def test_xor(self): + def test_xor_binary(self): stmt = lambda y, a, b: y.eq(a ^ b) self.assertStatement(stmt, [C(0b1100, 4), C(0b1010, 4)], C(0b0110, 4))