Skip to content

A Bounded Event Queue on Spectrum

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.

Spectrum 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

component.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.
; AF/BC/DE/HL may change; IX/IY survive.
event_queue_init:
    xor a
    ld (HEAD),a
    ld (TAIL),a
    ld (COUNT),a
    ld (VALUE),a
    ld (MODE),a
    ld (OK),a
    ld (B0),a
    ld (B1),a
    ld (B2),a
    ld (B3),a
    ret
event_queue_step:
    xor a
    ld (OK),a
    ld a,(MODE)
    cp 1
    jr z,queue_push
    cp 2
    ret nz
    ld a,(COUNT)
    or a
    ret z
    dec a
    ld (COUNT),a
    ld a,(HEAD)
    ld e,a
    ld d,0
    ld hl,B0
    add hl,de
    ld a,(hl)
    ld (VALUE),a
    ld a,e
    inc a
    cp QUEUE_CAP
    jr c,queue_pop_index
    xor a
queue_pop_index:
    ld (HEAD),a
    jr queue_success
queue_push:
    ld a,(COUNT)
    cp QUEUE_CAP
    ret nc
    inc a
    ld (COUNT),a
    ld a,(TAIL)
    ld e,a
    ld d,0
    ld hl,B0
    add hl,de
    ld a,(VALUE)
    ld (hl),a
    ld a,e
    inc a
    cp QUEUE_CAP
    jr c,queue_push_index
    xor a
queue_push_index:
    ld (TAIL),a
queue_success:
    ld a,1
    ld (OK),a
    ret
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.

AF/BC/DE/HL may change; IX/IY survive. A direct CALL uses two return-address stack bytes.

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 tape program starts at $8000, disables interrupts and owns bitmap/attribute RAM. It does not return to BASIC. The helper itself never disables interrupts or touches the display.

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
    org $8000
CUSTOM equ 0
BROKEN equ 0
SCORE_CAP equ 65535
QUEUE_CAP equ 4
REPEAT_DELAY equ 4
REPEAT_RATE equ 2
    include "caller.inc"
    include "exercise.inc"
component_start:
    include "component.inc"
    include "cases.inc"
    include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
HEAD: defb 0
TAIL: defb 0
COUNT: defb 0
VALUE: defb 0
MODE: defb 0
OK: defb 0
B0: defb 0
B1: defb 0
B2: defb 0
B3: defb 0
input_ptr: defw 0
output_ptr: defw 0
cases_left: defb 0
ready: defb 0
preview: defs 8,0
OUTPUT: defs 1300,0
    end start

A separate client retaining state between calls

exercise.inc, excerpt
preview_rules:
    if CUSTOM
    ld a,17
    ld (VALUE),a
    ld a,1
    ld (MODE),a
    if BROKEN
    ld a,(MODE)
    cp 1
    jr nz,trace_skip_reset_0_custom
    ld a,0
    ld (HEAD),a
    ld a,0
    ld (TAIL),a
    ld a,0
    ld (COUNT),a
trace_skip_reset_0_custom:
    call checked_step
    else
    call checked_step
    endif
    call emit_state
    ld a,34
    ld (VALUE),a
    ld a,1
    ld (MODE),a
    if BROKEN
    ld a,(MODE)
    cp 1
    jr nz,trace_skip_reset_1_custom
    ld a,0
    ld (HEAD),a
    ld a,0
    ld (TAIL),a
    ld a,0
    ld (COUNT),a
trace_skip_reset_1_custom:
    call checked_step
    else
    call checked_step
    endif
    call emit_state
    ld a,51
    ld (VALUE),a
    ld a,1
    ld (MODE),a
    if BROKEN
    ld a,(MODE)
    cp 1
    jr nz,trace_skip_reset_2_custom
    ld a,0
    ld (HEAD),a
    ld a,0
    ld (TAIL),a
    ld a,0
    ld (COUNT),a
trace_skip_reset_2_custom:
    call checked_step
    else
    call checked_step
    endif
    call emit_state
    ld a,2
    ld (MODE),a
    if BROKEN
    ld a,(MODE)
    cp 1
    jr nz,trace_skip_reset_3_custom
    ld a,0
    ld (HEAD),a
    ld a,0
    ld (TAIL),a
    ld a,0
    ld (COUNT),a
trace_skip_reset_3_custom:
    call checked_step
    else
    call checked_step
    endif
    call emit_state
    ld a,2
    ld (MODE),a
    if BROKEN
    ld a,(MODE)
    cp 1
    jr nz,trace_skip_reset_4_custom
    ld a,0
    ld (HEAD),a
    ld a,0
    ld (TAIL),a
    ld a,0
    ld (COUNT),a
