Skip to content

My Attempt at writing a simple RISC-V CPU in Verilog.

Notifications You must be signed in to change notification settings

flavian112/riscv_cpu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RISCV CPU

An attempt at building a simple RISCV CPU in verilog. Currently my CPU implements the RV32I ISA without FENCE/ECALL/EBREAK instructions. The design is very much based on David and Sarah Harris' book "Digital Design and Computer Architecture (RISC-V Edition)".

FPGA

The board used in this project is a Tang Nano 9K with a GW1NR-LV9QN88PC6/I5 FPGA. There is a crystal clock onboard running at 27 MHz.

Build

  • make all alias for make simulate.
  • make rom to build rom.
  • make objdump to disassemble rom.
  • make size to display size information of rom.
  • make testvec to generate testvectors.
  • make simulate to run testbenches.
  • make wave to view waveform of cpu testbench in gtkwave.
  • make bitstream to generate bitstream.
  • make upload to upload bitstream to fpga.
  • make flash to flash bitstream to fpga.
  • make clean to clean build folder.

Project Structure

riscv_cpu
  |-> build         # build folder
  |-> debug
    |-> cpu.gtkw    # template for gtkwave
  |-> prog          # program that gets compiled to run on cpu
    |-> include
    |-> src
    |-> link.ld
    |-> Makefile
  |-> res           # various resources
  |-> rtl           # rtl sources (verilog files)
    |-> cst         # constraints file for fpga
    |-> include
    |-> src
    |-> Makefile
  |-> sim
    |-> gentestvec  # programs to generate testvectors for testbenches
      |-> src
      |-> Makefile
    |-> testbenches # testbench sources
      |-> src
      |-> Makefile
  |-> Makefile
  |-> README.md

Tools

Simulation

  • clang for compiling testvector generator sources
  • iverilog for building simulation
  • vvp for running simulation

ROM

  • riscv64 toolchain for building prog source files, although here used for compiling for riscv32

Synthesis

Debugging

Currently Supported Instructions

  • R-type: add, sub, and, or, xor, sll, srl, sra, slt, sltu
  • I-type: addi, andi, ori, xori, slti, sltiu, slli, srli, srai, lw, lh, lhu, lb, lbu
  • S-type: sw, sh, sb
  • B-type: beq, bne, blt, bge, bltu, bgeu
  • U-type: lui, auipc
  • J-type: jal, jalr

Resources

Design

Microarchitecture (RISC-V multicycle rv32i without FENCE/ECALL/EBREAK)

Microarchitecture

Control Unit FSM

Control Unit FSM

Memory Layout

Memory Layout

ALU

ALU

Waveform Example

Here we can see the waveforms of various internal signal of the CPU, executing the following instructions:

  addi  t0, zero, 5
  addi  t1, zero, 3
  add   t2, t0,   t1

Waveform adding two numbers

RISC-V

RV32I ISA

RV32I ISA Table

Registers

RV32 Register Table