diff --git a/src/gpu.rs b/src/gpu.rs index 4b6d903..884b03b 100644 --- a/src/gpu.rs +++ b/src/gpu.rs @@ -102,20 +102,21 @@ impl GPU { } } - pub fn cycle(&mut self, cycles: u32) { + pub fn cycle(&mut self, cycles: u32) -> bool { if !self.lcdc.contains(LCDC::LCD_ENABLE) { - return; + return false; } self.cycle_count += cycles; - match self.ppu_mode { + return match self.ppu_mode { PPUMode::OAMScan => { if self.cycle_count > 80 { self.cycle_count -= 80; self.ppu_mode = PPUMode::Draw; println!("[PPU] Switching to Draw!"); } + false }, PPUMode::Draw => { // TODO: Allow variable length Mode 3 @@ -123,6 +124,9 @@ impl GPU { self.ppu_mode = PPUMode::HBlank; self.draw_bg(); println!("[PPU] Switching to HBlank!"); + true + } else { + false } }, PPUMode::HBlank => { @@ -138,6 +142,7 @@ impl GPU { println!("[PPU] Switching to OAMScan!"); } } + false }, PPUMode::VBlank => { if self.cycle_count > 4560 { @@ -146,6 +151,7 @@ impl GPU { self.ppu_mode = PPUMode::OAMScan; println!("[PPU] Switching to OAMScan!"); } + false } } } diff --git a/src/main.rs b/src/main.rs index 08a3e9f..41c9d28 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,10 +65,12 @@ async fn main() -> Result<(), impl std::error::Error> { loop { let cycles = cpu.cycle(); - cpu.mem.gpu.cycle(cycles); + let did_draw = cpu.mem.gpu.cycle(cycles); // TODO: This is WAY too slow - let frame_buffer = cpu.mem.gpu.frame_buffer.into_iter().flatten().flatten().collect(); - context.lock().unwrap().update(frame_buffer); + if did_draw { + let frame_buffer = cpu.mem.gpu.frame_buffer.into_iter().flatten().flatten().collect(); + context.lock().unwrap().update(frame_buffer); + } sleep(Duration::from_millis((1000_f64 / 4_194_304_f64 * cycles as f64) as u64)).await; }