diff --git a/amaranth/hdl/ast.py b/amaranth/hdl/ast.py index 2ed8801..e0a0c4f 100644 --- a/amaranth/hdl/ast.py +++ b/amaranth/hdl/ast.py @@ -727,7 +727,8 @@ class Const(Value): width = 0 for part in obj.parts: const = Const.cast(part) - value |= const.value << width + part_value = Const(const.value, unsigned(const.width)).value + value |= part_value << width width += len(const) return Const(value, width) else: diff --git a/tests/test_hdl_ast.py b/tests/test_hdl_ast.py index 625e0e8..be28c7a 100644 --- a/tests/test_hdl_ast.py +++ b/tests/test_hdl_ast.py @@ -935,6 +935,16 @@ class CatTestCase(FHDLTestCase): r"specify the width explicitly using C\(2, 2\)$"): Cat(2) + def test_const(self): + a = Const.cast(Cat(Const(1, 1), Const(0, 1), Const(3, 2), Const(2, 2))) + self.assertEqual(a.value, 0x2d) + self.assertEqual(a.width, 6) + self.assertEqual(a.signed, False) + a = Const.cast(Cat(Const(-4, 8), Const(-3, 8))) + self.assertEqual(a.value, 0xfdfc) + self.assertEqual(a.width, 16) + self.assertEqual(a.signed, False) + class ReplTestCase(FHDLTestCase): @_ignore_deprecated