1 from abc
import ABCMeta
, abstractmethod
7 from collections
import OrderedDict
8 from collections
.abc
import Iterable
, MutableMapping
, MutableSet
, MutableSequence
12 from .._utils
import *
13 from .._unused
import *
17 "Shape", "signed", "unsigned",
18 "Value", "Const", "C", "AnyConst", "AnySeq", "Operator", "Mux", "Part", "Slice", "Cat", "Repl",
19 "Array", "ArrayProxy",
20 "Signal", "ClockSignal", "ResetSignal",
21 "UserValue", "ValueCastable",
22 "Sample", "Past", "Stable", "Rose", "Fell", "Initial",
23 "Statement", "Switch",
24 "Property", "Assign", "Assert", "Assume", "Cover",
25 "ValueKey", "ValueDict", "ValueSet", "SignalKey", "SignalDict", "SignalSet",
30 """Deterministic Unique IDentifier."""
33 self
.duid
= DUID
.__next
_uid
38 """Bit width and signedness of a value.
40 A ``Shape`` can be constructed using:
41 * explicit bit width and signedness;
42 * aliases :func:`signed` and :func:`unsigned`;
43 * casting from a variety of objects.
45 A ``Shape`` can be cast from:
46 * an integer, where the integer specifies the bit width;
47 * a range, where the result is wide enough to represent any element of the range, and is
48 signed if any element of the range is signed;
49 * an :class:`Enum` with all integer members or :class:`IntEnum`, where the result is wide
50 enough to represent any member of the enumeration, and is signed if any member of
51 the enumeration is signed.
56 The number of bits in the representation, including the sign bit (if any).
58 If ``False``, the value is unsigned. If ``True``, the value is signed two's complement.
60 def __init__(self
, width
=1, signed
=False):
61 if not isinstance(width
, int) or width
< 0:
62 raise TypeError("Width must be a non-negative integer, not {!r}"
68 return iter((self
.width
, self
.signed
))
71 def cast(obj
, *, src_loc_at
=0):
72 if isinstance(obj
, Shape
):
74 if isinstance(obj
, int):
76 if isinstance(obj
, tuple):
78 warnings
.warn("instead of `{tuple}`, use `{constructor}({width})`"
79 .format(constructor
="signed" if signed
else "unsigned", width
=width
,
81 DeprecationWarning, stacklevel
=2 + src_loc_at
)
82 return Shape(width
, signed
)
83 if isinstance(obj
, range):
85 return Shape(0, obj
.start
< 0)
86 signed
= obj
.start
< 0 or (obj
.stop
- obj
.step
) < 0
87 width
= max(bits_for(obj
.start
, signed
),
88 bits_for(obj
.stop
- obj
.step
, signed
))
89 return Shape(width
, signed
)
90 if isinstance(obj
, type) and issubclass(obj
, Enum
):
91 min_value
= min(member
.value
for member
in obj
)
92 max_value
= max(member
.value
for member
in obj
)
93 if not isinstance(min_value
, int) or not isinstance(max_value
, int):
94 raise TypeError("Only enumerations with integer values can be used "
96 signed
= min_value
< 0 or max_value
< 0
97 width
= max(bits_for(min_value
, signed
), bits_for(max_value
, signed
))
98 return Shape(width
, signed
)
99 raise TypeError("Object {!r} cannot be used as value shape".format(obj
))
103 return "signed({})".format(self
.width
)
105 return "unsigned({})".format(self
.width
)
107 def __eq__(self
, other
):
108 if isinstance(other
, tuple) and len(other
) == 2:
109 width
, signed
= other
110 if isinstance(width
, int) and isinstance(signed
, bool):
111 return self
.width
== width
and self
.signed
== signed
113 raise TypeError("Shapes may be compared with other Shapes and (int, bool) tuples, "
116 if not isinstance(other
, Shape
):
117 raise TypeError("Shapes may be compared with other Shapes and (int, bool) tuples, "
120 return self
.width
== other
.width
and self
.signed
== other
.signed
124 """Shorthand for ``Shape(width, signed=False)``."""
125 return Shape(width
, signed
=False)
129 """Shorthand for ``Shape(width, signed=True)``."""
130 return Shape(width
, signed
=True)
133 class Value(metaclass
=ABCMeta
):
136 """Converts ``obj`` to an nMigen value.
138 Booleans and integers are wrapped into a :class:`Const`. Enumerations whose members are
139 all integers are converted to a :class:`Const` with a shape that fits every member.
141 if isinstance(obj
, Value
):
143 if isinstance(obj
, int):
145 if isinstance(obj
, Enum
):
146 return Const(obj
.value
, Shape
.cast(type(obj
)))
147 if isinstance(obj
, ValueCastable
):
148 return obj
.as_value()
149 raise TypeError("Object {!r} cannot be converted to an nMigen value".format(obj
))
151 def __init__(self
, *, src_loc_at
=0):
153 self
.src_loc
= tracer
.get_src_loc(1 + src_loc_at
)
156 raise TypeError("Attempted to convert nMigen value to Python boolean")
158 def __invert__(self
):
159 return Operator("~", [self
])
161 return Operator("-", [self
])
163 def __add__(self
, other
):
164 return Operator("+", [self
, other
])
165 def __radd__(self
, other
):
166 return Operator("+", [other
, self
])
167 def __sub__(self
, other
):
168 return Operator("-", [self
, other
])
169 def __rsub__(self
, other
):
170 return Operator("-", [other
, self
])
172 def __mul__(self
, other
):
173 return Operator("*", [self
, other
])
174 def __rmul__(self
, other
):
175 return Operator("*", [other
, self
])
177 def __check_divisor(self
):
178 width
, signed
= self
.shape()
180 # Python's division semantics and Verilog's division semantics differ for negative
181 # divisors (Python uses div/mod, Verilog uses quo/rem); for now, avoid the issue
182 # completely by prohibiting such division operations.
183 raise NotImplementedError("Division by a signed value is not supported")
184 def __mod__(self
, other
):
185 other
= Value
.cast(other
)
186 other
.__check
_divisor
()
187 return Operator("%", [self
, other
])
188 def __rmod__(self
, other
):
189 self
.__check
_divisor
()
190 return Operator("%", [other
, self
])
191 def __floordiv__(self
, other
):
192 other
= Value
.cast(other
)
193 other
.__check
_divisor
()
194 return Operator("//", [self
, other
])
195 def __rfloordiv__(self
, other
):
196 self
.__check
_divisor
()
197 return Operator("//", [other
, self
])
199 def __check_shamt(self
):
200 width
, signed
= self
.shape()
202 # Neither Python nor HDLs implement shifts by negative values; prohibit any shifts
203 # by a signed value to make sure the shift amount can always be interpreted as
205 raise TypeError("Shift amount must be unsigned")
206 def __lshift__(self
, other
):
207 other
= Value
.cast(other
)
208 other
.__check
_shamt
()
209 return Operator("<<", [self
, other
])
210 def __rlshift__(self
, other
):
212 return Operator("<<", [other
, self
])
213 def __rshift__(self
, other
):
214 other
= Value
.cast(other
)
215 other
.__check
_shamt
()
216 return Operator(">>", [self
, other
])
217 def __rrshift__(self
, other
):
219 return Operator(">>", [other
, self
])
221 def __and__(self
, other
):
222 return Operator("&", [self
, other
])
223 def __rand__(self
, other
):
224 return Operator("&", [other
, self
])
225 def __xor__(self
, other
):
226 return Operator("^", [self
, other
])
227 def __rxor__(self
, other
):
228 return Operator("^", [other
, self
])
229 def __or__(self
, other
):
230 return Operator("|", [self
, other
])
231 def __ror__(self
, other
):
232 return Operator("|", [other
, self
])
234 def __eq__(self
, other
):
235 return Operator("==", [self
, other
])
236 def __ne__(self
, other
):
237 return Operator("!=", [self
, other
])
238 def __lt__(self
, other
):
239 return Operator("<", [self
, other
])
240 def __le__(self
, other
):
241 return Operator("<=", [self
, other
])
242 def __gt__(self
, other
):
243 return Operator(">", [self
, other
])
244 def __ge__(self
, other
):
245 return Operator(">=", [self
, other
])
248 width
, signed
= self
.shape()
250 return Mux(self
>= 0, self
, -self
)
255 return self
.shape().width
257 def __getitem__(self
, key
):
259 if isinstance(key
, int):
260 if key
not in range(-n
, n
):
261 raise IndexError(f
"Index {key} is out of bounds for a {n}-bit value")
264 return Slice(self
, key
, key
+ 1)
265 elif isinstance(key
, slice):
266 start
, stop
, step
= key
.indices(n
)
268 return Cat(self
[i
] for i
in range(start
, stop
, step
))
269 return Slice(self
, start
, stop
)
271 raise TypeError("Cannot index value with {}".format(repr(key
)))
273 def as_unsigned(self
):
274 """Conversion to unsigned.
279 This ``Value`` reinterpreted as a unsigned integer.
281 return Operator("u", [self
])
284 """Conversion to signed.
289 This ``Value`` reinterpreted as a signed integer.
291 return Operator("s", [self
])
294 """Conversion to boolean.
299 ``1`` if any bits are set, ``0`` otherwise.
301 return Operator("b", [self
])
304 """Check if any bits are ``1``.
309 ``1`` if any bits are set, ``0`` otherwise.
311 return Operator("r|", [self
])
314 """Check if all bits are ``1``.
319 ``1`` if all bits are set, ``0`` otherwise.
321 return Operator("r&", [self
])
324 """Compute pairwise exclusive-or of every bit.
329 ``1`` if an odd number of bits are set, ``0`` if an even number of bits are set.
331 return Operator("r^", [self
])
333 def implies(premise
, conclusion
):
339 ``0`` if ``premise`` is true and ``conclusion`` is not, ``1`` otherwise.
341 return ~premise | conclusion
343 def bit_select(self
, offset
, width
):
344 """Part-select with bit granularity.
346 Selects a constant width but variable offset part of a ``Value``, such that successive
347 parts overlap by all but 1 bit.
352 Index of first selected bit.
354 Number of selected bits.
359 Selected part of the ``Value``
361 offset
= Value
.cast(offset
)
362 if type(offset
) is Const
and isinstance(width
, int):
363 return self
[offset
.value
:offset
.value
+ width
]
364 return Part(self
, offset
, width
, stride
=1, src_loc_at
=1)
366 def word_select(self
, offset
, width
):
367 """Part-select with word granularity.
369 Selects a constant width but variable offset part of a ``Value``, such that successive
370 parts do not overlap.
375 Index of first selected word.
377 Number of selected bits.
382 Selected part of the ``Value``
384 offset
= Value
.cast(offset
)
385 if type(offset
) is Const
and isinstance(width
, int):
386 return self
[offset
.value
* width
:(offset
.value
+ 1) * width
]
387 return Part(self
, offset
, width
, stride
=width
, src_loc_at
=1)
389 def matches(self
, *patterns
):
392 Matches against a set of patterns, which may be integers or bit strings, recognizing
393 the same grammar as ``Case()``.
397 patterns : int or str
398 Patterns to match against.
403 ``1`` if any pattern matches the value, ``0`` otherwise.
406 for pattern
in patterns
:
407 if not isinstance(pattern
, (int, str, Enum
)):
408 raise SyntaxError("Match pattern must be an integer, a string, or an enumeration, "
411 if isinstance(pattern
, str) and any(bit
not in "01- \t" for bit
in pattern
):
412 raise SyntaxError("Match pattern '{}' must consist of 0, 1, and - (don't care) "
413 "bits, and may include whitespace"
415 if (isinstance(pattern
, str) and
416 len("".join(pattern
.split())) != len(self
)):
417 raise SyntaxError("Match pattern '{}' must have the same width as match value "
419 .format(pattern
, len(self
)))
420 if isinstance(pattern
, int) and bits_for(pattern
) > len(self
):
421 warnings
.warn("Match pattern '{:b}' is wider than match value "
422 "(which has width {}); comparison will never be true"
423 .format(pattern
, len(self
)),
424 SyntaxWarning, stacklevel
=3)
426 if isinstance(pattern
, str):
427 pattern
= "".join(pattern
.split()) # remove whitespace
428 mask
= int(pattern
.replace("0", "1").replace("-", "0"), 2)
429 pattern
= int(pattern
.replace("-", "0"), 2)
430 matches
.append((self
& mask
) == pattern
)
431 elif isinstance(pattern
, int):
432 matches
.append(self
== pattern
)
433 elif isinstance(pattern
, Enum
):
434 matches
.append(self
== pattern
.value
)
439 elif len(matches
) == 1:
442 return Cat(*matches
).any()
444 def shift_left(self
, amount
):
445 """Shift left by constant amount.
455 If the amount is positive, the input shifted left. Otherwise, the input shifted right.
457 if not isinstance(amount
, int):
458 raise TypeError("Shift amount must be an integer, not {!r}".format(amount
))
460 return self
.shift_right(-amount
)
461 if self
.shape().signed
:
462 return Cat(Const(0, amount
), self
).as_signed()
464 return Cat(Const(0, amount
), self
) # unsigned
466 def shift_right(self
, amount
):
467 """Shift right by constant amount.
477 If the amount is positive, the input shifted right. Otherwise, the input shifted left.
479 if not isinstance(amount
, int):
480 raise TypeError("Shift amount must be an integer, not {!r}".format(amount
))
482 return self
.shift_left(-amount
)
483 if self
.shape().signed
:
484 return self
[amount
:].as_signed()
486 return self
[amount
:] # unsigned
488 def rotate_left(self
, amount
):
489 """Rotate left by constant amount.
499 If the amount is positive, the input rotated left. Otherwise, the input rotated right.
501 if not isinstance(amount
, int):
502 raise TypeError("Rotate amount must be an integer, not {!r}".format(amount
))
504 return Cat(self
[-amount
:], self
[:-amount
]) # meow :3
506 def rotate_right(self
, amount
):
507 """Rotate right by constant amount.
517 If the amount is positive, the input rotated right. Otherwise, the input rotated right.
519 if not isinstance(amount
, int):
520 raise TypeError("Rotate amount must be an integer, not {!r}".format(amount
))
522 return Cat(self
[amount
:], self
[:amount
])
530 Value to be assigned.
535 Assignment statement that can be used in combinatorial or synchronous context.
537 return Assign(self
, value
, src_loc_at
=1)
541 """Bit width and signedness of a value.
550 >>> Signal(8).shape()
551 Shape(width=8, signed=False)
552 >>> Const(0xaa).shape()
553 Shape(width=8, signed=False)
557 def _lhs_signals(self
):
558 raise TypeError("Value {!r} cannot be used in assignments".format(self
))
561 def _rhs_signals(self
):
565 raise TypeError("Value {!r} cannot be evaluated as constant".format(self
))
572 """A constant, literal integer value.
577 shape : int or tuple or None
578 Either an integer ``width`` or a tuple ``(width, signed)`` specifying the number of bits
579 in this constant and whether it is signed (can represent negative values).
580 ``shape`` defaults to the minimum possible width and signedness of ``value``.
590 def normalize(value
, shape
):
591 width
, signed
= shape
592 mask
= (1 << width
) - 1
594 if signed
and value
>> (width
- 1):
598 def __init__(self
, value
, shape
=None, *, src_loc_at
=0):
599 # We deliberately do not call Value.__init__ here.
600 self
.value
= int(value
)
602 shape
= Shape(bits_for(self
.value
), signed
=self
.value
< 0)
603 elif isinstance(shape
, int):
604 shape
= Shape(shape
, signed
=self
.value
< 0)
606 shape
= Shape
.cast(shape
, src_loc_at
=1 + src_loc_at
)
607 self
.width
, self
.signed
= shape
608 self
.value
= self
.normalize(self
.value
, shape
)
611 return Shape(self
.width
, self
.signed
)
613 def _rhs_signals(self
):
620 return "(const {}'{}d{})".format(self
.width
, "s" if self
.signed
else "", self
.value
)
623 C
= Const
# shorthand
626 class AnyValue(Value
, DUID
):
627 def __init__(self
, shape
, *, src_loc_at
=0):
628 super().__init
__(src_loc_at
=src_loc_at
)
629 self
.width
, self
.signed
= Shape
.cast(shape
, src_loc_at
=1 + src_loc_at
)
630 if not isinstance(self
.width
, int) or self
.width
< 0:
631 raise TypeError("Width must be a non-negative integer, not {!r}"
635 return Shape(self
.width
, self
.signed
)
637 def _rhs_signals(self
):
642 class AnyConst(AnyValue
):
644 return "(anyconst {}'{})".format(self
.width
, "s" if self
.signed
else "")
648 class AnySeq(AnyValue
):
650 return "(anyseq {}'{})".format(self
.width
, "s" if self
.signed
else "")
654 class Operator(Value
):
655 def __init__(self
, operator
, operands
, *, src_loc_at
=0):
656 super().__init
__(src_loc_at
=1 + src_loc_at
)
657 self
.operator
= operator
658 self
.operands
= [Value
.cast(op
) for op
in operands
]
661 def _bitwise_binary_shape(a_shape
, b_shape
):
662 a_bits
, a_sign
= a_shape
663 b_bits
, b_sign
= b_shape
664 if not a_sign
and not b_sign
:
665 # both operands unsigned
666 return Shape(max(a_bits
, b_bits
), False)
667 elif a_sign
and b_sign
:
668 # both operands signed
669 return Shape(max(a_bits
, b_bits
), True)
670 elif not a_sign
and b_sign
:
671 # first operand unsigned (add sign bit), second operand signed
672 return Shape(max(a_bits
+ 1, b_bits
), True)
674 # first signed, second operand unsigned (add sign bit)
675 return Shape(max(a_bits
, b_bits
+ 1), True)
677 op_shapes
= list(map(lambda x
: x
.shape(), self
.operands
))
678 if len(op_shapes
) == 1:
679 (a_width
, a_signed
), = op_shapes
680 if self
.operator
in ("+", "~"):
681 return Shape(a_width
, a_signed
)
682 if self
.operator
== "-":
683 return Shape(a_width
+ 1, True)
684 if self
.operator
in ("b", "r|", "r&", "r^"):
685 return Shape(1, False)
686 if self
.operator
== "u":
687 return Shape(a_width
, False)
688 if self
.operator
== "s":
689 return Shape(a_width
, True)
690 elif len(op_shapes
) == 2:
691 (a_width
, a_signed
), (b_width
, b_signed
) = op_shapes
692 if self
.operator
in ("+", "-"):
693 width
, signed
= _bitwise_binary_shape(*op_shapes
)
694 return Shape(width
+ 1, signed
)
695 if self
.operator
== "*":
696 return Shape(a_width
+ b_width
, a_signed
or b_signed
)
697 if self
.operator
in ("//", "%"):
699 return Shape(a_width
, a_signed
)
700 if self
.operator
in ("<", "<=", "==", "!=", ">", ">="):
701 return Shape(1, False)
702 if self
.operator
in ("&", "^", "|"):
703 return _bitwise_binary_shape(*op_shapes
)
704 if self
.operator
== "<<":
706 extra
= 2 ** (b_width
- 1) - 1
708 extra
= 2 ** (b_width
) - 1
709 return Shape(a_width
+ extra
, a_signed
)
710 if self
.operator
== ">>":
712 extra
= 2 ** (b_width
- 1)
715 return Shape(a_width
+ extra
, a_signed
)
716 elif len(op_shapes
) == 3:
717 if self
.operator
== "m":
718 s_shape
, a_shape
, b_shape
= op_shapes
719 return _bitwise_binary_shape(a_shape
, b_shape
)
720 raise NotImplementedError("Operator {}/{} not implemented"
721 .format(self
.operator
, len(op_shapes
))) # :nocov:
723 def _rhs_signals(self
):
724 return union(op
._rhs
_signals
() for op
in self
.operands
)
727 return "({} {})".format(self
.operator
, " ".join(map(repr, self
.operands
)))
730 def Mux(sel
, val1
, val0
):
731 """Choose between two values.
744 Output ``Value``. If ``sel`` is asserted, the Mux returns ``val1``, else ``val0``.
746 sel
= Value
.cast(sel
)
749 return Operator("m", [sel
, val1
, val0
])
754 def __init__(self
, value
, start
, stop
, *, src_loc_at
=0):
755 if not isinstance(start
, int):
756 raise TypeError("Slice start must be an integer, not {!r}".format(start
))
757 if not isinstance(stop
, int):
758 raise TypeError("Slice stop must be an integer, not {!r}".format(stop
))
761 if start
not in range(-(n
+1), n
+1):
762 raise IndexError("Cannot start slice {} bits into {}-bit value".format(start
, n
))
765 if stop
not in range(-(n
+1), n
+1):
766 raise IndexError("Cannot stop slice {} bits into {}-bit value".format(stop
, n
))
770 raise IndexError("Slice start {} must be less than slice stop {}".format(start
, stop
))
772 super().__init
__(src_loc_at
=src_loc_at
)
773 self
.value
= Value
.cast(value
)
778 return Shape(self
.stop
- self
.start
)
780 def _lhs_signals(self
):
781 return self
.value
._lhs
_signals
()
783 def _rhs_signals(self
):
784 return self
.value
._rhs
_signals
()
787 return "(slice {} {}:{})".format(repr(self
.value
), self
.start
, self
.stop
)
792 def __init__(self
, value
, offset
, width
, stride
=1, *, src_loc_at
=0):
793 if not isinstance(width
, int) or width
< 0:
794 raise TypeError("Part width must be a non-negative integer, not {!r}".format(width
))
795 if not isinstance(stride
, int) or stride
<= 0:
796 raise TypeError("Part stride must be a positive integer, not {!r}".format(stride
))
798 super().__init
__(src_loc_at
=src_loc_at
)
800 self
.offset
= Value
.cast(offset
)
805 return Shape(self
.width
)
807 def _lhs_signals(self
):
808 return self
.value
._lhs
_signals
()
810 def _rhs_signals(self
):
811 return self
.value
._rhs
_signals
() | self
.offset
._rhs
_signals
()
814 return "(part {} {} {} {})".format(repr(self
.value
), repr(self
.offset
),
815 self
.width
, self
.stride
)
820 """Concatenate values.
822 Form a compound ``Value`` from several smaller ones by concatenation.
823 The first argument occupies the lower bits of the result.
824 The return value can be used on either side of an assignment, that
825 is, the concatenated value can be used as an argument on the RHS or
826 as a target on the LHS. If it is used on the LHS, it must solely
827 consist of ``Signal`` s, slices of ``Signal`` s, and other concatenations
828 meeting these properties. The bit length of the return value is the sum of
829 the bit lengths of the arguments::
831 len(Cat(args)) == sum(len(arg) for arg in args)
835 *args : Values or iterables of Values, inout
836 ``Value`` s to be concatenated.
841 Resulting ``Value`` obtained by concatentation.
843 def __init__(self
, *args
, src_loc_at
=0):
844 super().__init
__(src_loc_at
=src_loc_at
)
845 self
.parts
= [Value
.cast(v
) for v
in flatten(args
)]
848 return Shape(sum(len(part
) for part
in self
.parts
))
850 def _lhs_signals(self
):
851 return union((part
._lhs
_signals
() for part
in self
.parts
), start
=SignalSet())
853 def _rhs_signals(self
):
854 return union((part
._rhs
_signals
() for part
in self
.parts
), start
=SignalSet())
858 for part
in reversed(self
.parts
):
860 value |
= part
._as
_const
()
864 return "(cat {})".format(" ".join(map(repr, self
.parts
)))
871 An input value is replicated (repeated) several times
872 to be used on the RHS of assignments::
874 len(Repl(s, n)) == len(s) * n
879 Input value to be replicated.
881 Number of replications.
888 def __init__(self
, value
, count
, *, src_loc_at
=0):
889 if not isinstance(count
, int) or count
< 0:
890 raise TypeError("Replication count must be a non-negative integer, not {!r}"
893 super().__init
__(src_loc_at
=src_loc_at
)
894 self
.value
= Value
.cast(value
)
898 return Shape(len(self
.value
) * self
.count
)
900 def _rhs_signals(self
):
901 return self
.value
._rhs
_signals
()
904 return "(repl {!r} {})".format(self
.value
, self
.count
)
908 class Signal(Value
, DUID
):
909 """A varying integer value.
913 shape : ``Shape``-castable object or None
914 Specification for the number of bits in this ``Signal`` and its signedness (whether it
915 can represent negative values). See ``Shape.cast`` for details.
916 If not specified, ``shape`` defaults to 1-bit and non-signed.
918 Name hint for this signal. If ``None`` (default) the name is inferred from the variable
919 name this ``Signal`` is assigned to.
920 reset : int or integral Enum
921 Reset (synchronous) or default (combinatorial) value.
922 When this ``Signal`` is assigned to in synchronous context and the corresponding clock
923 domain is reset, the ``Signal`` assumes the given value. When this ``Signal`` is unassigned
924 in combinatorial context (due to conditional assignments not being taken), the ``Signal``
925 assumes its ``reset`` value. Defaults to 0.
927 If ``True``, do not generate reset logic for this ``Signal`` in synchronous statements.
928 The ``reset`` value is only used as a combinatorial default or as the initial value.
929 Defaults to ``False``.
931 Dictionary of synthesis attributes.
932 decoder : function or Enum
933 A function converting integer signal values to human-readable strings (e.g. FSM state
934 names). If an ``Enum`` subclass is passed, it is concisely decoded using format string
935 ``"{0.name:}/{0.value:}"``, or a number if the signal value is not a member of
949 def __init__(self
, shape
=None, *, name
=None, reset
=0, reset_less
=False,
950 attrs
=None, decoder
=None, src_loc_at
=0):
951 super().__init
__(src_loc_at
=src_loc_at
)
953 if name
is not None and not isinstance(name
, str):
954 raise TypeError("Name must be a string, not {!r}".format(name
))
955 self
.name
= name
or tracer
.get_var_name(depth
=2 + src_loc_at
, default
="$signal")
959 self
.width
, self
.signed
= Shape
.cast(shape
, src_loc_at
=1 + src_loc_at
)
961 if isinstance(reset
, Enum
):
963 if not isinstance(reset
, int):
964 raise TypeError("Reset value has to be an int or an integral Enum")
966 reset_width
= bits_for(reset
, self
.signed
)
967 if reset
!= 0 and reset_width
> self
.width
:
968 warnings
.warn("Reset value {!r} requires {} bits to represent, but the signal "
970 .format(reset
, reset_width
, self
.width
),
971 SyntaxWarning, stacklevel
=2 + src_loc_at
)
974 self
.reset_less
= bool(reset_less
)
976 self
.attrs
= OrderedDict(() if attrs
is None else attrs
)
978 if decoder
is None and isinstance(shape
, type) and issubclass(shape
, Enum
):
980 if isinstance(decoder
, type) and issubclass(decoder
, Enum
):
981 def enum_decoder(value
):
983 return "{0.name:}/{0.value:}".format(decoder(value
))
986 self
.decoder
= enum_decoder
987 self
._enum
_class
= decoder
989 self
.decoder
= decoder
990 self
._enum
_class
= None
992 # Not a @classmethod because nmigen.compat requires it.
994 def like(other
, *, name
=None, name_suffix
=None, src_loc_at
=0, **kwargs
):
995 """Create Signal based on another.
1000 Object to base this Signal on.
1002 if name
is not None:
1003 new_name
= str(name
)
1004 elif name_suffix
is not None:
1005 new_name
= other
.name
+ str(name_suffix
)
1007 new_name
= tracer
.get_var_name(depth
=2 + src_loc_at
, default
="$like")
1008 kw
= dict(shape
=Value
.cast(other
).shape(), name
=new_name
)
1009 if isinstance(other
, Signal
):
1010 kw
.update(reset
=other
.reset
, reset_less
=other
.reset_less
,
1011 attrs
=other
.attrs
, decoder
=other
.decoder
)
1013 return Signal(**kw
, src_loc_at
=1 + src_loc_at
)
1016 return Shape(self
.width
, self
.signed
)
1018 def _lhs_signals(self
):
1019 return SignalSet((self
,))
1021 def _rhs_signals(self
):
1022 return SignalSet((self
,))
1025 return "(sig {})".format(self
.name
)
1029 class ClockSignal(Value
):
1030 """Clock signal for a clock domain.
1032 Any ``ClockSignal`` is equivalent to ``cd.clk`` for a clock domain with the corresponding name.
1033 All of these signals ultimately refer to the same signal, but they can be manipulated
1034 independently of the clock domain, even before the clock domain is created.
1039 Clock domain to obtain a clock signal for. Defaults to ``"sync"``.
1041 def __init__(self
, domain
="sync", *, src_loc_at
=0):
1042 super().__init
__(src_loc_at
=src_loc_at
)
1043 if not isinstance(domain
, str):
1044 raise TypeError("Clock domain name must be a string, not {!r}".format(domain
))
1045 if domain
== "comb":
1046 raise ValueError("Domain '{}' does not have a clock".format(domain
))
1047 self
.domain
= domain
1052 def _lhs_signals(self
):
1053 return SignalSet((self
,))
1055 def _rhs_signals(self
):
1056 raise NotImplementedError("ClockSignal must be lowered to a concrete signal") # :nocov:
1059 return "(clk {})".format(self
.domain
)
1063 class ResetSignal(Value
):
1064 """Reset signal for a clock domain.
1066 Any ``ResetSignal`` is equivalent to ``cd.rst`` for a clock domain with the corresponding name.
1067 All of these signals ultimately refer to the same signal, but they can be manipulated
1068 independently of the clock domain, even before the clock domain is created.
1073 Clock domain to obtain a reset signal for. Defaults to ``"sync"``.
1074 allow_reset_less : bool
1075 If the clock domain is reset-less, act as a constant ``0`` instead of reporting an error.
1077 def __init__(self
, domain
="sync", allow_reset_less
=False, *, src_loc_at
=0):
1078 super().__init
__(src_loc_at
=src_loc_at
)
1079 if not isinstance(domain
, str):
1080 raise TypeError("Clock domain name must be a string, not {!r}".format(domain
))
1081 if domain
== "comb":
1082 raise ValueError("Domain '{}' does not have a reset".format(domain
))
1083 self
.domain
= domain
1084 self
.allow_reset_less
= allow_reset_less
1089 def _lhs_signals(self
):
1090 return SignalSet((self
,))
1092 def _rhs_signals(self
):
1093 raise NotImplementedError("ResetSignal must be lowered to a concrete signal") # :nocov:
1096 return "(rst {})".format(self
.domain
)
1099 class Array(MutableSequence
):
1100 """Addressable multiplexer.
1102 An array is similar to a ``list`` that can also be indexed by ``Value``s; indexing by an integer or a slice works the same as for Python lists, but indexing by a ``Value`` results
1105 The array proxy can be used as an ordinary ``Value``, i.e. participate in calculations and
1106 assignments, provided that all elements of the array are values. The array proxy also supports
1107 attribute access and further indexing, each returning another array proxy; this means that
1108 the results of indexing into arrays, arrays of records, and arrays of arrays can all
1109 be used as first-class values.
1111 It is an error to change an array or any of its elements after an array proxy was created.
1112 Changing the array directly will raise an exception. However, it is not possible to detect
1113 the elements being modified; if an element's attribute or element is modified after the proxy
1114 for it has been created, the proxy will refer to stale data.
1121 gpios = Array(Signal() for _ in range(10))
1123 m.d.sync += gpios[bus.addr].eq(bus.w_data)
1125 m.d.sync += bus.r_data.eq(gpios[bus.addr])
1127 Multidimensional array::
1129 mult = Array(Array(x * y for y in range(10)) for x in range(10))
1130 a = Signal.range(10)
1131 b = Signal.range(10)
1133 m.d.comb += r.eq(mult[a][b])
1141 buses = Array(Record(layout) for busno in range(4))
1142 master = Record(layout)
1144 buses[sel].r_en.eq(master.r_en),
1145 master.r_data.eq(buses[sel].r_data),
1148 def __init__(self
, iterable
=()):
1149 self
._inner
= list(iterable
)
1150 self
._proxy
_at
= None
1151 self
._mutable
= True
1153 def __getitem__(self
, index
):
1154 if isinstance(index
, Value
):
1156 self
._proxy
_at
= tracer
.get_src_loc()
1157 self
._mutable
= False
1158 return ArrayProxy(self
, index
)
1160 return self
._inner
[index
]
1163 return len(self
._inner
)
1165 def _check_mutability(self
):
1166 if not self
._mutable
:
1167 raise ValueError("Array can no longer be mutated after it was indexed with a value "
1168 "at {}:{}".format(*self
._proxy
_at
))
1170 def __setitem__(self
, index
, value
):
1171 self
._check
_mutability
()
1172 self
._inner
[index
] = value
1174 def __delitem__(self
, index
):
1175 self
._check
_mutability
()
1176 del self
._inner
[index
]
1178 def insert(self
, index
, value
):
1179 self
._check
_mutability
()
1180 self
._inner
.insert(index
, value
)
1183 return "(array{} [{}])".format(" mutable" if self
._mutable
else "",
1184 ", ".join(map(repr, self
._inner
)))
1188 class ArrayProxy(Value
):
1189 def __init__(self
, elems
, index
, *, src_loc_at
=0):
1190 super().__init
__(src_loc_at
=1 + src_loc_at
)
1192 self
.index
= Value
.cast(index
)
1194 def __getattr__(self
, attr
):
1195 return ArrayProxy([getattr(elem
, attr
) for elem
in self
.elems
], self
.index
)
1197 def __getitem__(self
, index
):
1198 return ArrayProxy([ elem
[index
] for elem
in self
.elems
], self
.index
)
1200 def _iter_as_values(self
):
1201 return (Value
.cast(elem
) for elem
in self
.elems
)
1204 unsigned_width
= signed_width
= 0
1205 has_unsigned
= has_signed
= False
1206 for elem_width
, elem_signed
in (elem
.shape() for elem
in self
._iter
_as
_values
()):
1209 signed_width
= max(signed_width
, elem_width
)
1212 unsigned_width
= max(unsigned_width
, elem_width
)
1213 # The shape of the proxy must be such that it preserves the mathematical value of the array
1214 # elements. I.e., shape-wise, an array proxy must be identical to an equivalent mux tree.
1215 # To ensure this holds, if the array contains both signed and unsigned values, make sure
1216 # that every unsigned value is zero-extended by at least one bit.
1217 if has_signed
and has_unsigned
and unsigned_width
>= signed_width
:
1218 # Array contains both signed and unsigned values, and at least one of the unsigned
1219 # values won't be zero-extended otherwise.
1220 return signed(unsigned_width
+ 1)
1222 # Array contains values of the same signedness, or else all of the unsigned values
1223 # are zero-extended.
1224 return Shape(max(unsigned_width
, signed_width
), has_signed
)
1226 def _lhs_signals(self
):
1227 signals
= union((elem
._lhs
_signals
() for elem
in self
._iter
_as
_values
()),
1231 def _rhs_signals(self
):
1232 signals
= union((elem
._rhs
_signals
() for elem
in self
._iter
_as
_values
()),
1234 return self
.index
._rhs
_signals
() | signals
1237 return "(proxy (array [{}]) {!r})".format(", ".join(map(repr, self
.elems
)), self
.index
)
1240 class UserValue(Value
):
1241 """Value with custom lowering.
1243 A ``UserValue`` is a value whose precise representation does not have to be immediately known,
1244 which is useful in certain metaprogramming scenarios. Instead of providing fixed semantics
1245 upfront, it is kept abstract for as long as possible, only being lowered to a concrete nMigen
1246 value when required.
1248 Note that the ``lower`` method will only be called once; this is necessary to ensure that
1249 nMigen's view of representation of all values stays internally consistent. If the class
1250 deriving from ``UserValue`` is mutable, then it must ensure that after ``lower`` is called,
1251 it is not mutated in a way that changes its representation.
1253 The following is an incomplete list of actions that, when applied to an ``UserValue`` directly
1254 or indirectly, will cause it to be lowered, provided as an illustrative reference:
1255 * Querying the shape using ``.shape()`` or ``len()``;
1256 * Creating a similarly shaped signal using ``Signal.like``;
1257 * Indexing or iterating through individual bits;
1258 * Adding an assignment to the value to a ``Module`` using ``m.d.<domain> +=``.
1260 def __init__(self
, *, src_loc_at
=0):
1261 super().__init
__(src_loc_at
=1 + src_loc_at
)
1262 self
.__lowered
= None
1266 """Conversion to a concrete representation."""
1269 def _lazy_lower(self
):
1270 if self
.__lowered
is None:
1271 lowered
= self
.lower()
1272 if isinstance(lowered
, UserValue
):
1273 lowered
= lowered
._lazy
_lower
()
1274 self
.__lowered
= Value
.cast(lowered
)
1275 return self
.__lowered
1278 return self
._lazy
_lower
().shape()
1280 def _lhs_signals(self
):
1281 return self
._lazy
_lower
()._lhs
_signals
()
1283 def _rhs_signals(self
):
1284 return self
._lazy
_lower
()._rhs
_signals
()
1287 class ValueCastable
:
1288 """Base class for classes which can be cast to Values.
1290 A ``ValueCastable`` can be cast to ``Value``, meaning its precise representation does not have
1291 to be immediately known. This is useful in certain metaprogramming scenarios. Instead of
1292 providing fixed semantics upfront, it is kept abstract for as long as possible, only being
1293 cast to a concrete nMigen value when required.
1295 Note that it is necessary to ensure that nMigen's view of representation of all values stays
1296 internally consistent. The class deriving from ``ValueCastable`` must decorate the ``as_value``
1297 method with the ``lowermethod`` decorator, which ensures that all calls to ``as_value``return the
1298 same ``Value`` representation. If the class deriving from ``ValueCastable`` is mutable, it is
1299 up to the user to ensure that it is not mutated in a way that changes its representation after
1300 the first call to ``as_value``.
1302 def __new__(cls
, *args
, **kwargs
):
1303 self
= super().__new
__(cls
)
1304 if not hasattr(self
, "as_value"):
1305 raise TypeError(f
"Class '{cls.__name__}' deriving from `ValueCastable` must override the `as_value` method")
1307 if not hasattr(self
.as_value
, "_ValueCastable__memoized"):
1308 raise TypeError(f
"Class '{cls.__name__}' deriving from `ValueCastable` must decorate the `as_value` "
1309 "method with the `ValueCastable.lowermethod` decorator")
1313 def lowermethod(func
):
1314 """Decorator to memoize lowering methods.
1316 Ensures the decorated method is called only once, with subsequent method calls returning the
1317 object returned by the first first method call.
1319 This decorator is required to decorate the ``as_value`` method of ``ValueCastable`` subclasses.
1320 This is to ensure that nMigen's view of representation of all values stays internally
1323 @functools.wraps(func
)
1324 def wrapper_memoized(self
, *args
, **kwargs
):
1325 if not hasattr(self
, "_ValueCastable__lowered_to"):
1326 self
.__lowered
_to
= func(self
, *args
, **kwargs
)
1327 return self
.__lowered
_to
1328 wrapper_memoized
.__memoized
= True
1329 return wrapper_memoized
1333 class Sample(Value
):
1334 """Value from the past.
1336 A ``Sample`` of an expression is equal to the value of the expression ``clocks`` clock edges
1337 of the ``domain`` clock back. If that moment is before the beginning of time, it is equal
1338 to the value of the expression calculated as if each signal had its reset value.
1340 def __init__(self
, expr
, clocks
, domain
, *, src_loc_at
=0):
1341 super().__init
__(src_loc_at
=1 + src_loc_at
)
1342 self
.value
= Value
.cast(expr
)
1343 self
.clocks
= int(clocks
)
1344 self
.domain
= domain
1345 if not isinstance(self
.value
, (Const
, Signal
, ClockSignal
, ResetSignal
, Initial
)):
1346 raise TypeError("Sampled value must be a signal or a constant, not {!r}"
1347 .format(self
.value
))
1349 raise ValueError("Cannot sample a value {} cycles in the future"
1350 .format(-self
.clocks
))
1351 if not (self
.domain
is None or isinstance(self
.domain
, str)):
1352 raise TypeError("Domain name must be a string or None, not {!r}"
1353 .format(self
.domain
))
1356 return self
.value
.shape()
1358 def _rhs_signals(self
):
1359 return SignalSet((self
,))
1362 return "(sample {!r} @ {}[{}])".format(
1363 self
.value
, "<default>" if self
.domain
is None else self
.domain
, self
.clocks
)
1366 def Past(expr
, clocks
=1, domain
=None):
1367 return Sample(expr
, clocks
, domain
)
1370 def Stable(expr
, clocks
=0, domain
=None):
1371 return Sample(expr
, clocks
+ 1, domain
) == Sample(expr
, clocks
, domain
)
1374 def Rose(expr
, clocks
=0, domain
=None):
1375 return ~
Sample(expr
, clocks
+ 1, domain
) & Sample(expr
, clocks
, domain
)
1378 def Fell(expr
, clocks
=0, domain
=None):
1379 return Sample(expr
, clocks
+ 1, domain
) & ~
Sample(expr
, clocks
, domain
)
1383 class Initial(Value
):
1384 """Start indicator, for model checking.
1386 An ``Initial`` signal is ``1`` at the first cycle of model checking, and ``0`` at any other.
1388 def __init__(self
, *, src_loc_at
=0):
1389 super().__init
__(src_loc_at
=src_loc_at
)
1394 def _rhs_signals(self
):
1395 return SignalSet((self
,))
1401 class _StatementList(list):
1403 return "({})".format(" ".join(map(repr, self
)))
1407 def __init__(self
, *, src_loc_at
=0):
1408 self
.src_loc
= tracer
.get_src_loc(1 + src_loc_at
)
1412 if isinstance(obj
, Iterable
):
1413 return _StatementList(sum((Statement
.cast(e
) for e
in obj
), []))
1415 if isinstance(obj
, Statement
):
1416 return _StatementList([obj
])
1418 raise TypeError("Object {!r} is not an nMigen statement".format(obj
))
1422 class Assign(Statement
):
1423 def __init__(self
, lhs
, rhs
, *, src_loc_at
=0):
1424 super().__init
__(src_loc_at
=src_loc_at
)
1425 self
.lhs
= Value
.cast(lhs
)
1426 self
.rhs
= Value
.cast(rhs
)
1428 def _lhs_signals(self
):
1429 return self
.lhs
._lhs
_signals
()
1431 def _rhs_signals(self
):
1432 return self
.lhs
._rhs
_signals
() | self
.rhs
._rhs
_signals
()
1435 return "(eq {!r} {!r})".format(self
.lhs
, self
.rhs
)
1438 class UnusedProperty(UnusedMustUse
):
1442 class Property(Statement
, MustUse
):
1443 _MustUse__warning
= UnusedProperty
1445 def __init__(self
, test
, *, _check
=None, _en
=None, src_loc_at
=0):
1446 super().__init
__(src_loc_at
=src_loc_at
)
1447 self
.test
= Value
.cast(test
)
1448 self
._check
= _check
1450 if self
._check
is None:
1451 self
._check
= Signal(reset_less
=True, name
="${}$check".format(self
._kind
))
1452 self
._check
.src_loc
= self
.src_loc
1454 self
._en
= Signal(reset_less
=True, name
="${}$en".format(self
._kind
))
1455 self
._en
.src_loc
= self
.src_loc
1457 def _lhs_signals(self
):
1458 return SignalSet((self
._en
, self
._check
))
1460 def _rhs_signals(self
):
1461 return self
.test
._rhs
_signals
()
1464 return "({} {!r})".format(self
._kind
, self
.test
)
1468 class Assert(Property
):
1473 class Assume(Property
):
1478 class Cover(Property
):
1483 class Switch(Statement
):
1484 def __init__(self
, test
, cases
, *, src_loc
=None, src_loc_at
=0, case_src_locs
={}):
1486 super().__init
__(src_loc_at
=src_loc_at
)
1488 # Switch is a bit special in terms of location tracking because it is usually created
1489 # long after the control has left the statement that directly caused its creation.
1490 self
.src_loc
= src_loc
1491 # Switch is also a bit special in that its parts also have location information. It can't
1492 # be automatically traced, so whatever constructs a Switch may optionally provide it.
1493 self
.case_src_locs
= {}
1495 self
.test
= Value
.cast(test
)
1496 self
.cases
= OrderedDict()
1497 for orig_keys
, stmts
in cases
.items():
1498 # Map: None -> (); key -> (key,); (key...) -> (key...)
1502 if not isinstance(keys
, tuple):
1504 # Map: 2 -> "0010"; "0010" -> "0010"
1507 if isinstance(key
, str):
1508 key
= "".join(key
.split()) # remove whitespace
1509 elif isinstance(key
, int):
1510 key
= format(key
, "b").rjust(len(self
.test
), "0")
1511 elif isinstance(key
, Enum
):
1512 key
= format(key
.value
, "b").rjust(len(self
.test
), "0")
1514 raise TypeError("Object {!r} cannot be used as a switch key"
1516 assert len(key
) == len(self
.test
)
1517 new_keys
= (*new_keys
, key
)
1518 if not isinstance(stmts
, Iterable
):
1520 self
.cases
[new_keys
] = Statement
.cast(stmts
)
1521 if orig_keys
in case_src_locs
:
1522 self
.case_src_locs
[new_keys
] = case_src_locs
[orig_keys
]
1524 def _lhs_signals(self
):
1525 signals
= union((s
._lhs
_signals
() for ss
in self
.cases
.values() for s
in ss
),
1529 def _rhs_signals(self
):
1530 signals
= union((s
._rhs
_signals
() for ss
in self
.cases
.values() for s
in ss
),
1532 return self
.test
._rhs
_signals
() | signals
1535 def case_repr(keys
, stmts
):
1536 stmts_repr
= " ".join(map(repr, stmts
))
1538 return "(default {})".format(stmts_repr
)
1539 elif len(keys
) == 1:
1540 return "(case {} {})".format(keys
[0], stmts_repr
)
1542 return "(case ({}) {})".format(" ".join(keys
), stmts_repr
)
1543 case_reprs
= [case_repr(keys
, stmts
) for keys
, stmts
in self
.cases
.items()]
1544 return "(switch {!r} {})".format(self
.test
, " ".join(case_reprs
))
1547 class _MappedKeyCollection(metaclass
=ABCMeta
):
1549 def _map_key(self
, key
):
1553 def _unmap_key(self
, key
):
1557 class _MappedKeyDict(MutableMapping
, _MappedKeyCollection
):
1558 def __init__(self
, pairs
=()):
1559 self
._storage
= OrderedDict()
1560 for key
, value
in pairs
:
1563 def __getitem__(self
, key
):
1564 key
= None if key
is None else self
._map
_key
(key
)
1565 return self
._storage
[key
]
1567 def __setitem__(self
, key
, value
):
1568 key
= None if key
is None else self
._map
_key
(key
)
1569 self
._storage
[key
] = value
1571 def __delitem__(self
, key
):
1572 key
= None if key
is None else self
._map
_key
(key
)
1573 del self
._storage
[key
]
1576 for key
in self
._storage
:
1580 yield self
._unmap
_key
(key
)
1582 def __eq__(self
, other
):
1583 if not isinstance(other
, type(self
)):
1585 if len(self
) != len(other
):
1587 for ak
, bk
in zip(sorted(self
._storage
), sorted(other
._storage
)):
1590 if self
._storage
[ak
] != other
._storage
[bk
]:
1595 return len(self
._storage
)
1598 pairs
= ["({!r}, {!r})".format(k
, v
) for k
, v
in self
.items()]
1599 return "{}.{}([{}])".format(type(self
).__module
__, type(self
).__name
__,
1603 class _MappedKeySet(MutableSet
, _MappedKeyCollection
):
1604 def __init__(self
, elements
=()):
1605 self
._storage
= OrderedDict()
1606 for elem
in elements
:
1609 def add(self
, value
):
1610 self
._storage
[self
._map
_key
(value
)] = None
1612 def update(self
, values
):
1613 for value
in values
:
1616 def discard(self
, value
):
1618 del self
._storage
[self
._map
_key
(value
)]
1620 def __contains__(self
, value
):
1621 return self
._map
_key
(value
) in self
._storage
1624 for key
in [k
for k
in self
._storage
]:
1625 yield self
._unmap
_key
(key
)
1628 return len(self
._storage
)
1631 return "{}.{}({})".format(type(self
).__module
__, type(self
).__name
__,
1632 ", ".join(repr(x
) for x
in self
))
1636 def __init__(self
, value
):
1637 self
.value
= Value
.cast(value
)
1638 if isinstance(self
.value
, Const
):
1639 self
._hash
= hash(self
.value
.value
)
1640 elif isinstance(self
.value
, (Signal
, AnyValue
)):
1641 self
._hash
= hash(self
.value
.duid
)
1642 elif isinstance(self
.value
, (ClockSignal
, ResetSignal
)):
1643 self
._hash
= hash(self
.value
.domain
)
1644 elif isinstance(self
.value
, Operator
):
1645 self
._hash
= hash((self
.value
.operator
,
1646 tuple(ValueKey(o
) for o
in self
.value
.operands
)))
1647 elif isinstance(self
.value
, Slice
):
1648 self
._hash
= hash((ValueKey(self
.value
.value
), self
.value
.start
, self
.value
.stop
))
1649 elif isinstance(self
.value
, Part
):
1650 self
._hash
= hash((ValueKey(self
.value
.value
), ValueKey(self
.value
.offset
),
1651 self
.value
.width
, self
.value
.stride
))
1652 elif isinstance(self
.value
, Cat
):
1653 self
._hash
= hash(tuple(ValueKey(o
) for o
in self
.value
.parts
))
1654 elif isinstance(self
.value
, ArrayProxy
):
1655 self
._hash
= hash((ValueKey(self
.value
.index
),
1656 tuple(ValueKey(e
) for e
in self
.value
._iter
_as
_values
())))
1657 elif isinstance(self
.value
, Sample
):
1658 self
._hash
= hash((ValueKey(self
.value
.value
), self
.value
.clocks
, self
.value
.domain
))
1659 elif isinstance(self
.value
, Initial
):
1662 raise TypeError("Object {!r} cannot be used as a key in value collections"
1663 .format(self
.value
))
1668 def __eq__(self
, other
):
1669 if type(other
) is not ValueKey
:
1671 if type(self
.value
) is not type(other
.value
):
1674 if isinstance(self
.value
, Const
):
1675 return self
.value
.value
== other
.value
.value
1676 elif isinstance(self
.value
, (Signal
, AnyValue
)):
1677 return self
.value
is other
.value
1678 elif isinstance(self
.value
, (ClockSignal
, ResetSignal
)):
1679 return self
.value
.domain
== other
.value
.domain
1680 elif isinstance(self
.value
, Operator
):
1681 return (self
.value
.operator
== other
.value
.operator
and
1682 len(self
.value
.operands
) == len(other
.value
.operands
) and
1683 all(ValueKey(a
) == ValueKey(b
)
1684 for a
, b
in zip(self
.value
.operands
, other
.value
.operands
)))
1685 elif isinstance(self
.value
, Slice
):
1686 return (ValueKey(self
.value
.value
) == ValueKey(other
.value
.value
) and
1687 self
.value
.start
== other
.value
.start
and
1688 self
.value
.stop
== other
.value
.stop
)
1689 elif isinstance(self
.value
, Part
):
1690 return (ValueKey(self
.value
.value
) == ValueKey(other
.value
.value
) and
1691 ValueKey(self
.value
.offset
) == ValueKey(other
.value
.offset
) and
1692 self
.value
.width
== other
.value
.width
and
1693 self
.value
.stride
== other
.value
.stride
)
1694 elif isinstance(self
.value
, Cat
):
1695 return all(ValueKey(a
) == ValueKey(b
)
1696 for a
, b
in zip(self
.value
.parts
, other
.value
.parts
))
1697 elif isinstance(self
.value
, ArrayProxy
):
1698 return (ValueKey(self
.value
.index
) == ValueKey(other
.value
.index
) and
1699 len(self
.value
.elems
) == len(other
.value
.elems
) and
1700 all(ValueKey(a
) == ValueKey(b
)
1701 for a
, b
in zip(self
.value
._iter
_as
_values
(),
1702 other
.value
._iter
_as
_values
())))
1703 elif isinstance(self
.value
, Sample
):
1704 return (ValueKey(self
.value
.value
) == ValueKey(other
.value
.value
) and
1705 self
.value
.clocks
== other
.value
.clocks
and
1706 self
.value
.domain
== self
.value
.domain
)
1707 elif isinstance(self
.value
, Initial
):
1710 raise TypeError("Object {!r} cannot be used as a key in value collections"
1711 .format(self
.value
))
1713 def __lt__(self
, other
):
1714 if not isinstance(other
, ValueKey
):
1716 if type(self
.value
) != type(other
.value
):
1719 if isinstance(self
.value
, Const
):
1720 return self
.value
< other
.value
1721 elif isinstance(self
.value
, (Signal
, AnyValue
)):
1722 return self
.value
.duid
< other
.value
.duid
1723 elif isinstance(self
.value
, Slice
):
1724 return (ValueKey(self
.value
.value
) < ValueKey(other
.value
.value
) and
1725 self
.value
.start
< other
.value
.start
and
1726 self
.value
.end
< other
.value
.end
)
1728 raise TypeError("Object {!r} cannot be used as a key in value collections")
1731 return "<{}.ValueKey {!r}>".format(__name__
, self
.value
)
1734 class ValueDict(_MappedKeyDict
):
1736 _unmap_key
= lambda self
, key
: key
.value
1739 class ValueSet(_MappedKeySet
):
1741 _unmap_key
= lambda self
, key
: key
.value
1745 def __init__(self
, signal
):
1746 self
.signal
= signal
1747 if isinstance(signal
, Signal
):
1748 self
._intern
= (0, signal
.duid
)
1749 elif type(signal
) is ClockSignal
:
1750 self
._intern
= (1, signal
.domain
)
1751 elif type(signal
) is ResetSignal
:
1752 self
._intern
= (2, signal
.domain
)
1754 raise TypeError("Object {!r} is not an nMigen signal".format(signal
))
1757 return hash(self
._intern
)
1759 def __eq__(self
, other
):
1760 if type(other
) is not SignalKey
:
1762 return self
._intern
== other
._intern
1764 def __lt__(self
, other
):
1765 if type(other
) is not SignalKey
:
1766 raise TypeError("Object {!r} cannot be compared to a SignalKey".format(signal
))
1767 return self
._intern
< other
._intern
1770 return "<{}.SignalKey {!r}>".format(__name__
, self
.signal
)
1773 class SignalDict(_MappedKeyDict
):
1774 _map_key
= SignalKey
1775 _unmap_key
= lambda self
, key
: key
.signal
1778 class SignalSet(_MappedKeySet
):
1779 _map_key
= SignalKey
1780 _unmap_key
= lambda self
, key
: key
.signal