A noisy or bouncing input should settle before it changes the game’s logical state.

Use it in a project
RAW is a sampled byte. THRESHOLD 1–255 requires that many consecutive identical samples before replacing STABLE. CANDIDATE and RUN are retained private history. CHANGED pulses only when STABLE changes. THRESHOLD=0 disables acceptance and clears RUN. RUN saturates at 255.
Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: STABLE, CHANGED, CANDIDATE, RUN, RAW, THRESHOLD. They occupy 6 contiguous bytes. Also reserve the private workspace: none (0 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.
Call input_debounce_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_debounce_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 | STABLE, CANDIDATE and RUN |
| Supply before service | RAW and THRESHOLD |
| Read after service | STABLE and CHANGED |
A new candidate begins a run of one. Repeated samples grow that run until the threshold is reached; only a difference from the accepted stable value emits CHANGED.
Reusable initialisation and service
; SPDX-License-Identifier: MIT
; Debounce a Sampled Input Byte. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
input_debounce_init:
lda #0
sta STABLE
sta CHANGED
sta CANDIDATE
sta RUN
sta RAW
sta THRESHOLD
rts
input_debounce_step:
; CHANGED = 0
lda #0
sta CHANGED
; if THRESHOLD == 0:
lda THRESHOLD
cmp #0
beq input_debounce_L4
input_debounce_L5:
jmp input_debounce_L2
input_debounce_L4:
jmp input_debounce_L1
input_debounce_L1:
; RUN = 0
lda #0
sta RUN
; return
rts
jmp input_debounce_L3
input_debounce_L2:
input_debounce_L3:
; if RAW != CANDIDATE:
lda RAW
cmp CANDIDATE
bne input_debounce_L9
input_debounce_L10:
jmp input_debounce_L7
input_debounce_L9:
jmp input_debounce_L6
input_debounce_L6:
; CANDIDATE = RAW
lda RAW
sta CANDIDATE
; RUN = 1
lda #1
sta RUN
jmp input_debounce_L8
input_debounce_L7:
; if RUN < 255:
lda RUN
cmp #255
bcc input_debounce_L14
input_debounce_L15:
jmp input_debounce_L12
input_debounce_L14:
jmp input_debounce_L11
input_debounce_L11:
; RUN = RUN + 1
lda RUN
clc
adc #1
sta RUN
jmp input_debounce_L13
input_debounce_L12:
input_debounce_L13:
input_debounce_L8:
; if RUN >= THRESHOLD:
lda RUN
cmp THRESHOLD
bcs input_debounce_L19
input_debounce_L20:
jmp input_debounce_L17
input_debounce_L19:
jmp input_debounce_L16
input_debounce_L16:
; if STABLE != CANDIDATE:
lda STABLE
cmp CANDIDATE
bne input_debounce_L24
input_debounce_L25:
jmp input_debounce_L22
input_debounce_L24:
jmp input_debounce_L21
input_debounce_L21:
; STABLE = CANDIDATE
lda CANDIDATE
sta STABLE
; CHANGED = 1
lda #1
sta CHANGED
jmp input_debounce_L23
input_debounce_L22:
input_debounce_L23:
jmp input_debounce_L18
input_debounce_L17:
input_debounce_L18:
rts
component_end:Machine contract
A/X/status may change; Y and decimal mode are preserved. Use CLD for binary arithmetic. 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. The samples use fixed symbol bindings. Scope or rename every label and binding, including the component_end measurement marker, and allocate a separate block for each independent instance. Different helpers can also use common field names, so combining them needs separate namespaces or explicitly renamed bindings. Give each block one mutation owner.
The diagnostic PRG owns screen, font and colour RAM and disables interrupts; it does not return to BASIC. The reusable helper does none of those things.
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
STABLE=$80
CHANGED=$81
CANDIDATE=$82
RUN=$83
RAW=$84
THRESHOLD=$85
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$c000
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
!source "caller.inc"
!source "exercise.inc"
component_start:
!source "component.inc"
!source "data.inc"
!source "cases.inc"
hex_font:
!byte 0,0,0,0,0,0,0,0,0,56,40,40,40,56,0,0,0,16,48,16,16,56,0,0,0,56,8,56,32,56,0,0,0,56,8,56,8,56,0,0,0,40,40,56,8,8,0,0,0,56,32,56,8,56,0,0,0,56,32,56,40,56,0,0,0,56,8,8,8,8,0,0,0,56,40,56,40,56,0,0,0,56,40,56,8,56,0,0,0,16,40,56,40,40,0,0,0,48,40,48,40,48,0,0,0,56,32,32,32,56,0,0,0,48,40,40,40,48,0,0,0,56,32,48,32,56,0,0,0,56,32,48,32,32,0,0
code_end:A client retaining state between calls
preview_rules:
!if CUSTOM {
; RAW = 128
lda #128
sta RAW
; THRESHOLD = 1
lda #1
sta THRESHOLD
!if BROKEN {
; RUN = 0
lda #0
sta RUN
}
jsr checked_step
jsr emit_state
!if BROKEN {
; RUN = 0
lda #0
sta RUN
}
jsr checked_step
jsr emit_state
; RAW = 0
lda #0
sta RAW
!if BROKEN {
; RUN = 0
lda #0
sta RUN
}
jsr checked_step
jsr emit_state
rts
} else {
; RAW = 1
lda #1
sta RAW
; THRESHOLD = 3
lda #3
sta THRESHOLD
!if BROKEN {
; RUN = 0
lda #0
sta RUN
}
jsr checked_step
jsr emit_state
!if BROKEN {
; RUN = 0
lda #0
sta RUN
}
jsr checked_step
jsr emit_state
; RAW = 0
lda #0
sta RAW
!if BROKEN {
; RUN = 0
lda #0
sta RUN
}
jsr checked_step
jsr emit_state
; RAW = 1
lda #1
sta RAW
!if BROKEN {
; RUN = 0
lda #0
sta RUN
}
jsr checked_step
jsr emit_state
!if BROKEN {
; RUN = 0
lda #0
sta RUN
}
jsr checked_step
jsr emit_state
!if BROKEN {
; RUN = 0
lda #0
sta RUN
}
jsr checked_step
jsr emit_state
}
rtsCustomise it
Use immediate acceptance at threshold one. 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
STABLE=$80
CHANGED=$81
CANDIDATE=$82
RUN=$83
RAW=$84
THRESHOLD=$85
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$c000
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
!source "caller.inc"
!source "exercise.inc"
component_start:
!source "component.inc"
!source "data.inc"
!source "cases.inc"
hex_font:
!byte 0,0,0,0,0,0,0,0,0,56,40,40,40,56,0,0,0,16,48,16,16,56,0,0,0,56,8,56,32,56,0,0,0,56,8,56,8,56,0,0,0,40,40,56,8,8,0,0,0,56,32,56,8,56,0,0,0,56,32,56,40,56,0,0,0,56,8,8,8,8,0,0,0,56,40,56,40,56,0,0,0,56,40,56,8,56,0,0,0,16,40,56,40,40,0,0,0,48,40,48,40,48,0,0,0,56,32,32,32,56,0,0,0,48,40,40,40,48,0,0,0,56,32,48,32,56,0,0,0,56,32,48,32,32,0,0
code_end:The custom diagnostic row is 00 01 00 01 00 01 00 00. The full client trace in the source explains how it gets there.
Mistake and repair
The wrong caller clears the consecutive-sample count before every service. A threshold above one is never reached. Retain CANDIDATE and RUN between samples.
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
STABLE=$80
CHANGED=$81
CANDIDATE=$82
RUN=$83
RAW=$84
THRESHOLD=$85
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$c000
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
!source "caller.inc"
!source "exercise.inc"
component_start:
!source "component.inc"
!source "data.inc"
!source "cases.inc"
hex_font:
!byte 0,0,0,0,0,0,0,0,0,56,40,40,40,56,0,0,0,16,48,16,16,56,0,0,0,56,8,56,32,56,0,0,0,56,8,56,8,56,0,0,0,40,40,56,8,8,0,0,0,56,32,56,8,56,0,0,0,56,32,56,40,56,0,0,0,56,8,8,8,8,0,0,0,56,40,56,40,56,0,0,0,56,40,56,8,56,0,0,0,16,40,56,40,40,0,0,0,48,40,48,40,48,0,0,0,56,32,32,32,56,0,0,0,48,40,40,40,48,0,0,0,56,32,48,32,56,0,0,0,56,32,48,32,32,0,0
code_end:
Cost and alternatives
The measured helper extent is 123 code bytes; public state is 6 bytes and private scratch is 0 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 per sample. Latency is threshold samples, so the caller’s sampling frequency is part of the interface.
The entire byte must settle together. Independently bouncing buttons need separate instances or per-bit counters. The routine is useful for raw noisy inputs, not a substitute for normal press-edge detection.
Execution evidence
Four variants run on PAL stock-ROM PRG, 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
Input chords: Debounce raw signals before interpreting stable logical combinations.