Skip to content

Commit

Permalink
hdl: truncate init value read from Signal(...).init.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
whitequark committed Jul 19, 2024
1 parent 1cfefa6 commit f965624
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion amaranth/hdl/_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -2082,7 +2082,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:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_hdl_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,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 "
Expand Down

0 comments on commit f965624

Please sign in to comment.