Skip to content

Commit 05cb82b

Browse files
wanda-phiwhitequark
authored andcommitted
ast: fix const-castable expression handling in Signal(reset=).
The code to accept const-castable expressions was previously added in 0c4fda9, but it was untested and had a few bugs. Fixes #911.
1 parent 11d5bb1 commit 05cb82b

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

amaranth/hdl/ast.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,12 +1056,15 @@ def __init__(self, shape=None, *, name=None, reset=None, reset_less=False,
10561056
.format(orig_shape, Shape.cast(orig_shape),
10571057
reset.shape()))
10581058
else:
1059+
if reset is None:
1060+
reset = 0
10591061
try:
1060-
reset = Const.cast(reset or 0)
1062+
reset = Const.cast(reset)
10611063
except TypeError:
10621064
raise TypeError("Reset value must be a constant-castable expression, not {!r}"
10631065
.format(orig_reset))
1064-
if orig_reset not in (None, 0, -1): # Avoid false positives for all-zeroes and all-ones
1066+
# Avoid false positives for all-zeroes and all-ones
1067+
if orig_reset is not None and not (isinstance(orig_reset, int) and orig_reset in (0, -1)):
10651068
if reset.shape().signed and not self.signed:
10661069
warnings.warn(
10671070
message="Reset value {!r} is signed, but the signal shape is {!r}"

tests/test_hdl_ast.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,10 @@ def test_reset_enum(self):
10531053
r"not <StringEnum\.FOO: 'a'>$"):
10541054
Signal(1, reset=StringEnum.FOO)
10551055

1056+
def test_reset_const_castable(self):
1057+
s1 = Signal(4, reset=Cat(Const(0, 1), Const(1, 1), Const(0, 2)))
1058+
self.assertEqual(s1.reset, 2)
1059+
10561060
def test_reset_shape_castable_const(self):
10571061
class CastableFromHex(ShapeCastable):
10581062
def as_shape(self):

0 commit comments

Comments
 (0)