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
49
50
51
use std::fs;
use std::io;
use std::io::Read;
use std::path::Path;
use std::result::Result;
pub struct Rom {
data: Vec<u8>,
offset: u16,
}
impl Rom {
pub fn new(capacity: usize, offset: u16, pattern: u8) -> Self {
let mut data = vec![0x00; capacity];
for i in 0..data.len() {
data[i] = pattern;
}
Self { data, offset }
}
pub fn load(path: &Path, offset: u16) -> Result<Rom, io::Error> {
info!(target: "mem", "Loading ROM {:?}", path.to_str().unwrap());
let mut data = Vec::new();
let mut file = fs::File::open(path)?;
file.read_to_end(&mut data)?;
Ok(Rom { data, offset })
}
pub fn read(&self, address: u16) -> u8 {
self.data[(address - self.offset) as usize]
}
pub fn write(&mut self, _address: u16, _value: u8) {
panic!("writes to rom are not supported")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn read_address() {
let rom = Rom::load(&Path::new("res/rom/basic.rom"), 0x0000).unwrap();
assert_eq!(0x94, rom.read(0x0000));
}
}