From 504407c0d988e851a5d16760db9ac72296644678 Mon Sep 17 00:00:00 2001 From: Anton Lydike Date: Mon, 24 May 2021 14:34:35 +0200 Subject: [PATCH] [CSR] adding callbacks to each csr block --- riscemu/priv/CSR.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/riscemu/priv/CSR.py b/riscemu/priv/CSR.py index c14ba6f..e39e06c 100644 --- a/riscemu/priv/CSR.py +++ b/riscemu/priv/CSR.py @@ -1,4 +1,4 @@ -from typing import Dict, Union +from typing import Dict, Union, Callable from collections import defaultdict MSTATUS_OFFSETS = { @@ -59,14 +59,18 @@ class CSR: Translation for named registers """ + listeners: Dict[int, Callable[[int, int], None]] + def __init__(self): self.regs = defaultdict(lambda: 0) + self.listeners = defaultdict(lambda: (lambda x, y: ())) def set(self, addr: Union[str, int], val: int): if isinstance(addr, str): if not addr in self.name_to_addr: print("Unknown CSR register {}".format(addr)) addr = self.name_to_addr[addr] + self.listeners[addr](self.regs[addr], val) self.regs[addr] = val def get(self, addr: Union[str, int]): @@ -76,6 +80,13 @@ class CSR: addr = self.name_to_addr[addr] return self.regs[addr] + def set_listener(self, addr: Union[str, int], listener: Callable[[int, int], None]): + if isinstance(addr, str): + if not addr in self.name_to_addr: + print("Unknown CSR register {}".format(addr)) + addr = self.name_to_addr[addr] + self.listeners[addr] = listener + # mstatus properties def set_mstatus(self, name: str, val: int): """