Refactor MBCs

This commit is contained in:
Franco Colmenarez 2021-11-15 18:52:44 -05:00
parent 71692a05a5
commit 2ed5fdb823
2 changed files with 113 additions and 87 deletions

View File

@ -2,7 +2,7 @@ use std::ops::RangeInclusive;
use crate::utils::{ use crate::utils::{
join_bytes join_bytes
}; };
use crate::rom::ROM; use crate::rom::{ROM, load_rom};
use crate::ppu::{ use crate::ppu::{
PPU, PPU,
DMA_ADDRESS, DMA_ADDRESS,
@ -27,7 +27,7 @@ pub const INTERRUPT_ENABLE_ADDRESS: u16 = 0xFFFF;
pub const INTERRUPT_FLAG_ADDRESS: u16 = 0xFF0F; pub const INTERRUPT_FLAG_ADDRESS: u16 = 0xFF0F;
pub struct Bus { pub struct Bus {
game_rom: ROM, game_rom: Box<dyn ROM>,
data: [u8; 0x10000], data: [u8; 0x10000],
pub ppu: PPU, pub ppu: PPU,
pub joypad: Joypad, pub joypad: Joypad,
@ -41,7 +41,7 @@ impl Bus {
println!("Please, specify a ROM file"); println!("Please, specify a ROM file");
std::process::exit(1); std::process::exit(1);
} }
let game_rom = match ROM::load_file(&args[1]) { let game_rom = match load_rom(&args[1]) {
Ok(rom) => rom, Ok(rom) => rom,
Err(err) => { Err(err) => {
println!("Could not read ROM: {}", err); println!("Could not read ROM: {}", err);

View File

@ -14,10 +14,17 @@ pub const RAM_SIZE_ADDRESS: u16 = 0x0149;
pub const ROM_SIZE_ADDRESS: u16 = 0x0148; pub const ROM_SIZE_ADDRESS: u16 = 0x0148;
pub const DESTINATION_CODE_ADDRESS: u16 = 0x014A; pub const DESTINATION_CODE_ADDRESS: u16 = 0x014A;
#[derive(Debug)] pub fn load_rom(filename: &str) -> std::io::Result<Box<dyn ROM>> {
enum Region { let mut file = File::open(filename)?;
Japanese, let mut data = vec![];
NonJapanese, file.read_to_end(&mut data)?;
let info = ROMInfo::from_bytes(&data);
Ok(match info.mbc {
MBC::NoMBC => Box::new(NoMBC::new(data, info)),
MBC::MBC1 => Box::new(MBC1::new(data, info)),
_ => unimplemented!(),
})
} }
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
@ -36,6 +43,12 @@ enum MBC {
BandaiTIMA5, BandaiTIMA5,
} }
#[derive(Debug)]
enum Region {
Japanese,
NonJapanese,
}
#[derive(Debug)] #[derive(Debug)]
enum BankingMode { enum BankingMode {
Simple, Simple,
@ -144,7 +157,41 @@ impl ROMInfo {
} }
} }
pub struct ROM { pub trait ROM {
fn read(&self, address: u16) -> u8;
fn write(&mut self, address: u16, data: u8);
}
pub struct NoMBC {
data: Vec<u8>,
info: ROMInfo,
}
impl NoMBC {
pub fn new(data: Vec<u8>, info: ROMInfo) -> Self {
let rom = Self {
data,
info,
};
println!("MBC {:?}", rom.info.mbc);
println!("Region {:?}", rom.info.region);
rom
}
}
impl ROM for NoMBC {
fn read(&self, address: u16) -> u8 {
match self.data.get(address as usize) {
Some(byte) => *byte,
None => 0xFF,
}
}
fn write(&mut self, _address: u16, _data: u8) {}
}
pub struct MBC1 {
data: Vec<u8>, data: Vec<u8>,
info: ROMInfo, info: ROMInfo,
ram: Vec<u8>, ram: Vec<u8>,
@ -154,21 +201,15 @@ pub struct ROM {
banking_mode: BankingMode, banking_mode: BankingMode,
} }
impl ROM { impl MBC1 {
pub fn load_file(filename: &str) -> std::io::Result<Self> { fn new(data: Vec<u8>, info: ROMInfo) -> Self {
let mut file = File::open(filename)?;
let mut data = vec![];
file.read_to_end(&mut data)?;
let info = ROMInfo::from_bytes(&data);
println!("MBC {:?}", info.mbc); println!("MBC {:?}", info.mbc);
println!("Region {:?}", info.region);
println!("Has RAM {}", info.has_ram); println!("Has RAM {}", info.has_ram);
println!("ROM banks {}", info.rom_banks); println!("ROM banks {}", info.rom_banks);
println!("RAM banks {}", info.ram_banks); println!("RAM banks {}", info.ram_banks);
println!("Region {:?}", info.region);
let ram = Vec::with_capacity(info.ram_size() as usize); let ram = Vec::with_capacity(info.ram_size() as usize);
Self {
Ok(Self {
data, data,
info, info,
ram, ram,
@ -176,18 +217,22 @@ impl ROM {
ram_bank: 0, ram_bank: 0,
ram_enable: false, ram_enable: false,
banking_mode: BankingMode::Simple, banking_mode: BankingMode::Simple,
}) }
} }
pub fn read(&self, address: u16) -> u8 { fn switch_rom_bank(&mut self, bank: u16) {
match self.info.mbc { self.rom_bank = bank;
MBC::NoMBC => { if self.rom_bank > self.info.rom_banks.saturating_sub(1) {
return match self.data.get(address as usize) { self.rom_bank = self.info.rom_banks.saturating_sub(1);
Some(data) => *data, }
None => 0xFF, if self.rom_bank == 0 {
}; self.rom_bank = 1;
}, }
MBC::MBC1 => { }
}
impl ROM for MBC1 {
fn read(&self, address: u16) -> u8 {
if BANK_ZERO.contains(&address) { if BANK_ZERO.contains(&address) {
return self.data[address as usize]; return self.data[address as usize];
} else if BANK_SWITCHABLE.contains(&address) { } else if BANK_SWITCHABLE.contains(&address) {
@ -202,15 +247,9 @@ impl ROM {
}; };
} }
unreachable!("ROM read: Address {} not valid", address); unreachable!("ROM read: Address {} not valid", address);
},
_ => unimplemented!(),
}
} }
pub fn write(&mut self, address: u16, data: u8) { fn write(&mut self, address: u16, data: u8) {
match self.info.mbc {
MBC::NoMBC => {},
MBC::MBC1 => {
if address <= 0x1FFF { // RAM enable register if address <= 0x1FFF { // RAM enable register
if !self.info.has_ram { if !self.info.has_ram {
return; return;
@ -242,18 +281,5 @@ impl ROM {
} }
self.switch_rom_bank(self.rom_bank + (data as u16 >> 5)); self.switch_rom_bank(self.rom_bank + (data as u16 >> 5));
} }
},
_ => unimplemented!(),
}
}
pub fn switch_rom_bank(&mut self, bank: u16) {
self.rom_bank = bank;
if self.rom_bank > self.info.rom_banks.saturating_sub(1) {
self.rom_bank = self.info.rom_banks.saturating_sub(1);
}
if self.rom_bank == 0 {
self.rom_bank = 1;
}
} }
} }