Skip to content

Keep a Small Position History on C64

A trailing companion needs where the leader was, not another guess at where it might be going.

A trailing companion needs where the leader was, not another guess at where it might be going.

C64 shows the working diagnostic row 0A 14 01 03 03 32 3C 02.
A completed native caller displays 0A 14 01 03 03 32 3C 02. 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

An eight-entry ring stores byte X/Y pairs. HEAD is the next write index 0–7; COUNT is 0–8. PUSH nonzero appends the supplied position, advances HEAD modulo eight and saturates COUNT at eight. Then query LAG entries back from newest: zero means newest. A lag outside COUNT returns VALID=0 and zero outputs but does not undo a valid append. Invalid HEAD/COUNT rejects before writing.

Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: OUT_X, OUT_Y, VALID, HEAD, COUNT, INPUT_X, INPUT_Y, LAG, PUSH, HX0, HX1, HX2, HX3, HX4, HX5, HX6, HX7, HY0, HY1, HY2, HY3, HY4, HY5, HY6, HY7. They occupy 25 contiguous bytes. Also reserve the private workspace: T0, EXPR_SAVE (2 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.

Call motion_history_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 motion_history_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 HEAD, COUNT and HX0–HX7/HY0–HY7
Supply before service INPUT_X, INPUT_Y, LAG and PUSH
Read after service OUT_X, OUT_Y and VALID

The write cursor wraps cheaply because capacity is a power of two. A lag query subtracts from the next-write cursor rather than treating physical array order as chronological order.

Reusable initialisation and service

6502.inc
; SPDX-License-Identifier: MIT
; Keep a Small Position History. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
motion_history_init:
    lda #0
    sta OUT_X
    sta OUT_Y
    sta VALID
    sta HEAD
    sta COUNT
    sta INPUT_X
    sta INPUT_Y
    sta LAG
    sta PUSH
    sta HX0
    sta HX1
    sta HX2
    sta HX3
    sta HX4
    sta HX5
    sta HX6
    sta HX7
    sta HY0
    sta HY1
    sta HY2
    sta HY3
    sta HY4
    sta HY5
    sta HY6
    sta HY7
    sta T0
    sta EXPR_SAVE
    rts
motion_history_step:
; OUT_X = 0
    lda #0
    sta OUT_X
; OUT_Y = 0
    lda #0
    sta OUT_Y
; VALID = 0
    lda #0
    sta VALID
; if HEAD >= 8:
    lda HEAD
    cmp #8
    bcs motion_history_L4
motion_history_L5:
    jmp motion_history_L2
motion_history_L4:
    jmp motion_history_L1
motion_history_L1:
; return
    rts
    jmp motion_history_L3
motion_history_L2:
motion_history_L3:
; if COUNT > 8:
    lda COUNT
    cmp #8
    beq motion_history_L10
    bcs motion_history_L9
motion_history_L10:
    jmp motion_history_L7
motion_history_L9:
    jmp motion_history_L6
motion_history_L6:
; return
    rts
    jmp motion_history_L8
motion_history_L7:
motion_history_L8:
; if PUSH != 0:
    lda PUSH
    cmp #0
    bne motion_history_L14
motion_history_L15:
    jmp motion_history_L12
motion_history_L14:
    jmp motion_history_L11
motion_history_L11:
; HX[HEAD] = INPUT_X
    lda INPUT_X
    sta EXPR_SAVE
    ldx HEAD
    lda EXPR_SAVE
    sta HX0,x
; HY[HEAD] = INPUT_Y
    lda INPUT_Y
    sta EXPR_SAVE
    ldx HEAD
    lda EXPR_SAVE
    sta HY0,x
; HEAD = HEAD + 1
    lda HEAD
    clc
    adc #1
    sta HEAD
; HEAD = HEAD & 7
    lda HEAD
    and #7
    sta HEAD
; if COUNT < 8:
    lda COUNT
    cmp #8
    bcc motion_history_L19
motion_history_L20:
    jmp motion_history_L17
motion_history_L19:
    jmp motion_history_L16
motion_history_L16:
; COUNT = COUNT + 1
    lda COUNT
    clc
    adc #1
    sta COUNT
    jmp motion_history_L18
motion_history_L17:
motion_history_L18:
    jmp motion_history_L13
motion_history_L12:
motion_history_L13:
; if LAG >= COUNT:
    lda LAG
    cmp COUNT
    bcs motion_history_L24
motion_history_L25:
    jmp motion_history_L22
motion_history_L24:
    jmp motion_history_L21
motion_history_L21:
; return
    rts
    jmp motion_history_L23
motion_history_L22:
motion_history_L23:
; T0 = HEAD - 1
    lda HEAD
    sec
    sbc #1
    sta T0
; T0 = T0 - LAG
    lda T0
    sec
    sbc LAG
    sta T0
; T0 = T0 & 7
    lda T0
    and #7
    sta T0
; OUT_X = HX[T0]
    ldx T0
    lda HX0,x
    sta OUT_X
; OUT_Y = HY[T0]
    ldx T0
    lda HY0,x
    sta OUT_Y
; VALID = 1
    lda #1
    sta VALID
    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
OUT_X=$80
OUT_Y=$81
VALID=$82
HEAD=$83
COUNT=$84
INPUT_X=$85
INPUT_Y=$86
LAG=$87
PUSH=$88
HX0=$89
HX1=$8a
HX2=$8b
HX3=$8c
HX4=$8d
HX5=$8e
HX6=$8f
HX7=$90
HY0=$91
HY1=$92
HY2=$93
HY3=$94
HY4=$95
HY5=$96
HY6=$97
HY7=$98
T0=$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

exercise.inc, excerpt
preview_rules:
!if CUSTOM {
; INPUT_X = 0
    lda #0
    sta INPUT_X
; INPUT_Y = 1
    lda #1
    sta INPUT_Y
; PUSH = 1
    lda #1
    sta PUSH
!if BROKEN {
; HEAD = 0
    lda #0
    sta HEAD
}
    jsr checked_step
    jsr emit_state
; INPUT_X = 1
    lda #1
    sta INPUT_X
; INPUT_Y = 2
    lda #2
    sta INPUT_Y
; PUSH = 1
    lda #1
    sta PUSH
!if BROKEN {
; HEAD = 0
    lda #0
    sta HEAD
}
    jsr checked_step
    jsr emit_state
; INPUT_X = 2
    lda #2
    sta INPUT_X
; INPUT_Y = 3
    lda #3
    sta INPUT_Y
; PUSH = 1
    lda #1
    sta PUSH
!if BROKEN {
; HEAD = 0
    lda #0
    sta HEAD
}
    jsr checked_step
    jsr emit_state
; INPUT_X = 3
    lda #3
    sta INPUT_X
; INPUT_Y = 4
    lda #4
    sta INPUT_Y
; PUSH = 1
    lda #1
    sta PUSH
!if BROKEN {
; HEAD = 0
    lda #0
    sta HEAD
}
    jsr checked_step
    jsr emit_state
; INPUT_X = 4
    lda #4
    sta INPUT_X
; INPUT_Y = 5
    lda #5
    sta INPUT_Y
; PUSH = 1
    lda #1
    sta PUSH
!if BROKEN {
; HEAD = 0
    lda #0
    sta HEAD
}
    jsr checked_step
    jsr emit_state
; INPUT_X = 5
    lda #5
    sta INPUT_X
; INPUT_Y = 6
    lda #6
    sta INPUT_Y
; PUSH = 1
    lda #1
    sta PUSH
!if BROKEN {
; HEAD = 0
    lda #0
    sta HEAD
}
    jsr checked_step
    jsr emit_state
; INPUT_X = 6
    lda #6
    sta INPUT_X
; INPUT_Y = 7
    lda #7
    sta INPUT_Y
; PUSH = 1
    lda #1
    sta PUSH
!if BROKEN {
; HEAD = 0
    lda #0
    sta HEAD
}
    jsr checked_step
    jsr emit_state
; INPUT_X = 7
    lda #7
    sta INPUT_X
; INPUT_Y = 8
    lda #8
    sta INPUT_Y
; PUSH = 1
    lda #1
    sta PUSH
!if BROKEN {
; HEAD = 0
    lda #0
    sta HEAD
}
    jsr checked_step
    jsr emit_state
; INPUT_X = 8
    lda #8
    sta INPUT_X
; INPUT_Y = 9
    lda #9
    sta INPUT_Y
; PUSH = 1
    lda #1
    sta PUSH
!if BROKEN {
; HEAD = 0
    lda #0
    sta HEAD
}
    jsr checked_step
    jsr emit_state
; PUSH = 0
    lda #0
    sta PUSH
; LAG = 7
    lda #7
    sta LAG
!if BROKEN {
; HEAD = 0
    lda #0
    sta HEAD
}
    jsr checked_step
    jsr emit_state
    rts
} else {
; INPUT_X = 10
    lda #10
    sta INPUT_X
; INPUT_Y = 20
    lda #20
    sta INPUT_Y
; PUSH = 1
    lda #1
    sta PUSH
!if BROKEN {
; HEAD = 0
    lda #0
    sta HEAD
}
    jsr checked_step
    jsr emit_state
; INPUT_X = 30
    lda #30
    sta INPUT_X
; INPUT_Y = 40
    lda #40
    sta INPUT_Y
!if BROKEN {
; HEAD = 0
    lda #0
    sta HEAD
}
    jsr checked_step
    jsr emit_state
; INPUT_X = 50
    lda #50
    sta INPUT_X
; INPUT_Y = 60
    lda #60
    sta INPUT_Y
!if BROKEN {
; HEAD = 0
    lda #0
    sta HEAD
}
    jsr checked_step
    jsr emit_state
; PUSH = 0
    lda #0
    sta PUSH
; LAG = 2
    lda #2
    sta LAG
!if BROKEN {
; HEAD = 0
    lda #0
    sta HEAD
}
    jsr checked_step
    jsr emit_state
}
    rts

Customise it

Append nine positions, then query the oldest retained entry after wrap. 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
OUT_X=$80
OUT_Y=$81
VALID=$82
HEAD=$83
COUNT=$84
INPUT_X=$85
INPUT_Y=$86
LAG=$87
PUSH=$88
HX0=$89
HX1=$8a
HX2=$8b
HX3=$8c
HX4=$8d
HX5=$8e
HX6=$8f
HX7=$90
HY0=$91
HY1=$92
HY2=$93
HY3=$94
HY4=$95
HY5=$96
HY6=$97
HY7=$98
T0=$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 01 02 01 01 08 08 09 07. The full client trace in the source explains how it gets there.

Mistake and repair

The wrong caller resets HEAD on every service. New positions overwrite one slot and older queries read unrelated entries. Keep the ring cursor alongside the stored history.

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
OUT_X=$80
OUT_Y=$81
VALID=$82
HEAD=$83
COUNT=$84
INPUT_X=$85
INPUT_Y=$86
LAG=$87
PUSH=$88
HX0=$89
HX1=$8a
HX2=$8b
HX3=$8c
HX4=$8d
HX5=$8e
HX6=$8f
HX7=$90
HY0=$91
HY1=$92
HY2=$93
HY3=$94
HY4=$95
HY5=$96
HY6=$97
HY7=$98
T0=$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:
C64 shows the deliberate error row 00 00 01 00 03 32 3C 02.
The wrong client completes with 00 00 01 00 03 32 3C 02; its changed inputs and state explain the result.

Cost and alternatives

The measured helper extent is 226 code bytes; public state is 25 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 without shifting old positions. Eight pairs cost sixteen data bytes. A timestamped history costs more storage but can answer elapsed-time rather than sample-age queries.

These are serviced samples, not time-stamped seconds. Variable update spacing changes the time represented by a lag. Interpolation, full-state rewind and collision-safe followers require additional state and rules.

Execution evidence

Four variants run on PAL stock-ROM PRG, using Asm198x 0.0.72, Emu198x 0.44.2. Each executes 40 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

Event queues: Both use bounded storage, but a history retains recent samples for age queries rather than consuming queued events.

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