1 __all__
= ["Pins", "DiffPairs", "Subsignal", "Resource"]
5 def __init__(self
, names
, dir="io"):
6 if not isinstance(names
, str):
7 raise TypeError("Names must be a whitespace-separated string, not {!r}"
9 self
.names
= names
.split()
11 if dir not in ("i", "o", "io"):
12 raise TypeError("Direction must be one of \"i\", \"o\", \"oe\", or \"io\", not {!r}"
17 return len(self
.names
)
20 return iter(self
.names
)
23 return "(pins {} {})".format(self
.dir, " ".join(self
.names
))
27 def __init__(self
, p
, n
, dir="io"):
28 self
.p
= Pins(p
, dir=dir)
29 self
.n
= Pins(n
, dir=dir)
31 if len(self
.p
.names
) != len(self
.n
.names
):
32 raise TypeError("Positive and negative pins must have the same width, but {!r} "
34 .format(self
.p
, self
.n
))
39 return len(self
.p
.names
)
42 return zip(self
.p
.names
, self
.n
.names
)
45 return "(diffpairs {} (p {}) (n {}))".format(
46 self
.dir, " ".join(self
.p
.names
), " ".join(self
.n
.names
))
50 def __init__(self
, name
, *io
, extras
=None):
54 raise TypeError("Missing I/O constraints")
56 if not isinstance(c
, (Pins
, DiffPairs
, Subsignal
)):
57 raise TypeError("I/O constraint must be one of Pins, DiffPairs or Subsignal, "
60 if isinstance(io
[0], (Pins
, DiffPairs
)) and len(io
) > 1:
61 raise TypeError("Pins and DiffPairs cannot be followed by more I/O constraints, but "
62 "{!r} is followed by {!r}"
63 .format(io
[0], io
[1]))
64 if isinstance(io
[0], Subsignal
):
66 if not isinstance(c
, Subsignal
):
67 raise TypeError("A Subsignal can only be followed by more Subsignals, but "
68 "{!r} is followed by {!r}"
73 if extras
is not None:
74 if not isinstance(extras
, dict):
75 raise TypeError("Extra constraints must be a dict, not {!r}"
77 for extra_key
, extra_value
in extras
.items():
78 if not isinstance(extra_key
, str):
79 raise TypeError("Extra constraint key must be a string, not {!r}"
81 if not isinstance(extra_value
, str):
82 raise TypeError("Extra constraint value must be a string, not {!r}"
84 self
.extras
[extra_key
] = extra_value
86 if isinstance(self
.io
[0], Subsignal
):
88 sub
.extras
.update(self
.extras
)
91 return "(subsignal {} {} {})".format(self
.name
,
92 " ".join(map(repr, self
.io
)),
93 " ".join("{}={}".format(k
, v
)
94 for k
, v
in self
.extras
.items()))
97 class Resource(Subsignal
):
98 def __init__(self
, name
, number
, *io
, extras
=None):
99 super().__init
__(name
, *io
, extras
=extras
)
104 return "(resource {} {} {} {})".format(self
.name
, self
.number
,
105 " ".join(map(repr, self
.io
)),
106 " ".join("{}={}".format(k
, v
)
107 for k
, v
in self
.extras
.items()))