From b96eb339afd7ec6e7729984ba0c00ade35e5c754 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Thu, 25 Jul 2013 20:34:19 +0200 Subject: [PATCH] fhdl: compact Instance syntax --- doc/fhdl.rst | 8 ++++---- examples/basic/arrays.py | 2 +- migen/fhdl/specials.py | 30 ++++++++++++++++++++---------- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/doc/fhdl.rst b/doc/fhdl.rst index 86cef407..3d07472b 100644 --- a/doc/fhdl.rst +++ b/doc/fhdl.rst @@ -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. diff --git a/examples/basic/arrays.py b/examples/basic/arrays.py index 2822e197..8b10eaf9 100644 --- a/examples/basic/arrays.py +++ b/examples/basic/arrays.py @@ -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())) diff --git a/migen/fhdl/specials.py b/migen/fhdl/specials.py index 0f29f051..44d34473 100644 --- a/migen/fhdl/specials.py +++ b/migen/fhdl/specials.py @@ -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: -- 2.30.2