From c6c9aa4e5f3e5a01fcb9ccabf6dbd2f6da277650 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Mon, 15 Jul 2024 08:55:57 +0200 Subject: Add the notion of mapping MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mappings and segments are fundamental blocks on how code is distributed both on the actual ROM File and in the memory layout. Signed-off-by: Miquel Sabaté Solà --- lib/xixanta/src/mapping.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 lib/xixanta/src/mapping.rs (limited to 'lib/xixanta/src/mapping.rs') diff --git a/lib/xixanta/src/mapping.rs b/lib/xixanta/src/mapping.rs new file mode 100644 index 0000000..56acf18 --- /dev/null +++ b/lib/xixanta/src/mapping.rs @@ -0,0 +1,66 @@ +use crate::instruction::{Fill, Node, PString}; +use std::collections::HashMap; + +use crate::errors::ParseError; +type Result = std::result::Result; + +#[derive(Debug, Clone, Eq, Ord, PartialEq, PartialOrd)] +pub struct Segment { + pub name: String, + pub start: u16, + pub size: usize, + pub fill_value: Option, +} + +#[derive(Debug)] +pub struct Mapping { + pub segments: Vec, + pub nodes: HashMap>, + current: String, +} + +impl Mapping { + pub fn new(mut segments: Vec) -> Self { + segments.sort_by(|a, b| b.start.cmp(&a.start)); + + let mut nodes = HashMap::new(); + for segment in segments.iter() { + nodes.insert(segment.name.clone(), vec![]); + } + + let current_segment = &segments.first().unwrap().name.clone(); + + Mapping { + segments, + nodes, + current: current_segment.to_string(), + } + } + + pub fn reset(&mut self) { + // TODO + } + + pub fn switch(&mut self, id: &PString) -> Result<()> { + if !self.nodes.contains_key(&id.value) { + return Err( + id.parser_error(format!("segment '{}' has not been defined", id.value).as_str()) + ); + } + + id.value.clone_into(&mut self.current); + Ok(()) + } + + pub fn current(&self) -> &Vec { + self.nodes.get(&self.current).unwrap() + } + + pub fn current_mut(&mut self) -> &mut Vec { + self.nodes.get_mut(&self.current).unwrap() + } + + pub fn push(&mut self, node: Node) { + self.nodes.get_mut(&self.current).unwrap().push(node); + } +} -- cgit v1.2.3