diff --git a/src/emulator.rs b/src/emulator.rs index 07c3e24..167a02c 100644 --- a/src/emulator.rs +++ b/src/emulator.rs @@ -341,9 +341,9 @@ where self.program_counter = address + offset as u16 } - ProcessorInstruction::GenerateRandomNumber(register, data) => { + ProcessorInstruction::GenerateRandomNumber { vx, mask } => { trace!("Generate random number"); - self.registers[register as usize] = rand::thread_rng().gen_range(0x00..0xFF) & data + self.registers[vx as usize] = rand::thread_rng().gen_range(0x00..0xFF) & mask } ProcessorInstruction::SkipEqualVXData(vx, data) => { trace!("SkipEqualVXData"); diff --git a/src/instruction.rs b/src/instruction.rs index 32e46ab..0e79d58 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -53,7 +53,7 @@ pub enum ProcessorInstruction { /// Jumps to the address and adds V0 offset. JumpWithOffset { address: u16 }, /// Generates a random number ANDed with the data and stores it in VX. - GenerateRandomNumber(u8, u8), + GenerateRandomNumber { vx: u8, mask: u8 }, /// Skips the next instruction if VX is equal to data. SkipEqualVXData(u8, u8), /// Skip the next instruction if VX is not equal to data. @@ -201,10 +201,10 @@ impl Instruction { (0xB, _, _, _) => ProcessorInstruction::JumpWithOffset { address: Self::grab_inner_data(data), }, - (0xC, _, _, _) => ProcessorInstruction::GenerateRandomNumber( - Self::grab_first_nibble(data), - Self::grab_last_byte(data), - ), + (0xC, _, _, _) => ProcessorInstruction::GenerateRandomNumber { + vx: Self::grab_first_nibble(data), + mask: Self::grab_last_byte(data), + }, (0x3, _, _, _) => ProcessorInstruction::SkipEqualVXData( Self::grab_first_nibble(data), Self::grab_last_byte(data), @@ -524,7 +524,10 @@ mod tests { let instruction = Instruction::new([0xCA, 0xBC]); assert_eq!( instruction.processor_instruction, - ProcessorInstruction::GenerateRandomNumber(0xA, 0xBC) + ProcessorInstruction::GenerateRandomNumber { + vx: 0xA, + mask: 0xBC + } ) }