Skip to content

Commit

Permalink
hdl.ir, back.rtlil: allow specifying attributes on instances.
Browse files Browse the repository at this point in the history
Fixes #107.
  • Loading branch information
whitequark committed Jun 28, 2019
1 parent 2b92f12 commit 48d4ee4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
9 changes: 6 additions & 3 deletions nmigen/back/rtlil.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ def memory(self, width, size, name=None, src=""):
self._append(" memory width {} size {} {}\n", width, size, name)
return name

def cell(self, kind, name=None, params={}, ports={}, src=""):
def cell(self, kind, name=None, params={}, ports={}, attrs={}, src=""):
self._src(src)
name = self._make_name(name, local=False)
for attr_name, attr_value in attrs.items():
self.attribute(attr_name, attr_value)
self._append(" cell {} {}\n", kind, name)
for param, value in params.items():
if isinstance(value, str):
Expand Down Expand Up @@ -678,7 +680,7 @@ def convert_fragment(builder, fragment, hierarchy):
return "\\{}".format(fragment.type), port_map

module_name = hierarchy[-1] or "anonymous"
module_attrs = {}
module_attrs = OrderedDict()
if len(hierarchy) == 1:
module_attrs["top"] = 1
module_attrs["nmigen.hierarchy"] = ".".join(name or "anonymous" for name in hierarchy)
Expand Down Expand Up @@ -761,7 +763,8 @@ def convert_fragment(builder, fragment, hierarchy):
compiler_state.resolve_curr(signal, prefix=sub_name)
sub_ports[port] = rhs_compiler(value)

module.cell(sub_type, name=sub_name, ports=sub_ports, params=sub_params)
module.cell(sub_type, name=sub_name, ports=sub_ports, params=sub_params,
attrs=subfragment.attrs)

# If we emit all of our combinatorial logic into a single RTLIL process, Verilog
# simulators will break horribly, because Yosys write_verilog transforms RTLIL processes
Expand Down
9 changes: 7 additions & 2 deletions nmigen/hdl/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def __init__(self):
self.statements = []
self.domains = OrderedDict()
self.subfragments = []
self.attrs = OrderedDict()
self.generated = OrderedDict()
self.flatten = False

Expand Down Expand Up @@ -528,7 +529,9 @@ def __init__(self, type, *args, **kwargs):
self.named_ports = OrderedDict()

for (kind, name, value) in args:
if kind == "p":
if kind == "a":
self.attrs[name] = value
elif kind == "p":
self.parameters[name] = value
elif kind in ("i", "o", "io"):
self.named_ports[name] = (value, kind)
Expand All @@ -538,7 +541,9 @@ def __init__(self, type, *args, **kwargs):
.format((kind, name, value)))

for kw, arg in kwargs.items():
if kw.startswith("p_"):
if kw.startswith("a_"):
self.attrs[kw[2:]] = arg
elif kw.startswith("p_"):
self.parameters[kw[2:]] = arg
elif kw.startswith("i_"):
self.named_ports[kw[2:]] = (arg, "i")
Expand Down
1 change: 1 addition & 0 deletions nmigen/hdl/xfrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ def on_fragment(self, fragment):
else:
new_fragment = Fragment()
new_fragment.flatten = fragment.flatten
new_fragment.attrs = OrderedDict(fragment.attrs)
self.map_ports(fragment, new_fragment)
self.map_subfragments(fragment, new_fragment)
self.map_domains(fragment, new_fragment)
Expand Down
14 changes: 14 additions & 0 deletions nmigen/test/test_hdl_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,15 +555,21 @@ def test_construct(self):
s5 = Signal()
s6 = Signal()
inst = Instance("foo",
("a", "ATTR1", 1),
("p", "PARAM1", 0x1234),
("i", "s1", s1),
("o", "s2", s2),
("io", "s3", s3),
a_ATTR2=2,
p_PARAM2=0x5678,
i_s4=s4,
o_s5=s5,
io_s6=s6,
)
self.assertEqual(inst.attrs, OrderedDict([
("ATTR1", 1),
("ATTR2", 2),
]))
self.assertEqual(inst.parameters, OrderedDict([
("PARAM1", 0x1234),
("PARAM2", 0x5678),
Expand Down Expand Up @@ -648,3 +654,11 @@ def test_prepare_slice_in_port(self):
self.assertEqual(fp.ports, SignalDict([
(s, "o"),
]))

def test_prepare_attrs(self):
self.setUp_cpu()
self.inst.attrs["ATTR"] = 1
f = self.inst.prepare()
self.assertEqual(f.attrs, OrderedDict([
("ATTR", 1),
]))

0 comments on commit 48d4ee4

Please sign in to comment.