Skip to content

Build Bounded Spatial Buckets on Amiga

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.

Amiga 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:
    move.b #0,d0
    move.b d0,BUCKET
    move.b d0,COUNT
    move.b d0,INSERTED
    move.b d0,FULL
    move.b d0,VALID
    move.b d0,MODE
    move.b d0,INPUT_X
    move.b d0,INPUT_Y
    move.b d0,ITEM
    move.b d0,C0
    move.b d0,C1
    move.b d0,C2
    move.b d0,C3
    move.b d0,S0
    move.b d0,S1
    move.b d0,S2
    move.b d0,S3
    move.b d0,S4
    move.b d0,S5
    move.b d0,S6
    move.b d0,S7
    move.b d0,S8
    move.b d0,S9
    move.b d0,S10
    move.b d0,S11
    move.b d0,S12
    move.b d0,S13
    move.b d0,S14
    move.b d0,S15
    move.b d0,T0
    move.b d0,T1
    move.b d0,T2
    move.b d0,EXPR_SAVE
    rts
spatial_bucket_step:
; BUCKET = 255
    move.b #255,d0
    move.b d0,BUCKET
; COUNT = 0
    move.b #0,d0
    move.b d0,COUNT
; INSERTED = 0
    move.b #0,d0
    move.b d0,INSERTED
; FULL = 0
    move.b #0,d0
    move.b d0,FULL
; VALID = 0
    move.b #0,d0
    move.b d0,VALID
; if MODE > 2:
    move.b MODE,d0
    cmp.b #2,d0
    bhi spatial_bucket_L1
    jmp spatial_bucket_L2
spatial_bucket_L1:
; return
    rts
    jmp spatial_bucket_L3
spatial_bucket_L2:
spatial_bucket_L3:
; if MODE == 0:
    move.b MODE,d0
    cmp.b #0,d0
    beq spatial_bucket_L4
    jmp spatial_bucket_L5
spatial_bucket_L4:
; T0 = 0
    move.b #0,d0
    move.b d0,T0
; while T0 < 4:
spatial_bucket_L7:
    move.b T0,d0
    cmp.b #4,d0
    bcs spatial_bucket_L8
    jmp spatial_bucket_L9
spatial_bucket_L8:
; C[T0] = 0
    move.b #0,d0
    move.b d0,d2
    moveq #0,d1
    move.b T0,d1
    lea C0,a0
    move.b d2,0(a0,d1.w)
; T0 = T0 + 1
    move.b T0,d0
    add.b #1,d0
    move.b d0,T0
    jmp spatial_bucket_L7
spatial_bucket_L9:
; T0 = 0
    move.b #0,d0
    move.b d0,T0
; while T0 < 16:
spatial_bucket_L10:
    move.b T0,d0
    cmp.b #16,d0
    bcs spatial_bucket_L11
    jmp spatial_bucket_L12
spatial_bucket_L11:
; S[T0] = 0
    move.b #0,d0
    move.b d0,d2
    moveq #0,d1
    move.b T0,d1
    lea S0,a0
    move.b d2,0(a0,d1.w)
; T0 = T0 + 1
    move.b T0,d0
    add.b #1,d0
    move.b d0,T0
    jmp spatial_bucket_L10
spatial_bucket_L12:
; VALID = 1
    move.b #1,d0
    move.b d0,VALID
; return
    rts
    jmp spatial_bucket_L6
spatial_bucket_L5:
spatial_bucket_L6:
; if INPUT_X >= 16:
    move.b INPUT_X,d0
    cmp.b #16,d0
    bcc spatial_bucket_L13
    jmp spatial_bucket_L14
spatial_bucket_L13:
; return
    rts
    jmp spatial_bucket_L15
spatial_bucket_L14:
spatial_bucket_L15:
; if INPUT_Y >= 16:
    move.b INPUT_Y,d0
    cmp.b #16,d0
    bcc spatial_bucket_L16
    jmp spatial_bucket_L17
spatial_bucket_L16:
; return
    rts
    jmp spatial_bucket_L18
spatial_bucket_L17:
spatial_bucket_L18:
; T0 = INPUT_X >> 3
    move.b INPUT_X,d0
    lsr.b #3,d0
    move.b d0,T0
; T1 = INPUT_Y >> 3
    move.b INPUT_Y,d0
    lsr.b #3,d0
    move.b d0,T1
; T1 = T1 << 1
    move.b T1,d0
    lsl.b #1,d0
    move.b d0,T1
; T0 = T0 + T1
    move.b T0,d0
    move.b T1,d1
    add.b d1,d0
    move.b d0,T0
; T1 = C[T0]
    moveq #0,d1
    move.b T0,d1
    lea C0,a0
    move.b 0(a0,d1.w),d0
    move.b d0,T1
; if T1 > 4:
    move.b T1,d0
    cmp.b #4,d0
    bhi spatial_bucket_L19
    jmp spatial_bucket_L20
