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 std::str::from_utf8_unchecked;
#[derive(Debug)]
enum MBC {
ROM,
MBC1,
}
use self::mbcs::{MBC, MBC1, NONE};
mod mbcs;
pub struct ROM {
title: String,
data: Vec<u8>,
mbc: MBC,
mbc: Box<dyn MBC>,
}
impl ROM {
@ -27,12 +24,12 @@ impl ROM {
let _sgb_flag = data[0x146];
let _rom_size = data[0x148];
let _ram_size = data[0x149];
let mbc = match data[0x147] {
0x00 => MBC::ROM,
0x01 => MBC::MBC1,
let mbc: Box<dyn MBC> = match data[0x147] {
0x00 => Box::new(NONE { data }),
0x01 => Box::new(MBC1 { data }),
_ => panic!("unimplemented mbc: {:#X}", data[0x147]),
};
Self { title, data, mbc }
Self { title, mbc }
}
pub fn get_title(&self) -> &String {
@ -40,6 +37,6 @@ impl ROM {
}
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;
}
}