Skip to content

Keep Camera Shake out of World State on Spectrum

An impact can shake the view without moving the player, the level or the collision geometry.

An impact can shake the view without moving the player, the level or the collision geometry.

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

TRIGGER reloads AMPLITUDE from STRENGTH 0–15 and REMAINING from DURATION. While a lifetime tick remains, map each caller-supplied noise byte into −AMPLITUDE..+AMPLITUDE, return two’s-complement OFFSET_X/Y, then decrement REMAINING. Expired shake returns zero offsets. Invalid STRENGTH clears only outputs/VALID and leaves retained lifetime/amplitude unchanged. Existing AMPLITUDE must also be 0–15 when not retriggering.

Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: OFFSET_X, OFFSET_Y, REMAINING, AMPLITUDE, VALID, TRIGGER, STRENGTH, DURATION, NOISE_X, NOISE_Y. They occupy 10 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 camera_shake_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 camera_shake_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 REMAINING and AMPLITUDE
Supply before service TRIGGER, STRENGTH, DURATION and NOISE_X/Y
Read after service OFFSET_X/Y and VALID

Separate the lifetime from the world camera. Reduce noise into a bounded span, centre it around zero and leave authoritative world state untouched.

Reusable initialisation and service

component.inc
; SPDX-License-Identifier: MIT
; Keep Camera Shake out of World State. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
camera_shake_init:
    ld a,0
    ld (OFFSET_X),a
    ld (OFFSET_Y),a
    ld (REMAINING),a
    ld (AMPLITUDE),a
    ld (VALID),a
    ld (TRIGGER),a
    ld (STRENGTH),a
    ld (DURATION),a
    ld (NOISE_X),a
    ld (NOISE_Y),a
    ld (T0),a
    ld (T1),a
    ret
camera_shake_step:
; OFFSET_X = 0
    ld a,0
    ld (OFFSET_X),a
; OFFSET_Y = 0
    ld a,0
    ld (OFFSET_Y),a
; VALID = 0
    ld a,0
    ld (VALID),a
; if STRENGTH > 15:
    ld a,(STRENGTH)
    cp 15
    jp z,camera_shake_L2
    jp nc,camera_shake_L1
    jp camera_shake_L2
camera_shake_L1:
; return
    ret
    jp camera_shake_L3
camera_shake_L2:
camera_shake_L3:
; if TRIGGER == 0:
    ld a,(TRIGGER)
    cp 0
    jp z,camera_shake_L4
    jp camera_shake_L5
camera_shake_L4:
; if AMPLITUDE > 15:
    ld a,(AMPLITUDE)
    cp 15
    jp z,camera_shake_L8
    jp nc,camera_shake_L7
    jp camera_shake_L8
camera_shake_L7:
; return
    ret
    jp camera_shake_L9
camera_shake_L8:
camera_shake_L9:
    jp camera_shake_L6
camera_shake_L5:
camera_shake_L6:
; VALID = 1
    ld a,1
    ld (VALID),a
; if TRIGGER != 0:
    ld a,(TRIGGER)
    cp 0
    jp nz,camera_shake_L10
    jp camera_shake_L11
camera_shake_L10:
; AMPLITUDE = STRENGTH
    ld a,(STRENGTH)
    ld (AMPLITUDE),a
; REMAINING = DURATION
    ld a,(DURATION)
    ld (REMAINING),a
    jp camera_shake_L12
camera_shake_L11:
camera_shake_L12:
; if REMAINING == 0:
    ld a,(REMAINING)
    cp 0
    jp z,camera_shake_L13
    jp camera_shake_L14
camera_shake_L13:
; return
    ret
    jp camera_shake_L15
camera_shake_L14:
camera_shake_L15:
; REMAINING = REMAINING - 1
    ld a,(REMAINING)
    sub 1
    ld (REMAINING),a
; if AMPLITUDE == 0:
    ld a,(AMPLITUDE)
    cp 0
    jp z,camera_shake_L16
    jp camera_shake_L17
camera_shake_L16:
; return
    ret
    jp camera_shake_L18
camera_shake_L17:
camera_shake_L18:
; T0 = AMPLITUDE << 1
    ld a,(AMPLITUDE)
    sla a
    ld (T0),a
; T0 = T0 + 1
    ld a,(T0)
    add a,1
    ld (T0),a
; T1 = NOISE_X
    ld a,(NOISE_X)
    ld (T1),a
; while T1 >= T0:
camera_shake_L19:
    ld a,(T1)
    ld b,a
    ld a,(T0)
    ld c,a
    ld a,b
    cp c
    jp nc,camera_shake_L20
    jp camera_shake_L21
camera_shake_L20:
; T1 = T1 - T0
    ld a,(T1)
    ld b,a
    ld a,(T0)
    ld c,a
    ld a,b
    sub c
    ld (T1),a
    jp camera_shake_L19
camera_shake_L21:
; OFFSET_X = T1 - AMPLITUDE
    ld a,(T1)
    ld b,a
    ld a,(AMPLITUDE)
    ld c,a
    ld a,b
    sub c
    ld (OFFSET_X),a
