diff options
Diffstat (limited to 'src/rom.rs')
-rw-r--r-- | src/rom.rs | 37 |
1 files changed, 33 insertions, 4 deletions
@@ -1,7 +1,8 @@ use std::fmt; +use crate::memory::Memory; pub struct Rom { - pub array: [u8; u16::max_value() as usize + 1], + array: [u8; u16::max_value() as usize + 1], } impl fmt::Debug for Rom { @@ -83,8 +84,26 @@ impl HexLine { } impl Rom { + /// Empty constructor + pub fn new() -> Self { + Self { array: [0; u16::max_value() as usize + 1] } + } + + /// Constructor from hex pub fn from_hex(hex: String) -> Result<Self, String> { - let mut array = [0; u16::max_value() as usize + 1]; + Self::new().hex(hex) + } + + /// Method for sequential consuming building + pub fn hex(mut self, hex: String) -> Result<Self, String> { + match self.load_hex(hex) { + Ok(_) => Ok(self), + Err(string) => Err(string), + } + } + + /// Method for occasional incremental hex loading + pub fn load_hex(&mut self, hex: String) -> Result<(), String> { for (linenum, line) in hex.lines().enumerate() { let hex_line = match HexLine::from(line) { Ok(value) => value, @@ -94,12 +113,22 @@ impl Rom { match hex_line.rectyp { Rectyp::Data => { for (ptr, byte) in hex_line.data.iter().enumerate() { - array[ptr + offset as usize] = *byte; + self.array[ptr + offset as usize] = *byte; } } Rectyp::EndOfFile => {} } } - Ok(Self { array }) + Ok(()) + } +} + +impl Memory<u16> for Rom { + fn get(&self, a: u16) -> u8 { + self.array[a as usize] + } + + fn set(&mut self, a: u16, v: u8) { + self.array[a as usize] = v; } } |