From 5ef4e8c792950b4131f8e9bc1f80f3f4a0c0910b Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Tue, 7 Feb 2023 10:08:34 +1100 Subject: [PATCH] mbc trait --- src/processor/memory/rom.rs | 21 +++++++++----------- src/processor/memory/rom/mbcs.rs | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 src/processor/memory/rom/mbcs.rs diff --git a/src/processor/memory/rom.rs b/src/processor/memory/rom.rs index 5715854..fbd2ef4 100644 --- a/src/processor/memory/rom.rs +++ b/src/processor/memory/rom.rs @@ -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, - mbc: MBC, + mbc: Box, } 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 = 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) } } diff --git a/src/processor/memory/rom/mbcs.rs b/src/processor/memory/rom/mbcs.rs new file mode 100644 index 0000000..8e09a73 --- /dev/null +++ b/src/processor/memory/rom/mbcs.rs @@ -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, +} + +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, +} + +impl MBC for MBC1 { + fn get(&self, address: Address) -> u8 { + self.data[address as usize] + } + + fn set(&mut self, address: Address, data: u8) { + return; + } +}