Z80 Sudoku – Code – Candidate Mask Builder

Input: HL = address of target cell ($A000 base).

Output: BC = 9-bit candidate mask (bits 0–8 used).

Flags: Z set if BC == 0 (contradiction).

Mask Representation

BC initialised to 0x01FF.

• C = bits 0–7 → digits 1–8• B bit 0 = bit 8 → digit 9• Upper bits of B remain 0

BuildCandidateMask:
    LD      BC,01FFh        ; all digits allowed
    PUSH    HL              ; preserve target pointer

; --- Row elimination ---
    CALL    EliminateRow

; --- Column elimination ---
    POP     HL
    PUSH    HL
    CALL    EliminateColumn

; --- Box elimination ---
    POP     HL
    PUSH    HL
    CALL    EliminateBox

    POP     HL

    ; Set flags according to BC
    LD      A,B
    OR      C
    RET

; --------------------------------
; ClearBitInMask
; Input: A = bit index (0–8)
; Mask in BC
; Destroys: AF, E
; --------------------------------
ClearBitInMask:
    CP      8
    JR      Z,ClearBit8

    LD      E,1
    LD      B,A
ShiftLoop:
    DJNZ    ShiftLoop
    ; (simple shift placeholder – can optimise later)
    ; E now holds bit mask for 0–7 case
    LD      A,E
    CPL
    AND     C
    LD      C,A
    RET

ClearBit8:
    RES     0,B            ; clear digit 9 bit
    RET