Skip to content

Commit

Permalink
soc/builder: add initialize_memory()
Browse files Browse the repository at this point in the history
Allows the target soc to override memory initialization:
  rom and/or ram.
  • Loading branch information
Andrew Dennison committed Apr 2, 2024
1 parent 87137c3 commit a763e82
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
5 changes: 4 additions & 1 deletion litex/soc/integration/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,11 @@ def build(self, **kwargs):
self._prepare_rom_software()
self._generate_rom_software(compile_bios=use_bios)

# Allow soc to override the memory initialisation.
self.soc.initialize_memory(self.software_dir, **kwargs)

# Initialize ROM.
if use_bios and self.soc.integrated_rom_size:
if use_bios and self.soc.integrated_rom_size and not getattr(self.soc, "rom").mem.init:
self._initialize_rom_software()

# Translate compile_gateware to run.
Expand Down
42 changes: 42 additions & 0 deletions litex/soc/integration/soc_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,48 @@ def add_memory_region(self, name, origin, length, type="cached"):
def add_csr_region(self, name, origin, busword, obj):
self.csr_regions[name] = SoCCSRRegion(origin, busword, obj)

def initialize_memory(self, software_dir, **kwargs):
"""initialize_memory
The target SoC can implement this function to override the memory initialisation
during the build to load a program or data to main_ram and/or rom.
Parameters
----------
software_dir : str
Builder software_dir where the soc libs and bios are built.
kwargs
Builder kwargs for any additional context if required
.
Example:
class MySoC(SoCCore):
def __init__(self,
...
self.add_config("MAIN_RAM_INIT") # firmware is in ram
def initialize_memory(self, software_dir, **kwargs):
if self.cpu_type is None:
return
filename = os.path.join(software_dir, "firmware", "firmware.bin")
data = get_mem_data(filename, endianness=self.cpu.endianness)
self.init_rom(name="main_ram", contents=data, auto_size=False)
def main():
...
builder = Builder(soc, **parser.builder_argdict)
# add custom firmware: compiled by connecting here and stored in initialize_memory()
src="firmware"
src_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), src)
builder.add_software_package(src, src_dir)
builder.build(**parser.toolchain_argdict)
"""
pass

# SoCCore arguments --------------------------------------------------------------------------------

def soc_core_args(parser):
Expand Down

0 comments on commit a763e82

Please sign in to comment.