diff --git a/examples/sdl/src/main.rs b/examples/sdl/src/main.rs index 93bf270e65831e5656c09933c249420c436168cf..8f0ef696d9928fd182f430e5f2967d3584ea3d1d 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 bbdb384b597e0c1d149e5be04ed34d7a3d132f01..80619e5a2f0a8dcd11c92a0e3715d5bc36e01dd6 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 aa723ef215137c44cebde98a7855be183c6a3111..463d819157e33267aee950a0657c47fbee1b6eb1 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 c7bc9d23ec4c5423712346bdbac0ae361a9dfde1..5935dc888987e4a8f1d1f373d2aed0e3178e4e81 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 {