From ebe913ec61c5b158534a9d2e1c815c91cbbaed5a Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Fri, 13 Dec 2024 15:27:18 +0200 Subject: [PATCH] add test for fetch instruction, handle timers & fix handle timers --- src/emulator.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/emulator.rs b/src/emulator.rs index 9d62667..6edeca5 100644 --- a/src/emulator.rs +++ b/src/emulator.rs @@ -168,8 +168,8 @@ where if self.delay_timer > 0 { self.delay_timer -= 1 } - if self.delay_timer > 0 { - self.delay_timer -= 1 + if self.sound_timer > 0 { + self.sound_timer -= 1 } else { self.do_beep() } @@ -996,4 +996,34 @@ mod tests { assert_eq!(emulator.registers[0xA], 0xEF); } + + #[test] + fn test_fetch_instruction() { + let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + emulator.program_counter = 0x200; + emulator.memory[0x200] = 0x00; + emulator.memory[0x201] = 0xEE; + + let instruction = emulator.fetch_instruction(); + match instruction { + Ok(instruction) => { + assert_eq!(instruction, 0x00EE); + } + Err(_) => { + assert!(false, "Did not fetch"); + } + } + } + + #[test] + fn test_handle_timers() { + let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + emulator.sound_timer = 10; + emulator.delay_timer = 12; + + emulator.handle_timers(); + + assert_eq!(emulator.sound_timer, 9); + assert_eq!(emulator.delay_timer, 11); + } }