fix: get key resource
This commit is contained in:
@@ -3,6 +3,7 @@ use crate::device::keyboard::Keyboard;
|
|||||||
use crate::device::timer::DeviceTimerManager;
|
use crate::device::timer::DeviceTimerManager;
|
||||||
use crate::util::EmulatorResult;
|
use crate::util::EmulatorResult;
|
||||||
use rand::random;
|
use rand::random;
|
||||||
|
use rand::seq::IteratorRandom;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
@@ -55,8 +56,8 @@ impl Device {
|
|||||||
const ROM_START: usize = 0x200;
|
const ROM_START: usize = 0x200;
|
||||||
|
|
||||||
// Throttling configuration for cpu
|
// Throttling configuration for cpu
|
||||||
const DO_CHIP_CPU_THROTTLING:bool = true;
|
const DO_CHIP_CPU_THROTTLING:bool = false;
|
||||||
const TARGET_CPU_SPEED_INSTRUCTIONS_PER_SECOND: u64 = 800;
|
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);
|
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<()> {
|
pub fn cycle(&mut self) -> EmulatorResult<()> {
|
||||||
@@ -64,7 +65,7 @@ impl Device {
|
|||||||
self.device_keyboard.update_keyboard_registers()?;
|
self.device_keyboard.update_keyboard_registers()?;
|
||||||
|
|
||||||
let pc = self.registers.pc as usize;
|
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;
|
self.registers.pc += 2;
|
||||||
|
|
||||||
let instruction = Instruction::decode_instruction(instr_slice);
|
let instruction = Instruction::decode_instruction(instr_slice);
|
||||||
@@ -84,7 +85,6 @@ impl Device {
|
|||||||
y * Self::FRAME_BUFFER_WIDTH + x
|
y * Self::FRAME_BUFFER_WIDTH + x
|
||||||
}
|
}
|
||||||
pub fn execute_instruction(&mut self, instruction: Instruction) -> EmulatorResult<()> {
|
pub fn execute_instruction(&mut self, instruction: Instruction) -> EmulatorResult<()> {
|
||||||
// thread::sleep(Duration::from_millis(250));
|
|
||||||
log::trace!("Executing {:?}, {:?}", &instruction, &self.registers);
|
log::trace!("Executing {:?}, {:?}", &instruction, &self.registers);
|
||||||
match instruction {
|
match instruction {
|
||||||
Instruction::InvalidInstruction => {
|
Instruction::InvalidInstruction => {
|
||||||
@@ -249,10 +249,19 @@ impl Device {
|
|||||||
self.registers.i = addn_res;
|
self.registers.i = addn_res;
|
||||||
}
|
}
|
||||||
Instruction::GetKey(x) => {
|
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;
|
self.registers.pc -= 2;
|
||||||
}
|
}
|
||||||
|
// let key_expected = self.registers.v[x];
|
||||||
}
|
}
|
||||||
Instruction::SetIndexToFontCharacter(x) => {
|
Instruction::SetIndexToFontCharacter(x) => {
|
||||||
let requested_char = self.registers.v[x];
|
let requested_char = self.registers.v[x];
|
||||||
|
@@ -186,8 +186,9 @@ impl Instruction {
|
|||||||
let x = (instruction & 0xf00) >> 8;
|
let x = (instruction & 0xf00) >> 8;
|
||||||
Instruction::LoadRegistersFromMemory(x as usize)
|
Instruction::LoadRegistersFromMemory(x as usize)
|
||||||
}
|
}
|
||||||
_ => {
|
instruction_nibble => {
|
||||||
todo!("Unimplemented instruction")
|
log::error!("Unimplemented instruction with nibble {:?}",instruction_nibble);
|
||||||
|
Instruction::InvalidInstruction
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -80,7 +80,7 @@ mod tests {
|
|||||||
let (_sender,receiver) = std::sync::mpsc::sync_channel(1);
|
let (_sender,receiver) = std::sync::mpsc::sync_channel(1);
|
||||||
let keyboard = Keyboard::new(receiver);
|
let keyboard = Keyboard::new(receiver);
|
||||||
assert_no_key_pressed(&keyboard);
|
assert_no_key_pressed(&keyboard);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -88,12 +88,12 @@ mod tests {
|
|||||||
let (sender,receiver) = std::sync::mpsc::sync_channel(1);
|
let (sender,receiver) = std::sync::mpsc::sync_channel(1);
|
||||||
let mut keyboard = Keyboard::new(receiver);
|
let mut keyboard = Keyboard::new(receiver);
|
||||||
assert_no_key_pressed(&keyboard);
|
assert_no_key_pressed(&keyboard);
|
||||||
|
|
||||||
|
|
||||||
sender.try_send(super::KeyboardEvent::KeyDown(Key::K0)).expect("Could not send");
|
sender.try_send(super::KeyboardEvent::KeyDown(Key::K0)).expect("Could not send");
|
||||||
keyboard.update_keyboard_registers().expect("Could not update keyboard");
|
keyboard.update_keyboard_registers().expect("Could not update keyboard");
|
||||||
|
|
||||||
|
|
||||||
assert_eq!(1,keyboard.bitflags);
|
assert_eq!(1,keyboard.bitflags);
|
||||||
assert_eq!(true,keyboard.query_key_down(0));
|
assert_eq!(true,keyboard.query_key_down(0));
|
||||||
for i in 1..=0xF {
|
for i in 1..=0xF {
|
||||||
@@ -101,7 +101,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
sender.try_send(super::KeyboardEvent::KeyUp(Key::K0)).expect("Could not send");
|
sender.try_send(super::KeyboardEvent::KeyUp(Key::K0)).expect("Could not send");
|
||||||
keyboard.update_keyboard_registers().expect("Could not update keyboard");
|
keyboard.update_keyboard_registers().expect("Could not update keyboard");
|
||||||
assert_no_key_pressed(&keyboard);
|
assert_no_key_pressed(&keyboard);
|
||||||
@@ -114,6 +114,6 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user