From 9c698bee4f55a5cd2c5bff427c5e8d2bc58f095f Mon Sep 17 00:00:00 2001 From: whitequark Date: Fri, 20 Sep 2019 10:12:59 +0000 Subject: [PATCH] back.pysim: fix simulation of Value.xor(). --- nmigen/back/pysim.py | 2 +- nmigen/test/test_sim.py | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) 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)) -- 2.30.2