mbc trait
This commit is contained in:
parent
5fe3d2ef2e
commit
5ef4e8c792
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
34
src/processor/memory/rom/mbcs.rs
Normal file
34
src/processor/memory/rom/mbcs.rs
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue