From d6e813b927f8c8968c14f9e70ad85afb06984ea9 Mon Sep 17 00:00:00 2001 From: Atreya Bain Date: Wed, 19 Feb 2025 16:59:52 +0000 Subject: [PATCH] fix: get key resource --- src/device/device.rs | 21 +++++++++++++++------ src/device/instruction.rs | 5 +++-- src/device/keyboard.rs | 14 +++++++------- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/device/device.rs b/src/device/device.rs index 186f256..b22da79 100644 --- a/src/device/device.rs +++ b/src/device/device.rs @@ -3,6 +3,7 @@ use crate::device::keyboard::Keyboard; use crate::device::timer::DeviceTimerManager; use crate::util::EmulatorResult; use rand::random; +use rand::seq::IteratorRandom; use std::sync::{Arc, Mutex}; use std::thread::sleep; use std::time::Duration; @@ -55,8 +56,8 @@ impl Device { const ROM_START: usize = 0x200; // Throttling configuration for cpu - const DO_CHIP_CPU_THROTTLING:bool = true; - const TARGET_CPU_SPEED_INSTRUCTIONS_PER_SECOND: u64 = 800; + const DO_CHIP_CPU_THROTTLING:bool = false; + const TARGET_CPU_SPEED_INSTRUCTIONS_PER_SECOND: u64 = 10000; const TARGET_CPU_INSTRUCTION_TIME: Duration = Duration::from_micros(1_000_000/Self::TARGET_CPU_SPEED_INSTRUCTIONS_PER_SECOND); pub fn cycle(&mut self) -> EmulatorResult<()> { @@ -64,7 +65,7 @@ impl Device { self.device_keyboard.update_keyboard_registers()?; let pc = self.registers.pc as usize; - let instr_slice = self.memory.get(pc..pc + 2).expect("Failed to get memory"); + let instr_slice = self.memory.get(pc..pc + 2).expect(format!("Failed to get memory at {}",pc).as_str()); self.registers.pc += 2; let instruction = Instruction::decode_instruction(instr_slice); @@ -84,7 +85,6 @@ impl Device { y * Self::FRAME_BUFFER_WIDTH + x } pub fn execute_instruction(&mut self, instruction: Instruction) -> EmulatorResult<()> { - // thread::sleep(Duration::from_millis(250)); log::trace!("Executing {:?}, {:?}", &instruction, &self.registers); match instruction { Instruction::InvalidInstruction => { @@ -249,10 +249,19 @@ impl Device { self.registers.i = addn_res; } Instruction::GetKey(x) => { - let key_expected = self.registers.v[x]; - if !self.device_keyboard.query_key_down(key_expected) { + // if !self.device_keyboard.query_key_down(key_expected) { + // self.registers.pc -= 2; + // } + let mut possible_presses = (0..=0xfu8).filter(|x|{self.device_keyboard.query_key_down(*x)}); + let pressed = possible_presses.next(); + if let Some(pressed_key) = pressed { + + self.registers.v[x] = pressed_key; + + } else{ self.registers.pc -= 2; } + // let key_expected = self.registers.v[x]; } Instruction::SetIndexToFontCharacter(x) => { let requested_char = self.registers.v[x]; diff --git a/src/device/instruction.rs b/src/device/instruction.rs index c1fd0a4..7a0296a 100644 --- a/src/device/instruction.rs +++ b/src/device/instruction.rs @@ -186,8 +186,9 @@ impl Instruction { let x = (instruction & 0xf00) >> 8; Instruction::LoadRegistersFromMemory(x as usize) } - _ => { - todo!("Unimplemented instruction") + instruction_nibble => { + log::error!("Unimplemented instruction with nibble {:?}",instruction_nibble); + Instruction::InvalidInstruction } } } diff --git a/src/device/keyboard.rs b/src/device/keyboard.rs index 78840c5..fdbf22d 100644 --- a/src/device/keyboard.rs +++ b/src/device/keyboard.rs @@ -80,7 +80,7 @@ mod tests { let (_sender,receiver) = std::sync::mpsc::sync_channel(1); let keyboard = Keyboard::new(receiver); assert_no_key_pressed(&keyboard); - + } #[test] @@ -88,12 +88,12 @@ mod tests { let (sender,receiver) = std::sync::mpsc::sync_channel(1); let mut keyboard = Keyboard::new(receiver); assert_no_key_pressed(&keyboard); - - + + sender.try_send(super::KeyboardEvent::KeyDown(Key::K0)).expect("Could not send"); keyboard.update_keyboard_registers().expect("Could not update keyboard"); - - + + assert_eq!(1,keyboard.bitflags); assert_eq!(true,keyboard.query_key_down(0)); for i in 1..=0xF { @@ -101,7 +101,7 @@ mod tests { } - + sender.try_send(super::KeyboardEvent::KeyUp(Key::K0)).expect("Could not send"); keyboard.update_keyboard_registers().expect("Could not update keyboard"); assert_no_key_pressed(&keyboard); @@ -114,6 +114,6 @@ mod tests { } } - + }