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 "