Skip to content

Aim a Projectile with Bounded Integer Speed on Amiga

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

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

Amiga shows the working diagnostic row FA FD 01 64 64 50 5A 06.
A completed native caller displays FA FD 01 64 64 50 5A 06. 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

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

component.inc
; 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:
    move.b #0,d0
    move.b d0,VELOCITY_X
    move.b d0,VELOCITY_Y
    move.b d0,VALID
    move.b d0,ORIGIN_X
    move.b d0,ORIGIN_Y
    move.b d0,TARGET_X
    move.b d0,TARGET_Y
    move.b d0,SPEED
    move.b d0,T0
    move.b d0,T1
    move.b d0,T2
    move.b d0,T3
    move.b d0,T4
    move.b d0,T5
    move.b d0,T6
    move.b d0,T7
    move.b d0,T8
    move.b d0,T9
    move.b d0,T10
    rts
aimed_projectile_step:
; VELOCITY_X = 0
    move.b #0,d0
    move.b d0,VELOCITY_X
; VELOCITY_Y = 0
    move.b #0,d0
    move.b d0,VELOCITY_Y
; VALID = 0
    move.b #0,d0
    move.b d0,VALID
; if SPEED > 15:
    move.b SPEED,d0
    cmp.b #15,d0
    bhi aimed_projectile_L1
    jmp aimed_projectile_L2
aimed_projectile_L1:
; return
    rts
    jmp aimed_projectile_L3
aimed_projectile_L2:
aimed_projectile_L3:
; VALID = 1
    move.b #1,d0
    move.b d0,VALID
; if TARGET_X >= ORIGIN_X:
    move.b TARGET_X,d0
    move.b ORIGIN_X,d1
    cmp.b d1,d0
    bcc aimed_projectile_L4
    jmp aimed_projectile_L5
aimed_projectile_L4:
; T0 = TARGET_X - ORIGIN_X
    move.b TARGET_X,d0
    move.b ORIGIN_X,d1
    sub.b d1,d0
    move.b d0,T0
; T2 = 0
    move.b #0,d0
    move.b d0,T2
    jmp aimed_projectile_L6
aimed_projectile_L5:
; T0 = ORIGIN_X - TARGET_X
    move.b ORIGIN_X,d0
    move.b TARGET_X,d1
    sub.b d1,d0
    move.b d0,T0
; T2 = 1
    move.b #1,d0
    move.b d0,T2
aimed_projectile_L6:
; if TARGET_Y >= ORIGIN_Y:
    move.b TARGET_Y,d0
    move.b ORIGIN_Y,d1
    cmp.b d1,d0
    bcc aimed_projectile_L7
    jmp aimed_projectile_L8
aimed_projectile_L7:
; T1 = TARGET_Y - ORIGIN_Y
    move.b TARGET_Y,d0
    move.b ORIGIN_Y,d1
    sub.b d1,d0
    move.b d0,T1
; T3 = 0
    move.b #0,d0
    move.b d0,T3
    jmp aimed_projectile_L9
aimed_projectile_L8:
; T1 = ORIGIN_Y - TARGET_Y
    move.b ORIGIN_Y,d0
    move.b TARGET_Y,d1
    sub.b d1,d0
    move.b d0,T1
; T3 = 1
    move.b #1,d0
    move.b d0,T3
aimed_projectile_L9:
; T4 = T0
    move.b T0,d0
    move.b d0,T4
; if T1 > T4:
    move.b T1,d0
    move.b T4,d1
    cmp.b d1,d0
    bhi aimed_projectile_L10
    jmp aimed_projectile_L11
aimed_projectile_L10:
; T4 = T1
    move.b T1,d0
    move.b d0,T4
    jmp aimed_projectile_L12
aimed_projectile_L11:
aimed_projectile_L12:
; if T4 == 0:
    move.b T4,d0
    cmp.b #0,d0
    beq aimed_projectile_L13
    jmp aimed_projectile_L14
aimed_projectile_L13:
; return
    rts
    jmp aimed_projectile_L15
aimed_projectile_L14:
aimed_projectile_L15:
; T5 = 0
    move.b #0,d0
    move.b d0,T5
; T6 = 0
    move.b #0,d0
    move.b d0,T6
; T7 = 0
    move.b #0,d0
    move.b d0,T7
; T8 = 0
    move.b #0,d0
    move.b d0,T8
; T9 = 0
    move.b #0,d0
    move.b d0,T9
; while T5 < SPEED:
aimed_projectile_L16:
    move.b T5,d0
    move.b SPEED,d1
    cmp.b d1,d0
    bcs aimed_projectile_L17
    jmp aimed_projectile_L18
aimed_projectile_L17:
; T10 = T4 - T0
    move.b T4,d0
    move.b T0,d1
    sub.b d1,d0
    move.b d0,T10
