From 5f4981e995b96e56cf5cd0a5b65ff18ac8c2ccca Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Wed, 11 Dec 2024 00:31:22 +0200 Subject: [PATCH] use c-like struct for store and load memory --- src/emulator.rs | 4 ++-- src/instruction.rs | 16 ++++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/emulator.rs b/src/emulator.rs index e68f09b..ab55053 100644 --- a/src/emulator.rs +++ b/src/emulator.rs @@ -408,13 +408,13 @@ where self.memory[self.index_register as usize + 1] = (number / 10) % 10; self.memory[self.index_register as usize + 2] = ((number) % 100) % 10; } - ProcessorInstruction::LoadMemory(vx) => { + ProcessorInstruction::LoadMemory { vx } => { for i in 0..=vx { let memory_index = (self.index_register + (i as u16)) as usize; self.registers[i as usize] = self.memory[memory_index]; } } - ProcessorInstruction::StoreMemory(vx) => { + ProcessorInstruction::StoreMemory { vx } => { for i in 0..=vx { let memory_index = (self.index_register + (i as u16)) as usize; self.memory[memory_index] = self.registers[i as usize]; diff --git a/src/instruction.rs b/src/instruction.rs index 25628a3..a7aa2ce 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -75,9 +75,9 @@ pub enum ProcessorInstruction { /// Converts the number in VX to 3 digits. BinaryCodedDecimalConversion { vx: u8 }, /// Stores the general purpose registers in memory at index register address. - StoreMemory(u8), + StoreMemory { vx: u8 }, /// Loads the general purpose registers from memory at index register address. - LoadMemory(u8), + LoadMemory { vx: u8 }, /// Blocks execution and waits for input. If a key is pressed it will be put in VX. GetKeyBlocking(u8), /// Skips one instruction if a key value stored in VX is pressed. Doesn't block execution. @@ -239,8 +239,12 @@ impl Instruction { (0xF, _, 0x3, 0x3) => ProcessorInstruction::BinaryCodedDecimalConversion { vx: Self::grab_first_nibble(data), }, - (0xF, _, 0x5, 0x5) => ProcessorInstruction::StoreMemory(Self::grab_first_nibble(data)), - (0xF, _, 0x6, 0x5) => ProcessorInstruction::LoadMemory(Self::grab_first_nibble(data)), + (0xF, _, 0x5, 0x5) => ProcessorInstruction::StoreMemory { + vx: Self::grab_first_nibble(data), + }, + (0xF, _, 0x6, 0x5) => ProcessorInstruction::LoadMemory { + vx: Self::grab_first_nibble(data), + }, (0xE, _, 0x9, 0xE) => { ProcessorInstruction::SkipIfKeyIsPressed(Self::grab_first_nibble(data)) } @@ -611,7 +615,7 @@ mod tests { let instruction = Instruction::new([0xFA, 0x55]); assert_eq!( instruction.processor_instruction, - ProcessorInstruction::StoreMemory(0xA) + ProcessorInstruction::StoreMemory { vx: 0xA } ) } @@ -620,7 +624,7 @@ mod tests { let instruction = Instruction::new([0xFA, 0x65]); assert_eq!( instruction.processor_instruction, - ProcessorInstruction::LoadMemory(0xA) + ProcessorInstruction::LoadMemory { vx: 0xA } ) }