193. Instruction pha

implement PHA behavior.

Lesson 193 of 356 · tests/chapter_01_cpu/test_193_instruction_pha.py

In this step, change only `emulator/cpu/instructions.py by adding pha(cpu). The stack page begins at STACK_BASE = 0x0100`.

Why this step exists

PHA copies A to the 6502 hardware stack. A push writes to the current $0100 | S slot first and then decrements the 8-bit stack pointer.

Suggested implementation

cpu.bus.write(0x0100 | cpu.s, cpu.a)
cpu.s = (cpu.s - 1) & 0xFF

Place those statements in `def pha(cpu: CPU); using the module-level STACK_BASE = 0x0100` in place of the literal is equivalent.

Invariants: push exactly A; write before decrementing; wrap S to eight bits; leave A and all status flags unchanged.

Misconception: pre-decrementing S does not implement a 6502 push; it writes to the next free slot rather than the current stack slot.

Out of scope: importing/registering opcode $48 belongs to step 194. PLA, PHP, PLP, TXS, and TSX belong to later steps 195-204 and must not be added here.

Run this lesson

uv run pytest tests/chapter_01_cpu/test_193_instruction_pha.py -v