Skip to content


Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Computer Architecture Task 2


This is the second homework of Computer Architecture, and the author is Lmxyy.


  • Implement a Risc-v CPU using hardware description language(HDL), and then download the CPU on FPGA.
  • Using C++ to simulate the memory via the USB-UART protocol.

Instructions [100%]

  • [X] LUI
  • [X] AUIPC
  • [X] JAL
  • [X] JALR
  • [X] BEQ
  • [X] BNE
  • [X] BLT
  • [X] BGE
  • [X] BLTU
  • [X] BGEU
  • [X] ADDI
  • [X] SLTI
  • [X] SLTIU
  • [X] XORI
  • [X] ORI
  • [X] ANDI
  • [X] SLLI
  • [X] SRLI
  • [X] SRAI
  • [X] ADD
  • [X] SUB
  • [X] SLL
  • [X] SLT
  • [X] SLTU
  • [X] XOR
  • [X] SRL
  • [X] SRA
  • [X] OR
  • [X] AND
  • [X] LB
  • [X] LH
  • [X] LW
  • [X] LBU
  • [X] LHU
  • [X] SB
  • [X] SH
  • [X] SW

Memory [50%]

  • [-] UART-USB Protocol
    • [X] buf_que module
    • [X] uart_com module
    • [ ] encoder module
    • [ ] decoder module
  • [X] FPGA Communication
    • [X] adapter.cpp
    • [X] adapter.hpp
    • [X] env_iface.hpp
    • [X] environment.hpp
    • [X] main.cpp

Design Idea

According to the book 《自己动手写CPU》, implemented a riscv32 cpu which adopted Von Neumann Architecture. More details are following:





  • The instruction set arthitecture(ISA) is RV32I Base Integer Instruction Set, Version 2.0.
  • The implementation technology is FPGA.
  • The HDL is Verilog HDL.
  • The communication protocol is UART-USB.

Branch Predictor

pdt module is a branch predictor, which use tournament to predict the result:

  • According to the 2nd to 11th bits in pc address, choose an alloyed branch predictor, and the then by its saturating counter decide which predictor I would use.
  • If I decide the global one, I would choose a saturating counter according to the latest 10 branch results.
  • If I decide the local one, I would choose a saturating counter by both the 2nd to 11th bits in pc address and the latest 3 branch results(I guessed this would be more accurate).

Prediction will be done in the if stage, and I will pass the predicting results to pc and id. After id knows the branch results, it would deliver the feedback the the predictor.


  • 2 way set-associative cache, 8 bytes in each block, and the length of index is adjustable;
  • Replace strategy: LRU;
  • Write strategy: write through;
  • If replacement is needed, the cache will stop the cpu till the replacement finishes and the requiring data are read out from the memory(icache only stalls if, while dcache stalls all stages except wb);
  • Because the memory is just a simulator, so I connect both icache and dcache to memories which make them able to read data simultaneously.




Format of Riscv32 Assembler

LUIlui rd, imm(20bits)
ADDIaddi rd, rs, (signed)imm(11bits)
SLTIslti rd, rs, (signed)imm(11bits)
SLTIUsltiu rd, rs, (signed)imm(11bits)
XORIxori rd, rs, (signed)imm(11bits)
ORIori rd, rs, (signed)imm(11bits)
ANDIandi rd, rs, (signed)imm(11bits)
SLLIslli rd, rs, (signed)imm(5bits)
SRAIsrai rd, rs, (signed)imm(5bits)
ADDadd rd, rs1, rs2
SUBsub rd, rs1, rs2
SLLsll rd, rs1, rs2
SLTslt rd, rs1, rs2
SLTUsltu rd, rs1, rs2
XORxor rd, rs1, rs2
SRLsrl rd, rs1, rs2
SRAsra rd, rs1, rs2
ORor rd, rs1, rs2
ANDand rd, rs1, rs2
JALjal rd, label
JALRjalr rd, (signed)12-bit-offset(rs)
BEQbeq rs, rt, lable
BNEbne rs, rt, lable
BLTblt rs, rt, lable
BGEbge rs, rt, lable
BLTUbltu rs, rt, lable
BGEUbgeu rs, rt, lable
LBlb rd, (signed)12-bit-offset(rs)
LBUlbu rd, (signed)12-bit-offset(rs)
LHlh rd, (signed)12-bit-offset(rs)
LHUlhu rd, (signed)12-bit-offset(rs)
LWlw rd, (signed)12-bit-offset(rs)
SBsb rs, (signed)12-bit-offset(rs)
SHsh rs, (signed)12-bit-offset(rs)
SWsw rs, (signed)12-bit-offset(rs)

