cgb refactor + double speed groundwork

This commit is contained in:
Alex Janka 2023-04-21 20:05:31 +10:00
parent fbae48596e
commit 124b823ae1
6 changed files with 64 additions and 8 deletions

View file

@ -5,9 +5,10 @@ use self::{
addresses::{Address, AddressMarker, CgbIoAddress, IoAddress},
mmio::{
apu::ApuSaveState,
cgb::{DoubleSpeed, Infrared, VramDma},
gpu::{Colour, GpuSaveState},
serial::SerialSaveState,
Apu, Gpu, Infrared, Joypad, OamDma, Serial, Timer, VramDma,
Apu, Gpu, Joypad, OamDma, Serial, Timer,
},
rom::RomSaveState,
};
@ -84,6 +85,7 @@ impl Wram {
struct CgbPeripherals {
vram_dma: VramDma,
infrared: Infrared,
double_speed: DoubleSpeed,
}
pub struct Memory<ColourFormat, R, C>
@ -361,7 +363,7 @@ where
IoAddress::Cgb(address) => {
if let WramBanks::Cgb { banks: _, selected } = &self.ram.banks && let Some(cgb_peripherals) = &self.cgb_peripherals {
match address {
CgbIoAddress::PrepareSpeed => todo!(),
CgbIoAddress::PrepareSpeed => cgb_peripherals.double_speed.get(),
CgbIoAddress::VramBank => self.gpu.vram.get_vram_bank(),
CgbIoAddress::VramDma(address) => cgb_peripherals.vram_dma.get_register(address),
CgbIoAddress::Infrared => cgb_peripherals.infrared.get(),
@ -420,7 +422,7 @@ where
IoAddress::Cgb(address) => {
if let WramBanks::Cgb { banks: _, selected } = &mut self.ram.banks && let Some(cgb_peripherals) = &mut self.cgb_peripherals {
match address {
CgbIoAddress::PrepareSpeed => todo!(),
CgbIoAddress::PrepareSpeed => cgb_peripherals.double_speed.set(data),
CgbIoAddress::VramBank => self.gpu.vram.set_vram_bank(data),
CgbIoAddress::VramDma(address) => cgb_peripherals.vram_dma.set_register(address, data),
CgbIoAddress::Infrared => cgb_peripherals.infrared.set(data),

View file

@ -0,0 +1,50 @@
use crate::{
connect::{PocketCamera, Renderer},
processor::memory::{mmio::gpu::Colour, Memory},
};
use serde::{Deserialize, Serialize};
#[derive(Default, Serialize, Deserialize, Clone, Copy)]
pub(crate) struct DoubleSpeed {
prepared: bool,
current: bool,
}
impl DoubleSpeed {
pub(crate) fn get(&self) -> u8 {
0b01111110 | if self.current { 0b1 << 7 } else { 0 } | if self.prepared { 0b1 } else { 0 }
}
pub(crate) fn set(&mut self, data: u8) {
self.prepared = data & 0b1 == 0b1;
}
}
impl<ColourFormat, R, C> Memory<ColourFormat, R, C>
where
ColourFormat: From<Colour> + Clone,
R: Renderer<ColourFormat>,
C: PocketCamera + Send + 'static,
{
pub(crate) fn is_double_speed(&self) -> bool {
self.cgb_peripherals
.as_ref()
.map_or(false, |v| v.double_speed.current)
}
pub(crate) fn should_switch_speed(&mut self) -> bool {
if let Some(cgb_peripherals) = &mut self.cgb_peripherals {
if cgb_peripherals.double_speed.prepared && !cgb_peripherals.double_speed.current {
cgb_peripherals.double_speed.current = true;
cgb_peripherals.double_speed.prepared = false;
return true;
} else if cgb_peripherals.double_speed.prepared && cgb_peripherals.double_speed.current
{
cgb_peripherals.double_speed.current = false;
cgb_peripherals.double_speed.prepared = false;
return true;
}
}
false
}
}

View file

@ -0,0 +1,7 @@
mod double_speed;
mod ir;
mod vram_dma;
pub(crate) use double_speed::DoubleSpeed;
pub(crate) use ir::Infrared;
pub(crate) use vram_dma::VramDma;

View file

@ -1,9 +1,9 @@
use super::gpu::{Colour, DrawMode};
use crate::{
connect::{PocketCamera, Renderer},
processor::{
memory::{
addresses::{AddressMarker, VramDmaAddress},
mmio::gpu::{Colour, DrawMode},
Memory,
},
SplitRegister,

View file

@ -1,16 +1,13 @@
pub(crate) mod apu;
pub(crate) mod cgb;
pub(crate) mod gpu;
mod ir;
pub(crate) mod joypad;
mod oam_dma;
pub(crate) mod serial;
mod timer;
mod vram_dma;
pub use apu::Apu;
pub use gpu::Gpu;
pub use ir::Infrared;
pub use joypad::Joypad;
pub use oam_dma::OamDma;
pub use serial::Serial;
pub use timer::Timer;
pub use vram_dma::VramDma;