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:
lda #0
sta PREVIOUS
sta HELD
sta REQUIRED
sta FORBIDDEN
sta ACTIVE
sta PRESSED
sta VALID
sta T0
rts
input_chord_step:
; ACTIVE = 0
lda #0
sta ACTIVE
; PRESSED = 0
lda #0
sta PRESSED
; VALID = 0
lda #0
sta VALID
; if REQUIRED == 0:
lda REQUIRED
cmp #0
beq input_chord_L4
input_chord_L5:
jmp input_chord_L2
input_chord_L4:
jmp input_chord_L1
input_chord_L1:
; PREVIOUS = 0
lda #0
sta PREVIOUS
; return
rts
jmp input_chord_L3
input_chord_L2:
input_chord_L3:
; T0 = REQUIRED & FORBIDDEN
lda REQUIRED
and FORBIDDEN
sta T0
; if T0 != 0:
lda T0
cmp #0
bne input_chord_L9
input_chord_L10:
jmp input_chord_L7
input_chord_L9:
jmp input_chord_L6
input_chord_L6:
; PREVIOUS = 0
lda #0
sta PREVIOUS
; return
rts
jmp input_chord_L8
input_chord_L7:
input_chord_L8:
; VALID = 1
lda #1
sta VALID
; T0 = HELD & REQUIRED
lda HELD
and REQUIRED
sta T0
; if T0 == REQUIRED:
lda T0
cmp REQUIRED
beq input_chord_L14
input_chord_L15:
jmp input_chord_L12
input_chord_L14:
jmp input_chord_L11
input_chord_L11:
; T0 = HELD & FORBIDDEN
lda HELD
and FORBIDDEN
sta T0
; if T0 == 0:
lda T0
cmp #0
beq input_chord_L19
input_chord_L20:
jmp input_chord_L17
input_chord_L19:
jmp input_chord_L16
input_chord_L16:
; ACTIVE = 1
lda #1
sta ACTIVE
jmp input_chord_L18
input_chord_L17:
input_chord_L18:
jmp input_chord_L13
input_chord_L12:
input_chord_L13:
; if ACTIVE != 0:
lda ACTIVE
cmp #0
bne input_chord_L24
input_chord_L25:
jmp input_chord_L22
input_chord_L24:
jmp input_chord_L21
input_chord_L21:
; if PREVIOUS == 0:
lda PREVIOUS
cmp #0
beq input_chord_L29
input_chord_L30:
jmp input_chord_L27
input_chord_L29:
jmp input_chord_L26
input_chord_L26:
; PRESSED = 1
lda #1
sta PRESSED
jmp input_chord_L28
input_chord_L27:
input_chord_L28:
jmp input_chord_L23
input_chord_L22:
input_chord_L23:
; PREVIOUS = ACTIVE
lda ACTIVE
sta PREVIOUS
rts
component_end:Machine contract
A/X/status may change; Y is preserved. The 2A03 has no decimal arithmetic mode. A direct JSR 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 NROM caller owns PPU setup and background display, with NMI and DMC disabled. Table and packet sizes are bounded to fit the cartridge caller and internal RAM; the helper owns no PPU registers.
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
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
PREVIOUS=$80
HELD=$81
REQUIRED=$82
FORBIDDEN=$83
ACTIVE=$84
PRESSED=$85
VALID=$86
T0=$87
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$0200
preview=OUTPUT+1400
ready=OUTPUT+1408
CUSTOM=0
BROKEN=0
COYOTE_STEPS=4
BUFFER_STEPS=4
TEXT_CAP=8
PROPERTY_COUNT=16
STREAM_BYTES=13
CRC_POLY=7
.include "caller.inc"
.include "exercise.inc"
component_start:
.include "component.inc"
.include "data.inc"
.include "cases.inc"
code_end:
.segment "VECTORS"
.word nmi,reset,irq
.segment "CHARS"
.include "../../input/controller-reading/edges-font.inc"
.res 8192-272,0A client retaining state between calls
preview_rules:
.if CUSTOM
; REQUIRED = 5
lda #5
sta REQUIRED
; FORBIDDEN = 2
lda #2
sta FORBIDDEN
; HELD = 5
lda #5
sta HELD
.if BROKEN
; PREVIOUS = 0
lda #0
sta PREVIOUS
.endif
jsr checked_step
jsr emit_state
.if BROKEN
; PREVIOUS = 0
lda #0
sta PREVIOUS
.endif
jsr checked_step
jsr emit_state
.if BROKEN
; PREVIOUS = 0
lda #0
sta PREVIOUS
.endif
jsr checked_step
jsr emit_state
rts
.else
; REQUIRED = 3
lda #3
sta REQUIRED
; FORBIDDEN = 4
lda #4
sta FORBIDDEN
; HELD = 3
lda #3
sta HELD
.if BROKEN
; PREVIOUS = 0
lda #0
sta PREVIOUS
.endif
jsr checked_step
jsr emit_state
.if BROKEN
; PREVIOUS = 0
lda #0
sta PREVIOUS
.endif
jsr checked_step
jsr emit_state
; HELD = 7
lda #7
sta HELD
.if BROKEN
; PREVIOUS = 0
lda #0
sta PREVIOUS
.endif
jsr checked_step
jsr emit_state
; HELD = 3
lda #3
sta HELD
.if BROKEN
; PREVIOUS = 0
lda #0
sta PREVIOUS
.endif
jsr checked_step
jsr emit_state
.if BROKEN
; PREVIOUS = 0
lda #0
sta PREVIOUS
.endif
jsr checked_step
jsr emit_state
.endif
rtsCustomise 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
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
PREVIOUS=$80
HELD=$81
REQUIRED=$82
FORBIDDEN=$83
ACTIVE=$84
PRESSED=$85
VALID=$86
T0=$87
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$0200
preview=OUTPUT+1400
ready=OUTPUT+1408
CUSTOM=1
BROKEN=0
COYOTE_STEPS=2
BUFFER_STEPS=2
TEXT_CAP=4
PROPERTY_COUNT=16
STREAM_BYTES=13
CRC_POLY=49
.include "caller.inc"
.include "exercise.inc"
component_start:
.include "component.inc"
.include "data.inc"
.include "cases.inc"
code_end:
.segment "VECTORS"
.word nmi,reset,irq
.segment "CHARS"
.include "../../input/controller-reading/edges-font.inc"
.res 8192-272,0The 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
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
PREVIOUS=$80
HELD=$81
REQUIRED=$82
FORBIDDEN=$83
ACTIVE=$84
PRESSED=$85
VALID=$86
T0=$87
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$0200
preview=OUTPUT+1400
ready=OUTPUT+1408
CUSTOM=0
BROKEN=1
COYOTE_STEPS=4
BUFFER_STEPS=4
TEXT_CAP=8
PROPERTY_COUNT=16
STREAM_BYTES=13
CRC_POLY=7
.include "caller.inc"
.include "exercise.inc"
component_start:
.include "component.inc"
.include "data.inc"
.include "cases.inc"
code_end:
.segment "VECTORS"
.word nmi,reset,irq
.segment "CHARS"
.include "../../input/controller-reading/edges-font.inc"
.res 8192-272,0
Cost and alternatives
The measured helper extent is 166 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 NTSC NROM, background only, DMC/NMI disabled, 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.