This commit is contained in:
Alex Janka 2023-03-02 18:53:26 +11:00
parent e0979f5049
commit 2ba3de1894
3 changed files with 75 additions and 1 deletions

View file

@ -6,7 +6,7 @@ use std::{
str::from_utf8_unchecked,
};
use self::mbcs::{Mbc, Mbc1, Mbc3, Mbc5, None};
use self::mbcs::{Mbc, Mbc1, Mbc2, Mbc3, Mbc5, None};
mod mbcs;
@ -104,6 +104,8 @@ impl Rom {
0x01 => Box::new(Mbc1::init(data, rom_size, 0, None)),
0x02 => Box::new(Mbc1::init(data, rom_size, ram_size, None)),
0x03 => Box::new(Mbc1::init(data, rom_size, ram_size, Some(save_path))),
0x05 => Box::new(Mbc2::init(data, rom_size, None)),
0x06 => Box::new(Mbc2::init(data, rom_size, Some(save_path))),
0x0F => Box::new(Mbc3::init(data, rom_size, 0, true, Some(save_path))),
0x10 => Box::new(Mbc3::init(data, rom_size, ram_size, true, Some(save_path))),
0x11 => Box::new(Mbc3::init(data, rom_size, 0, false, None)),

View file

@ -1,10 +1,12 @@
use crate::processor::memory::Address;
mod mbc1;
mod mbc2;
mod mbc3;
mod mbc5;
mod none;
pub use mbc1::Mbc1;
pub use mbc2::Mbc2;
pub use mbc3::Mbc3;
pub use mbc5::Mbc5;
pub use none::None;

View file

@ -0,0 +1,70 @@
use std::path::PathBuf;
use crate::processor::memory::{rom::MaybeBufferedSram, Address};
use super::{rom_banks, Mbc, ROM_BANK_SIZE};
pub struct Mbc2 {
data: Vec<u8>,
rom_len: usize,
rom_bank: u8,
ram: MaybeBufferedSram,
ram_enabled: bool,
}
impl Mbc2 {
pub fn init(data: Vec<u8>, rom_size: u8, save_file: Option<PathBuf>) -> Self {
let rom_len = rom_banks(rom_size) * ROM_BANK_SIZE;
let ram = MaybeBufferedSram::new(save_file, 512);
Self {
data,
rom_len,
rom_bank: 1,
ram,
ram_enabled: false,
}
}
}
impl Mbc for Mbc2 {
fn get(&self, address: Address) -> u8 {
match address {
0x0..0x4000 => self.data[address as usize],
0x4000..0x8000 => {
self.data[((address as usize - 0x4000) + (0x4000 * self.rom_bank as usize))
% self.rom_len]
}
_ => panic!("passed wrong address to mbc"),
}
}
fn get_ram(&self, address: Address) -> u8 {
0xF0 | (0x0F & self.ram.get((address - 0xA000) as usize % 512))
}
fn set(&mut self, address: Address, data: u8) {
if address & (1 << 8) == (1 << 8) {
// bit 8 is set - rom bank
self.rom_bank = data & 0xF;
if self.rom_bank == 0 {
self.rom_bank = 1;
}
} else {
// bit 8 is clear - ram enable
self.ram_enabled = data == 0x0A;
}
}
fn set_ram(&mut self, address: Address, data: u8) {
self.ram.set((address - 0xA000) as usize % 512, data);
}
fn mbc_type(&self) -> String {
String::from("MBC2")
}
fn flush(&mut self) {
self.ram.flush();
}
}