Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: m-labs/nmigen
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: fb0185437260
Choose a base ref
...
head repository: m-labs/nmigen
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: b8a61edc2fc3
Choose a head ref
  • 2 commits
  • 4 files changed
  • 1 contributor

Commits on Jun 3, 2019

  1. hdl.ir: accept expanded (kind, name, value) tuples in Instance.

    This is useful for e.g. programmatically generating parameters
    without having to mess with kwargs dicts.
    whitequark committed Jun 3, 2019
    Copy the full SHA
    b64a312 View commit details
  2. Copy the full SHA
    b8a61ed View commit details
Showing with 67 additions and 3 deletions.
  1. +3 −0 nmigen/hdl/dsl.py
  2. +14 −3 nmigen/hdl/ir.py
  3. +6 −0 nmigen/test/test_hdl_dsl.py
  4. +44 −0 nmigen/test/test_hdl_ir.py
3 changes: 3 additions & 0 deletions nmigen/hdl/dsl.py
Original file line number Diff line number Diff line change
@@ -83,6 +83,9 @@ def __iadd__(self, modules):
def __setattr__(self, name, submodule):
self._builder._add_submodule(submodule, name)

def __setitem__(self, name, value):
return self.__setattr__(name, value)


class _ModuleBuilderDomainSet:
def __init__(self, builder):
17 changes: 14 additions & 3 deletions nmigen/hdl/ir.py
Original file line number Diff line number Diff line change
@@ -517,13 +517,23 @@ def prepare(self, ports=None, ensure_sync_exists=True):


class Instance(Fragment):
def __init__(self, type, **kwargs):
def __init__(self, type, *args, **kwargs):
super().__init__()

self.type = type
self.parameters = OrderedDict()
self.named_ports = OrderedDict()

for (kind, name, value) in args:
if kind == "p":
self.parameters[name] = value
elif kind in ("i", "o", "io"):
self.named_ports[name] = (value, kind)
else:
raise NameError("Instance argument {!r} should be a tuple (kind, name, value) "
"where kind is one of \"p\", \"i\", \"o\", or \"io\""
.format((kind, name, value)))

for kw, arg in kwargs.items():
if kw.startswith("p_"):
self.parameters[kw[2:]] = arg
@@ -534,5 +544,6 @@ def __init__(self, type, **kwargs):
elif kw.startswith("io_"):
self.named_ports[kw[3:]] = (arg, "io")
else:
raise NameError("Instance argument '{}' does not start with p_, i_, o_, or io_"
.format(arg))
raise NameError("Instance keyword argument {}={!r} does not start with one of "
"\"p_\", \"i_\", \"o_\", or \"io_\""
.format(kw, arg))
6 changes: 6 additions & 0 deletions nmigen/test/test_hdl_dsl.py
Original file line number Diff line number Diff line change
@@ -505,6 +505,12 @@ def test_submodule_named(self):
m1.submodules.foo = m2
self.assertEqual(m1._submodules, [(m2, "foo")])

def test_submodule_named_index(self):
m1 = Module()
m2 = Module()
m1.submodules["foo"] = m2
self.assertEqual(m1._submodules, [(m2, "foo")])

def test_submodule_wrong(self):
m = Module()
with self.assertRaises(TypeError,
44 changes: 44 additions & 0 deletions nmigen/test/test_hdl_ir.py
Original file line number Diff line number Diff line change
@@ -547,6 +547,50 @@ def test_explicit_flatten(self):


class InstanceTestCase(FHDLTestCase):
def test_construct(self):
s1 = Signal()
s2 = Signal()
s3 = Signal()
s4 = Signal()
s5 = Signal()
s6 = Signal()
inst = Instance("foo",
("p", "PARAM1", 0x1234),
("i", "s1", s1),
("o", "s2", s2),
("io", "s3", s3),
p_PARAM2=0x5678,
i_s4=s4,
o_s5=s5,
io_s6=s6,
)
self.assertEqual(inst.parameters, OrderedDict([
("PARAM1", 0x1234),
("PARAM2", 0x5678),
]))
self.assertEqual(inst.named_ports, OrderedDict([
("s1", (s1, "i")),
("s2", (s2, "o")),
("s3", (s3, "io")),
("s4", (s4, "i")),
("s5", (s5, "o")),
("s6", (s6, "io")),
]))

def test_wrong_construct_arg(self):
s = Signal()
with self.assertRaises(NameError,
msg="Instance argument ('', 's1', (sig s)) should be a tuple "
"(kind, name, value) where kind is one of \"p\", \"i\", \"o\", or \"io\""):
Instance("foo", ("", "s1", s))

def test_wrong_construct_kwarg(self):
s = Signal()
with self.assertRaises(NameError,
msg="Instance keyword argument x_s1=(sig s) does not start with one of "
"\"p_\", \"i_\", \"o_\", or \"io_\""):
Instance("foo", x_s1=s)

def setUp_cpu(self):
self.rst = Signal()
self.stb = Signal()