Skip to content

Commit

Permalink
Merge branch 'feat/pedantic-ppu' into 'master'
Browse files Browse the repository at this point in the history
New PPU asserts for pedantic mode

See merge request joamag/boytacean!62
  • Loading branch information
joamag committed Sep 16, 2024
2 parents 8943f0d + cbb54a0 commit 36bc807
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
15 changes: 15 additions & 0 deletions src/diag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::gb::GameBoy;
static mut GLOBAL_INSTANCE: *const GameBoy = null();

// Static mutable flag to enable or disable pedantic diagnostics.
#[cfg(feature = "pedantic")]
pub static mut PEDANTIC: bool = true;

impl GameBoy {
Expand Down Expand Up @@ -64,6 +65,7 @@ impl GameBoy {
}
}

#[cfg(feature = "pedantic")]
#[macro_export]
macro_rules! enable_pedantic {
() => {
Expand All @@ -73,6 +75,13 @@ macro_rules! enable_pedantic {
};
}

#[cfg(not(feature = "pedantic"))]
#[macro_export]
macro_rules! enable_pedantic {
() => {};
}

#[cfg(feature = "pedantic")]
#[macro_export]
macro_rules! disable_pedantic {
() => {
Expand All @@ -82,6 +91,12 @@ macro_rules! disable_pedantic {
};
}

#[cfg(not(feature = "pedantic"))]
#[macro_export]
macro_rules! disable_pedantic {
() => {};
}

#[macro_export]
macro_rules! panic_gb {
($msg:expr) => {
Expand Down
7 changes: 6 additions & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ macro_rules! infoln {
macro_rules! warnln {
($($rest:tt)*) => {
{
$crate::panic_gb!($($rest)*);
if unsafe { $crate::diag::PEDANTIC } {
$crate::panic_gb!($($rest)*);
} else {
std::print!("[WARNING] ");
std::println!($($rest)*);
}
}
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/ppu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::{
};

use crate::{
assert_pedantic_gb,
color::{
rgb555_to_rgb888, rgb888_to_rgb1555_array, rgb888_to_rgb1555_u16, rgb888_to_rgb565,
rgb888_to_rgb565_u16, Pixel, PixelAlpha, RGB1555_SIZE, RGB565_SIZE, RGB888_SIZE, RGB_SIZE,
Expand Down Expand Up @@ -718,6 +719,17 @@ impl Ppu {
return;
}

// runs a series of pre-emptive PPU state validations to ensure
// that no core invariants are being violated, this is a pedantic
// only check, proper features must be set
assert_pedantic_gb!(cycles < 80, "Invalid number of cycles in PPU: {}", cycles);
assert_pedantic_gb!(
self.mode_clock < 600,
"Invalid mode clock: {}",
self.mode_clock
);
assert_pedantic_gb!(self.ly < 154, "Invalid LY value: {}", self.ly);

// increments the current mode clock by the provided amount
// of CPU cycles (probably coming from a previous CPU clock)
self.mode_clock += cycles;
Expand Down Expand Up @@ -824,8 +836,9 @@ impl Ppu {
}
SCY_ADDR => self.scy,
SCX_ADDR => self.scx,
// 0xFF44 — LY: LCD Y coordinate
LY_ADDR => self.ly,
// 0xFF45 — LYC
// 0xFF45 — LYC: LY compare
LYC_ADDR => self.lyc,
// 0xFF47 — BGP (Non-CGB Mode only)
BGP_ADDR => self.palettes[0],
Expand Down
20 changes: 14 additions & 6 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::{
};

use crate::{
disable_pedantic, enable_pedantic,
gb::{GameBoy, GameBoyDevice, GameBoyMode, GameBoySpeed},
info::Info,
ppu::{DISPLAY_HEIGHT, DISPLAY_WIDTH, FRAME_BUFFER_SIZE},
Expand Down Expand Up @@ -1658,12 +1659,17 @@ impl State for BessCore {
gb.cpu_i().ime(),
gb.mmu_i().ie,
u8::from(gb.cpu().halted()),
// @TODO: These registers cannot be completely retrieved
// and because of that some audio noise is played when loading state.
// The loading of the registers should be done in a much
// more manual way like SameBoy does here:
// https://github.com/LIJI32/SameBoy/blob/7e6f1f866e89430adaa6be839aecc4a2ccabd69c/Core/save_state.c#L673
gb.mmu().read_many_raw(0xff00, 128).try_into().unwrap(),
{
// @TODO: These registers cannot be completely retrieved
// and because of that some audio noise is played when loading state.
// The loading of the registers should be done in a much
// more manual way like SameBoy does here:
// https://github.com/LIJI32/SameBoy/blob/7e6f1f866e89430adaa6be839aecc4a2ccabd69c/Core/save_state.c#L673
disable_pedantic!();
let io_registers = gb.mmu().read_many_raw(0xff00, 128).try_into().unwrap();
enable_pedantic!();
io_registers
},
);
core.ram.fill_buffer(gb.mmu().ram());
core.vram.fill_buffer(gb.ppu().vram_device());
Expand Down Expand Up @@ -1703,7 +1709,9 @@ impl State for BessCore {
// The registers should be handled in a more manual manner
// to avoid unwanted side effects
// https://github.com/LIJI32/SameBoy/blob/7e6f1f866e89430adaa6be839aecc4a2ccabd69c/Core/save_state.c#L1003
disable_pedantic!();
gb.mmu().write_many(0xff00, &self.io_registers);
enable_pedantic!();

gb.mmu().set_ram(self.ram.buffer.to_vec());
gb.ppu().set_vram(&self.vram.buffer);
Expand Down

0 comments on commit 36bc807

Please sign in to comment.