Compare commits

..

No commits in common. "4a1a21a08f141f0f0dacc537276793d6f6522106" and "70e0b5868d8ab473503594bb68d2a68dd1ac42bd" have entirely different histories.

4 changed files with 161 additions and 134 deletions

View File

@ -22,7 +22,7 @@ rtrb = "0.2"
directories-next = "2.0" directories-next = "2.0"
tracing = "0.1" tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["std", "env-filter"] } tracing-subscriber = { version = "0.3", features = ["std", "env-filter"] }
thiserror = "1.0" thiserror = "1.0.30"
[profile.release] [profile.release]
debug = true debug = true

View File

@ -10,7 +10,7 @@ use crate::work_ram::{VariableWorkRam, WorkRam};
pub(crate) const BOOT_SIZE: usize = 0x100; pub(crate) const BOOT_SIZE: usize = 0x100;
#[derive(Debug, Default)] #[derive(Debug)]
pub struct Bus { pub struct Bus {
boot: Option<[u8; BOOT_SIZE]>, // Boot ROM is 256b long boot: Option<[u8; BOOT_SIZE]>, // Boot ROM is 256b long
pub(crate) cart: Option<Cartridge>, pub(crate) cart: Option<Cartridge>,
@ -25,6 +25,24 @@ pub struct Bus {
pub(crate) joyp: Joypad, pub(crate) joyp: Joypad,
} }
impl Default for Bus {
fn default() -> Self {
Self {
boot: None,
cart: None,
ppu: Default::default(),
work_ram: Default::default(),
var_ram: Default::default(),
timer: Default::default(),
int: Default::default(),
apu: Default::default(),
high_ram: Default::default(),
serial: Default::default(),
joyp: Default::default(),
}
}
}
impl Bus { impl Bus {
pub(crate) fn with_boot(rom: [u8; 256]) -> Self { pub(crate) fn with_boot(rom: [u8; 256]) -> Self {
Self { Self {

View File

@ -15,7 +15,7 @@ const ROM_MANUFACTURER_START: usize = 0x13F;
pub(crate) struct Cartridge { pub(crate) struct Cartridge {
mem: Vec<u8>, mem: Vec<u8>,
pub(crate) title: Option<String>, pub(crate) title: Option<String>,
mbc: Box<dyn MbcIo>, mbc: Box<dyn MBCIo>,
} }
impl Cartridge { impl Cartridge {
@ -44,7 +44,7 @@ impl Cartridge {
self.mbc.tick() self.mbc.tick()
} }
fn detect_mbc(mem: &[u8]) -> Box<dyn MbcIo> { fn detect_mbc(mem: &[u8]) -> Box<dyn MBCIo> {
let ram_size: RamSize = mem[RAM_SIZE_ADDRESS].into(); let ram_size: RamSize = mem[RAM_SIZE_ADDRESS].into();
let rom_size: RomSize = mem[ROM_SIZE_ADDRESS].into(); let rom_size: RomSize = mem[ROM_SIZE_ADDRESS].into();
let mbc_kind = Self::detect_mbc_kind(mem[MBC_KIND_ADDRESS]); let mbc_kind = Self::detect_mbc_kind(mem[MBC_KIND_ADDRESS]);
@ -56,14 +56,14 @@ impl Cartridge {
tracing::info!("MBC kind: {:?}", mbc_kind); tracing::info!("MBC kind: {:?}", mbc_kind);
match mbc_kind { match mbc_kind {
MbcKind::None => Box::new(NoMbc), MBCKind::None => Box::new(NoMBC),
MbcKind::Mbc1(hw) => Box::new(Mbc1::new(hw, ram_size, rom_size)), MBCKind::MBC1(hw) => Box::new(MBC1::new(hw, ram_size, rom_size)),
MbcKind::Mbc2(hw) => Box::new(Mbc2::new(hw, rom_cap)), MBCKind::MBC2(hw) => Box::new(MBC2::new(hw, rom_cap)),
MbcKind::Mbc3(hw @ Mbc3Hardware::Rtc) => Box::new(Mbc3::new(hw, ram_cap)), MBCKind::MBC3(hw @ MBC3Hardware::RTC) => Box::new(MBC3::new(hw, ram_cap)),
MbcKind::Mbc3(hw @ Mbc3Hardware::RtcBatteryRam) => Box::new(Mbc3::new(hw, ram_cap)), MBCKind::MBC3(hw @ MBC3Hardware::RTCAndBatteryRAM) => Box::new(MBC3::new(hw, ram_cap)),
MbcKind::Mbc4(hw @ Mbc5Hardware::None) => Box::new(Mbc5::new(hw, ram_cap, rom_cap)), MBCKind::MBC5(hw @ MBC5Hardware::None) => Box::new(MBC5::new(hw, ram_cap, rom_cap)),
MbcKind::Mbc4(hw @ Mbc5Hardware::BatteryRam) => { MBCKind::MBC5(hw @ MBC5Hardware::BatteryRAM) => {
Box::new(Mbc5::new(hw, ram_cap, rom_cap)) Box::new(MBC5::new(hw, ram_cap, rom_cap))
} }
kind => todo!("ROMS with {:?} are currently unsupported", kind), kind => todo!("ROMS with {:?} are currently unsupported", kind),
} }
@ -88,29 +88,29 @@ impl Cartridge {
} }
} }
fn detect_mbc_kind(id: u8) -> MbcKind { fn detect_mbc_kind(id: u8) -> MBCKind {
use MbcKind::*; use MBCKind::*;
match id { match id {
0x00 => None, 0x00 => None,
0x01 => Mbc1(Mbc1Hardware::None), 0x01 => MBC1(MBC1Hardware::None),
0x02 => Mbc1(Mbc1Hardware::Ram), 0x02 => MBC1(MBC1Hardware::RAM),
0x03 => Mbc1(Mbc1Hardware::BatteryRam), 0x03 => MBC1(MBC1Hardware::BatteryRAM),
0x05 => Mbc2(Mbc2Hardware::None), 0x05 => MBC2(MBC2Hardware::None),
0x06 => Mbc2(Mbc2Hardware::BatteryRam), 0x06 => MBC2(MBC2Hardware::BatteryRAM),
0x08 | 0x09 => unimplemented!("NoMBC + RAM and NoMBC + Battery unsupported"), 0x08 | 0x09 => unimplemented!("NoMBC + RAM and NoMBC + Battery unsupported"),
0x0B | 0x0C | 0x0D => unimplemented!("MM01 unsupported"), 0x0B | 0x0C | 0x0D => unimplemented!("MM01 unsupported"),
0x0F => Mbc3(Mbc3Hardware::Rtc), 0x0F => MBC3(MBC3Hardware::RTC),
0x10 => Mbc3(Mbc3Hardware::RtcBatteryRam), 0x10 => MBC3(MBC3Hardware::RTCAndBatteryRAM),
0x11 => Mbc3(Mbc3Hardware::None), 0x11 => MBC3(MBC3Hardware::None),
0x12 => Mbc3(Mbc3Hardware::Ram), 0x12 => MBC3(MBC3Hardware::RAM),
0x13 => Mbc3(Mbc3Hardware::BatteryRam), 0x13 => MBC3(MBC3Hardware::BatteryRAM),
0x19 => Mbc4(Mbc5Hardware::None), 0x19 => MBC5(MBC5Hardware::None),
0x1A => Mbc4(Mbc5Hardware::Ram), 0x1A => MBC5(MBC5Hardware::RAM),
0x1B => Mbc4(Mbc5Hardware::BatteryRam), 0x1B => MBC5(MBC5Hardware::BatteryRAM),
0x1C => Mbc4(Mbc5Hardware::Rumble), 0x1C => MBC5(MBC5Hardware::Rumble),
0x1D => Mbc4(Mbc5Hardware::RumbleRam), 0x1D => MBC5(MBC5Hardware::RumbleRAM),
0x1E => Mbc4(Mbc5Hardware::RumbleBatteryRam), 0x1E => MBC5(MBC5Hardware::RumbleBatteryRAM),
id => unimplemented!("MBC with code {:#04X} is unsupported", id), id => unimplemented!("MBC with code {:#04X} is unsupported", id),
} }
} }
@ -118,7 +118,7 @@ impl Cartridge {
impl BusIo for Cartridge { impl BusIo for Cartridge {
fn read_byte(&self, addr: u16) -> u8 { fn read_byte(&self, addr: u16) -> u8 {
use MbcResult::*; use MBCResult::*;
match self.mbc.handle_read(addr) { match self.mbc.handle_read(addr) {
Addr(addr) => self.mem[addr], Addr(addr) => self.mem[addr],
@ -132,7 +132,7 @@ impl BusIo for Cartridge {
} }
#[derive(Debug)] #[derive(Debug)]
struct Mbc1 { struct MBC1 {
/// 5-bit number /// 5-bit number
rom_bank: u8, rom_bank: u8,
/// 2-bit number /// 2-bit number
@ -143,11 +143,11 @@ struct Mbc1 {
rom_size: RomSize, rom_size: RomSize,
mem_enabled: bool, mem_enabled: bool,
hw: Mbc1Hardware, hw: MBC1Hardware,
} }
impl Mbc1 { impl MBC1 {
fn new(hw: Mbc1Hardware, ram_size: RamSize, rom_size: RomSize) -> Self { fn new(hw: MBC1Hardware, ram_size: RamSize, rom_size: RomSize) -> Self {
Self { Self {
rom_bank: 0x01, rom_bank: 0x01,
mem: vec![0; ram_size.capacity() as usize], mem: vec![0; ram_size.capacity() as usize],
@ -225,25 +225,25 @@ impl Mbc1 {
} }
} }
impl Savable for Mbc1 { impl Savable for MBC1 {
fn ext_ram(&self) -> Option<&[u8]> { fn ext_ram(&self) -> Option<&[u8]> {
match self.hw { match self.hw {
Mbc1Hardware::BatteryRam => Some(&self.mem), MBC1Hardware::BatteryRAM => Some(&self.mem),
_ => None, _ => None,
} }
} }
fn ext_ram_mut(&mut self) -> Option<&mut [u8]> { fn ext_ram_mut(&mut self) -> Option<&mut [u8]> {
match self.hw { match self.hw {
Mbc1Hardware::BatteryRam => Some(&mut self.mem), MBC1Hardware::BatteryRAM => Some(&mut self.mem),
_ => None, _ => None,
} }
} }
} }
impl MbcIo for Mbc1 { impl MBCIo for MBC1 {
fn handle_read(&self, addr: u16) -> MbcResult { fn handle_read(&self, addr: u16) -> MBCResult {
use MbcResult::*; use MBCResult::*;
match addr { match addr {
0x0000..=0x3FFF if self.mode => { 0x0000..=0x3FFF if self.mode => {
@ -277,7 +277,7 @@ impl MbcIo for Mbc1 {
} }
} }
impl RtcTick for Mbc1 { impl RtClockTick for MBC1 {
fn tick(&mut self) {} fn tick(&mut self) {}
} }
@ -309,7 +309,7 @@ impl RtClock {
} }
} }
impl RtcTick for RtClock { impl RtClockTick for RtClock {
fn tick(&mut self) { fn tick(&mut self) {
// This is the sort of situation where you'd want to use a scheduler. // This is the sort of situation where you'd want to use a scheduler.
if self.day_high.halt() { if self.day_high.halt() {
@ -341,7 +341,7 @@ impl RtcTick for RtClock {
} }
} }
trait RtcTick { trait RtClockTick {
fn tick(&mut self); fn tick(&mut self);
} }
@ -379,41 +379,41 @@ impl From<DayHigh> for u8 {
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
enum Mbc3Device { enum MBC3Device {
Ram, ExternalRam,
Rtc(RtcRegister), Clock(RtcRegister),
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
enum RtcRegister { enum RtcRegister {
Sec, Second,
Min, Minute,
Hr, Hour,
DayLow, DayLow,
DayHigh, DayHigh,
} }
#[derive(Debug)] #[derive(Debug)]
struct Mbc3 { struct MBC3 {
/// 7-bit Number /// 7-bit Number
rom_bank: u8, rom_bank: u8,
/// 2-bit Number /// 2-bit Number
ram_bank: u8, ram_bank: u8,
devs_enabled: bool, devs_enabled: bool,
mapped: Option<Mbc3Device>, mapped: Option<MBC3Device>,
mem: Vec<u8>, mem: Vec<u8>,
// RTC Data Latch Previous Write // RTC Data Latch Previous Write
prev_latch_write: Option<u8>, prev_latch_write: Option<u8>,
hw: Mbc3Hardware, hw: MBC3Hardware,
rtc: RtClock, rtc: RtClock,
rtc_latch: Option<RtClock>, rtc_latch: Option<RtClock>,
} }
impl Mbc3 { impl MBC3 {
fn new(hw: Mbc3Hardware, ram_cap: usize) -> Self { fn new(hw: MBC3Hardware, ram_cap: usize) -> Self {
Self { Self {
mem: vec![0; ram_cap], mem: vec![0; ram_cap],
rom_bank: Default::default(), rom_bank: Default::default(),
@ -428,41 +428,41 @@ impl Mbc3 {
} }
} }
impl Savable for Mbc3 { impl Savable for MBC3 {
fn ext_ram(&self) -> Option<&[u8]> { fn ext_ram(&self) -> Option<&[u8]> {
match self.hw { match self.hw {
Mbc3Hardware::BatteryRam | Mbc3Hardware::RtcBatteryRam => Some(&self.mem), MBC3Hardware::BatteryRAM | MBC3Hardware::RTCAndBatteryRAM => Some(&self.mem),
_ => None, _ => None,
} }
} }
fn ext_ram_mut(&mut self) -> Option<&mut [u8]> { fn ext_ram_mut(&mut self) -> Option<&mut [u8]> {
match self.hw { match self.hw {
Mbc3Hardware::BatteryRam | Mbc3Hardware::RtcBatteryRam => Some(&mut self.mem), MBC3Hardware::BatteryRAM | MBC3Hardware::RTCAndBatteryRAM => Some(&mut self.mem),
_ => None, _ => None,
} }
} }
} }
impl MbcIo for Mbc3 { impl MBCIo for MBC3 {
fn handle_read(&self, addr: u16) -> MbcResult { fn handle_read(&self, addr: u16) -> MBCResult {
use MbcResult::*; use MBCResult::*;
use RtcRegister::*; use RtcRegister::*;
let res = match addr { let res = match addr {
0x0000..=0x3FFF => Addr(addr as usize), 0x0000..=0x3FFF => Addr(addr as usize),
0x4000..=0x7FFF => Addr(0x4000 * self.rom_bank as usize + (addr as usize - 0x4000)), 0x4000..=0x7FFF => Addr(0x4000 * self.rom_bank as usize + (addr as usize - 0x4000)),
0xA000..=0xBFFF => match self.mapped { 0xA000..=0xBFFF => match self.mapped {
Some(Mbc3Device::Ram) if self.devs_enabled => { Some(MBC3Device::ExternalRam) if self.devs_enabled => {
Byte(self.mem[0x2000 * self.ram_bank as usize + (addr as usize - 0xA000)]) Byte(self.mem[0x2000 * self.ram_bank as usize + (addr as usize - 0xA000)])
} }
Some(Mbc3Device::Rtc(reg)) if self.devs_enabled => Byte( Some(MBC3Device::Clock(reg)) if self.devs_enabled => Byte(
self.rtc_latch self.rtc_latch
.as_ref() .as_ref()
.map(|rtc| match reg { .map(|rtc| match reg {
Sec => rtc.sec, Second => rtc.sec,
Min => rtc.min, Minute => rtc.min,
Hr => rtc.hr, Hour => rtc.hr,
DayLow => rtc.day_low, DayLow => rtc.day_low,
DayHigh => rtc.day_high.into(), DayHigh => rtc.day_high.into(),
}) })
@ -490,13 +490,13 @@ impl MbcIo for Mbc3 {
0x4000..=0x5FFF => match byte { 0x4000..=0x5FFF => match byte {
0x00 | 0x01 | 0x02 | 0x03 => { 0x00 | 0x01 | 0x02 | 0x03 => {
self.ram_bank = byte & 0x03; self.ram_bank = byte & 0x03;
self.mapped = Some(Mbc3Device::Ram); self.mapped = Some(MBC3Device::ExternalRam);
} }
0x08 => self.mapped = Some(Mbc3Device::Rtc(Sec)), 0x08 => self.mapped = Some(MBC3Device::Clock(Second)),
0x09 => self.mapped = Some(Mbc3Device::Rtc(Min)), 0x09 => self.mapped = Some(MBC3Device::Clock(Minute)),
0x0A => self.mapped = Some(Mbc3Device::Rtc(Hr)), 0x0A => self.mapped = Some(MBC3Device::Clock(Hour)),
0x0B => self.mapped = Some(Mbc3Device::Rtc(DayLow)), 0x0B => self.mapped = Some(MBC3Device::Clock(DayLow)),
0x0C => self.mapped = Some(Mbc3Device::Rtc(DayHigh)), 0x0C => self.mapped = Some(MBC3Device::Clock(DayHigh)),
_ => {} _ => {}
}, },
@ -509,17 +509,17 @@ impl MbcIo for Mbc3 {
self.prev_latch_write = Some(byte); self.prev_latch_write = Some(byte);
} }
0xA000..=0xBFFF => match self.mapped { 0xA000..=0xBFFF => match self.mapped {
Some(Mbc3Device::Ram) if self.devs_enabled => { Some(MBC3Device::ExternalRam) if self.devs_enabled => {
self.mem[0x2000 * self.ram_bank as usize + (addr as usize - 0xA000)] = byte self.mem[0x2000 * self.ram_bank as usize + (addr as usize - 0xA000)] = byte
} }
Some(Mbc3Device::Rtc(rtc_reg)) if self.devs_enabled => match rtc_reg { Some(MBC3Device::Clock(rtc_reg)) if self.devs_enabled => match rtc_reg {
Sec => { Second => {
self.rtc.sec = byte & 0x3F; self.rtc.sec = byte & 0x3F;
// Writing to RTC S resets the internal sub-second counter // Writing to RTC S resets the internal sub-second counter
self.rtc.cycles = 0; self.rtc.cycles = 0;
} }
Min => self.rtc.min = byte & 0x3F, Minute => self.rtc.min = byte & 0x3F,
Hr => self.rtc.hr = byte & 0x1F, Hour => self.rtc.hr = byte & 0x1F,
DayLow => self.rtc.day_low = byte, DayLow => self.rtc.day_low = byte,
DayHigh => self.rtc.day_high = (byte & 0xC1).into(), DayHigh => self.rtc.day_high = (byte & 0xC1).into(),
}, },
@ -530,16 +530,16 @@ impl MbcIo for Mbc3 {
} }
} }
impl RtcTick for Mbc3 { impl RtClockTick for MBC3 {
fn tick(&mut self) { fn tick(&mut self) {
if let Mbc3Hardware::RtcBatteryRam | Mbc3Hardware::Rtc = self.hw { if let MBC3Hardware::RTCAndBatteryRAM | MBC3Hardware::RTC = self.hw {
self.rtc.tick(); self.rtc.tick();
} }
} }
} }
#[derive(Debug)] #[derive(Debug)]
struct Mbc5 { struct MBC5 {
/// 9-bit number /// 9-bit number
rom_bank: u16, rom_bank: u16,
/// 4-bit number /// 4-bit number
@ -549,11 +549,11 @@ struct Mbc5 {
mem: Vec<u8>, mem: Vec<u8>,
mem_enabled: bool, mem_enabled: bool,
hw: Mbc5Hardware, hw: MBC5Hardware,
} }
impl Mbc5 { impl MBC5 {
fn new(hw: Mbc5Hardware, ram_cap: usize, rom_cap: usize) -> Self { fn new(hw: MBC5Hardware, ram_cap: usize, rom_cap: usize) -> Self {
Self { Self {
rom_bank: 0x01, rom_bank: 0x01,
mem: vec![0; ram_cap], mem: vec![0; ram_cap],
@ -569,25 +569,25 @@ impl Mbc5 {
} }
} }
impl Savable for Mbc5 { impl Savable for MBC5 {
fn ext_ram(&self) -> Option<&[u8]> { fn ext_ram(&self) -> Option<&[u8]> {
match self.hw { match self.hw {
Mbc5Hardware::RumbleBatteryRam | Mbc5Hardware::BatteryRam => Some(&self.mem), MBC5Hardware::RumbleBatteryRAM | MBC5Hardware::BatteryRAM => Some(&self.mem),
_ => None, _ => None,
} }
} }
fn ext_ram_mut(&mut self) -> Option<&mut [u8]> { fn ext_ram_mut(&mut self) -> Option<&mut [u8]> {
match self.hw { match self.hw {
Mbc5Hardware::RumbleBatteryRam | Mbc5Hardware::BatteryRam => Some(&mut self.mem), MBC5Hardware::RumbleBatteryRAM | MBC5Hardware::BatteryRAM => Some(&mut self.mem),
_ => None, _ => None,
} }
} }
} }
impl MbcIo for Mbc5 { impl MBCIo for MBC5 {
fn handle_read(&self, addr: u16) -> MbcResult { fn handle_read(&self, addr: u16) -> MBCResult {
use MbcResult::*; use MBCResult::*;
match addr { match addr {
0x0000..=0x3FFF => Addr(addr as usize), 0x0000..=0x3FFF => Addr(addr as usize),
@ -615,25 +615,25 @@ impl MbcIo for Mbc5 {
} }
} }
impl RtcTick for Mbc5 { impl RtClockTick for MBC5 {
fn tick(&mut self) {} fn tick(&mut self) {}
} }
#[derive(Debug)] #[derive(Debug)]
struct Mbc2 { struct MBC2 {
/// 4-bit number /// 4-bit number
rom_bank: u8, rom_bank: u8,
mem: Box<[u8; Self::RAM_SIZE]>, mem: Box<[u8; Self::RAM_SIZE]>,
mem_enabled: bool, mem_enabled: bool,
rom_cap: usize, rom_cap: usize,
hw: Mbc2Hardware, hw: MBC2Hardware,
} }
impl Mbc2 { impl MBC2 {
const RAM_SIZE: usize = 0x0200; const RAM_SIZE: usize = 0x0200;
fn new(hw: Mbc2Hardware, rom_cap: usize) -> Self { fn new(hw: MBC2Hardware, rom_cap: usize) -> Self {
Self { Self {
rom_bank: 0x01, rom_bank: 0x01,
mem: Box::new([0; Self::RAM_SIZE]), mem: Box::new([0; Self::RAM_SIZE]),
@ -648,25 +648,25 @@ impl Mbc2 {
} }
} }
impl Savable for Mbc2 { impl Savable for MBC2 {
fn ext_ram(&self) -> Option<&[u8]> { fn ext_ram(&self) -> Option<&[u8]> {
match self.hw { match self.hw {
Mbc2Hardware::BatteryRam => Some(self.mem.as_ref()), MBC2Hardware::BatteryRAM => Some(self.mem.as_ref()),
Mbc2Hardware::None => None, MBC2Hardware::None => None,
} }
} }
fn ext_ram_mut(&mut self) -> Option<&mut [u8]> { fn ext_ram_mut(&mut self) -> Option<&mut [u8]> {
match self.hw { match self.hw {
Mbc2Hardware::BatteryRam => Some(self.mem.as_mut()), MBC2Hardware::BatteryRAM => Some(self.mem.as_mut()),
Mbc2Hardware::None => None, MBC2Hardware::None => None,
} }
} }
} }
impl MbcIo for Mbc2 { impl MBCIo for MBC2 {
fn handle_read(&self, addr: u16) -> MbcResult { fn handle_read(&self, addr: u16) -> MBCResult {
use MbcResult::*; use MBCResult::*;
match addr { match addr {
0x0000..=0x3FFF => Addr(addr as usize), 0x0000..=0x3FFF => Addr(addr as usize),
@ -698,14 +698,14 @@ impl MbcIo for Mbc2 {
} }
} }
impl RtcTick for Mbc2 { impl RtClockTick for MBC2 {
fn tick(&mut self) {} fn tick(&mut self) {}
} }
#[derive(Debug)] #[derive(Debug)]
struct NoMbc; struct NoMBC;
impl Savable for NoMbc { impl Savable for NoMBC {
fn ext_ram(&self) -> Option<&[u8]> { fn ext_ram(&self) -> Option<&[u8]> {
None None
} }
@ -715,9 +715,9 @@ impl Savable for NoMbc {
} }
} }
impl MbcIo for NoMbc { impl MBCIo for NoMBC {
fn handle_read(&self, addr: u16) -> MbcResult { fn handle_read(&self, addr: u16) -> MBCResult {
MbcResult::Addr(addr as usize) MBCResult::Addr(addr as usize)
} }
fn handle_write(&mut self, _: u16, byte: u8) { fn handle_write(&mut self, _: u16, byte: u8) {
@ -725,60 +725,60 @@ impl MbcIo for NoMbc {
} }
} }
impl RtcTick for NoMbc { impl RtClockTick for NoMBC {
fn tick(&mut self) {} fn tick(&mut self) {}
} }
trait MbcIo: Savable + RtcTick { trait MBCIo: Savable + RtClockTick {
fn handle_read(&self, addr: u16) -> MbcResult; fn handle_read(&self, addr: u16) -> MBCResult;
fn handle_write(&mut self, addr: u16, byte: u8); fn handle_write(&mut self, addr: u16, byte: u8);
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
enum MbcResult { enum MBCResult {
Addr(usize), Addr(usize),
Byte(u8), Byte(u8),
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
enum MbcKind { enum MBCKind {
None, None,
Mbc1(Mbc1Hardware), MBC1(MBC1Hardware),
Mbc2(Mbc2Hardware), MBC2(MBC2Hardware),
Mbc3(Mbc3Hardware), MBC3(MBC3Hardware),
Mbc4(Mbc5Hardware), MBC5(MBC5Hardware),
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
enum Mbc1Hardware { enum MBC1Hardware {
None, None,
Ram, RAM,
BatteryRam, BatteryRAM,
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
enum Mbc2Hardware { enum MBC2Hardware {
None, None,
BatteryRam, BatteryRAM,
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
enum Mbc3Hardware { enum MBC3Hardware {
Rtc, RTC,
RtcBatteryRam, RTCAndBatteryRAM,
None, None,
Ram, RAM,
BatteryRam, BatteryRAM,
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
enum Mbc5Hardware { enum MBC5Hardware {
None, None,
Ram, RAM,
BatteryRam, BatteryRAM,
Rumble, Rumble,
RumbleRam, RumbleRAM,
RumbleBatteryRam, RumbleBatteryRAM,
} }
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
@ -874,7 +874,7 @@ impl From<u8> for RomSize {
} }
} }
impl std::fmt::Debug for Box<dyn MbcIo> { impl std::fmt::Debug for Box<dyn MBCIo> {
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!("Implement Debug for Box<dyn MBC> Trait Object"); todo!("Implement Debug for Box<dyn MBC> Trait Object");
} }

View File

@ -797,12 +797,21 @@ impl Default for BackgroundFetcher {
} }
} }
#[derive(Debug, Default)] #[derive(Debug)]
struct ObjectFetcher { struct ObjectFetcher {
state: FetcherState, state: FetcherState,
tile: TileBuilder, tile: TileBuilder,
} }
impl Default for ObjectFetcher {
fn default() -> Self {
Self {
state: Default::default(),
tile: Default::default(),
}
}
}
impl Fetcher for ObjectFetcher { impl Fetcher for ObjectFetcher {
fn reset(&mut self) { fn reset(&mut self) {
self.state = Default::default(); self.state = Default::default();