diff options
Diffstat (limited to 'src/ram.rs')
-rw-r--r-- | src/ram.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/ram.rs b/src/ram.rs new file mode 100644 index 0000000..7027a0e --- /dev/null +++ b/src/ram.rs @@ -0,0 +1,50 @@ +use super::bus::Bus; +use std::fmt; + +const RESET_VALUE_ACC: u8 = 0x00; +const RESET_VALUE_SP: u8 = 0x07; + +pub const RAM_OFFSET_ACC: u8 = 0xE0; +pub const RAM_OFFSET_SP: u8 = 0x80; + +pub struct Ram { + array: [u8; u8::max_value() as usize + 1], +} + +impl fmt::Debug for Ram { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let mut v: Vec<char> = vec![]; + for (c, x) in self.array.iter().enumerate() { + for hex_char in format!("{:02X}", *x).chars() { + v.push(hex_char); + } + v.push(' '); + if c % 8 == 7 { + v.push('\n'); + } + } + write!(f, "{}", v.iter().collect::<String>()) + } +} + +impl Ram { + /// Empty constructor + pub fn new() -> Self { + let mut array = [0; u8::max_value() as usize + 1]; + array[RAM_OFFSET_SP as usize] = RESET_VALUE_SP; + array[RAM_OFFSET_ACC as usize] = RESET_VALUE_ACC; + Self { + array, + } + } +} + +impl Bus<u8> for Ram { + fn get(&self, a: u8) -> u8 { + self.array[a as usize] + } + + fn set(&mut self, a: u8, v: u8) { + self.array[a as usize] = v; + } +} |