From 50d8779283af2d3029ddfbc4ad7ee42c8b699ec9 Mon Sep 17 00:00:00 2001 From: Alex Mykyta Date: Thu, 8 Jun 2023 22:55:20 -0700 Subject: [PATCH] Use sized integer literals if bit width exceeds 32-bits. #43 --- src/peakrdl_regblock/addr_decode.py | 10 +++++----- src/peakrdl_regblock/dereferencer.py | 4 +++- src/peakrdl_regblock/hwif/generators.py | 3 ++- src/peakrdl_regblock/utils.py | 8 ++++++++ 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/peakrdl_regblock/addr_decode.py b/src/peakrdl_regblock/addr_decode.py index 0be0b36..3f01531 100644 --- a/src/peakrdl_regblock/addr_decode.py +++ b/src/peakrdl_regblock/addr_decode.py @@ -3,7 +3,7 @@ from systemrdl.node import FieldNode, RegNode from systemrdl.walker import WalkerAction -from .utils import get_indexed_path +from .utils import get_indexed_path, get_sv_int from .struct_generator import RDLStructGenerator from .forloop_generator import RDLForLoopGenerator from .identifier_filter import kw_filter as kwf @@ -137,7 +137,7 @@ def __init__(self, addr_decode: AddressDecode) -> None: super().__init__() # List of address strides for each dimension - self._array_stride_stack = [] # type: List[List[int]] + self._array_stride_stack = [] # type: List[int] def enter_AddressableComponent(self, node: 'AddressableNode') -> Optional[WalkerAction]: @@ -157,7 +157,7 @@ def enter_AddressableComponent(self, node: 'AddressableNode') -> Optional[Walker # Is an external block addr_str = self._get_address_str(node) strb = self.addr_decode.get_external_block_access_strobe(node) - rhs = f"cpuif_req_masked & (cpuif_addr >= {addr_str}) & (cpuif_addr <= {addr_str} + 'h{(node.size - 1):x})" + rhs = f"cpuif_req_masked & (cpuif_addr >= {addr_str}) & (cpuif_addr <= {addr_str} + {get_sv_int(node.size - 1)})" self.add_content(f"{strb} = {rhs};") self.add_content(f"is_external |= {rhs};") return WalkerAction.SkipDescendants @@ -166,9 +166,9 @@ def enter_AddressableComponent(self, node: 'AddressableNode') -> Optional[Walker def _get_address_str(self, node: 'AddressableNode', subword_offset: int=0) -> str: - a = f"'h{(node.raw_absolute_address - self.addr_decode.top_node.raw_absolute_address + subword_offset):x}" + a = get_sv_int(node.raw_absolute_address - self.addr_decode.top_node.raw_absolute_address + subword_offset) for i, stride in enumerate(self._array_stride_stack): - a += f" + i{i}*'h{stride:x}" + a += f" + i{i}*{get_sv_int(stride)}" return a diff --git a/src/peakrdl_regblock/dereferencer.py b/src/peakrdl_regblock/dereferencer.py index 6198fea..92732ff 100644 --- a/src/peakrdl_regblock/dereferencer.py +++ b/src/peakrdl_regblock/dereferencer.py @@ -2,6 +2,8 @@ from systemrdl.node import AddrmapNode, FieldNode, SignalNode, RegNode, AddressableNode from systemrdl.rdltypes import PropertyReference +from .utils import get_sv_int + if TYPE_CHECKING: from .exporter import RegblockExporter, DesignState from .hwif import Hwif @@ -48,7 +50,7 @@ def get_value(self, obj: Union[int, FieldNode, SignalNode, PropertyReference]) - """ if isinstance(obj, int): # Is a simple scalar value - return f"'h{obj:x}" + return get_sv_int(obj) if isinstance(obj, FieldNode): if obj.implements_storage: diff --git a/src/peakrdl_regblock/hwif/generators.py b/src/peakrdl_regblock/hwif/generators.py index b315795..6c7ae68 100644 --- a/src/peakrdl_regblock/hwif/generators.py +++ b/src/peakrdl_regblock/hwif/generators.py @@ -5,6 +5,7 @@ from ..struct_generator import RDLFlatStructGenerator from ..identifier_filter import kw_filter as kwf +from ..utils import get_sv_int if TYPE_CHECKING: from systemrdl.node import Node, SignalNode, AddressableNode, RegfileNode @@ -295,7 +296,7 @@ def _enum_typedef(self, user_enum: Type['UserEnum']) -> str: lines = [] for enum_member in user_enum: - lines.append(f" {prefix}__{enum_member.name} = 'd{enum_member.value}") + lines.append(f" {prefix}__{enum_member.name} = {get_sv_int(enum_member.value)}") return ( "typedef enum {\n" diff --git a/src/peakrdl_regblock/utils.py b/src/peakrdl_regblock/utils.py index 5359c7b..e952d34 100644 --- a/src/peakrdl_regblock/utils.py +++ b/src/peakrdl_regblock/utils.py @@ -64,3 +64,11 @@ def ref_is_internal(top_node: AddrmapNode, ref: Union[Node, PropertyReference]) # A root signal was referenced, which dodged the top addrmap # This is considerd internal for this exporter return True + +def get_sv_int(n: int) -> str: + if n.bit_length() <= 32: + return f"'h{n:x}" + else: + # SV standard only enforces that unsized literals shall be at least 32-bits + # To support larger literals, they need to be sized explicitly + return f"{n.bit_length()}'h{n:x}"