6502 CPU Emulation Specification

January 20, 2026 ยท View on GitHub

A technical Markdown specification for emulating the MOS Technology 6502 CPU family, suitable for software emulators, educational tools, testing frameworks, and retrocomputing projects.


1. Scope

This specification describes the functional requirements for emulating:

  • MOS 6502
  • WDC 65C02 (where noted)

Out of scope:

  • Cycle-exact analog behavior
  • Physical bus contention
  • Undocumented silicon defects (unless explicitly implemented)

2. CPU Overview

Core Characteristics

FeatureValue
Data width8-bit
Address width16-bit
Address space64 KB
EndiannessLittle-endian
ClockSingle-phase

3. Registers

RegisterSizeDescription
A8-bitAccumulator
X8-bitIndex register
Y8-bitIndex register
SP8-bitStack pointer (page $01xx)
PC16-bitProgram counter
P8-bitProcessor status flags

Status Flags (P)

BitNameMeaning
7NNegative
6VOverflow
5-Unused (always 1 when pushed)
4BBreak
3DDecimal
2IIRQ disable
1ZZero
0CCarry

4. Memory Model

Addressing

  • 16-bit address bus ($0000-$FFFF)
  • Byte-addressable

Required Emulator Interfaces

read(address)  -> byte
write(address, byte)

Stack Behavior

  • Stack base: $0100
  • Push: write(\$0100 + SP, value); SP--
  • Pull: SP++; value = read(\$0100 + SP)

5. Reset and Interrupt Handling

Reset Sequence

  1. Set I = 1
  2. Set SP = $FD
  3. Clear D
  4. Load PC from $FFFC-$FFFD

Interrupt Vectors

InterruptVector Address
NMI$FFFA-$FFFB
RESET$FFFC-$FFFD
IRQ/BRK$FFFE-$FFFF

6. Instruction Fetch-Decode-Execute Cycle

Execution Loop (Conceptual)

opcode = read(PC++)
decode opcode
fetch operands
execute instruction
update flags
increment cycles

7. Addressing Modes

ModeExampleNotes
ImmediateLDA #\$10Constant
Zero PageLDA \$20Wraps at $00FF
AbsoluteLDA \$2000Full address
IndexedLDA \$2000,XOptional page penalty
IndirectJMP ($FFFC)Page wrap bug
Indexed IndirectLDA (\$20,X)ZP indexed
Indirect IndexedLDA (\$20),YZP pointer

8. Instruction Set Requirements

Categories

  • Load/Store
  • Arithmetic (ADC, SBC)
  • Logic (AND, ORA, EOR)
  • Shifts & Rotates
  • Branches
  • Stack operations
  • System control

Decimal Mode (NMOS 6502)

  • Applies to ADC and SBC
  • Uses BCD arithmetic when D = 1

9. Flags Behavior Rules

Instruction TypeFlags Affected
LoadsN, Z
ADC/SBCN, V, Z, C
CMP/CPX/CPYN, Z, C
INC/DECN, Z
ShiftsN, Z, C

10. Cycle Counting

Cycle Accuracy Levels

LevelDescription
FunctionalCorrect results only
Instruction-accurateFixed cycle counts
Cycle-accuratePage-cross penalties

Page Boundary Penalties

  • Branch taken: +1 cycle
  • Branch crosses page: +2 cycles
  • Indexed load crosses page: +1 cycle

11. Known Hardware Quirks (NMOS 6502)

QuirkDescription
JMP indirect bugHigh byte wrap at page boundary
BRK sets B flagOnly when pushed
Unused flag bitAlways reads as 1

12. Illegal / Undocumented Opcodes (Optional)

  • Many opcodes perform composite operations
  • Behavior varies by silicon revision
  • Should be disabled or explicitly enabled

13. Timing and Clocking

  • One instruction executed per multiple clock cycles
  • Emulator may execute instructions per host tick
  • Cycle counter required for I/O timing

14. Integration with Peripherals

Memory-Mapped I/O

if address in IO range:
    delegate to device

Examples:

  • 6522 VIA
  • UART
  • Video hardware

15. Testing and Validation

  • Klaus Dormann 6502 functional tests
  • Interrupt and decimal mode tests

Validation Checklist

  • All instructions execute correctly
  • Flags match reference behavior
  • Vectors handled properly
  • Stack operations correct


Document Scope: Software emulation of the 6502 CPU Audience: Emulator developers, retrocomputing engineers Status: Stable technical reference