From 6d83724699e37bda18bff1b8828f8d86dbf1b330 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Nguyen?= Date: Fri, 17 May 2024 16:08:42 +0200 Subject: [PATCH] csr.reg: describe field shapes as shape-like objects. --- amaranth_soc/csr/action.py | 12 ++++++------ amaranth_soc/csr/reg.py | 13 ++++++------- tests/test_csr_reg.py | 2 +- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/amaranth_soc/csr/action.py b/amaranth_soc/csr/action.py index 0699326..6dc9b96 100644 --- a/amaranth_soc/csr/action.py +++ b/amaranth_soc/csr/action.py @@ -12,7 +12,7 @@ class R(FieldAction): Parameters ---------- - shape : :ref:`shape-castable ` + shape : :ref:`shape-like object ` Shape of the field. Interface attributes @@ -44,7 +44,7 @@ class W(FieldAction): Parameters ---------- - shape : :ref:`shape-castable ` + shape : :ref:`shape-like object ` Shape of the field. Interface attributes @@ -79,7 +79,7 @@ class RW(FieldAction): Parameters ---------- - shape : :ref:`shape-castable ` + shape : :ref:`shape-like object ` Shape of the field. init : :class:`int` Storage initial value. @@ -127,7 +127,7 @@ class RW1C(FieldAction): Parameters ---------- - shape : :ref:`shape-castable ` + shape : :ref:`shape-like object ` Shape of the field. init : :class:`int` Storage initial value. @@ -181,7 +181,7 @@ class RW1S(FieldAction): Parameters ---------- - shape : :ref:`shape-castable ` + shape : :ref:`shape-like object ` Shape of the field. init : :class:`int` Storage initial value. @@ -230,7 +230,7 @@ class _Reserved(FieldAction): Parameters ---------- - shape : :ref:`shape-castable ` + shape : :ref:`shape-like object ` Shape of the field. Interface attributes diff --git a/amaranth_soc/csr/reg.py b/amaranth_soc/csr/reg.py index 66d4dfe..da3c81a 100644 --- a/amaranth_soc/csr/reg.py +++ b/amaranth_soc/csr/reg.py @@ -1,6 +1,7 @@ from collections.abc import Mapping, Sequence from contextlib import contextmanager from amaranth import * +from amaranth.hdl import ShapeLike from amaranth.lib import enum, wiring from amaranth.lib.wiring import In, Out, connect, flipped from amaranth.utils import ceil_log2 @@ -34,7 +35,7 @@ class Signature(wiring.Signature): Parameters ---------- - shape : :ref:`shape-castable ` + shape : :ref:`shape-like object ` Shape of the field. access : :class:`FieldPort.Access` Field access mode. @@ -53,10 +54,8 @@ class Signature(wiring.Signature): this strobe is asserted. """ def __init__(self, shape, access): - try: - Shape.cast(shape) - except TypeError as e: - raise TypeError(f"Field shape must be a shape-castable object, not {shape!r}") from e + if not isinstance(shape, ShapeLike): + raise TypeError(f"Field shape must be a shape-like object, not {shape!r}") # TODO(py3.9): Remove this. Python 3.8 and below use cls.__name__ in the error message # instead of cls.__qualname__. # FieldPort.Access(access) @@ -119,7 +118,7 @@ def __repr__(self): Attributes ---------- - shape : :ref:`shape-castable ` + shape : :ref:`shape-like object ` Shape of the field. See :class:`FieldPort.Signature`. access : :class:`FieldPort.Access` Field access mode. See :class:`FieldPort.Signature`. @@ -190,7 +189,7 @@ class FieldAction(wiring.Component): Parameters ---------- - shape : :ref:`shape-castable ` + shape : :ref:`shape-like object ` Shape of the field. See :class:`FieldPort.Signature`. access : :class:`FieldPort.Access` Field access mode. See :class:`FieldPort.Signature`. diff --git a/tests/test_csr_reg.py b/tests/test_csr_reg.py index fc752df..bc3e1b9 100644 --- a/tests/test_csr_reg.py +++ b/tests/test_csr_reg.py @@ -102,7 +102,7 @@ def test_eq(self): def test_wrong_shape(self): with self.assertRaisesRegex(TypeError, - r"Field shape must be a shape-castable object, not 'foo'"): + r"Field shape must be a shape-like object, not 'foo'"): port = FieldPort.Signature("foo", "rw") def test_wrong_access(self):