096. Instruction lsr a

Add the LSR accumulator instruction behavior.

Lesson 96 of 356 · tests/chapter_01_cpu/test_096_instruction_lsr_a.py

In this step, add accumulator-targeted LSR behavior after the memory primitive from Test 095.

File and symbol

emulator/cpu/instructions.py: lsr_a

Why this step exists

Accumulator LSR shares the shift and flags of Test 095 but has a different data destination, so it must update cpu.a without treating that register as an address.

Suggested implementation for this step

# emulator/cpu/instructions.py
def lsr_a(cpu: CPU):
    value = cpu.a
    result = value >> 1
    result_8 = result & 0xFF

    # Set flags
    cpu.flags.set_carry_flag((value & 0x01) != 0)
    cpu.flags.set_negative_flag(False)
    cpu.flags.set_zero_flag(result_8 == 0)

    cpu.a = result_8

Important invariants

  • old A bit 0 replaces Carry
  • Zero follows the final A value and Negative is always cleared
  • no bus read or write occurs
  • memory at an address numerically equal to the old A remains unchanged

Common misconception

cpu.a is the value and destination, not an address to pass to memory lsr.

Out of scope

  • opcode 0x4A wiring in Test 097
  • memory addressing opcodes in Tests 098-101
  • refactoring shared LSR logic or adding cycle timing

Run this lesson

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