55 lines
2.2 KiB
Python
55 lines
2.2 KiB
Python
|
|
'''
|
|||
|
|
5. 缓存交互模块(midware/bus/cache_handler.py)
|
|||
|
|
实现与 SJA1000/61580 共享缓存的交互,提供WriteDoubleWord/ReadDoubleWord核心操作,对接虚拟芯片:
|
|||
|
|
'''
|
|||
|
|
from loguru import logger
|
|||
|
|
|
|||
|
|
class CacheHandler:
|
|||
|
|
"""缓存交互层,实现sysbus WriteDoubleWord/ReadDoubleWord操作"""
|
|||
|
|
def __init__(self):
|
|||
|
|
# 模拟共享缓存(字典实现,key=内存地址,value=32位无符号整数)
|
|||
|
|
self.shared_cache = {}
|
|||
|
|
logger.info("共享缓存初始化完成(模拟SJA1000/61580)")
|
|||
|
|
|
|||
|
|
def write_double_word(self, mem_addr, data):
|
|||
|
|
"""
|
|||
|
|
写入双字(32位)数据到共享缓存
|
|||
|
|
:param mem_addr: 内存地址(int)
|
|||
|
|
:param data: 16/32位数据(int),自动转为32位无符号整数
|
|||
|
|
"""
|
|||
|
|
if not isinstance(mem_addr, int) or not isinstance(data, int):
|
|||
|
|
logger.error("写入缓存参数错误,地址/数据必须为整数")
|
|||
|
|
return False
|
|||
|
|
# 转为32位无符号整数
|
|||
|
|
|
|||
|
|
##给RENODE的写入的语句未定
|
|||
|
|
self.shared_cache[mem_addr] = data & 0xFFFFFFFF
|
|||
|
|
logger.debug(f"写入缓存:地址{hex(mem_addr)},数据{hex(self.shared_cache[mem_addr])}")
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
def read_double_word(self, mem_addr):
|
|||
|
|
"""
|
|||
|
|
从共享缓存读取双字(32位)数据
|
|||
|
|
:param mem_addr: 内存地址(int)
|
|||
|
|
:return: 32位无符号整数,地址不存在返回0
|
|||
|
|
"""
|
|||
|
|
if not isinstance(mem_addr, int):
|
|||
|
|
logger.error("读取缓存参数错误,地址必须为整数")
|
|||
|
|
return 0
|
|||
|
|
data = self.shared_cache.get(mem_addr, 0)
|
|||
|
|
logger.debug(f"读取缓存:地址{hex(mem_addr)},数据{hex(data)}")
|
|||
|
|
return data
|
|||
|
|
|
|||
|
|
def clear_cache(self, mem_addr=None):
|
|||
|
|
"""清空指定地址/全部缓存"""
|
|||
|
|
if mem_addr:
|
|||
|
|
if mem_addr in self.shared_cache:
|
|||
|
|
del self.shared_cache[mem_addr]
|
|||
|
|
logger.debug(f"清空缓存地址:{hex(mem_addr)}")
|
|||
|
|
else:
|
|||
|
|
self.shared_cache.clear()
|
|||
|
|
logger.info("清空全部共享缓存")
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
# 单例模式,全局唯一缓存实例
|
|||
|
|
cache_handler = CacheHandler()
|