Skip to content

Commit

Permalink
hdl.ast: cast Mux() selector to bool if it is not a 1-bit value.
Browse files Browse the repository at this point in the history
Fixes #232.
  • Loading branch information
whitequark committed Sep 23, 2019
1 parent 7777b7b commit b227352
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions nmigen/hdl/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,8 @@ def Mux(sel, val1, val0):
Value, out
Output ``Value``. If ``sel`` is asserted, the Mux returns ``val1``, else ``val0``.
"""
if len(sel) != 1:
sel = Value.wrap(sel).bool()
return Operator("m", [sel, val1, val0])


Expand Down
5 changes: 5 additions & 0 deletions nmigen/test/test_hdl_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ def test_mux(self):
v4 = Mux(s, Const(0, (4, False)), Const(0, (4, True)))
self.assertEqual(v4.shape(), (5, True))

def test_mux_wide(self):
s = Const(0b100)
v = Mux(s, Const(0, (4, False)), Const(0, (6, False)))
self.assertEqual(repr(v), "(m (b (const 3'd4)) (const 4'd0) (const 6'd0))")

def test_bool(self):
v = Const(0).bool()
self.assertEqual(repr(v), "(b (const 1'd0))")
Expand Down

0 comments on commit b227352

Please sign in to comment.