-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThirtyTwoBit.cpp
More file actions
231 lines (173 loc) · 5.76 KB
/
ThirtyTwoBit.cpp
File metadata and controls
231 lines (173 loc) · 5.76 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <iostream>
#include <fstream>
#include "stdlib.h"
#include <iomanip>
#include <cstring>
#include <string>
#include <bits/stdc++.h>
#include"ThirtyTwoBit.h"
using namespace std;
string SFormat(unsigned int InstWord)
{ string x="x";
string sb="sb";
string sh="sh";
string sw="sw";
string toprint,register2,offset,register1;
unsigned int rs2,rs1,Imm1,Imm2,FinalImm,funct3;
signed int result;
rs2= (InstWord>>20) &0x0000001F; //saving value of rs2
register2 =x+to_string(rs2);//concatinate x with rs2 value
rs1=(InstWord>>15)&0x0000001F;//saving value of rs1
register1=x+to_string(rs1);//concatinate x with rs1
Imm1= (InstWord>>7) & 0x0000001F; //first 5 bits of offset
Imm2= (InstWord>>25) & 0x0000007F; // bits 6 to 12 of offset
FinalImm= (Imm2<<5) | Imm1 ; //get all 12 bits together
signed int check=(InstWord>>31); //most significant bit
if(check) // if immediate is negative
{
result=FinalImm ^0x00000FFF;
result=result+0x1;
result=result*(-1);
offset=to_string(result);
}
else {
offset=to_string(FinalImm);
}
funct3= InstWord & 0x00007000; //specific instruction
if (funct3==0x00000000)
toprint= sb +"\t\t\t"+ register2 +","+offset+"("+register1+")";
else if (funct3==0x00001000)
toprint= sh+"\t\t\t"+ register2 +","+offset+"("+register1+")";
else if (funct3==0x00002000)
toprint= sw+"\t\t\t"+ register2+ ","+offset+"("+register1+")";
else
toprint="Instruction not found!";
return toprint;
}
string BFormat( unsigned int InstWord,signed int PC)
{
string x="x";
string toprint;
string x1;
string x2;
unsigned int rs1,rs2,eleventh,oneto4,fiveto10,twelvth,funct3;
signed int finalImm,result;
rs1=(InstWord>>15)& 0x0000001F;//number of rs1
x1=x+to_string(rs1); // saving it in form of xnumber as string
rs2=(InstWord>>20) & 0x0000001F;//number of rs2
x2=x+to_string(rs2);//saving it in form of xnumber
eleventh= (InstWord>>7) & 0x00000001; //eleventh bit in unsigned
oneto4=(InstWord>>8) & 0x0000000F; //storing from one to fourth bit
fiveto10=(InstWord>>25) & 0x0000003F; //storing from fifth to tenth bit
twelvth=(InstWord>>31) & 0x00000001; //storing twelvth bit
finalImm= oneto4 |(fiveto10<<4) |(eleventh<<10) | (twelvth<<11);
if (twelvth)//handling if displacement is minus
{
result=finalImm^0x00000FFF;
result=result+0x1;
result=result*(-1);
result=result*2+PC;
}
else
{
result=finalImm*2+PC;
}
stringstream ss;
ss << hex <<result; //saving immediate as hexadecimal so that compiler does not change it to decimal
string res = ss.str();
funct3=(InstWord>>12) & 0x00000007;// to identify the instruction
if (funct3==0)
toprint="beq\t\t\t" +x1+","+x2+","+"0x"+res;
else if (funct3==1)
toprint="bne\t\t\t"+x1+","+x2+","+"0x"+res;
else if (funct3==4)
toprint="blt\t\t\t"+x1+","+x2+","+"0x"+res;
else if (funct3==5)
toprint="bge\t\t\t"+x1+","+x2+","+"0x"+res;
else if (funct3==6)
toprint="bltu\t\t"+x1+","+x2+","+"0x"+res;
else if (funct3==7)
toprint="bgeu\t\t"+x1+","+x2+","+"0x"+res;
else
toprint="Instruction not found!";
return toprint;
}
string UFormat (unsigned int InstWord)
{
string x="x";
string lui="lui";
string auipc="auipc";
string toprint;
unsigned int rd,Imm,spec;
rd= (InstWord>>7)& 0x0000001F;//getting destination register number
x=x+to_string(rd); //writing it in form of xnumber
Imm= (InstWord>>12)& 0x000FFFFF; // getting immediate
stringstream ss;
ss << hex << Imm; //saving immediate as hexadecimal so that compiler does not change it to decimal
string res = ss.str();
spec= (InstWord>>5)& 0x00000003; // saving bits that specify the instruction
if(spec==0)
toprint= auipc+"\t\t\t"+x+",0x"+res; //saving the instruction to be printed in string
else if(spec==1)
toprint= lui+"\t\t\t"+x+",0x"+res;
else
toprint="Instruction not found!";
return toprint;
}
/*void PrintPC(int PC)
{
output.open("output",ios::app)
stringstream ss;
ss << hex << PC;
string res = ss.str();
output << "0x" << res << endl;
output.close();
<<<<<<< HEAD
}
*/
string JFormat(unsigned int InstWord, unsigned int PC)
{
string x="x";
string toprint;
string AddressString;
unsigned int rd, Imm1to10,Imm11,Imm12to19,Imm20;
signed int FinalImm,address;
rd=(InstWord>>7) & 0x0000001F; //saving number of destination register
x=x+to_string(rd);
Imm1to10=(InstWord>>21) & 0x000003FF; //bits 1 to 10 of immediate
Imm11=(InstWord>>20) & 0x00000001; //bit 11
Imm12to19=(InstWord>>12) & 0x000000FF; //bit 12 to 19
Imm20=(InstWord>>31) & 0x00000001; //bit 20
FinalImm=(Imm1to10 | (Imm11<<10)| (Imm12to19<<11)|(Imm20<<19)); //displaying all bits
signed int check=(InstWord>>31)& 0x00000001;
if(check)// if immediate is negative
{
signed int result=FinalImm^0x000FFFFF;
FinalImm=result+0x1;
FinalImm=FinalImm*(-1);
address=FinalImm*2+PC;
}
else
address=FinalImm*2+PC;
//transform to hexadecimal
stringstream ss;
ss << hex << address;
string res = ss.str();
toprint="jal\t\t\t" + x + "," +"0x"+ res; //saving the instruction to be printed in a string
return toprint;
/*output.open("output",ios::app)
output<<"JAL"<<" ";
output<<RA;
output.close();
*/
}
/*void Print(unsigned int PC,string toprint )
{
fstream output;
output.openfile("output",ios::app);
stringstream ss;
ss << hex << PC;
string res = ss.str();
output<<"0x"<<res<<" "<<toprint;
output.close();
}*/