P2 · SystemVerilog · FPGA

RISC-V CPU_

A minimal RV64IMA RISC-V CPU, built from scratch, capable of booting Linux all the way into an interactive BusyBox shell. I built this to understand the full stack from instruction decode up through virtual memory and interrupt handling.

  • RV64IMA
  • Sv39
  • Boots Linux
  • 512-entry TLB
64-bitRV64 base ISA
Sv3939-bit virtual memory
512direct-mapped TLB entries
5stages, IF → WB
100 MHzclock in the device tree

ISA at a glance

RV64IMAUS_Zicsr_Zifencei_Sv39
I

64-bit integer base ISA.

M

Multiply / divide, every signed/unsigned variant. The kernel uses them all.

A

Atomics. Single-core, but OpenSBI needs at least LR/SC.

Zicsr

Control and Status Registers.

Zifencei

Instruction-fetch fence, implemented as a NOP.

S

Supervisor mode, where the Linux kernel runs.

U

User mode, where userspace runs.

Sv39

39-bit paged virtual memory.

Sstc

Supervisor timer interrupts. Simple, and cut timer overhead significantly.

DisabledC

Decoder implemented, but 4-byte instructions on 2-byte boundaries need misaligned access.

ShelvedF / D

Parameterized FP add/sub/mul/div/sqrt modules built, never wired in.

Unprivileged ISA Privilege & memory Not enabled

The core

In-order · single-issue
  1. IFFetch
  2. IDDecode
  3. EXExecute
  4. MEMMemory
  5. WBWrite-back

Not truly pipelined: each stage runs one after the other. That keeps complexity low once interrupts, virtual memory and variable-latency instructions enter the picture.

SoC memory map

from the device tree
0x0000_0000
RAM128 MiB

Main memory. The boot image is loaded at 0x1FF000.

0x1000_0000
UART16550A

Serial console at 115200 baud, interrupt-driven through the PLIC.

0x3000_0000
PLICSiFive-compatible

Platform-level interrupt controller for external interrupts.

0x4000_0000
CLINTSiFive

Core-local interruptor: mtime / mtimecmp timer interrupts.

From reset to shell

boot flow
  1. Load over UART

    Bootloader + fw_payload.bin are sent over serial and written to RAM at 0x1FF000.

    UART
  2. Bootloader

    Prints "Hello World!", sets a0 to 0 and a1 to the embedded device tree, then jumps to OpenSBI.

    M-mode
  3. OpenSBI

    Firmware built in payload mode, with the kernel bundled in.

    M-mode
  4. Linux kernel

    Custom Buildroot kernel with an embedded initramfs.

    S-mode
  5. BusyBox shell

    A working interactive shell prompt.

    U-mode

Hard parts

4 populated
Interrupts

Timer & external interrupts

Getting timer and external interrupts fully working, through the CLINT, Sstc and the PLIC.

Arithmetic

MUL / DIV / REM edge cases

Making sure every signed/unsigned edge case is covered, with tests aimed specifically at these instructions.

Hardware

A buggy FPGA

Perfect in Verilator, random crashes on the Gowin board. After ~10 days of debugging it turned out to be a known chip issue. Moving to an Alinx 7035b (Xilinx) fixed it.

Scope

Knowing when to stop

The C extension and floating-point units were built but left out: misaligned fetch and the sheer number of FP instructions weren't worth it.

Built with

  • SystemVerilog
  • Verilator
  • riscv-arch-test
  • Buildroot
  • OpenSBI
  • VSCode + TerosHDL
  • Vivado
  • Alinx 7035b

The full story (scoping, CSRs, interrupts, the device tree and the FPGA saga) is in the build log.