aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPolesznyák Márk <contact@pml68.dev>2026-02-23 19:44:57 +0100
committerPolesznyák Márk <contact@pml68.dev>2026-02-23 19:44:57 +0100
commit7c6c9af0261bd72ca42fe273591be40b2661bb10 (patch)
tree9ea3e1fd74bcb3d8cf7f1ddd4cc46a34b3890917
parentfix: incorrectly implemented instructions (8XY6, 8XYE) (diff)
downloadhare-chip8-7c6c9af0261bd72ca42fe273591be40b2661bb10.tar.gz
fix: broken FX0A instruction implementation
-rw-r--r--main.ha19
1 files 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 =>