Skip to content

Remember Contact Pairs for a Step on Amiga

The same contact can appear in both objects’ candidate lists.

The same contact can appear in both objects’ candidate lists. Apply its response once, even when the pair arrives in the opposite order.

Amiga shows the working diagnostic row 00 01 01 03 00 08 00 00.
A completed native caller displays 00 01 01 03 00 08 00 00. 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

Object identifiers are 0–7. MODE=0 clears the eight-byte cache; MODE=1 tests and remembers a distinct pair; MODE=2 tests without changing the cache. Return FIRST=1 if the unordered pair has not been remembered and VALID=1 for accepted operations. Invalid mode/identifier or a self-pair returns zero outputs without cache writes. Clear once before enumerating a simulation step.

Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: FIRST, VALID, MODE, OBJECT_A, OBJECT_B, B0, B1, B2, B3, B4, B5, B6, B7. They occupy 13 contiguous bytes. Also reserve the private workspace: T0, T1, T2, T3, T4, EXPR_SAVE (6 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.

Call contact_pair_cache_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 contact_pair_cache_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 B0..B7
Supply before service MODE and OBJECT_A/B
Read after service FIRST and VALID

Order the two identifiers, then use the smaller as a row and the larger as a bit. Both traversal orders address the same bit.

Reusable initialisation and service

component.inc
; SPDX-License-Identifier: MIT
; Remember Contact Pairs for a Step. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
contact_pair_cache_init:
    move.b #0,d0
    move.b d0,FIRST
    move.b d0,VALID
    move.b d0,MODE
    move.b d0,OBJECT_A
    move.b d0,OBJECT_B
    move.b d0,B0
    move.b d0,B1
    move.b d0,B2
    move.b d0,B3
    move.b d0,B4
    move.b d0,B5
    move.b d0,B6
    move.b d0,B7
    move.b d0,T0
    move.b d0,T1
    move.b d0,T2
    move.b d0,T3
    move.b d0,T4
    move.b d0,EXPR_SAVE
    rts
contact_pair_cache_step:
; FIRST = 0
    move.b #0,d0
    move.b d0,FIRST
; VALID = 0
    move.b #0,d0
    move.b d0,VALID
; if MODE > 2:
    move.b MODE,d0
    cmp.b #2,d0
    bhi contact_pair_cache_L1
    jmp contact_pair_cache_L2
contact_pair_cache_L1:
; return
    rts
    jmp contact_pair_cache_L3
contact_pair_cache_L2:
contact_pair_cache_L3:
; if MODE == 0:
    move.b MODE,d0
    cmp.b #0,d0
    beq contact_pair_cache_L4
    jmp contact_pair_cache_L5
contact_pair_cache_L4:
; T0 = 0
    move.b #0,d0
    move.b d0,T0
; while T0 < 8:
contact_pair_cache_L7:
    move.b T0,d0
    cmp.b #8,d0
    bcs contact_pair_cache_L8
    jmp contact_pair_cache_L9
contact_pair_cache_L8:
; B[T0] = 0
    move.b #0,d0
    move.b d0,d2
    moveq #0,d1
    move.b T0,d1
    lea B0,a0
    move.b d2,0(a0,d1.w)
; T0 = T0 + 1
    move.b T0,d0
    add.b #1,d0
    move.b d0,T0
    jmp contact_pair_cache_L7
contact_pair_cache_L9:
; VALID = 1
    move.b #1,d0
    move.b d0,VALID
; return
    rts
    jmp contact_pair_cache_L6
contact_pair_cache_L5:
contact_pair_cache_L6:
; if OBJECT_A >= 8:
    move.b OBJECT_A,d0
    cmp.b #8,d0
    bcc contact_pair_cache_L10
    jmp contact_pair_cache_L11
contact_pair_cache_L10:
; return
    rts
    jmp contact_pair_cache_L12
contact_pair_cache_L11:
contact_pair_cache_L12:
; if OBJECT_B >= 8:
    move.b OBJECT_B,d0
    cmp.b #8,d0
    bcc contact_pair_cache_L13
    jmp contact_pair_cache_L14
contact_pair_cache_L13:
; return
    rts
    jmp contact_pair_cache_L15
contact_pair_cache_L14:
contact_pair_cache_L15:
; if OBJECT_A == OBJECT_B:
    move.b OBJECT_A,d0
    move.b OBJECT_B,d1
    cmp.b d1,d0
    beq contact_pair_cache_L16
    jmp contact_pair_cache_L17
contact_pair_cache_L16:
; return
    rts
    jmp contact_pair_cache_L18
contact_pair_cache_L17:
contact_pair_cache_L18:
; T0 = OBJECT_A
    move.b OBJECT_A,d0
    move.b d0,T0
; T1 = OBJECT_B
    move.b OBJECT_B,d0
    move.b d0,T1
; if T0 > T1:
    move.b T0,d0
    move.b T1,d1
    cmp.b d1,d0
    bhi contact_pair_cache_L19
    jmp contact_pair_cache_L20
contact_pair_cache_L19:
; T0 = OBJECT_B
    move.b OBJECT_B,d0
    move.b d0,T0
; T1 = OBJECT_A
    move.b OBJECT_A,d0
    move.b d0,T1
    jmp contact_pair_cache_L21
contact_pair_cache_L20:
contact_pair_cache_L21:
; T2 = 1
    move.b #1,d0
    move.b d0,T2
; while T1 != 0:
contact_pair_cache_L22:
    move.b T1,d0
    cmp.b #0,d0
    bne contact_pair_cache_L23
    jmp contact_pair_cache_L24
contact_pair_cache_L23:
; T2 = T2 << 1
    move.b T2,d0
    lsl.b #1,d0
    move.b d0,T2
; T1 = T1 - 1
    move.b T1,d0
    sub.b #1,d0
    move.b d0,T1
    jmp contact_pair_cache_L22
contact_pair_cache_L24:
; T3 = B[T0]
    moveq #0,d1
    move.b T0,d1
    lea B0,a0
    move.b 0(a0,d1.w),d0
    move.b d0,T3
; T4 = T3 & T2
    move.b T3,d0
    move.b T2,d1
    and.b d1,d0
    move.b d0,T4
; VALID = 1
    move.b #1,d0
    move.b d0,VALID
; if T4 == 0:
    move.b T4,d0
    cmp.b #0,d0
    beq contact_pair_cache_L25
    jmp contact_pair_cache_L26
contact_pair_cache_L25:
; FIRST = 1
    move.b #1,d0
    move.b d0,FIRST
; if MODE == 1:
    move.b MODE,d0
    cmp.b #1,d0
    beq contact_pair_cache_L28
    jmp contact_pair_cache_L29
contact_pair_cache_L28:
; T3 = T3 | T2
    move.b T3,d0
    move.b T2,d1
    or.b d1,d0
    move.b d0,T3
; B[T0] = T3
    move.b T3,d0
    move.b d0,d2
    moveq #0,d1
    move.b T0,d1
    lea B0,a0
    move.b d2,0(a0,d1.w)
    jmp contact_pair_cache_L30
contact_pair_cache_L29:
contact_pair_cache_L30:
    jmp contact_pair_cache_L27
contact_pair_cache_L26:
contact_pair_cache_L27:
    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.

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:
FIRST: dc.b 0
VALID: dc.b 0
MODE: dc.b 0
OBJECT_A: dc.b 0
OBJECT_B: dc.b 0
B0: dc.b 0
B1: dc.b 0
B2: dc.b 0
B3: dc.b 0
B4: dc.b 0
B5: dc.b 0
B6: dc.b 0
B7: dc.b 0
T0: dc.b 0
T1: dc.b 0
T2: dc.b 0
T3: dc.b 0
T4: dc.b 0
EXPR_SAVE: dc.b 0
    even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
    even
signature: dc.b "198XNIGHTONE000",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
; MODE = 1
    move.b #1,d0
    move.b d0,MODE
; OBJECT_A = 2
    move.b #2,d0
    move.b d0,OBJECT_A
; OBJECT_B = 7
    move.b #7,d0
    move.b d0,OBJECT_B
    ifne BROKEN
; B0 = 0
    move.b #0,d0
    move.b d0,B0
    endif
    bsr checked_step
    bsr emit_state
; MODE = 2
    move.b #2,d0
    move.b d0,MODE
    ifne BROKEN
; B0 = 0
    move.b #0,d0
    move.b d0,B0
    endif
    bsr checked_step
    bsr emit_state
; MODE = 0
    move.b #0,d0
    move.b d0,MODE
    ifne BROKEN
; B0 = 0
    move.b #0,d0
    move.b d0,B0
    endif
    bsr checked_step
    bsr emit_state
; MODE = 1
    move.b #1,d0
    move.b d0,MODE
    ifne BROKEN
; B0 = 0
    move.b #0,d0
    move.b d0,B0
    endif
    bsr checked_step
    bsr emit_state
    rts
    else
; MODE = 1
    move.b #1,d0
    move.b d0,MODE
; OBJECT_A = 0
    move.b #0,d0
    move.b d0,OBJECT_A
; OBJECT_B = 3
    move.b #3,d0
    move.b d0,OBJECT_B
    ifne BROKEN
; B0 = 0
    move.b #0,d0
    move.b d0,B0
    endif
    bsr checked_step
    bsr emit_state

    ifne BROKEN
; B0 = 0
    move.b #0,d0
    move.b d0,B0
    endif
    bsr checked_step
    bsr emit_state
; OBJECT_A = 3
    move.b #3,d0
    move.b d0,OBJECT_A
; OBJECT_B = 0
    move.b #0,d0
    move.b d0,OBJECT_B
    ifne BROKEN
; B0 = 0
    move.b #0,d0
    move.b d0,B0
    endif
    bsr checked_step
    bsr emit_state
    endif
    rts

Customise it

Query without inserting, then reset and remember a pair in a new step. 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:
FIRST: dc.b 0
VALID: dc.b 0
MODE: dc.b 0
OBJECT_A: dc.b 0
OBJECT_B: dc.b 0
B0: dc.b 0
B1: dc.b 0
B2: dc.b 0
B3: dc.b 0
B4: dc.b 0
B5: dc.b 0
B6: dc.b 0
B7: dc.b 0
T0: dc.b 0
T1: dc.b 0
T2: dc.b 0
T3: dc.b 0
T4: dc.b 0
EXPR_SAVE: dc.b 0
    even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
    even
signature: dc.b "198XNIGHTONE000",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 01 01 01 02 07 00 00 80. The full client trace in the source explains how it gets there.

Mistake and repair

The wrong caller clears cache row zero before every pair. Repeated (0,3) and (3,0) contacts are each reported as first contacts. Reset once before the whole traversal, not between callbacks.

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:
FIRST: dc.b 0
VALID: dc.b 0
MODE: dc.b 0
OBJECT_A: dc.b 0
OBJECT_B: dc.b 0
B0: dc.b 0
B1: dc.b 0
B2: dc.b 0
B3: dc.b 0
B4: dc.b 0
B5: dc.b 0
B6: dc.b 0
B7: dc.b 0
T0: dc.b 0
T1: dc.b 0
T2: dc.b 0
T3: dc.b 0
T4: dc.b 0
EXPR_SAVE: dc.b 0
    even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
    even
signature: dc.b "198XNIGHTONE000",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 01 01 01 03 00 08 00 00.
The wrong client completes with 01 01 01 03 00 08 00 00; its changed inputs and state explain the result.

Cost and alternatives

The measured helper extent is 548 code bytes; public state is 13 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.

Eight reset writes, or bounded mask construction per pair. A triangular packed table saves unused bits but complicates indexing; larger object counts need a different storage budget.

Identifiers must remain stable for the step; do not recycle a slot halfway through enumeration. This is step-local deduplication, not persistent enter/stay/exit tracking or an interrupt-safe shared container.

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

Trigger transitions: Persistent contact transitions need previous/current sets, beyond a once-per-step pair cache.

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