040. Instruction ldy
Add the core LDY instruction.
Lesson 40 of 356 · tests/chapter_01_cpu/test_040_instruction_ldy.py
File to update
emulator/cpu/instructions.pyLocation
instructions.ldy, beside stxWhy this step exists
LDY begins the Y-register load family by establishing its value-level behavior before any LDY opcode is connected. It mirrors LDA and LDX: assign an already-resolved value to the destination register, then update Zero and Negative.
Complete example implementation
# emulator/cpu/instructions.py
def ldy(cpu: CPU, value):
cpu.y = value
cpu._update_zero_and_negative_flags(cpu.y)Important invariants
- ldy receives a value, not an address
- Y receives the value while A and X remain unchanged
- Zero is set exactly for $00 and cleared otherwise
- Negative mirrors bit 7 and is cleared when bit 7 is zero
Common misconception
Do not fetch an operand or read memory inside ldy; future opcode handlers will resolve a value before calling this instruction.
Out of scope
- all LDY opcode handlers and opcode-table entries
- the STY instruction and its encodings
- cycle timing
Run this lesson
uv run pytest tests/chapter_01_cpu/test_040_instruction_ldy.py -v