A menu should move once when you press Down, pause briefly, then scroll while you hold it. Reading the held button every update is too fast; reading only new presses never repeats. This helper adds the delay and repeat cadence between those two choices.

Include, initialise and call
Bind HELD, WAS_HELD, TIMER, PULSE as 4 contiguous private bytes in that order. Include the helper once. Call input_repeat_init at the deliberate reset boundary: it clears all named bytes, including inputs. Set your starting values afterwards. Call input_repeat_step when this service is needed.
Initialise once when the menu gains control. Feed a held level every update, then consume PULSE immediately. Use a separate block for each independently repeating direction. Resolve opposite directions before service, according to your menu policy. The keyboard reader provides the machine-facing input path. Select the particular held control and convert any active-low raw bit before supplying HELD; a newly pressed pulse alone cannot sustain repeat.
Supply HELD as any nonzero value for a held control, zero for release. Step replaces PULSE, normalises WAS_HELD, and retains TIMER. The first held call pulses immediately and loads REPEAT_DELAY. Later held calls decrement a positive timer; reaching zero pulses and reloads REPEAT_RATE. Release clears the history and timer. Both configured delays must be 1–255 simulation updates.
Reusable initialisation and service
; SPDX-License-Identifier: MIT
; Menu Key Repeat. Bind these contiguous private bytes: HELD, WAS_HELD, TIMER, PULSE.
; init clears the whole block; step uses current inputs. No I/O, waits or IRQ owner.
; AF/BC/DE/HL may change; IX/IY survive.
input_repeat_init:
xor a
ld (HELD),a
ld (WAS_HELD),a
ld (TIMER),a
ld (PULSE),a
ret
input_repeat_step:
xor a
ld (PULSE),a
ld a,(HELD)
or a
jr z,repeat_release
ld a,(WAS_HELD)
or a
jr z,repeat_first
ld a,(TIMER)
or a
jr z,repeat_again
dec a
ld (TIMER),a
jr nz,repeat_held
repeat_again:
ld a,REPEAT_RATE
jr repeat_pulse
repeat_first:
ld a,REPEAT_DELAY
repeat_pulse:
ld (TIMER),a
ld a,1
ld (PULSE),a
repeat_held:
ld a,1
ld (WAS_HELD),a
ret
repeat_release:
ld (TIMER),a
ld (WAS_HELD),a
ret
component_end:State and ownership
HELD is an input. WAS_HELD and TIMER are persistent private history. PULSE is a one-call output. The helper reads no keyboard or controller and assumes a regular service cadence.
AF/BC/DE/HL may change; IX/IY survive. A direct CALL uses two return-address stack bytes.
This implementation has fixed symbol bindings and one mutation owner. Independent users need distinct state blocks and scoped/renamed bindings; including another copy with the same global labels is not sufficient. Sequential queries may reuse a scratch block only after the previous result has been consumed. Do not let an interrupt and the main loop mutate the same block without a separately designed ownership policy.
The standalone tape program starts at $8000, disables interrupts and owns bitmap/attribute RAM. It does not return to BASIC. The helper itself never disables interrupts or touches the display.
A small caller you can adapt
The standalone program includes the helper and a separate client. Its read-only table exercises 96 valid inputs, then this client initialises once and retains state between calls. Replace the supplied input changes with your own game values; the display and packet machinery are diagnostics rather than dependencies of the component.
Buildable caller, bindings and include order
org $8000
CUSTOM equ 0
BROKEN equ 0
SCORE_CAP equ 65535
QUEUE_CAP equ 4
REPEAT_DELAY equ 4
REPEAT_RATE equ 2
include "caller.inc"
include "exercise.inc"
component_start:
include "component.inc"
include "cases.inc"
include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
HELD: defb 0
WAS_HELD: defb 0
TIMER: defb 0
PULSE: 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 separate client retaining state between calls
preview_rules:
if CUSTOM
ld a,1
ld (HELD),a
if BROKEN
ld a,0
ld (WAS_HELD),a
ld a,0
ld (TIMER),a
call checked_step
else
call checked_step
endif
call emit_state
ld a,1
ld (HELD),a
if BROKEN
ld a,0
ld (WAS_HELD),a
ld a,0
ld (TIMER),a
call checked_step
else
call checked_step
endif
call emit_state
ld a,1
ld (HELD),a
if BROKEN
ld a,0
ld (WAS_HELD),a
ld a,0
ld (TIMER),a
call checked_step
else
call checked_step
endif
call emit_state
ld a,1
ld (HELD),a
if BROKEN
ld a,0
ld (WAS_HELD),a
ld a,0
ld (TIMER),a
call checked_step
else
call checked_step
endif
call emit_state
ld a,1
ld (HELD),a
if BROKEN
ld a,0
ld (WAS_HELD),a
ld a,0
ld (TIMER),a
call checked_step
else
call checked_step
endif
call emit_state
ld a,1
ld (HELD),a
if BROKEN
ld a,0
ld (WAS_HELD),a
ld a,0
ld (TIMER),a
call checked_step
else
call checked_step
endif
call emit_state
ld a,1
ld (HELD),a
if BROKEN
ld a,0
ld (WAS_HELD),a
ld a,0
ld (TIMER),a
call checked_step
else
call checked_step
endif
call emit_state
ld a,1
ld (HELD),a
if BROKEN
ld a,0
ld (WAS_HELD),a
ld a,0
ld (TIMER),a
call checked_step
else
call checked_step
endif
call emit_state
else
ld a,1
ld (HELD),a
if BROKEN
ld a,0
ld (WAS_HELD),a
ld a,0
ld (TIMER),a
call checked_step
else
call checked_step
endif
call emit_state
ld a,1
ld (HELD),a
if BROKEN
ld a,0
ld (WAS_HELD),a
ld a,0
ld (TIMER),a
call checked_step
else
call checked_step
endif
call emit_state
ld a,1
ld (HELD),a
if BROKEN
ld a,0
ld (WAS_HELD),a
ld a,0
ld (TIMER),a
call checked_step
else
call checked_step
endif
call emit_state
ld a,1
ld (HELD),a
if BROKEN
ld a,0
ld (WAS_HELD),a
ld a,0
ld (TIMER),a
call checked_step
else
call checked_step
endif
call emit_state
ld a,1
ld (HELD),a
if BROKEN
ld a,0
ld (WAS_HELD),a
ld a,0
ld (TIMER),a
call checked_step
else
call checked_step
endif
call emit_state
ld a,1
ld (HELD),a
if BROKEN
ld a,0
ld (WAS_HELD),a
ld a,0
ld (TIMER),a
call checked_step
else
call checked_step
endif
call emit_state
ld a,1
ld (HELD),a
if BROKEN
ld a,0
ld (WAS_HELD),a
ld a,0
ld (TIMER),a
call checked_step
else
call checked_step
endif
call emit_state
ld a,1
ld (HELD),a
if BROKEN
ld a,0
ld (WAS_HELD),a
ld a,0
ld (TIMER),a
call checked_step
else
call checked_step
endif
call emit_state
endif
retThe eight display fields follow HELD, WAS_HELD, TIMER, PULSE; unused display fields are zero. Each field is a hexadecimal byte. The working row is 01 01 01 00 00 00 00 00. The output is a completed diagnostic state, not a recording of movement, input or sound hardware.
Make it your own
The custom build changes the first delay from four to six updates and the repeat interval from two to three. The immediate press still pulses at index zero. The eight held calls now pulse at indices 0 and 6 rather than 0, 4 and 6.
Maintained custom configuration or caller
org $8000
CUSTOM equ 1
BROKEN equ 0
SCORE_CAP equ 999
QUEUE_CAP equ 2
REPEAT_DELAY equ 6
REPEAT_RATE equ 3
include "caller.inc"
include "exercise.inc"
component_start:
include "component.inc"
include "cases.inc"
include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
HELD: defb 0
WAS_HELD: defb 0
TIMER: defb 0
PULSE: 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 observed custom row is 01 01 02 00 00 00 00 00. The retained records qualify these named configurations; a new input domain or larger layout needs its own bounds and fresh execution checks.
Common mistake and repair
The wrong client clears WAS_HELD and TIMER before every held sample. Every update looks like a fresh press, so all eight updates pulse. Retain repeat history between calls; reset it only on intentional focus/session changes.
Deliberately wrong client selection
org $8000
CUSTOM equ 0
BROKEN equ 1
SCORE_CAP equ 65535
QUEUE_CAP equ 4
REPEAT_DELAY equ 4
REPEAT_RATE equ 2
include "caller.inc"
include "exercise.inc"
component_start:
include "component.inc"
include "cases.inc"
include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
HELD: defb 0
WAS_HELD: defb 0
TIMER: defb 0
PULSE: 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
The wrong branch is in the displayed client above, selected by BROKEN. repaired.asm selects the original client again. Its executable matches demo exactly; Spectrum tape filenames can differ while loaded CODE is identical. The bad caller is a labelled experiment, not an alternative recommended implementation.
Cost and verification
The measured helper occupies 69 code bytes plus 4 named state bytes in this layout. Return addresses, alignment, tables and display diagnostics are additional. The sizes cover init and step together, not the whole standalone program.
One constant-size state machine runs per control per update. No busy wait or input scan occurs here. Very short intervals feel fast only if the menu is actually serviced at the intended rate.
A faster held-scroll stage can shorten the repeat interval after a threshold, but needs another counter and a clear acceleration policy. A real-time repeat service can use elapsed ticks instead; this version deliberately counts service calls and freezes when they stop.
Four native runs use Spectrum 48K with stock ROM tape loading and USR entry; Asm198x 0.0.72 and Emu198x 0.44.2. Every run checks 96 table results, dirty initialisation and every state in a separate sequential trace against an independent integer model. This is boundary/varied-input qualification, not exhaustive coverage of every byte combination. Source/tool/firmware hashes, native startup and completion, preserved-register sentinels, immutable loaded code, actual helper extent, capture bytes and pixels, and exact repair are checked. Deliberately changed observations must fail. Firmware is hashed, never distributed.