From ab84ad75629b0a4124221023ca91411d2cd62a32 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Tue, 25 Mar 2025 12:46:49 +1300 Subject: Restructure program This commit also includes changes to devices according to the latest devices specification, in particular the math and system devices. --- src/types/debug_symbols.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/types/debug_symbols.rs (limited to 'src/types/debug_symbols.rs') diff --git a/src/types/debug_symbols.rs b/src/types/debug_symbols.rs new file mode 100644 index 0000000..f4fc412 --- /dev/null +++ b/src/types/debug_symbols.rs @@ -0,0 +1,59 @@ +use crate::*; + + +pub struct DebugSymbols { + symbols: Vec +} + +impl DebugSymbols { + /// Load debug symbols from a symbols file. + pub fn from_path_opt>(path: Option

) -> Self { + let mut symbols = Vec::new(); + if let Some(path) = path { + if let Ok(string) = std::fs::read_to_string(path) { + for line in string.lines() { + if let Some(symbol) = DebugSymbol::from_line(line) { + symbols.push(symbol); + } + } + } + } + symbols.sort_by_key(|s| s.address); + Self { symbols } + } + + /// Return the symbol matching a given address. + pub fn for_address(&self, address: u16) -> Option<&DebugSymbol> { + if self.symbols.is_empty() { return None; } + let symbol = match self.symbols.binary_search_by_key(&address, |s| s.address) { + Ok(index) => self.symbols.get(index)?, + Err(index) => self.symbols.get(index.checked_sub(1)?)?, + }; + Some(&symbol) + } +} + + +pub struct DebugSymbol { + pub address: u16, + pub name: String, + pub location: Option, +} + +impl DebugSymbol { + pub fn from_line(line: &str) -> Option { + if let Some((address, line)) = line.split_once(' ') { + let address = u16::from_str_radix(address, 16).ok()?; + if let Some((name, location)) = line.split_once(' ') { + let name = name.to_string(); + let location = Some(location.to_string()); + Some( DebugSymbol { address, name, location } ) + } else { + let name = line.to_string(); + Some( DebugSymbol { address, name, location: None } ) + } + } else { + None + } + } +} -- cgit v1.2.3-70-g09d2