Spawning or deleting objects during a traversal can accidentally update a new object twice or skip a neighbour.

Use it in a project
Four object slots use ACTIVE bits0..3 and generations G0..G3. MODE=1 queues a spawn in SLOT; MODE=2 queues a removal of SLOT with GENERATION; MODE=3 flushes. MODE=0 clears pending commands without changing objects. COUNT is0..4. Queueing never changes the object table. A full queue returns FULL=1 and preserves its commands. Flush handles commands in insertion order: spawn only a free slot and increment its generation modulo256; remove only a live matching-generation slot. APPLIED/REJECTED count results and COUNT becomes zero. Invalid global state or queued command structure sets VALID=0 and leaves objects and queue intact. Slot payload initialisation is caller-owned.
Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: APPLIED, REJECTED, FULL, VALID, MODE, SLOT, GENERATION, ACTIVE, COUNT, G0, G1, G2, G3, O0, O1, O2, O3, S0, S1, S2, S3, H0, H1, H2, H3. They occupy 25 contiguous bytes. Also reserve the private workspace: T0, T1, T2, T3, T4, T5, T6, EXPR_SAVE (8 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.
Call object_command_buffer_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 object_command_buffer_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 | ACTIVE, G0..G3, COUNT and queued O/S/H arrays |
| Supply before service | MODE, SLOT and GENERATION |
| Read after service | APPLIED, REJECTED, FULL, VALID and updated table/queue |
A validation pass checks every queued operation before mutation. The application pass then honours insertion order and current generations, so a stale removal cannot delete a newly reused slot. Queueing and flushing are distinct mutation boundaries.
Reusable initialisation and service
; SPDX-License-Identifier: MIT
; Defer Object Changes Until After Traversal. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
object_command_buffer_init:
lda #0
sta APPLIED
sta REJECTED
sta FULL
sta VALID
sta MODE
sta SLOT
sta GENERATION
sta ACTIVE
sta COUNT
sta G0
sta G1
sta G2
sta G3
sta O0
sta O1
sta O2
sta O3
sta S0
sta S1
sta S2
sta S3
sta H0
sta H1
sta H2
sta H3
sta T0
sta T1
sta T2
sta T3
sta T4
sta T5
sta T6
sta EXPR_SAVE
rts
object_command_buffer_step:
; APPLIED = 0
lda #0
sta APPLIED
; REJECTED = 0
lda #0
sta REJECTED
; FULL = 0
lda #0
sta FULL
; VALID = 0
lda #0
sta VALID
; if ACTIVE >= 16:
lda ACTIVE
cmp #16
bcs object_command_buffer_L4
object_command_buffer_L5:
jmp object_command_buffer_L2
object_command_buffer_L4:
jmp object_command_buffer_L1
object_command_buffer_L1:
; return
rts
jmp object_command_buffer_L3
object_command_buffer_L2:
object_command_buffer_L3:
; if COUNT > 4:
lda COUNT
cmp #4
beq object_command_buffer_L10
bcs object_command_buffer_L9
object_command_buffer_L10:
jmp object_command_buffer_L7
object_command_buffer_L9:
jmp object_command_buffer_L6
object_command_buffer_L6:
; return
rts
jmp object_command_buffer_L8
object_command_buffer_L7:
object_command_buffer_L8:
; if MODE > 3:
lda MODE
cmp #3
beq object_command_buffer_L15
bcs object_command_buffer_L14
object_command_buffer_L15:
jmp object_command_buffer_L12
object_command_buffer_L14:
jmp object_command_buffer_L11
object_command_buffer_L11:
; return
rts
jmp object_command_buffer_L13
object_command_buffer_L12:
object_command_buffer_L13:
; if MODE == 0:
lda MODE
cmp #0
beq object_command_buffer_L19
object_command_buffer_L20:
jmp object_command_buffer_L17
object_command_buffer_L19:
jmp object_command_buffer_L16
object_command_buffer_L16:
; COUNT = 0
lda #0
sta COUNT
; VALID = 1
lda #1
sta VALID
; return
rts
jmp object_command_buffer_L18
object_command_buffer_L17:
object_command_buffer_L18:
; if MODE < 3:
lda MODE
cmp #3
bcc object_command_buffer_L24
object_command_buffer_L25:
jmp object_command_buffer_L22
object_command_buffer_L24:
jmp object_command_buffer_L21
object_command_buffer_L21:
; if SLOT >= 4:
lda SLOT
cmp #4
bcs object_command_buffer_L29
object_command_buffer_L30:
jmp object_command_buffer_L27
object_command_buffer_L29:
jmp object_command_buffer_L26
object_command_buffer_L26:
; return
rts
jmp object_command_buffer_L28
object_command_buffer_L27:
object_command_buffer_L28:
; VALID = 1
lda #1
sta VALID
; if COUNT == 4:
lda COUNT
cmp #4
beq object_command_buffer_L34
object_command_buffer_L35:
jmp object_command_buffer_L32
object_command_buffer_L34:
jmp object_command_buffer_L31
object_command_buffer_L31:
; FULL = 1
lda #1
sta FULL
; return
rts
jmp object_command_buffer_L33
object_command_buffer_L32:
object_command_buffer_L33:
; O[COUNT] = MODE
lda MODE
sta EXPR_SAVE
ldx COUNT
lda EXPR_SAVE
sta O0,x
; S[COUNT] = SLOT
lda SLOT
sta EXPR_SAVE
ldx COUNT
lda EXPR_SAVE
sta S0,x
; H[COUNT] = GENERATION
lda GENERATION
sta EXPR_SAVE
ldx COUNT
lda EXPR_SAVE
sta H0,x
; COUNT = COUNT + 1
lda COUNT
clc
adc #1
sta COUNT
; return
rts
jmp object_command_buffer_L23
object_command_buffer_L22:
object_command_buffer_L23:
; T0 = 0
lda #0
sta T0
; while T0 < COUNT:
object_command_buffer_L36:
lda T0
cmp COUNT
bcc object_command_buffer_L39
object_command_buffer_L40:
jmp object_command_buffer_L38
object_command_buffer_L39:
jmp object_command_buffer_L37
object_command_buffer_L37:
; T1 = O[T0]
ldx T0
lda O0,x
sta T1
; if T1 == 0:
lda T1
cmp #0
beq object_command_buffer_L44
object_command_buffer_L45:
jmp object_command_buffer_L42
object_command_buffer_L44:
jmp object_command_buffer_L41
object_command_buffer_L41:
; return
rts
jmp object_command_buffer_L43
object_command_buffer_L42:
object_command_buffer_L43:
; if T1 > 2:
lda T1
cmp #2
beq object_command_buffer_L50
bcs object_command_buffer_L49
object_command_buffer_L50:
jmp object_command_buffer_L47
object_command_buffer_L49:
jmp object_command_buffer_L46
object_command_buffer_L46:
; return
rts
jmp object_command_buffer_L48
object_command_buffer_L47:
object_command_buffer_L48:
; T1 = S[T0]
ldx T0
lda S0,x
sta T1
; if T1 >= 4:
lda T1
cmp #4
bcs object_command_buffer_L54
object_command_buffer_L55:
jmp object_command_buffer_L52
object_command_buffer_L54:
jmp object_command_buffer_L51
object_command_buffer_L51:
; return
rts
jmp object_command_buffer_L53
object_command_buffer_L52:
object_command_buffer_L53:
; T0 = T0 + 1
lda T0
clc
adc #1
sta T0
jmp object_command_buffer_L36
object_command_buffer_L38:
; VALID = 1
lda #1
sta VALID
; T0 = 0
lda #0
sta T0
; while T0 < COUNT:
object_command_buffer_L56:
lda T0
cmp COUNT
bcc object_command_buffer_L59
object_command_buffer_L60:
jmp object_command_buffer_L58
object_command_buffer_L59:
jmp object_command_buffer_L57
object_command_buffer_L57:
; T1 = S[T0]
ldx T0
lda S0,x
sta T1
; T2 = 1
lda #1
sta T2
; T3 = T1
lda T1
sta T3
; while T3 != 0:
object_command_buffer_L61:
lda T3
cmp #0
bne object_command_buffer_L64
object_command_buffer_L65:
jmp object_command_buffer_L63
object_command_buffer_L64:
jmp object_command_buffer_L62
object_command_buffer_L62:
; T2 = T2 << 1
lda T2
asl
sta T2
; T3 = T3 - 1
lda T3
sec
sbc #1
sta T3
jmp object_command_buffer_L61
object_command_buffer_L63:
; T3 = ACTIVE & T2
lda ACTIVE
and T2
sta T3
; T4 = O[T0]
ldx T0
lda O0,x
sta T4
; T5 = 0
lda #0
sta T5
; if T4 == 1:
lda T4
cmp #1
beq object_command_buffer_L69
object_command_buffer_L70:
jmp object_command_buffer_L67
object_command_buffer_L69:
jmp object_command_buffer_L66
object_command_buffer_L66:
; if T3 == 0:
lda T3
cmp #0
beq object_command_buffer_L74
object_command_buffer_L75:
jmp object_command_buffer_L72
object_command_buffer_L74:
jmp object_command_buffer_L71
object_command_buffer_L71:
; ACTIVE = ACTIVE | T2
lda ACTIVE
ora T2
sta ACTIVE
; T4 = G[T1]
ldx T1
lda G0,x
sta T4
; T4 = T4 + 1
lda T4
clc
adc #1
sta T4
; G[T1] = T4
lda T4
sta EXPR_SAVE
ldx T1
lda EXPR_SAVE
sta G0,x
; T5 = 1
lda #1
sta T5
jmp object_command_buffer_L73
object_command_buffer_L72:
object_command_buffer_L73:
jmp object_command_buffer_L68
object_command_buffer_L67:
; if T3 != 0:
lda T3
cmp #0
bne object_command_buffer_L79
object_command_buffer_L80:
jmp object_command_buffer_L77
object_command_buffer_L79:
jmp object_command_buffer_L76
object_command_buffer_L76:
; T4 = G[T1]
ldx T1
lda G0,x
sta T4
; T6 = H[T0]
ldx T0
lda H0,x
sta T6
; if T4 == T6:
lda T4
cmp T6
beq object_command_buffer_L84
object_command_buffer_L85:
jmp object_command_buffer_L82
object_command_buffer_L84:
jmp object_command_buffer_L81
object_command_buffer_L81:
; ACTIVE = ACTIVE ^ T2
lda ACTIVE
eor T2
sta ACTIVE
; T5 = 1
lda #1
sta T5
jmp object_command_buffer_L83
object_command_buffer_L82:
object_command_buffer_L83:
jmp object_command_buffer_L78
object_command_buffer_L77:
object_command_buffer_L78:
object_command_buffer_L68:
; if T5 != 0:
lda T5
cmp #0
bne object_command_buffer_L89
object_command_buffer_L90:
jmp object_command_buffer_L87
object_command_buffer_L89:
jmp object_command_buffer_L86
object_command_buffer_L86:
; APPLIED = APPLIED + 1
lda APPLIED
clc
adc #1
sta APPLIED
jmp object_command_buffer_L88
object_command_buffer_L87:
; REJECTED = REJECTED + 1
lda REJECTED
clc
adc #1
sta REJECTED
object_command_buffer_L88:
; T0 = T0 + 1
lda T0
clc
adc #1
sta T0
jmp object_command_buffer_L56
object_command_buffer_L58:
; COUNT = 0
lda #0
sta COUNT
rts
component_end:Machine contract
A/X/status may change; Y and decimal mode are preserved. Use CLD for binary arithmetic. 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 diagnostic PRG owns screen, font and colour RAM and disables interrupts; it does not return to BASIC. The reusable helper does none of those things.
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
APPLIED=$80
REJECTED=$81
FULL=$82
VALID=$83
MODE=$84
SLOT=$85
GENERATION=$86
ACTIVE=$87
COUNT=$88
G0=$89
G1=$8a
G2=$8b
G3=$8c
O0=$8d
O1=$8e
O2=$8f
O3=$90
S0=$91
S1=$92
S2=$93
S3=$94
H0=$95
H1=$96
H2=$97
H3=$98
T0=$99
T1=$9a
T2=$9b
T3=$9c
T4=$9d
T5=$9e
T6=$9f
EXPR_SAVE=$a0
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$c000
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
!source "caller.inc"
!source "exercise.inc"
component_start:
!source "component.inc"
!source "data.inc"
!source "cases.inc"
hex_font:
!byte 0,0,0,0,0,0,0,0,0,56,40,40,40,56,0,0,0,16,48,16,16,56,0,0,0,56,8,56,32,56,0,0,0,56,8,56,8,56,0,0,0,40,40,56,8,8,0,0,0,56,32,56,8,56,0,0,0,56,32,56,40,56,0,0,0,56,8,8,8,8,0,0,0,56,40,56,40,56,0,0,0,56,40,56,8,56,0,0,0,16,40,56,40,40,0,0,0,48,40,48,40,48,0,0,0,56,32,32,32,56,0,0,0,48,40,40,40,48,0,0,0,56,32,48,32,56,0,0,0,56,32,48,32,32,0,0
code_end:A client retaining state between calls
preview_rules:
!if CUSTOM {
; MODE = 1
lda #1
sta MODE
; SLOT = 2
lda #2
sta SLOT
!if BROKEN {
; COUNT = 0
lda #0
sta COUNT
}
jsr checked_step
jsr emit_state
; MODE = 2
lda #2
sta MODE
; GENERATION = 1
lda #1
sta GENERATION
!if BROKEN {
; COUNT = 0
lda #0
sta COUNT
}
jsr checked_step
jsr emit_state
; MODE = 3
lda #3
sta MODE
!if BROKEN {
; COUNT = 0
lda #0
sta COUNT
}
jsr checked_step
jsr emit_state
rts
} else {
; MODE = 1
lda #1
sta MODE
; SLOT = 1
lda #1
sta SLOT
!if BROKEN {
; COUNT = 0
lda #0
sta COUNT
}
jsr checked_step
jsr emit_state
; MODE = 3
lda #3
sta MODE
!if BROKEN {
; COUNT = 0
lda #0
sta COUNT
}
jsr checked_step
jsr emit_state
; MODE = 2
lda #2
sta MODE
; SLOT = 1
lda #1
sta SLOT
; GENERATION = 0
lda #0
sta GENERATION
!if BROKEN {
; COUNT = 0
lda #0
sta COUNT
}
jsr checked_step
jsr emit_state
; MODE = 3
lda #3
sta MODE
!if BROKEN {
; COUNT = 0
lda #0
sta COUNT
}
jsr checked_step
jsr emit_state
}
rtsCustomise it
Queue a spawn then a matching removal of the same slot; insertion order leaves it free after one flush. 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
APPLIED=$80
REJECTED=$81
FULL=$82
VALID=$83
MODE=$84
SLOT=$85
GENERATION=$86
ACTIVE=$87
COUNT=$88
G0=$89
G1=$8a
G2=$8b
G3=$8c
O0=$8d
O1=$8e
O2=$8f
O3=$90
S0=$91
S1=$92
S2=$93
S3=$94
H0=$95
H1=$96
H2=$97
H3=$98
T0=$99
T1=$9a
T2=$9b
T3=$9c
T4=$9d
T5=$9e
T6=$9f
EXPR_SAVE=$a0
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$c000
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
!source "caller.inc"
!source "exercise.inc"
component_start:
!source "component.inc"
!source "data.inc"
!source "cases.inc"
hex_font:
!byte 0,0,0,0,0,0,0,0,0,56,40,40,40,56,0,0,0,16,48,16,16,56,0,0,0,56,8,56,32,56,0,0,0,56,8,56,8,56,0,0,0,40,40,56,8,8,0,0,0,56,32,56,8,56,0,0,0,56,32,56,40,56,0,0,0,56,8,8,8,8,0,0,0,56,40,56,40,56,0,0,0,56,40,56,8,56,0,0,0,16,40,56,40,40,0,0,0,48,40,48,40,48,0,0,0,56,32,32,32,56,0,0,0,48,40,40,40,48,0,0,0,56,32,48,32,56,0,0,0,56,32,48,32,32,0,0
code_end:The custom diagnostic row is 02 00 00 01 03 02 01 00. The full client trace in the source explains how it gets there.
Mistake and repair
The wrong client clears the pending count before each service. Flush sees an empty queue and the requested spawn never happens. Retain the command buffer until the traversal ends and a deliberate flush consumes it.
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
APPLIED=$80
REJECTED=$81
FULL=$82
VALID=$83
MODE=$84
SLOT=$85
GENERATION=$86
ACTIVE=$87
COUNT=$88
G0=$89
G1=$8a
G2=$8b
G3=$8c
O0=$8d
O1=$8e
O2=$8f
O3=$90
S0=$91
S1=$92
S2=$93
S3=$94
H0=$95
H1=$96
H2=$97
H3=$98
T0=$99
T1=$9a
T2=$9b
T3=$9c
T4=$9d
T5=$9e
T6=$9f
EXPR_SAVE=$a0
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$c000
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
!source "caller.inc"
!source "exercise.inc"
component_start:
!source "component.inc"
!source "data.inc"
!source "cases.inc"
hex_font:
!byte 0,0,0,0,0,0,0,0,0,56,40,40,40,56,0,0,0,16,48,16,16,56,0,0,0,56,8,56,32,56,0,0,0,56,8,56,8,56,0,0,0,40,40,56,8,8,0,0,0,56,32,56,8,56,0,0,0,56,32,56,40,56,0,0,0,56,8,8,8,8,0,0,0,56,40,56,40,56,0,0,0,56,40,56,8,56,0,0,0,16,40,56,40,40,0,0,0,48,40,48,40,48,0,0,0,56,32,32,32,56,0,0,0,48,40,40,40,48,0,0,0,56,32,48,32,56,0,0,0,56,32,48,32,32,0,0
code_end:
Cost and alternatives
The measured helper extent is 578 code bytes; public state is 25 bytes and private scratch is 8 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.
At most four validation steps and four applications, each with at most three bit shifts. Compared with immediate mutation this needs queue storage and a flush phase, but traversal sees a stable object set. The existing event FIFO is smaller when only byte event IDs are needed.
This component owns occupancy and generations only, not a payload constructor or resource allocator. After a successful flush initialise newly live payloads before any object consumer runs; retain the queued commands externally if you need per-command payload data or outcomes. A generation byte wraps after256 reuses, so ancient handles require a stronger lifetime policy. Single update owner only; interrupt producers need separate synchronisation.
Execution evidence
Four variants run on PAL stock-ROM PRG, using Asm198x 0.0.72, Emu198x 0.44.2. Each executes 40 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
Generation handles: Use the same lifetime rule for references outside the command buffer and keep payload ownership explicit.