Skip to content

Push a Block Atomically on Spectrum

A crate should move with the player only when the space beyond it is free.

A crate should move with the player only when the space beyond it is free.

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

CELL is the player’s separate index in a 4×4 row-major map and must point at tile zero. Tiles are 0 empty, 2 crate, and any other value blocking. REQUEST is 1 left, 2 right, 3 up or 4 down. Return RESULT=0 blocked, 1 walked or 2 pushed. Validate the complete push before changing either map cell; invalid player/request returns VALID=0 and no movement. A boundary-blocked move is a valid no-op.

Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: CELL, RESULT, VALID, REQUEST, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11, C12, C13, C14, C15. They occupy 20 contiguous bytes. Also reserve the private workspace: T0, T1, T2, T3, T4, T5, EXPR_SAVE (7 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.

Call push_block_transaction_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 push_block_transaction_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 CELL and C0..C15
Supply before service REQUEST
Read after service CELL, map cells, RESULT and VALID

Inspect the adjacent tile, then the tile beyond a crate. Both bounds and occupancy checks finish before the first write, so a failed push cannot leave half a move behind.

Reusable initialisation and service

component.inc
; SPDX-License-Identifier: MIT
; Push a Block Atomically. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
push_block_transaction_init:
    ld a,0
    ld (CELL),a
    ld (RESULT),a
    ld (VALID),a
    ld (REQUEST),a
    ld (C0),a
    ld (C1),a
    ld (C2),a
    ld (C3),a
    ld (C4),a
    ld (C5),a
    ld (C6),a
    ld (C7),a
    ld (C8),a
    ld (C9),a
    ld (C10),a
    ld (C11),a
    ld (C12),a
    ld (C13),a
    ld (C14),a
    ld (C15),a
    ld (T0),a
    ld (T1),a
    ld (T2),a
    ld (T3),a
    ld (T4),a
    ld (T5),a
    ld (EXPR_SAVE),a
    ret
push_block_transaction_step:
; RESULT = 0
    ld a,0
    ld (RESULT),a
; VALID = 0
    ld a,0
    ld (VALID),a
; if CELL >= 16:
    ld a,(CELL)
    cp 16
    jp nc,push_block_transaction_L1
    jp push_block_transaction_L2
push_block_transaction_L1:
; return
    ret
    jp push_block_transaction_L3
push_block_transaction_L2:
push_block_transaction_L3:
; if REQUEST == 0:
    ld a,(REQUEST)
    cp 0
    jp z,push_block_transaction_L4
    jp push_block_transaction_L5
push_block_transaction_L4:
; return
    ret
    jp push_block_transaction_L6
push_block_transaction_L5:
push_block_transaction_L6:
; if REQUEST > 4:
    ld a,(REQUEST)
    cp 4
    jp z,push_block_transaction_L8
    jp nc,push_block_transaction_L7
    jp push_block_transaction_L8
push_block_transaction_L7:
; return
    ret
    jp push_block_transaction_L9
push_block_transaction_L8:
push_block_transaction_L9:
; T0 = C[CELL]
    ld a,(CELL)
    ld e,a
    ld d,0
    ld hl,C0
    add hl,de
    ld a,(hl)
    ld (T0),a
; if T0 != 0:
    ld a,(T0)
    cp 0
    jp nz,push_block_transaction_L10
    jp push_block_transaction_L11
push_block_transaction_L10:
; return
    ret
    jp push_block_transaction_L12
push_block_transaction_L11:
push_block_transaction_L12:
; VALID = 1
    ld a,1
    ld (VALID),a
; T0 = CELL
    ld a,(CELL)
    ld (T0),a
; T4 = 0
    ld a,0
    ld (T4),a
; while T4 < 2:
push_block_transaction_L13:
    ld a,(T4)
    cp 2
    jp c,push_block_transaction_L14
    jp push_block_transaction_L15
push_block_transaction_L14:
; T1 = 255
    ld a,255
    ld (T1),a
; T2 = T0 & 3
    ld a,(T0)
    and 3
    ld (T2),a
; if REQUEST == 1:
    ld a,(REQUEST)
    cp 1
    jp z,push_block_transaction_L16
    jp push_block_transaction_L17
push_block_transaction_L16:
; if T2 != 0:
    ld a,(T2)
    cp 0
    jp nz,push_block_transaction_L19
    jp push_block_transaction_L20
push_block_transaction_L19:
; T1 = T0 - 1
    ld a,(T0)
    sub 1
    ld (T1),a
    jp push_block_transaction_L21
push_block_transaction_L20:
push_block_transaction_L21:
    jp push_block_transaction_L18
push_block_transaction_L17:
push_block_transaction_L18:
; if REQUEST == 2:
    ld a,(REQUEST)
    cp 2
    jp z,push_block_transaction_L22
    jp push_block_transaction_L23
push_block_transaction_L22:
; if T2 != 3:
    ld a,(T2)
    cp 3
    jp nz,push_block_transaction_L25
    jp push_block_transaction_L26
push_block_transaction_L25:
; T1 = T0 + 1
    ld a,(T0)
    add a,1
    ld (T1),a
    jp push_block_transaction_L27
push_block_transaction_L26:
push_block_transaction_L27:
    jp push_block_transaction_L24
push_block_transaction_L23:
push_block_transaction_L24:
; if REQUEST == 3:
    ld a,(REQUEST)
    cp 3
    jp z,push_block_transaction_L28
    jp push_block_transaction_L29
push_block_transaction_L28:
; if T0 >= 4:
    ld a,(T0)
    cp 4
    jp nc,push_block_transaction_L31
    jp push_block_transaction_L32
push_block_transaction_L31:
; T1 = T0 - 4
    ld a,(T0)
    sub 4
    ld (T1),a
    jp push_block_transaction_L33
push_block_transaction_L32:
push_block_transaction_L33:
    jp push_block_transaction_L30
push_block_transaction_L29:
push_block_transaction_L30:
; if REQUEST == 4:
    ld a,(REQUEST)
    cp 4
    jp z,push_block_transaction_L34
    jp push_block_transaction_L35
push_block_transaction_L34:
; if T0 < 12:
    ld a,(T0)
    cp 12
    jp c,push_block_transaction_L37
    jp push_block_transaction_L38
push_block_transaction_L37:
; T1 = T0 + 4
    ld a,(T0)
    add a,4
    ld (T1),a
    jp push_block_transaction_L39
push_block_transaction_L38:
push_block_transaction_L39:
    jp push_block_transaction_L36
push_block_transaction_L35:
push_block_transaction_L36:
; if T1 >= 16:
    ld a,(T1)
    cp 16
    jp nc,push_block_transaction_L40
    jp push_block_transaction_L41
push_block_transaction_L40:
; return
    ret
    jp push_block_transaction_L42
push_block_transaction_L41:
push_block_transaction_L42:
; T3 = C[T1]
    ld a,(T1)
    ld e,a
    ld d,0
    ld hl,C0
    add hl,de
    ld a,(hl)
    ld (T3),a
; if T4 == 0:
    ld a,(T4)
    cp 0
    jp z,push_block_transaction_L43
    jp push_block_transaction_L44
push_block_transaction_L43:
; if T3 == 0:
    ld a,(T3)
    cp 0
    jp z,push_block_transaction_L46
    jp push_block_transaction_L47
push_block_transaction_L46:
; CELL = T1
    ld a,(T1)
    ld (CELL),a
; RESULT = 1
    ld a,1
    ld (RESULT),a
; return
    ret
    jp push_block_transaction_L48
push_block_transaction_L47:
push_block_transaction_L48:
; if T3 != 2:
    ld a,(T3)
    cp 2
    jp nz,push_block_transaction_L49
    jp push_block_transaction_L50
push_block_transaction_L49:
; return
    ret
    jp push_block_transaction_L51
push_block_transaction_L50:
push_block_transaction_L51:
; T5 = T1
    ld a,(T1)
    ld (T5),a
    jp push_block_transaction_L45
push_block_transaction_L44:
; if T3 != 0:
    ld a,(T3)
    cp 0
    jp nz,push_block_transaction_L52
    jp push_block_transaction_L53
push_block_transaction_L52:
; return
    ret
    jp push_block_transaction_L54
push_block_transaction_L53:
push_block_transaction_L54:
; C[T1] = 2
    ld a,2
    ld c,a
    ld a,(T1)
    ld e,a
    ld d,0
    ld hl,C0
    add hl,de
    ld (hl),c
; C[T5] = 0
    ld a,0
    ld c,a
    ld a,(T5)
    ld e,a
    ld d,0
    ld hl,C0
    add hl,de
    ld (hl),c
; CELL = T5
    ld a,(T5)
    ld (CELL),a
; RESULT = 2
    ld a,2
    ld (RESULT),a
; return
    ret
push_block_transaction_L45:
; T0 = T1
    ld a,(T1)
    ld (T0),a
; T4 = T4 + 1
    ld a,(T4)
    add a,1
    ld (T4),a
    jp push_block_transaction_L13
push_block_transaction_L15:
    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:
CELL: defb 0
RESULT: defb 0
VALID: defb 0
REQUEST: defb 0
C0: defb 0
C1: defb 0
C2: defb 0
C3: defb 0
C4: defb 0
C5: defb 0
C6: defb 0
C7: defb 0
C8: defb 0
C9: defb 0
C10: defb 0
C11: defb 0
C12: defb 0
C13: defb 0
C14: defb 0
C15: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
T3: defb 0
T4: defb 0
T5: 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
; CELL = 8
    ld a,8
    ld (CELL),a
; REQUEST = 3
    ld a,3
    ld (REQUEST),a
; C4 = 2
    ld a,2
    ld (C4),a
    if BROKEN
; C6 = 1
    ld a,1
    ld (C6),a
    endif
    call checked_step
    call emit_state
; REQUEST = 2
    ld a,2
    ld (REQUEST),a
    if BROKEN
; C6 = 1
    ld a,1
    ld (C6),a
    endif
    call checked_step
    call emit_state
    ret
    else
; CELL = 4
    ld a,4
    ld (CELL),a
; REQUEST = 2
    ld a,2
    ld (REQUEST),a
; C5 = 2
    ld a,2
    ld (C5),a
    if BROKEN
; C6 = 1
    ld a,1
    ld (C6),a
    endif
    call checked_step
    call emit_state

    if BROKEN
; C6 = 1
    ld a,1
    ld (C6),a
    endif
    call checked_step
    call emit_state

    if BROKEN
; C6 = 1
    ld a,1
    ld (C6),a
    endif
    call checked_step
    call emit_state
    endif
    ret

Customise it

Push upwards, then walk sideways away from the crate. 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:
CELL: defb 0
RESULT: defb 0
VALID: defb 0
REQUEST: defb 0
C0: defb 0
C1: defb 0
C2: defb 0
C3: defb 0
C4: defb 0
C5: defb 0
C6: defb 0
C7: defb 0
C8: defb 0
C9: defb 0
C10: defb 0
C11: defb 0
C12: defb 0
C13: defb 0
C14: defb 0
C15: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
T3: defb 0
T4: defb 0
T5: 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 05 01 01 02 02 00 00 00. The full client trace in the source explains how it gets there.

Mistake and repair

The wrong caller writes a wall into the crate’s destination before service. The push correctly refuses the altered map, so the player appears stuck. Keep the authoritative map unchanged until the move is validated.

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:
CELL: defb 0
RESULT: defb 0
VALID: defb 0
REQUEST: defb 0
C0: defb 0
C1: defb 0
C2: defb 0
C3: defb 0
C4: defb 0
C5: defb 0
C6: defb 0
C7: defb 0
C8: defb 0
C9: defb 0
C10: defb 0
C11: defb 0
C12: defb 0
C13: defb 0
C14: defb 0
C15: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
T3: defb 0
T4: defb 0
T5: 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 04 00 01 02 00 00 00 00.
The wrong client completes with 04 00 01 02 00 00 00 00; its changed inputs and state explain the result.

Cost and alternatives

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

At most two neighbouring tile reads. Larger pushes need a complete validation pass before a commit pass, or an explicitly reversible journal.

Only one crate can be pushed. This is a deliberately bounded logical puzzle grid; animation, several linked crates, goals and undo use separate state. Reserve exclusive ownership while committing the two map writes.

Execution evidence

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

Bounded undo logs: Record every changed cell and player position if the whole push must be undoable.

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