Skip to content

Commit f965624

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 1cfefa6 commit f965624

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
@@ -2082,7 +2082,7 @@ def __init__(self, shape=None, *, name=None, init=None, reset=None, reset_less=F
20822082
.format(orig_init, shape),
20832083
category=SyntaxWarning,
20842084
stacklevel=2)
2085-
self._init = init.value
2085+
self._init = Const(init.value, shape).value
20862086
self._reset_less = bool(reset_less)
20872087

20882088
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
@@ -1264,6 +1264,14 @@ def test_init_wrong_too_wide(self):
12641264
r"^Initial value -2 will be truncated to the signal shape signed\(1\)$"):
12651265
Signal(signed(1), init=-2)
12661266

1267+
def test_init_truncated(self):
1268+
s1 = Signal(unsigned(2), init=-1)
1269+
self.assertEqual(s1.init, 0b11)
1270+
with warnings.catch_warnings():
1271+
warnings.filterwarnings(action="ignore", category=SyntaxWarning)
1272+
s2 = Signal(signed(2), init=-33)
1273+
self.assertEqual(s2.init, -1)
1274+
12671275
def test_init_wrong_fencepost(self):
12681276
with self.assertRaisesRegex(SyntaxError,
12691277
r"^Initial value 10 equals the non-inclusive end of the signal shape "

0 commit comments

Comments
 (0)