Deterministic Core 5 — WriteCellValue and SolveDeterministic

This note implements the value-commit routine and the top-level deterministic solve loop. At this point we have:

• BuildCandidateMask (BC = 9-bit mask)

• SingleCandidateFromMask (detect power-of-two mask → digit)

• Contradiction detection (BC == 0)

We now add:

• WriteCellValue — commit digit to board and trigger peer elimination

• SolveDeterministic — repeat-until-stable scan loop

Assumptions

Board layout:

• 81 bytes, row-major

• 0 = empty

• 1–9 = fixed digit

HL = pointer to current cell during scan

Routine: WriteCellValue

Input:

• HL = pointer to cell

• A = digit 1–9

Output:

• Cell written

• Global progress flag set

• Carry set on contradiction (if overwriting non-zero)

WriteCellValue:
    LD   B,A              ; preserve digit in B

    LD   A,(HL)
    OR   A
    JR   Z,WriteOK        ; empty → safe to write

    CP   B
    RET  Z                ; already same value → no-op

    SCF                   ; contradiction (different value)
    RET

WriteOK:
    LD   A,B
    LD   (HL),A           ; commit value

    LD   A,1
    LD   (ProgressFlag),A ; mark progress this pass

    OR   A                ; ensure carry clear
    RET

Global State

ProgressFlag:   DEFB 0
BoardBase:      DEFW 0    ; pointer to 81-byte board

Routine: SolveDeterministic

Algorithm:

1. Clear progress flag

2. Scan all 81 cells

3. For each empty cell:

• BuildCandidateMask

• If mask == 0 → contradiction

• If single candidate → WriteCellValue

4. If progress made → repeat

5. If no progress → return stable

SolveDeterministic:
RepeatPass:
    XOR  A
    LD   (ProgressFlag),A

    LD   HL,(BoardBase)
    LD   DE,81

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

    PUSH HL
    CALL BuildCandidateMask   ; BC = mask

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

    CALL SingleCandidateFromMask
    JR   Z,HaveSingle     ; Z=0 means single found

    POP  HL
    JR   NextCell

HaveSingle:
    ; A = digit
    POP  HL
    PUSH AF
    CALL WriteCellValue
    JR   C,Contradiction
    POP  AF

NextCell:
    INC  HL
    DEC  DE
    LD   A,D
    OR   E
    JR   NZ,ScanLoop

    LD   A,(ProgressFlag)
    OR   A
    JR   NZ,RepeatPass    ; continue until stable

    OR   A                ; stable, clear carry
    RET

Contradiction:
    SCF
    RET

Properties

• Deterministic naked-single solver

• Repeat-until-stable loop

• Immediate contradiction propagation via carry

• No recursion yet (pure propagation phase)

This completes the deterministic propagation engine. Next structural layer will be branching/backtracking built on top of this stable core.