How to Install and Use Riscv Toolchain

Clone the toolchain locally.

If you could climb over the wall:

Run the following codes:

$ git clone --recursive

or alternatively:

$ git clone
$ cd riscv-gnu-toolchain
$ git submodule update --init --recursive

The source codes are somewhat large, please wait patiently.

If you couldn’t climb over the wall:

Download the package at by Zhanghao Wu). Remember to depackage with the command:

tar -zxvf riscv-gnu-toolchain.tar.gz

If you use git directly, maybe you couldn’t clone all source codes which may cause that your toolchain cannot work normally.

Make the source code.

Run the following command to get riscv32.

sudo ./configure --prefix=/opt/riscv --with-arch=rv32gc --with-abi=ilp32d
sudo make

Modify your path variable.

First run the command

sudo gedit /etc/profile

Then add

export PATH="$PATH:/opt/riscv/bin"

Next, reboot or logout.

Tranform the assembler to riscv instructions.

Use the following command

if [ $# -eq 0 ]; then
    riscv32-unknown-elf-as -o instr.o -march=rv32i instr.s 
    riscv32-unknown-elf-ld instr.o -o
    riscv32-unknown-elf-objcopy -O binary instr.bin
    ../Tools/Bin_to_Text instr.bin >
    rm instr.o instr.bin    
elif [ $# -eq 1 ]; then
    riscv32-unknown-elf-as -o $1.o -march=rv32i $1.s 
    riscv32-unknown-elf-ld $1.o -o $
    riscv32-unknown-elf-objcopy -O binary $ $1.bin
    ../Tools/Bin_to_Text $1.bin >
    rm $1.o $ $1.bin
    echo "Please input less than 2 paraments!"

which was included in You need run the bash with exactly one parament which shows that your assembler source code’s name is “$1.s”. And you will get an file named which contains your hexadecimal riscv32 instructions. Attention: Please modify the path of Bin_to_Text according to your current directory.

Transform a binary file to a text file.

The source code was Bin_to_Text.cpp.

using namespace std;

typedef long long ll;
const int NSIZE = 8;

inline ll convert(ll num)
    ll a[4] = {0,0,0,0},ret = 0;
    for (int i = 0;i < 4;++i,num >>= 8)
        a[i] = num&((1<<8)-1);
    for (int i = 0;i < 4;++i)
        ret = (ret<<8)|a[i];
    return ret;

int main(int argc,char *argv[])
    if (argc == 1||argc > 2)
        cerr << "Please input an binary file." << endl;
        return 0;
    ifstream ifile(argv[1],ios::in|ios::binary);
    if (!ifile)
        cerr << "Cannot open file." << endl;
        return 0;
    int head = ifile.tellg(),tail = (ifile.seekg(0,ios::end)).tellg();
    int N = (tail-head)/4;
    while (N--)
        ll num = 0; int now = 0;
        for (int k = 0;k < 4;++k)
            char c; *)&c,sizeof(char));
            for (int i = 0;i < NSIZE;++i,c >>= 1)
                num |= ((ll)(c&1))<<(now++);
        cout.width(8); cout.fill('0');
        cerr.width(8); cerr.fill('0');
        cout << hex << convert(num) << endl;
        cerr << hex << num << endl;
    cerr << "Congratulations, convert successfully!." << endl;
    return 0;

Install C++ Serial Communication Library and Boost.

  • Serial Communication Library
  • Boost or you could run the following command in Ubuntu
    sudo apt install libboost-program-options-dev 
  • To compile the cpp, you need to run the following command in the correspondent directory:
    g++ *.cpp -c -std=c++14 -I /tmp/usr/local/include/
    g++ *.o -o cpu-judge -L /tmp/usr/local/lib/ -lboost_program_options -lserial


Riscv32 CPU Project








No releases published


No packages published