From 8432aab597bc0ced32d888c980e67c5d3759c1be Mon Sep 17 00:00:00 2001 From: Catherine Date: Fri, 19 Jul 2024 01:10:59 +0000 Subject: [PATCH] hdl: truncate init value read from `Signal(...).init`. We have a warning saying that the init value will be truncated to fit the signal's shape (suppressed for common patterns of 0 and -1), but the introspected via `.init` value is, confusingly, not truncated. --- amaranth/hdl/_ast.py | 2 +- tests/test_hdl_ast.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/amaranth/hdl/_ast.py b/amaranth/hdl/_ast.py index a490707bf..1293f871d 100644 --- a/amaranth/hdl/_ast.py +++ b/amaranth/hdl/_ast.py @@ -2052,7 +2052,7 @@ def __init__(self, shape=None, *, name=None, init=None, reset=None, reset_less=F .format(orig_init, shape), category=SyntaxWarning, stacklevel=2) - self._init = init.value + self._init = Const(init.value, shape).value self._reset_less = bool(reset_less) if isinstance(orig_shape, range) and orig_init is not None and orig_init not in orig_shape: diff --git a/tests/test_hdl_ast.py b/tests/test_hdl_ast.py index d74ad773d..85d9ecfb6 100644 --- a/tests/test_hdl_ast.py +++ b/tests/test_hdl_ast.py @@ -1245,6 +1245,14 @@ def test_init_wrong_too_wide(self): r"^Initial value -2 will be truncated to the signal shape signed\(1\)$"): Signal(signed(1), init=-2) + def test_init_truncated(self): + s1 = Signal(unsigned(2), init=-1) + self.assertEqual(s1.init, 0b11) + with warnings.catch_warnings(): + warnings.filterwarnings(action="ignore", category=SyntaxWarning) + s2 = Signal(signed(2), init=-33) + self.assertEqual(s2.init, -1) + def test_init_wrong_fencepost(self): with self.assertRaisesRegex(SyntaxError, r"^Initial value 10 equals the non-inclusive end of the signal shape "