use c-like struct for SkipEqualVXVY & SkipNotEqualVXVY instruction

This commit is contained in:
Denis-Cosmin Nutiu 2024-12-11 00:25:50 +02:00
parent 4cba27de97
commit e7b69b72b2
2 changed files with 27 additions and 21 deletions

View file

@ -345,21 +345,21 @@ where
trace!("Generate random number"); trace!("Generate random number");
self.registers[vx as usize] = rand::thread_rng().gen_range(0x00..0xFF) & mask 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");
let vx_data = self.registers[vx as usize]; let vx_data = self.registers[vx as usize];
if vx_data == data { if vx_data == data {
self.program_counter += 2 self.program_counter += 2
} }
} }
ProcessorInstruction::SkipNotEqualVXData { vx, data} => { ProcessorInstruction::SkipNotEqualVXData { vx, data } => {
trace!("SkipNotEqualVXData"); trace!("SkipNotEqualVXData");
let vx_data = self.registers[vx as usize]; let vx_data = self.registers[vx as usize];
if vx_data != data { if vx_data != data {
self.program_counter += 2 self.program_counter += 2
} }
} }
ProcessorInstruction::SkipEqualVXVY(vx, vy) => { ProcessorInstruction::SkipEqualVXVY { vx, vy } => {
trace!("SkipNotEqualVXData"); trace!("SkipNotEqualVXData");
let vx_data = self.registers[vx as usize]; let vx_data = self.registers[vx as usize];
let vy_data = self.registers[vy as usize]; let vy_data = self.registers[vy as usize];
@ -367,7 +367,7 @@ where
self.program_counter += 2 self.program_counter += 2
} }
} }
ProcessorInstruction::SkipNotEqualVXVY(vx, vy) => { ProcessorInstruction::SkipNotEqualVXVY { vx, vy } => {
trace!("SkipNotEqualVXVY"); trace!("SkipNotEqualVXVY");
let vx_data = self.registers[vx as usize]; let vx_data = self.registers[vx as usize];
let vy_data = self.registers[vy as usize]; let vy_data = self.registers[vy as usize];

View file

@ -55,13 +55,13 @@ pub enum ProcessorInstruction {
/// 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 { vx: u8, mask: 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 {vx: u8, data: u8}, SkipEqualVXData { vx: u8, data: u8 },
/// Skip the next instruction if VX is not equal to data. /// Skip the next instruction if VX is not equal to data.
SkipNotEqualVXData {vx: u8, data: u8}, SkipNotEqualVXData { vx: u8, data: u8 },
/// Skips the next instruction if VX is equal to VY. /// Skips the next instruction if VX is equal to VY.
SkipEqualVXVY(u8, u8), SkipEqualVXVY { vx: u8, vy: u8 },
/// Skip the next instruction if VX is not equal to VY. /// Skip the next instruction if VX is not equal to VY.
SkipNotEqualVXVY(u8, u8), SkipNotEqualVXVY { vx: u8, vy: u8 },
/// Sets the value of the VX instruction to the current value of the delay timer. /// Sets the value of the VX instruction to the current value of the delay timer.
SetVXToDelayTimer(u8), SetVXToDelayTimer(u8),
/// Sets the delay timer to the value in VX. /// Sets the delay timer to the value in VX.
@ -205,7 +205,7 @@ impl Instruction {
vx: Self::grab_first_nibble(data), vx: Self::grab_first_nibble(data),
mask: Self::grab_last_byte(data), mask: Self::grab_last_byte(data),
}, },
(0x3, _, _, _) => ProcessorInstruction::SkipEqualVXData{ (0x3, _, _, _) => ProcessorInstruction::SkipEqualVXData {
vx: Self::grab_first_nibble(data), vx: Self::grab_first_nibble(data),
data: Self::grab_last_byte(data), data: Self::grab_last_byte(data),
}, },
@ -213,14 +213,14 @@ impl Instruction {
vx: Self::grab_first_nibble(data), vx: Self::grab_first_nibble(data),
data: Self::grab_last_byte(data), data: Self::grab_last_byte(data),
}, },
(0x5, _, _, 0x0) => ProcessorInstruction::SkipEqualVXVY( (0x5, _, _, 0x0) => ProcessorInstruction::SkipEqualVXVY {
Self::grab_first_nibble(data), vx: Self::grab_first_nibble(data),
Self::grab_middle_nibble(data), vy: Self::grab_middle_nibble(data),
), },
(0x9, _, _, 0x0) => ProcessorInstruction::SkipNotEqualVXVY( (0x9, _, _, 0x0) => ProcessorInstruction::SkipNotEqualVXVY {
Self::grab_first_nibble(data), vx: Self::grab_first_nibble(data),
Self::grab_middle_nibble(data), vy: Self::grab_middle_nibble(data),
), },
(0xF, _, 0x0, 0x7) => { (0xF, _, 0x0, 0x7) => {
ProcessorInstruction::SetVXToDelayTimer(Self::grab_first_nibble(data)) ProcessorInstruction::SetVXToDelayTimer(Self::grab_first_nibble(data))
} }
@ -365,7 +365,10 @@ mod tests {
let instruction = Instruction::new([0x3A, 0xBC]); let instruction = Instruction::new([0x3A, 0xBC]);
assert_eq!( assert_eq!(
instruction.processor_instruction, instruction.processor_instruction,
ProcessorInstruction::SkipEqualVXData{vx: 0xA, data: 0xBC} ProcessorInstruction::SkipEqualVXData {
vx: 0xA,
data: 0xBC
}
) )
} }
@ -374,7 +377,10 @@ mod tests {
let instruction = Instruction::new([0x4A, 0xBC]); let instruction = Instruction::new([0x4A, 0xBC]);
assert_eq!( assert_eq!(
instruction.processor_instruction, instruction.processor_instruction,
ProcessorInstruction::SkipNotEqualVXData{vx: 0xA, data: 0xBC} ProcessorInstruction::SkipNotEqualVXData {
vx: 0xA,
data: 0xBC
}
) )
} }
@ -383,7 +389,7 @@ mod tests {
let instruction = Instruction::new([0x5A, 0xB0]); let instruction = Instruction::new([0x5A, 0xB0]);
assert_eq!( assert_eq!(
instruction.processor_instruction, instruction.processor_instruction,
ProcessorInstruction::SkipEqualVXVY(0xA, 0xB) ProcessorInstruction::SkipEqualVXVY { vx: 0xA, vy: 0xB }
) )
} }
@ -392,7 +398,7 @@ mod tests {
let instruction = Instruction::new([0x9A, 0xB0]); let instruction = Instruction::new([0x9A, 0xB0]);
assert_eq!( assert_eq!(
instruction.processor_instruction, instruction.processor_instruction,
ProcessorInstruction::SkipNotEqualVXVY(0xA, 0xB) ProcessorInstruction::SkipNotEqualVXVY { vx: 0xA, vy: 0xB }
) )
} }