Skip to content

Commit

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

pub struct MBC2 {
rom: Vec<u8>,
ram: Vec<u8>,
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<u8>) -> Self {
Self {
rom,
ram: vec![0x00; 512],
ram_enabled: false,
rom_bank: 1
}
}
}

3 changes: 2 additions & 1 deletion src/mbc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pub mod mode;
pub mod rom_only;
pub mod mbc1;
pub mod mbc3;
pub mod mbc5;
pub mod mbc5;
pub mod mbc2;
2 changes: 2 additions & 0 deletions src/mmu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,6 +39,7 @@ impl MMU {
let mbc: Box<dyn MBC> = 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)
Expand Down

0 comments on commit ece218d

Please sign in to comment.