hdl.ast: accept any constant-castable expression in Signal(reset=).

See amaranth-lang/rfcs#4.

This functionality was not explicitly specified in the RFC but it
falls under "anywhere an integer or an enumeration is accepted".
This commit is contained in:
Catherine 2023-03-03 06:20:34 +00:00
parent f77a335abf
commit 0c4fda92fe
4 changed files with 43 additions and 29 deletions

View file

@ -986,20 +986,25 @@ class SignalTestCase(FHDLTestCase):
s1 = Signal(2, reset=UnsignedEnum.BAR)
self.assertEqual(s1.reset, 2)
with self.assertRaisesRegex(TypeError,
r"^Reset value has to be an int or an integral Enum$"
):
r"^Reset value must be a constant-castable expression, "
r"not <StringEnum\.FOO: 'a'>$"):
Signal(1, reset=StringEnum.FOO)
def test_reset_narrow(self):
def test_reset_signed_mismatch(self):
with self.assertWarnsRegex(SyntaxWarning,
r"^Reset value 8 requires 4 bits to represent, but the signal only has 3 bits$"):
Signal(3, reset=8)
r"^Reset value -2 is signed, but the signal shape is unsigned\(2\)$"):
Signal(unsigned(2), reset=-2)
def test_reset_wrong_too_wide(self):
with self.assertWarnsRegex(SyntaxWarning,
r"^Reset value 4 requires 4 bits to represent, but the signal only has 3 bits$"):
Signal(signed(3), reset=4)
r"^Reset value 2 will be truncated to the signal shape unsigned\(1\)$"):
Signal(unsigned(1), reset=2)
with self.assertWarnsRegex(SyntaxWarning,
r"^Reset value -5 requires 4 bits to represent, but the signal only has 3 bits$"):
Signal(signed(3), reset=-5)
r"^Reset value 1 will be truncated to the signal shape signed\(1\)$"):
Signal(signed(1), reset=1)
with self.assertWarnsRegex(SyntaxWarning,
r"^Reset value -2 will be truncated to the signal shape signed\(1\)$"):
Signal(signed(1), reset=-2)
def test_attrs(self):
s1 = Signal()

View file

@ -54,25 +54,25 @@ class EnumTestCase(FHDLTestCase):
def test_shape_explicit_wrong_signed_mismatch(self):
with self.assertWarnsRegex(SyntaxWarning,
r"^Value of enumeration member <EnumA\.A: -1> is signed, but enumeration "
r"^Value of enumeration member <EnumA\.A: -1> is signed, but the enumeration "
r"shape is unsigned\(1\)$"):
class EnumA(Enum, shape=unsigned(1)):
A = -1
def test_shape_explicit_wrong_too_wide(self):
with self.assertWarnsRegex(SyntaxWarning,
r"^Value of enumeration member <EnumA\.A: 2> will be truncated to enumeration "
r"^Value of enumeration member <EnumA\.A: 2> will be truncated to the enumeration "
r"shape unsigned\(1\)$"):
class EnumA(Enum, shape=unsigned(1)):
A = 2
with self.assertWarnsRegex(SyntaxWarning,
r"^Value of enumeration member <EnumB\.A: 1> will be truncated to enumeration "
r"^Value of enumeration member <EnumB\.A: 1> will be truncated to the enumeration "
r"shape signed\(1\)$"):
class EnumB(Enum, shape=signed(1)):
A = 1
with self.assertWarnsRegex(SyntaxWarning,
r"^Value of enumeration member <EnumC\.A: -2> will be truncated to enumeration "
r"shape signed\(1\)$"):
r"^Value of enumeration member <EnumC\.A: -2> will be truncated to the "
r"enumeration shape signed\(1\)$"):
class EnumC(Enum, shape=signed(1)):
A = -2