; if T6 >= T10:
    move.b T6,d0
    move.b T10,d1
    cmp.b d1,d0
    bcc aimed_projectile_L19
    jmp aimed_projectile_L20
aimed_projectile_L19:
; T6 = T6 - T10
    move.b T6,d0
    move.b T10,d1
    sub.b d1,d0
    move.b d0,T6
; T7 = T7 + 1
    move.b T7,d0
    add.b #1,d0
    move.b d0,T7
    jmp aimed_projectile_L21
aimed_projectile_L20:
; T6 = T6 + T0
    move.b T6,d0
    move.b T0,d1
    add.b d1,d0
    move.b d0,T6
aimed_projectile_L21:
; T10 = T4 - T1
    move.b T4,d0
    move.b T1,d1
    sub.b d1,d0
    move.b d0,T10
; if T8 >= T10:
    move.b T8,d0
    move.b T10,d1
    cmp.b d1,d0
    bcc aimed_projectile_L22
    jmp aimed_projectile_L23
aimed_projectile_L22:
; T8 = T8 - T10
    move.b T8,d0
    move.b T10,d1
    sub.b d1,d0
    move.b d0,T8
; T9 = T9 + 1
    move.b T9,d0
    add.b #1,d0
    move.b d0,T9
    jmp aimed_projectile_L24
aimed_projectile_L23:
; T8 = T8 + T1
    move.b T8,d0
    move.b T1,d1
    add.b d1,d0
    move.b d0,T8
aimed_projectile_L24:
; T5 = T5 + 1
    move.b T5,d0
    add.b #1,d0
    move.b d0,T5
    jmp aimed_projectile_L16
aimed_projectile_L18:
; VELOCITY_X = T7
    move.b T7,d0
    move.b d0,VELOCITY_X
; VELOCITY_Y = T9
    move.b T9,d0
    move.b d0,VELOCITY_Y
; if T2 != 0:
    move.b T2,d0
    cmp.b #0,d0
    bne aimed_projectile_L25
    jmp aimed_projectile_L26
aimed_projectile_L25:
; VELOCITY_X = 0 - T7
    move.b #0,d0
    move.b T7,d1
    sub.b d1,d0
    move.b d0,VELOCITY_X
    jmp aimed_projectile_L27
aimed_projectile_L26:
aimed_projectile_L27:
; if T3 != 0:
    move.b T3,d0
    cmp.b #0,d0
    bne aimed_projectile_L28
    jmp aimed_projectile_L29
aimed_projectile_L28:
; VELOCITY_Y = 0 - T9
    move.b #0,d0
    move.b T9,d1
    sub.b d1,d0
    move.b d0,VELOCITY_Y
    jmp aimed_projectile_L30
aimed_projectile_L29:
aimed_projectile_L30:
    rts
component_end:

Machine contract

D0–D3/A0–A1/CCR may change; D4–D7/A2–A6 are preserved. A direct BSR/JSR needs four 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 A500 OCS diagnostic cold-boots an OFS disk, owns the Copper and bitplane and disables interrupt/DMA sources. The helper does not take over hardware or allocate Chip RAM.

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 and BUILD198X=/path/to/build198x. 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
    section code,code_c
CUSTOM = 0
BROKEN = 0
COYOTE_STEPS = 4
BUFFER_STEPS = 4
TEXT_CAP = 8
PROPERTY_COUNT = 16
STREAM_BYTES = 13
CRC_POLY = 7
    include "caller.inc"
    include "exercise.inc"
component_start:
    include "component.inc"
    even
    include "data.inc"
    include "cases.inc"
    even
    include "display.inc"
    include "../../framework/turn-deadline/font.inc"
code_end:
STATE:
VELOCITY_X: dc.b 0
VELOCITY_Y: dc.b 0
VALID: dc.b 0
ORIGIN_X: dc.b 0
ORIGIN_Y: dc.b 0
TARGET_X: dc.b 0
TARGET_Y: dc.b 0
SPEED: dc.b 0
T0: dc.b 0
T1: dc.b 0
T2: dc.b 0
T3: dc.b 0
T4: dc.b 0
T5: dc.b 0
T6: dc.b 0
T7: dc.b 0
T8: dc.b 0
T9: dc.b 0
T10: dc.b 0
    even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
    even
signature: dc.b "198XNIGHTTWO000",0
    even
copperlist:
    dc.w $08e,$2c81,$090,$2cc1,$092,$0038,$094,$00d0
    dc.w $100,$1200,$102,0,$104,0,$108,0,$10a,0
    dc.w $180,$012,$182,$fff
copbpl:
    dc.w $0e0,0,$0e2,0
    dc.w $ffff,$fffe
plane: dcb.b 10240,0

A client retaining state between calls

exercise.inc, excerpt
preview_rules:
    ifne CUSTOM
; ORIGIN_X = 0
    move.b #0,d0
    move.b d0,ORIGIN_X
