Source code for luna_soc.generate.c

#
# This file is part of LUNA.
#
# Copyright (c) 2023-2025 Great Scott Gadgets <info@greatscottgadgets.com>
# SPDX-License-Identifier: BSD-3-Clause

"""Generate a C library for SoC designs."""

import datetime

from amaranth             import unsigned

from amaranth_soc.memory  import MemoryMap, ResourceInfo
from amaranth_soc         import csr

from ..gateware.cpu.ic    import InterruptMap
from .                    import introspect


[docs] class LinkerScript(): def __init__(self, memory_map: MemoryMap, reset_addr: int = 0x00000000): self.memory_map = memory_map self.reset_addr = reset_addr
[docs] def generate(self, file=None, macro_name="SOC_RESOURCES", platform_name="Generic Platform"): """ Generates a ldscript that holds our primary RAM and ROM regions. Parameters: file -- Optional. If provided, this will be treated as the file= argument to the print() function. This can be used to generate file content instead of printing to the terminal. """ def emit(content): """ Utility function that emits a string to the targeted file. """ print(content, file=file) # TODO this should be determined by introspection memories = ["ram", "rom", "blockram", "spiflash", "bootrom", "scratchpad", "mainram"] # Insert our automatically generated header. emit("/**") emit(" * Linker memory regions.") emit(" *") emit(" * Automatically generated by LUNA; edits will be discarded on rebuild.") emit(" * (Most header files phrase this 'Do not edit.'; be warned accordingly.)") emit(" *") emit(f" * Generated: {datetime.datetime.now()}.") emit(" */") emit("") # memory regions emit("MEMORY") emit("{") window: MemoryMap name: MemoryMap.Name for window, name, (start, end, ratio) in self.memory_map.windows(): name = name[0] if name not in memories: continue if self.reset_addr >= start and self.reset_addr < end: start = self.reset_addr emit(f" {name} : ORIGIN = 0x{start:08x}, LENGTH = 0x{end-start:08x}") emit("}") emit("")