fhdl: compact Instance syntax
authorSebastien Bourdeauducq <sebastien@milkymist.org>
Thu, 25 Jul 2013 18:34:19 +0000 (20:34 +0200)
committerSebastien Bourdeauducq <sebastien@milkymist.org>
Thu, 25 Jul 2013 18:34:19 +0000 (20:34 +0200)
doc/fhdl.rst
examples/basic/arrays.py
migen/fhdl/specials.py

index 86cef407f9c83c3f1e67c5dd33c76118ff89a713..3d07472bb82805e3854e1181fa31a056b87af499 100644 (file)
@@ -139,10 +139,10 @@ A triplet (O, OE, I) of one-way signals defining a tri-state I/O port is represe
 The object that can be used in as a module special is ``Tristate``, and it behaves exactly like an instance of a tri-state I/O buffer that would be defined as follows: ::
 
   Instance("Tristate",
-    Instance.Inout("target", target),
-    Instance.Input("o", o),
-    Instance.Input("oe", oe),
-    Instance.Output("i", i)
+    io_target=target,
+    i_o=o,
+    i_oe=oe,
+    o_i=i
   )
 
 Signals ``target``, ``o`` and ``i`` can have any width, while ``oe`` is 1-bit wide. The ``target`` signal should go to a port and not be used elsewhere in the design. Like modern FPGA architectures, Migen does not support internal tri-states.
index 2822e197f0071c19142aa87e5f50e0102593ac68..8b10eaf9ca27449cf93d498cbae3d0d8d863f4b6 100644 (file)
@@ -21,6 +21,6 @@ class Example(Module):
 
                ina = Array(Signal() for a in range(dx))
                outa = Array(Signal() for a in range(dy))
-               self.specials += Instance("test", Instance.Output("O", outa[y]), Instance.Input("I", ina[x]))
+               self.specials += Instance("test", o_O=outa[y], i_I=ina[x])
 
 print(verilog.convert(Example()))
index 0f29f05109154dcca613080784b6af55f74ff40a..44d34473812565c9c24ee3334a28adca8783dd67 100644 (file)
@@ -1,3 +1,5 @@
+from operator import itemgetter
+
 from migen.fhdl.structure import *
 from migen.fhdl.size import bits_for, value_bits_sign
 from migen.fhdl.tools import *
@@ -68,15 +70,6 @@ class TSTriple:
                return Tristate(target, self.o, self.oe, self.i)
 
 class Instance(Special):
-       def __init__(self, of, *items, name=""):
-               Special.__init__(self)
-               self.of = of
-               if name:
-                       self.name_override = name
-               else:
-                       self.name_override = of
-               self.items = items
-       
        class _IO:
                def __init__(self, name, expr=None):
                        self.name = name
@@ -89,11 +82,28 @@ class Instance(Special):
                pass
        class InOut(_IO):
                pass
-
        class Parameter:
                def __init__(self, name, value):
                        self.name = name
                        self.value = value
+
+       def __init__(self, of, *items, name="", **kwargs):
+               Special.__init__(self)
+               self.of = of
+               if name:
+                       self.name_override = name
+               else:
+                       self.name_override = of
+               self.items = list(items)
+               for k, v in sorted(kwargs.items(), key=itemgetter(1)):
+                       item_type, item_name = k.split("_", maxsplit=1)
+                       item_class = {
+                               "i": Instance.Input,
+                               "o": Instance.Output,
+                               "io": Instance.InOut,
+                               "p": Instance.Parameter
+                       }[item_type]
+                       self.items.append(item_class(item_name, v))
        
        def get_io(self, name):
                for item in self.items: