use c-like struct for more instructions

This commit is contained in:
Denis-Cosmin Nutiu 2024-12-11 00:28:21 +02:00
parent e7b69b72b2
commit a9432ce7ed
2 changed files with 36 additions and 34 deletions

View file

@ -375,19 +375,19 @@ where
self.program_counter += 2 self.program_counter += 2
} }
} }
ProcessorInstruction::SetVXToDelayTimer(vx) => { ProcessorInstruction::SetVXToDelayTimer { vx } => {
trace!("SetVXToDelayTimer"); trace!("SetVXToDelayTimer");
self.registers[vx as usize] = self.delay_timer self.registers[vx as usize] = self.delay_timer
} }
ProcessorInstruction::SetDelayTimer(vx) => { ProcessorInstruction::SetDelayTimer { vx } => {
trace!("SetDelayTimer"); trace!("SetDelayTimer");
self.delay_timer = self.registers[vx as usize] self.delay_timer = self.registers[vx as usize]
} }
ProcessorInstruction::SetSoundTimer(vx) => { ProcessorInstruction::SetSoundTimer { vx } => {
trace!("SetSoundTimer"); trace!("SetSoundTimer");
self.sound_timer = self.registers[vx as usize] self.sound_timer = self.registers[vx as usize]
} }
ProcessorInstruction::AddToIndex(vx) => { ProcessorInstruction::AddToIndex { vx } => {
trace!("AddToIndex"); trace!("AddToIndex");
let (result, overflow) = self let (result, overflow) = self
.index_register .index_register
@ -399,10 +399,10 @@ where
self.registers[0xF] = 0 self.registers[0xF] = 0
} }
} }
ProcessorInstruction::FontCharacter(vx) => { ProcessorInstruction::FontCharacter { vx } => {
self.index_register = 0xF0 + (self.registers[vx as usize] as u16 & 0xF) * 5u16; self.index_register = 0xF0 + (self.registers[vx as usize] as u16 & 0xF) * 5u16;
} }
ProcessorInstruction::BinaryCodedDecimalConversion(vx) => { ProcessorInstruction::BinaryCodedDecimalConversion { vx } => {
let number = self.registers[vx as usize]; let number = self.registers[vx as usize];
self.memory[self.index_register as usize] = number / 100; self.memory[self.index_register as usize] = number / 100;
self.memory[self.index_register as usize + 1] = (number / 10) % 10; self.memory[self.index_register as usize + 1] = (number / 10) % 10;

View file

@ -63,17 +63,17 @@ pub enum ProcessorInstruction {
/// Skip the next instruction if VX is not equal to VY. /// Skip the next instruction if VX is not equal to VY.
SkipNotEqualVXVY { vx: u8, vy: 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 { vx: u8 },
/// Sets the delay timer to the value in VX. /// Sets the delay timer to the value in VX.
SetDelayTimer(u8), SetDelayTimer { vx: u8 },
/// Sets the sound timer to the value in VX. /// Sets the sound timer to the value in VX.
SetSoundTimer(u8), SetSoundTimer { vx: u8 },
/// Adds the value of VX to the index register and sets the overflow flag. /// Adds the value of VX to the index register and sets the overflow flag.
AddToIndex(u8), AddToIndex { vx: u8 },
/// Sets the index register to the hexadecimal character in VX. /// Sets the index register to the hexadecimal character in VX.
FontCharacter(u8), FontCharacter { vx: u8 },
/// Converts the number in VX to 3 digits. /// Converts the number in VX to 3 digits.
BinaryCodedDecimalConversion(u8), BinaryCodedDecimalConversion { vx: u8 },
/// Stores the general purpose registers in memory at index register address. /// Stores the general purpose registers in memory at index register address.
StoreMemory(u8), StoreMemory(u8),
/// Loads the general purpose registers from memory at index register address. /// Loads the general purpose registers from memory at index register address.
@ -221,22 +221,24 @@ impl Instruction {
vx: Self::grab_first_nibble(data), vx: Self::grab_first_nibble(data),
vy: Self::grab_middle_nibble(data), vy: Self::grab_middle_nibble(data),
}, },
(0xF, _, 0x0, 0x7) => { (0xF, _, 0x0, 0x7) => ProcessorInstruction::SetVXToDelayTimer {
ProcessorInstruction::SetVXToDelayTimer(Self::grab_first_nibble(data)) vx: Self::grab_first_nibble(data),
} },
(0xF, _, 0x1, 0x5) => { (0xF, _, 0x1, 0x5) => ProcessorInstruction::SetDelayTimer {
ProcessorInstruction::SetDelayTimer(Self::grab_first_nibble(data)) vx: Self::grab_first_nibble(data),
} },
(0xF, _, 0x1, 0x8) => { (0xF, _, 0x1, 0x8) => ProcessorInstruction::SetSoundTimer {
ProcessorInstruction::SetSoundTimer(Self::grab_first_nibble(data)) vx: Self::grab_first_nibble(data),
} },
(0xF, _, 0x1, 0xE) => ProcessorInstruction::AddToIndex(Self::grab_first_nibble(data)), (0xF, _, 0x1, 0xE) => ProcessorInstruction::AddToIndex {
(0xF, _, 0x2, 0x9) => { vx: Self::grab_first_nibble(data),
ProcessorInstruction::FontCharacter(Self::grab_first_nibble(data)) },
} (0xF, _, 0x2, 0x9) => ProcessorInstruction::FontCharacter {
(0xF, _, 0x3, 0x3) => { vx: Self::grab_first_nibble(data),
ProcessorInstruction::BinaryCodedDecimalConversion(Self::grab_first_nibble(data)) },
} (0xF, _, 0x3, 0x3) => ProcessorInstruction::BinaryCodedDecimalConversion {
vx: Self::grab_first_nibble(data),
},
(0xF, _, 0x5, 0x5) => ProcessorInstruction::StoreMemory(Self::grab_first_nibble(data)), (0xF, _, 0x5, 0x5) => ProcessorInstruction::StoreMemory(Self::grab_first_nibble(data)),
(0xF, _, 0x6, 0x5) => ProcessorInstruction::LoadMemory(Self::grab_first_nibble(data)), (0xF, _, 0x6, 0x5) => ProcessorInstruction::LoadMemory(Self::grab_first_nibble(data)),
(0xE, _, 0x9, 0xE) => { (0xE, _, 0x9, 0xE) => {
@ -555,7 +557,7 @@ mod tests {
let instruction = Instruction::new([0xFA, 0x07]); let instruction = Instruction::new([0xFA, 0x07]);
assert_eq!( assert_eq!(
instruction.processor_instruction, instruction.processor_instruction,
ProcessorInstruction::SetVXToDelayTimer(0xA) ProcessorInstruction::SetVXToDelayTimer { vx: 0xA }
) )
} }
@ -564,7 +566,7 @@ mod tests {
let instruction = Instruction::new([0xFA, 0x15]); let instruction = Instruction::new([0xFA, 0x15]);
assert_eq!( assert_eq!(
instruction.processor_instruction, instruction.processor_instruction,
ProcessorInstruction::SetDelayTimer(0xA) ProcessorInstruction::SetDelayTimer { vx: 0xA }
) )
} }
@ -573,7 +575,7 @@ mod tests {
let instruction = Instruction::new([0xFA, 0x18]); let instruction = Instruction::new([0xFA, 0x18]);
assert_eq!( assert_eq!(
instruction.processor_instruction, instruction.processor_instruction,
ProcessorInstruction::SetSoundTimer(0xA) ProcessorInstruction::SetSoundTimer { vx: 0xA }
) )
} }
@ -582,7 +584,7 @@ mod tests {
let instruction = Instruction::new([0xFA, 0x1E]); let instruction = Instruction::new([0xFA, 0x1E]);
assert_eq!( assert_eq!(
instruction.processor_instruction, instruction.processor_instruction,
ProcessorInstruction::AddToIndex(0xA) ProcessorInstruction::AddToIndex { vx: 0xA }
) )
} }
@ -591,7 +593,7 @@ mod tests {
let instruction = Instruction::new([0xFA, 0x29]); let instruction = Instruction::new([0xFA, 0x29]);
assert_eq!( assert_eq!(
instruction.processor_instruction, instruction.processor_instruction,
ProcessorInstruction::FontCharacter(0xA) ProcessorInstruction::FontCharacter { vx: 0xA }
) )
} }
@ -600,7 +602,7 @@ mod tests {
let instruction = Instruction::new([0xFA, 0x33]); let instruction = Instruction::new([0xFA, 0x33]);
assert_eq!( assert_eq!(
instruction.processor_instruction, instruction.processor_instruction,
ProcessorInstruction::BinaryCodedDecimalConversion(0xA) ProcessorInstruction::BinaryCodedDecimalConversion { vx: 0xA }
) )
} }