Skip to content

Commit 8b9df22

Browse files
authored
RISC-V CPU files
1 parent a05ead9 commit 8b9df22

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+67296
-0
lines changed

rv32i/README.md

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Multicycle RISC-V CPU - Core Design
2+
3+
The implementation of the RISC-V CPU rv32i (integer subset of RISC-V spec) system with a multicycle core.
4+
5+
Designed by Sparsh Gupta as the final project for Computer Architecture class at Olin College of Engineering - Fall 2023.
6+
7+
## Instructions Supported
8+
9+
### R-types
10+
- [x] add
11+
- [x] sub
12+
- [x] xor
13+
- [x] or
14+
- [x] and
15+
- [x] sll
16+
- [x] srl
17+
- [x] sra
18+
- [x] slt
19+
- [x] sltu
20+
21+
### I-types
22+
- [x] addi
23+
- [x] xori
24+
- [x] ori
25+
- [x] andi
26+
- [x] slli
27+
- [x] srli
28+
- [x] srai
29+
- [x] slti
30+
- [x] sltiu
31+
32+
### Memory-Types (Loads/Stores)
33+
- [x] lw
34+
- [x] sw
35+
- [x] *lb*
36+
- [x] *lh*
37+
- [x] *lbu*
38+
- [x] *lhu*
39+
- [x] *sb*
40+
- [x] *sh*
41+
42+
### B-types (Branches)
43+
- [x] beq
44+
- [x] bne
45+
- [x] *blt*
46+
- [x] *bge*
47+
- [x] *bltu*
48+
- [x] *bgeu*
49+
50+
### J-types (Jumps)
51+
- [x] jal
52+
- [x] jalr (technically an i-type)
53+
54+
### U-types (Upper immediates)
55+
- [x] *lui*
56+
- [x] *auipc*
57+
58+
### Pseudo-Instruction
59+
- [x] *li*
60+
61+
## Setup
62+
63+
**Please skip if you already have a setup_cafe() env configured on your machine.**
64+
65+
You will need a machine/VM using Linux to simulate this CPU.
66+
67+
Make sure you have OSS-cad-suite-linux-x64 installed. You can obtain it here: https://www.opensourceagenda.com/projects/oss-cad-suite-build#Installation
68+
69+
Obtaining Xilinx version 2023.1 and AMD Vivado would also be helpful. (https://www.xilinx.com/support/download.html)
70+
71+
If you have already completed the above steps, then setting up an environment for these tools is recommended. If you already have an env, make sure it is enabled when simulating the CPU and if you do not, you can set up an env by editing your `~/.bashrc` file.
72+
73+
```bash
74+
function setup_cafe(){
75+
# Enable open source tools.
76+
source ~/embedded/oss-cad-suite/environment
77+
78+
# Enable Xilinx.
79+
export XILINX_INSTALL_PATH="${HOME}/embedded/xilinx/"
80+
VERSION="2023.1"
81+
export VIVADO_PATH=${XILINX_INSTALL_PATH}/Vivado/${VERSION}/
82+
export PATH="${VIVADO_PATH}/bin:$PATH"
83+
# Setup variables for synthesis. You may need to change this based on your FPGA board.
84+
export FPGA_PART=xc7a35tcpg236-1
85+
export PS1="$PS1"
86+
}
87+
88+
# Uncomment this if you aren't using this Linux install for any other projects.
89+
# setup_cafe;
90+
```
91+
92+
Make sure you are in this `submission` directory, then run `./rv32i/tools/check_install` to check whether you have all the necessary dependencies installed and ready.
93+
94+
Once you have all this setup, configure your pwd to `~/submission/rv32i`. Next, just enter the command `setup_cafe()` or start your personal configured environment.
95+
96+
## Simulation
97+
98+
Please change your pwd to `~/submission/rv32i/core/` to access the **Makefile** and be in the same directory level (also make sure you have setup_cafe() enabled). The Makefile is capable of testing instructions and generating waveform simulations for the CPU.
99+
100+
The first thing to be done is to generate the rv32i simulator, which can be done using below. (the CPU won't work if you skip this step, also if the simulator has already been generated, then this step will just make sure it is up to date)
101+
```
102+
make rv32_simulator
103+
```
104+
105+
The rv32i assembly scripts are present in the directory `~/submission/rv32i/asm/` to test out different types of instructions.
106+
107+
For example, to test the `btypes` instructions, simply run the below. This will output the register file state after the operations and will also let you generate waveform simulations for it. The sample register file is also included in the asm (.s) files.
108+
```
109+
make test_rv32_btypes
110+
```
111+
You can test out other instruction types as well once you take a look at the `Makefile`.
112+
113+
To generate waveforms for simulating the CPU, make sure you have `gtkwave` installed. Then execute the command below.
114+
```
115+
make waves_rv32
116+
```
117+
118+
## General Info
119+
120+
**Components**: The required components to simulate the CPU are all present within the `~/submission/rv32i/core/` directory.
121+
122+
**Filter Files**: The `~/submission/rv32i/filter_files/` directory houses some `.txt` filter files used in translating waves generated by gtkwave and makes it easier to track signals, etc.
123+
124+
**Tools**: Some tools that are used in this project, such as an `assembler` to convert assembly (`.s`) files into CPU-executable (`.memh`), the tool to check install, etc. are present in `~/submission/rv32i/tools/`.
125+
126+
## CPU Schematic
127+
128+
Note: The schematic does not include the control unit and FSM logic and is just a reference I used for wiring the components.
129+
130+
<img width="1000" alt="rv32i_schematic" src="rv32i_schematic.png">
131+
132+
## References
133+
134+
References used in designing this CPU:
135+
136+
- [Official RISC-V Manual](https://riscv.org/wp-content/uploads/2019/12/riscv-spec-20191213.pdf)
137+
- [Digital Design & Computer Architecture RISC-V Edition - Sarah L. Harris and David Harris](https://www.sciencedirect.com/book/9780128200643/digital-design-and-computer-architecture)

rv32i/asm/btypes.memh

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
00a00093 // PC=0x0 line=1: addi x1, x0, 10 # x01 = 10
2+
ff600113 // PC=0x4 line=2: addi x2, x0, -10 # x02 = -10
3+
00500193 // PC=0x8 line=3: addi x3, x0, 5 # x03 = 5
4+
00000463 // PC=0xc line=7: beq x0, x0, equal # testing equal
5+
001f8f93 // PC=0x10 line=8: addi x31, x31, 1
6+
00100293 // PC=0x14 line=10: addi x5, x0, 1
7+
00009463 // PC=0x18 line=12: bne x1, x0, not_equal # testing not equal
8+
001f8f93 // PC=0x1c line=13: addi x31, x31, 1
9+
00200313 // PC=0x20 line=15: addi x6, x0, 2
10+
0020c463 // PC=0x24 line=17: blt x1, x2, less_than # testing less than
11+
001f8f93 // PC=0x28 line=18: addi x31, x31, 1
12+
00300393 // PC=0x2c line=20: addi x7, x0, 3
13+
0030d463 // PC=0x30 line=22: bge x1, x3, greater_than # testing greater than
14+
001f8f93 // PC=0x34 line=23: addi x31, x31, 1
15+
00400413 // PC=0x38 line=25: addi x8, x0, 4
16+
0011e463 // PC=0x3c line=27: bltu x3, x1, less_than_unsigned # testing less than less_than_unsigned
17+
001f8f93 // PC=0x40 line=28: addi x31, x31, 1
18+
00500493 // PC=0x44 line=30: addi x9, x0, 5
19+
0030f463 // PC=0x48 line=32: bgeu x1, x3, greater_than_unsigned # testing greater than unsigned
20+
001f8f93 // PC=0x4c line=33: addi x31, x31, 1
21+
00600513 // PC=0x50 line=35: addi x10, x0, 6
22+
00000063 // PC=0x54 line=37: infinite_loop: beq x0, x0, infinite_loop

rv32i/asm/btypes.s

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
addi x1, x0, 10 # x01 = 10
2+
addi x2, x0, -10 # x02 = -10
3+
addi x3, x0, 5 # x03 = 5
4+
5+
# debugging register is x31
6+
7+
beq x0, x0, equal # testing equal
8+
addi x31, x31, 1
9+
equal:
10+
addi x5, x0, 1
11+
12+
bne x1, x0, not_equal # testing not equal
13+
addi x31, x31, 1
14+
not_equal:
15+
addi x6, x0, 2
16+
17+
blt x1, x2, less_than # testing less than
18+
addi x31, x31, 1
19+
less_than:
20+
addi x7, x0, 3
21+
22+
bge x1, x3, greater_than # testing greater than
23+
addi x31, x31, 1
24+
greater_than:
25+
addi x8, x0, 4
26+
27+
bltu x3, x1, less_than_unsigned # testing less than less_than_unsigned
28+
addi x31, x31, 1
29+
less_than_unsigned:
30+
addi x9, x0, 5
31+
32+
bgeu x1, x3, greater_than_unsigned # testing greater than unsigned
33+
addi x31, x31, 1
34+
greater_than_unsigned:
35+
addi x10, x0, 6
36+
37+
infinite_loop: beq x0, x0, infinite_loop
38+
39+
|--------------------|
40+
| Register File State|
41+
|--------------------|
42+
| x00, zero = 0 |
43+
| x01, ra = 10 |
44+
| x02, sp = -10 |
45+
| x03, gp = 5 |
46+
| x04, tp = x |
47+
| x05, t0 = 1 |
48+
| x06, t1 = 2 |
49+
| x07, t2 = 3 |
50+
| x08, s0 = 4 |
51+
| x09, s1 = 5 |
52+
| x10, a0 = 6 |
53+
| x11-31 = x |
54+
|--------------------|

rv32i/asm/irtypes.memh

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
fff00093 // PC=0x0 line=1: addi x1, x0, -1 # x01 = -1
2+
02300113 // PC=0x4 line=2: addi x2, x0, 35 # x02 = 35
3+
04414193 // PC=0x8 line=3: xori x3, x2, 68 # x03 = 103
4+
01116213 // PC=0xc line=4: ori x4, x2, 17 # x04 = 51
5+
01117293 // PC=0x10 line=5: andi x5, x2, 17 # x05 = 1
6+
00809313 // PC=0x14 line=6: slli x6, x1, 8 # x06 = -256
7+
0021d393 // PC=0x18 line=7: srli x7, x3, 2 # x07 = 25
8+
40635413 // PC=0x1c line=8: srai x8, x6, 6 # x08 = -4
9+
0230a493 // PC=0x20 line=9: slti x9, x1, 35 # x09 = 1
10+
0230b513 // PC=0x24 line=10: sltiu x10, x1, 35 # x10 = 0
11+
001085b3 // PC=0x28 line=11: add x11, x1, x1 # x11 = -2
12+
40108633 // PC=0x2c line=12: sub x12, x1, x1 # x12 = 0
13+
00c0c6b3 // PC=0x30 line=13: xor x13, x1, x12 # x13 = -1
14+
00316733 // PC=0x34 line=14: or x14, x2, x3 # x14 = 103
15+
003177b3 // PC=0x38 line=15: and x15, x2, x3 # x15 = 35
16+
00400813 // PC=0x3c line=16: addi x16, x0, 4 # x16 = 4
17+
010118b3 // PC=0x40 line=17: sll x17, x2, x16 # x17 = 560
18+
0101d933 // PC=0x44 line=18: srl x18, x3, x16 # x18 = 6
19+
410359b3 // PC=0x48 line=19: sra x19, x6, x16 # x19 = -16
20+
0020aa33 // PC=0x4c line=20: slt x20, x1, x2 # x20 = 1
21+
0020bab3 // PC=0x50 line=21: sltu x21, x1, x2 # x20 = 0

rv32i/asm/irtypes.s

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
addi x1, x0, -1 # x01 = -1
2+
addi x2, x0, 35 # x02 = 35
3+
xori x3, x2, 68 # x03 = 103
4+
ori x4, x2, 17 # x04 = 51
5+
andi x5, x2, 17 # x05 = 1
6+
slli x6, x1, 8 # x06 = -256
7+
srli x7, x3, 2 # x07 = 25
8+
srai x8, x6, 6 # x08 = -4
9+
slti x9, x1, 35 # x09 = 1
10+
sltiu x10, x1, 35 # x10 = 0
11+
add x11, x1, x1 # x11 = -2
12+
sub x12, x1, x1 # x12 = 0
13+
xor x13, x1, x12 # x13 = -1
14+
or x14, x2, x3 # x14 = 103
15+
and x15, x2, x3 # x15 = 35
16+
addi x16, x0, 4 # x16 = 4
17+
sll x17, x2, x16 # x17 = 560
18+
srl x18, x3, x16 # x18 = 6
19+
sra x19, x6, x16 # x19 = -16
20+
slt x20, x1, x2 # x20 = 1
21+
sltu x21, x1, x2 # x20 = 0
22+
|---------------------------------------|
23+
| Register File State |
24+
|---------------------------------------|
25+
| x00, zero = 0x00000000 ( 0)|
26+
| x01, ra = 0xffffffff ( -1)|
27+
| x02, sp = 0x00000023 ( 35)|
28+
| x03, gp = 0x00000067 ( 103)|
29+
| x04, tp = 0x00000033 ( 51)|
30+
| x05, t0 = 0x00000001 ( 1)|
31+
| x06, t1 = 0xffffff00 ( -256)|
32+
| x07, t2 = 0x00000019 ( 25)|
33+
| x08, s0 = 0xfffffffc ( -4)|
34+
| x09, s1 = 0x00000001 ( 1)|
35+
| x10, a0 = 0x00000000 ( 0)|
36+
| x11, a1 = 0xfffffffe ( -2)|
37+
| x12, a2 = 0x00000000 ( 0)|
38+
| x13, a3 = 0xffffffff ( -1)|
39+
| x14, a4 = 0x00000067 ( 103)|
40+
| x15, a5 = 0x00000023 ( 35)|
41+
| x16, a6 = 0x00000004 ( 4)|
42+
| x17, a7 = 0x00000230 ( 560)|
43+
| x18, s2 = 0x00000006 ( 6)|
44+
| x19, s3 = 0xfffffff0 ( -16)|
45+
| x20, s4 = 0x00000001 ( 1)|
46+
| x21, s5 = 0x00000000 ( 0)|
47+
| x22, s6 = 0xxxxxxxxx ( x)|
48+
| x23, s7 = 0xxxxxxxxx ( x)|
49+
| x24, s8 = 0xxxxxxxxx ( x)|
50+
| x25, s9 = 0xxxxxxxxx ( x)|
51+
| x26, s10 = 0xxxxxxxxx ( x)|
52+
| x27, s11 = 0xxxxxxxxx ( x)|
53+
| x28, t3 = 0xxxxxxxxx ( x)|
54+
| x29, t4 = 0xxxxxxxxx ( x)|
55+
| x30, t5 = 0xxxxxxxxx ( x)|
56+
| x31, t6 = 0xxxxxxxxx ( x)|
57+
|---------------------------------------|

rv32i/asm/itypes.memh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
01100093 // PC=0x0 line=1: addi x1, x0, 17
2+
ff000513 // PC=0x4 line=2: addi x10, zero, -16
3+
00409113 // PC=0x8 line=3: slli x2, x1, 4
4+
3e812193 // PC=0xc line=4: slti x3, x2, 1000
5+
3e813213 // PC=0x10 line=5: sltiu x4, x2, 1000
6+
1df14293 // PC=0x14 line=6: xori x5, x2, 479
7+
00455313 // PC=0x18 line=7: srli x6, x10, 4
8+
40455393 // PC=0x1c line=8: srai x7, x10, 4
9+
7fc16413 // PC=0x20 line=9: ori x8, x2, 2044
10+
06447493 // PC=0x24 line=10: andi x9, x8, 100

rv32i/asm/itypes.s

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
addi x1, x0, 17
2+
addi x10, zero, -16
3+
slli x2, x1, 4
4+
slti x3, x2, 1000
5+
sltiu x4, x2, 1000
6+
xori x5, x2, 479
7+
srli x6, x10, 4
8+
srai x7, x10, 4
9+
ori x8, x2, 2044
10+
andi x9, x8, 100
11+
12+
# |---------------------------------------|
13+
# | Register File State |
14+
# |---------------------------------------|
15+
# | x00, zero = 0x00000000 ( 0)|
16+
# | x01, ra = 0x00000011 ( 17)|
17+
# | x02, sp = 0x00000110 ( 272)|
18+
# | x03, gp = 0x00000001 ( 1)|
19+
# | x04, tp = 0x00000001 ( 1)|
20+
# | x05, t0 = 0x000000cf ( 207)|
21+
# | x06, t1 = 0x0fffffff ( 268435455)|
22+
# | x07, t2 = 0xffffffff ( -1)|
23+
# | x08, s0 = 0x000007fc ( 2044)|
24+
# | x09, s1 = 0x00000064 ( 100)|
25+
# | x10, a0 = 0xfffffff0 ( -16)|
26+
# | x11, a1 = 0xxxxxxxxx ( x)|
27+
# | x12, a2 = 0xxxxxxxxx ( x)|
28+
# | x13, a3 = 0xxxxxxxxx ( x)|
29+
# | x14, a4 = 0xxxxxxxxx ( x)|
30+
# | x15, a5 = 0xxxxxxxxx ( x)|
31+
# | x16, a6 = 0xxxxxxxxx ( x)|
32+
# | x17, a7 = 0xxxxxxxxx ( x)|
33+
# | x18, s2 = 0xxxxxxxxx ( x)|
34+
# | x19, s3 = 0xxxxxxxxx ( x)|
35+
# | x20, s4 = 0xxxxxxxxx ( x)|
36+
# | x21, s5 = 0xxxxxxxxx ( x)|
37+
# | x22, s6 = 0xxxxxxxxx ( x)|
38+
# | x23, s7 = 0xxxxxxxxx ( x)|
39+
# | x24, s8 = 0xxxxxxxxx ( x)|
40+
# | x25, s9 = 0xxxxxxxxx ( x)|
41+
# | x26, s10 = 0xxxxxxxxx ( x)|
42+
# | x27, s11 = 0xxxxxxxxx ( x)|
43+
# | x28, t3 = 0xxxxxxxxx ( x)|
44+
# | x29, t4 = 0xxxxxxxxx ( x)|
45+
# | x30, t5 = 0xxxxxxxxx ( x)|
46+
# | x31, t6 = 0xxxxxxxxx ( x)|
47+
# |---------------------------------------|

rv32i/asm/jtypes.memh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
00900213 // PC=0x0 line=3: addi x4, zero, 9 # x04 = 9
2+
00100093 // PC=0x4 line=5: LOOP_START: addi x1, zero, 1
3+
00200113 // PC=0x8 line=6: addi x2, zero, 2
4+
001001b3 // PC=0xc line=7: add x3, zero, x1
5+
00208a63 // PC=0x10 line=9: NOT_TAKEN: beq x1, x2, INVALID
6+
fff20213 // PC=0x14 line=10: addi x4, x4, -1 # i--
7+
00020463 // PC=0x18 line=11: beq x4, zero, END # i==0
8+
fe9ff06f // PC=0x1c line=16: jal zero, LOOP_START # go back to LOOP_START.
9+
00308063 // PC=0x20 line=18: END: beq x1, x3, END
10+
00000013 // PC=0x24 line=20: INVALID: nop

0 commit comments

Comments
 (0)