Skip to content

Keep a Stable Interaction Focus on Amiga

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

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

Amiga shows the working diagnostic row 01 01 01 03 14 02 08 0A.
A completed native caller displays 01 01 01 03 14 02 08 0A. The public field order below identifies these bytes; the capture is a state diagnostic, not a recording of a finished game.

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, DIST0, DIST1, DIST2, DIST3. 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

component.inc
; 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:
    move.b #0,d0
    move.b d0,SELECTED
    move.b d0,VALID
    move.b d0,CURRENT
    move.b d0,ACTIVE
    move.b d0,RANGE
    move.b d0,MARGIN
    move.b d0,DIST0
    move.b d0,DIST1
    move.b d0,DIST2
    move.b d0,DIST3
    move.b d0,T0
    move.b d0,T1
    move.b d0,T2
    move.b d0,T3
    move.b d0,T4
    move.b d0,T5
    rts
interaction_focus_step:
; SELECTED = 255
    move.b #255,d0
    move.b d0,SELECTED
; VALID = 0
    move.b #0,d0
    move.b d0,VALID
; if CURRENT != 255:
    move.b CURRENT,d0
    cmp.b #255,d0
    bne interaction_focus_L1
    jmp interaction_focus_L2
interaction_focus_L1:
; if CURRENT >= 4:
    move.b CURRENT,d0
    cmp.b #4,d0
    bcc interaction_focus_L4
    jmp interaction_focus_L5
interaction_focus_L4:
; return
    rts
    jmp interaction_focus_L6
interaction_focus_L5:
interaction_focus_L6:
    jmp interaction_focus_L3
interaction_focus_L2:
interaction_focus_L3:
; if ACTIVE >= 16:
    move.b ACTIVE,d0
    cmp.b #16,d0
    bcc interaction_focus_L7
    jmp interaction_focus_L8
interaction_focus_L7:
; return
    rts
    jmp interaction_focus_L9
interaction_focus_L8:
interaction_focus_L9:
; VALID = 1
    move.b #1,d0
    move.b d0,VALID
; T0 = 0
    move.b #0,d0
    move.b d0,T0
; T1 = ACTIVE
    move.b ACTIVE,d0
    move.b d0,T1
; T2 = 255
    move.b #255,d0
    move.b d0,T2
; T3 = 0
    move.b #0,d0
    move.b d0,T3
; T4 = 255
    move.b #255,d0
    move.b d0,T4
; while T0 < 4:
interaction_focus_L10:
    move.b T0,d0
    cmp.b #4,d0
    bcs interaction_focus_L11
    jmp interaction_focus_L12
interaction_focus_L11:
; T5 = T1 & 1
    move.b T1,d0
    and.b #1,d0
    move.b d0,T5
; if T5 != 0:
    move.b T5,d0
    cmp.b #0,d0
    bne interaction_focus_L13
    jmp interaction_focus_L14
interaction_focus_L13:
; T5 = D[T0]
    moveq #0,d1
    move.b T0,d1
    lea DIST0,a0
    move.b 0(a0,d1.w),d0
    move.b d0,T5
; if T5 <= RANGE:
    move.b T5,d0
    move.b RANGE,d1
    cmp.b d1,d0
    bls interaction_focus_L16
    jmp interaction_focus_L17
interaction_focus_L16:
; if SELECTED == 255:
    move.b SELECTED,d0
    cmp.b #255,d0
    beq interaction_focus_L19
    jmp interaction_focus_L20
interaction_focus_L19:
; SELECTED = T0
    move.b T0,d0
    move.b d0,SELECTED
; T2 = T5
    move.b T5,d0
    move.b d0,T2
    jmp interaction_focus_L21
interaction_focus_L20:
; if T5 < T2:
    move.b T5,d0
    move.b T2,d1
    cmp.b d1,d0
    bcs interaction_focus_L22
    jmp interaction_focus_L23
interaction_focus_L22:
; SELECTED = T0
    move.b T0,d0
    move.b d0,SELECTED
; T2 = T5
    move.b T5,d0
    move.b d0,T2
    jmp interaction_focus_L24
interaction_focus_L23:
interaction_focus_L24:
interaction_focus_L21:
; if T0 == CURRENT:
    move.b T0,d0
    move.b CURRENT,d1
    cmp.b d1,d0
    beq interaction_focus_L25
    jmp interaction_focus_L26
