Skip to content

Patrol Between Two Endpoints on Amiga

A guard walks to the end of a corridor, turns and comes back.

A guard walks to the end of a corridor, turns and comes back. The turn should happen at the endpoint, not after a step through the wall. This routine keeps that small behaviour separate from sprite drawing and collision.

Amiga shows the working result 02 00 04 00 00 00 00 00.
The completed native caller shows hexadecimal state bytes. The field order below explains the result; the full verification packet retains every call.

Include, initialise and call

Bind POSITION, LOWER, UPPER, DIRECTION as 4 contiguous private bytes in that order. Include the helper once. Call endpoint_patrol_init at the deliberate reset boundary: it clears all named bytes, including inputs. Set your starting values afterwards. Call endpoint_patrol_step when this service is needed.

Initialise, then supply valid bounds and a starting position before the first step. Service the helper at your chosen movement cadence. A countdown can make a guard step less often; this helper itself does not own a clock. Pause simply omits service.

Use unsigned byte LOWER, UPPER and POSITION with LOWER ≤ POSITION ≤ UPPER. DIRECTION zero means increasing, nonzero means decreasing. Step turns inward at either endpoint, takes one unit step, and normalises DIRECTION to 0 or 1. A one-point interval stays there with direction zero. There is no dwell or duplicated endpoint call.

Reusable initialisation and service

component.inc
; SPDX-License-Identifier: MIT
; Patrol Between Two Endpoints. Bind these contiguous private bytes: POSITION, LOWER, UPPER, DIRECTION.
; init clears the whole block; step uses current inputs. No I/O, waits or IRQ owner.
; D0-D3/A0-A1/CCR may change; D4-D7/A2-A6 survive.
endpoint_patrol_init:
    clr.b POSITION
    clr.b LOWER
    clr.b UPPER
    clr.b DIRECTION
    rts
endpoint_patrol_step:
    move.b LOWER,d0
    cmp.b UPPER,d0
    bne.s patrol_range
    move.b d0,POSITION
    clr.b DIRECTION
    rts
patrol_range:
    cmp.b POSITION,d0
    beq.s patrol_positive
    move.b UPPER,d0
    cmp.b POSITION,d0
    beq.s patrol_negative
    tst.b DIRECTION
    beq.s patrol_positive
patrol_negative:
    move.b #1,DIRECTION
    subq.b #1,POSITION
    rts
patrol_positive:
    clr.b DIRECTION
    addq.b #1,POSITION
    rts
component_end:

State and ownership

POSITION and DIRECTION persist. LOWER and UPPER are caller-owned bounds. Altering the bounds requires moving the position into the new interval before the next call; this component does not repair arbitrary corrupt state.

D0–D3/A0–A1/CCR may change; D4–D7/A2–A6 survive. A direct BSR/JSR uses four return-address stack bytes. The program is a 68000 implementation with no division instruction requirement.

This implementation has fixed symbol bindings and one mutation owner. Independent users need distinct state blocks and scoped/renamed bindings; including another copy with the same global labels is not sufficient. Sequential queries may reuse a scratch block only after the previous result has been consumed. Do not let an interrupt and the main loop mutate the same block without a separately designed ownership policy.

The standalone boot program takes over Copper/bitplane/interrupt/DMA resources, explicitly disarms all eight sprites, and does not restore Workbench. Those are diagnostic presentation choices; the helper itself owns no chipset registers.

A small caller you can adapt

The standalone program includes the helper and a separate client. Its read-only table exercises 96 valid inputs, then this client initialises once and retains state between calls. Replace the supplied input changes with your own game values; the display and packet machinery are diagnostics rather than dependencies of the component.

Buildable caller, bindings and include order

demo.asm
    section code,code_c
CUSTOM = 0
BROKEN = 0
SCORE_CAP = 65535
QUEUE_CAP = 4
REPEAT_DELAY = 4
REPEAT_RATE = 2
    include "caller.inc"
    include "exercise.inc"
component_start:
    include "component.inc"
    include "cases.inc"
    include "display.inc"
    include "../turn-deadline/font.inc"
