handle timers inside frame update
This commit is contained in:
parent
4e6e470dec
commit
92621fac73
1 changed files with 12 additions and 17 deletions
|
@ -112,17 +112,17 @@ where
|
||||||
|
|
||||||
/// Emulation loop executes the fetch -> decode -> execute pipeline
|
/// Emulation loop executes the fetch -> decode -> execute pipeline
|
||||||
fn emulation_loop<T>(&mut self) -> Result<(), anyhow::Error> {
|
fn emulation_loop<T>(&mut self) -> Result<(), anyhow::Error> {
|
||||||
let mut timers_timer = Instant::now();
|
|
||||||
let mut tick_timer = Instant::now();
|
let mut tick_timer = Instant::now();
|
||||||
let target_fps: u128 = 60;
|
let target_fps: u128 = 60;
|
||||||
loop {
|
loop {
|
||||||
// Handle sound and delay timer.
|
|
||||||
self.handle_timers(&mut timers_timer);
|
|
||||||
|
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
let elapsed_time = now.duration_since(tick_timer);
|
let elapsed_time = now.duration_since(tick_timer);
|
||||||
let elapsed_ms = elapsed_time.as_millis();
|
let elapsed_ms = elapsed_time.as_millis();
|
||||||
if elapsed_ms >= (1000 / target_fps) {
|
if elapsed_ms >= (1000 / target_fps) {
|
||||||
|
// Handle sound and delay timer.
|
||||||
|
self.handle_timers();
|
||||||
|
|
||||||
for _ in 0..=7 {
|
for _ in 0..=7 {
|
||||||
// fetch instruction & decode it
|
// fetch instruction & decode it
|
||||||
let instruction = self.fetch_instruction()?;
|
let instruction = self.fetch_instruction()?;
|
||||||
|
@ -140,11 +140,8 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handles the timers logic.
|
/// Handles the timers logic.
|
||||||
fn handle_timers(&mut self, start_time: &mut Instant) {
|
fn handle_timers(&mut self) {
|
||||||
// Handle 60hz timers
|
// Handle timers
|
||||||
let elapsed_time = start_time.elapsed().as_micros();
|
|
||||||
// 16667 us which is 1/60 of a second
|
|
||||||
if elapsed_time > 16667 {
|
|
||||||
if self.delay_timer > 0 {
|
if self.delay_timer > 0 {
|
||||||
self.delay_timer -= 1
|
self.delay_timer -= 1
|
||||||
}
|
}
|
||||||
|
@ -153,8 +150,6 @@ where
|
||||||
} else {
|
} else {
|
||||||
self.do_beep()
|
self.do_beep()
|
||||||
}
|
}
|
||||||
*start_time = Instant::now()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Should make an audible beep.
|
/// Should make an audible beep.
|
||||||
|
|
Loading…
Reference in a new issue