soc_core/get_mem_data: add endianness support and use it in builder/initialize_rom...
authorFlorent Kermarrec <florent@enjoy-digital.fr>
Mon, 24 Sep 2018 06:01:32 +0000 (08:01 +0200)
committerFlorent Kermarrec <florent@enjoy-digital.fr>
Mon, 24 Sep 2018 06:01:32 +0000 (08:01 +0200)
litex/soc/integration/builder.py
litex/soc/integration/soc_core.py

index 1bd23bdfe514a563cbffd971e6ea620d16c2c2ab..9f912bfda6b29aa14799701fa140ab08b49f2722 100644 (file)
@@ -4,7 +4,7 @@ import struct
 import shutil
 
 from litex.build.tools import write_to_file
-from litex.soc.integration import cpu_interface, soc_sdram
+from litex.soc.integration import cpu_interface, soc_core, soc_sdram
 
 from litedram import sdram_init
 
@@ -134,20 +134,9 @@ class Builder:
                     subprocess.check_call(["make", "-C", dst_dir, "-f", makefile])
 
     def _initialize_rom(self):
-        bios_file = os.path.join(self.output_dir, "software", "bios",
-                                 "bios.bin")
-        endianness =  self.soc.cpu_or_bridge.endianness
-        with open(bios_file, "rb") as boot_file:
-            boot_data = []
-            while True:
-                w = boot_file.read(4)
-                if not w:
-                    break
-                if endianness == 'little':
-                    boot_data.append(struct.unpack("<I", w)[0])
-                else:
-                    boot_data.append(struct.unpack(">I", w)[0])
-        self.soc.initialize_rom(boot_data)
+        bios_file = os.path.join(self.output_dir, "software", "bios","bios.bin")
+        bios_data = soc_core.get_mem_data(bios_file, self.soc.cpu_or_bridge.endianness)
+        self.soc.initialize_rom(bios_data)
 
     def build(self, toolchain_path=None, **kwargs):
         self.soc.finalize()
index 87678a3167f867c465c297722f034ad9f492da0f..24a013f21b8e527231bf649e0550cc373819a2c0 100644 (file)
@@ -28,14 +28,17 @@ 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=None):
+def get_mem_data(filename, endianness="big", mem_size=None):
     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])
+            if endianness == "little":
+                data.append(struct.unpack("<I", w)[0])
+            else:
+                data.append(struct.unpack(">I", w)[0])
     data_size = len(data)*4
     assert data_size > 0
     if mem_size is not None: