summaryrefslogtreecommitdiff
path: root/src/ram.rs
blob: 6278b1c94b7934fc1a1c565489acba3131e35506 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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;
    }
}