Signal: allow to use integral Enum for reset value.

This commit is contained in:
Staf Verhaegen 2020-01-10 13:28:19 +01:00 committed by whitequark
parent 8184efd612
commit e4e26717be
2 changed files with 14 additions and 1 deletions

View file

@ -797,7 +797,7 @@ class Signal(Value, DUID):
name this ``Signal`` is assigned to. Name collisions are automatically resolved by
prepending names of objects that contain this ``Signal`` and by appending integer
sequences.
reset : int
reset : int or integral Enum
Reset (synchronous) or default (combinatorial) value.
When this ``Signal`` is assigned to in synchronous context and the corresponding clock
domain is reset, the ``Signal`` assumes the given value. When this ``Signal`` is unassigned
@ -834,6 +834,11 @@ class Signal(Value, DUID):
attrs=None, decoder=None, src_loc_at=0):
super().__init__(src_loc_at=src_loc_at)
if isinstance(reset, Enum):
reset = reset.value
if not isinstance(reset, int):
raise TypeError("Reset value has to be an int or an integral Enum")
# TODO(nmigen-0.2): move this to nmigen.compat and make it a deprecated extension
if min is not None or max is not None:
warnings.warn("instead of `Signal(min={min}, max={max})`, "

View file

@ -755,6 +755,14 @@ class SignalTestCase(FHDLTestCase):
self.assertEqual(s1.reset, 0b111)
self.assertEqual(s1.reset_less, True)
def test_reset_enum(self):
s1 = Signal(2, reset=UnsignedEnum.BAR)
self.assertEqual(s1.reset, 2)
with self.assertRaises(TypeError,
msg="Reset value has to be an int or an integral Enum"
):
Signal(1, reset=StringEnum.FOO)
def test_reset_narrow(self):
with self.assertWarns(SyntaxWarning,
msg="Reset value 8 requires 4 bits to represent, but the signal only has 3 bits"):