Skip to content

Commit cde68fb

Browse files
committed
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.
1 parent 499c20e commit cde68fb

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

amaranth/hdl/_ast.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2052,7 +2052,7 @@ def __init__(self, shape=None, *, name=None, init=None, reset=None, reset_less=F
20522052
.format(orig_init, shape),
20532053
category=SyntaxWarning,
20542054
stacklevel=2)
2055-
self._init = init.value
2055+
self._init = Const(init.value, shape).value
20562056
self._reset_less = bool(reset_less)
20572057

20582058
if isinstance(orig_shape, range) and orig_init is not None and orig_init not in orig_shape:

tests/test_hdl_ast.py

+8
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,14 @@ def test_init_wrong_too_wide(self):
12451245
r"^Initial value -2 will be truncated to the signal shape signed\(1\)$"):
12461246
Signal(signed(1), init=-2)
12471247

1248+
def test_init_truncated(self):
1249+
s1 = Signal(unsigned(2), init=-1)
1250+
self.assertEqual(s1.init, 0b11)
1251+
with warnings.catch_warnings():
1252+
warnings.filterwarnings(action="ignore", category=SyntaxWarning)
1253+
s2 = Signal(signed(2), init=-33)
1254+
self.assertEqual(s2.init, -1)
1255+
12481256
def test_init_wrong_fencepost(self):
12491257
with self.assertRaisesRegex(SyntaxError,
12501258
r"^Initial value 10 equals the non-inclusive end of the signal shape "

0 commit comments

Comments
 (0)