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:
lda #0
sta CELL
sta RESULT
sta VALID
sta REQUEST
sta C0
sta C1
sta C2
sta C3
sta C4
sta C5
sta C6
sta C7
sta C8
sta C9
sta C10
sta C11
sta C12
sta C13
sta C14
sta C15
sta T0
sta T1
sta T2
sta T3
sta T4
sta T5
sta EXPR_SAVE
rts
push_block_transaction_step:
; RESULT = 0
lda #0
sta RESULT
; VALID = 0
lda #0
sta VALID
; if CELL >= 16:
lda CELL
cmp #16
bcs push_block_transaction_L4
push_block_transaction_L5:
jmp push_block_transaction_L2
push_block_transaction_L4:
jmp push_block_transaction_L1
push_block_transaction_L1:
; return
rts
jmp push_block_transaction_L3
push_block_transaction_L2:
push_block_transaction_L3:
; if REQUEST == 0:
lda REQUEST
cmp #0
beq push_block_transaction_L9
push_block_transaction_L10:
jmp push_block_transaction_L7
push_block_transaction_L9:
jmp push_block_transaction_L6
push_block_transaction_L6:
; return
rts
jmp push_block_transaction_L8
push_block_transaction_L7:
push_block_transaction_L8:
; if REQUEST > 4:
lda REQUEST
cmp #4
beq push_block_transaction_L15
bcs push_block_transaction_L14
push_block_transaction_L15:
jmp push_block_transaction_L12
push_block_transaction_L14:
jmp push_block_transaction_L11
push_block_transaction_L11:
; return
rts
jmp push_block_transaction_L13
push_block_transaction_L12:
push_block_transaction_L13:
; T0 = C[CELL]
ldx CELL
lda C0,x
sta T0
; if T0 != 0:
lda T0
cmp #0
bne push_block_transaction_L19
push_block_transaction_L20:
jmp push_block_transaction_L17
push_block_transaction_L19:
jmp push_block_transaction_L16
push_block_transaction_L16:
; return
rts
jmp push_block_transaction_L18
push_block_transaction_L17:
push_block_transaction_L18:
; VALID = 1
lda #1
sta VALID
; T0 = CELL
lda CELL
sta T0
; T4 = 0
lda #0
sta T4
; while T4 < 2:
push_block_transaction_L21:
lda T4
cmp #2
bcc push_block_transaction_L24
push_block_transaction_L25:
jmp push_block_transaction_L23
push_block_transaction_L24:
jmp push_block_transaction_L22
push_block_transaction_L22:
; T1 = 255
lda #255
sta T1
; T2 = T0 & 3
lda T0
and #3
sta T2
; if REQUEST == 1:
lda REQUEST
cmp #1
beq push_block_transaction_L29
push_block_transaction_L30:
jmp push_block_transaction_L27
push_block_transaction_L29:
jmp push_block_transaction_L26
push_block_transaction_L26:
; if T2 != 0:
lda T2
cmp #0
bne push_block_transaction_L34
push_block_transaction_L35:
jmp push_block_transaction_L32
push_block_transaction_L34:
jmp push_block_transaction_L31
push_block_transaction_L31:
; T1 = T0 - 1
lda T0
sec
sbc #1
sta T1
jmp push_block_transaction_L33
push_block_transaction_L32:
push_block_transaction_L33:
jmp push_block_transaction_L28
push_block_transaction_L27:
push_block_transaction_L28:
; if REQUEST == 2:
lda REQUEST
cmp #2
beq push_block_transaction_L39
push_block_transaction_L40:
jmp push_block_transaction_L37
push_block_transaction_L39:
jmp push_block_transaction_L36
push_block_transaction_L36:
; if T2 != 3:
lda T2
cmp #3
bne push_block_transaction_L44
push_block_transaction_L45:
jmp push_block_transaction_L42
push_block_transaction_L44:
jmp push_block_transaction_L41
push_block_transaction_L41:
; T1 = T0 + 1
lda T0
clc
adc #1
sta T1
jmp push_block_transaction_L43
push_block_transaction_L42:
push_block_transaction_L43:
jmp push_block_transaction_L38
push_block_transaction_L37:
push_block_transaction_L38:
; if REQUEST == 3:
lda REQUEST
cmp #3
beq push_block_transaction_L49
push_block_transaction_L50:
jmp push_block_transaction_L47
push_block_transaction_L49:
jmp push_block_transaction_L46
push_block_transaction_L46:
; if T0 >= 4:
lda T0
cmp #4
bcs push_block_transaction_L54
push_block_transaction_L55:
jmp push_block_transaction_L52
push_block_transaction_L54:
jmp push_block_transaction_L51
push_block_transaction_L51:
; T1 = T0 - 4
lda T0
sec
sbc #4
sta T1
jmp push_block_transaction_L53
push_block_transaction_L52:
push_block_transaction_L53:
jmp push_block_transaction_L48
push_block_transaction_L47:
push_block_transaction_L48:
; if REQUEST == 4:
lda REQUEST
cmp #4
beq push_block_transaction_L59
push_block_transaction_L60:
jmp push_block_transaction_L57
push_block_transaction_L59:
jmp push_block_transaction_L56
push_block_transaction_L56:
; if T0 < 12:
lda T0
cmp #12
bcc push_block_transaction_L64
push_block_transaction_L65:
jmp push_block_transaction_L62
push_block_transaction_L64:
jmp push_block_transaction_L61
push_block_transaction_L61:
; T1 = T0 + 4
lda T0
clc
adc #4
sta T1
jmp push_block_transaction_L63
push_block_transaction_L62:
push_block_transaction_L63:
jmp push_block_transaction_L58
push_block_transaction_L57:
push_block_transaction_L58:
; if T1 >= 16:
lda T1
cmp #16
bcs push_block_transaction_L69
push_block_transaction_L70:
jmp push_block_transaction_L67
push_block_transaction_L69:
jmp push_block_transaction_L66
push_block_transaction_L66:
; return
rts
jmp push_block_transaction_L68
push_block_transaction_L67:
push_block_transaction_L68:
; T3 = C[T1]
ldx T1
lda C0,x
sta T3
; if T4 == 0:
lda T4
cmp #0
beq push_block_transaction_L74
push_block_transaction_L75:
jmp push_block_transaction_L72
push_block_transaction_L74:
jmp push_block_transaction_L71
push_block_transaction_L71:
; if T3 == 0:
lda T3
cmp #0
beq push_block_transaction_L79
push_block_transaction_L80:
jmp push_block_transaction_L77
push_block_transaction_L79:
jmp push_block_transaction_L76
push_block_transaction_L76:
; CELL = T1
lda T1
sta CELL
; RESULT = 1
lda #1
sta RESULT
; return
rts
jmp push_block_transaction_L78
push_block_transaction_L77:
push_block_transaction_L78:
; if T3 != 2:
lda T3
cmp #2
bne push_block_transaction_L84
push_block_transaction_L85:
jmp push_block_transaction_L82
push_block_transaction_L84:
jmp push_block_transaction_L81
push_block_transaction_L81:
; return
rts
jmp push_block_transaction_L83
push_block_transaction_L82:
push_block_transaction_L83:
; T5 = T1
lda T1
sta T5
jmp push_block_transaction_L73
push_block_transaction_L72:
; if T3 != 0:
lda T3
cmp #0
bne push_block_transaction_L89
push_block_transaction_L90:
jmp push_block_transaction_L87
push_block_transaction_L89:
jmp push_block_transaction_L86
push_block_transaction_L86:
; return
rts
jmp push_block_transaction_L88
push_block_transaction_L87:
push_block_transaction_L88:
; C[T1] = 2
lda #2
sta EXPR_SAVE
ldx T1
lda EXPR_SAVE
sta C0,x
; C[T5] = 0
lda #0
sta EXPR_SAVE
ldx T5
lda EXPR_SAVE
sta C0,x
; CELL = T5
lda T5
sta CELL
; RESULT = 2
lda #2
sta RESULT
; return
rts
push_block_transaction_L73:
; T0 = T1
lda T1
sta T0
; T4 = T4 + 1
lda T4
clc
adc #1
sta T4
jmp push_block_transaction_L21
push_block_transaction_L23:
rts
component_end:Machine contract
A/X/status may change; Y and decimal mode are preserved. Use CLD for binary arithmetic. A direct JSR 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 diagnostic PRG owns screen, font and colour RAM and disables interrupts; it does not return to BASIC. The reusable helper does none of those things.
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
CELL=$80
RESULT=$81
VALID=$82
REQUEST=$83
C0=$84
C1=$85
C2=$86
C3=$87
C4=$88
C5=$89
C6=$8a
C7=$8b
C8=$8c
C9=$8d
C10=$8e
C11=$8f
C12=$90
C13=$91
C14=$92
C15=$93
T0=$94
T1=$95
T2=$96
T3=$97
T4=$98
T5=$99
EXPR_SAVE=$9a
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$c000
preview=OUTPUT+1400
ready=OUTPUT+1408
CUSTOM=0
BROKEN=0
COYOTE_STEPS=4
BUFFER_STEPS=4
TEXT_CAP=8
PROPERTY_COUNT=16
STREAM_BYTES=13
CRC_POLY=7
!source "caller.inc"
!source "exercise.inc"
component_start:
!source "component.inc"
!source "data.inc"
!source "cases.inc"
hex_font:
!byte 0,0,0,0,0,0,0,0,0,56,40,40,40,56,0,0,0,16,48,16,16,56,0,0,0,56,8,56,32,56,0,0,0,56,8,56,8,56,0,0,0,40,40,56,8,8,0,0,0,56,32,56,8,56,0,0,0,56,32,56,40,56,0,0,0,56,8,8,8,8,0,0,0,56,40,56,40,56,0,0,0,56,40,56,8,56,0,0,0,16,40,56,40,40,0,0,0,48,40,48,40,48,0,0,0,56,32,32,32,56,0,0,0,48,40,40,40,48,0,0,0,56,32,48,32,56,0,0,0,56,32,48,32,32,0,0
code_end:A client retaining state between calls
preview_rules:
!if CUSTOM {
; CELL = 8
lda #8
sta CELL
; REQUEST = 3
lda #3
sta REQUEST
; C4 = 2
lda #2
sta C4
!if BROKEN {
; C6 = 1
lda #1
sta C6
}
jsr checked_step
jsr emit_state
; REQUEST = 2
lda #2
sta REQUEST
!if BROKEN {
; C6 = 1
lda #1
sta C6
}
jsr checked_step
jsr emit_state
rts
} else {
; CELL = 4
lda #4
sta CELL
; REQUEST = 2
lda #2
sta REQUEST
; C5 = 2
lda #2
sta C5
!if BROKEN {
; C6 = 1
lda #1
sta C6
}
jsr checked_step
jsr emit_state
!if BROKEN {
; C6 = 1
lda #1
sta C6
}
jsr checked_step
jsr emit_state
!if BROKEN {
; C6 = 1
lda #1
sta C6
}
jsr checked_step
jsr emit_state
}
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
CELL=$80
RESULT=$81
VALID=$82
REQUEST=$83
C0=$84
C1=$85
C2=$86
C3=$87
C4=$88
C5=$89
C6=$8a
C7=$8b
C8=$8c
C9=$8d
C10=$8e
C11=$8f
C12=$90
C13=$91
C14=$92
C15=$93
T0=$94
T1=$95
T2=$96
T3=$97
T4=$98
T5=$99
EXPR_SAVE=$9a
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$c000
preview=OUTPUT+1400
ready=OUTPUT+1408
CUSTOM=1
BROKEN=0
COYOTE_STEPS=2
BUFFER_STEPS=2
TEXT_CAP=4
PROPERTY_COUNT=16
STREAM_BYTES=13
CRC_POLY=49
!source "caller.inc"
!source "exercise.inc"
component_start:
!source "component.inc"
!source "data.inc"
!source "cases.inc"
hex_font:
!byte 0,0,0,0,0,0,0,0,0,56,40,40,40,56,0,0,0,16,48,16,16,56,0,0,0,56,8,56,32,56,0,0,0,56,8,56,8,56,0,0,0,40,40,56,8,8,0,0,0,56,32,56,8,56,0,0,0,56,32,56,40,56,0,0,0,56,8,8,8,8,0,0,0,56,40,56,40,56,0,0,0,56,40,56,8,56,0,0,0,16,40,56,40,40,0,0,0,48,40,48,40,48,0,0,0,56,32,32,32,56,0,0,0,48,40,40,40,48,0,0,0,56,32,48,32,56,0,0,0,56,32,48,32,32,0,0
code_end: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
CELL=$80
RESULT=$81
VALID=$82
REQUEST=$83
C0=$84
C1=$85
C2=$86
C3=$87
C4=$88
C5=$89
C6=$8a
C7=$8b
C8=$8c
C9=$8d
C10=$8e
C11=$8f
C12=$90
C13=$91
C14=$92
C15=$93
T0=$94
T1=$95
T2=$96
T3=$97
T4=$98
T5=$99
EXPR_SAVE=$9a
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$c000
preview=OUTPUT+1400
ready=OUTPUT+1408
CUSTOM=0
BROKEN=1
COYOTE_STEPS=4
BUFFER_STEPS=4
TEXT_CAP=8
PROPERTY_COUNT=16
STREAM_BYTES=13
CRC_POLY=7
!source "caller.inc"
!source "exercise.inc"
component_start:
!source "component.inc"
!source "data.inc"
!source "cases.inc"
hex_font:
!byte 0,0,0,0,0,0,0,0,0,56,40,40,40,56,0,0,0,16,48,16,16,56,0,0,0,56,8,56,32,56,0,0,0,56,8,56,8,56,0,0,0,40,40,56,8,8,0,0,0,56,32,56,8,56,0,0,0,56,32,56,40,56,0,0,0,56,8,8,8,8,0,0,0,56,40,56,40,56,0,0,0,56,40,56,8,56,0,0,0,16,40,56,40,40,0,0,0,48,40,48,40,48,0,0,0,56,32,32,32,56,0,0,0,48,40,40,40,48,0,0,0,56,32,48,32,56,0,0,0,56,32,48,32,32,0,0
code_end:
Cost and alternatives
The measured helper extent is 460 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 PAL stock-ROM PRG, 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.