Skip to content

Keep Camera Shake out of World State on C64

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.

C64 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

6502.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:
    lda #0
    sta OFFSET_X
    sta OFFSET_Y
    sta REMAINING
    sta AMPLITUDE
    sta VALID
    sta TRIGGER
    sta STRENGTH
    sta DURATION
    sta NOISE_X
    sta NOISE_Y
    sta T0
    sta T1
    rts
camera_shake_step:
; OFFSET_X = 0
    lda #0
    sta OFFSET_X
; OFFSET_Y = 0
    lda #0
    sta OFFSET_Y
; VALID = 0
    lda #0
    sta VALID
; if STRENGTH > 15:
    lda STRENGTH
    cmp #15
    beq camera_shake_L5
    bcs camera_shake_L4
camera_shake_L5:
    jmp camera_shake_L2
camera_shake_L4:
    jmp camera_shake_L1
camera_shake_L1:
; return
    rts
    jmp camera_shake_L3
camera_shake_L2:
camera_shake_L3:
; if TRIGGER == 0:
    lda TRIGGER
    cmp #0
    beq camera_shake_L9
camera_shake_L10:
    jmp camera_shake_L7
camera_shake_L9:
    jmp camera_shake_L6
camera_shake_L6:
; if AMPLITUDE > 15:
    lda AMPLITUDE
    cmp #15
    beq camera_shake_L15
    bcs camera_shake_L14
camera_shake_L15:
    jmp camera_shake_L12
camera_shake_L14:
    jmp camera_shake_L11
camera_shake_L11:
; return
    rts
    jmp camera_shake_L13
camera_shake_L12:
camera_shake_L13:
    jmp camera_shake_L8
camera_shake_L7:
camera_shake_L8:
; VALID = 1
    lda #1
    sta VALID
; if TRIGGER != 0:
    lda TRIGGER
    cmp #0
    bne camera_shake_L19
camera_shake_L20:
    jmp camera_shake_L17
camera_shake_L19:
    jmp camera_shake_L16
camera_shake_L16:
; AMPLITUDE = STRENGTH
    lda STRENGTH
    sta AMPLITUDE
; REMAINING = DURATION
    lda DURATION
    sta REMAINING
    jmp camera_shake_L18
camera_shake_L17:
camera_shake_L18:
; if REMAINING == 0:
    lda REMAINING
    cmp #0
    beq camera_shake_L24
camera_shake_L25:
    jmp camera_shake_L22
camera_shake_L24:
    jmp camera_shake_L21
camera_shake_L21:
; return
    rts
    jmp camera_shake_L23
camera_shake_L22:
camera_shake_L23:
; REMAINING = REMAINING - 1
    lda REMAINING
    sec
    sbc #1
    sta REMAINING
; if AMPLITUDE == 0:
    lda AMPLITUDE
    cmp #0
    beq camera_shake_L29
camera_shake_L30:
    jmp camera_shake_L27
camera_shake_L29:
    jmp camera_shake_L26
camera_shake_L26:
; return
    rts
    jmp camera_shake_L28
camera_shake_L27:
camera_shake_L28:
; T0 = AMPLITUDE << 1
    lda AMPLITUDE
    asl
    sta T0
; T0 = T0 + 1
    lda T0
    clc
    adc #1
    sta T0
; T1 = NOISE_X
    lda NOISE_X
    sta T1
; while T1 >= T0:
camera_shake_L31:
    lda T1
    cmp T0
    bcs camera_shake_L34
camera_shake_L35:
    jmp camera_shake_L33
camera_shake_L34:
    jmp camera_shake_L32
camera_shake_L32:
; T1 = T1 - T0
    lda T1
    sec
    sbc T0
    sta T1
    jmp camera_shake_L31
camera_shake_L33:
; OFFSET_X = T1 - AMPLITUDE
    lda T1
    sec
    sbc AMPLITUDE
    sta OFFSET_X
; T1 = NOISE_Y
    lda NOISE_Y
    sta T1
; while T1 >= T0:
camera_shake_L36:
    lda T1
    cmp T0
    bcs camera_shake_L39
camera_shake_L40:
    jmp camera_shake_L38
camera_shake_L39:
    jmp camera_shake_L37
camera_shake_L37:
; T1 = T1 - T0
    lda T1
    sec
    sbc T0
    sta T1
    jmp camera_shake_L36
camera_shake_L38:
; OFFSET_Y = T1 - AMPLITUDE
    lda T1
    sec
    sbc AMPLITUDE
    sta OFFSET_Y
    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

demo.asm
OFFSET_X=$80
OFFSET_Y=$81
REMAINING=$82
AMPLITUDE=$83
VALID=$84
TRIGGER=$85
STRENGTH=$86
DURATION=$87
NOISE_X=$88
NOISE_Y=$89
T0=$8a
T1=$8b
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

exercise.inc, excerpt
preview_rules:
!if CUSTOM {
; TRIGGER = 1
    lda #1
    sta TRIGGER
; STRENGTH = 15
    lda #15
    sta STRENGTH
; DURATION = 1
    lda #1
    sta DURATION
; NOISE_X = 255
    lda #255
    sta NOISE_X
; NOISE_Y = 0
    lda #0
    sta NOISE_Y
!if BROKEN {
; REMAINING = 0
    lda #0
    sta REMAINING
}
    jsr checked_step
    jsr emit_state
; TRIGGER = 0
    lda #0
    sta TRIGGER
!if BROKEN {
; REMAINING = 0
    lda #0
    sta REMAINING
}
    jsr checked_step
    jsr emit_state
    rts
} else {
; TRIGGER = 1
    lda #1
    sta TRIGGER
; STRENGTH = 3
    lda #3
    sta STRENGTH
; DURATION = 3
    lda #3
    sta DURATION
; NOISE_X = 0
    lda #0
    sta NOISE_X
; NOISE_Y = 6
    lda #6
    sta NOISE_Y
!if BROKEN {
; REMAINING = 0
    lda #0
    sta REMAINING
}
    jsr checked_step
    jsr emit_state
; TRIGGER = 0
    lda #0
    sta TRIGGER
; NOISE_X = 6
    lda #6
    sta NOISE_X
; NOISE_Y = 0
    lda #0
    sta NOISE_Y
!if BROKEN {
; REMAINING = 0
    lda #0
    sta REMAINING
}
    jsr checked_step
    jsr emit_state
; NOISE_X = 1
    lda #1
    sta NOISE_X
; NOISE_Y = 5
    lda #5
    sta NOISE_Y
!if BROKEN {
; REMAINING = 0
    lda #0
    sta REMAINING
}
    jsr checked_step
    jsr emit_state
}
    rts

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
OFFSET_X=$80
OFFSET_Y=$81
REMAINING=$82
AMPLITUDE=$83
VALID=$84
TRIGGER=$85
STRENGTH=$86
DURATION=$87
NOISE_X=$88
NOISE_Y=$89
T0=$8a
T1=$8b
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 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
OFFSET_X=$80
OFFSET_Y=$81
REMAINING=$82
AMPLITUDE=$83
VALID=$84
TRIGGER=$85
STRENGTH=$86
DURATION=$87
NOISE_X=$88
NOISE_Y=$89
T0=$8a
T1=$8b
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:
C64 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 235 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 PAL stock-ROM PRG, 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.