add test for fetch instruction, handle timers & fix handle timers

This commit is contained in:
Denis-Cosmin Nutiu 2024-12-13 15:27:18 +02:00
parent baf2f50406
commit ebe913ec61

View file

@ -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);
}
}