From ac5271e80e014171e039236e1107b80654ae12b7 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Mon, 19 Oct 2015 20:08:46 +0800 Subject: [PATCH] sim: truncate case test value --- migen/sim/core.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/migen/sim/core.py b/migen/sim/core.py index 8ad699136..fa571ff38 100644 --- a/migen/sim/core.py +++ b/migen/sim/core.py @@ -5,6 +5,7 @@ from migen.fhdl.structure import * from migen.fhdl.structure import (_Value, _Statement, _Operator, _Slice, _ArrayProxy, _Assign, _Fragment) +from migen.fhdl.bitcontainer import value_bits_sign from migen.fhdl.tools import list_signals, list_targets, insert_resets from migen.fhdl.simplify import MemoryToArray from migen.fhdl.specials import _MemoryLocation @@ -75,6 +76,13 @@ str2op = { } +def _truncate(value, nbits, signed): + value = value & (2**nbits - 1) + if signed and (value & 2**(nbits - 1)): + value -= 2**nbits + return value + + class Evaluator: def __init__(self, clock_domains, replaced_memories): self.clock_domains = clock_domains @@ -152,10 +160,8 @@ class Evaluator: def assign(self, node, value): if isinstance(node, Signal): assert not node.variable - value = value & (2**node.nbits - 1) - if node.signed and (value & 2**(node.nbits - 1)): - value -= 2**node.nbits - self.modifications[node] = value + self.modifications[node] = _truncate(value, + node.nbits, node.signed) elif isinstance(node, Cat): for element in node.l: nbits = len(element) @@ -187,7 +193,8 @@ class Evaluator: else: self.execute(s.f) elif isinstance(s, Case): - test = self.eval(s.test) + nbits, signed = value_bits_sign(s.test) + test = _truncate(self.eval(s.test), nbits, signed) for k, v in s.cases.items(): if isinstance(k, Constant) and k.value == test: self.execute(v)