Skip to content

Build Bounded Spatial Buckets on Spectrum

Nearby objects are worth comparing; distant ones usually are not.

Nearby objects are worth comparing; distant ones usually are not. A small bucket table can narrow that candidate list.

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

Partition a 16×16 logical area into four 8×8 buckets. Each holds four byte item identifiers in S0..S15, with counts C0..C3. MODE=0 clears the table; MODE=1 inserts ITEM at INPUT_X/Y; MODE=2 queries that bucket. Out-of-range coordinates, modes or selected counts return VALID=0 without table writes. A full insertion returns INSERTED=0, FULL=1; successful insertion reports INSERTED=1. Query COUNT and BUCKET identify the active prefix at S[4*BUCKET].

Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: BUCKET, COUNT, INSERTED, FULL, VALID, MODE, INPUT_X, INPUT_Y, ITEM, C0, C1, C2, C3, S0, S1, S2, S3, S4, S5, S6, S7, S8, S9, S10, S11, S12, S13, S14, S15. They occupy 29 contiguous bytes. Also reserve the private workspace: T0, T1, T2, EXPR_SAVE (4 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.

Call spatial_bucket_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 spatial_bucket_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 C0..C3 and S0..S15
Supply before service MODE, INPUT_X/Y and ITEM
Read after service BUCKET, COUNT, INSERTED, FULL and VALID

Two coordinate high bits select a quadrant. Four fixed slots per bucket avoid allocation; the insertion count is checked before forming the destination index. This is a native bounded companion to the existing broader collision reference, not a replacement for its geometric tests.

Reusable initialisation and service

component.inc
; SPDX-License-Identifier: MIT
; Build Bounded Spatial Buckets. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
spatial_bucket_init:
    ld a,0
    ld (BUCKET),a
    ld (COUNT),a
    ld (INSERTED),a
    ld (FULL),a
    ld (VALID),a
    ld (MODE),a
    ld (INPUT_X),a
    ld (INPUT_Y),a
    ld (ITEM),a
    ld (C0),a
    ld (C1),a
    ld (C2),a
    ld (C3),a
    ld (S0),a
    ld (S1),a
    ld (S2),a
    ld (S3),a
    ld (S4),a
    ld (S5),a
    ld (S6),a
    ld (S7),a
    ld (S8),a
    ld (S9),a
    ld (S10),a
    ld (S11),a
    ld (S12),a
    ld (S13),a
    ld (S14),a
    ld (S15),a
    ld (T0),a
    ld (T1),a
    ld (T2),a
    ld (EXPR_SAVE),a
    ret
spatial_bucket_step:
; BUCKET = 255
    ld a,255
    ld (BUCKET),a
; COUNT = 0
    ld a,0
    ld (COUNT),a
; INSERTED = 0
    ld a,0
    ld (INSERTED),a
; FULL = 0
    ld a,0
    ld (FULL),a
; VALID = 0
    ld a,0
    ld (VALID),a
; if MODE > 2:
    ld a,(MODE)
    cp 2
    jp z,spatial_bucket_L2
    jp nc,spatial_bucket_L1
    jp spatial_bucket_L2
spatial_bucket_L1:
; return
    ret
    jp spatial_bucket_L3
spatial_bucket_L2:
spatial_bucket_L3:
; if MODE == 0:
    ld a,(MODE)
    cp 0
    jp z,spatial_bucket_L4
    jp spatial_bucket_L5
spatial_bucket_L4:
; T0 = 0
    ld a,0
    ld (T0),a
; while T0 < 4:
spatial_bucket_L7:
    ld a,(T0)
    cp 4
    jp c,spatial_bucket_L8
    jp spatial_bucket_L9
spatial_bucket_L8:
; C[T0] = 0
    ld a,0
    ld c,a
    ld a,(T0)
    ld e,a
    ld d,0
    ld hl,C0
    add hl,de
    ld (hl),c
; T0 = T0 + 1
    ld a,(T0)
    add a,1
    ld (T0),a
    jp spatial_bucket_L7
spatial_bucket_L9:
; T0 = 0
    ld a,0
    ld (T0),a
; while T0 < 16:
spatial_bucket_L10:
    ld a,(T0)
    cp 16
    jp c,spatial_bucket_L11
    jp spatial_bucket_L12
spatial_bucket_L11:
; S[T0] = 0
    ld a,0
    ld c,a
    ld a,(T0)
    ld e,a
    ld d,0
    ld hl,S0
    add hl,de
    ld (hl),c
; T0 = T0 + 1
    ld a,(T0)
    add a,1
    ld (T0),a
    jp spatial_bucket_L10
spatial_bucket_L12:
; VALID = 1
    ld a,1
    ld (VALID),a
; return
    ret
    jp spatial_bucket_L6
spatial_bucket_L5:
spatial_bucket_L6:
; if INPUT_X >= 16:
    ld a,(INPUT_X)
    cp 16
    jp nc,spatial_bucket_L13
    jp spatial_bucket_L14
spatial_bucket_L13:
; return
    ret
    jp spatial_bucket_L15
spatial_bucket_L14:
spatial_bucket_L15:
; if INPUT_Y >= 16:
    ld a,(INPUT_Y)
    cp 16
    jp nc,spatial_bucket_L16
    jp spatial_bucket_L17
spatial_bucket_L16:
; return
    ret
    jp spatial_bucket_L18
spatial_bucket_L17:
spatial_bucket_L18:
; T0 = INPUT_X >> 3
    ld a,(INPUT_X)
    srl a
    srl a
    srl a
    ld (T0),a
; T1 = INPUT_Y >> 3
    ld a,(INPUT_Y)
    srl a
    srl a
    srl a
    ld (T1),a
; T1 = T1 << 1
    ld a,(T1)
    sla a
    ld (T1),a
; T0 = T0 + T1
    ld a,(T0)
    ld b,a
    ld a,(T1)
    ld c,a
    ld a,b
    add a,c
    ld (T0),a
; T1 = C[T0]
    ld a,(T0)
    ld e,a
    ld d,0
    ld hl,C0
    add hl,de
    ld a,(hl)
    ld (T1),a
; if T1 > 4:
    ld a,(T1)
    cp 4
    jp z,spatial_bucket_L20
    jp nc,spatial_bucket_L19
    jp spatial_bucket_L20
spatial_bucket_L19:
; return
    ret
    jp spatial_bucket_L21
spatial_bucket_L20:
spatial_bucket_L21:
; BUCKET = T0
    ld a,(T0)
    ld (BUCKET),a
; COUNT = T1
    ld a,(T1)
    ld (COUNT),a
; VALID = 1
    ld a,1
    ld (VALID),a
; if MODE == 1:
    ld a,(MODE)
    cp 1
    jp z,spatial_bucket_L22
    jp spatial_bucket_L23
spatial_bucket_L22:
; if T1 < 4:
    ld a,(T1)
    cp 4
    jp c,spatial_bucket_L25
    jp spatial_bucket_L26
spatial_bucket_L25:
; T2 = T0 << 2
    ld a,(T0)
    sla a
    sla a
    ld (T2),a
; T2 = T2 + T1
    ld a,(T2)
    ld b,a
    ld a,(T1)
    ld c,a
    ld a,b
    add a,c
    ld (T2),a
; S[T2] = ITEM
    ld a,(ITEM)
    ld c,a
    ld a,(T2)
    ld e,a
    ld d,0
    ld hl,S0
    add hl,de
    ld (hl),c
; T1 = T1 + 1
    ld a,(T1)
    add a,1
    ld (T1),a
; C[T0] = T1
    ld a,(T1)
    ld c,a
    ld a,(T0)
    ld e,a
    ld d,0
    ld hl,C0
    add hl,de
    ld (hl),c
; COUNT = T1
    ld a,(T1)
    ld (COUNT),a
; INSERTED = 1
    ld a,1
    ld (INSERTED),a
    jp spatial_bucket_L27
spatial_bucket_L26:
spatial_bucket_L27:
    jp spatial_bucket_L24
spatial_bucket_L23:
spatial_bucket_L24:
; if COUNT == 4:
    ld a,(COUNT)
    cp 4
    jp z,spatial_bucket_L28
    jp spatial_bucket_L29
spatial_bucket_L28:
; FULL = 1
    ld a,1
    ld (FULL),a
    jp spatial_bucket_L30
spatial_bucket_L29:
spatial_bucket_L30:
    ret
component_end:

Machine contract

AF/BC/DE/HL may change; IX/IY are preserved. A direct CALL 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 48K diagnostic boots through ROM tape loading and USR, disables interrupts and owns the screen. The helper performs no port or screen writes.

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
    org $8000
CUSTOM equ 0
BROKEN equ 0
COYOTE_STEPS equ 4
BUFFER_STEPS equ 4
TEXT_CAP equ 8
PROPERTY_COUNT equ 16
STREAM_BYTES equ 13
CRC_POLY equ 7
    include "caller.inc"
    include "exercise.inc"
component_start:
    include "component.inc"
    include "data.inc"
    include "cases.inc"
    include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
BUCKET: defb 0
COUNT: defb 0
INSERTED: defb 0
FULL: defb 0
VALID: defb 0
MODE: defb 0
INPUT_X: defb 0
INPUT_Y: defb 0
ITEM: defb 0
C0: defb 0
C1: defb 0
C2: defb 0
C3: defb 0
S0: defb 0
S1: defb 0
S2: defb 0
S3: defb 0
S4: defb 0
S5: defb 0
S6: defb 0
S7: defb 0
S8: defb 0
S9: defb 0
S10: defb 0
S11: defb 0
S12: defb 0
S13: defb 0
S14: defb 0
S15: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
EXPR_SAVE: 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 client retaining state between calls

exercise.inc, excerpt
preview_rules:
    if CUSTOM
; MODE = 1
    ld a,1
    ld (MODE),a
; INPUT_X = 15
    ld a,15
    ld (INPUT_X),a
; INPUT_Y = 15
    ld a,15
    ld (INPUT_Y),a
; ITEM = 255
    ld a,255
    ld (ITEM),a
    if BROKEN
; MODE = 0
    ld a,0
    ld (MODE),a
    endif
    call checked_step
    call emit_state
; ITEM = 1
    ld a,1
    ld (ITEM),a
    if BROKEN
; MODE = 0
    ld a,0
    ld (MODE),a
    endif
    call checked_step
    call emit_state
; ITEM = 2
    ld a,2
    ld (ITEM),a
    if BROKEN
; MODE = 0
    ld a,0
    ld (MODE),a
    endif
    call checked_step
    call emit_state
; ITEM = 3
    ld a,3
    ld (ITEM),a
    if BROKEN
; MODE = 0
    ld a,0
    ld (MODE),a
    endif
    call checked_step
    call emit_state
; ITEM = 4
    ld a,4
    ld (ITEM),a
    if BROKEN
; MODE = 0
    ld a,0
    ld (MODE),a
    endif
    call checked_step
    call emit_state
    ret
    else
; MODE = 1
    ld a,1
    ld (MODE),a
; INPUT_X = 2
    ld a,2
    ld (INPUT_X),a
; INPUT_Y = 3
    ld a,3
    ld (INPUT_Y),a
; ITEM = 7
    ld a,7
    ld (ITEM),a
    if BROKEN
; MODE = 0
    ld a,0
    ld (MODE),a
    endif
    call checked_step
    call emit_state
; ITEM = 8
    ld a,8
    ld (ITEM),a
    if BROKEN
; MODE = 0
    ld a,0
    ld (MODE),a
    endif
    call checked_step
    call emit_state
; MODE = 2
    ld a,2
    ld (MODE),a
    if BROKEN
; MODE = 0
    ld a,0
    ld (MODE),a
    endif
    call checked_step
    call emit_state
    endif
    ret

Customise it

Fill the bottom-right bucket, then attempt one overflow insertion. 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
    org $8000
CUSTOM equ 1
BROKEN equ 0
COYOTE_STEPS equ 2
BUFFER_STEPS equ 2
TEXT_CAP equ 4
PROPERTY_COUNT equ 16
STREAM_BYTES equ 13
CRC_POLY equ 49
    include "caller.inc"
    include "exercise.inc"
component_start:
    include "component.inc"
    include "data.inc"
    include "cases.inc"
    include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
BUCKET: defb 0
COUNT: defb 0
INSERTED: defb 0
FULL: defb 0
VALID: defb 0
MODE: defb 0
INPUT_X: defb 0
INPUT_Y: defb 0
ITEM: defb 0
C0: defb 0
C1: defb 0
C2: defb 0
C3: defb 0
S0: defb 0
S1: defb 0
S2: defb 0
S3: defb 0
S4: defb 0
S5: defb 0
S6: defb 0
S7: defb 0
S8: defb 0
S9: defb 0
S10: defb 0
S11: defb 0
S12: defb 0
S13: defb 0
S14: defb 0
S15: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
EXPR_SAVE: 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 custom diagnostic row is 03 04 00 01 01 01 0F 0F. The full client trace in the source explains how it gets there.

Mistake and repair

The wrong caller selects clear instead of insert/query. Counts remain zero and candidates disappear. Clear once at the intended rebuild boundary, then retain the populated buckets for all queries.

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
    org $8000
CUSTOM equ 0
BROKEN equ 1
COYOTE_STEPS equ 4
BUFFER_STEPS equ 4
TEXT_CAP equ 8
PROPERTY_COUNT equ 16
STREAM_BYTES equ 13
CRC_POLY equ 7
    include "caller.inc"
    include "exercise.inc"
component_start:
    include "component.inc"
    include "data.inc"
    include "cases.inc"
    include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
BUCKET: defb 0
COUNT: defb 0
INSERTED: defb 0
FULL: defb 0
VALID: defb 0
MODE: defb 0
INPUT_X: defb 0
INPUT_Y: defb 0
ITEM: defb 0
C0: defb 0
C1: defb 0
C2: defb 0
C3: defb 0
S0: defb 0
S1: defb 0
S2: defb 0
S3: defb 0
S4: defb 0
S5: defb 0
S6: defb 0
S7: defb 0
S8: defb 0
S9: defb 0
S10: defb 0
S11: defb 0
S12: defb 0
S13: defb 0
S14: defb 0
S15: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
EXPR_SAVE: 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 error row FF 00 00 00 01 00 02 03.
The wrong client completes with FF 00 00 00 01 00 02 03; its changed inputs and state explain the result.

Cost and alternatives

The measured helper extent is 491 code bytes; public state is 29 bytes and private scratch is 4 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.

Clear touches twenty table bytes; insert/query takes bounded arithmetic and one selected count read. More buckets reduce candidate density but cost counts/storage, while larger bucket capacity increases worst-case pair testing.

This table stores point membership, not shape overlap. Objects spanning buckets require insertion into every covered bucket or neighbouring-bucket queries; otherwise broad-phase false negatives are possible. Duplicate identifiers are permitted, and a full bucket needs an explicit game policy rather than silently dropping possible collisions.

Execution evidence

Four variants run on 48K PAL ROM tape load and USR, using Asm198x 0.0.72, Emu198x 0.44.2. Each executes 34 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

Rectangle overlap: Use the bucket as a broad phase and apply an actual shape test to candidate pairs.

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