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

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
; 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:
lda #0
sta FIRST
sta VALID
sta MODE
sta OBJECT_A
sta OBJECT_B
sta B0
sta B1
sta B2
sta B3
sta B4
sta B5
sta B6
sta B7
sta T0
sta T1
sta T2
sta T3
sta T4
sta EXPR_SAVE
rts
contact_pair_cache_step:
; FIRST = 0
lda #0
sta FIRST
; VALID = 0
lda #0
sta VALID
; if MODE > 2:
lda MODE
cmp #2
beq contact_pair_cache_L5
bcs contact_pair_cache_L4
contact_pair_cache_L5:
jmp contact_pair_cache_L2
contact_pair_cache_L4:
jmp contact_pair_cache_L1
contact_pair_cache_L1:
; return
rts
jmp contact_pair_cache_L3
contact_pair_cache_L2:
contact_pair_cache_L3:
; if MODE == 0:
lda MODE
cmp #0
beq contact_pair_cache_L9
contact_pair_cache_L10:
jmp contact_pair_cache_L7
contact_pair_cache_L9:
jmp contact_pair_cache_L6
contact_pair_cache_L6:
; T0 = 0
lda #0
sta T0
; while T0 < 8:
contact_pair_cache_L11:
lda T0
cmp #8
bcc contact_pair_cache_L14
contact_pair_cache_L15:
jmp contact_pair_cache_L13
contact_pair_cache_L14:
jmp contact_pair_cache_L12
contact_pair_cache_L12:
; B[T0] = 0
lda #0
sta EXPR_SAVE
ldx T0
lda EXPR_SAVE
sta B0,x
; T0 = T0 + 1
lda T0
clc
adc #1
sta T0
jmp contact_pair_cache_L11
contact_pair_cache_L13:
; VALID = 1
lda #1
sta VALID
; return
rts
jmp contact_pair_cache_L8
contact_pair_cache_L7:
contact_pair_cache_L8:
; if OBJECT_A >= 8:
lda OBJECT_A
cmp #8
bcs contact_pair_cache_L19
contact_pair_cache_L20:
jmp contact_pair_cache_L17
contact_pair_cache_L19:
jmp contact_pair_cache_L16
contact_pair_cache_L16:
; return
rts
jmp contact_pair_cache_L18
contact_pair_cache_L17:
contact_pair_cache_L18:
; if OBJECT_B >= 8:
lda OBJECT_B
cmp #8
bcs contact_pair_cache_L24
contact_pair_cache_L25:
jmp contact_pair_cache_L22
contact_pair_cache_L24:
jmp contact_pair_cache_L21
contact_pair_cache_L21:
; return
rts
jmp contact_pair_cache_L23
contact_pair_cache_L22:
contact_pair_cache_L23:
; if OBJECT_A == OBJECT_B:
lda OBJECT_A
cmp OBJECT_B
beq contact_pair_cache_L29
contact_pair_cache_L30:
jmp contact_pair_cache_L27
contact_pair_cache_L29:
jmp contact_pair_cache_L26
contact_pair_cache_L26:
; return
rts
jmp contact_pair_cache_L28
contact_pair_cache_L27:
contact_pair_cache_L28:
; T0 = OBJECT_A
lda OBJECT_A
sta T0
; T1 = OBJECT_B
lda OBJECT_B
sta T1
; if T0 > T1:
lda T0
cmp T1
beq contact_pair_cache_L35
bcs contact_pair_cache_L34
contact_pair_cache_L35:
jmp contact_pair_cache_L32
contact_pair_cache_L34:
jmp contact_pair_cache_L31
contact_pair_cache_L31:
; T0 = OBJECT_B
lda OBJECT_B
sta T0
; T1 = OBJECT_A
lda OBJECT_A
sta T1
jmp contact_pair_cache_L33
contact_pair_cache_L32:
contact_pair_cache_L33:
; T2 = 1
lda #1
sta T2
; while T1 != 0:
contact_pair_cache_L36:
lda T1
cmp #0
bne contact_pair_cache_L39
contact_pair_cache_L40:
jmp contact_pair_cache_L38
contact_pair_cache_L39:
jmp contact_pair_cache_L37
contact_pair_cache_L37:
; T2 = T2 << 1
lda T2
asl
sta T2
; T1 = T1 - 1
lda T1
sec
sbc #1
sta T1
jmp contact_pair_cache_L36
contact_pair_cache_L38:
; T3 = B[T0]
ldx T0
lda B0,x
sta T3
; T4 = T3 & T2
lda T3
and T2
sta T4
; VALID = 1
lda #1
sta VALID
; if T4 == 0:
lda T4
cmp #0
beq contact_pair_cache_L44
contact_pair_cache_L45:
jmp contact_pair_cache_L42
contact_pair_cache_L44:
jmp contact_pair_cache_L41
contact_pair_cache_L41:
; FIRST = 1
lda #1
sta FIRST
; if MODE == 1:
lda MODE
cmp #1
beq contact_pair_cache_L49
contact_pair_cache_L50:
jmp contact_pair_cache_L47
contact_pair_cache_L49:
jmp contact_pair_cache_L46
contact_pair_cache_L46:
; T3 = T3 | T2
lda T3
ora T2
sta T3
; B[T0] = T3
lda T3
sta EXPR_SAVE
ldx T0
lda EXPR_SAVE
sta B0,x
jmp contact_pair_cache_L48
contact_pair_cache_L47:
contact_pair_cache_L48:
jmp contact_pair_cache_L43
contact_pair_cache_L42:
contact_pair_cache_L43:
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
FIRST=$80
VALID=$81
MODE=$82
OBJECT_A=$83
OBJECT_B=$84
B0=$85
B1=$86
B2=$87
B3=$88
B4=$89
B5=$8a
B6=$8b
B7=$8c
T0=$8d
T1=$8e
T2=$8f
T3=$90
T4=$91
EXPR_SAVE=$92
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
; MODE = 1
lda #1
sta MODE
; OBJECT_A = 2
lda #2
sta OBJECT_A
; OBJECT_B = 7
lda #7
sta OBJECT_B
.if BROKEN
; B0 = 0
lda #0
sta B0
.endif
jsr checked_step
jsr emit_state
; MODE = 2
lda #2
sta MODE
.if BROKEN
; B0 = 0
lda #0
sta B0
.endif
jsr checked_step
jsr emit_state
; MODE = 0
lda #0
sta MODE
.if BROKEN
; B0 = 0
lda #0
sta B0
.endif
jsr checked_step
jsr emit_state
; MODE = 1
lda #1
sta MODE
.if BROKEN
; B0 = 0
lda #0
sta B0
.endif
jsr checked_step
jsr emit_state
rts
.else
; MODE = 1
lda #1
sta MODE
; OBJECT_A = 0
lda #0
sta OBJECT_A
; OBJECT_B = 3
lda #3
sta OBJECT_B
.if BROKEN
; B0 = 0
lda #0
sta B0
.endif
jsr checked_step
jsr emit_state
.if BROKEN
; B0 = 0
lda #0
sta B0
.endif
jsr checked_step
jsr emit_state
; OBJECT_A = 3
lda #3
sta OBJECT_A
; OBJECT_B = 0
lda #0
sta OBJECT_B
.if BROKEN
; B0 = 0
lda #0
sta B0
.endif
jsr checked_step
jsr emit_state
.endif
rtsCustomise 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
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
FIRST=$80
VALID=$81
MODE=$82
OBJECT_A=$83
OBJECT_B=$84
B0=$85
B1=$86
B2=$87
B3=$88
B4=$89
B5=$8a
B6=$8b
B7=$8c
T0=$8d
T1=$8e
T2=$8f
T3=$90
T4=$91
EXPR_SAVE=$92
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 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
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
FIRST=$80
VALID=$81
MODE=$82
OBJECT_A=$83
OBJECT_B=$84
B0=$85
B1=$86
B2=$87
B3=$88
B4=$89
B5=$8a
B6=$8b
B7=$8c
T0=$8d
T1=$8e
T2=$8f
T3=$90
T4=$91
EXPR_SAVE=$92
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 302 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 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
Trigger transitions: Persistent contact transitions need previous/current sets, beyond a once-per-step pair cache.