From a9d252f758c3978e20cf45c87be63e9b18d66062 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Tue, 10 Dec 2024 19:05:20 +0200 Subject: [PATCH] c-like struct for Draw instruction --- src/emulator.rs | 12 +++++------- src/instruction.rs | 18 +++++++++++------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/emulator.rs b/src/emulator.rs index 8a4e414..f245946 100644 --- a/src/emulator.rs +++ b/src/emulator.rs @@ -221,18 +221,16 @@ where trace!("Set index register to data {:04x}", data); self.index_register = data; } - ProcessorInstruction::Draw(vx_register, vy_register, num_rows) => { - trace!( - "Draw vx_register={vx_register} vy_register={vy_register} pixels={num_rows}" - ); - let x_coordinate = self.registers[vx_register as usize]; - let y_coordinate = self.registers[vy_register as usize]; + ProcessorInstruction::Draw { vx, vy, rows } => { + trace!("Draw vx_register={vx} vy_register={vy} pixels={rows}"); + let x_coordinate = self.registers[vx as usize]; + let y_coordinate = self.registers[vy as usize]; // Keep track if any pixels were flipped let mut flipped = false; // 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 let addr = self.index_register + y_line as u16; let pixels = self.memory[addr as usize]; diff --git a/src/instruction.rs b/src/instruction.rs index b59b041..6ece8eb 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -25,7 +25,7 @@ pub enum ProcessorInstruction { /// Sets the index register SetIndexRegister { data: u16 }, /// 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(u16), /// Pops the stack and sets the PC @@ -152,11 +152,11 @@ impl Instruction { // Draw on screen (0xD, _, _, _) => { // DXYN - ProcessorInstruction::Draw( - Self::grab_first_nibble(data), - Self::grab_middle_nibble(data), - Self::grab_last_nibble(data), - ) + ProcessorInstruction::Draw { + vx: Self::grab_first_nibble(data), + vy: Self::grab_middle_nibble(data), + rows: Self::grab_last_nibble(data), + } } (0x0, 0x0, 0xE, 0xE) => ProcessorInstruction::Return, (0x2, _, _, _) => ProcessorInstruction::Call(Self::grab_inner_data(data)), @@ -529,7 +529,11 @@ mod tests { let instruction = Instruction::new([0xDA, 0xBC]); assert_eq!( instruction.processor_instruction, - ProcessorInstruction::Draw(0xA, 0xB, 0xC) + ProcessorInstruction::Draw { + vx: 0xA, + vy: 0xB, + rows: 0xC + } ) }