Skip to content

A Bounded Event Queue on NES

When three things happen before the game gets around to handling them, one event byte can only remember the last one.

When three things happen before the game gets around to handling them, one event byte can only remember the last one. A small FIFO keeps their order. This queue has an explicit full result, so a burst cannot silently overwrite an event that still matters.

NES shows the working result 03 03 00 33 02 01 11 22.
The completed native caller shows hexadecimal state bytes. The field order below explains the result; the full verification packet retains every call.

Include, initialise and call

Bind HEAD, TAIL, COUNT, VALUE, MODE, OK, B0, B1, B2, B3 as 10 contiguous private bytes in that order. Include the helper once. Call event_queue_init at the deliberate reset boundary: it clears all named bytes, including inputs. Set your starting values afterwards. Call event_queue_step when this service is needed.

Initialise once, then offer each event and check OK. If a push fails, choose a game policy: retry, discard a dispensable effect, or stop producing work. Do not pretend the event was queued. Pop until OK becomes zero, with a per-update work budget if producers could keep adding events.

HEAD and TAIL are indices into contiguous B0–B3. COUNT is the number of occupied entries. MODE 1 pushes VALUE, MODE 2 pops into VALUE; other modes do nothing. OK is replaced with 1 on success or 0 on rejection. A full push and an empty pop preserve the queue and VALUE. Use only state produced by init/step: indices are below QUEUE_CAP, COUNT is 0–QUEUE_CAP and TAIL equals (HEAD + COUNT) modulo capacity.

Reusable initialisation and service

6502.inc
; SPDX-License-Identifier: MIT
; A Bounded Event Queue. Bind these contiguous private bytes: HEAD, TAIL, COUNT, VALUE, MODE, OK, B0, B1, B2, B3.
; init clears the whole block; step uses current inputs. No I/O, waits or IRQ owner.
; A/X/flags may change; Y and decimal mode survive. Binary arithmetic requires D=0.
event_queue_init:
    lda #0
    sta HEAD
    sta TAIL
    sta COUNT
    sta VALUE
    sta MODE
    sta OK
    sta B0
    sta B1
    sta B2
    sta B3
    rts
event_queue_step:
    lda #0
    sta OK
    lda MODE
    cmp #1
    beq queue_push
    cmp #2
    bne queue_done
    lda COUNT
    beq queue_done
    ldx HEAD
    lda B0,x
    sta VALUE
    inx
    cpx #QUEUE_CAP
    bcc queue_pop_index
    ldx #0
queue_pop_index:
    stx HEAD
    dec COUNT
    jmp queue_success
queue_push:
    lda COUNT
    cmp #QUEUE_CAP
    bcs queue_done
    ldx TAIL
    lda VALUE
    sta B0,x
    inx
    cpx #QUEUE_CAP
    bcc queue_push_index
    ldx #0
queue_push_index:
    stx TAIL
    inc COUNT
queue_success:
    inc OK
queue_done:
    rts
component_end:

State and ownership

Six control/input/output bytes precede four reserved buffer bytes. A queue stores byte event IDs, not payloads or pointers. Popped buffer cells retain stale data; COUNT decides which entries are live. Head equals tail can mean either empty or full, so COUNT must be retained.

A/X/flags may change; Y and decimal mode survive. Clear decimal mode before binary arithmetic; the standalone caller uses CLD. A direct call pushes a two-byte return address.

This implementation has fixed symbol bindings and one mutation owner. Independent users need distinct state blocks and scoped/renamed bindings; including another copy with the same global labels is not sufficient. Sequential queries may reuse a scratch block only after the previous result has been consumed. Do not let an interrupt and the main loop mutate the same block without a separately designed ownership policy.

The standalone cartridge owns its background, CHR font and RAM layout. Its diagnostic packet is bounded below the end of the NES’s 2 KiB internal RAM; the component owns no PPU or audio registers.

A small caller you can adapt

The standalone program includes the helper and a separate client. Its read-only table exercises 96 valid inputs, then this client initialises once and retains state between calls. Replace the supplied input changes with your own game values; the display and packet machinery are diagnostics rather than dependencies of the component.

Buildable caller, bindings and include order