; ORIGIN_Y = 0
    move.b #0,d0
    move.b d0,ORIGIN_Y
; TARGET_X = 255
    move.b #255,d0
    move.b d0,TARGET_X
; TARGET_Y = 128
    move.b #128,d0
    move.b d0,TARGET_Y
; SPEED = 15
    move.b #15,d0
    move.b d0,SPEED
    ifne BROKEN
; TARGET_X = ORIGIN_X
    move.b ORIGIN_X,d0
    move.b d0,TARGET_X
    endif
    bsr checked_step
    bsr emit_state
    rts
    else
; ORIGIN_X = 100
    move.b #100,d0
    move.b d0,ORIGIN_X
; ORIGIN_Y = 100
    move.b #100,d0
    move.b d0,ORIGIN_Y
; TARGET_X = 80
    move.b #80,d0
    move.b d0,TARGET_X
; TARGET_Y = 90
    move.b #90,d0
    move.b d0,TARGET_Y
; SPEED = 6
    move.b #6,d0
    move.b d0,SPEED
    ifne BROKEN
; TARGET_X = ORIGIN_X
    move.b ORIGIN_X,d0
    move.b d0,TARGET_X
    endif
    bsr checked_step
    bsr emit_state
    endif
    rts

Customise 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

custom.asm
    section code,code_c
CUSTOM = 1
BROKEN = 0
COYOTE_STEPS = 2
BUFFER_STEPS = 2
TEXT_CAP = 4
PROPERTY_COUNT = 16
STREAM_BYTES = 13
CRC_POLY = 49
    include "caller.inc"
    include "exercise.inc"
component_start:
    include "component.inc"
    even
    include "data.inc"
    include "cases.inc"
    even
    include "display.inc"
    include "../../framework/turn-deadline/font.inc"
code_end:
STATE:
VELOCITY_X: dc.b 0
VELOCITY_Y: dc.b 0
VALID: dc.b 0
ORIGIN_X: dc.b 0
ORIGIN_Y: dc.b 0
TARGET_X: dc.b 0
TARGET_Y: dc.b 0
SPEED: dc.b 0
T0: dc.b 0
T1: dc.b 0
T2: dc.b 0
T3: dc.b 0
T4: dc.b 0
T5: dc.b 0
T6: dc.b 0
T7: dc.b 0
T8: dc.b 0
T9: dc.b 0
T10: dc.b 0
    even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
    even
signature: dc.b "198XNIGHTTWO000",0
    even
copperlist:
    dc.w $08e,$2c81,$090,$2cc1,$092,$0038,$094,$00d0
    dc.w $100,$1200,$102,0,$104,0,$108,0,$10a,0
    dc.w $180,$012,$182,$fff
copbpl:
    dc.w $0e0,0,$0e2,0
    dc.w $ffff,$fffe
plane: dcb.b 10240,0

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

wrong.asm
    section code,code_c
CUSTOM = 0
BROKEN = 1
COYOTE_STEPS = 4
BUFFER_STEPS = 4
TEXT_CAP = 8
PROPERTY_COUNT = 16
STREAM_BYTES = 13
CRC_POLY = 7
    include "caller.inc"
    include "exercise.inc"
component_start:
    include "component.inc"
    even
    include "data.inc"
    include "cases.inc"
    even
    include "display.inc"
    include "../../framework/turn-deadline/font.inc"
code_end:
STATE:
VELOCITY_X: dc.b 0
VELOCITY_Y: dc.b 0
VALID: dc.b 0
ORIGIN_X: dc.b 0
ORIGIN_Y: dc.b 0
TARGET_X: dc.b 0
TARGET_Y: dc.b 0
SPEED: dc.b 0
T0: dc.b 0
T1: dc.b 0
T2: dc.b 0
T3: dc.b 0
T4: dc.b 0
T5: dc.b 0
T6: dc.b 0
T7: dc.b 0
T8: dc.b 0
T9: dc.b 0
T10: dc.b 0
    even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
    even
signature: dc.b "198XNIGHTTWO000",0
    even
copperlist:
    dc.w $08e,$2c81,$090,$2cc1,$092,$0038,$094,$00d0
    dc.w $100,$1200,$102,0,$104,0,$108,0,$10a,0
    dc.w $180,$012,$182,$fff
copbpl:
    dc.w $0e0,0,$0e2,0
    dc.w $ffff,$fffe
plane: dcb.b 10240,0
Amiga shows the deliberate error row 00 FA 01 64 64 64 5A 06.
The wrong client completes with 00 FA 01 64 64 64 5A 06; its changed inputs and state explain the result.

Cost and alternatives

The measured helper extent is 710 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 A500 OCS PAL, Kickstart1.3 OFS cold boot, using Asm198x 0.0.72, Emu198x 0.44.2 and Build198x 0.2.9. 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.

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