# Copyright 2026 Apheleia
#
# Description:
# Apheleia Verification Library Register Abstraction Component
from typing import Any
import systemrdl.node
[docs]
class Reg(systemrdl.node.RegNode):
def __str__(self) -> str:
"""
Custom String Formatting
:return: The formatted string
:rtype: str
"""
accesswidth = self.get_property("accesswidth")
regwidth = self.get_property("regwidth")
shared = self.get_property("shared")
# Pulling bits and name
_str_ = ""
_str_ += f"\tRegister: {self.inst_name:<16}\n"
_str_ += f"\t\t{'Address':<16}: 0xx{self.absolute_address:08x}\n"
_str_ += f"\t\t{'Access Width':<16}: {accesswidth}\n"
_str_ += f"\t\t{'Register Width':16}: {regwidth}\n"
_str_ += f"\t\t{'Shared':<16}: {shared}\n"
# Add in all fields
for f in self.children():
_str_ += str(f)
return _str_
[docs]
def get_root(self) -> systemrdl.node.Node:
"""
Get the root of the register
:return: The root of the register
:rtype: Reg
"""
if not hasattr(self, "_root_"):
curr = self
while curr.parent is not None:
curr = curr.parent
self._root_ = curr
return self._root_
[docs]
def peek(self) -> int | None:
"""
Peek value of field directly from HDL handle
:return: The value of the register
:rtype: int
"""
value = 0
for f in self.children():
v = f.peek()
if v:
value |= v << f.low
return value
[docs]
def poke(self, value: Any) -> None:
"""
Peek value of field directly from HDL handle
:param value: The value to poke
:type value: Any
:return: None
"""
for f in self.children():
v = f._extract_(value)
f.poke(v)
[docs]
async def read(self) -> int:
"""
Read the value of the register
:return: The value of the register
:rtype: int
"""
root = self.get_root()
assert hasattr(root, "read_callback"), f"{self.inst_name} has no read callback"
return await root.read_callback(self.absolute_address)
[docs]
async def write(self, value : int) -> None:
"""
Read the value of the register
:param value: The value to write
:type value: int
:return: None
"""
root = self.get_root()
assert hasattr(root, "write_callback"), f"{self.inst_name} has no write callback"
await root.write_callback(self.absolute_address, value)
# Monkey Patch
systemrdl.node.RegNode = Reg
__all__ = ["Reg"]