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.

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
; 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.
; A/X/flags may change; Y and decimal mode survive. Binary arithmetic requires D=0.
endpoint_patrol_init:
lda #0
sta POSITION
sta LOWER
sta UPPER
sta DIRECTION
rts
endpoint_patrol_step:
lda LOWER
cmp UPPER
bne patrol_range
sta POSITION
lda #0
sta DIRECTION
rts
patrol_range:
cmp POSITION
beq patrol_positive
lda UPPER
cmp POSITION
beq patrol_negative
lda DIRECTION
beq patrol_positive
patrol_negative:
lda #1
sta DIRECTION
dec POSITION
rts
patrol_positive:
lda #0
sta DIRECTION
inc 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.
A/X/flags may change; Y and decimal mode survive. Clear decimal mode before binary arithmetic; the standalone caller uses CLD. A direct call pushes a two-byte return address.
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 PRG takes over screen/font/colour RAM, disables IRQ sources and does not return to BASIC. Its private diagnostic workspace is $70–$89; its packet starts at $C000.
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
POSITION=$80
LOWER=$81
UPPER=$82
DIRECTION=$83
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
SCORE_CAP=65535
QUEUE_CAP=4
REPEAT_DELAY=4
REPEAT_RATE=2
!source "caller.inc"
!source "exercise.inc"
component_start:
!source "component.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 separate client retaining state between calls
preview_rules:
!if CUSTOM {
lda #4
sta POSITION
lda #0
sta LOWER
lda #6
sta UPPER
lda #0
sta DIRECTION
!if BROKEN {
lda DIRECTION
beq bad_patrol_up_0_custom
dec POSITION
jmp bad_patrol_check_0_custom
bad_patrol_up_0_custom:
inc POSITION
bad_patrol_check_0_custom:
lda POSITION
cmp LOWER
bcc bad_patrol_flip_0_custom
cmp UPPER
bcc bad_patrol_end_0_custom
beq bad_patrol_end_0_custom
bad_patrol_flip_0_custom:
lda DIRECTION
eor #1
sta DIRECTION
bad_patrol_end_0_custom:
} else {
jsr checked_step
}
jsr emit_state
!if BROKEN {
lda DIRECTION
beq bad_patrol_up_1_custom
dec POSITION
jmp bad_patrol_check_1_custom
bad_patrol_up_1_custom:
inc POSITION
bad_patrol_check_1_custom:
lda POSITION
cmp LOWER
bcc bad_patrol_flip_1_custom
cmp UPPER
bcc bad_patrol_end_1_custom
beq bad_patrol_end_1_custom
bad_patrol_flip_1_custom:
lda DIRECTION
eor #1
sta DIRECTION
bad_patrol_end_1_custom:
} else {
jsr checked_step
}
jsr emit_state
!if BROKEN {
lda DIRECTION
beq bad_patrol_up_2_custom
dec POSITION
jmp bad_patrol_check_2_custom
bad_patrol_up_2_custom:
inc POSITION
bad_patrol_check_2_custom:
lda POSITION
cmp LOWER
bcc bad_patrol_flip_2_custom
cmp UPPER
bcc bad_patrol_end_2_custom
beq bad_patrol_end_2_custom
bad_patrol_flip_2_custom:
lda DIRECTION
eor #1
sta DIRECTION
bad_patrol_end_2_custom:
} else {
jsr checked_step
}
jsr emit_state
!if BROKEN {
lda DIRECTION
beq bad_patrol_up_3_custom
dec POSITION
jmp bad_patrol_check_3_custom
bad_patrol_up_3_custom:
inc POSITION
bad_patrol_check_3_custom:
lda POSITION
cmp LOWER
bcc bad_patrol_flip_3_custom
cmp UPPER
bcc bad_patrol_end_3_custom
beq bad_patrol_end_3_custom
bad_patrol_flip_3_custom:
lda DIRECTION
eor #1
sta DIRECTION
bad_patrol_end_3_custom:
} else {
jsr checked_step
}
jsr emit_state
!if BROKEN {
lda DIRECTION
beq bad_patrol_up_4_custom
dec POSITION
jmp bad_patrol_check_4_custom
bad_patrol_up_4_custom:
inc POSITION
bad_patrol_check_4_custom:
lda POSITION
cmp LOWER
bcc bad_patrol_flip_4_custom
cmp UPPER
bcc bad_patrol_end_4_custom
beq bad_patrol_end_4_custom
bad_patrol_flip_4_custom:
lda DIRECTION
eor #1
sta DIRECTION
bad_patrol_end_4_custom:
} else {
jsr checked_step
}
jsr emit_state
!if BROKEN {
lda DIRECTION
beq bad_patrol_up_5_custom
dec POSITION
jmp bad_patrol_check_5_custom
bad_patrol_up_5_custom:
inc POSITION
bad_patrol_check_5_custom:
lda POSITION
cmp LOWER
bcc bad_patrol_flip_5_custom
cmp UPPER
bcc bad_patrol_end_5_custom
beq bad_patrol_end_5_custom
bad_patrol_flip_5_custom:
lda DIRECTION
eor #1
sta DIRECTION
bad_patrol_end_5_custom:
} else {
jsr checked_step
}
jsr emit_state
} else {
lda #4
sta POSITION
lda #0
sta LOWER
lda #4
sta UPPER
lda #0
sta DIRECTION
!if BROKEN {
lda DIRECTION
beq bad_patrol_up_0
dec POSITION
jmp bad_patrol_check_0
bad_patrol_up_0:
inc POSITION
bad_patrol_check_0:
lda POSITION
cmp LOWER
bcc bad_patrol_flip_0
cmp UPPER
bcc bad_patrol_end_0
beq bad_patrol_end_0
bad_patrol_flip_0:
lda DIRECTION
eor #1
sta DIRECTION
bad_patrol_end_0:
} else {
jsr checked_step
}
jsr emit_state
!if BROKEN {
lda DIRECTION
beq bad_patrol_up_1
dec POSITION
jmp bad_patrol_check_1
bad_patrol_up_1:
inc POSITION
bad_patrol_check_1:
lda POSITION
cmp LOWER
bcc bad_patrol_flip_1
cmp UPPER
bcc bad_patrol_end_1
beq bad_patrol_end_1
bad_patrol_flip_1:
lda DIRECTION
eor #1
sta DIRECTION
bad_patrol_end_1:
} else {
jsr checked_step
}
jsr emit_state
!if BROKEN {
lda DIRECTION
beq bad_patrol_up_2
dec POSITION
jmp bad_patrol_check_2
bad_patrol_up_2:
inc POSITION
bad_patrol_check_2:
lda POSITION
cmp LOWER
bcc bad_patrol_flip_2
cmp UPPER
bcc bad_patrol_end_2
beq bad_patrol_end_2
bad_patrol_flip_2:
lda DIRECTION
eor #1
sta DIRECTION
bad_patrol_end_2:
} else {
jsr checked_step
}
jsr emit_state
!if BROKEN {
lda DIRECTION
beq bad_patrol_up_3
dec POSITION
jmp bad_patrol_check_3
bad_patrol_up_3:
inc POSITION
bad_patrol_check_3:
lda POSITION
cmp LOWER
bcc bad_patrol_flip_3
cmp UPPER
bcc bad_patrol_end_3
beq bad_patrol_end_3
bad_patrol_flip_3:
lda DIRECTION
eor #1
sta DIRECTION
bad_patrol_end_3:
} else {
jsr checked_step
}
jsr emit_state
!if BROKEN {
lda DIRECTION
beq bad_patrol_up_4
dec POSITION
jmp bad_patrol_check_4
bad_patrol_up_4:
inc POSITION
bad_patrol_check_4:
lda POSITION
cmp LOWER
bcc bad_patrol_flip_4
cmp UPPER
bcc bad_patrol_end_4
beq bad_patrol_end_4
bad_patrol_flip_4:
lda DIRECTION
eor #1
sta DIRECTION
bad_patrol_end_4:
} else {
jsr checked_step
}
jsr emit_state
!if BROKEN {
lda DIRECTION
beq bad_patrol_up_5
dec POSITION
jmp bad_patrol_check_5
bad_patrol_up_5:
inc POSITION
bad_patrol_check_5:
lda POSITION
cmp LOWER
bcc bad_patrol_flip_5
cmp UPPER
bcc bad_patrol_end_5
beq bad_patrol_end_5
bad_patrol_flip_5:
lda DIRECTION
eor #1
sta DIRECTION
bad_patrol_end_5:
} else {
jsr checked_step
}
jsr emit_state
}
rtsThe 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
POSITION=$80
LOWER=$81
UPPER=$82
DIRECTION=$83
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
SCORE_CAP=999
QUEUE_CAP=2
REPEAT_DELAY=6
REPEAT_RATE=3
!source "caller.inc"
!source "exercise.inc"
component_start:
!source "component.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 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
POSITION=$80
LOWER=$81
UPPER=$82
DIRECTION=$83
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
SCORE_CAP=65535
QUEUE_CAP=4
REPEAT_DELAY=4
REPEAT_RATE=2
!source "caller.inc"
!source "exercise.inc"
component_start:
!source "component.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 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 52 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 PAL C64 with stock BASIC/KERNAL PRG startup and RUN; Asm198x 0.0.72 and Emu198x 0.44.2. 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.