from collections import OrderedDict
-__all__ = ["Pins", "DiffPairs", "Attrs", "Clock", "Subsignal", "Resource", "Connector"]
+__all__ = ["Pins", "PinsN", "DiffPairs", "DiffPairsN",
+ "Attrs", "Clock", "Subsignal", "Resource", "Connector"]
class Pins:
- def __init__(self, names, *, dir="io", conn=None):
+ def __init__(self, names, *, dir="io", invert=False, conn=None, assert_width=None):
if not isinstance(names, str):
raise TypeError("Names must be a whitespace-separated string, not {!r}"
.format(names))
if conn is not None:
conn_name, conn_number = conn
- if not (isinstance(conn_name, str) and isinstance(conn_number, int)):
- raise TypeError("Connector must be None or a pair of string and integer, not {!r}"
+ if not (isinstance(conn_name, str) and isinstance(conn_number, (int, str))):
+ raise TypeError("Connector must be None or a pair of string (connector name) and "
+ "integer/string (connector number), not {!r}"
.format(conn))
names = ["{}_{}:{}".format(conn_name, conn_number, name) for name in names]
raise TypeError("Direction must be one of \"i\", \"o\", \"oe\", or \"io\", not {!r}"
.format(dir))
- self.names = names
- self.dir = dir
+ if assert_width is not None and len(names) != assert_width:
+ raise AssertionError("{} names are specified ({}), but {} names are expected"
+ .format(len(names), " ".join(names), assert_width))
+
+ self.names = names
+ self.dir = dir
+ self.invert = bool(invert)
def __len__(self):
return len(self.names)
return mapped_names
def __repr__(self):
- return "(pins {} {})".format(self.dir, " ".join(self.names))
+ return "(pins{} {} {})".format("-n" if self.invert else "",
+ self.dir, " ".join(self.names))
+
+
+def PinsN(*args, **kwargs):
+ return Pins(*args, invert=True, **kwargs)
class DiffPairs:
- def __init__(self, p, n, *, dir="io", conn=None):
- self.p = Pins(p, dir=dir, conn=conn)
- self.n = Pins(n, dir=dir, conn=conn)
+ def __init__(self, p, n, *, dir="io", invert=False, conn=None, assert_width=None):
+ self.p = Pins(p, dir=dir, conn=conn, assert_width=assert_width)
+ self.n = Pins(n, dir=dir, conn=conn, assert_width=assert_width)
if len(self.p.names) != len(self.n.names):
raise TypeError("Positive and negative pins must have the same width, but {!r} "
"and {!r} do not"
.format(self.p, self.n))
- self.dir = dir
+ self.dir = dir
+ self.invert = bool(invert)
def __len__(self):
return len(self.p.names)
return zip(self.p.names, self.n.names)
def __repr__(self):
- return "(diffpairs {} (p {}) (n {}))".format(
+ return "(diffpairs{} {} (p {}) (n {}))".format("-n" if self.invert else "",
self.dir, " ".join(self.p.names), " ".join(self.n.names))
+def DiffPairsN(*args, **kwargs):
+ return DiffPairs(*args, invert=True, **kwargs)
+
+
class Attrs(OrderedDict):
def __init__(self, **attrs):
- for attr_key, attr_value in attrs.items():
- if not isinstance(attr_value, str):
- raise TypeError("Attribute value must be a string, not {!r}"
- .format(attr_value))
+ for key, value in attrs.items():
+ if not (value is None or isinstance(value, (str, int)) or hasattr(value, "__call__")):
+ raise TypeError("Value of attribute {} must be None, int, str, or callable, "
+ "not {!r}"
+ .format(key, value))
super().__init__(**attrs)
def __repr__(self):
- return "(attrs {})".format(" ".join("{}={}".format(k, v)
- for k, v in self.items()))
+ items = []
+ for key, value in self.items():
+ if value is None:
+ items.append("!" + key)
+ else:
+ items.append(key + "=" + repr(value))
+ return "(attrs {})".format(" ".join(items))
class Clock:
class Resource(Subsignal):
+ @classmethod
+ def family(cls, name_or_number, number=None, *, ios, default_name, name_suffix=""):
+ # This constructor accepts two different forms:
+ # 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)`.
+ # The name_suffix argument is meant to support creating resources with
+ # similar names, such as spi_flash, spi_flash_2x, etc.
+ if name_suffix: # Only add "_" if we actually have a suffix.
+ name_suffix = "_" + name_suffix
+
+ if number is None: # name_or_number is number
+ return cls(default_name + name_suffix, name_or_number, *ios)
+ else: # name_or_number is name
+ return cls(name_or_number + name_suffix, number, *ios)
+
def __init__(self, name, number, *args):
super().__init__(name, *args)
class Connector:
- def __init__(self, name, number, io):
+ def __init__(self, name, number, io, *, conn=None):
self.name = name
self.number = number
- self.mapping = OrderedDict()
+ mapping = OrderedDict()
if isinstance(io, dict):
for conn_pin, plat_pin in io.items():
if not isinstance(plat_pin, str):
raise TypeError("Platform pin name must be a string, not {!r}"
.format(plat_pin))
- self.mapping[conn_pin] = plat_pin
+ mapping[conn_pin] = plat_pin
elif isinstance(io, str):
for conn_pin, plat_pin in enumerate(io.split(), start=1):
if plat_pin == "-":
continue
- self.mapping[str(conn_pin)] = plat_pin
+ mapping[str(conn_pin)] = plat_pin
else:
raise TypeError("Connector I/Os must be a dictionary or a string, not {!r}"
.format(io))
+ if conn is not None:
+ conn_name, conn_number = conn
+ if not (isinstance(conn_name, str) and isinstance(conn_number, (int, str))):
+ raise TypeError("Connector must be None or a pair of string (connector name) and "
+ "integer/string (connector number), not {!r}"
+ .format(conn))
+
+ for conn_pin, plat_pin in mapping.items():
+ mapping[conn_pin] = "{}_{}:{}".format(conn_name, conn_number, plat_pin)
+
+ self.mapping = mapping
+
def __repr__(self):
return "(connector {} {} {})".format(self.name, self.number,
" ".join("{}=>{}".format(conn, plat)