{"title":"Deterministic Core 6 — WriteCellValue and Solve Loop","content":[{"heading":{"level":1,"text":"WriteCellValue"}},{"para":["Commits a resolved digit to the board, clears candidate mask, and sets progress flag."]},{"codeblock":{"lang":"asm","body":";----------------------------------------\n; WriteCellValue\n;----------------------------------------\n; Input:\n; HL = address of cell value byte\n; A = digit 1–9\n; Uses:\n; BC, DE\n;\n; Globals:\n; ProgressFlag (byte)\n;\nWriteCellValue:\n LD (HL),A ; store solved digit\n\n ; Clear candidate mask (2 bytes after value)\n INC HL\n XOR A\n LD (HL),A ; mask low\n INC HL\n LD (HL),A ; mask high\n\n ; Set progress flag\n LD A,1\n LD (ProgressFlag),A\n RET"}},{"heading":{"level":1,"text":"SolveDeterministic"}},{"para":["Full repeat-until-stable deterministic solver loop. Scans all 81 cells, builds mask for empty cells, detects naked singles, commits them, and halts on contradiction."]},{"codeblock":{"lang":"asm","body":";----------------------------------------\n; SolveDeterministic\n;----------------------------------------\n; Returns:\n; Z = 1 success (stable, no contradiction)\n; Z = 0 contradiction detected\n;\nSolveDeterministic:\nOuterLoop:\n XOR A\n LD (ProgressFlag),A ; clear progress\n\n LD HL,Board ; start of board\n LD DE,CellSize ; bytes per cell\n LD B,81 ; cell counter\n\nScanLoop:\n LD A,(HL)\n OR A\n JR NZ,NextCell ; skip filled cells\n\n PUSH BC\n PUSH HL\n\n CALL BuildCandidateMask ; -> BC = mask\n\n LD A,B\n OR C\n JR Z,Contradiction ; no candidates\n\n CALL SingleCandidateFromMask\n JR Z,NotSingle ; Z=1 means not single\n\n ; A = solved digit\n POP HL\n CALL WriteCellValue\n POP BC\n JR NextCell\n\nNotSingle:\n POP HL\n POP BC\n\nNextCell:\n ADD HL,DE\n DJNZ ScanLoop\n\n LD A,(ProgressFlag)\n OR A\n JR NZ,OuterLoop ; repeat if progress made\n\n XOR A ; stable, no contradiction\n CP A ; set Z=1\n RET\n\nContradiction:\n POP HL\n POP BC\n LD A,1\n OR 1 ; ensure NZ\n RET"}},{"heading":{"level":2,"text":"State Added"}},{"para":["• ProgressFlag byte controls repeat-until-stable loop","• Deterministic phase now fully wired: mask → single detection → commit","• Explicit contradiction exit when mask == 0","Next phase: integrate hidden singles (row/column/box scanning) into same outer loop before advancing to backtracking."}]}