Skip to content

Commit

Permalink
MBC5
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed Dec 15, 2023
1 parent 0753603 commit 3e5baf3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
58 changes: 58 additions & 0 deletions src/mbc/mbc5.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use crate::mbc::mode::MBC;
use crate::memory::Memory;

pub struct MBC5 {
rom: Vec<u8>,
ram: Vec<u8>,
ram_enabled: bool,
rom_bank: usize,
ram_bank: usize
}

impl Memory for MBC5 {
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..=0xBFFF => {
if self.ram_enabled {
self.ram[a as usize + self.ram_bank * 0x2000 - 0xA000]
} else {
0x00
}
}
_ => panic!("Read to unsupported MBC5 address ({:#06x})!", a),
}
}

fn write(&mut self, a: u16, v: u8) {
match a {
0x0000..=0x1FFF => self.ram_enabled = v & 0x0F == 0x0A,
0x2000..=0x2FFF => self.rom_bank = (self.rom_bank & 0x100) | (v as usize),
0x3000..=0x3FFF => self.rom_bank = (self.rom_bank & 0x0ff) | (((v & 0x01) as usize) << 8),
0x4000..=0x5FFF => self.ram_bank = (v & 0x0f) as usize,
// Unknown writes
0x6000..=0x7FFF => {},
0xA000..=0xBFFF => {
if self.ram_enabled {
self.ram[a as usize + self.ram_bank * 0x2000 - 0xA000] = v;
}
}
_ => panic!("Write to unsupported MBC5 address ({:#06x})!", a),
}
}
}

impl MBC for MBC5 { }

impl MBC5 {
pub fn new(rom: Vec<u8>) -> Self {
Self {
rom,
ram: vec![0x00; 131072],
ram_enabled: false,
rom_bank: 0x00,
ram_bank: 0x00
}
}
}
3 changes: 2 additions & 1 deletion src/mbc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod mode;
pub mod rom_only;
pub mod mbc1;
pub mod mbc3;
pub mod mbc3;
pub mod mbc5;
2 changes: 2 additions & 0 deletions src/mmu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::mbc::mode::{MBC, MBCMode};
use crate::mbc::rom_only::ROMOnly;
use crate::mbc::mbc1::MBC1;
use crate::mbc::mbc3::MBC3;
use crate::mbc::mbc5::MBC5;
use crate::memory::Memory;
use crate::ppu::PPU;
use crate::timer::Timer;
Expand Down Expand Up @@ -38,6 +39,7 @@ impl MMU {
MBCMode::RomOnly => Box::new(ROMOnly::new(rom)),
MBCMode::MBC1 => Box::new(MBC1::new(rom)),
MBCMode::MBC3 => Box::new(MBC3::new(rom)),
MBCMode::MBC5 => Box::new(MBC5::new(rom)),
v => panic!("Unsupported MBC type! {:}", v)
};

Expand Down

0 comments on commit 3e5baf3

Please sign in to comment.