Skip to content

Commit

Permalink
Use sized integer literals if bit width exceeds 32-bits. #43
Browse files Browse the repository at this point in the history
  • Loading branch information
amykyta3 committed Jun 9, 2023
1 parent f36d761 commit 50d8779
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/peakrdl_regblock/addr_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand All @@ -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
Expand All @@ -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


Expand Down
4 changes: 3 additions & 1 deletion src/peakrdl_regblock/dereferencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion src/peakrdl_regblock/hwif/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions src/peakrdl_regblock/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

0 comments on commit 50d8779

Please sign in to comment.