A passenger should inherit a moving platform’s displacement without changing the platform’s own position.

Use it in a project
PASSENGER, PLATFORM_OLD and PLATFORM_NEW are unsigned byte coordinates on one axis. MINIMUM/MAXIMUM bound the passenger’s allowed coordinate. If SUPPORTED is nonzero, add the signed platform displacement and clamp at those limits; otherwise keep PASSENGER. Return POSITION, CLAMPED and VALID. Reversed limits or an already out-of-range passenger return POSITION=PASSENGER and VALID=0.
Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: POSITION, CLAMPED, VALID, PASSENGER, PLATFORM_OLD, PLATFORM_NEW, SUPPORTED, MINIMUM, MAXIMUM. They occupy 9 contiguous bytes. Also reserve the private workspace: T0, T1 (2 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.
Call moving_platform_carry_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 moving_platform_carry_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 | none |
| Supply before service | passenger, platform coordinates, support and bounds |
| Read after service | POSITION, CLAMPED and VALID |
Choose the displacement sign by comparing platform coordinates. Check byte carry or borrow before applying world bounds, so arithmetic wrapping cannot put the passenger on the opposite side of the level.
Reusable initialisation and service
; SPDX-License-Identifier: MIT
; Carry a Supported Passenger. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
moving_platform_carry_init:
ld a,0
ld (POSITION),a
ld (CLAMPED),a
ld (VALID),a
ld (PASSENGER),a
ld (PLATFORM_OLD),a
ld (PLATFORM_NEW),a
ld (SUPPORTED),a
ld (MINIMUM),a
ld (MAXIMUM),a
ld (T0),a
ld (T1),a
ret
moving_platform_carry_step:
; POSITION = PASSENGER
ld a,(PASSENGER)
ld (POSITION),a
; CLAMPED = 0
ld a,0
ld (CLAMPED),a
; VALID = 0
ld a,0
ld (VALID),a
; if MINIMUM > MAXIMUM:
ld a,(MINIMUM)
ld b,a
ld a,(MAXIMUM)
ld c,a
ld a,b
cp c
jp z,moving_platform_carry_L2
jp nc,moving_platform_carry_L1
jp moving_platform_carry_L2
moving_platform_carry_L1:
; return
ret
jp moving_platform_carry_L3
moving_platform_carry_L2:
moving_platform_carry_L3:
; if PASSENGER < MINIMUM:
ld a,(PASSENGER)
ld b,a
ld a,(MINIMUM)
ld c,a
ld a,b
cp c
jp c,moving_platform_carry_L4
jp moving_platform_carry_L5
moving_platform_carry_L4:
; return
ret
jp moving_platform_carry_L6
moving_platform_carry_L5:
moving_platform_carry_L6:
; if PASSENGER > MAXIMUM:
ld a,(PASSENGER)
ld b,a
ld a,(MAXIMUM)
ld c,a
ld a,b
cp c
jp z,moving_platform_carry_L8
jp nc,moving_platform_carry_L7
jp moving_platform_carry_L8
moving_platform_carry_L7:
; return
ret
jp moving_platform_carry_L9
moving_platform_carry_L8:
moving_platform_carry_L9:
; VALID = 1
ld a,1
ld (VALID),a
; if SUPPORTED == 0:
ld a,(SUPPORTED)
cp 0
jp z,moving_platform_carry_L10
jp moving_platform_carry_L11
moving_platform_carry_L10:
; return
ret
jp moving_platform_carry_L12
moving_platform_carry_L11:
moving_platform_carry_L12:
; if PLATFORM_NEW >= PLATFORM_OLD:
ld a,(PLATFORM_NEW)
ld b,a
ld a,(PLATFORM_OLD)
ld c,a
ld a,b
cp c
jp nc,moving_platform_carry_L13
jp moving_platform_carry_L14
moving_platform_carry_L13:
; T0 = PLATFORM_NEW - PLATFORM_OLD
ld a,(PLATFORM_NEW)
ld b,a
ld a,(PLATFORM_OLD)
ld c,a
ld a,b
sub c
ld (T0),a
; T1 = PASSENGER + T0
ld a,(PASSENGER)
ld b,a
ld a,(T0)
ld c,a
ld a,b
add a,c
ld (T1),a
; if T1 < PASSENGER:
ld a,(T1)
ld b,a
ld a,(PASSENGER)
ld c,a
ld a,b
cp c
jp c,moving_platform_carry_L16
jp moving_platform_carry_L17
moving_platform_carry_L16:
; POSITION = MAXIMUM
ld a,(MAXIMUM)
ld (POSITION),a
; CLAMPED = 1
ld a,1
ld (CLAMPED),a
; return
ret
jp moving_platform_carry_L18
moving_platform_carry_L17:
moving_platform_carry_L18:
; if T1 > MAXIMUM:
ld a,(T1)
ld b,a
ld a,(MAXIMUM)
ld c,a
ld a,b
cp c
jp z,moving_platform_carry_L20
jp nc,moving_platform_carry_L19
jp moving_platform_carry_L20
moving_platform_carry_L19:
; POSITION = MAXIMUM
ld a,(MAXIMUM)
ld (POSITION),a
; CLAMPED = 1
ld a,1
ld (CLAMPED),a
; return
ret
jp moving_platform_carry_L21
moving_platform_carry_L20:
moving_platform_carry_L21:
; POSITION = T1
ld a,(T1)
ld (POSITION),a
jp moving_platform_carry_L15
moving_platform_carry_L14:
; T0 = PLATFORM_OLD - PLATFORM_NEW
ld a,(PLATFORM_OLD)
ld b,a
ld a,(PLATFORM_NEW)
ld c,a
ld a,b
sub c
ld (T0),a
; if PASSENGER < T0:
ld a,(PASSENGER)
ld b,a
ld a,(T0)
ld c,a
ld a,b
cp c
jp c,moving_platform_carry_L22
jp moving_platform_carry_L23
moving_platform_carry_L22:
; POSITION = MINIMUM
ld a,(MINIMUM)
ld (POSITION),a
; CLAMPED = 1
ld a,1
ld (CLAMPED),a
; return
ret
jp moving_platform_carry_L24
moving_platform_carry_L23:
moving_platform_carry_L24:
; T1 = PASSENGER - T0
ld a,(PASSENGER)
ld b,a
ld a,(T0)
ld c,a
ld a,b
sub c
ld (T1),a
; if T1 < MINIMUM:
ld a,(T1)
ld b,a
ld a,(MINIMUM)
ld c,a
ld a,b
cp c
jp c,moving_platform_carry_L25
jp moving_platform_carry_L26
moving_platform_carry_L25:
; POSITION = MINIMUM
ld a,(MINIMUM)
ld (POSITION),a
; CLAMPED = 1
ld a,1
ld (CLAMPED),a
; return
ret
jp moving_platform_carry_L27
moving_platform_carry_L26:
moving_platform_carry_L27:
; POSITION = T1
ld a,(T1)
ld (POSITION),a
moving_platform_carry_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
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:
POSITION: defb 0
CLAMPED: defb 0
VALID: defb 0
PASSENGER: defb 0
PLATFORM_OLD: defb 0
PLATFORM_NEW: defb 0
SUPPORTED: defb 0
MINIMUM: defb 0
MAXIMUM: defb 0
T0: defb 0
T1: 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
; PASSENGER = 2
ld a,2
ld (PASSENGER),a
; PLATFORM_OLD = 10
ld a,10
ld (PLATFORM_OLD),a
; PLATFORM_NEW = 0
ld a,0
ld (PLATFORM_NEW),a
; SUPPORTED = 1
ld a,1
ld (SUPPORTED),a
; MINIMUM = 0
ld a,0
ld (MINIMUM),a
; MAXIMUM = 100
ld a,100
ld (MAXIMUM),a
if BROKEN
; PLATFORM_OLD = PLATFORM_NEW
ld a,(PLATFORM_NEW)
ld (PLATFORM_OLD),a
endif
call checked_step
call emit_state
ret
else
; PASSENGER = 20
ld a,20
ld (PASSENGER),a
; PLATFORM_OLD = 5
ld a,5
ld (PLATFORM_OLD),a
; PLATFORM_NEW = 9
ld a,9
ld (PLATFORM_NEW),a
; SUPPORTED = 1
ld a,1
ld (SUPPORTED),a
; MINIMUM = 0
ld a,0
ld (MINIMUM),a
; MAXIMUM = 100
ld a,100
ld (MAXIMUM),a
if BROKEN
; PLATFORM_OLD = PLATFORM_NEW
ld a,(PLATFORM_NEW)
ld (PLATFORM_OLD),a
endif
call checked_step
call emit_state
endif
retCustomise it
Carry left past the lower world limit and observe the clamp. 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:
POSITION: defb 0
CLAMPED: defb 0
VALID: defb 0
PASSENGER: defb 0
PLATFORM_OLD: defb 0
PLATFORM_NEW: defb 0
SUPPORTED: defb 0
MINIMUM: defb 0
MAXIMUM: defb 0
T0: defb 0
T1: 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 01 02 0A 00 01 00. The full client trace in the source explains how it gets there.
Mistake and repair
The wrong caller overwrites the old platform coordinate with the new one. Its measured displacement becomes zero and the passenger stays behind. Keep both positions until passenger carry is complete.
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:
POSITION: defb 0
CLAMPED: defb 0
VALID: defb 0
PASSENGER: defb 0
PLATFORM_OLD: defb 0
PLATFORM_NEW: defb 0
SUPPORTED: defb 0
MINIMUM: defb 0
MAXIMUM: defb 0
T0: defb 0
T1: 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 349 code bytes; public state is 9 bytes and private scratch is 2 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 for one axis. A wider fixed-point position preserves subpixel platform motion; this byte version assumes both coordinates share the same unit and do not wrap.
SUPPORTED comes from the caller’s contact test. Carry alone does not resolve walls or crush situations; after moving the platform, apply carry once and resolve the passenger against other geometry. A clamp can break relative alignment, which needs a game policy.
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
One-way platforms: Support must be established using the chosen contact and pass-through rules.