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:
ld a,0
ld (SELECTED),a
ld (VALID),a
ld (CURRENT),a
ld (ACTIVE),a
ld (RANGE),a
ld (MARGIN),a
ld (D0),a
ld (D1),a
ld (D2),a
ld (D3),a
ld (T0),a
ld (T1),a
ld (T2),a
ld (T3),a
ld (T4),a
ld (T5),a
ret
interaction_focus_step:
; SELECTED = 255
ld a,255
ld (SELECTED),a
; VALID = 0
ld a,0
ld (VALID),a
; if CURRENT != 255:
ld a,(CURRENT)
cp 255
jp nz,interaction_focus_L1
jp interaction_focus_L2
interaction_focus_L1:
; if CURRENT >= 4:
ld a,(CURRENT)
cp 4
jp nc,interaction_focus_L4
jp interaction_focus_L5
interaction_focus_L4:
; return
ret
jp interaction_focus_L6
interaction_focus_L5:
interaction_focus_L6:
jp interaction_focus_L3
interaction_focus_L2:
interaction_focus_L3:
; if ACTIVE >= 16:
ld a,(ACTIVE)
cp 16
jp nc,interaction_focus_L7
jp interaction_focus_L8
interaction_focus_L7:
; return
ret
jp interaction_focus_L9
interaction_focus_L8:
interaction_focus_L9:
; VALID = 1
ld a,1
ld (VALID),a
; T0 = 0
ld a,0
ld (T0),a
; T1 = ACTIVE
ld a,(ACTIVE)
ld (T1),a
; T2 = 255
ld a,255
ld (T2),a
; T3 = 0
ld a,0
ld (T3),a
; T4 = 255
ld a,255
ld (T4),a
; while T0 < 4:
interaction_focus_L10:
ld a,(T0)
cp 4
jp c,interaction_focus_L11
jp interaction_focus_L12
interaction_focus_L11:
; T5 = T1 & 1
ld a,(T1)
and 1
ld (T5),a
; if T5 != 0:
ld a,(T5)
cp 0
jp nz,interaction_focus_L13
jp interaction_focus_L14
interaction_focus_L13:
; T5 = D[T0]
ld a,(T0)
ld e,a
ld d,0
ld hl,D0
add hl,de
ld a,(hl)
ld (T5),a
; if T5 <= RANGE:
ld a,(T5)
ld b,a
ld a,(RANGE)
ld c,a
ld a,b
cp c
jp c,interaction_focus_L16
jp z,interaction_focus_L16
jp interaction_focus_L17
interaction_focus_L16:
; if SELECTED == 255:
ld a,(SELECTED)
cp 255
jp z,interaction_focus_L19
jp interaction_focus_L20
interaction_focus_L19:
; SELECTED = T0
ld a,(T0)
ld (SELECTED),a
; T2 = T5
ld a,(T5)
ld (T2),a
jp interaction_focus_L21
interaction_focus_L20:
; if T5 < T2:
ld a,(T5)
ld b,a
ld a,(T2)
ld c,a
ld a,b
cp c
jp c,interaction_focus_L22
jp interaction_focus_L23
interaction_focus_L22:
; SELECTED = T0
ld a,(T0)
ld (SELECTED),a
; T2 = T5
ld a,(T5)
ld (T2),a
jp interaction_focus_L24
interaction_focus_L23:
interaction_focus_L24:
interaction_focus_L21:
; if T0 == CURRENT:
ld a,(T0)
ld b,a
ld a,(CURRENT)
ld c,a
ld a,b
cp c
jp z,interaction_focus_L25
jp interaction_focus_L26
interaction_focus_L25:
; T3 = 1
ld a,1
ld (T3),a
; T4 = T5
ld a,(T5)
ld (T4),a
jp interaction_focus_L27
interaction_focus_L26:
interaction_focus_L27:
jp interaction_focus_L18
interaction_focus_L17:
interaction_focus_L18:
jp interaction_focus_L15
interaction_focus_L14:
interaction_focus_L15:
; T1 = T1 >> 1
ld a,(T1)
srl a
ld (T1),a
; T0 = T0 + 1
ld a,(T0)
add a,1
ld (T0),a
jp interaction_focus_L10
interaction_focus_L12:
; if T3 != 0:
ld a,(T3)
cp 0
jp nz,interaction_focus_L28
jp interaction_focus_L29
interaction_focus_L28:
; T5 = T4 - T2
ld a,(T4)
ld b,a
ld a,(T2)
ld c,a
ld a,b
sub c
ld (T5),a
; if T5 <= MARGIN:
ld a,(T5)
ld b,a
ld a,(MARGIN)
ld c,a
ld a,b
cp c
jp c,interaction_focus_L31
jp z,interaction_focus_L31
jp interaction_focus_L32
interaction_focus_L31:
; SELECTED = CURRENT
ld a,(CURRENT)
ld (SELECTED),a
jp interaction_focus_L33
interaction_focus_L32:
interaction_focus_L33:
jp interaction_focus_L30
interaction_focus_L29:
interaction_focus_L30:
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. 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 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:
SELECTED: defb 0
VALID: defb 0
CURRENT: defb 0
ACTIVE: defb 0
RANGE: defb 0
MARGIN: defb 0
D0: defb 0
D1: defb 0
D2: defb 0
D3: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
T3: defb 0
T4: defb 0
T5: 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
; CURRENT = 3
ld a,3
ld (CURRENT),a
; ACTIVE = 15
ld a,15
ld (ACTIVE),a
; RANGE = 10
ld a,10
ld (RANGE),a
; MARGIN = 0
ld a,0
ld (MARGIN),a
; D0 = 10
ld a,10
ld (D0),a
; D1 = 10
ld a,10
ld (D1),a
; D2 = 10
ld a,10
ld (D2),a
; D3 = 10
ld a,10
ld (D3),a
if BROKEN
; CURRENT = 255
ld a,255
ld (CURRENT),a
endif
call checked_step
call emit_state
ret
else
; CURRENT = 1
ld a,1
ld (CURRENT),a
; ACTIVE = 3
ld a,3
ld (ACTIVE),a
; RANGE = 20
ld a,20
ld (RANGE),a
; MARGIN = 2
ld a,2
ld (MARGIN),a
; D0 = 9
ld a,9
ld (D0),a
; D1 = 10
ld a,10
ld (D1),a
if BROKEN
; CURRENT = 255
ld a,255
ld (CURRENT),a
endif
call checked_step
call emit_state
; CURRENT = 1
ld a,1
ld (CURRENT),a
; D0 = 8
ld a,8
ld (D0),a
if BROKEN
; CURRENT = 255
ld a,255
ld (CURRENT),a
endif
call checked_step
call emit_state
; CURRENT = 1
ld a,1
ld (CURRENT),a
; D0 = 8
ld a,8
ld (D0),a
if BROKEN
; CURRENT = 255
ld a,255
ld (CURRENT),a
endif
call checked_step
call emit_state
endif
retCustomise 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
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:
SELECTED: defb 0
VALID: defb 0
CURRENT: defb 0
ACTIVE: defb 0
RANGE: defb 0
MARGIN: defb 0
D0: defb 0
D1: defb 0
D2: defb 0
D3: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
T3: defb 0
T4: defb 0
T5: 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 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
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:
SELECTED: defb 0
VALID: defb 0
CURRENT: defb 0
ACTIVE: defb 0
RANGE: defb 0
MARGIN: defb 0
D0: defb 0
D1: defb 0
D2: defb 0
D3: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
T3: defb 0
T4: defb 0
T5: 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 367 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 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
Nearest target: Reuse the existing Manhattan-distance selector when you need immediate nearest-target behaviour without focus retention.