From e4e26717beed2fc7ead2e4ca6e4c541320487ad3 Mon Sep 17 00:00:00 2001 From: Staf Verhaegen Date: Fri, 10 Jan 2020 13:28:19 +0100 Subject: [PATCH] Signal: allow to use integral Enum for reset value. --- nmigen/hdl/ast.py | 7 ++++++- nmigen/test/test_hdl_ast.py | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/nmigen/hdl/ast.py b/nmigen/hdl/ast.py index 34c8e2b..38e0408 100644 --- a/nmigen/hdl/ast.py +++ b/nmigen/hdl/ast.py @@ -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})`, " diff --git a/nmigen/test/test_hdl_ast.py b/nmigen/test/test_hdl_ast.py index d5e19f1..de0eb65 100644 --- a/nmigen/test/test_hdl_ast.py +++ b/nmigen/test/test_hdl_ast.py @@ -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"): -- 2.30.2