Skip to content

Test a Point Against a Circle on NES

A round pickup should not become a square just because a box test was convenient.

A round pickup should not become a square just because a box test was convenient.

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

All coordinates and RADIUS are unsigned bytes in the same non-wrapping coordinate space. Return absolute DX/DY and HIT=1 when DX²+DY²≤RADIUS², including the boundary. Radius zero accepts only the centre. Inputs are preserved.

Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: HIT, DX, DY, POINT_X, POINT_Y, CENTRE_X, CENTRE_Y, RADIUS. They occupy 8 contiguous bytes. Also reserve the private workspace: T0, T1, T2, T3, T4, T5, T6, T7, T8, T9 (10 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.

Call point_circle_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 point_circle_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 POINT_X, POINT_Y, CENTRE_X, CENTRE_Y and RADIUS
Read after service HIT, DX and DY

Reject a point outside the radius on either axis first. For a surviving point, compare the second squared displacement with the squared-radius budget left after the first. No square root is needed.

Reusable initialisation and service

6502.inc
; SPDX-License-Identifier: MIT
; Test a Point Against a Circle. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
point_circle_init:
    lda #0
    sta HIT
    sta DX
    sta DY
    sta POINT_X
    sta POINT_Y
    sta CENTRE_X
    sta CENTRE_Y
    sta RADIUS
    sta T0
    sta T1
    sta T2
    sta T3
    sta T4
    sta T5
    sta T6
    sta T7
    sta T8
    sta T9
    rts
point_circle_step:
; HIT = 0
    lda #0
    sta HIT
; if POINT_X >= CENTRE_X:
    lda POINT_X
    cmp CENTRE_X
    bcs point_circle_L4
point_circle_L5:
    jmp point_circle_L2
point_circle_L4:
    jmp point_circle_L1
point_circle_L1:
; DX = POINT_X - CENTRE_X
    lda POINT_X
    sec
    sbc CENTRE_X
    sta DX
    jmp point_circle_L3
point_circle_L2:
; DX = CENTRE_X - POINT_X
    lda CENTRE_X
    sec
    sbc POINT_X
    sta DX
point_circle_L3:
; if POINT_Y >= CENTRE_Y:
    lda POINT_Y
    cmp CENTRE_Y
    bcs point_circle_L9
point_circle_L10:
    jmp point_circle_L7
point_circle_L9:
    jmp point_circle_L6
point_circle_L6:
; DY = POINT_Y - CENTRE_Y
    lda POINT_Y
    sec
    sbc CENTRE_Y
    sta DY
    jmp point_circle_L8
point_circle_L7:
; DY = CENTRE_Y - POINT_Y
    lda CENTRE_Y
    sec
    sbc POINT_Y
    sta DY
point_circle_L8:
; if DX > RADIUS:
    lda DX
    cmp RADIUS
    beq point_circle_L15
    bcs point_circle_L14
point_circle_L15:
    jmp point_circle_L12
point_circle_L14:
    jmp point_circle_L11
point_circle_L11:
; return
    rts
    jmp point_circle_L13
point_circle_L12:
point_circle_L13:
; if DY > RADIUS:
    lda DY
    cmp RADIUS
    beq point_circle_L20
    bcs point_circle_L19
point_circle_L20:
    jmp point_circle_L17
point_circle_L19:
    jmp point_circle_L16
point_circle_L16:
; return
    rts
    jmp point_circle_L18
point_circle_L17:
point_circle_L18:
; T4 = RADIUS
    lda RADIUS
    sta T4
; T5 = 0
    lda #0
    sta T5
; T1 = RADIUS
    lda RADIUS
    sta T1
; T6 = 0
    lda #0
    sta T6
; T7 = 0
    lda #0
    sta T7
; T0 = 8
    lda #8
    sta T0
; while T0 != 0:
point_circle_L21:
    lda T0
    cmp #0
    bne point_circle_L24
point_circle_L25:
    jmp point_circle_L23
point_circle_L24:
    jmp point_circle_L22
point_circle_L22:
; T9 = T1 & 1
    lda T1
    and #1
    sta T9
; if T9 != 0:
    lda T9
    cmp #0
    bne point_circle_L29
point_circle_L30:
    jmp point_circle_L27
point_circle_L29:
    jmp point_circle_L26
point_circle_L26:
; T8 = T6
    lda T6
    sta T8
; T6 = T6 + T4
    lda T6
    clc
    adc T4
    sta T6
; T9 = 0
    lda #0
    sta T9
; if T6 < T8:
    lda T6
    cmp T8
    bcc point_circle_L34
point_circle_L35:
    jmp point_circle_L32
point_circle_L34:
    jmp point_circle_L31
point_circle_L31:
; T9 = 1
    lda #1
    sta T9
    jmp point_circle_L33
point_circle_L32:
point_circle_L33:
; T7 = T7 + T5
    lda T7
    clc
    adc T5
    sta T7
; T7 = T7 + T9
    lda T7
    clc
    adc T9
    sta T7
    jmp point_circle_L28
point_circle_L27:
point_circle_L28:
; T9 = T4 >> 7
    lda T4
    lsr
    lsr
    lsr
    lsr
    lsr
    lsr
    lsr
    sta T9
; T4 = T4 << 1
    lda T4
    asl
    sta T4
; T5 = T5 << 1
    lda T5
    asl
    sta T5
; T5 = T5 | T9
    lda T5
    ora T9
    sta T5
; T1 = T1 >> 1
    lda T1
    lsr
    sta T1
; T0 = T0 - 1
    lda T0
    sec
    sbc #1
    sta T0
    jmp point_circle_L21
point_circle_L23:
; T2 = T6
    lda T6
    sta T2
; T3 = T7
    lda T7
    sta T3
; T4 = DX
    lda DX
    sta T4
; T5 = 0
    lda #0
    sta T5
; T1 = DX
    lda DX
    sta T1
; T6 = 0
    lda #0
    sta T6
; T7 = 0
    lda #0
    sta T7
; T0 = 8
    lda #8
    sta T0
; while T0 != 0:
point_circle_L36:
    lda T0
    cmp #0
    bne point_circle_L39
point_circle_L40:
    jmp point_circle_L38
point_circle_L39:
    jmp point_circle_L37
point_circle_L37:
; T9 = T1 & 1
    lda T1
    and #1
    sta T9
; if T9 != 0:
    lda T9
    cmp #0
    bne point_circle_L44
point_circle_L45:
    jmp point_circle_L42
point_circle_L44:
    jmp point_circle_L41
point_circle_L41:
; T8 = T6
    lda T6
    sta T8
; T6 = T6 + T4
    lda T6
    clc
    adc T4
    sta T6
; T9 = 0
    lda #0
    sta T9
; if T6 < T8:
    lda T6
    cmp T8
    bcc point_circle_L49
point_circle_L50:
    jmp point_circle_L47
point_circle_L49:
    jmp point_circle_L46
point_circle_L46:
; T9 = 1
    lda #1
    sta T9
    jmp point_circle_L48
point_circle_L47:
point_circle_L48:
; T7 = T7 + T5
    lda T7
    clc
    adc T5
    sta T7
; T7 = T7 + T9
    lda T7
    clc
    adc T9
    sta T7
    jmp point_circle_L43
point_circle_L42:
point_circle_L43:
; T9 = T4 >> 7
    lda T4
    lsr
    lsr
    lsr
    lsr
    lsr
    lsr
    lsr
    sta T9
; T4 = T4 << 1
    lda T4
    asl
    sta T4
; T5 = T5 << 1
    lda T5
    asl
    sta T5
; T5 = T5 | T9
    lda T5
    ora T9
    sta T5
; T1 = T1 >> 1
    lda T1
    lsr
    sta T1
; T0 = T0 - 1
    lda T0
    sec
    sbc #1
    sta T0
    jmp point_circle_L36
point_circle_L38:
; T8 = 0
    lda #0
    sta T8
; if T2 < T6:
    lda T2
    cmp T6
    bcc point_circle_L54
point_circle_L55:
    jmp point_circle_L52
point_circle_L54:
    jmp point_circle_L51
point_circle_L51:
; T8 = 1
    lda #1
    sta T8
    jmp point_circle_L53
point_circle_L52:
point_circle_L53:
; T2 = T2 - T6
    lda T2
    sec
    sbc T6
    sta T2
; T3 = T3 - T7
    lda T3
    sec
    sbc T7
    sta T3
; T3 = T3 - T8
    lda T3
    sec
    sbc T8
    sta T3
; T4 = DY
    lda DY
    sta T4
; T5 = 0
    lda #0
    sta T5
; T1 = DY
    lda DY
    sta T1
; T6 = 0
    lda #0
    sta T6
; T7 = 0
    lda #0
    sta T7
; T0 = 8
    lda #8
    sta T0
; while T0 != 0:
point_circle_L56:
    lda T0
    cmp #0
    bne point_circle_L59
point_circle_L60:
    jmp point_circle_L58
point_circle_L59:
    jmp point_circle_L57
point_circle_L57:
; T9 = T1 & 1
    lda T1
    and #1
    sta T9
; if T9 != 0:
    lda T9
    cmp #0
    bne point_circle_L64
point_circle_L65:
    jmp point_circle_L62
point_circle_L64:
    jmp point_circle_L61
point_circle_L61:
; T8 = T6
    lda T6
    sta T8
; T6 = T6 + T4
    lda T6
    clc
    adc T4
    sta T6
; T9 = 0
    lda #0
    sta T9
; if T6 < T8:
    lda T6
    cmp T8
    bcc point_circle_L69
point_circle_L70:
    jmp point_circle_L67
point_circle_L69:
    jmp point_circle_L66
point_circle_L66:
; T9 = 1
    lda #1
    sta T9
    jmp point_circle_L68
point_circle_L67:
point_circle_L68:
; T7 = T7 + T5
    lda T7
    clc
    adc T5
    sta T7
; T7 = T7 + T9
    lda T7
    clc
    adc T9
    sta T7
    jmp point_circle_L63
point_circle_L62:
point_circle_L63:
; T9 = T4 >> 7
    lda T4
    lsr
    lsr
    lsr
    lsr
    lsr
    lsr
    lsr
    sta T9
; T4 = T4 << 1
    lda T4
    asl
    sta T4
; T5 = T5 << 1
    lda T5
    asl
    sta T5
; T5 = T5 | T9
    lda T5
    ora T9
    sta T5
; T1 = T1 >> 1
    lda T1
    lsr
    sta T1
; T0 = T0 - 1
    lda T0
    sec
    sbc #1
    sta T0
    jmp point_circle_L56
point_circle_L58:
; if T7 > T3:
    lda T7
    cmp T3
    beq point_circle_L75
    bcs point_circle_L74
point_circle_L75:
    jmp point_circle_L72
point_circle_L74:
    jmp point_circle_L71
point_circle_L71:
; return
    rts
    jmp point_circle_L73
point_circle_L72:
point_circle_L73:
; if T7 == T3:
    lda T7
    cmp T3
    beq point_circle_L79
point_circle_L80:
    jmp point_circle_L77
point_circle_L79:
    jmp point_circle_L76
point_circle_L76:
; if T6 > T2:
    lda T6
    cmp T2
    beq point_circle_L85
    bcs point_circle_L84
point_circle_L85:
    jmp point_circle_L82
point_circle_L84:
    jmp point_circle_L81
point_circle_L81:
; return
    rts
    jmp point_circle_L83
point_circle_L82:
point_circle_L83:
    jmp point_circle_L78
point_circle_L77:
point_circle_L78:
; HIT = 1
    lda #1
    sta HIT
    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
HIT=$80
DX=$81
DY=$82
POINT_X=$83
POINT_Y=$84
CENTRE_X=$85
CENTRE_Y=$86
RADIUS=$87
T0=$88
T1=$89
T2=$8a
T3=$8b
T4=$8c
T5=$8d
T6=$8e
T7=$8f
T8=$90
T9=$91
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
; POINT_X = 3
    lda #3
    sta POINT_X
; POINT_Y = 4
    lda #4
    sta POINT_Y
; RADIUS = 5
    lda #5
    sta RADIUS
.if BROKEN
; POINT_Y = CENTRE_Y
    lda CENTRE_Y
    sta POINT_Y
.endif
    jsr checked_step
    jsr emit_state
    rts
.else
; POINT_X = 4
    lda #4
    sta POINT_X
; POINT_Y = 4
    lda #4
    sta POINT_Y
; RADIUS = 5
    lda #5
    sta RADIUS
.if BROKEN
; POINT_Y = CENTRE_Y
    lda CENTRE_Y
    sta POINT_Y
.endif
    jsr checked_step
    jsr emit_state
.endif
    rts

Customise it

Move onto the familiar 3–4–5 boundary. 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
HIT=$80
DX=$81
DY=$82
POINT_X=$83
POINT_Y=$84
CENTRE_X=$85
CENTRE_Y=$86
RADIUS=$87
T0=$88
T1=$89
T2=$8a
T3=$8b
T4=$8c
T5=$8d
T6=$8e
T7=$8f
T8=$90
T9=$91
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 03 04 03 04 00 00 05. The full client trace in the source explains how it gets there.

Mistake and repair

The wrong caller replaces the point’s vertical coordinate with the centre’s. A point outside the round boundary is tested as though it lay on the horizontal axis. Keep both axes in the same coordinate space.

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
HIT=$80
DX=$81
DY=$82
POINT_X=$83
POINT_Y=$84
CENTRE_X=$85
CENTRE_Y=$86
RADIUS=$87
T0=$88
T1=$89
T2=$8a
T3=$8b
T4=$8c
T5=$8d
T6=$8e
T7=$8f
T8=$90
T9=$91
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 04 00 04 00 00 00 05.
The wrong client completes with 01 04 00 04 00 00 00 05; its changed inputs and state explain the result.

Cost and alternatives

The measured helper extent is 686 code bytes; public state is 8 bytes and private scratch is 10 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.

Cheap axis rejection can avoid all three products. Surviving points use three eight-stage byte products. A bounding box can precede the test; a pixel mask can follow it when the actual opaque outline matters.

This is a point/circle test, not circle/circle or pixel-mask collision. Two byte-sized squared axis distances can sum beyond 65,535; the helper avoids that overflow by subtracting DX² from the radius budget before comparing DY².

Execution evidence

Four variants run on NTSC NROM, background only, DMC/NMI disabled, using Asm198x 0.0.72, Emu198x 0.44.2. 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

Rectangle overlap: Use a broad-phase box to discard distant pairs before paying for a more precise shape test.

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