Skip to content

Commit

Permalink
build.dsl: add Resource.family abstraction.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Jul 9, 2019
1 parent 7b4fbf8 commit 367ad5a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
14 changes: 14 additions & 0 deletions nmigen/build/dsl.py
Expand Up @@ -181,6 +181,20 @@ def __repr__(self):


class Resource(Subsignal):
@classmethod
def family(cls, name_or_number, number=None, *, ios, default_name):
# This constructor accepts two different forms:

This comment has been minimized.

Copy link
@mithro

mithro Jul 9, 2019

Should this be a docstring rather than a comment like this?

This comment has been minimized.

Copy link
@whitequark

whitequark Jul 9, 2019

Author Contributor

There's not many docstrings in nmigen.build, so it'll be a part of some later effort to write coherent docs for it.

# 1. Number-only form:
# Resource.family(0, default_name="name", ios=[Pins("A0 A1")])
# 2. Name-and-number (name override) form:
# Resource.family("override", 0, default_name="name", ios=...)
# This makes it easier to build abstractions for resources, e.g. an SPIResource abstraction
# could simply delegate to `Resource.family(*args, default_name="spi", ios=ios)`.
if number is None: # name_or_number is number
return cls(default_name, name_or_number, *ios)
else: # name_or_number is name
return cls(name_or_number, number, *ios)

def __init__(self, name, number, *args):
super().__init__(name, *args)

Expand Down
9 changes: 9 additions & 0 deletions nmigen/test/test_build_dsl.py
Expand Up @@ -225,6 +225,15 @@ def test_basic(self):
" (subsignal rx (pins i A1))"
" (attrs IOSTANDARD=LVCMOS33))")

def test_family(self):
ios = [Subsignal("clk", Pins("A0", dir="o"))]
r1 = Resource.family(0, default_name="spi", ios=ios)
r2 = Resource.family("spi_flash", 0, default_name="spi", ios=ios)
self.assertEqual(r1.name, "spi")
self.assertEqual(r1.ios, ios)
self.assertEqual(r2.name, "spi_flash")
self.assertEqual(r2.ios, ios)


class ConnectorTestCase(FHDLTestCase):
def test_string(self):
Expand Down

0 comments on commit 367ad5a

Please sign in to comment.