RHDL includes a complete, cycle-accurate implementation of the MOS 6502 microprocessor — all 56 official instructions, 13 addressing modes, BCD arithmetic, and multiple simulation backends.

Architecture

┌─────────────────────────────────────────────────────┐
│                    MOS6502::CPU                      │
├─────────────────────────────────────────────────────┤
│  ┌─────────────────────────────────────────────┐    │
│  │              Registers                       │    │
│  │  A (8b)  X (8b)  Y (8b)  PC (16b)  SP (8b) │    │
│  │  Status: N V - B D I Z C                     │    │
│  └─────────────────────────────────────────────┘    │
│  ┌──────────────┐  ┌──────────────────────────┐    │
│  │   ALU (14    │  │   Control Unit (26-state  │    │
│  │  operations) │  │   FSM)                    │    │
│  └──────────────┘  └──────────────────────────┘    │
│  ┌──────────────┐  ┌──────────────────────────┐    │
│  │   Address    │  │   Instruction Decoder     │    │
│  │  Generator   │  │   (151 valid opcodes)     │    │
│  └──────────────┘  └──────────────────────────┘    │
└─────────────────────────────────────────────────────┘
                          │
                    ┌─────┴─────┐
                    │  64KB RAM │
                    └───────────┘

Components

ALU — 14 Operations

CodeOperationFlags
ADCAdd with carryN, V, Z, C
SBCSubtract with borrowN, V, Z, C
ANDBitwise ANDN, Z
ORABitwise ORN, Z
EORBitwise XORN, Z
ASLArithmetic shift leftN, Z, C
LSRLogical shift rightN, Z, C
ROLRotate leftN, Z, C
RORRotate rightN, Z, C
INCIncrementN, Z
DECDecrementN, Z
CMPCompareN, Z, C
BITBit testN, V, Z
TSTPass throughN, Z

Full BCD (decimal) mode support for ADC and SBC, matching real hardware behavior.

Control Unit — 26-State FSM

The control unit sequences instruction execution through states including RESET, FETCH, DECODE, EXECUTE, WRITE_MEM, PUSH, PULL, BRANCH, and interrupt handling (JSR/RTS/RTI/BRK).

13 Addressing Modes

ModeExampleCycles
ImpliedCLC2
AccumulatorASL A2
ImmediateLDA #$422
Zero PageLDA $003
Zero Page,XLDA $00,X4
Zero Page,YLDX $00,Y4
AbsoluteLDA $12344
Absolute,XLDA $1234,X4+
Absolute,YLDA $1234,Y4+
IndirectJMP ($1234)5
Indexed IndirectLDA ($00,X)6
Indirect IndexedLDA ($00),Y5+
RelativeBNE label2+

Modes marked ”+” take an extra cycle when crossing page boundaries.

Memory Map

RangeDescription
00FFZero Page
01FFStack
7FFFRAM
FFFFROM (program space)
FFFBNMI Vector
FFFDReset Vector
FFFFIRQ/BRK Vector

Built-In Assembler

RHDL includes a two-pass 6502 assembler:

        *= $8000
COUNT   = $10
 
START:  LDA #0
        STA COUNT
LOOP:   INC COUNT
        LDA COUNT
        CMP #10
        BNE LOOP
        BRK
cpu = MOS6502::CPU.new
cpu.assemble_and_load(source, 0x8000)
cpu.reset
cpu.run
puts cpu.status_string
# A:0A X:00 Y:00 SP:FD PC:800B P:33 [nv-BdIZC]

Simulation Backends

BackendSpeedUse Case
HDL Simulation~50K IPSDevelopment, debugging
Ruby ISA Simulator~500K IPSTest suites
Native Rust ISA~3.5M IPSPerformance benchmarks
IR JIT~230K cycles/sGate-level verification
IR Compiler~1.58M cycles/sLong simulations

Testing

189+ tests covering all instructions, addressing modes, BCD arithmetic, and algorithms (bubble sort, Fibonacci, multiplication, division):

bundle exec rake spec_6502

Next Steps