From f87fe618fd0b6cbd8a219a580b44df12f2017cdb Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Fri, 3 Feb 2023 08:54:29 +1100 Subject: [PATCH] vblank should be working --- src/main.rs | 2 ++ src/processor/mod.rs | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 643b1fe..ce353de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -265,6 +265,8 @@ fn main() { reg, last_instruction: 0x0, last_instruction_addr: 0x0, + vblank_timer: 0., + vblank_remaining: 0, }; cpu_ram_init(&mut cpu); let mut cycle_num = 0; diff --git a/src/processor/mod.rs b/src/processor/mod.rs index c670ea7..94b99c7 100644 --- a/src/processor/mod.rs +++ b/src/processor/mod.rs @@ -23,6 +23,8 @@ pub struct CPU { pub reg: Registers, pub last_instruction: u8, pub last_instruction_addr: u16, + pub vblank_timer: f64, + pub vblank_remaining: u8, } // Hz @@ -43,6 +45,12 @@ impl CPU { } } + if self.vblank_timer >= (1. / 59.7) { + self.vblank_timer = 0.; + self.vblank_remaining = 20; + self.memory.set(0xFF0F, set_bit(self.memory.get(0xFF0F), 0)); + } + verbose_println!( "exec {:#4X} from pc: {:#X}", opcode, @@ -58,6 +66,15 @@ impl CPU { fn increment_timers(&mut self, cycles: u8) { let secs = (cycles * 4) as f64 / (CLOCK_SPEED * SPEEDUP); + self.vblank_timer += secs; + if self.vblank_remaining > 0 { + self.vblank_remaining = self.vblank_remaining.saturating_sub(cycles); + if self.vblank_remaining == 0 { + self.memory + .set(0xFF0F, clear_bit(self.memory.get(0xFF0F), 0)); + } + } + self.memory.set( 0xFF04, self.memory @@ -245,8 +262,12 @@ fn get_bit(byte: u8, flag: u8) -> bool { return got > 0x0; } +fn set_bit(byte: u8, flag: u8) -> u8 { + byte | (1 << flag) +} + fn clear_bit(byte: u8, flag: u8) -> u8 { - byte & (!(1 << flag as u8)) + byte & (!(1 << flag)) } fn rotate(byte: u8, direction: &Direction) -> (u8, bool) {