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:
ld a,0
ld (VELOCITY_X),a
ld (VELOCITY_Y),a
ld (VALID),a
ld (ORIGIN_X),a
ld (ORIGIN_Y),a
ld (TARGET_X),a
ld (TARGET_Y),a
ld (SPEED),a
ld (T0),a
ld (T1),a
ld (T2),a
ld (T3),a
ld (T4),a
ld (T5),a
ld (T6),a
ld (T7),a
ld (T8),a
ld (T9),a
ld (T10),a
ret
aimed_projectile_step:
; VELOCITY_X = 0
ld a,0
ld (VELOCITY_X),a
; VELOCITY_Y = 0
ld a,0
ld (VELOCITY_Y),a
; VALID = 0
ld a,0
ld (VALID),a
; if SPEED > 15:
ld a,(SPEED)
cp 15
jp z,aimed_projectile_L2
jp nc,aimed_projectile_L1
jp aimed_projectile_L2
aimed_projectile_L1:
; return
ret
jp aimed_projectile_L3
aimed_projectile_L2:
aimed_projectile_L3:
; VALID = 1
ld a,1
ld (VALID),a
; if TARGET_X >= ORIGIN_X:
ld a,(TARGET_X)
ld b,a
ld a,(ORIGIN_X)
ld c,a
ld a,b
cp c
jp nc,aimed_projectile_L4
jp aimed_projectile_L5
aimed_projectile_L4:
; T0 = TARGET_X - ORIGIN_X
ld a,(TARGET_X)
ld b,a
ld a,(ORIGIN_X)
ld c,a
ld a,b
sub c
ld (T0),a
; T2 = 0
ld a,0
ld (T2),a
jp aimed_projectile_L6
aimed_projectile_L5:
; T0 = ORIGIN_X - TARGET_X
ld a,(ORIGIN_X)
ld b,a
ld a,(TARGET_X)
ld c,a
ld a,b
sub c
ld (T0),a
; T2 = 1
ld a,1
ld (T2),a
aimed_projectile_L6:
; if TARGET_Y >= ORIGIN_Y:
ld a,(TARGET_Y)
ld b,a
ld a,(ORIGIN_Y)
ld c,a
ld a,b
cp c
jp nc,aimed_projectile_L7
jp aimed_projectile_L8
aimed_projectile_L7:
; T1 = TARGET_Y - ORIGIN_Y
ld a,(TARGET_Y)
ld b,a
ld a,(ORIGIN_Y)
ld c,a
ld a,b
sub c
ld (T1),a
; T3 = 0
ld a,0
ld (T3),a
jp aimed_projectile_L9
aimed_projectile_L8:
; T1 = ORIGIN_Y - TARGET_Y
ld a,(ORIGIN_Y)
ld b,a
ld a,(TARGET_Y)
ld c,a
ld a,b
sub c
ld (T1),a
; T3 = 1
ld a,1
ld (T3),a
aimed_projectile_L9:
; T4 = T0
ld a,(T0)
ld (T4),a
; if T1 > T4:
ld a,(T1)
ld b,a
ld a,(T4)
ld c,a
ld a,b
cp c
jp z,aimed_projectile_L11
jp nc,aimed_projectile_L10
jp aimed_projectile_L11
aimed_projectile_L10:
; T4 = T1
ld a,(T1)
ld (T4),a
jp aimed_projectile_L12
aimed_projectile_L11:
aimed_projectile_L12:
; if T4 == 0:
ld a,(T4)
cp 0
jp z,aimed_projectile_L13
jp aimed_projectile_L14
aimed_projectile_L13:
; return
ret
jp aimed_projectile_L15
aimed_projectile_L14:
aimed_projectile_L15:
; T5 = 0
ld a,0
ld (T5),a
; T6 = 0
ld a,0
ld (T6),a
; T7 = 0
ld a,0
ld (T7),a
; T8 = 0
ld a,0
ld (T8),a
; T9 = 0
ld a,0
ld (T9),a
; while T5 < SPEED:
aimed_projectile_L16:
ld a,(T5)
ld b,a
ld a,(SPEED)
ld c,a
ld a,b
cp c
jp c,aimed_projectile_L17
jp aimed_projectile_L18
aimed_projectile_L17:
; T10 = T4 - T0
ld a,(T4)
ld b,a
ld a,(T0)
ld c,a
ld a,b
sub c
ld (T10),a
; if T6 >= T10:
ld a,(T6)
ld b,a
ld a,(T10)
ld c,a
ld a,b
cp c
jp nc,aimed_projectile_L19
jp aimed_projectile_L20
aimed_projectile_L19:
; T6 = T6 - T10
ld a,(T6)
ld b,a
ld a,(T10)
ld c,a
ld a,b
sub c
ld (T6),a
; T7 = T7 + 1
ld a,(T7)
add a,1
ld (T7),a
jp aimed_projectile_L21
aimed_projectile_L20:
; T6 = T6 + T0
ld a,(T6)
ld b,a
ld a,(T0)
ld c,a
ld a,b
add a,c
ld (T6),a
aimed_projectile_L21:
; T10 = T4 - T1
ld a,(T4)
ld b,a
ld a,(T1)
ld c,a
ld a,b
sub c
ld (T10),a
; if T8 >= T10:
ld a,(T8)
ld b,a
ld a,(T10)
ld c,a
ld a,b
cp c
jp nc,aimed_projectile_L22
jp aimed_projectile_L23
aimed_projectile_L22:
; T8 = T8 - T10
ld a,(T8)
ld b,a
ld a,(T10)
ld c,a
ld a,b
sub c
ld (T8),a
; T9 = T9 + 1
ld a,(T9)
add a,1
ld (T9),a
jp aimed_projectile_L24
aimed_projectile_L23:
; T8 = T8 + T1
ld a,(T8)
ld b,a
ld a,(T1)
ld c,a
ld a,b
add a,c
ld (T8),a
aimed_projectile_L24:
; T5 = T5 + 1
ld a,(T5)
add a,1
ld (T5),a
jp aimed_projectile_L16
aimed_projectile_L18:
; VELOCITY_X = T7
ld a,(T7)
ld (VELOCITY_X),a
; VELOCITY_Y = T9
ld a,(T9)
ld (VELOCITY_Y),a
; if T2 != 0:
ld a,(T2)
cp 0
jp nz,aimed_projectile_L25
jp aimed_projectile_L26
aimed_projectile_L25:
; VELOCITY_X = 0 - T7
ld a,0
ld b,a
ld a,(T7)
ld c,a
ld a,b
sub c
ld (VELOCITY_X),a
jp aimed_projectile_L27
aimed_projectile_L26:
aimed_projectile_L27:
; if T3 != 0:
ld a,(T3)
cp 0
jp nz,aimed_projectile_L28
jp aimed_projectile_L29
aimed_projectile_L28:
; VELOCITY_Y = 0 - T9
ld a,0
ld b,a
ld a,(T9)
ld c,a
ld a,b
sub c
ld (VELOCITY_Y),a
jp aimed_projectile_L30
aimed_projectile_L29:
aimed_projectile_L30:
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
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:
VELOCITY_X: defb 0
VELOCITY_Y: defb 0
VALID: defb 0
ORIGIN_X: defb 0
ORIGIN_Y: defb 0
TARGET_X: defb 0
TARGET_Y: defb 0
SPEED: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
T3: defb 0
T4: defb 0
T5: defb 0
T6: defb 0
T7: defb 0
T8: defb 0
T9: defb 0
T10: 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 startA client retaining state between calls
preview_rules:
if CUSTOM
; ORIGIN_X = 0
ld a,0
ld (ORIGIN_X),a
; ORIGIN_Y = 0
ld a,0
ld (ORIGIN_Y),a
; TARGET_X = 255
ld a,255
ld (TARGET_X),a
; TARGET_Y = 128
ld a,128
ld (TARGET_Y),a
; SPEED = 15
ld a,15
ld (SPEED),a
if BROKEN
; TARGET_X = ORIGIN_X
ld a,(ORIGIN_X)
ld (TARGET_X),a
endif
call checked_step
call emit_state
ret
else
; ORIGIN_X = 100
ld a,100
ld (ORIGIN_X),a
; ORIGIN_Y = 100
ld a,100
ld (ORIGIN_Y),a
; TARGET_X = 80
ld a,80
ld (TARGET_X),a
; TARGET_Y = 90
ld a,90
ld (TARGET_Y),a
; SPEED = 6
ld a,6
ld (SPEED),a
if BROKEN
; TARGET_X = ORIGIN_X
ld a,(ORIGIN_X)
ld (TARGET_X),a
endif
call checked_step
call emit_state
endif
retCustomise 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
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:
VELOCITY_X: defb 0
VELOCITY_Y: defb 0
VALID: defb 0
ORIGIN_X: defb 0
ORIGIN_Y: defb 0
TARGET_X: defb 0
TARGET_Y: defb 0
SPEED: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
T3: defb 0
T4: defb 0
T5: defb 0
T6: defb 0
T7: defb 0
T8: defb 0
T9: defb 0
T10: 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 startThe 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
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:
VELOCITY_X: defb 0
VELOCITY_Y: defb 0
VALID: defb 0
ORIGIN_X: defb 0
ORIGIN_Y: defb 0
TARGET_X: defb 0
TARGET_Y: defb 0
SPEED: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
T3: defb 0
T4: defb 0
T5: defb 0
T6: defb 0
T7: defb 0
T8: defb 0
T9: defb 0
T10: 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
Cost and alternatives
The measured helper extent is 506 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 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
Fractional position: Feed signed velocities into a suitably widened fractional mover rather than treating them as unsigned coordinates.