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

@ -204,11 +204,11 @@ where
self.display_data = [false; DISPLAY_WIDTH * DISPLAY_HEIGHT]; self.display_data = [false; DISPLAY_WIDTH * DISPLAY_HEIGHT];
self.display.clear() self.display.clear()
} }
ProcessorInstruction::Jump {address} => { ProcessorInstruction::Jump { address } => {
trace!("Jump to address {:04x}", address); trace!("Jump to address {:04x}", address);
self.program_counter = address self.program_counter = address
} }
ProcessorInstruction::SetRegister(register, data) => { ProcessorInstruction::SetRegister { register, data } => {
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
} }

View file

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