Holding two buttons together should trigger one action, not one action every frame.

Use it in a project
REQUIRED is a nonempty mask; FORBIDDEN must not overlap it. All required bits must be held and every forbidden bit clear. ACTIVE reports that condition and PRESSED reports its rising edge against PREVIOUS. Invalid configuration clears result flags and PREVIOUS, preventing a stale active latch.
Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: PREVIOUS, HELD, REQUIRED, FORBIDDEN, ACTIVE, PRESSED, VALID. They occupy 7 contiguous bytes. Also reserve the private workspace: T0 (1 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.
Call input_chord_init once at the intended reset boundary. It clears inputs as well as outputs and workspace. Set your starting state and configuration afterwards, then call input_chord_step whenever the described event occurs. The helper reads no device, waits for no frame and installs no interrupt handler.
| Ownership | Fields |
|---|---|
| Retain between calls | PREVIOUS |
| Supply before service | HELD, REQUIRED and FORBIDDEN |
| Read after service | ACTIVE, PRESSED and VALID |
First compute whether the chord is active as a whole. Then compare that condition with the previous snapshot. This lets the player press the required buttons one after another and still create one chord edge.
Reusable initialisation and service
; SPDX-License-Identifier: MIT
; Detect a Button Chord Once. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
input_chord_init:
ld a,0
ld (PREVIOUS),a
ld (HELD),a
ld (REQUIRED),a
ld (FORBIDDEN),a
ld (ACTIVE),a
ld (PRESSED),a
ld (VALID),a
ld (T0),a
ret
input_chord_step:
; ACTIVE = 0
ld a,0
ld (ACTIVE),a
; PRESSED = 0
ld a,0
ld (PRESSED),a
; VALID = 0
ld a,0
ld (VALID),a
; if REQUIRED == 0:
ld a,(REQUIRED)
cp 0
jp z,input_chord_L1
jp input_chord_L2
input_chord_L1:
; PREVIOUS = 0
ld a,0
ld (PREVIOUS),a
; return
ret
jp input_chord_L3
input_chord_L2:
input_chord_L3:
; T0 = REQUIRED & FORBIDDEN
ld a,(REQUIRED)
ld b,a
ld a,(FORBIDDEN)
ld c,a
ld a,b
and c
ld (T0),a
; if T0 != 0:
ld a,(T0)
cp 0
jp nz,input_chord_L4
jp input_chord_L5
input_chord_L4:
; PREVIOUS = 0
ld a,0
ld (PREVIOUS),a
; return
ret
jp input_chord_L6
input_chord_L5:
input_chord_L6:
; VALID = 1
ld a,1
ld (VALID),a
; T0 = HELD & REQUIRED
ld a,(HELD)
ld b,a
ld a,(REQUIRED)
ld c,a
ld a,b
and c
ld (T0),a
; if T0 == REQUIRED:
ld a,(T0)
ld b,a
ld a,(REQUIRED)
ld c,a
ld a,b
cp c
jp z,input_chord_L7
jp input_chord_L8
input_chord_L7:
; T0 = HELD & FORBIDDEN
ld a,(HELD)
ld b,a
ld a,(FORBIDDEN)
ld c,a
ld a,b
and c
ld (T0),a
; if T0 == 0:
ld a,(T0)
cp 0
jp z,input_chord_L10
jp input_chord_L11
input_chord_L10:
; ACTIVE = 1
ld a,1
ld (ACTIVE),a
jp input_chord_L12
input_chord_L11:
input_chord_L12:
jp input_chord_L9
input_chord_L8:
input_chord_L9:
; if ACTIVE != 0:
ld a,(ACTIVE)
cp 0
jp nz,input_chord_L13
jp input_chord_L14
input_chord_L13:
; if PREVIOUS == 0:
ld a,(PREVIOUS)
cp 0
jp z,input_chord_L16
jp input_chord_L17
input_chord_L16:
; PRESSED = 1
ld a,1
ld (PRESSED),a
jp input_chord_L18
input_chord_L17:
input_chord_L18:
jp input_chord_L15
input_chord_L14:
input_chord_L15:
; PREVIOUS = ACTIVE
ld a,(ACTIVE)
ld (PREVIOUS),a
ret
component_end:Machine contract
AF/BC/DE/HL may change; IX/IY are preserved. A direct CALL needs two return-address stack bytes. The helper makes no nested calls and adds no other stack use. Its named scratch is private workspace, not a pointer, a ROM variable or shared zero-page space that the game may borrow concurrently. Scope or rename routine and field labels and allocate a separate block for each independent instance. Give each block one mutation owner.
The 48K diagnostic boots through ROM tape loading and USR, disables interrupts and owns the screen. The helper performs no port or screen writes.
Adapt the caller
demo.asm supplies the bindings and include order. exercise.inc first runs table cases, then a separate client retaining state between calls. A game can call the public step entry directly and remove the diagnostic packet and display. Do not copy the diagnostic’s hardware takeover into an existing game loop.
Build with make ASM198X=/path/to/asm198x. The Makefile produces normal, custom, deliberately wrong and repaired programs. Tools must already be installed; the sample downloads nothing.
Bindings and include order
org $8000
CUSTOM equ 0
BROKEN equ 0
COYOTE_STEPS equ 4
BUFFER_STEPS equ 4
TEXT_CAP equ 8
PROPERTY_COUNT equ 16
STREAM_BYTES equ 13
CRC_POLY equ 7
include "caller.inc"
include "exercise.inc"
component_start:
include "component.inc"
include "data.inc"
include "cases.inc"
include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
PREVIOUS: defb 0
HELD: defb 0
REQUIRED: defb 0
FORBIDDEN: defb 0
ACTIVE: defb 0
PRESSED: defb 0
VALID: defb 0
T0: defb 0
input_ptr: defw 0
output_ptr: defw 0
cases_left: defb 0
ready: defb 0
preview: defs 8,0
OUTPUT: defs 1300,0
end startA client retaining state between calls
preview_rules:
if CUSTOM
; REQUIRED = 5
ld a,5
ld (REQUIRED),a
; FORBIDDEN = 2
ld a,2
ld (FORBIDDEN),a
; HELD = 5
ld a,5
ld (HELD),a
if BROKEN
; PREVIOUS = 0
ld a,0
ld (PREVIOUS),a
endif
call checked_step
call emit_state
if BROKEN
; PREVIOUS = 0
ld a,0
ld (PREVIOUS),a
endif
call checked_step
call emit_state
if BROKEN
; PREVIOUS = 0
ld a,0
ld (PREVIOUS),a
endif
call checked_step
call emit_state
ret
else
; REQUIRED = 3
ld a,3
ld (REQUIRED),a
; FORBIDDEN = 4
ld a,4
ld (FORBIDDEN),a
; HELD = 3
ld a,3
ld (HELD),a
if BROKEN
; PREVIOUS = 0
ld a,0
ld (PREVIOUS),a
endif
call checked_step
call emit_state
if BROKEN
; PREVIOUS = 0
ld a,0
ld (PREVIOUS),a
endif
call checked_step
call emit_state
; HELD = 7
ld a,7
ld (HELD),a
if BROKEN
; PREVIOUS = 0
ld a,0
ld (PREVIOUS),a
endif
call checked_step
call emit_state
; HELD = 3
ld a,3
ld (HELD),a
if BROKEN
; PREVIOUS = 0
ld a,0
ld (PREVIOUS),a
endif
call checked_step
call emit_state
if BROKEN
; PREVIOUS = 0
ld a,0
ld (PREVIOUS),a
endif
call checked_step
call emit_state
endif
retCustomise it
Require buttons 1 and 4 rather than buttons 1 and 2. The maintained custom caller changes its inputs or client sequence rather than hiding a different implementation behind the same label. Compare every state of the two clients, not only their final display.
Custom client selection
org $8000
CUSTOM equ 1
BROKEN equ 0
COYOTE_STEPS equ 2
BUFFER_STEPS equ 2
TEXT_CAP equ 4
PROPERTY_COUNT equ 16
STREAM_BYTES equ 13
CRC_POLY equ 49
include "caller.inc"
include "exercise.inc"
component_start:
include "component.inc"
include "data.inc"
include "cases.inc"
include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
PREVIOUS: defb 0
HELD: defb 0
REQUIRED: defb 0
FORBIDDEN: defb 0
ACTIVE: defb 0
PRESSED: defb 0
VALID: defb 0
T0: defb 0
input_ptr: defw 0
output_ptr: defw 0
cases_left: defb 0
ready: defb 0
preview: defs 8,0
OUTPUT: defs 1300,0
end startThe custom diagnostic row is 01 05 05 02 01 00 01 00. The full client trace in the source explains how it gets there.
Mistake and repair
The wrong caller clears PREVIOUS before every snapshot. A held chord becomes a new press every time. Retain the previous chord condition, rather than the previous single-key press pulse.
The BROKEN branch lives in the sequential client, not the reusable helper. wrong.asm selects it; repaired.asm restores the normal call sequence. The repaired executable matches the normal program (for Spectrum, compare loaded CODE rather than the tape filename).
Select the deliberately wrong client
org $8000
CUSTOM equ 0
BROKEN equ 1
COYOTE_STEPS equ 4
BUFFER_STEPS equ 4
TEXT_CAP equ 8
PROPERTY_COUNT equ 16
STREAM_BYTES equ 13
CRC_POLY equ 7
include "caller.inc"
include "exercise.inc"
component_start:
include "component.inc"
include "data.inc"
include "cases.inc"
include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
PREVIOUS: defb 0
HELD: defb 0
REQUIRED: defb 0
FORBIDDEN: defb 0
ACTIVE: defb 0
PRESSED: defb 0
VALID: defb 0
T0: defb 0
input_ptr: defw 0
output_ptr: defw 0
cases_left: defb 0
ready: defb 0
preview: defs 8,0
OUTPUT: defs 1300,0
end start
Cost and alternatives
The measured helper extent is 204 code bytes; public state is 7 bytes and private scratch is 1 bytes. There are no immutable helper tables. Caller data, the test cases, return-address stack, alignment and display buffers cost additional space. These are assembled sizes, not cycle measurements.
Constant work using byte masks. More than eight buttons needs more bytes and a definition of how snapshots are collected consistently.
A chord uses a held snapshot; do not AND together separate per-button press pulses. Sequentially pressed buttons can form the chord. Strict simultaneity needs an explicit timing window.
Execution evidence
Four variants run on 48K PAL ROM tape load and USR, using Asm198x 0.0.72, Emu198x 0.44.2. Each executes 64 boundary/varied/error table cases, dirty initialisation and the separate retained-state client. An independent mathematical/container model checks every public byte after every call. This is bounded example coverage, not exhaustive correctness for every possible state.
The records retain loaded immutable code, completion, register sentinels, full packets, source/tool/firmware hashes and captures. The public gate validates these retained observations and rejects altered evidence; it does not run restricted firmware. No firmware is distributed. The eight displayed bytes are the first eight public fields, zero-padded when shorter; the packet includes every field.
Where it fits
Menu navigation: A recognised chord can become one menu action; keep held state, press edges and the action request distinct.