This CPU implementation aims to help you learn the CPU architecture, RISC-V (an open-source instruction set architecture developed at UC Berkeley), and Chisel (a Scala Embedded Language), mainly guided by the publication "CPU Design with RISC-V and Chisel - First step to custom CPU implementation with open-source ISA."
The publication covers
- the basics of computer architecture
- RISC-V implementation with Chisel
- Base Integer Instruction Set
- CSR
- Instruction pipelining
- V Vector extension
- Custom Instruction (e.g., Population Count)
- riscv-tests
-
Clone this repo.
git clone https://github.com/chadyuu/riscv-chisel-book.git
-
Build the Docker image.
Recommended to install the Docker image from Docker Hub.
docker pull yutaronishiyama/riscv-chisel-book
Or build the image from Dockerfile.
docker build . -t yutaronishiyama/riscv-chisel-book
-
Run the image.
docker run -it -v $PWD/riscv-chisel-book:/src yutaronishiyama/riscv-chisel-book
-
Set constants
-
Base Instructions & riscv-tests
Below are the steps to run riscv-tests.
- Update
link.ld
$ vim /opt/riscv/riscv-tests/env/p/link.ld SECTIONS { . = 0x00000000; // update "0x80000000" }
- Build riscv-tests.
cd /opt/riscv/riscv-tests autoconf ./configure --prefix=/src/target make make install
- Generate Hex files of riscv-tests.
cd /src/chisel-template/src/shell ./tohex.sh
- Execute riscv-tests
cd /src/chisel-template/src/shell ./riscv_tests.sh riscvtests 05_RiscvTests
- Update
-
- Control hazards
- Data hazards
-
- VSETVLI
- Vector Load
- Vector Add
- Vector Store
-
Custom Instruction "Population Count (pcnt)"
-
Add "pcnt" to GNU Assembler.
/opt/riscv/riscv-gnu-toolchain/riscv-binutils/opcodes/riscv-opc.c
#include "opcode/riscv.h" ... const struct riscv_opcode riscv_opcodes[] = { ... /* name, xlen, isa, operands, match, mask, match_func, pinfo */ {"pcnt", 0, INSN_CLASS_I, "d,s", MATCH_PCNT, MASK_PCNT, match_opcode, 0}, // add ... }
-
Rebuild the assembler.
cd /opt/riscv/riscv-gnu-toolchain/build make clean make
-
-
Generate a Hex file from C program
cd /src/chisel-template/src/c make [filename.c]
-
ChiselTest on a Hex file
cd /src/chisel-template sbt "testOnly [package_name].HexTest"
Please see fpga branch and the documents for the security camp 2022.