Small refactor and define rom trait

This commit is contained in:
2022-12-10 15:26:48 -05:00
parent c0e0afb1c2
commit 4678647cf0
8 changed files with 80 additions and 3 deletions
+28
View File
@@ -0,0 +1,28 @@
use super::{ROM, load_rom};
pub struct LoROM {
data: Vec<u8>,
}
impl LoROM {
pub fn new() -> Self {
Self {
data: vec![],
}
}
}
impl ROM for LoROM {
fn load(&mut self, filename: &String) -> std::io::Result<bool> {
load_rom(filename, &mut self.data)
}
fn read(&self, address: u32) -> u8 {
match self.data.get(address as usize) {
Some(byte) => *byte,
None => 0xFF,
}
}
fn write(&mut self, _address: u32, _value: u8) {}
}
+17
View File
@@ -0,0 +1,17 @@
pub mod lo_rom;
use std::fs::File;
use std::io::Read;
pub fn load_rom(filename: &String, target: &mut Vec<u8>) -> std::io::Result<bool> {
let mut file = File::open(filename)?;
file.read_to_end(target)?;
// TODO: header checksum here
Ok(true)
}
pub trait ROM {
fn load(&mut self, filename: &String) -> std::io::Result<bool>;
fn read(&self, address: u32) -> u8;
fn write(&mut self, address: u32, value: u8);
}