Skip to content

Commit

Permalink
MBC3 Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed Dec 15, 2023
1 parent ece218d commit 64fe3c3
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/mbc/mbc3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@ pub struct MBC3 {
ram: Vec<u8>,
rtc: RTC,
ram_enabled: bool,
rom_bank: u8,
ram_bank: u8
rom_bank: usize,
ram_bank: usize
}

impl Memory for MBC3 {
fn read(&self, a: u16) -> u8 {
match a {
0x0000..=0x3FFF => self.rom[a as usize],
0x4000..=0x7FFF => {
let rom_bank = if self.rom_bank == 0x00 { 0x01 } else { self.rom_bank };
self.rom[a as usize + (rom_bank - 1) as usize * 0x4000]
},
0x4000..=0x7FFF => self.rom[a as usize + self.rom_bank * 0x4000 - 0x4000],
0xA000..=0xBFFF => {
if self.ram_enabled {
if self.ram_bank <= 0x03 {
self.ram[a as usize + self.ram_bank as usize * 0x2000 - 0xA000]
self.ram[a as usize + self.ram_bank * 0x2000 - 0xA000]
} else {
self.rtc.read(self.ram_bank as u16)
}
Expand All @@ -37,8 +34,14 @@ impl Memory for MBC3 {
fn write(&mut self, a: u16, v: u8) {
match a {
0x0000..=0x1FFF => self.ram_enabled = v & 0x0F == 0x0A,
0x2000..=0x3FFF => self.rom_bank = v,
0x4000..=0x5FFF => self.ram_bank = (v & 0x0F),
0x2000..=0x3FFF => {
let n = match v & 0x7F {
0x00 => 0x01,
n => n,
};
self.rom_bank = n as usize;
},
0x4000..=0x5FFF => self.ram_bank = (v & 0x0F) as usize,
0x6000..=0x7FFF => {
if v & 0x01 != 0 {
self.rtc.tick();
Expand All @@ -47,7 +50,7 @@ impl Memory for MBC3 {
0xA000..=0xBFFF => {
if self.ram_enabled {
if self.ram_bank <= 0x03 {
self.ram[a as usize + self.ram_bank as usize * 0x2000 - 0xA000] = v;
self.ram[a as usize + self.ram_bank * 0x2000 - 0xA000] = v;
} else {
self.rtc.write(self.ram_bank as u16, v);
}
Expand All @@ -67,8 +70,8 @@ impl MBC3 {
ram: vec![0x00; 32768],
rtc: RTC::new(),
ram_enabled: false,
rom_bank: 0x00,
ram_bank: 0x00
rom_bank: 1,
ram_bank: 0
}
}
}
Expand Down Expand Up @@ -124,7 +127,7 @@ impl Memory for RTC {
0x0A => self.h,
0x0B => self.dl,
0x0C => self.dh,
_ => panic!("No entry"),
_ => panic!("Read to unsupported RTC address ({:#06x})!", a),
}
}

Expand All @@ -135,7 +138,7 @@ impl Memory for RTC {
0x0A => self.h = v,
0x0B => self.dl = v,
0x0C => self.dh = v,
_ => panic!("No entry"),
_ => panic!("Write to unsupported RTC address ({:#06x})!", a),
}
}
}

0 comments on commit 64fe3c3

Please sign in to comment.