vblank should be working

This commit is contained in:
Alex Janka 2023-02-03 08:54:29 +11:00
parent 8b9bcb5026
commit f87fe618fd
2 changed files with 24 additions and 1 deletions

View file

@ -265,6 +265,8 @@ fn main() {
reg, reg,
last_instruction: 0x0, last_instruction: 0x0,
last_instruction_addr: 0x0, last_instruction_addr: 0x0,
vblank_timer: 0.,
vblank_remaining: 0,
}; };
cpu_ram_init(&mut cpu); cpu_ram_init(&mut cpu);
let mut cycle_num = 0; let mut cycle_num = 0;

View file

@ -23,6 +23,8 @@ pub struct CPU {
pub reg: Registers, pub reg: Registers,
pub last_instruction: u8, pub last_instruction: u8,
pub last_instruction_addr: u16, pub last_instruction_addr: u16,
pub vblank_timer: f64,
pub vblank_remaining: u8,
} }
// Hz // 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!( verbose_println!(
"exec {:#4X} from pc: {:#X}", "exec {:#4X} from pc: {:#X}",
opcode, opcode,
@ -58,6 +66,15 @@ impl CPU {
fn increment_timers(&mut self, cycles: u8) { fn increment_timers(&mut self, cycles: u8) {
let secs = (cycles * 4) as f64 / (CLOCK_SPEED * SPEEDUP); 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( self.memory.set(
0xFF04, 0xFF04,
self.memory self.memory
@ -245,8 +262,12 @@ fn get_bit(byte: u8, flag: u8) -> bool {
return got > 0x0; return got > 0x0;
} }
fn set_bit(byte: u8, flag: u8) -> u8 {
byte | (1 << flag)
}
fn clear_bit(byte: u8, flag: u8) -> u8 { fn clear_bit(byte: u8, flag: u8) -> u8 {
byte & (!(1 << flag as u8)) byte & (!(1 << flag))
} }
fn rotate(byte: u8, direction: &Direction) -> (u8, bool) { fn rotate(byte: u8, direction: &Direction) -> (u8, bool) {