code_end:
STATE:
POSITION: dc.b 0
LOWER: dc.b 0
UPPER: dc.b 0
DIRECTION: dc.b 0
    even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
    even
signature: dc.b "198XGAMEPLAY000",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 separate client retaining state between calls

exercise.inc, excerpt
preview_rules:
    ifne CUSTOM
    move.b #4,POSITION
    move.b #0,LOWER
    move.b #6,UPPER
    move.b #0,DIRECTION
    ifne BROKEN
    tst.b DIRECTION
    beq.s bad_patrol_up_0_custom
    subq.b #1,POSITION
    bra.s bad_patrol_check_0_custom
bad_patrol_up_0_custom:
    addq.b #1,POSITION
bad_patrol_check_0_custom:
    move.b POSITION,d0
    cmp.b LOWER,d0
    blo.s bad_patrol_flip_0_custom
    cmp.b UPPER,d0
    bls.s bad_patrol_end_0_custom
bad_patrol_flip_0_custom:
    eori.b #1,DIRECTION
bad_patrol_end_0_custom:
    else
    bsr checked_step
    endif
    bsr emit_state
    ifne BROKEN
    tst.b DIRECTION
    beq.s bad_patrol_up_1_custom
    subq.b #1,POSITION
    bra.s bad_patrol_check_1_custom
bad_patrol_up_1_custom:
    addq.b #1,POSITION
bad_patrol_check_1_custom:
    move.b POSITION,d0
    cmp.b LOWER,d0
    blo.s bad_patrol_flip_1_custom
    cmp.b UPPER,d0
    bls.s bad_patrol_end_1_custom
bad_patrol_flip_1_custom:
    eori.b #1,DIRECTION
bad_patrol_end_1_custom:
    else
    bsr checked_step
    endif
    bsr emit_state
    ifne BROKEN
    tst.b DIRECTION
    beq.s bad_patrol_up_2_custom
    subq.b #1,POSITION
    bra.s bad_patrol_check_2_custom
bad_patrol_up_2_custom:
    addq.b #1,POSITION
bad_patrol_check_2_custom:
    move.b POSITION,d0
    cmp.b LOWER,d0
    blo.s bad_patrol_flip_2_custom
    cmp.b UPPER,d0
    bls.s bad_patrol_end_2_custom
bad_patrol_flip_2_custom:
    eori.b #1,DIRECTION
bad_patrol_end_2_custom:
    else
    bsr checked_step
    endif
    bsr emit_state
    ifne BROKEN
    tst.b DIRECTION
    beq.s bad_patrol_up_3_custom
    subq.b #1,POSITION
    bra.s bad_patrol_check_3_custom
bad_patrol_up_3_custom:
    addq.b #1,POSITION
bad_patrol_check_3_custom:
    move.b POSITION,d0
    cmp.b LOWER,d0
    blo.s bad_patrol_flip_3_custom
    cmp.b UPPER,d0
    bls.s bad_patrol_end_3_custom
bad_patrol_flip_3_custom:
    eori.b #1,DIRECTION
bad_patrol_end_3_custom:
    else
    bsr checked_step
    endif
    bsr emit_state
    ifne BROKEN
    tst.b DIRECTION
    beq.s bad_patrol_up_4_custom
    subq.b #1,POSITION
    bra.s bad_patrol_check_4_custom
bad_patrol_up_4_custom:
    addq.b #1,POSITION
bad_patrol_check_4_custom:
    move.b POSITION,d0
    cmp.b LOWER,d0
    blo.s bad_patrol_flip_4_custom
    cmp.b UPPER,d0
    bls.s bad_patrol_end_4_custom
bad_patrol_flip_4_custom:
    eori.b #1,DIRECTION
bad_patrol_end_4_custom:
    else
    bsr checked_step
    endif
    bsr emit_state
    ifne BROKEN
    tst.b DIRECTION
    beq.s bad_patrol_up_5_custom
    subq.b #1,POSITION
    bra.s bad_patrol_check_5_custom
