Skip to content

Service a Small Spawn Timeline on NES

A wave can have deliberate pauses without burying every spawn in the main loop.

A wave can have deliberate pauses without burying every spawn in the main loop.

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

COUNT is 1–4 and INDEX must be below it. E0–E3 are event IDs and L0–L3 are delay ticks before each event. WAIT is retained elapsed delay. Each call with WAIT below the current delay increments it without firing; otherwise emit that event, reset WAIT and advance INDEX. At the final event DONE latches. At most one event fires per call, even when delays are zero.

Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: EVENT, FIRED, INDEX, WAIT, COUNT, DONE, VALID, E0, E1, E2, E3, L0, L1, L2, L3. They occupy 15 contiguous bytes. Also reserve the private workspace: T0 (1 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.

Call spawn_timeline_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 spawn_timeline_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 INDEX, WAIT and DONE
Supply before service COUNT, E0–E3 and L0–L3
Read after service EVENT, FIRED and VALID

A cursor selects the event and its delay. Emitting at most one event bounds per-update work and makes zero-delay entries consecutive updates rather than an unbounded burst.

Reusable initialisation and service

6502.inc
; SPDX-License-Identifier: MIT
; Service a Small Spawn Timeline. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
spawn_timeline_init:
    lda #0
    sta EVENT
    sta FIRED
    sta INDEX
    sta WAIT
    sta COUNT
    sta DONE
    sta VALID
    sta E0
    sta E1
    sta E2
    sta E3
    sta L0
    sta L1
    sta L2
    sta L3
    sta T0
    rts
spawn_timeline_step:
; EVENT = 255
    lda #255
    sta EVENT
; FIRED = 0
    lda #0
    sta FIRED
; VALID = 0
    lda #0
    sta VALID
; if COUNT == 0:
    lda COUNT
    cmp #0
    beq spawn_timeline_L4
spawn_timeline_L5:
    jmp spawn_timeline_L2
spawn_timeline_L4:
    jmp spawn_timeline_L1
spawn_timeline_L1:
; return
    rts
    jmp spawn_timeline_L3
spawn_timeline_L2:
spawn_timeline_L3:
; if COUNT > 4:
    lda COUNT
    cmp #4
    beq spawn_timeline_L10
    bcs spawn_timeline_L9
spawn_timeline_L10:
    jmp spawn_timeline_L7
spawn_timeline_L9:
    jmp spawn_timeline_L6
spawn_timeline_L6:
; return
    rts
    jmp spawn_timeline_L8
spawn_timeline_L7:
spawn_timeline_L8:
; if INDEX >= COUNT:
    lda INDEX
    cmp COUNT
    bcs spawn_timeline_L14
spawn_timeline_L15:
    jmp spawn_timeline_L12
spawn_timeline_L14:
    jmp spawn_timeline_L11
spawn_timeline_L11:
; return
    rts
    jmp spawn_timeline_L13
spawn_timeline_L12:
spawn_timeline_L13:
; VALID = 1
    lda #1
    sta VALID
; if DONE != 0:
    lda DONE
    cmp #0
    bne spawn_timeline_L19
spawn_timeline_L20:
    jmp spawn_timeline_L17
spawn_timeline_L19:
    jmp spawn_timeline_L16
spawn_timeline_L16:
; return
    rts
    jmp spawn_timeline_L18
spawn_timeline_L17:
spawn_timeline_L18:
; T0 = L[INDEX]
    ldx INDEX
    lda L0,x
    sta T0
; if WAIT < T0:
    lda WAIT
    cmp T0
    bcc spawn_timeline_L24
spawn_timeline_L25:
    jmp spawn_timeline_L22
spawn_timeline_L24:
    jmp spawn_timeline_L21
spawn_timeline_L21:
; WAIT = WAIT + 1
    lda WAIT
    clc
    adc #1
    sta WAIT
; return
    rts
    jmp spawn_timeline_L23
spawn_timeline_L22:
spawn_timeline_L23:
; EVENT = E[INDEX]
    ldx INDEX
    lda E0,x
    sta EVENT
; FIRED = 1
    lda #1
    sta FIRED
; WAIT = 0
    lda #0
    sta WAIT
; T0 = COUNT - 1
    lda COUNT
    sec
    sbc #1
    sta T0
; if INDEX < T0:
    lda INDEX
    cmp T0
    bcc spawn_timeline_L29
spawn_timeline_L30:
    jmp spawn_timeline_L27
spawn_timeline_L29:
    jmp spawn_timeline_L26
spawn_timeline_L26:
; INDEX = INDEX + 1
    lda INDEX
    clc
    adc #1
    sta INDEX
    jmp spawn_timeline_L28
spawn_timeline_L27:
; DONE = 1
    lda #1
    sta DONE
spawn_timeline_L28:
    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

demo.asm
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
EVENT=$80
FIRED=$81
INDEX=$82
WAIT=$83
COUNT=$84
DONE=$85
VALID=$86
E0=$87
E1=$88
E2=$89
E3=$8a
L0=$8b
L1=$8c
L2=$8d
L3=$8e
T0=$8f
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,0

A client retaining state between calls

exercise.inc, excerpt
preview_rules:
.if CUSTOM
; COUNT = 2
    lda #2
    sta COUNT
; E0 = 9
    lda #9
    sta E0
; E1 = 3
    lda #3
    sta E1
; L0 = 1
    lda #1
    sta L0
; L1 = 0
    lda #0
    sta L1
.if BROKEN
; INDEX = 0
    lda #0
    sta INDEX
.endif
    jsr checked_step
    jsr emit_state

.if BROKEN
; INDEX = 0
    lda #0
    sta INDEX
.endif
    jsr checked_step
    jsr emit_state

.if BROKEN
; INDEX = 0
    lda #0
    sta INDEX
.endif
    jsr checked_step
    jsr emit_state

.if BROKEN
; INDEX = 0
    lda #0
    sta INDEX
.endif
    jsr checked_step
    jsr emit_state
    rts
.else
; COUNT = 3
    lda #3
    sta COUNT
; E0 = 2
    lda #2
    sta E0
; E1 = 4
    lda #4
    sta E1
; E2 = 7
    lda #7
    sta E2
; L0 = 0
    lda #0
    sta L0
; L1 = 2
    lda #2
    sta L1
; L2 = 1
    lda #1
    sta L2
.if BROKEN
; INDEX = 0
    lda #0
    sta INDEX
.endif
    jsr checked_step
    jsr emit_state

.if BROKEN
; INDEX = 0
    lda #0
    sta INDEX
.endif
    jsr checked_step
    jsr emit_state

.if BROKEN
; INDEX = 0
    lda #0
    sta INDEX
.endif
    jsr checked_step
    jsr emit_state

.if BROKEN
; INDEX = 0
    lda #0
    sta INDEX
.endif
    jsr checked_step
    jsr emit_state

.if BROKEN
; INDEX = 0
    lda #0
    sta INDEX
.endif
    jsr checked_step
    jsr emit_state

.if BROKEN
; INDEX = 0
    lda #0
    sta INDEX
.endif
    jsr checked_step
    jsr emit_state

.if BROKEN
; INDEX = 0
    lda #0
    sta INDEX
.endif
    jsr checked_step
    jsr emit_state
.endif
    rts

Customise it

Use two different events and move the opening delay. 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
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
EVENT=$80
FIRED=$81
INDEX=$82
WAIT=$83
COUNT=$84
DONE=$85
VALID=$86
E0=$87
E1=$88
E2=$89
E3=$8a
L0=$8b
L1=$8c
L2=$8d
L3=$8e
T0=$8f
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,0

The custom diagnostic row is FF 00 01 00 02 01 01 09. The full client trace in the source explains how it gets there.

Mistake and repair

The wrong caller rewinds INDEX each update, so the first event keeps firing and the rest of the wave never arrives. Retain the cursor and waiting state until a deliberate timeline restart.

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
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
EVENT=$80
FIRED=$81
INDEX=$82
WAIT=$83
COUNT=$84
DONE=$85
VALID=$86
E0=$87
E1=$88
E2=$89
E3=$8a
L0=$8b
L1=$8c
L2=$8d
L3=$8e
T0=$8f
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
NES shows the deliberate error row 02 01 01 00 03 00 01 02.
The wrong client completes with 02 01 01 00 03 00 01 02; its changed inputs and state explain the result.

Cost and alternatives

The measured helper extent is 194 code bytes; public state is 15 bytes and private scratch is 1 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.

Constant work per tick with four caller-owned event/delay pairs. A larger asset-backed timeline needs bounds checks on its record count and a policy for reading data.

Events are requests: the caller owns actual spawning, pool capacity and what to do when an event cannot be accepted. This helper advances after emission, so a reliable deferred-spawn policy must retain or queue rejected work explicitly.

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

Event queues: A timeline can emit into a queue when the spawn consumer runs at a different point in the frame.

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