From a92eb11f4b77bd34db3eb529ec8e3877afdc8d65 Mon Sep 17 00:00:00 2001 From: cspark Date: Tue, 18 Nov 2025 03:07:40 +0000 Subject: [PATCH] Decode instructions using unions --- main.c | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/main.c b/main.c index 3bd3b3b..2653a3e 100644 --- a/main.c +++ b/main.c @@ -105,24 +105,19 @@ void overture_condition_jump(enum overture_condition_mode mode, uint8_t r0, uint }; } - -typedef struct overture_decoded_instruction { - uint8_t overture_mode : 2; - uint8_t left_operand : 3; - uint8_t right_operand : 3; - uint8_t combined_immediate : 6; -} overture_decoded_instruction; - -overture_decoded_instruction overture_decode_instruction(uint8_t current_instruction) { - overture_decoded_instruction instruction = { - .overture_mode = current_instruction >> 6, - .left_operand = (current_instruction << 2) >> 5, - .right_operand = (current_instruction << 5) >> 5, - .combined_immediate = (current_instruction << 2) >> 2, +typedef union overture_instruction { + uint8_t instruction; + union { + struct { + uint8_t combined_immediate : 6; + uint8_t overture_mode : 2; + }; + struct { + uint8_t right_operand : 3; + uint8_t left_operand : 3; + }; }; - - return instruction; -}; +} overture_instruction; void overture_fetch_instruction(uint8_t program_counter, uint8_t* current_instruction, uint8_t rom[256]) { *current_instruction = rom[program_counter - 1]; @@ -144,7 +139,7 @@ typedef struct overture { uint8_t output; bool output_enable; - uint8_t current_instruction; + overture_instruction current_instruction; } overture; void sleep_hz(int hz) { @@ -211,7 +206,7 @@ uint8_t* multiplex_destination(uint8_t operand, overture* cpu) { return destination; } -void overture_execute_instruction(overture_decoded_instruction instruction, overture* cpu) { +void overture_execute_instruction(overture_instruction instruction, overture* cpu) { uint8_t* source_register = multiplex_source(instruction.left_operand, cpu); uint8_t* destination_register = multiplex_destination(instruction.right_operand, cpu); @@ -252,9 +247,13 @@ void cpu_diagnostics(overture* cpu) { printf("input: %d\n", cpu->input); printf("output: %d\n", cpu->output); - printf("Output enabled?: %d\n", cpu->output_enable); + printf("Output enabled?: %d\n\n", cpu->output_enable); - printf("Current instruction: %d\n\n", cpu->current_instruction); + printf("Current instruction: %d\n", cpu->current_instruction.instruction); + printf("Mode: %d\n", cpu->current_instruction.overture_mode); + printf("Left Operand: %d\n", cpu->current_instruction.left_operand); + printf("Right Operand: %d\n", cpu->current_instruction.right_operand); + printf("Combined Immediate: %d\n\n", cpu->current_instruction.combined_immediate); } void overture_cycle(overture* cpu, uint8_t rom[256]) { @@ -263,10 +262,9 @@ void overture_cycle(overture* cpu, uint8_t rom[256]) { }; cpu->program_counter += 1; cpu->output_enable = false; - overture_fetch_instruction(cpu->program_counter, &(cpu->current_instruction), rom); + overture_fetch_instruction(cpu->program_counter, &(cpu->current_instruction.instruction), rom); - overture_decoded_instruction instruction = overture_decode_instruction(cpu->current_instruction); - overture_execute_instruction(instruction, cpu); + overture_execute_instruction(cpu->current_instruction, cpu); cpu_diagnostics(cpu); } @@ -301,7 +299,7 @@ int main() { .output = 0, .output_enable = true, - .current_instruction = 0 + .current_instruction.instruction = 0 }; overture_start_clock(&cpu, rom);