use c-like struct for GenerateRandomNumber instruction

This commit is contained in:
Denis-Cosmin Nutiu 2024-12-11 00:20:06 +02:00
parent 2842c55cf8
commit b2666d4226
2 changed files with 11 additions and 8 deletions

View file

@ -341,9 +341,9 @@ where
self.program_counter = address + offset as u16 self.program_counter = address + offset as u16
} }
ProcessorInstruction::GenerateRandomNumber(register, data) => { ProcessorInstruction::GenerateRandomNumber { vx, mask } => {
trace!("Generate random number"); 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) => { ProcessorInstruction::SkipEqualVXData(vx, data) => {
trace!("SkipEqualVXData"); trace!("SkipEqualVXData");

View file

@ -53,7 +53,7 @@ pub enum ProcessorInstruction {
/// Jumps to the address and adds V0 offset. /// Jumps to the address and adds V0 offset.
JumpWithOffset { address: u16 }, JumpWithOffset { address: u16 },
/// Generates a random number ANDed with the data and stores it in VX. /// 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. /// Skips the next instruction if VX is equal to data.
SkipEqualVXData(u8, u8), SkipEqualVXData(u8, u8),
/// Skip the next instruction if VX is not equal to data. /// Skip the next instruction if VX is not equal to data.
@ -201,10 +201,10 @@ impl Instruction {
(0xB, _, _, _) => ProcessorInstruction::JumpWithOffset { (0xB, _, _, _) => ProcessorInstruction::JumpWithOffset {
address: Self::grab_inner_data(data), address: Self::grab_inner_data(data),
}, },
(0xC, _, _, _) => ProcessorInstruction::GenerateRandomNumber( (0xC, _, _, _) => ProcessorInstruction::GenerateRandomNumber {
Self::grab_first_nibble(data), vx: Self::grab_first_nibble(data),
Self::grab_last_byte(data), mask: Self::grab_last_byte(data),
), },
(0x3, _, _, _) => ProcessorInstruction::SkipEqualVXData( (0x3, _, _, _) => ProcessorInstruction::SkipEqualVXData(
Self::grab_first_nibble(data), Self::grab_first_nibble(data),
Self::grab_last_byte(data), Self::grab_last_byte(data),
@ -524,7 +524,10 @@ mod tests {
let instruction = Instruction::new([0xCA, 0xBC]); let instruction = Instruction::new([0xCA, 0xBC]);
assert_eq!( assert_eq!(
instruction.processor_instruction, instruction.processor_instruction,
ProcessorInstruction::GenerateRandomNumber(0xA, 0xBC) ProcessorInstruction::GenerateRandomNumber {
vx: 0xA,
mask: 0xBC
}
) )
} }