Repository: amaranth-lang/amaranth
Pull request #1136 (2024-02-13 23:11:43)

sim: fix using 0-width `Switch`.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1136?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`353a8ce`)](https://app.codecov.io/gh/amaranth-lang/amaranth/commit/353a8ce7e313e8d203635151402ef88a0171bf45?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) 85.47% compared to head [(`1fbc132`)](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1136?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) 85.46%.


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main    #1136      +/-   ##
==========================================
- Coverage   85.47%   85.46%   -0.02%     
==========================================
  Files          42       42              
  Lines        8358     8358              
  Branches     1973     1973              
==========================================
- Hits         7144     7143       -1     
  Misses       1017     1017              
- Partials      197      198       +1     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1136?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang).


Patch:
diff --git a/amaranth/sim/_pyrtl.py b/amaranth/sim/_pyrtl.py
index b18f99f..7e52eea 100644
--- a/amaranth/sim/_pyrtl.py
+++ b/amaranth/sim/_pyrtl.py
@@ -378,7 +378,7 @@ class _StatementCompiler(StatementVisitor, _Compiler):
                         value = int("".join("0" if b == "-" else  b  for b in pattern), 2)
                         gen_checks.append(f"{value} == ({mask} & {gen_test})")
                     else:
-                        value = int(pattern, 2)
+                        value = int(pattern or "0", 2)
                         gen_checks.append(f"{value} == {gen_test}")
             if index == 0:
                 self.emitter.append(f"if {' or '.join(gen_checks)}:")
diff --git a/pyproject.toml b/pyproject.toml
index 7049cae..ed58a02 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -57,7 +57,7 @@ source-includes = [
 
 [tool.pdm.dev-dependencies]
 test = [
-  "yowasp-yosys!=0.37.0.*",
+  "yowasp-yosys>=0.38",
   "coverage",
 ]
 docs = [


Test patch:
diff --git a/tests/test_sim.py b/tests/test_sim.py
index 01e55d3..7950fe6 100644
--- a/tests/test_sim.py
+++ b/tests/test_sim.py
@@ -1064,6 +1064,18 @@ class SimulatorIntegrationTestCase(FHDLTestCase):
         m.d.comb += a.eq(op)
         Simulator(m)
 
+    def test_switch_zero(self):
+        m = Module()
+        a = Signal(0)
+        o = Signal()
+        with m.Switch(a):
+            with m.Case(""):
+                m.d.comb += o.eq(1)
+        with self.assertSimulation(m) as sim:
+            def process():
+                self.assertEqual((yield o), 1)
+            sim.add_testbench(process)
+
 
 class SimulatorRegressionTestCase(FHDLTestCase):
     def test_bug_325(self):