Skip to content

Trace a Thin Grid Line of Sight on NES

A guard looking across a room needs more than a same-row check.

A guard looking across a room needs more than a same-row check.

NES shows the working diagnostic row 00 01 00 0F 00 00 00 00.
A completed native caller displays 00 01 00 0F 00 00 00 00. 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

FROM and TO are row-major indices 0–15 in a 4×4 grid. Zero cells are transparent and nonzero cells block. Trace a thin Bresenham line, excluding the observer and including the destination; a cell sees itself. Invalid coordinates return VALID=0 and VISIBLE=0 without indexing the grid.

Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: VISIBLE, VALID, FROM, TO, 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, T3, T4, T5, T6, T7, T8, T9, T10, T11 (12 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.

Call grid_line_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 grid_line_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
Supply before service FROM, TO and C0–C15
Read after service VISIBLE and VALID

The error accumulator decides when to advance each axis. A bias of 128 represents the small signed error using byte comparisons; doubled error stays within range on this fixed 4×4 domain.

Reusable initialisation and service

6502.inc
; SPDX-License-Identifier: MIT
; Trace a Thin Grid Line of Sight. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
grid_line_sight_init:
    lda #0
    sta VISIBLE
    sta VALID
    sta FROM
    sta TO
    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
    sta T3
    sta T4
    sta T5
    sta T6
    sta T7
    sta T8
    sta T9
    sta T10
    sta T11
    rts
grid_line_sight_step:
; VISIBLE = 0
    lda #0
    sta VISIBLE
; VALID = 0
    lda #0
    sta VALID
; if FROM >= 16:
    lda FROM
    cmp #16
    bcs grid_line_sight_L4
grid_line_sight_L5:
    jmp grid_line_sight_L2
grid_line_sight_L4:
    jmp grid_line_sight_L1
grid_line_sight_L1:
; return
    rts
    jmp grid_line_sight_L3
grid_line_sight_L2:
grid_line_sight_L3:
; if TO >= 16:
    lda TO
    cmp #16
    bcs grid_line_sight_L9
grid_line_sight_L10:
    jmp grid_line_sight_L7
grid_line_sight_L9:
    jmp grid_line_sight_L6
grid_line_sight_L6:
; return
    rts
    jmp grid_line_sight_L8
grid_line_sight_L7:
grid_line_sight_L8:
; VALID = 1
    lda #1
    sta VALID
; T0 = FROM & 3
    lda FROM
    and #3
    sta T0
; T1 = FROM >> 2
    lda FROM
    lsr
    lsr
    sta T1
; T2 = TO & 3
    lda TO
    and #3
    sta T2
; T3 = TO >> 2
    lda TO
    lsr
    lsr
    sta T3
; T6 = 1
    lda #1
    sta T6
; if T2 >= T0:
    lda T2
    cmp T0
    bcs grid_line_sight_L14
grid_line_sight_L15:
    jmp grid_line_sight_L12
grid_line_sight_L14:
    jmp grid_line_sight_L11
grid_line_sight_L11:
; T4 = T2 - T0
    lda T2
    sec
    sbc T0
    sta T4
    jmp grid_line_sight_L13
grid_line_sight_L12:
; T4 = T0 - T2
    lda T0
    sec
    sbc T2
    sta T4
; T6 = 255
    lda #255
    sta T6
grid_line_sight_L13:
; T7 = 1
    lda #1
    sta T7
; if T3 >= T1:
    lda T3
    cmp T1
    bcs grid_line_sight_L19
grid_line_sight_L20:
    jmp grid_line_sight_L17
grid_line_sight_L19:
    jmp grid_line_sight_L16
grid_line_sight_L16:
; T5 = T3 - T1
    lda T3
    sec
    sbc T1
    sta T5
    jmp grid_line_sight_L18
grid_line_sight_L17:
; T5 = T1 - T3
    lda T1
    sec
    sbc T3
    sta T5
; T7 = 255
    lda #255
    sta T7
grid_line_sight_L18:
; T8 = 128 + T4
    lda #128
    clc
    adc T4
    sta T8
; T8 = T8 - T5
    lda T8
    sec
    sbc T5
    sta T8
; T9 = FROM
    lda FROM
    sta T9
; while T9 != TO:
grid_line_sight_L21:
    lda T9
    cmp TO
    bne grid_line_sight_L24
grid_line_sight_L25:
    jmp grid_line_sight_L23
grid_line_sight_L24:
    jmp grid_line_sight_L22
grid_line_sight_L22:
; T10 = T8 - 128
    lda T8
    sec
    sbc #128
    sta T10
; T10 = T10 << 1
    lda T10
    asl
    sta T10
; T10 = T10 + 128
    lda T10
    clc
    adc #128
    sta T10
; T11 = 128 - T5
    lda #128
    sec
    sbc T5
    sta T11
; if T10 > T11:
    lda T10
    cmp T11
    beq grid_line_sight_L30
    bcs grid_line_sight_L29
grid_line_sight_L30:
    jmp grid_line_sight_L27
grid_line_sight_L29:
    jmp grid_line_sight_L26
grid_line_sight_L26:
; T8 = T8 - T5
    lda T8
    sec
    sbc T5
    sta T8
; T0 = T0 + T6
    lda T0
    clc
    adc T6
    sta T0
    jmp grid_line_sight_L28
grid_line_sight_L27:
grid_line_sight_L28:
; T11 = 128 + T4
    lda #128
    clc
    adc T4
    sta T11
; if T10 < T11:
    lda T10
    cmp T11
    bcc grid_line_sight_L34
grid_line_sight_L35:
    jmp grid_line_sight_L32
grid_line_sight_L34:
    jmp grid_line_sight_L31
grid_line_sight_L31:
; T8 = T8 + T4
    lda T8
    clc
    adc T4
    sta T8
; T1 = T1 + T7
    lda T1
    clc
    adc T7
    sta T1
    jmp grid_line_sight_L33
grid_line_sight_L32:
grid_line_sight_L33:
; T9 = T1 << 2
    lda T1
    asl
    asl
    sta T9
; T9 = T9 + T0
    lda T9
    clc
    adc T0
    sta T9
; T11 = C[T9]
    ldx T9
    lda C0,x
    sta T11
; if T11 != 0:
    lda T11
    cmp #0
    bne grid_line_sight_L39
grid_line_sight_L40:
    jmp grid_line_sight_L37
grid_line_sight_L39:
    jmp grid_line_sight_L36
grid_line_sight_L36:
; return
    rts
    jmp grid_line_sight_L38
grid_line_sight_L37:
grid_line_sight_L38:
    jmp grid_line_sight_L21
grid_line_sight_L23:
; 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. 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 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

demo.asm
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
VISIBLE=$80
VALID=$81
FROM=$82
TO=$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
T3=$97
T4=$98
T5=$99
T6=$9a
T7=$9b
T8=$9c
T9=$9d
T10=$9e
T11=$9f
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,0

A client retaining state between calls

exercise.inc, excerpt
preview_rules:
.if CUSTOM
; FROM = 0
    lda #0
    sta FROM
; TO = 14
    lda #14
    sta TO
; C5 = 0
    lda #0
    sta C5
.if BROKEN
; C5 = 0
    lda #0
    sta C5
.endif
    jsr checked_step
    jsr emit_state
    rts
.else
; FROM = 0
    lda #0
    sta FROM
; TO = 15
    lda #15
    sta TO
; C5 = 1
    lda #1
    sta C5
.if BROKEN
; C5 = 0
    lda #0
    sta C5
.endif
    jsr checked_step
    jsr emit_state
.endif
    rts

Customise it

Trace a different diagonal through a clear grid. 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
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
VISIBLE=$80
VALID=$81
FROM=$82
TO=$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
T3=$97
T4=$98
T5=$99
T6=$9a
T7=$9b
T8=$9c
T9=$9d
T10=$9e
T11=$9f
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,0

The custom diagnostic row is 01 01 00 0E 00 00 00 00. The full client trace in the source explains how it gets there.

Mistake and repair

The wrong caller clears a blocking cell before the query. The ray becomes clear because the world data was altered, not because the sight algorithm should see through a wall. Keep visibility queries read-only.

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
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
VISIBLE=$80
VALID=$81
FROM=$82
TO=$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
T3=$97
T4=$98
T5=$99
T6=$9a
T7=$9b
T8=$9c
T9=$9d
T10=$9e
T11=$9f
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
NES shows the deliberate error row 01 01 00 0F 00 00 00 00.
The wrong client completes with 01 01 00 0F 00 00 00 00; its changed inputs and state explain the result.

Cost and alternatives

The measured helper extent is 375 code bytes; public state is 20 bytes and private scratch is 12 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 visited cells after the observer. A cardinal corridor query is cheaper. Larger grids need wider coordinate/error arithmetic and a freshly checked bound.

This is a thin line with a stated tie rule, not a conservative supercover or a complete field-of-view system. Reversing a half-cell tie can choose a different raster cell. Decide corner visibility deliberately rather than assuming all line algorithms agree.

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

Cardinal sight: Keep the simpler row/column path when a game’s rules do not need diagonal rays.

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