hdl.ast: fix signed Const normalization.

Fixes #805.
This commit is contained in:
Marcelina Kościelnicka 2023-06-07 12:18:13 +02:00 committed by Catherine
parent 0681cb77b1
commit 656db317d2
2 changed files with 3 additions and 1 deletions

View file

@ -647,7 +647,7 @@ class Const(Value):
shape = Shape.cast(shape, src_loc_at=1 + src_loc_at)
self.width = shape.width
self.signed = shape.signed
if self.signed and self.value >> (self.width - 1):
if self.signed and self.value >> (self.width - 1) & 1:
self.value |= -(1 << self.width)
else:
self.value &= (1 << self.width) - 1

View file

@ -369,6 +369,8 @@ class ConstTestCase(FHDLTestCase):
def test_normalization(self):
self.assertEqual(Const(0b10110, signed(5)).value, -10)
self.assertEqual(Const(0b10000, signed(4)).value, 0)
self.assertEqual(Const(-16, 4).value, 0)
def test_value(self):
self.assertEqual(Const(10).value, 10)