171. Instruction bvs
implement Branch if Overflow Set behavior.
Lesson 171 of 356 · tests/chapter_01_cpu/test_171_instruction_bvs.py
In this step, add `emulator/cpu/instructions.py::bvs`:
def bvs(cpu: CPU, offset: int):
if cpu.flags.get_overflow_flag():
cpu.pc = (cpu.pc + offset) & 0xFFFFWhy this step exists
Step 163 already decodes the signed displacement, and PC is already at the next instruction when `bvs` runs. BVS therefore only tests Overflow and conditionally adds that displacement, masking the result to the CPU's 16-bit address space.
Invariants: Overflow set takes the branch; Overflow clear leaves PC unchanged. No flag, other register, or memory is changed. Misconception: BVS observes Overflow; it neither sets Overflow nor fetches or sign-converts an operand.
Out of scope: importing `bvs into emulator/cpu/opcodes.py, creating its relative opcode handler, and registering opcode 0x70` belong to step 179. Steps 172-178 first wire the other branch opcodes.
Run this lesson
uv run pytest tests/chapter_01_cpu/test_171_instruction_bvs.py -v