spatial_bucket_L19:
; return
    rts
    jmp spatial_bucket_L21
spatial_bucket_L20:
spatial_bucket_L21:
; BUCKET = T0
    move.b T0,d0
    move.b d0,BUCKET
; COUNT = T1
    move.b T1,d0
    move.b d0,COUNT
; VALID = 1
    move.b #1,d0
    move.b d0,VALID
; if MODE == 1:
    move.b MODE,d0
    cmp.b #1,d0
    beq spatial_bucket_L22
    jmp spatial_bucket_L23
spatial_bucket_L22:
; if T1 < 4:
    move.b T1,d0
    cmp.b #4,d0
    bcs spatial_bucket_L25
    jmp spatial_bucket_L26
spatial_bucket_L25:
; T2 = T0 << 2
    move.b T0,d0
    lsl.b #2,d0
    move.b d0,T2
; T2 = T2 + T1
    move.b T2,d0
    move.b T1,d1
    add.b d1,d0
    move.b d0,T2
; S[T2] = ITEM
    move.b ITEM,d0
    move.b d0,d2
    moveq #0,d1
    move.b T2,d1
    lea S0,a0
    move.b d2,0(a0,d1.w)
; T1 = T1 + 1
    move.b T1,d0
    add.b #1,d0
    move.b d0,T1
; C[T0] = T1
    move.b T1,d0
    move.b d0,d2
    moveq #0,d1
    move.b T0,d1
    lea C0,a0
    move.b d2,0(a0,d1.w)
; COUNT = T1
    move.b T1,d0
    move.b d0,COUNT
; INSERTED = 1
    move.b #1,d0
    move.b d0,INSERTED
    jmp spatial_bucket_L27
spatial_bucket_L26:
spatial_bucket_L27:
    jmp spatial_bucket_L24
spatial_bucket_L23:
spatial_bucket_L24:
; if COUNT == 4:
    move.b COUNT,d0
    cmp.b #4,d0
    beq spatial_bucket_L28
    jmp spatial_bucket_L29
spatial_bucket_L28:
; FULL = 1
    move.b #1,d0
    move.b d0,FULL
    jmp spatial_bucket_L30
spatial_bucket_L29:
spatial_bucket_L30:
    rts
component_end:

Machine contract

D0–D3/A0–A1/CCR may change; D4–D7/A2–A6 are preserved. A direct BSR/JSR needs four 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 A500 OCS diagnostic cold-boots an OFS disk, owns the Copper and bitplane and disables interrupt/DMA sources. The helper does not take over hardware or allocate Chip RAM.

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 and BUILD198X=/path/to/build198x. 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
    section code,code_c
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"
    even
    include "data.inc"
    include "cases.inc"
    even
    include "display.inc"
    include "../../framework/turn-deadline/font.inc"
code_end:
STATE:
BUCKET: dc.b 0
COUNT: dc.b 0
INSERTED: dc.b 0
FULL: dc.b 0
VALID: dc.b 0
MODE: dc.b 0
INPUT_X: dc.b 0
INPUT_Y: dc.b 0
ITEM: dc.b 0
C0: dc.b 0
C1: dc.b 0
C2: dc.b 0
C3: dc.b 0
S0: dc.b 0
S1: dc.b 0
S2: dc.b 0
S3: dc.b 0
S4: dc.b 0
S5: dc.b 0
S6: dc.b 0
S7: dc.b 0
S8: dc.b 0
S9: dc.b 0
S10: dc.b 0
S11: dc.b 0
S12: dc.b 0
S13: dc.b 0
S14: dc.b 0
S15: dc.b 0
T0: dc.b 0
T1: dc.b 0
T2: dc.b 0
EXPR_SAVE: dc.b 0
    even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
    even
signature: dc.b "198XNIGHTONE000",0
    even
copperlist:
    dc.w $08e,$2c81,$090,$2cc1,$092,$0038,$094,$00d0
    dc.w $100,$1200,$102,0,$104,0,$108,0,$10a,0
    dc.w $180,$012,$182,$fff
copbpl:
    dc.w $0e0,0,$0e2,0
    dc.w $ffff,$fffe
plane: dcb.b 10240,0

A client retaining state between calls

exercise.inc, excerpt
preview_rules:
    ifne CUSTOM
; MODE = 1
    move.b #1,d0
    move.b d0,MODE
; INPUT_X = 15
    move.b #15,d0
    move.b d0,INPUT_X
; INPUT_Y = 15
    move.b #15,d0
    move.b d0,INPUT_Y
; ITEM = 255
    move.b #255,d0
    move.b d0,ITEM
    ifne BROKEN
; MODE = 0
    move.b #0,d0
    move.b d0,MODE
    endif
    bsr checked_step
    bsr emit_state
; ITEM = 1
    move.b #1,d0
    move.b d0,ITEM
    ifne BROKEN
; MODE = 0
    move.b #0,d0
    move.b d0,MODE
    endif
    bsr checked_step
    bsr emit_state
