Skip to content

Fill a Connected Grid Region on C64

A paint fill can also discover a connected room or reveal matching puzzle cells.

A paint fill can also discover a connected room or reveal matching puzzle cells.

C64 shows the working diagnostic row 00 09 03 01 09 09 00 00.
A completed native caller displays 00 09 03 01 09 09 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

The caller supplies a 4×4 row-major byte grid and SEED 0–15. Replace its four-connected region with REPLACEMENT and return CHANGED. Equal source/replacement is a valid no-op. The implementation repeatedly scans for source-colour cells touching the growing replacement region, but tracks membership separately in scratch so pre-existing replacement-colour cells cannot leak into it.

Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: SEED, REPLACEMENT, CHANGED, 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, T3, T4, T5, T6, T7, EXPR_SAVE, V0, V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15 (25 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.

Call bounded_flood_fill_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 bounded_flood_fill_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 C0–C15
Supply before service SEED and REPLACEMENT
Read after service CHANGED and VALID

A separate membership map distinguishes cells reached from the seed from cells that happened to have the replacement colour already. Repeated scans grow that membership until a scan adds nothing; only then are colours changed.

Reusable initialisation and service

6502.inc
; SPDX-License-Identifier: MIT
; Fill a Connected Grid Region. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
bounded_flood_fill_init:
    lda #0
    sta SEED
    sta REPLACEMENT
    sta CHANGED
    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
    sta T3
    sta T4
    sta T5
    sta T6
    sta T7
    sta EXPR_SAVE
    sta V0
    sta V1
    sta V2
    sta V3
    sta V4
    sta V5
    sta V6
    sta V7
    sta V8
    sta V9
    sta V10
    sta V11
    sta V12
    sta V13
    sta V14
    sta V15
    rts
bounded_flood_fill_step:
; VALID = 0
    lda #0
    sta VALID
; CHANGED = 0
    lda #0
    sta CHANGED
; if SEED >= 16:
    lda SEED
    cmp #16
    bcs bounded_flood_fill_L4
bounded_flood_fill_L5:
    jmp bounded_flood_fill_L2
bounded_flood_fill_L4:
    jmp bounded_flood_fill_L1
bounded_flood_fill_L1:
; return
    rts
    jmp bounded_flood_fill_L3
bounded_flood_fill_L2:
bounded_flood_fill_L3:
; VALID = 1
    lda #1
    sta VALID
; T0 = C[SEED]
    ldx SEED
    lda C0,x
    sta T0
; if T0 == REPLACEMENT:
    lda T0
    cmp REPLACEMENT
    beq bounded_flood_fill_L9
bounded_flood_fill_L10:
    jmp bounded_flood_fill_L7
bounded_flood_fill_L9:
    jmp bounded_flood_fill_L6
bounded_flood_fill_L6:
; return
    rts
    jmp bounded_flood_fill_L8
bounded_flood_fill_L7:
bounded_flood_fill_L8:
; T1 = 0
    lda #0
    sta T1
; while T1 < 16:
bounded_flood_fill_L11:
    lda T1
    cmp #16
    bcc bounded_flood_fill_L14
bounded_flood_fill_L15:
    jmp bounded_flood_fill_L13
bounded_flood_fill_L14:
    jmp bounded_flood_fill_L12
bounded_flood_fill_L12:
; V[T1] = 0
    lda #0
    sta EXPR_SAVE
    ldx T1
    lda EXPR_SAVE
    sta V0,x
; T1 = T1 + 1
    lda T1
    clc
    adc #1
    sta T1
    jmp bounded_flood_fill_L11
bounded_flood_fill_L13:
; V[SEED] = 1
    lda #1
    sta EXPR_SAVE
    ldx SEED
    lda EXPR_SAVE
    sta V0,x
; T2 = 1
    lda #1
    sta T2
; while T2 != 0:
bounded_flood_fill_L16:
    lda T2
    cmp #0
    bne bounded_flood_fill_L19
bounded_flood_fill_L20:
    jmp bounded_flood_fill_L18
bounded_flood_fill_L19:
    jmp bounded_flood_fill_L17
bounded_flood_fill_L17:
; T2 = 0
    lda #0
    sta T2
; T1 = 0
    lda #0
    sta T1
; while T1 < 16:
bounded_flood_fill_L21:
    lda T1
    cmp #16
    bcc bounded_flood_fill_L24
bounded_flood_fill_L25:
    jmp bounded_flood_fill_L23
bounded_flood_fill_L24:
    jmp bounded_flood_fill_L22
bounded_flood_fill_L22:
; T3 = V[T1]
    ldx T1
    lda V0,x
    sta T3
; if T3 == 0:
    lda T3
    cmp #0
    beq bounded_flood_fill_L29
bounded_flood_fill_L30:
    jmp bounded_flood_fill_L27
bounded_flood_fill_L29:
    jmp bounded_flood_fill_L26
bounded_flood_fill_L26:
; T3 = C[T1]
    ldx T1
    lda C0,x
    sta T3
; if T3 == T0:
    lda T3
    cmp T0
    beq bounded_flood_fill_L34
bounded_flood_fill_L35:
    jmp bounded_flood_fill_L32
bounded_flood_fill_L34:
    jmp bounded_flood_fill_L31
bounded_flood_fill_L31:
; T4 = 0
    lda #0
    sta T4
; T5 = T1 & 3
    lda T1
    and #3
    sta T5
; if T5 != 0:
    lda T5
    cmp #0
    bne bounded_flood_fill_L39
bounded_flood_fill_L40:
    jmp bounded_flood_fill_L37
bounded_flood_fill_L39:
    jmp bounded_flood_fill_L36
bounded_flood_fill_L36:
; T6 = T1 - 1
    lda T1
    sec
    sbc #1
    sta T6
; T4 = V[T6]
    ldx T6
    lda V0,x
    sta T4
    jmp bounded_flood_fill_L38
bounded_flood_fill_L37:
bounded_flood_fill_L38:
; if T5 != 3:
    lda T5
    cmp #3
    bne bounded_flood_fill_L44
bounded_flood_fill_L45:
    jmp bounded_flood_fill_L42
bounded_flood_fill_L44:
    jmp bounded_flood_fill_L41
bounded_flood_fill_L41:
; T6 = T1 + 1
    lda T1
    clc
    adc #1
    sta T6
; T7 = V[T6]
    ldx T6
    lda V0,x
    sta T7
; T4 = T4 | T7
    lda T4
    ora T7
    sta T4
    jmp bounded_flood_fill_L43
bounded_flood_fill_L42:
bounded_flood_fill_L43:
; if T1 >= 4:
    lda T1
    cmp #4
    bcs bounded_flood_fill_L49
bounded_flood_fill_L50:
    jmp bounded_flood_fill_L47
bounded_flood_fill_L49:
    jmp bounded_flood_fill_L46
bounded_flood_fill_L46:
; T6 = T1 - 4
    lda T1
    sec
    sbc #4
    sta T6
; T7 = V[T6]
    ldx T6
    lda V0,x
    sta T7
; T4 = T4 | T7
    lda T4
    ora T7
    sta T4
    jmp bounded_flood_fill_L48
bounded_flood_fill_L47:
bounded_flood_fill_L48:
; if T1 < 12:
    lda T1
    cmp #12
    bcc bounded_flood_fill_L54
bounded_flood_fill_L55:
    jmp bounded_flood_fill_L52
bounded_flood_fill_L54:
    jmp bounded_flood_fill_L51
bounded_flood_fill_L51:
; T6 = T1 + 4
    lda T1
    clc
    adc #4
    sta T6
; T7 = V[T6]
    ldx T6
    lda V0,x
    sta T7
; T4 = T4 | T7
    lda T4
    ora T7
    sta T4
    jmp bounded_flood_fill_L53
bounded_flood_fill_L52:
bounded_flood_fill_L53:
; if T4 != 0:
    lda T4
    cmp #0
    bne bounded_flood_fill_L59
bounded_flood_fill_L60:
    jmp bounded_flood_fill_L57
bounded_flood_fill_L59:
    jmp bounded_flood_fill_L56
bounded_flood_fill_L56:
; V[T1] = 1
    lda #1
    sta EXPR_SAVE
    ldx T1
    lda EXPR_SAVE
    sta V0,x
; T2 = 1
    lda #1
    sta T2
    jmp bounded_flood_fill_L58
bounded_flood_fill_L57:
bounded_flood_fill_L58:
    jmp bounded_flood_fill_L33
bounded_flood_fill_L32:
bounded_flood_fill_L33:
    jmp bounded_flood_fill_L28
bounded_flood_fill_L27:
bounded_flood_fill_L28:
; T1 = T1 + 1
    lda T1
    clc
    adc #1
    sta T1
    jmp bounded_flood_fill_L21
bounded_flood_fill_L23:
    jmp bounded_flood_fill_L16
bounded_flood_fill_L18:
; T1 = 0
    lda #0
    sta T1
; while T1 < 16:
bounded_flood_fill_L61:
    lda T1
    cmp #16
    bcc bounded_flood_fill_L64
bounded_flood_fill_L65:
    jmp bounded_flood_fill_L63
bounded_flood_fill_L64:
    jmp bounded_flood_fill_L62
bounded_flood_fill_L62:
; T3 = V[T1]
    ldx T1
    lda V0,x
    sta T3
; if T3 != 0:
    lda T3
    cmp #0
    bne bounded_flood_fill_L69
bounded_flood_fill_L70:
    jmp bounded_flood_fill_L67
bounded_flood_fill_L69:
    jmp bounded_flood_fill_L66
bounded_flood_fill_L66:
; C[T1] = REPLACEMENT
    lda REPLACEMENT
    sta EXPR_SAVE
    ldx T1
    lda EXPR_SAVE
    sta C0,x
; CHANGED = CHANGED + 1
    lda CHANGED
    clc
    adc #1
    sta CHANGED
    jmp bounded_flood_fill_L68
bounded_flood_fill_L67:
bounded_flood_fill_L68:
; T1 = T1 + 1
    lda T1
    clc
    adc #1
    sta T1
    jmp bounded_flood_fill_L61
bounded_flood_fill_L63:
    rts
component_end:

Machine contract

A/X/status may change; Y and decimal mode are preserved. Use CLD for binary arithmetic. 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 diagnostic PRG owns screen, font and colour RAM and disables interrupts; it does not return to BASIC. The reusable helper does none of those things.

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
SEED=$80
REPLACEMENT=$81
CHANGED=$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
T3=$97
T4=$98
T5=$99
T6=$9a
T7=$9b
EXPR_SAVE=$9c
V0=$9d
V1=$9e
V2=$9f
V3=$a0
V4=$a1
V5=$a2
V6=$a3
V7=$a4
V8=$a5
V9=$a6
V10=$a7
V11=$a8
V12=$a9
V13=$aa
V14=$ab
V15=$ac
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
COYOTE_STEPS=4
BUFFER_STEPS=4
TEXT_CAP=8
PROPERTY_COUNT=16
STREAM_BYTES=13
CRC_POLY=7
!source "caller.inc"
!source "exercise.inc"
component_start:
!source "component.inc"
!source "data.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 client retaining state between calls

exercise.inc, excerpt
preview_rules:
!if CUSTOM {
; SEED = 15
    lda #15
    sta SEED
; REPLACEMENT = 9
    lda #9
    sta REPLACEMENT
; C0 = 1
    lda #1
    sta C0
; C1 = 1
    lda #1
    sta C1
; C4 = 1
    lda #1
    sta C4
; C5 = 2
    lda #2
    sta C5
; C15 = 1
    lda #1
    sta C15
!if BROKEN {
; SEED = 15
    lda #15
    sta SEED
}
    jsr checked_step
    jsr emit_state
    rts
} else {
; SEED = 0
    lda #0
    sta SEED
; REPLACEMENT = 9
    lda #9
    sta REPLACEMENT
; C0 = 1
    lda #1
    sta C0
; C1 = 1
    lda #1
    sta C1
; C4 = 1
    lda #1
    sta C4
; C5 = 2
    lda #2
    sta C5
; C15 = 1
    lda #1
    sta C15
!if BROKEN {
; SEED = 15
    lda #15
    sta SEED
}
    jsr checked_step
    jsr emit_state
}
    rts

Customise it

Seed the disconnected bottom-right cell instead of the upper-left region. 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
SEED=$80
REPLACEMENT=$81
CHANGED=$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
T3=$97
T4=$98
T5=$99
T6=$9a
T7=$9b
EXPR_SAVE=$9c
V0=$9d
V1=$9e
V2=$9f
V3=$a0
V4=$a1
V5=$a2
V6=$a3
V7=$a4
V8=$a5
V9=$a6
V10=$a7
V11=$a8
V12=$a9
V13=$aa
V14=$ab
V15=$ac
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
COYOTE_STEPS=2
BUFFER_STEPS=2
TEXT_CAP=4
PROPERTY_COUNT=16
STREAM_BYTES=13
CRC_POLY=49
!source "caller.inc"
!source "exercise.inc"
component_start:
!source "component.inc"
!source "data.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 custom diagnostic row is 0F 09 01 01 01 01 00 00. The full client trace in the source explains how it gets there.

Mistake and repair

The wrong caller always supplies SEED=15. It changes the disconnected bottom-right region instead of the selected upper-left region. Convert the selected cell to a checked row-major index, then pass that index unchanged.

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
SEED=$80
REPLACEMENT=$81
CHANGED=$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
T3=$97
T4=$98
T5=$99
T6=$9a
T7=$9b
EXPR_SAVE=$9c
V0=$9d
V1=$9e
V2=$9f
V3=$a0
V4=$a1
V5=$a2
V6=$a3
V7=$a4
V8=$a5
V9=$a6
V10=$a7
V11=$a8
V12=$a9
V13=$aa
V14=$ab
V15=$ac
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
COYOTE_STEPS=4
BUFFER_STEPS=4
TEXT_CAP=8
PROPERTY_COUNT=16
STREAM_BYTES=13
CRC_POLY=7
!source "caller.inc"
!source "exercise.inc"
component_start:
!source "component.inc"
!source "data.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:
C64 shows the deliberate error row 0F 09 01 01 01 01 00 00.
The wrong client completes with 0F 09 01 01 01 01 00 00; its changed inputs and state explain the result.

Cost and alternatives

The measured helper extent is 514 code bytes; public state is 20 bytes and private scratch is 25 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.

Repeated full scans deliberately favour a small fixed workspace over a queue. At most fifteen growth scans can add cells, followed by a terminating scan. Larger or frame-budgeted maps need a different implementation.

The fixed 16-cell scan trades a queue for sixteen scratch membership bytes. It may scan repeatedly; larger maps should use an explicit bounded queue or span fill and budget work across frames. Diagonal contact is not connectivity. It is not a complete navigation solver.

Execution evidence

Four variants run on PAL stock-ROM PRG, 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-map queries: The fill changes a connected region in storage; a separate map query or redraw path can consume that result.

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