|
1 |
| -from .. import * |
| 1 | +import enum |
| 2 | + |
| 3 | +from ..hdl import * |
2 | 4 | from ..lib import wiring
|
3 | 5 | from ..lib.wiring import In, Out
|
4 | 6 | from .. import tracer
|
5 | 7 |
|
6 | 8 |
|
7 |
| -__all__ = ["Pin"] |
| 9 | +__all__ = ["Direction", "SingleEndedPort", "DifferentialPort", "Pin"] |
| 10 | + |
| 11 | + |
| 12 | +class Direction(enum.Enum): |
| 13 | + Input = "i" |
| 14 | + Output = "o" |
| 15 | + Bidir = "io" |
| 16 | + |
| 17 | + def __or__(self, other): |
| 18 | + if not isinstance(other, Direction): |
| 19 | + return NotImplemented |
| 20 | + if self == other: |
| 21 | + return self |
| 22 | + else: |
| 23 | + return Direction.Bidir |
| 24 | + |
| 25 | + |
| 26 | +class SingleEndedPort: |
| 27 | + """TODO |
| 28 | + """ |
| 29 | + def __init__(self, io, *, invert=False, direction=Direction.Bidir): |
| 30 | + self._io = IOValue.cast(io) |
| 31 | + if isinstance(invert, bool): |
| 32 | + self._invert = (invert,) * len(self._io) |
| 33 | + else: |
| 34 | + self._invert = tuple(invert) |
| 35 | + if len(self._invert) != len(self._io): |
| 36 | + raise ValueError(f"Length of 'invert' ({len(self._invert)}) must match " |
| 37 | + f"length of 'io' ({len(self._io)}).") |
| 38 | + if not all(isinstance(item, bool) for item in self._invert): |
| 39 | + raise ValueError(f"'invert' must be a bool or iterable of bool, not {invert!r}") |
| 40 | + self._direction = Direction(direction) |
| 41 | + |
| 42 | + @property |
| 43 | + def io(self): |
| 44 | + return self._io |
| 45 | + |
| 46 | + @property |
| 47 | + def invert(self): |
| 48 | + return self._invert |
| 49 | + |
| 50 | + @property |
| 51 | + def direction(self): |
| 52 | + return self._direction |
| 53 | + |
| 54 | + def __len__(self): |
| 55 | + return len(self._io) |
| 56 | + |
| 57 | + def __invert__(self): |
| 58 | + return SingleEndedPort(self._io, invert=tuple(~inv for inv in self._invert), |
| 59 | + direction=self._direction) |
| 60 | + |
| 61 | + def __getitem__(self, index): |
| 62 | + return SingleEndedPort(self._io[index], invert=self._invert[index], |
| 63 | + direction=self._direction) |
| 64 | + |
| 65 | + def __add__(self, other): |
| 66 | + if not isinstance(other, SingleEndedPort): |
| 67 | + return NotImplemented |
| 68 | + return SingleEndedPort(Cat(self._io, other._io), invert=self._invert + other._invert, |
| 69 | + direction=self._direction | other._direction) |
| 70 | + |
| 71 | + |
| 72 | +class DifferentialPort: |
| 73 | + """TODO |
| 74 | + """ |
| 75 | + def __init__(self, p, n, *, invert=False, direction=Direction.Bidir): |
| 76 | + self._p = IOValue.cast(p) |
| 77 | + self._n = IOValue.cast(n) |
| 78 | + if len(self._p) != len(self._n): |
| 79 | + raise ValueError(f"Length of 'p' ({len(self._p)}) must match length of 'n' " |
| 80 | + f"({len(self._n)})") |
| 81 | + if isinstance(invert, bool): |
| 82 | + self._invert = (invert,) * len(self._p) |
| 83 | + else: |
| 84 | + self._invert = tuple(invert) |
| 85 | + if len(self._invert) != len(self._p): |
| 86 | + raise ValueError(f"Length of 'invert' ({len(self._invert)}) must match " |
| 87 | + f"length of 'p' ({len(self._p)}).") |
| 88 | + if not all(isinstance(item, bool) for item in self._invert): |
| 89 | + raise ValueError(f"'invert' must be a bool or iterable of bool, not {invert!r}") |
| 90 | + self._direction = Direction(direction) |
| 91 | + |
| 92 | + @property |
| 93 | + def p(self): |
| 94 | + return self._p |
| 95 | + |
| 96 | + @property |
| 97 | + def n(self): |
| 98 | + return self._n |
| 99 | + |
| 100 | + @property |
| 101 | + def invert(self): |
| 102 | + return self._invert |
| 103 | + |
| 104 | + @property |
| 105 | + def direction(self): |
| 106 | + return self._direction |
| 107 | + |
| 108 | + def __len__(self): |
| 109 | + return len(self._p) |
| 110 | + |
| 111 | + def __invert__(self): |
| 112 | + return DifferentialPort(self._p, self._n, invert=tuple(~inv for inv in self._invert), |
| 113 | + direction=self._direction) |
| 114 | + |
| 115 | + def __getitem__(self, index): |
| 116 | + return DifferentialPort(self._p[index], self._n[index], invert=self._invert[index], |
| 117 | + direction=self._direction) |
| 118 | + |
| 119 | + def __add__(self, other): |
| 120 | + if not isinstance(other, DifferentialPort): |
| 121 | + return NotImplemented |
| 122 | + return DifferentialPort(Cat(self._p, other._p), Cat(self._n, other._n), |
| 123 | + invert=self._invert + other._invert, |
| 124 | + direction=self._direction | other._direction) |
8 | 125 |
|
9 | 126 |
|
10 | 127 | class Pin(wiring.PureInterface):
|
|
0 commit comments