-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path32_bit.cpp
More file actions
193 lines (175 loc) · 5.3 KB
/
32_bit.cpp
File metadata and controls
193 lines (175 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include "stdlib.h"
#include <iomanip>
#include <string>
#include <cstring>
#include "ThirtyTwoBit.h"
using namespace std;
#include <cstdint>
#include <type_traits>
string ecall_func (){ //This function returns the ecall
return "ecall";
}
string r_type (unsigned int instWord){ //This function returns the instruction format for the R type
string x,b,c,d;
unsigned int rd = (instWord >> 7) & 0x0000001F; //Extract the fields - registers and functions using shifting to the right and anding
unsigned int funct3 = (instWord >> 12) & 0x00000007;
unsigned int rs1 = (instWord >> 15) & 0x0000001F;
unsigned int rs2 = (instWord >> 20) & 0x0000001F;
unsigned int funct7 = (instWord >> 25) & 0x0000007F;
if (funct3 == 0){ //Print the instruction type based on the functions fields
if (funct7 == 0){
x = "add";
}
else {
x = "sub";
}
}
else if (funct3 == 1){
x = "sll";
}
else if (funct3 == 2){
x = "slt";
}
else if (funct3 == 3){
x = "sltu";
}
else if (funct3 == 4){
x = "xor";
}
else if (funct3 == 5){
if (funct7 == 0){
x = "srl";
}
else {
x = "sra";
}
}
else if (funct3 == 6){
x = "or";
}
else if (funct3 == 7){
x = "and";
}
else {
return "Instruction not found"; //If no function is found then return this statement
}
b = to_string(rd);
c = to_string(rs1);
d = to_string(rs2);
x += " \t\tx" + b + ",x" + c + ",x" + d; //Concatinate the fields into one string and return it
return x;
}
string i_type (unsigned int instWord){ //This function returns the instruction format for the I type
string x,b,c,d;
unsigned int rd = (instWord >> 7) & 0x0000001F; //Extract the fields - registers, immediates and functions using shifting to the right and anding
unsigned int funct3 = (instWord >> 12) & 0x00000007;
unsigned int rs1 = (instWord >> 15) & 0x0000001F;
signed int imm = (instWord >> 20) & 0x000007FF;
signed int check = (instWord >> 31) & 0x1;
unsigned int imm3 = (instWord >> 20) & 0x00000FFF;
unsigned int imm2 = (instWord >> 20) & 0x0000001F;
unsigned int funct7 = (instWord >> 25) & 0x0000007F;
if (check){ //Check if the immediate is negativeto get its 2's complement and multiply by -1
signed int r = imm ^ 0x000007FF;
imm = r + 0x1;
imm = imm * (-1);
}
if (funct3 == 0){ //Print the instruction type based on the functions fields
x = "addi";
}
else if (funct3 == 2){
x = "slti";
}
else if (funct3 == 3){
x = "sltiu";
}
else if (funct3 == 4){
x = "xori";
}
else if (funct3 == 6){
x = "ori";
}
else if (funct3 == 7){
x = "andi";
}
else if (funct3 == 1){
x = "slli";
imm = imm2;
}
else if (funct3 == 5){
if (funct7 == 0){
x = "srli";
imm = imm2;
}
else {
x = "srai";
imm = imm2;
}
}
else {
return "Instruction not found"; //If function not found return this statement
}
b = to_string(rd);
c = to_string(rs1);
d = to_string(imm);
x += " \t\tx" + b + ",x" + c + "," + d; //Concatinate the fields into one string and return it
return x;
}
string i_type_load (unsigned int instWord){ //This function returns the instruction format for the I_load type (different opcode and format than previous)
string x,b,c,d;
unsigned int rd = (instWord >> 7) & 0x0000001F; //Extract the fields - registers, immediates and functions using shifting to the right and anding
unsigned int funct3 = (instWord >> 12) & 0x00000007;
unsigned int rs1 = (instWord >> 15) & 0x0000001F;
signed int imm = (instWord >> 20) & 0x000007FF;
signed int check = (instWord >> 31) & 0x1;
if (check){ //Check if the immediate is negativeto get its 2's complement and multiply by -1
signed int r = imm ^ 0x000007FF;
imm = r + 0x1;
imm = imm * (-1);
}
if (funct3 == 0){ //Print the instruction type based on the functions fields
x = "lb";
}
else if (funct3 == 1){
x = "lh";
}
else if (funct3 == 2){
x = "lw";
}
else if (funct3 == 4){
x = "lbu";
}
else if (funct3 == 5){
x = "lhu";
}
else {
return "Instruction not found"; //If function not found return this statement
}
b = to_string(rd);
c = to_string(rs1);
d = to_string(imm);
x += "\t\t\tx" + b + "," + d + "(x" + c + ")"; //Concatinate the fields into one string and return it
return x;
}
string jalr_type (unsigned int instWord){ //Just like the I type but it has a different opcode
string x,b,c,d;
unsigned int rd = (instWord >> 7) & 0x0000001F;
unsigned int funct3 = (instWord >> 12) & 0x00000007;
unsigned int rs1 = (instWord >> 15) & 0x0000001F;
signed int imm = (instWord >> 20) & 0x000007FF;
signed int check = (instWord >> 31) & 0x1;
if (check){
signed int r = imm ^ 0x000007FF;
imm = r + 0x1;
imm = imm * (-1);
}
x = "jalr";
b = to_string(rd);
c = to_string(rs1);
d = to_string(imm);
x += "\t\tx" + b + ",x" + c + "," + d;
return x;
}