; T1 = NOISE_Y
    ld a,(NOISE_Y)
    ld (T1),a
; while T1 >= T0:
camera_shake_L22:
    ld a,(T1)
    ld b,a
    ld a,(T0)
    ld c,a
    ld a,b
    cp c
    jp nc,camera_shake_L23
    jp camera_shake_L24
camera_shake_L23:
; T1 = T1 - T0
    ld a,(T1)
    ld b,a
    ld a,(T0)
    ld c,a
    ld a,b
    sub c
    ld (T1),a
    jp camera_shake_L22
camera_shake_L24:
; OFFSET_Y = T1 - AMPLITUDE
    ld a,(T1)
    ld b,a
    ld a,(AMPLITUDE)
    ld c,a
    ld a,b
    sub c
    ld (OFFSET_Y),a
    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:
OFFSET_X: defb 0
OFFSET_Y: defb 0
REMAINING: defb 0
AMPLITUDE: defb 0
VALID: defb 0
TRIGGER: defb 0
STRENGTH: defb 0
DURATION: defb 0
NOISE_X: defb 0
NOISE_Y: 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

A client retaining state between calls

exercise.inc, excerpt
preview_rules:
    if CUSTOM
; TRIGGER = 1
    ld a,1
    ld (TRIGGER),a
; STRENGTH = 15
    ld a,15
    ld (STRENGTH),a
; DURATION = 1
    ld a,1
    ld (DURATION),a
; NOISE_X = 255
    ld a,255
    ld (NOISE_X),a
; NOISE_Y = 0
    ld a,0
    ld (NOISE_Y),a
    if BROKEN
; REMAINING = 0
    ld a,0
    ld (REMAINING),a
    endif
    call checked_step
    call emit_state
; TRIGGER = 0
    ld a,0
    ld (TRIGGER),a
    if BROKEN
; REMAINING = 0
    ld a,0
    ld (REMAINING),a
    endif
    call checked_step
    call emit_state
    ret
    else
; TRIGGER = 1
    ld a,1
    ld (TRIGGER),a
; STRENGTH = 3
    ld a,3
    ld (STRENGTH),a
; DURATION = 3
    ld a,3
    ld (DURATION),a
; NOISE_X = 0
    ld a,0
    ld (NOISE_X),a
; NOISE_Y = 6
    ld a,6
    ld (NOISE_Y),a
    if BROKEN
; REMAINING = 0
    ld a,0
    ld (REMAINING),a
    endif
    call checked_step
    call emit_state
; TRIGGER = 0
    ld a,0
    ld (TRIGGER),a
; NOISE_X = 6
    ld a,6
    ld (NOISE_X),a
; NOISE_Y = 0
    ld a,0
    ld (NOISE_Y),a
    if BROKEN
; REMAINING = 0
    ld a,0
    ld (REMAINING),a
    endif
    call checked_step
    call emit_state
; NOISE_X = 1
    ld a,1
    ld (NOISE_X),a
; NOISE_Y = 5
    ld a,5
    ld (NOISE_Y),a
    if BROKEN
; REMAINING = 0
    ld a,0
    ld (REMAINING),a
    endif
    call checked_step
    call emit_state
    endif
    ret

Customise it

Use the largest supported amplitude for a single-tick impulse. 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:
OFFSET_X: defb 0
OFFSET_Y: defb 0
REMAINING: defb 0
AMPLITUDE: defb 0
VALID: defb 0
TRIGGER: defb 0
STRENGTH: defb 0
DURATION: defb 0
NOISE_X: defb 0
NOISE_Y: 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

The custom diagnostic row is 00 00 00 0F 01 00 0F 01. The full client trace in the source explains how it gets there.

Mistake and repair

The wrong caller clears the lifetime on every tick. The trigger produces one offset, then shake disappears even though its configured duration has not elapsed. Retain REMAINING and apply offsets only to rendering coordinates.

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:
OFFSET_X: defb 0
OFFSET_Y: defb 0
REMAINING: defb 0
AMPLITUDE: defb 0
VALID: defb 0
TRIGGER: defb 0
STRENGTH: defb 0
DURATION: defb 0
NOISE_X: defb 0
NOISE_Y: 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
Spectrum shows the deliberate error row 00 00 00 03 01 00 03 03.
The wrong client completes with 00 00 00 03 01 00 03 03; its changed inputs and state explain the result.

Cost and alternatives

The measured helper extent is 292 code bytes; public state is 10 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.

Modulo by subtraction takes at most 85 iterations per nonzero-amplitude axis. A lookup table or masked power-of-two distribution is faster with different storage or visual distribution. No frame budget is claimed.

The helper does not generate random values, write a camera position or fade amplitude. Noise reduction is biased when the span does not divide 256; that is an explicit visual choice, not an unbiased random-selection routine. Supply a separate visual noise stream if gameplay randomness must remain unchanged.

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

Seeded random values: Choose a separate stream or deterministic samples when visual effects must not perturb game rules.

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