fhdl: handle negative constants correctly
authorSebastien Bourdeauducq <sebastien@milkymist.org>
Thu, 8 Mar 2012 19:49:24 +0000 (20:49 +0100)
committerSebastien Bourdeauducq <sebastien@milkymist.org>
Thu, 8 Mar 2012 19:49:24 +0000 (20:49 +0100)
migen/fhdl/structure.py

index 69f53f65d845b924709b4f6dd0f469a550c7b198..748ea059de70f9745453d7158581df8cbdc6d824 100644 (file)
@@ -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):