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

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
; 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:
move.b #0,d0
move.b d0,CELL
move.b d0,RESULT
move.b d0,VALID
move.b d0,REQUEST
move.b d0,C0
move.b d0,C1
move.b d0,C2
move.b d0,C3
move.b d0,C4
move.b d0,C5
move.b d0,C6
move.b d0,C7
move.b d0,C8
move.b d0,C9
move.b d0,C10
move.b d0,C11
move.b d0,C12
move.b d0,C13
move.b d0,C14
move.b d0,C15
move.b d0,T0
move.b d0,T1
move.b d0,T2
move.b d0,T3
move.b d0,T4
move.b d0,T5
move.b d0,EXPR_SAVE
rts
push_block_transaction_step:
; RESULT = 0
move.b #0,d0
move.b d0,RESULT
; VALID = 0
move.b #0,d0
move.b d0,VALID
; if CELL >= 16:
move.b CELL,d0
cmp.b #16,d0
bcc push_block_transaction_L1
jmp push_block_transaction_L2
push_block_transaction_L1:
; return
rts
jmp push_block_transaction_L3
push_block_transaction_L2:
push_block_transaction_L3:
; if REQUEST == 0:
move.b REQUEST,d0
cmp.b #0,d0
beq push_block_transaction_L4
jmp push_block_transaction_L5
push_block_transaction_L4:
; return
rts
jmp push_block_transaction_L6
push_block_transaction_L5:
push_block_transaction_L6:
; if REQUEST > 4:
move.b REQUEST,d0
cmp.b #4,d0
bhi push_block_transaction_L7
jmp push_block_transaction_L8
push_block_transaction_L7:
; return
rts
jmp push_block_transaction_L9
push_block_transaction_L8:
push_block_transaction_L9:
; T0 = C[CELL]
moveq #0,d1
move.b CELL,d1
lea C0,a0
move.b 0(a0,d1.w),d0
move.b d0,T0
; if T0 != 0:
move.b T0,d0
cmp.b #0,d0
bne push_block_transaction_L10
jmp push_block_transaction_L11
push_block_transaction_L10:
; return
rts
jmp push_block_transaction_L12
push_block_transaction_L11:
push_block_transaction_L12:
; VALID = 1
move.b #1,d0
move.b d0,VALID
; T0 = CELL
move.b CELL,d0
move.b d0,T0
; T4 = 0
move.b #0,d0
move.b d0,T4
; while T4 < 2:
push_block_transaction_L13:
move.b T4,d0
cmp.b #2,d0
bcs push_block_transaction_L14
jmp push_block_transaction_L15
push_block_transaction_L14:
; T1 = 255
move.b #255,d0
move.b d0,T1
; T2 = T0 & 3
move.b T0,d0
and.b #3,d0
move.b d0,T2
; if REQUEST == 1:
move.b REQUEST,d0
cmp.b #1,d0
beq push_block_transaction_L16
jmp push_block_transaction_L17
push_block_transaction_L16:
; if T2 != 0:
move.b T2,d0
cmp.b #0,d0
bne push_block_transaction_L19
jmp push_block_transaction_L20
push_block_transaction_L19:
; T1 = T0 - 1
move.b T0,d0
sub.b #1,d0
move.b d0,T1
jmp push_block_transaction_L21
push_block_transaction_L20:
push_block_transaction_L21:
jmp push_block_transaction_L18
push_block_transaction_L17:
push_block_transaction_L18:
; if REQUEST == 2:
move.b REQUEST,d0
cmp.b #2,d0
beq push_block_transaction_L22
jmp push_block_transaction_L23
push_block_transaction_L22:
; if T2 != 3:
move.b T2,d0
cmp.b #3,d0
bne push_block_transaction_L25
jmp push_block_transaction_L26
push_block_transaction_L25:
; T1 = T0 + 1
move.b T0,d0
add.b #1,d0
move.b d0,T1
jmp push_block_transaction_L27
push_block_transaction_L26:
push_block_transaction_L27:
jmp push_block_transaction_L24
push_block_transaction_L23:
push_block_transaction_L24:
; if REQUEST == 3:
move.b REQUEST,d0
cmp.b #3,d0
beq push_block_transaction_L28
jmp push_block_transaction_L29
push_block_transaction_L28:
; if T0 >= 4:
move.b T0,d0
cmp.b #4,d0
bcc push_block_transaction_L31
jmp push_block_transaction_L32
push_block_transaction_L31:
; T1 = T0 - 4
move.b T0,d0
sub.b #4,d0
move.b d0,T1
jmp push_block_transaction_L33
push_block_transaction_L32:
push_block_transaction_L33:
jmp push_block_transaction_L30
push_block_transaction_L29:
push_block_transaction_L30:
; if REQUEST == 4:
move.b REQUEST,d0
cmp.b #4,d0
beq push_block_transaction_L34
jmp push_block_transaction_L35
push_block_transaction_L34:
; if T0 < 12:
move.b T0,d0
cmp.b #12,d0
bcs push_block_transaction_L37
jmp push_block_transaction_L38
push_block_transaction_L37:
; T1 = T0 + 4
move.b T0,d0
add.b #4,d0
move.b d0,T1
jmp push_block_transaction_L39
push_block_transaction_L38:
push_block_transaction_L39:
jmp push_block_transaction_L36
push_block_transaction_L35:
push_block_transaction_L36:
; if T1 >= 16:
move.b T1,d0
cmp.b #16,d0
bcc push_block_transaction_L40
jmp push_block_transaction_L41
push_block_transaction_L40:
; return
rts
jmp push_block_transaction_L42
push_block_transaction_L41:
push_block_transaction_L42:
; T3 = C[T1]
moveq #0,d1
move.b T1,d1
lea C0,a0
move.b 0(a0,d1.w),d0
move.b d0,T3
; if T4 == 0:
move.b T4,d0
cmp.b #0,d0
beq push_block_transaction_L43
jmp push_block_transaction_L44
push_block_transaction_L43:
; if T3 == 0:
move.b T3,d0
cmp.b #0,d0
beq push_block_transaction_L46
jmp push_block_transaction_L47
push_block_transaction_L46:
; CELL = T1
move.b T1,d0
move.b d0,CELL
; RESULT = 1
move.b #1,d0
move.b d0,RESULT
; return
rts
jmp push_block_transaction_L48
push_block_transaction_L47:
push_block_transaction_L48:
; if T3 != 2:
move.b T3,d0
cmp.b #2,d0
bne push_block_transaction_L49
jmp push_block_transaction_L50
push_block_transaction_L49:
; return
rts
jmp push_block_transaction_L51
push_block_transaction_L50:
push_block_transaction_L51:
; T5 = T1
move.b T1,d0
move.b d0,T5
jmp push_block_transaction_L45
push_block_transaction_L44:
; if T3 != 0:
move.b T3,d0
cmp.b #0,d0
bne push_block_transaction_L52
jmp push_block_transaction_L53
push_block_transaction_L52:
; return
rts
jmp push_block_transaction_L54
push_block_transaction_L53:
push_block_transaction_L54:
; C[T1] = 2
move.b #2,d0
move.b d0,d2
moveq #0,d1
move.b T1,d1
lea C0,a0
move.b d2,0(a0,d1.w)
; C[T5] = 0
move.b #0,d0
move.b d0,d2
moveq #0,d1
move.b T5,d1
lea C0,a0
move.b d2,0(a0,d1.w)
; CELL = T5
move.b T5,d0
move.b d0,CELL
; RESULT = 2
move.b #2,d0
move.b d0,RESULT
; return
rts
push_block_transaction_L45:
; T0 = T1
move.b T1,d0
move.b d0,T0
; T4 = T4 + 1
move.b T4,d0
add.b #1,d0
move.b d0,T4
jmp push_block_transaction_L13
push_block_transaction_L15:
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
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:
CELL: dc.b 0
RESULT: dc.b 0
VALID: dc.b 0
REQUEST: dc.b 0
C0: dc.b 0
C1: dc.b 0
C2: dc.b 0
C3: dc.b 0
C4: dc.b 0
C5: dc.b 0
C6: dc.b 0
C7: dc.b 0
C8: dc.b 0
C9: dc.b 0
C10: dc.b 0
C11: dc.b 0
C12: dc.b 0
C13: dc.b 0
C14: dc.b 0
C15: dc.b 0
T0: dc.b 0
T1: dc.b 0
T2: dc.b 0
T3: dc.b 0
T4: dc.b 0
T5: 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,0A client retaining state between calls
preview_rules:
ifne CUSTOM
; CELL = 8
move.b #8,d0
move.b d0,CELL
; REQUEST = 3
move.b #3,d0
move.b d0,REQUEST
; C4 = 2
move.b #2,d0
move.b d0,C4
ifne BROKEN
; C6 = 1
move.b #1,d0
move.b d0,C6
endif
bsr checked_step
bsr emit_state
; REQUEST = 2
move.b #2,d0
move.b d0,REQUEST
ifne BROKEN
; C6 = 1
move.b #1,d0
move.b d0,C6
endif
bsr checked_step
bsr emit_state
rts
else
; CELL = 4
move.b #4,d0
move.b d0,CELL
; REQUEST = 2
move.b #2,d0
move.b d0,REQUEST
; C5 = 2
move.b #2,d0
move.b d0,C5
ifne BROKEN
; C6 = 1
move.b #1,d0
move.b d0,C6
endif
bsr checked_step
bsr emit_state
ifne BROKEN
; C6 = 1
move.b #1,d0
move.b d0,C6
endif
bsr checked_step
bsr emit_state
ifne BROKEN
; C6 = 1
move.b #1,d0
move.b d0,C6
endif
bsr checked_step
bsr emit_state
endif
rtsCustomise 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
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:
CELL: dc.b 0
RESULT: dc.b 0
VALID: dc.b 0
REQUEST: dc.b 0
C0: dc.b 0
C1: dc.b 0
C2: dc.b 0
C3: dc.b 0
C4: dc.b 0
C5: dc.b 0
C6: dc.b 0
C7: dc.b 0
C8: dc.b 0
C9: dc.b 0
C10: dc.b 0
C11: dc.b 0
C12: dc.b 0
C13: dc.b 0
C14: dc.b 0
C15: dc.b 0
T0: dc.b 0
T1: dc.b 0
T2: dc.b 0
T3: dc.b 0
T4: dc.b 0
T5: 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,0The 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
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:
CELL: dc.b 0
RESULT: dc.b 0
VALID: dc.b 0
REQUEST: dc.b 0
C0: dc.b 0
C1: dc.b 0
C2: dc.b 0
C3: dc.b 0
C4: dc.b 0
C5: dc.b 0
C6: dc.b 0
C7: dc.b 0
C8: dc.b 0
C9: dc.b 0
C10: dc.b 0
C11: dc.b 0
C12: dc.b 0
C13: dc.b 0
C14: dc.b 0
C15: dc.b 0
T0: dc.b 0
T1: dc.b 0
T2: dc.b 0
T3: dc.b 0
T4: dc.b 0
T5: 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
Cost and alternatives
The measured helper extent is 774 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 A500 OCS PAL, Kickstart1.3 OFS cold boot, using Asm198x 0.0.72, Emu198x 0.44.2 and Build198x 0.2.9. 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.