use named enum for SetRegister instruction

This commit is contained in:
Denis-Cosmin Nutiu 2024-12-10 18:58:21 +02:00
parent aa27658864
commit ede70d15a0
2 changed files with 22 additions and 10 deletions

View file

@ -208,7 +208,7 @@ where
trace!("Jump to address {:04x}", address);
self.program_counter = address
}
ProcessorInstruction::SetRegister(register, data) => {
ProcessorInstruction::SetRegister { register, data } => {
trace!("Set register {} to data {:04x}", register, data);
self.registers[register as usize] = data
}

View file

@ -19,7 +19,7 @@ pub enum ProcessorInstruction {
/// Jumps to a given address
Jump { address: u16 },
/// Sets the register in the first argument to the given value
SetRegister(u8, u8),
SetRegister { register: u8, data: u8 },
/// Adds the value to the register
AddValueToRegister(u8, u8),
/// Sets the index register
@ -126,16 +126,16 @@ impl Instruction {
(0x1, _, _, _) => {
// 1NNN
ProcessorInstruction::Jump {
address: Self::grab_inner_data(data)
address: Self::grab_inner_data(data),
}
}
// Set Register
(0x6, _, _, _) => {
// 6XNN
ProcessorInstruction::SetRegister(
Self::grab_first_nibble(data),
Self::grab_last_byte(data),
)
ProcessorInstruction::SetRegister {
register: Self::grab_first_nibble(data),
data: Self::grab_last_byte(data),
}
}
// Add value to register
(0x7, _, _, _) => {
@ -327,6 +327,15 @@ mod tests {
)
}
#[test]
fn test_instruction_jump() {
let instruction = Instruction::new([0x1A, 0xBC]);
assert_eq!(
instruction.processor_instruction,
ProcessorInstruction::Jump { address: 0xABC }
)
}
#[test]
fn test_instruction_call() {
let instruction = Instruction::new([0x2A, 0xBC]);
@ -386,7 +395,10 @@ mod tests {
let instruction = Instruction::new([0x61, 0x40]);
assert_eq!(
instruction.processor_instruction,
ProcessorInstruction::SetRegister(0x1, 0x40)
ProcessorInstruction::SetRegister {
register: 0x1,
data: 0x40
}
)
}