From: Sebastien Bourdeauducq Date: Thu, 8 Mar 2012 19:49:24 +0000 (+0100) Subject: fhdl: handle negative constants correctly X-Git-Tag: 24jan2021_ls180~2099^2~978 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=bfcd4e636be832e657bcf8639ccb68e29fd8d666;p=litex.git fhdl: handle negative constants correctly --- diff --git a/migen/fhdl/structure.py b/migen/fhdl/structure.py index 69f53f65..748ea059 100644 --- a/migen/fhdl/structure.py +++ b/migen/fhdl/structure.py @@ -8,7 +8,9 @@ def bits_for(n): if isinstance(n, Constant): return n.bv.width else: - if n == 0: + if n < 0: + return bits_for(-n) + 1 + elif n == 0: return 1 else: return int(math.ceil(math.log(n+1, 2))) @@ -118,7 +120,7 @@ class Replicate(Value): class Constant(Value): def __init__(self, n, bv=None): - self.bv = bv or BV(bits_for(n)) + self.bv = bv or BV(bits_for(n), n < 0) self.n = n def __repr__(self):