From f703d195f99b7a1c868f085b3811627343045a94 Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Sat, 11 Nov 2023 14:54:35 -0500 Subject: [PATCH] Refactor InstructionRunner trait to be Run trait on each instruction type --- src/cpu/run.rs | 18 ++++++------------ src/cpu/run/arith8.rs | 34 ++++++++++++++-------------------- src/cpu/run/load16.rs | 13 ++++--------- src/cpu/run/load8.rs | 13 ++++--------- 4 files changed, 28 insertions(+), 50 deletions(-) diff --git a/src/cpu/run.rs b/src/cpu/run.rs index 862427e..1bd002b 100644 --- a/src/cpu/run.rs +++ b/src/cpu/run.rs @@ -26,23 +26,17 @@ pub enum Error { Unknown(Box), } -/// `InstructionRunner` takes a single instruction and runs it on the given processor. -trait InstructionRunner { - fn run_instruction(processor: &mut Processor, instruction: I) -> Result<(), Error>; +/// `Run` takes a single instruction and runs it on the given processor. +trait Run { + fn run_on(&self, processor: &mut Processor) -> Result<(), Error>; } /// `run_instruction` will run the given instruction on the processor. #[allow(clippy::module_name_repetitions)] pub fn run_instruction(processor: &mut Processor, instruction: Instruction) -> Result<(), Error> { match instruction { - Instruction::EightBitLoad(load_instruction) => { - load8::EightBitLoadRunner::run_instruction(processor, load_instruction) - } - Instruction::SixteenBitLoad(load_instruction) => { - load16::SixteenBitLoadRunner::run_instruction(processor, load_instruction) - } - Instruction::EightBitArithmetic(arith_instruction) => { - arith8::EightBitArithmeticRunner::run_instruction(processor, arith_instruction) - } + Instruction::EightBitLoad(load_instruction) => load_instruction.run_on(processor), + Instruction::SixteenBitLoad(load_instruction) => load_instruction.run_on(processor), + Instruction::EightBitArithmetic(arith_instruction) => arith_instruction.run_on(processor), } } diff --git a/src/cpu/run/arith8.rs b/src/cpu/run/arith8.rs index 89b4fc0..2a3f2f2 100644 --- a/src/cpu/run/arith8.rs +++ b/src/cpu/run/arith8.rs @@ -4,14 +4,11 @@ use crate::{ register, }; -use super::InstructionRunner; +use super::Run; mod binary; mod unary; -#[derive(Copy, Clone, Debug)] -pub struct EightBitArithmeticRunner; - struct OperationOutput { value: u8, flags: OperationFlagOutput, @@ -25,31 +22,28 @@ struct OperationFlagOutput { half_carry_flag: u8, } -impl InstructionRunner for EightBitArithmeticRunner { - fn run_instruction( - processor: &mut Processor, - instruction: EightBitArithmeticInstruction, - ) -> Result<(), Error> { - match instruction.operation { - Operation::Add => binary::run_add(processor, instruction.operand), +impl Run for EightBitArithmeticInstruction { + fn run_on(&self, processor: &mut Processor) -> Result<(), Error> { + match self.operation { + Operation::Add => binary::run_add(processor, self.operand), - Operation::AddWithCarry => binary::run_add_with_carry(processor, instruction.operand), + Operation::AddWithCarry => binary::run_add_with_carry(processor, self.operand), - Operation::Sub => binary::run_sub(processor, instruction.operand), + Operation::Sub => binary::run_sub(processor, self.operand), - Operation::SubWithCarry => binary::run_sub_with_carry(processor, instruction.operand), + Operation::SubWithCarry => binary::run_sub_with_carry(processor, self.operand), - Operation::And => binary::run_and(processor, instruction.operand), + Operation::And => binary::run_and(processor, self.operand), - Operation::Xor => binary::run_xor(processor, instruction.operand), + Operation::Xor => binary::run_xor(processor, self.operand), - Operation::Or => binary::run_or(processor, instruction.operand), + Operation::Or => binary::run_or(processor, self.operand), - Operation::Compare => binary::run_compare(processor, instruction.operand), + Operation::Compare => binary::run_compare(processor, self.operand), - Operation::Inc => unary::run_inc(processor, instruction.operand), + Operation::Inc => unary::run_inc(processor, self.operand), - Operation::Dec => unary::run_dec(processor, instruction.operand), + Operation::Dec => unary::run_dec(processor, self.operand), } } } diff --git a/src/cpu/run/load16.rs b/src/cpu/run/load16.rs index e942ec1..c8ee6b1 100644 --- a/src/cpu/run/load16.rs +++ b/src/cpu/run/load16.rs @@ -1,18 +1,13 @@ use super::arithutil::CarryingAdd; -use super::{Error, InstructionRunner}; +use super::{Error, Run}; use crate::cpu::{instructions::load16::SixteenBitLoadInstruction, Processor}; use crate::{memory, register}; -pub(super) struct SixteenBitLoadRunner; - -impl InstructionRunner for SixteenBitLoadRunner { +impl Run for SixteenBitLoadInstruction { // TODO: Fix this #[allow(clippy::too_many_lines)] - fn run_instruction( - processor: &mut Processor, - instruction: SixteenBitLoadInstruction, - ) -> Result<(), Error> { - match instruction { + fn run_on(&self, processor: &mut Processor) -> Result<(), Error> { + match *self { SixteenBitLoadInstruction::LoadImmediateToRegister { dst, value } => { processor.registers.set_16bit_register(dst, value); diff --git a/src/cpu/run/load8.rs b/src/cpu/run/load8.rs index 169f81a..b71898c 100644 --- a/src/cpu/run/load8.rs +++ b/src/cpu/run/load8.rs @@ -1,19 +1,14 @@ -use super::{Error, InstructionRunner}; +use super::{Error, Run}; use crate::{ cpu::{instructions::load8::EightBitLoadInstruction, Processor}, memory, register, }; -pub(super) struct EightBitLoadRunner; - -impl InstructionRunner for EightBitLoadRunner { +impl Run for EightBitLoadInstruction { // TODO: Break this up somehow #[allow(clippy::too_many_lines)] - fn run_instruction( - processor: &mut Processor, - instruction: EightBitLoadInstruction, - ) -> Result<(), Error> { - match instruction { + fn run_on(&self, processor: &mut Processor) -> Result<(), Error> { + match *self { EightBitLoadInstruction::LoadImmediateToRegister { value, register } => { processor .registers