add redirection of __Mux__ to allow overrides for more advanced behaviour
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Mon, 27 Sep 2021 17:33:44 +0000 (18:33 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Mon, 27 Sep 2021 17:33:44 +0000 (18:33 +0100)
without changing fundamental language characteristics or semantics in nmigen
https://bugs.libre-soc.org/show_bug.cgi?id=458

nmigen/hdl/ast.py

index 685924ac2f9481e12cb2d9424db4e619ec71d14a..a2af8c744a772924b5489f78dbb83448b8a7e98d 100644 (file)
@@ -150,6 +150,9 @@ class Value(metaclass=ABCMeta):
         super().__init__()
         self.src_loc = tracer.get_src_loc(1 + src_loc_at)
 
+    def __Mux__(self, val1, val0):
+        return _InternalMux(self, val1, val0)
+
     def __bool__(self):
         raise TypeError("Attempted to convert nMigen value to Python boolean")
 
@@ -720,6 +723,13 @@ class Operator(Value):
 
 
 def Mux(sel, val1, val0):
+    sel = Value.cast(sel)
+    if len(sel) != 1:
+        sel = sel.bool()
+    return sel.__Mux__(val1, val0)
+
+
+def _InternalMux(sel, val1, val0):
     """Choose between two values.
 
     Parameters