From ece218d3cec9abf815096a0fdd21853e71820ebc Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Fri, 15 Dec 2023 00:18:52 -0500 Subject: [PATCH] MBC2 --- src/mbc/mbc2.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ src/mbc/mod.rs | 3 ++- src/mmu.rs | 2 ++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/mbc/mbc2.rs diff --git a/src/mbc/mbc2.rs b/src/mbc/mbc2.rs new file mode 100644 index 0000000..76a2131 --- /dev/null +++ b/src/mbc/mbc2.rs @@ -0,0 +1,62 @@ +use crate::mbc::mode::MBC; +use crate::memory::Memory; + +pub struct MBC2 { + rom: Vec, + ram: Vec, + ram_enabled: bool, + rom_bank: usize +} + +impl Memory for MBC2 { + fn read(&self, a: u16) -> u8 { + match a { + 0x0000..=0x3FFF => self.rom[a as usize], + 0x4000..=0x7FFF => self.rom[a as usize + self.rom_bank * 0x4000 - 0x4000], + 0xA000..=0xA1FF => { + if self.ram_enabled { + self.ram[(a - 0xA000) as usize] + } else { + 0x00 + } + } + _ => panic!("Read to unsupported MBC2 address ({:#06x})!", a), + } + } + + fn write(&mut self, a: u16, v: u8) { + let v = v & 0x0F; + match a { + 0x0000..=0x1FFF => { + if a & 0x0100 == 0 { + self.ram_enabled = v == 0x0A; + } + }, + 0x2000..=0x3FFF => { + if a & 0x0100 != 0 { + self.rom_bank = v as usize; + } + }, + 0xA000..=0xA1FF => { + if self.ram_enabled { + self.ram[(a - 0xa000) as usize] = v + } + } + _ => panic!("Write to unsupported MBC2 address ({:#06x})!", a), + } + } +} + +impl MBC for MBC2 { } + +impl MBC2 { + pub fn new(rom: Vec) -> Self { + Self { + rom, + ram: vec![0x00; 512], + ram_enabled: false, + rom_bank: 1 + } + } +} + diff --git a/src/mbc/mod.rs b/src/mbc/mod.rs index dfc7870..b996342 100644 --- a/src/mbc/mod.rs +++ b/src/mbc/mod.rs @@ -2,4 +2,5 @@ pub mod mode; pub mod rom_only; pub mod mbc1; pub mod mbc3; -pub mod mbc5; \ No newline at end of file +pub mod mbc5; +pub mod mbc2; \ No newline at end of file diff --git a/src/mmu.rs b/src/mmu.rs index a100050..870e69a 100644 --- a/src/mmu.rs +++ b/src/mmu.rs @@ -2,6 +2,7 @@ use bitflags::bitflags; use crate::mbc::mode::{MBC, MBCMode}; use crate::mbc::rom_only::ROMOnly; use crate::mbc::mbc1::MBC1; +use crate::mbc::mbc2::MBC2; use crate::mbc::mbc3::MBC3; use crate::mbc::mbc5::MBC5; use crate::memory::Memory; @@ -38,6 +39,7 @@ impl MMU { let mbc: Box = match mbc_mode { MBCMode::RomOnly => Box::new(ROMOnly::new(rom)), MBCMode::MBC1 => Box::new(MBC1::new(rom)), + MBCMode::MBC2 => Box::new(MBC2::new(rom)), MBCMode::MBC3 => Box::new(MBC3::new(rom)), MBCMode::MBC5 => Box::new(MBC5::new(rom)), v => panic!("Unsupported MBC type! {:}", v)