Skip to content

Commit

Permalink
dynamically create memory.x based on memory map
Browse files Browse the repository at this point in the history
  • Loading branch information
hydrolarus committed Aug 12, 2024
1 parent b9c56c2 commit 20c96e5
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 215 deletions.
68 changes: 63 additions & 5 deletions firmware-binaries/examples/hello/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use std::path::Path;

use memmap_generate as mm;
use memmap_generate::generators::{
generate_rust_wrappers, types::DebugDerive, GenerateConfig, ItemScopeMode,
generate_rust_wrappers, DebugDerive, GenerateConfig, ItemScopeMode,
};
use mm::parse::MemoryMapTree;
use mm::MemoryMapDesc;

/// Put the linker script somewhere the linker can find it.
fn main() {
Expand Down Expand Up @@ -47,10 +49,10 @@ fn main() {
"#;

for (_, ty_src) in &wrappers.type_defs {
for ty_src in wrappers.type_defs.values() {
s += &ty_src.to_string();
}
for (_, dev_src) in &wrappers.device_defs {
for dev_src in wrappers.device_defs.values() {
s += &dev_src.to_string();
}
s += &wrappers.device_instances_struct.to_string();
Expand All @@ -62,10 +64,21 @@ fn main() {

let memory_map_wrapper_path = wrapper_src_path.join("memory_map.rs");

std::fs::write(&memory_map_wrapper_path, wrapper_code).unwrap();
std::fs::write(memory_map_wrapper_path, wrapper_code).unwrap();

let dest_path = out_dir.join("memory.x");
fs::write(dest_path, include_bytes!("memory.x")).expect("Could not write file");
{
let (imem_start, imem_size) = storage_info(&memory_map, "Instruction").unwrap();
let (dmem_start, dmem_size) = storage_info(&memory_map, "Data").unwrap();

let mut input = fs::read_to_string("memory.x").unwrap();
input = input.replace("{imem_start}", &format!("{}", imem_start));
input = input.replace("{dmem_start}", &format!("{}", dmem_start));
input = input.replace("{imem_size}", &format!("{}", imem_size));
input = input.replace("{dmem_size}", &format!("{}", dmem_size));

fs::write(dest_path, &input).expect("Could not write file");
}

if env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "riscv32" {
println!("cargo:rustc-link-arg=-Tmemory.x");
Expand All @@ -76,3 +89,48 @@ fn main() {
println!("cargo:rerun-if-changed=memory.x");
println!("cargo:rerun-if-changed=build.rs");
}

fn storage_info(mmap: &MemoryMapDesc, instance_name: &str) -> Option<(u64, u64)> {
fn find<'t>(tree: &'t MemoryMapTree, inst_name: &str) -> Option<(u64, &'t String)> {
match tree {
MemoryMapTree::Interconnect {
absolute_address: _,
components,
src_location: _,
} => {
for comp in components {
let res = find(&comp.tree, inst_name);
if res.is_some() {
return res;
} else {
continue;
}
}
None
}
MemoryMapTree::DeviceInstance {
absolute_address,
device_name,
instance_name,
src_location: _,
} => {
if instance_name == inst_name {
Some((*absolute_address, device_name))
} else {
None
}
}
}
}

let (addr, device_name) = find(&mmap.tree, instance_name)?;

let device_desc = mmap.devices.get(device_name)?;
let size = device_desc
.registers
.iter()
.map(|reg| reg.address + reg.size)
.max()?;

Some((addr, size))
}
4 changes: 2 additions & 2 deletions firmware-binaries/examples/hello/memory.x
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ SPDX-License-Identifier: CC0-1.0

MEMORY
{
IMEM : ORIGIN = 0x80000000, LENGTH = 64K
DMEM : ORIGIN = 0x40000000, LENGTH = 64K
IMEM : ORIGIN = {imem_start}, LENGTH = {imem_size}
DMEM : ORIGIN = {dmem_start}, LENGTH = {dmem_size}
}

REGION_ALIAS("REGION_TEXT", IMEM);
Expand Down
Loading

0 comments on commit 20c96e5

Please sign in to comment.