From 1d8fb8d32a8c201578baf3beab44565f128904e9 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 16 Jul 2021 21:01:30 -0500 Subject: [PATCH] fix(ppu): delay ppu by one cycle on scx fifo discard --- src/ppu.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/ppu.rs b/src/ppu.rs index f00a391..424ae4e 100644 --- a/src/ppu.rs +++ b/src/ppu.rs @@ -51,6 +51,10 @@ pub struct Ppu { obj_buffer: ObjectBuffer, frame_buf: Box<[u8; GB_WIDTH * GB_HEIGHT * 4]>, window_stat: WindowStatus, + + scanline_start: bool, + to_discard: u8, + x_pos: u8, cycle: Cycle, } @@ -105,6 +109,8 @@ impl Ppu { } self.x_pos = 0; + self.scanline_start = true; + self.to_discard = 0; self.fetch.hblank_reset(); self.window_stat.hblank_reset(); @@ -375,12 +381,17 @@ impl Ppu { } if self.fifo.is_enabled() { - if self.x_pos == 0 && !self.fifo.back.is_empty() { - let to_discard = self.pos.scroll_x % 8; + if self.x_pos == 0 && !self.fifo.back.is_empty() && self.scanline_start { + self.to_discard = self.pos.scroll_x % 8; + self.scanline_start = false; + } - for _ in 0..to_discard { - let _ = self.fifo.back.pop_front(); - } + if self.to_discard > 0 { + let _ = self.fifo.back.pop_front(); + self.to_discard -= 1; + + // Delay the PPU by one cycle + return; } // Handle Background Pixel and Sprite FIFO @@ -472,6 +483,8 @@ impl Default for Ppu { window_stat: Default::default(), dma: Default::default(), x_pos: 0, + scanline_start: true, + to_discard: Default::default(), } } }