All Projects → lmxyy → Computer-Architecture-Task-2

lmxyy / Computer-Architecture-Task-2

Licence: MIT License
Riscv32 CPU Project

Programming Languages

Verilog
626 projects
assembly
5116 projects
c
50402 projects - #5 most used programming language
Stata
111 projects
HTML
75241 projects
Makefile
30231 projects

Projects that are alternatives of or similar to Computer-Architecture-Task-2

Mipt Mips
Cycle-accurate pre-silicon simulator of RISC-V and MIPS CPUs
Stars: ✭ 250 (+481.4%)
Mutual labels:  cpu, pipeline, risc-v
Neorv32
A small and customizable full-scale 32-bit RISC-V soft-core CPU and SoC written in platform-independent VHDL.
Stars: ✭ 106 (+146.51%)
Mutual labels:  cpu, risc-v
Riscboy
Portable games console, designed from scratch: CPU, graphics, PCB, and the kitchen sink
Stars: ✭ 103 (+139.53%)
Mutual labels:  cpu, risc-v
Rv12
RISC-V CPU Core
Stars: ✭ 162 (+276.74%)
Mutual labels:  cpu, risc-v
Ttyplot
a realtime plotting utility for terminal/console with data input from stdin
Stars: ✭ 532 (+1137.21%)
Mutual labels:  cpu, pipeline
Darkriscv
opensouce RISC-V cpu core implemented in Verilog from scratch in one night!
Stars: ✭ 1,062 (+2369.77%)
Mutual labels:  cpu, risc-v
E200 opensource
This repository hosts the project for open-source hummingbird E203 RISC processor Core.
Stars: ✭ 1,909 (+4339.53%)
Mutual labels:  cpu, risc-v
Awesome Cpus
All CPU and MCU documentation in one place
Stars: ✭ 1,602 (+3625.58%)
Mutual labels:  cpu, risc-v
HTGS
The Hybrid Task Graph Scheduler API
Stars: ✭ 36 (-16.28%)
Mutual labels:  cpu, pipeline
Biriscv
32-bit Superscalar RISC-V CPU
Stars: ✭ 208 (+383.72%)
Mutual labels:  cpu, risc-v
Riscv
RISC-V CPU Core (RV32IM)
Stars: ✭ 272 (+532.56%)
Mutual labels:  cpu, risc-v
WebRISC-V
WebRISC-V: A Web-Based Education-Oriented RISC-V Pipeline Simulation Environment [PHP]
Stars: ✭ 74 (+72.09%)
Mutual labels:  pipeline, risc-v
Riscv Rust
RISC-V processor emulator written in Rust+WASM
Stars: ✭ 253 (+488.37%)
Mutual labels:  cpu, risc-v
Ustc Rvsoc
FPGA-based RISC-V CPU+SoC.
Stars: ✭ 77 (+79.07%)
Mutual labels:  cpu, risc-v
Riscv Fs
F# RISC-V Instruction Set formal specification
Stars: ✭ 173 (+302.33%)
Mutual labels:  cpu, risc-v
yatcpu
Yet another toy CPU.
Stars: ✭ 42 (-2.33%)
Mutual labels:  cpu, risc-v
smag
Show Me A Graph - Command Line Graphing
Stars: ✭ 78 (+81.4%)
Mutual labels:  cpu, pipeline
fire-cloud
基于Spring Cloud的微服务业务框架
Stars: ✭ 16 (-62.79%)
Mutual labels:  pipeline
Sensors
A macOS application displaying the thermal, voltage and current sensor values.
Stars: ✭ 70 (+62.79%)
Mutual labels:  cpu
rurality
开源运维平台设计及开发样例、CMS、RBAC、python开发教程、管理系统设计及开发样例、jenkinsfile(pipeline)/ansible使用教程,一切想到的,想不到的,应有尽有
Stars: ✭ 51 (+18.6%)
Mutual labels:  pipeline

Computer Architecture Task 2

Authority

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

Aim

  • 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:

Picture/openmips.png

Picture/openmips_min_sopc.png

Feature

General

  • 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.

Cache

  • 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.

Thanks

Reference

Appendix

Format of Riscv32 Assembler

InstructionFormat
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 https://github.com/riscv/riscv-gnu-toolchain

or alternatively:

$ git clone https://github.com/riscv/riscv-gnu-toolchain
$ 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 https://jbox.sjtu.edu.cn/link/view/8971e3226df74d35b2dcbe588397958f(Provided 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 instr.om
    riscv32-unknown-elf-objcopy -O binary instr.om instr.bin
    ../Tools/Bin_to_Text instr.bin > instr.data
    cat instr.data
    rm instr.o instr.om instr.bin    
elif [ $# -eq 1 ]; then
    riscv32-unknown-elf-as -o $1.o -march=rv32i $1.s 
    riscv32-unknown-elf-ld $1.o -o $1.om
    riscv32-unknown-elf-objcopy -O binary $1.om $1.bin
    ../Tools/Bin_to_Text $1.bin > instr.data
    cat instr.data
    rm $1.o $1.om $1.bin
else
    echo "Please input less than 2 paraments!"
fi

which was included in get_instr.sh. 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 instr.data 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.

#include<cstring>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<fstream>
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();
    ifile.seekg(0,ios::beg);
    int N = (tail-head)/4;
    while (N--)
    {
        ll num = 0; int now = 0;
        for (int k = 0;k < 4;++k)
        {
            char c; ifile.read((char *)&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
        
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].