handle timers inside frame update

This commit is contained in:
Denis-Cosmin Nutiu 2024-12-09 19:11:14 +02:00
parent 4e6e470dec
commit 92621fac73

View file

@ -112,17 +112,17 @@ where
/// Emulation loop executes the fetch -> decode -> execute pipeline
fn emulation_loop<T>(&mut self) -> Result<(), anyhow::Error> {
let mut timers_timer = Instant::now();
let mut tick_timer = Instant::now();
let target_fps: u128 = 60;
loop {
// Handle sound and delay timer.
self.handle_timers(&mut timers_timer);
let now = Instant::now();
let elapsed_time = now.duration_since(tick_timer);
let elapsed_ms = elapsed_time.as_millis();
if elapsed_ms >= (1000 / target_fps) {
// Handle sound and delay timer.
self.handle_timers();
for _ in 0..=7 {
// fetch instruction & decode it
let instruction = self.fetch_instruction()?;
@ -140,11 +140,8 @@ where
}
/// Handles the timers logic.
fn handle_timers(&mut self, start_time: &mut Instant) {
// Handle 60hz timers
let elapsed_time = start_time.elapsed().as_micros();
// 16667 us which is 1/60 of a second
if elapsed_time > 16667 {
fn handle_timers(&mut self) {
// Handle timers
if self.delay_timer > 0 {
self.delay_timer -= 1
}
@ -153,8 +150,6 @@ where
} else {
self.do_beep()
}
*start_time = Instant::now()
}
}
/// Should make an audible beep.