hdl._ast: add enum name argument to Format.Enum.

Turns out that RTLIL enum representation requires such, so add a place to store it.
This commit is contained in:
Wanda 2024-04-11 19:27:19 +02:00 committed by Catherine
parent 1fdd9bf4e9
commit 67f5b61bcc
4 changed files with 23 additions and 6 deletions

View file

@ -1740,7 +1740,7 @@ class FormatEnumTestCase(FHDLTestCase):
def test_construct(self):
a = Signal(3)
fmt = Format.Enum(a, {1: "A", 2: "B", 3: "C"})
self.assertRepr(fmt, "(format-enum (sig a) (1 'A') (2 'B') (3 'C'))")
self.assertRepr(fmt, "(format-enum (sig a) - (1 'A') (2 'B') (3 'C'))")
self.assertRepr(Format("{}", fmt), """
(format '{:s}' (switch-value (sig a)
(case 001 (const 8'd65))
@ -1755,8 +1755,8 @@ class FormatEnumTestCase(FHDLTestCase):
B = 3
C = 4
fmt = Format.Enum(a, MyEnum)
self.assertRepr(fmt, "(format-enum (sig a) (0 'A') (3 'B') (4 'C'))")
fmt = Format.Enum(a, MyEnum, name="MyEnum")
self.assertRepr(fmt, "(format-enum (sig a) 'MyEnum' (0 'A') (3 'B') (4 'C'))")
self.assertRepr(Format("{}", fmt), """
(format '{:s}' (switch-value (sig a)
(case 000 (const 8'd65))
@ -1774,6 +1774,9 @@ class FormatEnumTestCase(FHDLTestCase):
with self.assertRaisesRegex(TypeError,
r"^Variant names must be strings, not 123$"):
Format.Enum(a, {1: 123})
with self.assertRaisesRegex(TypeError,
r"^Enum name must be a string or None, not 123$"):
Format.Enum(a, {}, name=123)
class FormatStructTestCase(FHDLTestCase):

View file

@ -320,3 +320,13 @@ class EnumTestCase(FHDLTestCase):
A = 1
B = py_enum.member(2)
self.assertIs(2, EnumB.B.value)
def test_format(self):
class EnumA(Enum, shape=unsigned(2)):
A = 0
B = 1
sig = Signal(EnumA)
self.assertRepr(EnumA.format(sig, ""), """
(format-enum (sig sig) 'EnumA' (0 'A') (1 'B'))
""")