Deterministic Fill Loop (Full Implementation)

This routine repeatedly scans all 81 cells, builds candidate masks for empty cells, fills naked singles, and repeats until no further progress is possible.

High-Level Structure

DeterministicFillLoop:

OuterLoop:
    LD   D,0              ; D = progress flag (0 = none this pass)

    LD   HL,BoardBase     ; HL points to first cell
    LD   B,9              ; row counter

RowLoop:
    LD   C,9              ; column counter

ColLoop:
    LD   A,(HL)
    OR   A
    JR   NZ,NextCell      ; skip filled cells

    ; --- Empty cell ---
    PUSH HL               ; preserve cell pointer

    CALL BuildCandidateMask   ; returns BC = mask

    LD   A,B
    OR   C
    JR   Z,Contradiction      ; no candidates

    CALL SingleCandidateFromMask
    JR   Z,NotSingle          ; Z=1 means not single

    ; --- Single found ---
    POP  HL
    LD   (HL),A               ; write digit 1–9
    LD   D,1                  ; mark progress
    JR   Advance

NotSingle:
    POP  HL
    JR   Advance

NextCell:
    ; already filled

Advance:
    INC  HL
    DEC  C
    JR   NZ,ColLoop

    DEC  B
    JR   NZ,RowLoop

    LD   A,D
    OR   A
    JR   NZ,OuterLoop     ; repeat if progress made

    RET                   ; stable — deterministic phase complete

Contradiction:
    ; BC == 0 at some empty cell
    ; Signal failure via Z flag set
    XOR  A
    RET

Design Notes

• D register is used as a per-pass progress flag.

• HL linearly traverses BoardBase (row-major layout, 81 bytes).

• Contradiction detected immediately when BC == 0.

• Outer loop repeats until a full pass produces no writes.

• Deterministic phase terminates in stable fixed point state.