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 = 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::()) } } 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 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; } }