trace_skip_reset_4_custom:
    call checked_step
    else
    call checked_step
    endif
    call emit_state
    ld a,2
    ld (MODE),a
    if BROKEN
    ld a,(MODE)
    cp 1
    jr nz,trace_skip_reset_5_custom
    ld a,0
    ld (HEAD),a
    ld a,0
    ld (TAIL),a
    ld a,0
    ld (COUNT),a
trace_skip_reset_5_custom:
    call checked_step
    else
    call checked_step
    endif
    call emit_state
    else
    ld a,17
    ld (VALUE),a
    ld a,1
    ld (MODE),a
    if BROKEN
    ld a,(MODE)
    cp 1
    jr nz,trace_skip_reset_0
    ld a,0
    ld (HEAD),a
    ld a,0
    ld (TAIL),a
    ld a,0
    ld (COUNT),a
trace_skip_reset_0:
    call checked_step
    else
    call checked_step
    endif
    call emit_state
    ld a,34
    ld (VALUE),a
    ld a,1
    ld (MODE),a
    if BROKEN
    ld a,(MODE)
    cp 1
    jr nz,trace_skip_reset_1
    ld a,0
    ld (HEAD),a
    ld a,0
    ld (TAIL),a
    ld a,0
    ld (COUNT),a
trace_skip_reset_1:
    call checked_step
    else
    call checked_step
    endif
    call emit_state
    ld a,51
    ld (VALUE),a
    ld a,1
    ld (MODE),a
    if BROKEN
    ld a,(MODE)
    cp 1
    jr nz,trace_skip_reset_2
    ld a,0
    ld (HEAD),a
    ld a,0
    ld (TAIL),a
    ld a,0
    ld (COUNT),a
trace_skip_reset_2:
    call checked_step
    else
    call checked_step
    endif
    call emit_state
    ld a,2
    ld (MODE),a
    if BROKEN
    ld a,(MODE)
    cp 1
    jr nz,trace_skip_reset_3
    ld a,0
    ld (HEAD),a
    ld a,0
    ld (TAIL),a
    ld a,0
    ld (COUNT),a
trace_skip_reset_3:
    call checked_step
    else
    call checked_step
    endif
    call emit_state
    ld a,2
    ld (MODE),a
    if BROKEN
    ld a,(MODE)
    cp 1
    jr nz,trace_skip_reset_4
    ld a,0
    ld (HEAD),a
    ld a,0
    ld (TAIL),a
    ld a,0
    ld (COUNT),a
trace_skip_reset_4:
    call checked_step
    else
    call checked_step
    endif
    call emit_state
    ld a,2
    ld (MODE),a
    if BROKEN
    ld a,(MODE)
    cp 1
    jr nz,trace_skip_reset_5
    ld a,0
    ld (HEAD),a
    ld a,0
    ld (TAIL),a
    ld a,0
    ld (COUNT),a
trace_skip_reset_5:
    call checked_step
    else
    call checked_step
    endif
    call emit_state
    endif
    ret

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
    org $8000
CUSTOM equ 1
BROKEN equ 0
SCORE_CAP equ 999
QUEUE_CAP equ 2
REPEAT_DELAY equ 6
REPEAT_RATE equ 3
    include "caller.inc"
    include "exercise.inc"
component_start:
    include "component.inc"
    include "cases.inc"
    include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
HEAD: defb 0
TAIL: defb 0
COUNT: defb 0
VALUE: defb 0
MODE: defb 0
OK: defb 0
B0: defb 0
B1: defb 0
B2: defb 0
B3: defb 0
input_ptr: defw 0
output_ptr: defw 0
cases_left: defb 0
ready: defb 0
preview: defs 8,0
OUTPUT: defs 1300,0
    end start

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
    org $8000
CUSTOM equ 0
BROKEN equ 1
SCORE_CAP equ 65535
QUEUE_CAP equ 4
REPEAT_DELAY equ 4
REPEAT_RATE equ 2
    include "caller.inc"
    include "exercise.inc"
component_start:
    include "component.inc"
    include "cases.inc"
    include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
HEAD: defb 0
TAIL: defb 0
COUNT: defb 0
VALUE: defb 0
MODE: defb 0
OK: defb 0
B0: defb 0
B1: defb 0
B2: defb 0
B3: defb 0
input_ptr: defw 0
output_ptr: defw 0
cases_left: defb 0
ready: defb 0
preview: defs 8,0
OUTPUT: defs 1300,0
    end start
Spectrum 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 121 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 Spectrum 48K with stock ROM tape loading and USR entry; 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.