A turret can fire towards the player without a trigonometry table or floating-point division.

Use it in a project
Coordinates are unsigned bytes. SPEED is 0–15. Return signed two’s-complement velocity bytes using floor(abs(delta)*SPEED/max(abs(dx),abs(dy))) on each axis, restoring each sign. A coincident target returns zero velocity with VALID=1. Invalid speed clears both outputs and VALID. This uses the maximum norm: the dominant axis has SPEED magnitude, so diagonal Euclidean speed can be up to sqrt(2) times larger.
Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: VELOCITY_X, VELOCITY_Y, VALID, ORIGIN_X, ORIGIN_Y, TARGET_X, TARGET_Y, SPEED. They occupy 8 contiguous bytes. Also reserve the private workspace: T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 (11 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.
Call aimed_projectile_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 aimed_projectile_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 | origin, target and SPEED |
| Read after service | VELOCITY_X/Y and VALID |
Repeated bounded remainder accumulation computes the scale without overflowing a byte product: compare against denominator minus numerator before each add. Signs are stored separately until the final result. This is a direct link to vector normalisation and norms in Maths for Games.
Reusable initialisation and service
; SPDX-License-Identifier: MIT
; Aim a Projectile with Bounded Integer Speed. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
aimed_projectile_init:
lda #0
sta VELOCITY_X
sta VELOCITY_Y
sta VALID
sta ORIGIN_X
sta ORIGIN_Y
sta TARGET_X
sta TARGET_Y
sta SPEED
sta T0
sta T1
sta T2
sta T3
sta T4
sta T5
sta T6
sta T7
sta T8
sta T9
sta T10
rts
aimed_projectile_step:
; VELOCITY_X = 0
lda #0
sta VELOCITY_X
; VELOCITY_Y = 0
lda #0
sta VELOCITY_Y
; VALID = 0
lda #0
sta VALID
; if SPEED > 15:
lda SPEED
cmp #15
beq aimed_projectile_L5
bcs aimed_projectile_L4
aimed_projectile_L5:
jmp aimed_projectile_L2
aimed_projectile_L4:
jmp aimed_projectile_L1
aimed_projectile_L1:
; return
rts
jmp aimed_projectile_L3
aimed_projectile_L2:
aimed_projectile_L3:
; VALID = 1
lda #1
sta VALID
; if TARGET_X >= ORIGIN_X:
lda TARGET_X
cmp ORIGIN_X
bcs aimed_projectile_L9
aimed_projectile_L10:
jmp aimed_projectile_L7
aimed_projectile_L9:
jmp aimed_projectile_L6
aimed_projectile_L6:
; T0 = TARGET_X - ORIGIN_X
lda TARGET_X
sec
sbc ORIGIN_X
sta T0
; T2 = 0
lda #0
sta T2
jmp aimed_projectile_L8
aimed_projectile_L7:
; T0 = ORIGIN_X - TARGET_X
lda ORIGIN_X
sec
sbc TARGET_X
sta T0
; T2 = 1
lda #1
sta T2
aimed_projectile_L8:
; if TARGET_Y >= ORIGIN_Y:
lda TARGET_Y
cmp ORIGIN_Y
bcs aimed_projectile_L14
aimed_projectile_L15:
jmp aimed_projectile_L12
aimed_projectile_L14:
jmp aimed_projectile_L11
aimed_projectile_L11:
; T1 = TARGET_Y - ORIGIN_Y
lda TARGET_Y
sec
sbc ORIGIN_Y
sta T1
; T3 = 0
lda #0
sta T3
jmp aimed_projectile_L13
aimed_projectile_L12:
; T1 = ORIGIN_Y - TARGET_Y
lda ORIGIN_Y
sec
sbc TARGET_Y
sta T1
; T3 = 1
lda #1
sta T3
aimed_projectile_L13:
; T4 = T0
lda T0
sta T4
; if T1 > T4:
lda T1
cmp T4
beq aimed_projectile_L20
bcs aimed_projectile_L19
aimed_projectile_L20:
jmp aimed_projectile_L17
aimed_projectile_L19:
jmp aimed_projectile_L16
aimed_projectile_L16:
; T4 = T1
lda T1
sta T4
jmp aimed_projectile_L18
aimed_projectile_L17:
aimed_projectile_L18:
; if T4 == 0:
lda T4
cmp #0
beq aimed_projectile_L24
aimed_projectile_L25:
jmp aimed_projectile_L22
aimed_projectile_L24:
jmp aimed_projectile_L21
aimed_projectile_L21:
; return
rts
jmp aimed_projectile_L23
aimed_projectile_L22:
aimed_projectile_L23:
; T5 = 0
lda #0
sta T5
; T6 = 0
lda #0
sta T6
; T7 = 0
lda #0
sta T7
; T8 = 0
lda #0
sta T8
; T9 = 0
lda #0
sta T9
; while T5 < SPEED:
aimed_projectile_L26:
lda T5
cmp SPEED
bcc aimed_projectile_L29
aimed_projectile_L30:
jmp aimed_projectile_L28
aimed_projectile_L29:
jmp aimed_projectile_L27
aimed_projectile_L27:
; T10 = T4 - T0
lda T4
sec
sbc T0
sta T10
; if T6 >= T10:
lda T6
cmp T10
bcs aimed_projectile_L34
aimed_projectile_L35:
jmp aimed_projectile_L32
aimed_projectile_L34:
jmp aimed_projectile_L31
aimed_projectile_L31:
; T6 = T6 - T10
lda T6
sec
sbc T10
sta T6
; T7 = T7 + 1
lda T7
clc
adc #1
sta T7
jmp aimed_projectile_L33
aimed_projectile_L32:
; T6 = T6 + T0
lda T6
clc
adc T0
sta T6
aimed_projectile_L33:
; T10 = T4 - T1
lda T4
sec
sbc T1
sta T10
; if T8 >= T10:
lda T8
cmp T10
bcs aimed_projectile_L39
aimed_projectile_L40:
jmp aimed_projectile_L37
aimed_projectile_L39:
jmp aimed_projectile_L36
aimed_projectile_L36:
; T8 = T8 - T10
lda T8
sec
sbc T10
sta T8
; T9 = T9 + 1
lda T9
clc
adc #1
sta T9
jmp aimed_projectile_L38
aimed_projectile_L37:
; T8 = T8 + T1
lda T8
clc
adc T1
sta T8
aimed_projectile_L38:
; T5 = T5 + 1
lda T5
clc
adc #1
sta T5
jmp aimed_projectile_L26
aimed_projectile_L28:
; VELOCITY_X = T7
lda T7
sta VELOCITY_X
; VELOCITY_Y = T9
lda T9
sta VELOCITY_Y
; if T2 != 0:
lda T2
cmp #0
bne aimed_projectile_L44
aimed_projectile_L45:
jmp aimed_projectile_L42
aimed_projectile_L44:
jmp aimed_projectile_L41
aimed_projectile_L41:
; VELOCITY_X = 0 - T7
lda #0
sec
sbc T7
sta VELOCITY_X
jmp aimed_projectile_L43
aimed_projectile_L42:
aimed_projectile_L43:
; if T3 != 0:
lda T3
cmp #0
bne aimed_projectile_L49
aimed_projectile_L50:
jmp aimed_projectile_L47
aimed_projectile_L49:
jmp aimed_projectile_L46
aimed_projectile_L46:
; VELOCITY_Y = 0 - T9
lda #0
sec
sbc T9
sta VELOCITY_Y
jmp aimed_projectile_L48
aimed_projectile_L47:
aimed_projectile_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
VELOCITY_X=$80
VELOCITY_Y=$81
VALID=$82
ORIGIN_X=$83
ORIGIN_Y=$84
TARGET_X=$85
TARGET_Y=$86
SPEED=$87
T0=$88
T1=$89
T2=$8a
T3=$8b
T4=$8c
T5=$8d
T6=$8e
T7=$8f
T8=$90
T9=$91
T10=$92
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
preview_rules:
!if CUSTOM {
; ORIGIN_X = 0
lda #0
sta ORIGIN_X
; ORIGIN_Y = 0
lda #0
sta ORIGIN_Y
; TARGET_X = 255
lda #255
sta TARGET_X
; TARGET_Y = 128
lda #128
sta TARGET_Y
; SPEED = 15
lda #15
sta SPEED
!if BROKEN {
; TARGET_X = ORIGIN_X
lda ORIGIN_X
sta TARGET_X
}
jsr checked_step
jsr emit_state
rts
} else {
; ORIGIN_X = 100
lda #100
sta ORIGIN_X
; ORIGIN_Y = 100
lda #100
sta ORIGIN_Y
; TARGET_X = 80
lda #80
sta TARGET_X
; TARGET_Y = 90
lda #90
sta TARGET_Y
; SPEED = 6
lda #6
sta SPEED
!if BROKEN {
; TARGET_X = ORIGIN_X
lda ORIGIN_X
sta TARGET_X
}
jsr checked_step
jsr emit_state
}
rtsCustomise it
Aim across the full byte range with a fifteen-unit dominant-axis speed. 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
VELOCITY_X=$80
VELOCITY_Y=$81
VALID=$82
ORIGIN_X=$83
ORIGIN_Y=$84
TARGET_X=$85
TARGET_Y=$86
SPEED=$87
T0=$88
T1=$89
T2=$8a
T3=$8b
T4=$8c
T5=$8d
T6=$8e
T7=$8f
T8=$90
T9=$91
T10=$92
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 0F 07 01 00 00 FF 80 0F. The full client trace in the source explains how it gets there.
Mistake and repair
The wrong client overwrites the horizontal target with the origin. The projectile travels vertically instead of diagonally. Keep the captured target coordinates distinct from the launch position.
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
VELOCITY_X=$80
VELOCITY_Y=$81
VALID=$82
ORIGIN_X=$83
ORIGIN_Y=$84
TARGET_X=$85
TARGET_Y=$86
SPEED=$87
T0=$88
T1=$89
T2=$8a
T3=$8b
T4=$8c
T5=$8d
T6=$8e
T7=$8f
T8=$90
T9=$91
T10=$92
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:
Cost and alternatives
The measured helper extent is 371 code bytes; public state is 8 bytes and private scratch is 11 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.
At most fifteen stages, each handling two axes. A word multiply/divide is attractive on 68000; the byte implementation keeps identical semantics on 6502 and Z80 at the cost of more instructions. No comparative cycle claim is made.
Integer truncation can erase a shallow minor component. For smooth subpixel travel, retain fractional velocity in the movement layer. This computes an initial aim, not homing, intercept prediction, collision or equal Euclidean speed. Widen coordinates and workspace deliberately for larger worlds.
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
Fractional position: Feed signed velocities into a suitably widened fractional mover rather than treating them as unsigned coordinates.