Skip to content
Snippets Groups Projects
Verified Commit 1f3be783 authored by João Magalhães's avatar João Magalhães :rocket:
Browse files

fix: timer support

parent 73cb5974
No related branches found
No related tags found
No related merge requests found
Pipeline #604 passed
...@@ -27,6 +27,7 @@ const COLORS: [[u8; 3]; 6] = [ ...@@ -27,6 +27,7 @@ const COLORS: [[u8; 3]; 6] = [
const LOGIC_HZ: u32 = 480; const LOGIC_HZ: u32 = 480;
const VISUAL_HZ: u32 = 60; const VISUAL_HZ: u32 = 60;
const IDLE_HZ: u32 = 60; const IDLE_HZ: u32 = 60;
const TIMER_HZ: u32 = 60;
const BEEP_DURATION: f32 = 0.1; const BEEP_DURATION: f32 = 0.1;
...@@ -87,6 +88,7 @@ pub struct State { ...@@ -87,6 +88,7 @@ pub struct State {
logic_frequency: u32, logic_frequency: u32,
visual_frequency: u32, visual_frequency: u32,
idle_frequency: u32, idle_frequency: u32,
timer_frequency: u32,
screen_scale: f32, screen_scale: f32,
beep_duration: f32, beep_duration: f32,
next_tick_time: u32, next_tick_time: u32,
...@@ -112,6 +114,7 @@ fn main() { ...@@ -112,6 +114,7 @@ fn main() {
logic_frequency: LOGIC_HZ, logic_frequency: LOGIC_HZ,
visual_frequency: VISUAL_HZ, visual_frequency: VISUAL_HZ,
idle_frequency: IDLE_HZ, idle_frequency: IDLE_HZ,
timer_frequency: TIMER_HZ,
screen_scale: SCREEN_SCALE, screen_scale: SCREEN_SCALE,
beep_duration: BEEP_DURATION, beep_duration: BEEP_DURATION,
next_tick_time: 0, next_tick_time: 0,
...@@ -313,9 +316,19 @@ fn main() { ...@@ -313,9 +316,19 @@ fn main() {
// to make sure that the proper number of updates are performed // to make sure that the proper number of updates are performed
let logic_visual_ratio = state.logic_frequency / state.visual_frequency; let logic_visual_ratio = state.logic_frequency / state.visual_frequency;
for _ in 0..logic_visual_ratio { 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 // 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(); beep |= state.system.beep();
} }
......
...@@ -128,12 +128,6 @@ impl Chip8 { ...@@ -128,12 +128,6 @@ impl Chip8 {
self.ram[ROM_START..ROM_START + rom.len()].clone_from_slice(rom); 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) { pub fn clock(&mut self) {
let opcode = self.fetch_opcode(); let opcode = self.fetch_opcode();
self.process_opcode(opcode); self.process_opcode(opcode);
......
...@@ -5,7 +5,7 @@ pub const STACK_SIZE: usize = 16; ...@@ -5,7 +5,7 @@ pub const STACK_SIZE: usize = 16;
pub const REGISTERS_SIZE: usize = 16; pub const REGISTERS_SIZE: usize = 16;
/// The starting address for the ROM loading, should be /// The starting address for the ROM loading, should be
/// the initial PC position. /// the initial PC position for execution.
const ROM_START: usize = 0x200; const ROM_START: usize = 0x200;
static FONT_SET: [u8; 80] = [ static FONT_SET: [u8; 80] = [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment