A prompt should not flicker between two nearby objects whenever their distances differ by one pixel.

Use it in a project
ACTIVE uses its low four bits. D0..D3 are caller-computed distances on one consistent byte scale. Choose the nearest active candidate at distance ≤ RANGE; lowest slot wins a fresh tie. CURRENT is 0–3 or 255 for none. Retain a current eligible candidate when its distance is no more than MARGIN worse than the nearest. SELECTED is 255 when none qualifies. Invalid CURRENT or high ACTIVE bits clears VALID and returns SELECTED=255. Feed SELECTED back as CURRENT next time.
Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: SELECTED, VALID, CURRENT, ACTIVE, RANGE, MARGIN, D0, D1, D2, D3. They occupy 10 contiguous bytes. Also reserve the private workspace: T0, T1, T2, T3, T4, T5 (6 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.
Call interaction_focus_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 interaction_focus_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 | CURRENT supplied from the previous SELECTED |
| Supply before service | ACTIVE, distances, RANGE and MARGIN |
| Read after service | SELECTED and VALID |
First find the strict nearest candidate. Compare the old candidate’s excess distance against the margin afterwards; subtracting the nearest avoids overflow from adding margin to a byte distance.
Reusable initialisation and service
; SPDX-License-Identifier: MIT
; Keep a Stable Interaction Focus. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
interaction_focus_init:
lda #0
sta SELECTED
sta VALID
sta CURRENT
sta ACTIVE
sta RANGE
sta MARGIN
sta D0
sta D1
sta D2
sta D3
sta T0
sta T1
sta T2
sta T3
sta T4
sta T5
rts
interaction_focus_step:
; SELECTED = 255
lda #255
sta SELECTED
; VALID = 0
lda #0
sta VALID
; if CURRENT != 255:
lda CURRENT
cmp #255
bne interaction_focus_L4
interaction_focus_L5:
jmp interaction_focus_L2
interaction_focus_L4:
jmp interaction_focus_L1
interaction_focus_L1:
; if CURRENT >= 4:
lda CURRENT
cmp #4
bcs interaction_focus_L9
interaction_focus_L10:
jmp interaction_focus_L7
interaction_focus_L9:
jmp interaction_focus_L6
interaction_focus_L6:
; return
rts
jmp interaction_focus_L8
interaction_focus_L7:
interaction_focus_L8:
jmp interaction_focus_L3
interaction_focus_L2:
interaction_focus_L3:
; if ACTIVE >= 16:
lda ACTIVE
cmp #16
bcs interaction_focus_L14
interaction_focus_L15:
jmp interaction_focus_L12
interaction_focus_L14:
jmp interaction_focus_L11
interaction_focus_L11:
; return
rts
jmp interaction_focus_L13
interaction_focus_L12:
interaction_focus_L13:
; VALID = 1
lda #1
sta VALID
; T0 = 0
lda #0
sta T0
; T1 = ACTIVE
lda ACTIVE
sta T1
; T2 = 255
lda #255
sta T2
; T3 = 0
lda #0
sta T3
; T4 = 255
lda #255
sta T4
; while T0 < 4:
interaction_focus_L16:
lda T0
cmp #4
bcc interaction_focus_L19
interaction_focus_L20:
jmp interaction_focus_L18
interaction_focus_L19:
jmp interaction_focus_L17
interaction_focus_L17:
; T5 = T1 & 1
lda T1
and #1
sta T5
; if T5 != 0:
lda T5
cmp #0
bne interaction_focus_L24
interaction_focus_L25:
jmp interaction_focus_L22
interaction_focus_L24:
jmp interaction_focus_L21
interaction_focus_L21:
; T5 = D[T0]
ldx T0
lda D0,x
sta T5
; if T5 <= RANGE:
lda T5
cmp RANGE
bcc interaction_focus_L29
beq interaction_focus_L29
interaction_focus_L30:
jmp interaction_focus_L27
interaction_focus_L29:
jmp interaction_focus_L26
interaction_focus_L26:
; if SELECTED == 255:
lda SELECTED
cmp #255
beq interaction_focus_L34
interaction_focus_L35:
jmp interaction_focus_L32
interaction_focus_L34:
jmp interaction_focus_L31
interaction_focus_L31:
; SELECTED = T0
lda T0
sta SELECTED
; T2 = T5
lda T5
sta T2
jmp interaction_focus_L33
interaction_focus_L32:
; if T5 < T2:
lda T5
cmp T2
bcc interaction_focus_L39
interaction_focus_L40:
jmp interaction_focus_L37
interaction_focus_L39:
jmp interaction_focus_L36
interaction_focus_L36:
; SELECTED = T0
lda T0
sta SELECTED
; T2 = T5
lda T5
sta T2
jmp interaction_focus_L38
interaction_focus_L37:
interaction_focus_L38:
interaction_focus_L33:
; if T0 == CURRENT:
lda T0
cmp CURRENT
beq interaction_focus_L44
interaction_focus_L45:
jmp interaction_focus_L42
interaction_focus_L44:
jmp interaction_focus_L41
interaction_focus_L41:
; T3 = 1
lda #1
sta T3
; T4 = T5
lda T5
sta T4
jmp interaction_focus_L43
interaction_focus_L42:
interaction_focus_L43:
jmp interaction_focus_L28
interaction_focus_L27:
interaction_focus_L28:
jmp interaction_focus_L23
interaction_focus_L22:
interaction_focus_L23:
; T1 = T1 >> 1
lda T1
lsr
sta T1
; T0 = T0 + 1
lda T0
clc
adc #1
sta T0
jmp interaction_focus_L16
interaction_focus_L18:
; if T3 != 0:
lda T3
cmp #0
bne interaction_focus_L49
interaction_focus_L50:
jmp interaction_focus_L47
interaction_focus_L49:
jmp interaction_focus_L46
interaction_focus_L46:
; T5 = T4 - T2
lda T4
sec
sbc T2
sta T5
; if T5 <= MARGIN:
lda T5
cmp MARGIN
bcc interaction_focus_L54
beq interaction_focus_L54
interaction_focus_L55:
jmp interaction_focus_L52
interaction_focus_L54:
jmp interaction_focus_L51
interaction_focus_L51:
; SELECTED = CURRENT
lda CURRENT
sta SELECTED
jmp interaction_focus_L53
interaction_focus_L52:
interaction_focus_L53:
jmp interaction_focus_L48
interaction_focus_L47:
interaction_focus_L48:
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. 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 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
SELECTED=$80
VALID=$81
CURRENT=$82
ACTIVE=$83
RANGE=$84
MARGIN=$85
D0=$86
D1=$87
D2=$88
D3=$89
T0=$8a
T1=$8b
T2=$8c
T3=$8d
T4=$8e
T5=$8f
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
; CURRENT = 3
lda #3
sta CURRENT
; ACTIVE = 15
lda #15
sta ACTIVE
; RANGE = 10
lda #10
sta RANGE
; MARGIN = 0
lda #0
sta MARGIN
; D0 = 10
lda #10
sta D0
; D1 = 10
lda #10
sta D1
; D2 = 10
lda #10
sta D2
; D3 = 10
lda #10
sta D3
.if BROKEN
; CURRENT = 255
lda #255
sta CURRENT
.endif
jsr checked_step
jsr emit_state
rts
.else
; CURRENT = 1
lda #1
sta CURRENT
; ACTIVE = 3
lda #3
sta ACTIVE
; RANGE = 20
lda #20
sta RANGE
; MARGIN = 2
lda #2
sta MARGIN
; D0 = 9
lda #9
sta D0
; D1 = 10
lda #10
sta D1
.if BROKEN
; CURRENT = 255
lda #255
sta CURRENT
.endif
jsr checked_step
jsr emit_state
; CURRENT = 1
lda #1
sta CURRENT
; D0 = 8
lda #8
sta D0
.if BROKEN
; CURRENT = 255
lda #255
sta CURRENT
.endif
jsr checked_step
jsr emit_state
; CURRENT = 1
lda #1
sta CURRENT
; D0 = 8
lda #8
sta D0
.if BROKEN
; CURRENT = 255
lda #255
sta CURRENT
.endif
jsr checked_step
jsr emit_state
.endif
rtsCustomise it
Use zero margin so only equal-distance ties retain the current target. 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
SELECTED=$80
VALID=$81
CURRENT=$82
ACTIVE=$83
RANGE=$84
MARGIN=$85
D0=$86
D1=$87
D2=$88
D3=$89
T0=$8a
T1=$8b
T2=$8c
T3=$8d
T4=$8e
T5=$8f
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 03 01 03 0F 0A 00 0A 0A. The full client trace in the source explains how it gets there.
Mistake and repair
The wrong client forgets the retained focus. A small distance advantage immediately switches the prompt despite the requested hysteresis. Carry SELECTED into CURRENT between updates.
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
SELECTED=$80
VALID=$81
CURRENT=$82
ACTIVE=$83
RANGE=$84
MARGIN=$85
D0=$86
D1=$87
D2=$88
D3=$89
T0=$8a
T1=$8b
T2=$8c
T3=$8d
T4=$8e
T5=$8f
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 298 code bytes; public state is 10 bytes and private scratch is 6 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.
Exactly four candidates are examined. Retention needs no extra per-candidate table; a larger set needs wider indexing and a measured search budget.
The helper consumes distances, not positions or line-of-sight tests. ACTIVE must already exclude unavailable objects. Hysteresis trades responsiveness for stable prompts; a large margin can keep a substantially farther target. Separate object identities need generation validation when pool slots are reused.
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
Nearest target: Reuse the existing Manhattan-distance selector when you need immediate nearest-target behaviour without focus retention.