demo.asm
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
HEAD=$80
TAIL=$81
COUNT=$82
VALUE=$83
MODE=$84
OK=$85
B0=$86
B1=$87
B2=$88
B3=$89
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
SCORE_CAP=65535
QUEUE_CAP=4
REPEAT_DELAY=4
REPEAT_RATE=2
.include "caller.inc"
.include "exercise.inc"
component_start:
.include "component.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 separate client retaining state between calls

exercise.inc, excerpt
preview_rules:
.if CUSTOM
    lda #17
    sta VALUE
    lda #1
    sta MODE
.if BROKEN
    lda MODE
    cmp #1
    bne trace_skip_reset_0_custom
    lda #0
    sta HEAD
    lda #0
    sta TAIL
    lda #0
    sta COUNT
trace_skip_reset_0_custom:
    jsr checked_step
.else
    jsr checked_step
.endif
    jsr emit_state
    lda #34
    sta VALUE
    lda #1
    sta MODE
.if BROKEN
    lda MODE
    cmp #1
    bne trace_skip_reset_1_custom
    lda #0
    sta HEAD
    lda #0
    sta TAIL
    lda #0
    sta COUNT
trace_skip_reset_1_custom:
    jsr checked_step
.else
    jsr checked_step
.endif
    jsr emit_state
    lda #51
    sta VALUE
    lda #1
    sta MODE
.if BROKEN
    lda MODE
    cmp #1
    bne trace_skip_reset_2_custom
    lda #0
    sta HEAD
    lda #0
    sta TAIL
    lda #0
    sta COUNT
trace_skip_reset_2_custom:
    jsr checked_step
.else
    jsr checked_step
.endif
    jsr emit_state
    lda #2
    sta MODE
.if BROKEN
    lda MODE
    cmp #1
    bne trace_skip_reset_3_custom
    lda #0
    sta HEAD
    lda #0
    sta TAIL
    lda #0
    sta COUNT
trace_skip_reset_3_custom:
    jsr checked_step
.else
    jsr checked_step
.endif
    jsr emit_state
    lda #2
    sta MODE
.if BROKEN
    lda MODE
    cmp #1
    bne trace_skip_reset_4_custom
    lda #0
    sta HEAD
    lda #0
    sta TAIL
    lda #0
    sta COUNT
trace_skip_reset_4_custom:
    jsr checked_step
.else
    jsr checked_step
.endif
    jsr emit_state
    lda #2
    sta MODE
.if BROKEN
    lda MODE
    cmp #1
    bne trace_skip_reset_5_custom
    lda #0
    sta HEAD
    lda #0
    sta TAIL
    lda #0
    sta COUNT
trace_skip_reset_5_custom:
    jsr checked_step
.else
    jsr checked_step
.endif
    jsr emit_state
.else
    lda #17
    sta VALUE
    lda #1
    sta MODE
.if BROKEN
    lda MODE
    cmp #1
    bne trace_skip_reset_0
    lda #0
    sta HEAD
    lda #0
    sta TAIL
    lda #0
    sta COUNT
trace_skip_reset_0:
    jsr checked_step
.else
    jsr checked_step
.endif
    jsr emit_state
    lda #34
    sta VALUE
    lda #1
    sta MODE
.if BROKEN
    lda MODE
    cmp #1
    bne trace_skip_reset_1
    lda #0
    sta HEAD
    lda #0
    sta TAIL
    lda #0
    sta COUNT
trace_skip_reset_1:
    jsr checked_step
.else
    jsr checked_step
.endif
    jsr emit_state
    lda #51
    sta VALUE
    lda #1
    sta MODE
.if BROKEN
    lda MODE
    cmp #1
    bne trace_skip_reset_2
    lda #0
    sta HEAD
    lda #0
    sta TAIL
    lda #0
    sta COUNT
trace_skip_reset_2:
    jsr checked_step
.else
    jsr checked_step
.endif
    jsr emit_state
    lda #2
    sta MODE
.if BROKEN
    lda MODE
    cmp #1
    bne trace_skip_reset_3
    lda #0
    sta HEAD
    lda #0
    sta TAIL
    lda #0
    sta COUNT
trace_skip_reset_3:
    jsr checked_step
.else
    jsr checked_step
.endif
    jsr emit_state
    lda #2
    sta MODE