bad_patrol_up_5_custom:
    addq.b #1,POSITION
bad_patrol_check_5_custom:
    move.b POSITION,d0
    cmp.b LOWER,d0
    blo.s bad_patrol_flip_5_custom
    cmp.b UPPER,d0
    bls.s bad_patrol_end_5_custom
bad_patrol_flip_5_custom:
    eori.b #1,DIRECTION
bad_patrol_end_5_custom:
    else
    bsr checked_step
    endif
    bsr emit_state
    else
    move.b #4,POSITION
    move.b #0,LOWER
    move.b #4,UPPER
    move.b #0,DIRECTION
    ifne BROKEN
    tst.b DIRECTION
    beq.s bad_patrol_up_0
    subq.b #1,POSITION
    bra.s bad_patrol_check_0
bad_patrol_up_0:
    addq.b #1,POSITION
bad_patrol_check_0:
    move.b POSITION,d0
    cmp.b LOWER,d0
    blo.s bad_patrol_flip_0
    cmp.b UPPER,d0
    bls.s bad_patrol_end_0
bad_patrol_flip_0:
    eori.b #1,DIRECTION
bad_patrol_end_0:
    else
    bsr checked_step
    endif
    bsr emit_state
    ifne BROKEN
    tst.b DIRECTION
    beq.s bad_patrol_up_1
    subq.b #1,POSITION
    bra.s bad_patrol_check_1
bad_patrol_up_1:
    addq.b #1,POSITION
bad_patrol_check_1:
    move.b POSITION,d0
    cmp.b LOWER,d0
    blo.s bad_patrol_flip_1
    cmp.b UPPER,d0
    bls.s bad_patrol_end_1
bad_patrol_flip_1:
    eori.b #1,DIRECTION
bad_patrol_end_1:
    else
    bsr checked_step
    endif
    bsr emit_state
    ifne BROKEN
    tst.b DIRECTION
    beq.s bad_patrol_up_2
    subq.b #1,POSITION
    bra.s bad_patrol_check_2
bad_patrol_up_2:
    addq.b #1,POSITION
bad_patrol_check_2:
    move.b POSITION,d0
    cmp.b LOWER,d0
    blo.s bad_patrol_flip_2
    cmp.b UPPER,d0
    bls.s bad_patrol_end_2
bad_patrol_flip_2:
    eori.b #1,DIRECTION
bad_patrol_end_2:
    else
    bsr checked_step
    endif
    bsr emit_state
    ifne BROKEN
    tst.b DIRECTION
    beq.s bad_patrol_up_3
    subq.b #1,POSITION
    bra.s bad_patrol_check_3
bad_patrol_up_3:
    addq.b #1,POSITION
bad_patrol_check_3:
    move.b POSITION,d0
    cmp.b LOWER,d0
    blo.s bad_patrol_flip_3
    cmp.b UPPER,d0
    bls.s bad_patrol_end_3
bad_patrol_flip_3:
    eori.b #1,DIRECTION
bad_patrol_end_3:
    else
    bsr checked_step
    endif
    bsr emit_state
    ifne BROKEN
    tst.b DIRECTION
    beq.s bad_patrol_up_4
    subq.b #1,POSITION
    bra.s bad_patrol_check_4
bad_patrol_up_4:
    addq.b #1,POSITION
bad_patrol_check_4:
    move.b POSITION,d0
    cmp.b LOWER,d0
    blo.s bad_patrol_flip_4
    cmp.b UPPER,d0
    bls.s bad_patrol_end_4
bad_patrol_flip_4:
    eori.b #1,DIRECTION
bad_patrol_end_4:
    else
    bsr checked_step
    endif
    bsr emit_state
    ifne BROKEN
    tst.b DIRECTION
    beq.s bad_patrol_up_5
    subq.b #1,POSITION
    bra.s bad_patrol_check_5
bad_patrol_up_5:
    addq.b #1,POSITION
bad_patrol_check_5:
    move.b POSITION,d0
    cmp.b LOWER,d0
    blo.s bad_patrol_flip_5
    cmp.b UPPER,d0
    bls.s bad_patrol_end_5
