From 62c2eef4ecd6b04e6d3e42b7f45c84cdd7b72382 Mon Sep 17 00:00:00 2001 From: Alex Forencich Date: Thu, 23 Mar 2023 23:44:29 -0700 Subject: [PATCH] Add SparseMemoryRegion object Signed-off-by: Alex Forencich --- cocotbext/axi/__init__.py | 2 +- cocotbext/axi/address_space.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/cocotbext/axi/__init__.py b/cocotbext/axi/__init__.py index 156b87b..5c9f63f 100644 --- a/cocotbext/axi/__init__.py +++ b/cocotbext/axi/__init__.py @@ -27,7 +27,7 @@ from .version import __version__ from .constants import AxiBurstType, AxiBurstSize, AxiLockType, AxiCacheBit, AxiProt, AxiResp from .address_space import MemoryInterface, Window, WindowPool -from .address_space import Region, MemoryRegion, PeripheralRegion +from .address_space import Region, MemoryRegion, SparseMemoryRegion, PeripheralRegion from .address_space import AddressSpace, Pool from .axis import AxiStreamFrame, AxiStreamBus, AxiStreamSource, AxiStreamSink, AxiStreamMonitor diff --git a/cocotbext/axi/address_space.py b/cocotbext/axi/address_space.py index f16aaf2..8e0d8a8 100644 --- a/cocotbext/axi/address_space.py +++ b/cocotbext/axi/address_space.py @@ -25,6 +25,7 @@ THE SOFTWARE. import mmap from .buddy_allocator import BuddyAllocator +from .sparse_memory import SparseMemory from .utils import hexdump, hexdump_lines, hexdump_str @@ -216,6 +217,35 @@ class MemoryRegion(Region): return bytes(self.mem) +class SparseMemoryRegion(Region): + def __init__(self, size=2**64, mem=None, **kwargs): + super().__init__(size, **kwargs) + if mem is None: + mem = SparseMemory(size) + self.mem = mem + + async def _read(self, address, length, **kwargs): + return self.mem.read(address, length) + + async def _write(self, address, data, **kwargs): + self.mem.write(address, data) + + def hexdump(self, address, length, prefix=""): + self.mem.hexdump(address, length, prefix=prefix) + + def hexdump_lines(self, address, length, prefix=""): + return self.mem.hexdump_lines(address, length, prefix=prefix) + + def hexdump_str(self, address, length, prefix=""): + return self.mem.hexdump_str(address, length, prefix=prefix) + + def __getitem__(self, key): + return self.mem[key] + + def __setitem__(self, key, value): + self.mem[key] = value + + class PeripheralRegion(Region): def __init__(self, obj, size, **kwargs): super().__init__(size, **kwargs)