.if BROKEN
    lda MODE
    cmp #1
    bne trace_skip_reset_4
    lda #0
    sta HEAD
    lda #0
    sta TAIL
    lda #0
    sta COUNT
trace_skip_reset_4:
    jsr checked_step
.else
    jsr checked_step
.endif
    jsr emit_state
    lda #2
    sta MODE
.if BROKEN
    lda MODE
    cmp #1
    bne trace_skip_reset_5
    lda #0
    sta HEAD
    lda #0
    sta TAIL
    lda #0
    sta COUNT
trace_skip_reset_5:
    jsr checked_step
.else
    jsr checked_step
.endif
    jsr emit_state
.endif
    rts

The eight display fields follow HEAD, TAIL, COUNT, VALUE, MODE, OK, B0, B1; the full packet also retains B2, B3. Each field is a hexadecimal byte. The working row is 03 03 00 33 02 01 11 22. The output is a completed diagnostic state, not a recording of movement, input or sound hardware.

Make it your own

The custom build lowers QUEUE_CAP from four to two while retaining the four-byte storage layout. A third push is rejected, and the two original events still pop in order. This demonstrates a capacity limit rather than a RAM-saving layout change.

Maintained custom configuration or caller

custom.asm
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
HEAD=$80
TAIL=$81
COUNT=$82
VALUE=$83
MODE=$84
OK=$85
B0=$86
B1=$87
B2=$88
B3=$89
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
SCORE_CAP=999
QUEUE_CAP=2
REPEAT_DELAY=6
REPEAT_RATE=3
.include "caller.inc"
.include "exercise.inc"
component_start:
.include "component.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 observed custom row is 00 00 00 22 02 00 11 22. The retained records qualify these named configurations; a new input domain or larger layout needs its own bounds and fresh execution checks.

Common mistake and repair

The wrong client resets HEAD, TAIL and COUNT before every push. The later event replaces the history of earlier submissions. The first pop now returns 51 instead of 17. Initialise at session creation or an intentional flush, not when submitting an event.

Deliberately wrong client selection

wrong.asm
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
HEAD=$80
TAIL=$81
COUNT=$82
VALUE=$83
MODE=$84
OK=$85
B0=$86
B1=$87
B2=$88
B3=$89
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
SCORE_CAP=65535
QUEUE_CAP=4
REPEAT_DELAY=4
REPEAT_RATE=2
.include "caller.inc"
.include "exercise.inc"
component_start:
.include "component.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 fault result 01 01 00 33 02 00 33 00.
The wrong client completes and records 01 01 00 33 02 00 33 00. Compare its state with the working row and the explained failure.

The wrong branch is in the displayed client above, selected by BROKEN. repaired.asm selects the original client again. Its executable matches demo exactly; Spectrum tape filenames can differ while loaded CODE is identical. The bad caller is a labelled experiment, not an alternative recommended implementation.

Cost and verification

The measured helper occupies 87 code bytes plus 10 named state bytes in this layout. Return addresses, alignment, tables and display diagnostics are additional. The sizes cover init and step together, not the whole standalone program.

Each push/pop touches one buffer cell and a few counters, without moving the other entries. This buys constant work at the cost of indices, count and explicit overflow handling. Draining the entire queue is a caller loop and needs its own budget.

Larger queues need wider indices or a bounded capacity and updated storage. A power-of-two capacity can use a mask to wrap, but this version uses comparison so its policy is visible. An IRQ producer and foreground consumer need a separately designed single-producer/single-consumer protocol or critical section; this is a single-owner queue.

Four native runs use NTSC NES with a mapper-0 cartridge, CHR-ROM diagnostic font, NMI and DMC disabled; Asm198x 0.0.72 and Emu198x 0.44.2. Every run checks 96 table results, dirty initialisation and every state in a separate sequential trace against an independent integer model. This is boundary/varied-input qualification, not exhaustive coverage of every byte combination. Source/tool/firmware hashes, native startup and completion, preserved-register sentinels, immutable loaded code, actual helper extent, capture bytes and pixels, and exact repair are checked. Deliberately changed observations must fail. Firmware is hashed, never distributed.

Related reusable technique · Maths for Games · The Craft.

Source and native evidence.