c-like struct for AddValueToRegister instruction

This commit is contained in:
Denis-Cosmin Nutiu 2024-12-10 19:01:34 +02:00
parent ede70d15a0
commit 7f07d3b184
2 changed files with 12 additions and 9 deletions

View file

@ -212,9 +212,9 @@ where
trace!("Set register {} to data {:04x}", register, data); trace!("Set register {} to data {:04x}", register, data);
self.registers[register as usize] = data self.registers[register as usize] = data
} }
ProcessorInstruction::AddValueToRegister(register, data) => { ProcessorInstruction::AddValueToRegister { register, value } => {
trace!("Add to register {} data {:04x}", register, data); trace!("Add to register {} data {:04x}", register, value);
let (result, _) = self.registers[register as usize].overflowing_add(data); let (result, _) = self.registers[register as usize].overflowing_add(value);
self.registers[register as usize] = result; self.registers[register as usize] = result;
} }
ProcessorInstruction::SetIndexRegister(data) => { ProcessorInstruction::SetIndexRegister(data) => {

View file

@ -21,7 +21,7 @@ pub enum ProcessorInstruction {
/// Sets the register in the first argument to the given value /// Sets the register in the first argument to the given value
SetRegister { register: u8, data: u8 }, SetRegister { register: u8, data: u8 },
/// Adds the value to the register /// Adds the value to the register
AddValueToRegister(u8, u8), AddValueToRegister { register: u8, value: u8 },
/// Sets the index register /// Sets the index register
SetIndexRegister(u16), SetIndexRegister(u16),
/// Draws to the screen. /// Draws to the screen.
@ -140,10 +140,10 @@ impl Instruction {
// Add value to register // Add value to register
(0x7, _, _, _) => { (0x7, _, _, _) => {
// 7XNN // 7XNN
ProcessorInstruction::AddValueToRegister( ProcessorInstruction::AddValueToRegister {
Self::grab_first_nibble(data), register: Self::grab_first_nibble(data),
Self::grab_last_byte(data), value: Self::grab_last_byte(data),
) }
} }
// Set index register // Set index register
(0xA, _, _, _) => ProcessorInstruction::SetIndexRegister(Self::grab_inner_data(data)), (0xA, _, _, _) => ProcessorInstruction::SetIndexRegister(Self::grab_inner_data(data)),
@ -407,7 +407,10 @@ mod tests {
let instruction = Instruction::new([0x71, 0x40]); let instruction = Instruction::new([0x71, 0x40]);
assert_eq!( assert_eq!(
instruction.processor_instruction, instruction.processor_instruction,
ProcessorInstruction::AddValueToRegister(0x1, 0x40) ProcessorInstruction::AddValueToRegister {
register: 0x1,
value: 0x40
}
) )
} }