c-like struct for Draw instruction

This commit is contained in:
Denis-Cosmin Nutiu 2024-12-10 19:05:20 +02:00
parent a464c00836
commit a9d252f758
2 changed files with 16 additions and 14 deletions

View file

@ -221,18 +221,16 @@ where
trace!("Set index register to data {:04x}", data); trace!("Set index register to data {:04x}", data);
self.index_register = data; self.index_register = data;
} }
ProcessorInstruction::Draw(vx_register, vy_register, num_rows) => { ProcessorInstruction::Draw { vx, vy, rows } => {
trace!( trace!("Draw vx_register={vx} vy_register={vy} pixels={rows}");
"Draw vx_register={vx_register} vy_register={vy_register} pixels={num_rows}" let x_coordinate = self.registers[vx as usize];
); let y_coordinate = self.registers[vy as usize];
let x_coordinate = self.registers[vx_register as usize];
let y_coordinate = self.registers[vy_register as usize];
// Keep track if any pixels were flipped // Keep track if any pixels were flipped
let mut flipped = false; let mut flipped = false;
// Iterate over each row of our sprite // Iterate over each row of our sprite
for y_line in 0..num_rows { for y_line in 0..rows {
// Determine which memory address our row's data is stored // Determine which memory address our row's data is stored
let addr = self.index_register + y_line as u16; let addr = self.index_register + y_line as u16;
let pixels = self.memory[addr as usize]; let pixels = self.memory[addr as usize];

View file

@ -25,7 +25,7 @@ pub enum ProcessorInstruction {
/// Sets the index register /// Sets the index register
SetIndexRegister { data: u16 }, SetIndexRegister { data: u16 },
/// Draws to the screen. /// Draws to the screen.
Draw(u8, u8, u8), Draw { vx: u8, vy: u8, rows: u8 },
/// Call sets PC to the address and saves the return address on the stack /// Call sets PC to the address and saves the return address on the stack
Call(u16), Call(u16),
/// Pops the stack and sets the PC /// Pops the stack and sets the PC
@ -152,11 +152,11 @@ impl Instruction {
// Draw on screen // Draw on screen
(0xD, _, _, _) => { (0xD, _, _, _) => {
// DXYN // DXYN
ProcessorInstruction::Draw( ProcessorInstruction::Draw {
Self::grab_first_nibble(data), vx: Self::grab_first_nibble(data),
Self::grab_middle_nibble(data), vy: Self::grab_middle_nibble(data),
Self::grab_last_nibble(data), rows: Self::grab_last_nibble(data),
) }
} }
(0x0, 0x0, 0xE, 0xE) => ProcessorInstruction::Return, (0x0, 0x0, 0xE, 0xE) => ProcessorInstruction::Return,
(0x2, _, _, _) => ProcessorInstruction::Call(Self::grab_inner_data(data)), (0x2, _, _, _) => ProcessorInstruction::Call(Self::grab_inner_data(data)),
@ -529,7 +529,11 @@ mod tests {
let instruction = Instruction::new([0xDA, 0xBC]); let instruction = Instruction::new([0xDA, 0xBC]);
assert_eq!( assert_eq!(
instruction.processor_instruction, instruction.processor_instruction,
ProcessorInstruction::Draw(0xA, 0xB, 0xC) ProcessorInstruction::Draw {
vx: 0xA,
vy: 0xB,
rows: 0xC
}
) )
} }