diff options
author | Oxore <oxore@protonmail.com> | 2020-03-04 02:06:37 +0300 |
---|---|---|
committer | Oxore <oxore@protonmail.com> | 2020-03-04 02:19:23 +0300 |
commit | 52b665108f6f2203df251eec273184b142ee2e16 (patch) | |
tree | 58703ea1a50960a10001cd737f9bb6ec3706bdcd /src/rom.rs | |
parent | e3bf2663362b7362569679bbbd15287e73a8ad2a (diff) |
Add Memory trait for Rom, refactor instantiating
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; } } |