From 7c6c9af0261bd72ca42fe273591be40b2661bb10 Mon Sep 17 00:00:00 2001 From: Polesznyák Márk Date: Mon, 23 Feb 2026 19:44:57 +0100 Subject: fix: broken FX0A instruction implementation --- main.ha | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/main.ha b/main.ha index bcdbf96..7b84ff7 100644 --- a/main.ha +++ b/main.ha @@ -50,6 +50,7 @@ type state = struct { mem: [MEM_SIZE]u8, display: [SCREEN_WIDTH * SCREEN_HEIGHT]bool, draw: bool, + current_key_pressed: u8, keys: [16]bool, stack: [16]u16, SP: u16, @@ -63,6 +64,7 @@ type state = struct { fn chip8() state = { let state = state { PC = START_ADDR, + current_key_pressed = 0xFF, ... }; @@ -301,24 +303,19 @@ fn execute_instruction(state: *state, inst: inst, random: *random::random) void case 0x07 => state.V[inst.X] = state.DT; case 0x0A => - let any_pressed = false; - let key: u8 = 0xFF; - - for (let i = 0z; key == 0xFF && i < len(state.keys); i += 1) + for (let i = 0z; state.current_key_pressed == 0xFF && i < len(state.keys); i += 1) if (state.keys[i]) { - any_pressed = true; - key = i: u8; + state.current_key_pressed = i: u8; break; }; - if (!any_pressed) state.PC -= 2 + if (state.current_key_pressed == 0xFF) state.PC -= 2 else { - if (state.keys[key]) + if (state.keys[state.current_key_pressed]) state.PC -= 2 else { - state.V[inst.X] = key; - key = 0xFF; - any_pressed = false; + state.V[inst.X] = state.current_key_pressed; + state.current_key_pressed = 0xFF; }; }; case 0x15 => -- cgit v1.2.3