aboutsummaryrefslogtreecommitdiff
path: root/src/memory.rs
blob: 3409ed4aa243ba4d757fca1e00bb04376d984e85 (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
use isa::{Instruction};
use binary::{Binary};

pub struct Memory {
    memory: Vec<u32>,
}

impl Memory {
    pub fn new(size: usize, binary: Binary) -> Memory {
        let mut memory = binary.words.clone();
        if size > memory.len() {
            let remainder = size - memory.len();
            memory.reserve(remainder);
        }
        Memory {
            memory: memory,
        }
    }

    pub fn read_word(&self, address: usize) -> Option<u32> {
        // memory is word-addressed but addresses are byte-addressed
        self.memory.get(address / 4).map(Clone::clone)
    }

    pub fn read_instruction(&self, pc: usize) -> Option<Instruction> {
        self.memory.get(pc / 4).map(Clone::clone).map(Instruction::new)
    }
}