Last active
November 1, 2020 03:42
-
-
Save martinmroz/709e1fc96d7f91974b9de17491a2bf20 to your computer and use it in GitHub Desktop.
Dump MemoryRegion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct MemoryRegion { | |
pub start: u32, | |
pub end: u32, | |
pub buffer: Vec<u8>, | |
pub name: String, | |
} | |
/// I32HEX supports record types: | |
/// - Data | |
/// Specifies up to 255 bytes of data. | |
/// - EndOfFile | |
/// Marks the end of the IHEX file. | |
/// - ExtendedLinearAddress | |
/// Specifies the upper 16 bits of the address. | |
/// - StartLinearAddress | |
/// Specifies the EIP register value on load. | |
pub fn to_i32hex<I>(start_address: u32, buffer: I) -> Vec<ihex::Record> | |
where | |
I: AsRef<[u8]>, | |
{ | |
let input = buffer.as_ref(); | |
let mut records = Vec::new(); | |
// Tracks the address of the current chunk. | |
let mut current_address = start_address; | |
// Tracks the last set Extended Linear Address. IHEX defauts to 0x0. | |
let mut last_set_ela = 0u16; | |
// Process the input in 64 byte chunks (maximum 256). | |
for chunk in input.chunks(64) { | |
let current_address_hi = ((current_address & 0xFFFF_0000) >> 16) as u16; | |
let current_address_lo = (current_address & 0x0000_FFFF) as u16; | |
// Update the high 16 bits of the address if needed. | |
if current_address_hi != last_set_ela { | |
records.push(ihex::Record::ExtendedLinearAddress(current_address_hi)); | |
last_set_ela = current_address_hi; | |
} | |
// Push the current chunk. | |
records.push(ihex::Record::Data { | |
offset: current_address_lo, | |
value: Vec::from(chunk), | |
}); | |
// Increment the address to track the ELA. | |
current_address = current_address + (chunk.len() as u32); | |
} | |
// Append an End-of-File marker. | |
records.push(ihex::Record::EndOfFile); | |
records | |
} | |
fn main() { | |
let region = MemoryRegion { | |
start: 0x50000000, | |
end: 0x5001DFFF, | |
buffer: vec![0; 0x1E000], | |
name: String::from("CPU2_DSPR"), | |
}; | |
let i32hex_records = to_i32hex(region.start, region.buffer); | |
println!("Dump of {}", region.name); | |
println!( | |
"{}", | |
ihex::create_object_file_representation(&i32hex_records).unwrap() | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment