From 1f3be7836a954f0f570f7f090fc25f6a3a873d95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com> Date: Sat, 18 Jun 2022 19:03:43 +0100 Subject: [PATCH] fix: timer support --- examples/sdl/src/main.rs | 17 +++++++++++++++-- src/chip8.rs | 6 ------ src/chip8_neo.rs | 2 +- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/examples/sdl/src/main.rs b/examples/sdl/src/main.rs index ea70f4c..4c4955f 100644 --- a/examples/sdl/src/main.rs +++ b/examples/sdl/src/main.rs @@ -27,6 +27,7 @@ const COLORS: [[u8; 3]; 6] = [ const LOGIC_HZ: u32 = 480; const VISUAL_HZ: u32 = 60; const IDLE_HZ: u32 = 60; +const TIMER_HZ: u32 = 60; const BEEP_DURATION: f32 = 0.1; @@ -87,6 +88,7 @@ pub struct State { logic_frequency: u32, visual_frequency: u32, idle_frequency: u32, + timer_frequency: u32, screen_scale: f32, beep_duration: f32, next_tick_time: u32, @@ -112,6 +114,7 @@ fn main() { logic_frequency: LOGIC_HZ, visual_frequency: VISUAL_HZ, idle_frequency: IDLE_HZ, + timer_frequency: TIMER_HZ, screen_scale: SCREEN_SCALE, beep_duration: BEEP_DURATION, next_tick_time: 0, @@ -313,9 +316,19 @@ fn main() { // to make sure that the proper number of updates are performed let logic_visual_ratio = state.logic_frequency / state.visual_frequency; for _ in 0..logic_visual_ratio { - // runs the tick operation in the CHIP-8 system, + // runs the clock operation in the CHIP-8 system, // effectively changing the logic state of the machine - state.system.tick(); + state.system.clock(); + } + + // calculates the ration between the timer and the visual frequency + // so that the proper timer updates are rune + let timer_visual_ratio = state.timer_frequency / state.visual_frequency; + for _ in 0..timer_visual_ratio { + // runs the clock for the timers (both sound and delay), + // after that tries to determine if a beep should be sounded + state.system.clock_dt(); + state.system.clock_st(); beep |= state.system.beep(); } diff --git a/src/chip8.rs b/src/chip8.rs index d282dbf..c4898f2 100644 --- a/src/chip8.rs +++ b/src/chip8.rs @@ -128,12 +128,6 @@ impl Chip8 { self.ram[ROM_START..ROM_START + rom.len()].clone_from_slice(rom); } - pub fn tick(&mut self) { - self.clock(); - self.clock_dt(); - self.clock_st(); - } - pub fn clock(&mut self) { let opcode = self.fetch_opcode(); self.process_opcode(opcode); diff --git a/src/chip8_neo.rs b/src/chip8_neo.rs index eb9724b..ab88193 100644 --- a/src/chip8_neo.rs +++ b/src/chip8_neo.rs @@ -5,7 +5,7 @@ pub const STACK_SIZE: usize = 16; pub const REGISTERS_SIZE: usize = 16; /// The starting address for the ROM loading, should be -/// the initial PC position. +/// the initial PC position for execution. const ROM_START: usize = 0x200; static FONT_SET: [u8; 80] = [ -- GitLab