Moving arrows from a chest to a pack should never remove more than the pack can hold.

Use it in a project
Zero count requires kind zero; a nonempty stack requires a nonzero kind. Both counts must be at most CAPACITY. AMOUNT requests up to that many items from source into destination. A nonempty destination accepts only the same kind. MOVED reports the actual minimum of requested, available and free space; zero is a valid refusal. On success clear an emptied source kind and assign the source kind to an empty destination. Invalid stack representation or capacity sets VALID=0 without changing either stack.
Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: MOVED, VALID, SOURCE_KIND, SOURCE_COUNT, DEST_KIND, DEST_COUNT, CAPACITY, AMOUNT. They occupy 8 contiguous bytes. Also reserve the private workspace: T0 (1 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.
Call inventory_transfer_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 inventory_transfer_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 | source and destination kinds/counts |
| Supply before service | CAPACITY and AMOUNT |
| Read after service | updated stacks, MOVED and VALID |
Validate both representations before writing, calculate the complete accepted amount, then commit both counts and kinds. Conservation of the total item count is the central invariant.
Reusable initialisation and service
; SPDX-License-Identifier: MIT
; Transfer Inventory Stacks Atomically. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
inventory_transfer_init:
ld a,0
ld (MOVED),a
ld (VALID),a
ld (SOURCE_KIND),a
ld (SOURCE_COUNT),a
ld (DEST_KIND),a
ld (DEST_COUNT),a
ld (CAPACITY),a
ld (AMOUNT),a
ld (T0),a
ret
inventory_transfer_step:
; MOVED = 0
ld a,0
ld (MOVED),a
; VALID = 0
ld a,0
ld (VALID),a
; if SOURCE_COUNT > CAPACITY:
ld a,(SOURCE_COUNT)
ld b,a
ld a,(CAPACITY)
ld c,a
ld a,b
cp c
jp z,inventory_transfer_L2
jp nc,inventory_transfer_L1
jp inventory_transfer_L2
inventory_transfer_L1:
; return
ret
jp inventory_transfer_L3
inventory_transfer_L2:
inventory_transfer_L3:
; if DEST_COUNT > CAPACITY:
ld a,(DEST_COUNT)
ld b,a
ld a,(CAPACITY)
ld c,a
ld a,b
cp c
jp z,inventory_transfer_L5
jp nc,inventory_transfer_L4
jp inventory_transfer_L5
inventory_transfer_L4:
; return
ret
jp inventory_transfer_L6
inventory_transfer_L5:
inventory_transfer_L6:
; if SOURCE_COUNT == 0:
ld a,(SOURCE_COUNT)
cp 0
jp z,inventory_transfer_L7
jp inventory_transfer_L8
inventory_transfer_L7:
; if SOURCE_KIND != 0:
ld a,(SOURCE_KIND)
cp 0
jp nz,inventory_transfer_L10
jp inventory_transfer_L11
inventory_transfer_L10:
; return
ret
jp inventory_transfer_L12
inventory_transfer_L11:
inventory_transfer_L12:
jp inventory_transfer_L9
inventory_transfer_L8:
; if SOURCE_KIND == 0:
ld a,(SOURCE_KIND)
cp 0
jp z,inventory_transfer_L13
jp inventory_transfer_L14
inventory_transfer_L13:
; return
ret
jp inventory_transfer_L15
inventory_transfer_L14:
inventory_transfer_L15:
inventory_transfer_L9:
; if DEST_COUNT == 0:
ld a,(DEST_COUNT)
cp 0
jp z,inventory_transfer_L16
jp inventory_transfer_L17
inventory_transfer_L16:
; if DEST_KIND != 0:
ld a,(DEST_KIND)
cp 0
jp nz,inventory_transfer_L19
jp inventory_transfer_L20
inventory_transfer_L19:
; return
ret
jp inventory_transfer_L21
inventory_transfer_L20:
inventory_transfer_L21:
jp inventory_transfer_L18
inventory_transfer_L17:
; if DEST_KIND == 0:
ld a,(DEST_KIND)
cp 0
jp z,inventory_transfer_L22
jp inventory_transfer_L23
inventory_transfer_L22:
; return
ret
jp inventory_transfer_L24
inventory_transfer_L23:
inventory_transfer_L24:
inventory_transfer_L18:
; VALID = 1
ld a,1
ld (VALID),a
; if SOURCE_COUNT == 0:
ld a,(SOURCE_COUNT)
cp 0
jp z,inventory_transfer_L25
jp inventory_transfer_L26
inventory_transfer_L25:
; return
ret
jp inventory_transfer_L27
inventory_transfer_L26:
inventory_transfer_L27:
; if DEST_COUNT != 0:
ld a,(DEST_COUNT)
cp 0
jp nz,inventory_transfer_L28
jp inventory_transfer_L29
inventory_transfer_L28:
; if DEST_KIND != SOURCE_KIND:
ld a,(DEST_KIND)
ld b,a
ld a,(SOURCE_KIND)
ld c,a
ld a,b
cp c
jp nz,inventory_transfer_L31
jp inventory_transfer_L32
inventory_transfer_L31:
; return
ret
jp inventory_transfer_L33
inventory_transfer_L32:
inventory_transfer_L33:
jp inventory_transfer_L30
inventory_transfer_L29:
inventory_transfer_L30:
; T0 = CAPACITY - DEST_COUNT
ld a,(CAPACITY)
ld b,a
ld a,(DEST_COUNT)
ld c,a
ld a,b
sub c
ld (T0),a
; if AMOUNT < T0:
ld a,(AMOUNT)
ld b,a
ld a,(T0)
ld c,a
ld a,b
cp c
jp c,inventory_transfer_L34
jp inventory_transfer_L35
inventory_transfer_L34:
; T0 = AMOUNT
ld a,(AMOUNT)
ld (T0),a
jp inventory_transfer_L36
inventory_transfer_L35:
inventory_transfer_L36:
; if SOURCE_COUNT < T0:
ld a,(SOURCE_COUNT)
ld b,a
ld a,(T0)
ld c,a
ld a,b
cp c
jp c,inventory_transfer_L37
jp inventory_transfer_L38
inventory_transfer_L37:
; T0 = SOURCE_COUNT
ld a,(SOURCE_COUNT)
ld (T0),a
jp inventory_transfer_L39
inventory_transfer_L38:
inventory_transfer_L39:
; if T0 == 0:
ld a,(T0)
cp 0
jp z,inventory_transfer_L40
jp inventory_transfer_L41
inventory_transfer_L40:
; return
ret
jp inventory_transfer_L42
inventory_transfer_L41:
inventory_transfer_L42:
; DEST_KIND = SOURCE_KIND
ld a,(SOURCE_KIND)
ld (DEST_KIND),a
; DEST_COUNT = DEST_COUNT + T0
ld a,(DEST_COUNT)
ld b,a
ld a,(T0)
ld c,a
ld a,b
add a,c
ld (DEST_COUNT),a
; SOURCE_COUNT = SOURCE_COUNT - T0
ld a,(SOURCE_COUNT)
ld b,a
ld a,(T0)
ld c,a
ld a,b
sub c
ld (SOURCE_COUNT),a
; MOVED = T0
ld a,(T0)
ld (MOVED),a
; if SOURCE_COUNT == 0:
ld a,(SOURCE_COUNT)
cp 0
jp z,inventory_transfer_L43
jp inventory_transfer_L44
inventory_transfer_L43:
; SOURCE_KIND = 0
ld a,0
ld (SOURCE_KIND),a
jp inventory_transfer_L45
inventory_transfer_L44:
inventory_transfer_L45:
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
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:
MOVED: defb 0
VALID: defb 0
SOURCE_KIND: defb 0
SOURCE_COUNT: defb 0
DEST_KIND: defb 0
DEST_COUNT: defb 0
CAPACITY: defb 0
AMOUNT: defb 0
T0: 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 startA client retaining state between calls
preview_rules:
if CUSTOM
; SOURCE_KIND = 2
ld a,2
ld (SOURCE_KIND),a
; SOURCE_COUNT = 4
ld a,4
ld (SOURCE_COUNT),a
; CAPACITY = 4
ld a,4
ld (CAPACITY),a
; AMOUNT = 255
ld a,255
ld (AMOUNT),a
if BROKEN
; AMOUNT = 0
ld a,0
ld (AMOUNT),a
endif
call checked_step
call emit_state
if BROKEN
; AMOUNT = 0
ld a,0
ld (AMOUNT),a
endif
call checked_step
call emit_state
ret
else
; SOURCE_KIND = 3
ld a,3
ld (SOURCE_KIND),a
; SOURCE_COUNT = 7
ld a,7
ld (SOURCE_COUNT),a
; DEST_KIND = 3
ld a,3
ld (DEST_KIND),a
; DEST_COUNT = 8
ld a,8
ld (DEST_COUNT),a
; CAPACITY = 10
ld a,10
ld (CAPACITY),a
; AMOUNT = 5
ld a,5
ld (AMOUNT),a
if BROKEN
; AMOUNT = 0
ld a,0
ld (AMOUNT),a
endif
call checked_step
call emit_state
; DEST_KIND = 0
ld a,0
ld (DEST_KIND),a
; DEST_COUNT = 0
ld a,0
ld (DEST_COUNT),a
; AMOUNT = 5
ld a,5
ld (AMOUNT),a
if BROKEN
; AMOUNT = 0
ld a,0
ld (AMOUNT),a
endif
call checked_step
call emit_state
endif
retCustomise it
Move the whole source into an empty four-item destination, then try the now-empty source again. 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
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:
MOVED: defb 0
VALID: defb 0
SOURCE_KIND: defb 0
SOURCE_COUNT: defb 0
DEST_KIND: defb 0
DEST_COUNT: defb 0
CAPACITY: defb 0
AMOUNT: defb 0
T0: 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 startThe custom diagnostic row is 00 01 00 00 02 04 04 FF. The full client trace in the source explains how it gets there.
Mistake and repair
The wrong client clears the transfer request before service. Both stacks remain unchanged and MOVED is zero. Keep the requested amount until it has been consumed.
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
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:
MOVED: defb 0
VALID: defb 0
SOURCE_KIND: defb 0
SOURCE_COUNT: defb 0
DEST_KIND: defb 0
DEST_COUNT: defb 0
CAPACITY: defb 0
AMOUNT: defb 0
T0: 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
Cost and alternatives
The measured helper extent is 364 code bytes; public state is 8 bytes and private scratch is 1 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.
Constant work with no array search. Full inventory placement can scan candidate stacks first and invoke this transaction under the same mutation owner.
One source and one destination are borrowed exclusively during a call. This does not find stacks, merge an entire inventory, reserve network transactions or support different per-item caps. The capacity is shared by these two stacks; use a larger transaction for several destinations.
Execution evidence
Four variants run on 48K PAL ROM tape load and USR, using Asm198x 0.0.72, Emu198x 0.44.2. Each executes 64 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
Inventory stacks: Reuse the existing stack rules when adding or removing items without a second container.