From 0849e1af0bbecc629b1331ee257c58bf931b241a Mon Sep 17 00:00:00 2001 From: Wanda Date: Sat, 30 Dec 2023 12:10:29 +0100 Subject: [PATCH] hdl.ast: make `Slice` const-castable. Fixes #1006. --- amaranth/hdl/ast.py | 3 +++ docs/changes.rst | 1 + docs/lang.rst | 1 + tests/test_hdl_ast.py | 16 ++++++++++++++++ 4 files changed, 21 insertions(+) diff --git a/amaranth/hdl/ast.py b/amaranth/hdl/ast.py index e0a0c4f..70e4763 100644 --- a/amaranth/hdl/ast.py +++ b/amaranth/hdl/ast.py @@ -731,6 +731,9 @@ class Const(Value): value |= part_value << width width += len(const) return Const(value, width) + elif type(obj) is Slice: + value = Const.cast(obj.value) + return Const(value.value >> obj.start, unsigned(obj.stop - obj.start)) else: raise TypeError(f"Value {obj!r} cannot be converted to an Amaranth constant") diff --git a/docs/changes.rst b/docs/changes.rst index 6b7e752..34a251c 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -15,6 +15,7 @@ Language changes .. currentmodule:: amaranth.hdl +* Added: `class:ast.Slice` objects have been made const-castable. * Removed: (deprecated in 0.4) :meth:`Const.normalize`. (`RFC 5`_) * Removed: (deprecated in 0.4) :class:`ast.Sample`, :class:`ast.Past`, :class:`ast.Stable`, :class:`ast.Rose`, :class:`ast.Fell`. diff --git a/docs/lang.rst b/docs/lang.rst index fdd197b..d238050 100644 --- a/docs/lang.rst +++ b/docs/lang.rst @@ -325,6 +325,7 @@ They may also be provided as a pattern to the :ref:`match operator