mbc trait

This commit is contained in:
Alex Janka 2023-02-07 10:08:34 +11:00
parent 5fe3d2ef2e
commit 5ef4e8c792
2 changed files with 43 additions and 12 deletions

View file

@ -1,16 +1,13 @@
use crate::processor::memory::Address; use crate::processor::memory::Address;
use std::str::from_utf8_unchecked; use std::str::from_utf8_unchecked;
#[derive(Debug)] use self::mbcs::{MBC, MBC1, NONE};
enum MBC {
ROM, mod mbcs;
MBC1,
}
pub struct ROM { pub struct ROM {
title: String, title: String,
data: Vec<u8>, mbc: Box<dyn MBC>,
mbc: MBC,
} }
impl ROM { impl ROM {
@ -27,12 +24,12 @@ impl ROM {
let _sgb_flag = data[0x146]; let _sgb_flag = data[0x146];
let _rom_size = data[0x148]; let _rom_size = data[0x148];
let _ram_size = data[0x149]; let _ram_size = data[0x149];
let mbc = match data[0x147] { let mbc: Box<dyn MBC> = match data[0x147] {
0x00 => MBC::ROM, 0x00 => Box::new(NONE { data }),
0x01 => MBC::MBC1, 0x01 => Box::new(MBC1 { data }),
_ => panic!("unimplemented mbc: {:#X}", data[0x147]), _ => panic!("unimplemented mbc: {:#X}", data[0x147]),
}; };
Self { title, data, mbc } Self { title, mbc }
} }
pub fn get_title(&self) -> &String { pub fn get_title(&self) -> &String {
@ -40,6 +37,6 @@ impl ROM {
} }
pub(super) fn get(&self, address: Address) -> u8 { pub(super) fn get(&self, address: Address) -> u8 {
self.data[address as usize] self.mbc.get(address)
} }
} }

View file

@ -0,0 +1,34 @@
use crate::processor::memory::Address;
pub(super) trait MBC {
fn get(&self, address: Address) -> u8;
fn set(&mut self, address: Address, data: u8);
}
pub(super) struct NONE {
pub(super) data: Vec<u8>,
}
impl MBC for NONE {
fn get(&self, address: Address) -> u8 {
self.data[address as usize]
}
fn set(&mut self, address: Address, data: u8) {
return;
}
}
pub(super) struct MBC1 {
pub(super) data: Vec<u8>,
}
impl MBC for MBC1 {
fn get(&self, address: Address) -> u8 {
self.data[address as usize]
}
fn set(&mut self, address: Address, data: u8) {
return;
}
}