From d1ca9c46a5ec5311667db20a78c2e71b0ecd45aa Mon Sep 17 00:00:00 2001 From: Catherine Date: Tue, 18 Jul 2023 14:20:05 +0000 Subject: [PATCH] lib.data: allow Const as value of Layout.const(...) field. Fixes #838. --- amaranth/lib/data.py | 2 +- tests/test_lib_data.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/amaranth/lib/data.py b/amaranth/lib/data.py index 1013863..237cc93 100644 --- a/amaranth/lib/data.py +++ b/amaranth/lib/data.py @@ -238,7 +238,7 @@ class Layout(ShapeCastable, metaclass=ABCMeta): "it casts to, {!r}, and not {!r}" .format(field.shape, cast_field_shape, key_value.shape())) - else: + elif not isinstance(key_value, Const): key_value = Const(key_value, cast_field_shape) int_value &= ~(((1 << cast_field_shape.width) - 1) << field.offset) int_value |= key_value.value << field.offset diff --git a/tests/test_lib_data.py b/tests/test_lib_data.py index e368095..845d240 100644 --- a/tests/test_lib_data.py +++ b/tests/test_lib_data.py @@ -424,6 +424,10 @@ class LayoutTestCase(FHDLTestCase): r"that it casts to, unsigned\(8\), and not unsigned\(1\)$"): sl.const({"f": "01"}) + def test_const_field_const(self): + sl = StructLayout({"f": unsigned(1)}) + self.assertRepr(sl.const({"f": Const(1)}), "(const 1'd1)") + def test_signal_reset(self): sl = StructLayout({ "a": unsigned(1),