bad_patrol_flip_5:
    eori.b #1,DIRECTION
bad_patrol_end_5:
    else
    bsr checked_step
    endif
    bsr emit_state
    endif
    rts

The eight display fields follow POSITION, LOWER, UPPER, DIRECTION; unused display fields are zero. Each field is a hexadecimal byte. The working row is 02 00 04 00 00 00 00 00. The output is a completed diagnostic state, not a recording of movement, input or sound hardware.

Make it your own

The custom caller extends the upper endpoint from four to six. The same initial position now moves forward before turning. The lower bound remains zero and the movement amount remains one.

Maintained custom configuration or caller

custom.asm
    section code,code_c
CUSTOM = 1
BROKEN = 0
SCORE_CAP = 999
QUEUE_CAP = 2
REPEAT_DELAY = 6
REPEAT_RATE = 3
    include "caller.inc"
    include "exercise.inc"
component_start:
    include "component.inc"
    include "cases.inc"
    include "display.inc"
    include "../turn-deadline/font.inc"
code_end:
STATE:
POSITION: dc.b 0
LOWER: dc.b 0
UPPER: dc.b 0
DIRECTION: dc.b 0
    even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
    even
signature: dc.b "198XGAMEPLAY000",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 observed custom row is 02 00 06 01 00 00 00 00. The retained records qualify these named configurations; a new input domain or larger layout needs its own bounds and fresh execution checks.

Common mistake and repair

The wrong client steps first and only reverses after leaving the interval. Starting at the upper endpoint four, it visibly records position five before turning back. Turn before taking the next step. The wrong trace is a separate naive implementation; the valid boundary table still exercises the maintained helper.

Deliberately wrong client selection

wrong.asm
    section code,code_c
CUSTOM = 0
BROKEN = 1
SCORE_CAP = 65535
QUEUE_CAP = 4
REPEAT_DELAY = 4
REPEAT_RATE = 2
    include "caller.inc"
    include "exercise.inc"
component_start:
    include "component.inc"
    include "cases.inc"
    include "display.inc"
    include "../turn-deadline/font.inc"
code_end:
STATE:
POSITION: dc.b 0
LOWER: dc.b 0
UPPER: dc.b 0
DIRECTION: dc.b 0
    even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
    even
signature: dc.b "198XGAMEPLAY000",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 fault result 00 00 04 01 00 00 00 00.
The wrong client completes and records 00 00 04 01 00 00 00 00. Compare its state with the working row and the explained failure.

The wrong branch is in the displayed client above, selected by BROKEN. repaired.asm selects the original client again. Its executable matches demo exactly; Spectrum tape filenames can differ while loaded CODE is identical. The bad caller is a labelled experiment, not an alternative recommended implementation.

Cost and verification

The measured helper occupies 104 code bytes plus 4 named state bytes in this layout. Return addresses, alignment, tables and display diagnostics are additional. The sizes cover init and step together, not the whole standalone program.

There are a few byte comparisons and one increment/decrement per service, with no route scan or rendering. Keeping movement cadence outside allows several guards to share a clock without sharing their positions.

A multi-point route needs a waypoint index and route data. Variable-speed reflection must decide how to use leftover distance after hitting an endpoint; this one-unit routine cannot be made faster by replacing its increment with an unchecked larger addition.

Four native runs use A500 OCS PAL with Kickstart 1.3 and an OFS boot disk; Asm198x 0.0.72 and Emu198x 0.44.2, with Build198x 0.2.9 for disk mastering. Every run checks 96 table results, dirty initialisation and every state in a separate sequential trace against an independent integer model. This is boundary/varied-input qualification, not exhaustive coverage of every byte combination. Source/tool/firmware hashes, native startup and completion, preserved-register sentinels, immutable loaded code, actual helper extent, capture bytes and pixels, and exact repair are checked. Deliberately changed observations must fail. Firmware is hashed, never distributed.

Related reusable technique · Maths for Games · The Craft.

Source and native evidence.