integration/soc_core: add get_mem_data function to read memory content from file
authorFlorent Kermarrec <florent@enjoy-digital.fr>
Wed, 19 Sep 2018 22:46:06 +0000 (00:46 +0200)
committerFlorent Kermarrec <florent@enjoy-digital.fr>
Wed, 19 Sep 2018 22:46:06 +0000 (00:46 +0200)
litex/soc/integration/soc_core.py

index f407cd0bf08c58226e80f1a9636419089413f233..5e05aa1b505715f57c4569cb6ce31d22555216ea 100644 (file)
@@ -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")