205. Flags handler decimal flag
add Decimal-bit support.
Lesson 205 of 356 · tests/chapter_01_cpu/test_205_flags_handler_decimal_flag.py
Why this step exists
In this step, add `emulator/cpu/flags_handler.py symbols DECIMAL_FLAG, FlagsHandler.set_decimal_flag, and FlagsHandler.get_decimal_flag`. The helper keeps bit-3 manipulation in the status abstraction needed by subsequent flag-control operations. The NES CPU retains D even though ADC/SBC do not use 6502 BCD arithmetic.
Suggested implementation
DECIMAL_FLAG = 1 << 3
class FlagsHandler:
def set_decimal_flag(self, enabled: bool):
if enabled:
self.cpu.p |= DECIMAL_FLAG
else:
self.cpu.p &= ~DECIMAL_FLAG
def get_decimal_flag(self) -> bool:
return bool(self.cpu.p & DECIMAL_FLAG)Invariant: setting or clearing D preserves every other P bit, especially the neighboring Interrupt Disable bit. The common misconception is clearing with `p &= DECIMAL_FLAG`, which retains D and destroys unrelated flags.
Out of scope: instruction functions and opcode mappings belong to steps 206 and 207. Decimal-mode ADC/SBC behavior must not be introduced for the NES CPU.
Run this lesson
uv run pytest tests/chapter_01_cpu/test_205_flags_handler_decimal_flag.py -v