A sentry looking down a corridor must stop at the first wall.

Use it in a project
On a 4×4 row-major grid, FROM and TO must be 0–15 and share a row or column. Zero grid bytes are transparent; nonzero bytes block. Exclude the observer cell and include the destination. A cell sees itself. Non-cardinal or out-of-range requests return VALID=0 and VISIBLE=0.
Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: FROM, TO, VISIBLE, VALID, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11, C12, C13, C14, C15. They occupy 20 contiguous bytes. Also reserve the private workspace: T0, T1, T2 (3 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.
Call cardinal_sight_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 cardinal_sight_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 inside the helper |
| Supply before service | FROM, TO and C0–C15 |
| Read after service | VISIBLE and VALID |
A shared row has stride one; a shared column has stride four. Walk from the cell after the observer to the destination, stopping at a blocker. Validating alignment prevents a row walk from wrapping into the next row.
Reusable initialisation and service
; SPDX-License-Identifier: MIT
; Test Sight Along a Grid Row or Column. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
cardinal_sight_init:
lda #0
sta FROM
sta TO
sta VISIBLE
sta VALID
sta C0
sta C1
sta C2
sta C3
sta C4
sta C5
sta C6
sta C7
sta C8
sta C9
sta C10
sta C11
sta C12
sta C13
sta C14
sta C15
sta T0
sta T1
sta T2
rts
cardinal_sight_step:
; VISIBLE = 0
lda #0
sta VISIBLE
; VALID = 0
lda #0
sta VALID
; if FROM >= 16:
lda FROM
cmp #16
bcs cardinal_sight_L4
cardinal_sight_L5:
jmp cardinal_sight_L2
cardinal_sight_L4:
jmp cardinal_sight_L1
cardinal_sight_L1:
; return
rts
jmp cardinal_sight_L3
cardinal_sight_L2:
cardinal_sight_L3:
; if TO >= 16:
lda TO
cmp #16
bcs cardinal_sight_L9
cardinal_sight_L10:
jmp cardinal_sight_L7
cardinal_sight_L9:
jmp cardinal_sight_L6
cardinal_sight_L6:
; return
rts
jmp cardinal_sight_L8
cardinal_sight_L7:
cardinal_sight_L8:
; if FROM == TO:
lda FROM
cmp TO
beq cardinal_sight_L14
cardinal_sight_L15:
jmp cardinal_sight_L12
cardinal_sight_L14:
jmp cardinal_sight_L11
cardinal_sight_L11:
; VALID = 1
lda #1
sta VALID
; VISIBLE = 1
lda #1
sta VISIBLE
; return
rts
jmp cardinal_sight_L13
cardinal_sight_L12:
cardinal_sight_L13:
; T0 = FROM >> 2
lda FROM
lsr
lsr
sta T0
; T1 = TO >> 2
lda TO
lsr
lsr
sta T1
; T2 = 1
lda #1
sta T2
; if T0 != T1:
lda T0
cmp T1
bne cardinal_sight_L19
cardinal_sight_L20:
jmp cardinal_sight_L17
cardinal_sight_L19:
jmp cardinal_sight_L16
cardinal_sight_L16:
; T0 = FROM & 3
lda FROM
and #3
sta T0
; T1 = TO & 3
lda TO
and #3
sta T1
; if T0 != T1:
lda T0
cmp T1
bne cardinal_sight_L24
cardinal_sight_L25:
jmp cardinal_sight_L22
cardinal_sight_L24:
jmp cardinal_sight_L21
cardinal_sight_L21:
; return
rts
jmp cardinal_sight_L23
cardinal_sight_L22:
cardinal_sight_L23:
; T2 = 4
lda #4
sta T2
jmp cardinal_sight_L18
cardinal_sight_L17:
cardinal_sight_L18:
; VALID = 1
lda #1
sta VALID
; T0 = FROM
lda FROM
sta T0
; while T0 != TO:
cardinal_sight_L26:
lda T0
cmp TO
bne cardinal_sight_L29
cardinal_sight_L30:
jmp cardinal_sight_L28
cardinal_sight_L29:
jmp cardinal_sight_L27
cardinal_sight_L27:
; if T0 < TO:
lda T0
cmp TO
bcc cardinal_sight_L34
cardinal_sight_L35:
jmp cardinal_sight_L32
cardinal_sight_L34:
jmp cardinal_sight_L31
cardinal_sight_L31:
; T0 = T0 + T2
lda T0
clc
adc T2
sta T0
jmp cardinal_sight_L33
cardinal_sight_L32:
; T0 = T0 - T2
lda T0
sec
sbc T2
sta T0
cardinal_sight_L33:
; T1 = C[T0]
ldx T0
lda C0,x
sta T1
; if T1 != 0:
lda T1
cmp #0
bne cardinal_sight_L39
cardinal_sight_L40:
jmp cardinal_sight_L37
cardinal_sight_L39:
jmp cardinal_sight_L36
cardinal_sight_L36:
; return
rts
jmp cardinal_sight_L38
cardinal_sight_L37:
cardinal_sight_L38:
jmp cardinal_sight_L26
cardinal_sight_L28:
; VISIBLE = 1
lda #1
sta VISIBLE
rts
component_end:Machine contract
A/X/status may change; Y is preserved. The 2A03 has no decimal arithmetic mode. 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. Scope or rename routine and field labels and allocate a separate block for each independent instance. Give each block one mutation owner.
The NROM caller owns PPU setup and background display, with NMI and DMC disabled. Table and packet sizes are bounded to fit the cartridge caller and internal RAM; the helper owns no PPU registers.
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
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
FROM=$80
TO=$81
VISIBLE=$82
VALID=$83
C0=$84
C1=$85
C2=$86
C3=$87
C4=$88
C5=$89
C6=$8a
C7=$8b
C8=$8c
C9=$8d
C10=$8e
C11=$8f
C12=$90
C13=$91
C14=$92
C15=$93
T0=$94
T1=$95
T2=$96
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$0200
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
.include "caller.inc"
.include "exercise.inc"
component_start:
.include "component.inc"
.include "data.inc"
.include "cases.inc"
code_end:
.segment "VECTORS"
.word nmi,reset,irq
.segment "CHARS"
.include "../../input/controller-reading/edges-font.inc"
.res 8192-272,0A client retaining state between calls
preview_rules:
.if CUSTOM
; FROM = 0
lda #0
sta FROM
; TO = 12
lda #12
sta TO
; C2 = 1
lda #1
sta C2
; C8 = 1
lda #1
sta C8
.if BROKEN
; C2 = 0
lda #0
sta C2
.endif
jsr checked_step
jsr emit_state
rts
.else
; FROM = 0
lda #0
sta FROM
; TO = 3
lda #3
sta TO
; C2 = 1
lda #1
sta C2
.if BROKEN
; C2 = 0
lda #0
sta C2
.endif
jsr checked_step
jsr emit_state
.endif
rtsCustomise it
Look down the first column, with a blocker at cell eight. 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
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
FROM=$80
TO=$81
VISIBLE=$82
VALID=$83
C0=$84
C1=$85
C2=$86
C3=$87
C4=$88
C5=$89
C6=$8a
C7=$8b
C8=$8c
C9=$8d
C10=$8e
C11=$8f
C12=$90
C13=$91
C14=$92
C15=$93
T0=$94
T1=$95
T2=$96
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$0200
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
.include "caller.inc"
.include "exercise.inc"
component_start:
.include "component.inc"
.include "data.inc"
.include "cases.inc"
code_end:
.segment "VECTORS"
.word nmi,reset,irq
.segment "CHARS"
.include "../../input/controller-reading/edges-font.inc"
.res 8192-272,0The custom diagnostic row is 00 0C 00 01 00 00 01 00. The full client trace in the source explains how it gets there.
Mistake and repair
The wrong caller clears C2 before querying sight. A real blocker disappears and the observer sees through it. Keep the query read-only with respect to the collision grid.
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
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
FROM=$80
TO=$81
VISIBLE=$82
VALID=$83
C0=$84
C1=$85
C2=$86
C3=$87
C4=$88
C5=$89
C6=$8a
C7=$8b
C8=$8c
C9=$8d
C10=$8e
C11=$8f
C12=$90
C13=$91
C14=$92
C15=$93
T0=$94
T1=$95
T2=$96
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$0200
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
.include "caller.inc"
.include "exercise.inc"
component_start:
.include "component.inc"
.include "data.inc"
.include "cases.inc"
code_end:
.segment "VECTORS"
.word nmi,reset,irq
.segment "CHARS"
.include "../../input/controller-reading/edges-font.inc"
.res 8192-272,0
Cost and alternatives
The measured helper extent is 255 code bytes; public state is 20 bytes and private scratch is 3 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 three destination-inclusive probes on a valid 4×4 cardinal query. General line traversal needs more arithmetic and an explicit corner policy.
This is explicitly cardinal sight, not a raycaster or arbitrary-angle field of view. Use a grid line traversal for diagonal rays and choose corner visibility deliberately. A renderer does not establish whether game logic should see through a tile.
Execution evidence
Four variants run on NTSC NROM, background only, DMC/NMI disabled, using Asm198x 0.0.72, Emu198x 0.44.2. Each executes 50 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
Tile properties: Use a visibility-blocking property when visual tile IDs alone do not tell you whether a corridor is transparent.