199. Instruction plp
implement PLP behavior.
Lesson 199 of 356 · tests/chapter_01_cpu/test_199_instruction_plp.py
In this step, change only `emulator/cpu/instructions.py by adding plp(cpu). Prerequisites: STACK_BASE` and RTI's status-mask convention already exist.
Why this step exists
PLP advances S to the saved status byte and replaces P with that byte after removing this emulator model's non-persistent bits 4 and 5.
Suggested implementation
cpu.s = (cpu.s + 1) & 0xFF
flags = cpu.bus.read(0x0100 | cpu.s)
cpu.p = flags & 0b1100_1111Place those statements in `def plp(cpu: CPU); the equivalent STACK_BASE + cpu.s` address also selects stack page $0100.
Invariants: increment and wrap S before reading; replace rather than merge P; clear bits 4 and 5; restore all other saved flag bits exactly; do not compute Z or N from the numeric byte as PLA does.
Misconception: PLP does not selectively OR flags into the old P. Doing so leaves stale flags that the stacked status explicitly cleared.
Out of scope: opcode $28 registration belongs to step 200. Later flag APIs must not be added to this implementation.
Run this lesson
uv run pytest tests/chapter_01_cpu/test_199_instruction_plp.py -v