Skip to content

Commit

Permalink
Only draw frame_buffer if GPU drew
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed Dec 11, 2023
1 parent ed91e29 commit 9cd1052
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,31 @@ 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
if self.cycle_count > 172 {
self.ppu_mode = PPUMode::HBlank;
self.draw_bg();
println!("[PPU] Switching to HBlank!");
true
} else {
false
}
},
PPUMode::HBlank => {
Expand All @@ -138,6 +142,7 @@ impl GPU {
println!("[PPU] Switching to OAMScan!");
}
}
false
},
PPUMode::VBlank => {
if self.cycle_count > 4560 {
Expand All @@ -146,6 +151,7 @@ impl GPU {
self.ppu_mode = PPUMode::OAMScan;
println!("[PPU] Switching to OAMScan!");
}
false
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 9cd1052

Please sign in to comment.