; ITEM = 2
    move.b #2,d0
    move.b d0,ITEM
    ifne BROKEN
; MODE = 0
    move.b #0,d0
    move.b d0,MODE
    endif
    bsr checked_step
    bsr emit_state
; ITEM = 3
    move.b #3,d0
    move.b d0,ITEM
    ifne BROKEN
; MODE = 0
    move.b #0,d0
    move.b d0,MODE
    endif
    bsr checked_step
    bsr emit_state
; ITEM = 4
    move.b #4,d0
    move.b d0,ITEM
    ifne BROKEN
; MODE = 0
    move.b #0,d0
    move.b d0,MODE
    endif
    bsr checked_step
    bsr emit_state
    rts
    else
; MODE = 1
    move.b #1,d0
    move.b d0,MODE
; INPUT_X = 2
    move.b #2,d0
    move.b d0,INPUT_X
; INPUT_Y = 3
    move.b #3,d0
    move.b d0,INPUT_Y
; ITEM = 7
    move.b #7,d0
    move.b d0,ITEM
    ifne BROKEN
; MODE = 0
    move.b #0,d0
    move.b d0,MODE
    endif
    bsr checked_step
    bsr emit_state
; ITEM = 8
    move.b #8,d0
    move.b d0,ITEM
    ifne BROKEN
; MODE = 0
    move.b #0,d0
    move.b d0,MODE
    endif
    bsr checked_step
    bsr emit_state
; MODE = 2
    move.b #2,d0
    move.b d0,MODE
    ifne BROKEN
; MODE = 0
    move.b #0,d0
    move.b d0,MODE
    endif
    bsr checked_step
    bsr emit_state
    endif
    rts

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
    section code,code_c
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"
    even
    include "data.inc"
    include "cases.inc"
    even
    include "display.inc"
    include "../../framework/turn-deadline/font.inc"
code_end:
STATE:
BUCKET: dc.b 0
COUNT: dc.b 0
INSERTED: dc.b 0
FULL: dc.b 0
VALID: dc.b 0
MODE: dc.b 0
INPUT_X: dc.b 0
INPUT_Y: dc.b 0
ITEM: dc.b 0
C0: dc.b 0
C1: dc.b 0
C2: dc.b 0
C3: dc.b 0
S0: dc.b 0
S1: dc.b 0
S2: dc.b 0
S3: dc.b 0
S4: dc.b 0
S5: dc.b 0
S6: dc.b 0
S7: dc.b 0
S8: dc.b 0
S9: dc.b 0
S10: dc.b 0
S11: dc.b 0
S12: dc.b 0
S13: dc.b 0
S14: dc.b 0
S15: dc.b 0
T0: dc.b 0
T1: dc.b 0
T2: dc.b 0
EXPR_SAVE: dc.b 0
    even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
    even
signature: dc.b "198XNIGHTONE000",0
    even
copperlist:
    dc.w $08e,$2c81,$090,$2cc1,$092,$0038,$094,$00d0
    dc.w $100,$1200,$102,0,$104,0,$108,0,$10a,0
    dc.w $180,$012,$182,$fff
copbpl:
    dc.w $0e0,0,$0e2,0
    dc.w $ffff,$fffe
plane: dcb.b 10240,0

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
    section code,code_c
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"
    even
    include "data.inc"
    include "cases.inc"
    even
    include "display.inc"
    include "../../framework/turn-deadline/font.inc"
code_end:
STATE:
BUCKET: dc.b 0
COUNT: dc.b 0
INSERTED: dc.b 0
FULL: dc.b 0
VALID: dc.b 0
MODE: dc.b 0
INPUT_X: dc.b 0
INPUT_Y: dc.b 0
ITEM: dc.b 0
C0: dc.b 0
C1: dc.b 0
C2: dc.b 0
C3: dc.b 0
S0: dc.b 0
S1: dc.b 0
S2: dc.b 0
S3: dc.b 0
S4: dc.b 0
S5: dc.b 0
S6: dc.b 0
S7: dc.b 0
S8: dc.b 0
S9: dc.b 0
S10: dc.b 0
S11: dc.b 0
S12: dc.b 0
S13: dc.b 0
S14: dc.b 0
S15: dc.b 0
T0: dc.b 0
T1: dc.b 0
T2: dc.b 0
EXPR_SAVE: dc.b 0
    even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
    even
signature: dc.b "198XNIGHTONE000",0
    even
copperlist:
    dc.w $08e,$2c81,$090,$2cc1,$092,$0038,$094,$00d0
    dc.w $100,$1200,$102,0,$104,0,$108,0,$10a,0
    dc.w $180,$012,$182,$fff
copbpl:
    dc.w $0e0,0,$0e2,0
    dc.w $ffff,$fffe
plane: dcb.b 10240,0
Amiga 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 750 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 A500 OCS PAL, Kickstart1.3 OFS cold boot, using Asm198x 0.0.72, Emu198x 0.44.2 and Build198x 0.2.9. 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.