Skip to content

Choose an Eight-way Direction on C64

A character can face a target in eight directions without angles, trigonometry or a lookup table.

A character can face a target in eight directions without angles, trigonometry or a lookup table.

C64 shows the working diagnostic row 03 0A 03 00 00 0A 03 00.
A completed native caller displays 03 0A 03 00 00 0A 03 00. 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

Return DIRECTION=0 when points coincide, otherwise 1 north, 2 north-east, 3 east, 4 south-east, 5 south, 6 south-west, 7 west, 8 north-west. Screen Y increases downwards. Choose a cardinal direction when its absolute displacement is at least twice the other; remaining vectors are diagonal. DX/DY return unsigned magnitudes.

Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: DIRECTION, DX, DY, FROM_X, FROM_Y, TO_X, TO_Y. They occupy 7 contiguous bytes. Also reserve the private workspace: T0, T1, T2 (3 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.

Call eight_way_direction_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 eight_way_direction_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 FROM_X/Y and TO_X/Y
Read after service DIRECTION and DX/DY

Compare a magnitude with half the other to avoid overflow from doubling a byte. Two sign flags select the quadrant after choosing horizontal, vertical or diagonal.

Reusable initialisation and service

6502.inc
; SPDX-License-Identifier: MIT
; Choose an Eight-way Direction. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
eight_way_direction_init:
    lda #0
    sta DIRECTION
    sta DX
    sta DY
    sta FROM_X
    sta FROM_Y
    sta TO_X
    sta TO_Y
    sta T0
    sta T1
    sta T2
    rts
eight_way_direction_step:
; T0 = 0
    lda #0
    sta T0
; T1 = 0
    lda #0
    sta T1
; if TO_X >= FROM_X:
    lda TO_X
    cmp FROM_X
    bcs eight_way_direction_L4
eight_way_direction_L5:
    jmp eight_way_direction_L2
eight_way_direction_L4:
    jmp eight_way_direction_L1
eight_way_direction_L1:
; DX = TO_X - FROM_X
    lda TO_X
    sec
    sbc FROM_X
    sta DX
    jmp eight_way_direction_L3
eight_way_direction_L2:
; DX = FROM_X - TO_X
    lda FROM_X
    sec
    sbc TO_X
    sta DX
; T0 = 1
    lda #1
    sta T0
eight_way_direction_L3:
; if TO_Y >= FROM_Y:
    lda TO_Y
    cmp FROM_Y
    bcs eight_way_direction_L9
eight_way_direction_L10:
    jmp eight_way_direction_L7
eight_way_direction_L9:
    jmp eight_way_direction_L6
eight_way_direction_L6:
; DY = TO_Y - FROM_Y
    lda TO_Y
    sec
    sbc FROM_Y
    sta DY
    jmp eight_way_direction_L8
eight_way_direction_L7:
; DY = FROM_Y - TO_Y
    lda FROM_Y
    sec
    sbc TO_Y
    sta DY
; T1 = 1
    lda #1
    sta T1
eight_way_direction_L8:
; DIRECTION = 0
    lda #0
    sta DIRECTION
; if DX == 0:
    lda DX
    cmp #0
    beq eight_way_direction_L14
eight_way_direction_L15:
    jmp eight_way_direction_L12
eight_way_direction_L14:
    jmp eight_way_direction_L11
eight_way_direction_L11:
; if DY == 0:
    lda DY
    cmp #0
    beq eight_way_direction_L19
eight_way_direction_L20:
    jmp eight_way_direction_L17
eight_way_direction_L19:
    jmp eight_way_direction_L16
eight_way_direction_L16:
; return
    rts
    jmp eight_way_direction_L18
eight_way_direction_L17:
eight_way_direction_L18:
    jmp eight_way_direction_L13
eight_way_direction_L12:
eight_way_direction_L13:
; T2 = DY >> 1
    lda DY
    lsr
    sta T2
; if DX <= T2:
    lda DX
    cmp T2
    bcc eight_way_direction_L24
    beq eight_way_direction_L24
eight_way_direction_L25:
    jmp eight_way_direction_L22
eight_way_direction_L24:
    jmp eight_way_direction_L21
eight_way_direction_L21:
; DIRECTION = 5
    lda #5
    sta DIRECTION
; if T1 != 0:
    lda T1
    cmp #0
    bne eight_way_direction_L29
eight_way_direction_L30:
    jmp eight_way_direction_L27
eight_way_direction_L29:
    jmp eight_way_direction_L26
eight_way_direction_L26:
; DIRECTION = 1
    lda #1
    sta DIRECTION
    jmp eight_way_direction_L28
eight_way_direction_L27:
eight_way_direction_L28:
; return
    rts
    jmp eight_way_direction_L23
eight_way_direction_L22:
eight_way_direction_L23:
; T2 = DX >> 1
    lda DX
    lsr
    sta T2
; if DY <= T2:
    lda DY
    cmp T2
    bcc eight_way_direction_L34
    beq eight_way_direction_L34
eight_way_direction_L35:
    jmp eight_way_direction_L32
eight_way_direction_L34:
    jmp eight_way_direction_L31
eight_way_direction_L31:
; DIRECTION = 3
    lda #3
    sta DIRECTION
; if T0 != 0:
    lda T0
    cmp #0
    bne eight_way_direction_L39
eight_way_direction_L40:
    jmp eight_way_direction_L37
eight_way_direction_L39:
    jmp eight_way_direction_L36
eight_way_direction_L36:
; DIRECTION = 7
    lda #7
    sta DIRECTION
    jmp eight_way_direction_L38
eight_way_direction_L37:
eight_way_direction_L38:
; return
    rts
    jmp eight_way_direction_L33
eight_way_direction_L32:
eight_way_direction_L33:
; DIRECTION = 4
    lda #4
    sta DIRECTION
; if T0 != 0:
    lda T0
    cmp #0
    bne eight_way_direction_L44
eight_way_direction_L45:
    jmp eight_way_direction_L42
eight_way_direction_L44:
    jmp eight_way_direction_L41
eight_way_direction_L41:
; DIRECTION = 6
    lda #6
    sta DIRECTION
    jmp eight_way_direction_L43
eight_way_direction_L42:
eight_way_direction_L43:
; if T1 != 0:
    lda T1
    cmp #0
    bne eight_way_direction_L49
eight_way_direction_L50:
    jmp eight_way_direction_L47
eight_way_direction_L49:
    jmp eight_way_direction_L46
eight_way_direction_L46:
; DIRECTION = 2
    lda #2
    sta DIRECTION
; if T0 != 0:
    lda T0
    cmp #0
    bne eight_way_direction_L54
eight_way_direction_L55:
    jmp eight_way_direction_L52
eight_way_direction_L54:
    jmp eight_way_direction_L51
eight_way_direction_L51:
; DIRECTION = 8
    lda #8
    sta DIRECTION
    jmp eight_way_direction_L53
eight_way_direction_L52:
eight_way_direction_L53:
    jmp eight_way_direction_L48
eight_way_direction_L47:
eight_way_direction_L48:
    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
DIRECTION=$80
DX=$81
DY=$82
FROM_X=$83
FROM_Y=$84
TO_X=$85
TO_Y=$86
T0=$87
T1=$88
T2=$89
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 {
; FROM_X = 100
    lda #100
    sta FROM_X
; FROM_Y = 100
    lda #100
    sta FROM_Y
; TO_X = 90
    lda #90
    sta TO_X
; TO_Y = 90
    lda #90
    sta TO_Y
!if BROKEN {
; TO_Y = FROM_Y
    lda FROM_Y
    sta TO_Y
}
    jsr checked_step
    jsr emit_state
    rts
} else {
; TO_X = 10
    lda #10
    sta TO_X
; TO_Y = 3
    lda #3
    sta TO_Y
!if BROKEN {
; TO_Y = FROM_Y
    lda FROM_Y
    sta TO_Y
}
    jsr checked_step
    jsr emit_state
}
    rts

Customise it

Face a target north-west of the origin. 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
DIRECTION=$80
DX=$81
DY=$82
FROM_X=$83
FROM_Y=$84
TO_X=$85
TO_Y=$86
T0=$87
T1=$88
T2=$89
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 08 0A 0A 64 64 5A 5A 00. The full client trace in the source explains how it gets there.

Mistake and repair

The wrong caller flattens the target’s vertical coordinate. DY becomes zero instead of three in the normal client; a steeper vector can also acquire the wrong facing. Preserve both 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
DIRECTION=$80
DX=$81
DY=$82
FROM_X=$83
FROM_Y=$84
TO_X=$85
TO_Y=$86
T0=$87
T1=$88
T2=$89
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 03 0A 00 00 00 0A 00 00.
The wrong client completes with 03 0A 00 00 00 0A 00 00; its changed inputs and state explain the result.

Cost and alternatives

The measured helper extent is 286 code bytes; public state is 7 bytes and private scratch is 3 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 table. A finer angular quantiser can use a ratio table; continuous steering needs a different representation.

These sectors use a 2:1 slope threshold, not exact 22.5-degree boundaries. This selects facing; it does not normalise movement speed.

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

Four-way input: Facing selection and movement constraints can use different direction sets.

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