interaction_focus_L25:
; T3 = 1
    move.b #1,d0
    move.b d0,T3
; T4 = T5
    move.b T5,d0
    move.b d0,T4
    jmp interaction_focus_L27
interaction_focus_L26:
interaction_focus_L27:
    jmp interaction_focus_L18
interaction_focus_L17:
interaction_focus_L18:
    jmp interaction_focus_L15
interaction_focus_L14:
interaction_focus_L15:
; T1 = T1 >> 1
    move.b T1,d0
    lsr.b #1,d0
    move.b d0,T1
; T0 = T0 + 1
    move.b T0,d0
    add.b #1,d0
    move.b d0,T0
    jmp interaction_focus_L10
interaction_focus_L12:
; if T3 != 0:
    move.b T3,d0
    cmp.b #0,d0
    bne interaction_focus_L28
    jmp interaction_focus_L29
interaction_focus_L28:
; T5 = T4 - T2
    move.b T4,d0
    move.b T2,d1
    sub.b d1,d0
    move.b d0,T5
; if T5 <= MARGIN:
    move.b T5,d0
    move.b MARGIN,d1
    cmp.b d1,d0
    bls interaction_focus_L31
    jmp interaction_focus_L32
interaction_focus_L31:
; SELECTED = CURRENT
    move.b CURRENT,d0
    move.b d0,SELECTED
    jmp interaction_focus_L33
interaction_focus_L32:
interaction_focus_L33:
    jmp interaction_focus_L30
interaction_focus_L29:
interaction_focus_L30:
    rts
component_end:

Machine contract

D0–D3/A0–A1/CCR may change; D4–D7/A2–A6 are preserved. A direct BSR/JSR needs four 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 A500 OCS diagnostic cold-boots an OFS disk, owns the Copper and bitplane and disables interrupt/DMA sources. The helper does not take over hardware or allocate Chip RAM. The four distance bytes are named DIST0..DIST3 here because D0..D3 identify CPU registers on 68000. Their order matches the abstract D0..D3 interface.

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 and BUILD198X=/path/to/build198x. The Makefile produces normal, custom, deliberately wrong and repaired programs. Tools must already be installed; the sample downloads nothing.

Bindings and include order

demo.asm
    section code,code_c
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"
    even
    include "data.inc"
    include "cases.inc"
    even
    include "display.inc"
    include "../../framework/turn-deadline/font.inc"
code_end:
STATE:
SELECTED: dc.b 0
VALID: dc.b 0
CURRENT: dc.b 0
ACTIVE: dc.b 0
RANGE: dc.b 0
MARGIN: dc.b 0
DIST0: dc.b 0
DIST1: dc.b 0
DIST2: dc.b 0
DIST3: dc.b 0
T0: dc.b 0
T1: dc.b 0
T2: dc.b 0
T3: dc.b 0
T4: dc.b 0
T5: dc.b 0
    even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
    even
signature: dc.b "198XNIGHTTWO000",0
    even
copperlist:
    dc.w $08e,$2c81,$090,$2cc1,$092,$0038,$094,$00d0
    dc.w $100,$1200,$102,0,$104,0,$108,0,$10a,0
    dc.w $180,$012,$182,$fff
copbpl:
    dc.w $0e0,0,$0e2,0
    dc.w $ffff,$fffe
plane: dcb.b 10240,0

A client retaining state between calls

exercise.inc, excerpt
preview_rules:
    ifne CUSTOM
; CURRENT = 3
    move.b #3,d0
    move.b d0,CURRENT
; ACTIVE = 15
    move.b #15,d0
    move.b d0,ACTIVE
; RANGE = 10
    move.b #10,d0
    move.b d0,RANGE
; MARGIN = 0
    move.b #0,d0
    move.b d0,MARGIN
; DIST0 = 10
    move.b #10,d0
    move.b d0,DIST0
; DIST1 = 10
    move.b #10,d0
    move.b d0,DIST1
; DIST2 = 10
    move.b #10,d0
    move.b d0,DIST2
; DIST3 = 10
    move.b #10,d0
    move.b d0,DIST3
    ifne BROKEN
