Skip to content

hdl: truncate init value read from Signal(...).init #1449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion amaranth/hdl/_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
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 @@ -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 "
Expand Down