From 9893c2460a7250782c438082fa150218b74c5ce1 Mon Sep 17 00:00:00 2001 From: Florent Kermarrec Date: Thu, 20 Sep 2018 00:46:06 +0200 Subject: [PATCH] integration/soc_core: add get_mem_data function to read memory content from file --- litex/soc/integration/soc_core.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/litex/soc/integration/soc_core.py b/litex/soc/integration/soc_core.py index f407cd0b..5e05aa1b 100644 --- a/litex/soc/integration/soc_core.py +++ b/litex/soc/integration/soc_core.py @@ -1,3 +1,4 @@ +import struct import inspect from operator import itemgetter @@ -10,7 +11,7 @@ from litex.soc.interconnect import wishbone, csr_bus, wishbone2csr from litex.soc.integration.cpu_interface import cpu_endianness -__all__ = ["mem_decoder", "SoCCore", "soc_core_args", "soc_core_argdict"] +__all__ = ["mem_decoder", "get_mem_data", "SoCCore", "soc_core_args", "soc_core_argdict"] def version(with_time=True): @@ -28,6 +29,22 @@ def mem_decoder(address, start=26, end=29): return lambda a: a[start:end] == ((address >> (start+2)) & (2**(end-start))-1) +def get_mem_data(filename, mem_size): + data = [] + with open(filename, "rb") as mem_file: + while True: + w = mem_file.read(4) + if not w: + break + data.append(struct.unpack(">I", w)[0]) + data_size = len(data)*4 + assert data_size > 0 + assert data_size < mem_size, ( + "file is too big: {}/{} bytes".format( + data_size, mem_size)) + return data + + class ReadOnlyDict(dict): def __readonly__(self, *args, **kwargs): raise RuntimeError("Cannot modify ReadOnlyDict") -- 2.30.2