use c-like struct for input keys instructions

This commit is contained in:
Denis-Cosmin Nutiu 2024-12-11 00:33:45 +02:00
parent 5f4981e995
commit 8148bdbbbe
2 changed files with 18 additions and 18 deletions

View file

@ -420,21 +420,21 @@ where
self.memory[memory_index] = self.registers[i as usize]; self.memory[memory_index] = self.registers[i as usize];
} }
} }
ProcessorInstruction::GetKeyBlocking(vx) => { ProcessorInstruction::GetKeyBlocking { vx } => {
if let Some(key) = self.last_key_pressed { if let Some(key) = self.last_key_pressed {
self.registers[vx as usize] = key; self.registers[vx as usize] = key;
} else { } else {
self.program_counter -= 2; self.program_counter -= 2;
} }
} }
ProcessorInstruction::SkipIfKeyIsPressed(vx) => { ProcessorInstruction::SkipIfKeyIsPressed { vx } => {
if let Some(key) = self.last_key_pressed { if let Some(key) = self.last_key_pressed {
if self.registers[vx as usize] == key { if self.registers[vx as usize] == key {
self.program_counter += 2; self.program_counter += 2;
} }
} }
} }
ProcessorInstruction::SkipIfKeyIsNotPressed(vx) => { ProcessorInstruction::SkipIfKeyIsNotPressed { vx } => {
if let Some(key) = self.last_key_pressed { if let Some(key) = self.last_key_pressed {
if self.registers[vx as usize] != key { if self.registers[vx as usize] != key {
self.program_counter += 2; self.program_counter += 2;

View file

@ -79,11 +79,11 @@ pub enum ProcessorInstruction {
/// Loads the general purpose registers from memory at index register address. /// Loads the general purpose registers from memory at index register address.
LoadMemory { vx: u8 }, LoadMemory { vx: u8 },
/// Blocks execution and waits for input. If a key is pressed it will be put in VX. /// Blocks execution and waits for input. If a key is pressed it will be put in VX.
GetKeyBlocking(u8), GetKeyBlocking { vx: u8 },
/// Skips one instruction if a key value stored in VX is pressed. Doesn't block execution. /// Skips one instruction if a key value stored in VX is pressed. Doesn't block execution.
SkipIfKeyIsPressed(u8), SkipIfKeyIsPressed { vx: u8 },
/// Skips one instruction if a key value stored in VX is NOT pressed. Doesn't block execution. /// Skips one instruction if a key value stored in VX is NOT pressed. Doesn't block execution.
SkipIfKeyIsNotPressed(u8), SkipIfKeyIsNotPressed { vx: u8 },
/// Unknown instruction /// Unknown instruction
UnknownInstruction, UnknownInstruction,
} }
@ -245,15 +245,15 @@ impl Instruction {
(0xF, _, 0x6, 0x5) => ProcessorInstruction::LoadMemory { (0xF, _, 0x6, 0x5) => ProcessorInstruction::LoadMemory {
vx: Self::grab_first_nibble(data), vx: Self::grab_first_nibble(data),
}, },
(0xE, _, 0x9, 0xE) => { (0xE, _, 0x9, 0xE) => ProcessorInstruction::SkipIfKeyIsPressed {
ProcessorInstruction::SkipIfKeyIsPressed(Self::grab_first_nibble(data)) vx: Self::grab_first_nibble(data),
} },
(0xE, _, 0xA, 0x1) => { (0xE, _, 0xA, 0x1) => ProcessorInstruction::SkipIfKeyIsNotPressed {
ProcessorInstruction::SkipIfKeyIsNotPressed(Self::grab_first_nibble(data)) vx: Self::grab_first_nibble(data),
} },
(0xF, _, 0x0, 0xA) => { (0xF, _, 0x0, 0xA) => ProcessorInstruction::GetKeyBlocking {
ProcessorInstruction::GetKeyBlocking(Self::grab_first_nibble(data)) vx: Self::grab_first_nibble(data),
} },
// Unknown instruction // Unknown instruction
_ => ProcessorInstruction::UnknownInstruction, _ => ProcessorInstruction::UnknownInstruction,
} }
@ -633,7 +633,7 @@ mod tests {
let instruction = Instruction::new([0xEF, 0x9E]); let instruction = Instruction::new([0xEF, 0x9E]);
assert_eq!( assert_eq!(
instruction.processor_instruction, instruction.processor_instruction,
ProcessorInstruction::SkipIfKeyIsPressed(0xF) ProcessorInstruction::SkipIfKeyIsPressed { vx: 0xF }
) )
} }
@ -642,7 +642,7 @@ mod tests {
let instruction = Instruction::new([0xEF, 0xA1]); let instruction = Instruction::new([0xEF, 0xA1]);
assert_eq!( assert_eq!(
instruction.processor_instruction, instruction.processor_instruction,
ProcessorInstruction::SkipIfKeyIsNotPressed(0xF) ProcessorInstruction::SkipIfKeyIsNotPressed { vx: 0xF }
) )
} }
@ -651,7 +651,7 @@ mod tests {
let instruction = Instruction::new([0xFE, 0x0A]); let instruction = Instruction::new([0xFE, 0x0A]);
assert_eq!( assert_eq!(
instruction.processor_instruction, instruction.processor_instruction,
ProcessorInstruction::GetKeyBlocking(0xE) ProcessorInstruction::GetKeyBlocking { vx: 0xF }
) )
} }
} }