From 24fd0c4ad60d387e6c77f7ded058db5c8648547f Mon Sep 17 00:00:00 2001 From: Themis Demetriades Date: Wed, 12 Jun 2024 18:42:32 +0100 Subject: [PATCH] Changed execute function to use function pointer array w/ S --- src/emulator/execute.c | 51 +++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/src/emulator/execute.c b/src/emulator/execute.c index 37f89e7..abb6e0e 100644 --- a/src/emulator/execute.c +++ b/src/emulator/execute.c @@ -16,44 +16,33 @@ // instruction if the shift flag is enabled. #define DPI_WIDEMOV_SHIFT 16 +// Type definition for a pointer to a function that executes a particular type +// of a64instruction (requires pointer to the processor state to update, and a +// pointer to the instruction struct to execute). +typedef void (*executeFunction)(Machine *, a64inst_instruction *); + // Prototypes static void executeSDT(Machine *state, a64inst_instruction *inst); static void executeBranch(Machine *state, a64inst_instruction *inst); static void executeDPImmediate(Machine *state, a64inst_instruction *inst); static void executeDPRegister(Machine *state, a64inst_instruction *inst); +// Halt function definition +static void executeHalt(Machine *state, a64inst_instruction *inst) { /*NOP*/ } + +// Define a constant array mapping instruction type enum values to their +// corresponding execute functions +const executeFunction EXECUTE_FUNCTIONS[] = + {&executeDPImmediate, + &executeDPRegister, + &executeSDT, + &executeSDT, + &executeBranch, + &executeHalt}; + +// Main execute function void execute(Machine *state, a64inst_instruction *inst) { - - switch (inst->type) { - - // Halt the program - case a64inst_HALT: - break; - - // Execute a data processing immediate instruction - case a64inst_DPIMMEDIATE: - executeDPImmediate(state, inst); - break; - - // Execute a branch instruction - case a64inst_BRANCH: - executeBranch(state, inst); - break; - - // Execute a data processing register instruction - case a64inst_DPREGISTER: - executeDPRegister(state, inst); - break; - - case a64inst_SINGLETRANSFER: - executeSDT(state, inst); - break; - - // Unknown instruction - default: - break; - } - + EXECUTE_FUNCTIONS[inst->type](state, inst); } // Updates N and Z condition codes given the machine and a result value