Skip to content

Commit e3324e1

Browse files
wanda-phiwhitequark
authored andcommitted
hdl._dsl: fix using 0-width Switch with integer keys.
Fixes #1133.
1 parent 5ffb48b commit e3324e1

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

amaranth/hdl/_dsl.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ def Case(self, *patterns):
328328
"expression, not {!r}"
329329
.format(pattern)) from e
330330
pattern_len = bits_for(pattern.value)
331+
if pattern.value == 0:
332+
pattern_len = 0
331333
if pattern_len > len(switch_data["test"]):
332334
warnings.warn("Case pattern '{!r}' ({}'{:b}) is wider than switch value "
333335
"(which has width {}); comparison will never be true"

tests/test_hdl_dsl.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,21 @@ class Color(Enum):
502502
m.d.comb += dummy.eq(0)
503503
self.assertEqual(m._statements, {})
504504

505+
def test_Switch_zero_width(self):
506+
m = Module()
507+
s = Signal(0)
508+
with m.Switch(s):
509+
with m.Case(0):
510+
m.d.comb += self.c1.eq(1)
511+
m._flush()
512+
self.assertRepr(m._statements["comb"], """
513+
(
514+
(switch (sig s)
515+
(case (eq (sig c1) (const 1'd1)))
516+
)
517+
)
518+
""")
519+
505520
def test_Case_bits_wrong(self):
506521
m = Module()
507522
with m.Switch(self.w1):

0 commit comments

Comments
 (0)