; CURRENT = 255
    move.b #255,d0
    move.b d0,CURRENT
    endif
    bsr checked_step
    bsr emit_state
    rts
    else
; CURRENT = 1
    move.b #1,d0
    move.b d0,CURRENT
; ACTIVE = 3
    move.b #3,d0
    move.b d0,ACTIVE
; RANGE = 20
    move.b #20,d0
    move.b d0,RANGE
; MARGIN = 2
    move.b #2,d0
    move.b d0,MARGIN
; DIST0 = 9
    move.b #9,d0
    move.b d0,DIST0
; DIST1 = 10
    move.b #10,d0
    move.b d0,DIST1
    ifne BROKEN
; CURRENT = 255
    move.b #255,d0
    move.b d0,CURRENT
    endif
    bsr checked_step
    bsr emit_state
; CURRENT = 1
    move.b #1,d0
    move.b d0,CURRENT
; DIST0 = 8
    move.b #8,d0
    move.b d0,DIST0
    ifne BROKEN
; CURRENT = 255
    move.b #255,d0
    move.b d0,CURRENT
    endif
    bsr checked_step
    bsr emit_state
; CURRENT = 1
    move.b #1,d0
    move.b d0,CURRENT
; DIST0 = 8
    move.b #8,d0
    move.b d0,DIST0
    ifne BROKEN
; CURRENT = 255
    move.b #255,d0
    move.b d0,CURRENT
    endif
    bsr checked_step
    bsr emit_state
    endif
    rts

Customise 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

custom.asm
    section code,code_c
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"
    even
    include "data.inc"
    include "cases.inc"
    even
    include "display.inc"
    include "../../framework/turn-deadline/font.inc"
code_end:
STATE:
SELECTED: dc.b 0
VALID: dc.b 0
CURRENT: dc.b 0
ACTIVE: dc.b 0
RANGE: dc.b 0
MARGIN: dc.b 0
DIST0: dc.b 0
DIST1: dc.b 0
DIST2: dc.b 0
DIST3: dc.b 0
T0: dc.b 0
T1: dc.b 0
T2: dc.b 0
T3: dc.b 0
T4: dc.b 0
T5: dc.b 0
    even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
    even
signature: dc.b "198XNIGHTTWO000",0
    even
copperlist:
    dc.w $08e,$2c81,$090,$2cc1,$092,$0038,$094,$00d0
    dc.w $100,$1200,$102,0,$104,0,$108,0,$10a,0
    dc.w $180,$012,$182,$fff
copbpl:
    dc.w $0e0,0,$0e2,0
    dc.w $ffff,$fffe
plane: dcb.b 10240,0

The 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

wrong.asm
    section code,code_c
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"
    even
    include "data.inc"
    include "cases.inc"
    even
    include "display.inc"
    include "../../framework/turn-deadline/font.inc"
code_end:
STATE:
SELECTED: dc.b 0
VALID: dc.b 0
CURRENT: dc.b 0
ACTIVE: dc.b 0
RANGE: dc.b 0
MARGIN: dc.b 0
DIST0: dc.b 0
DIST1: dc.b 0
DIST2: dc.b 0
DIST3: dc.b 0
T0: dc.b 0
T1: dc.b 0
T2: dc.b 0
T3: dc.b 0
T4: dc.b 0
T5: dc.b 0
    even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
    even
signature: dc.b "198XNIGHTTWO000",0
    even
copperlist:
    dc.w $08e,$2c81,$090,$2cc1,$092,$0038,$094,$00d0
    dc.w $100,$1200,$102,0,$104,0,$108,0,$10a,0
    dc.w $180,$012,$182,$fff
copbpl:
    dc.w $0e0,0,$0e2,0
    dc.w $ffff,$fffe
plane: dcb.b 10240,0
Amiga shows the deliberate error row 00 01 FF 03 14 02 08 0A.
The wrong client completes with 00 01 FF 03 14 02 08 0A; its changed inputs and state explain the result.

Cost and alternatives

The measured helper extent is 534 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 A500 OCS PAL, Kickstart1.3 OFS cold boot, using Asm198x 0.0.72, Emu198x 0.44.2 and Build198x 0.2.9. 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.

The Craft · Maths for Games · Source and native evidence.