From b0c52823a5a7ecab60d742cc153ddf92b00e5ce0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com> Date: Fri, 1 Jul 2022 11:33:23 +0100 Subject: [PATCH] feat: new game boy API --- examples/sdl/src/main.rs | 9 ++++++++- src/cpu.rs | 8 ++++---- src/gb.rs | 6 +++++- src/ppu.rs | 4 ++-- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/examples/sdl/src/main.rs b/examples/sdl/src/main.rs index 93bf270e..8f0ef696 100644 --- a/examples/sdl/src/main.rs +++ b/examples/sdl/src/main.rs @@ -5,7 +5,14 @@ fn main() { game_boy.load_boot_default(); for i in 0..37000 { - game_boy.clock(); + // runs the CPU clock and determines the number of + // cycles that have advanced for that clock tick + let cycles = game_boy.clock(); + + // calls the clock in the PPU to update its own + // execution lifecycle by one set of ticks + game_boy.ppu_clock(cycles); + if game_boy.cpu().pc() >= 0x6032 { println!("{}", i); break; diff --git a/src/cpu.rs b/src/cpu.rs index bbdb384b..80619e5a 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -506,7 +506,7 @@ impl Cpu { } } - pub fn clock(&mut self) { + pub fn clock(&mut self) -> u8 { let pc = self.pc; // fetches the current instruction and increments @@ -535,9 +535,9 @@ impl Cpu { instruction_fn(self); self.ticks = self.ticks.wrapping_add(*instruction_time as u32); - // calls the clock in the PPU to update its own - // execution lifecycle by one set of ticks - self.ppu().clock(*instruction_time); + // returns the number of cycles that the operation + // that has been executed has taken + *instruction_time } #[inline(always)] diff --git a/src/gb.rs b/src/gb.rs index aa723ef2..463d8191 100644 --- a/src/gb.rs +++ b/src/gb.rs @@ -12,10 +12,14 @@ impl GameBoy { GameBoy { cpu: cpu } } - pub fn clock(&mut self) { + pub fn clock(&mut self) -> u8 { self.cpu.clock() } + pub fn ppu_clock(&mut self, cycles: u8) { + self.ppu().clock(cycles) + } + pub fn cpu(&mut self) -> &mut Cpu { &mut self.cpu } diff --git a/src/ppu.rs b/src/ppu.rs index c7bc9d23..5935dc88 100644 --- a/src/ppu.rs +++ b/src/ppu.rs @@ -88,8 +88,8 @@ impl Ppu { } } - pub fn clock(&mut self, ticks: u8) { - self.mode_clock += ticks as u16; + pub fn clock(&mut self, cycles: u8) { + self.mode_clock += cycles as u16; match self.mode { PpuMode::OamRead => { if self.mode_clock >= 204 { -- GitLab