-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathID.cpp
336 lines (296 loc) · 5.58 KB
/
ID.cpp
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "ID.h"
#include <iostream>
ID::ID(int instruction, int pcplus1, int store, int* regs, int** branchAddr, stack<int>& S,
int IDEXmemread, int IDEX_rt, int EXMEMmemread,int EXMEMrt)
{
this->instruction = instruction;
this->regs = regs;
this->pcplus1 = pcplus1;
this->store = store;
this->branchAddr = branchAddr;
this->S = S;
this->IDEXmemread = IDEXmemread;
this->IDEXrt = IDEX_rt;
this->EXMEMmemread = EXMEMmemread;
this->EXMEMrt = EXMEMrt;
flush = false;
stall1 = false;
stall2 = false;
setRt();
setRd();
setRs();
readBoth();
se();
setControls();
checkBranch(); // change pcsrc
checkPrediction(); //check prediction and update
checkJump();
checkStall();
//cout << "This is now in ID : " << this->instruction << endl;
S = this->S;
}
void ID::checkStall()
{
if (IDEXmemread)
{
if (controls != 2056)
{
if (IDEXrt == rt || IDEXrt == rs)
{
stall2 = true;
controls = 0;
}
}
else
if (IDEXrt == rs)
{
stall2 = true;
controls = 0;
}
}
if (EXMEMmemread)
{
if (controls != 2056)
{
if (EXMEMrt == rt || EXMEMrt == rs)
{
stall1 = true;
controls = 0;
}
}
else
if (EXMEMrt == rs)
{
stall1 = true;
controls = 0;
}
}
}
bool ID::getStall1()
{
return stall1;
}
bool ID::getStall2()
{
return stall2;
}
void ID::checkPrediction()
{
if (store != -1) // branch instruction
{
int state = branchAddr[store][2];
if (pcsrc && (state == 0 || state == 1)) //true is taken and predicted is taken
{
address = -1; flush = false;
}
else if (!pcsrc && (state != 0 && state != 1)) //true is non taken and predicted is non taken
{
address = -1; flush = false;
}
else if (pcsrc && (state != 0 && state != 1)) // true is taken and predicted is non taken
{
address = branchAddr[store][1]; flush = true;
}
else if (!pcsrc && (state == 0 || state == 1)) //true is non taken and predicted is taken
{
address = branchAddr[store][0] + 1; flush = true;
}
}
else
address = -1;
update();
}
bool ID::getFlush()
{
return flush;
}
void ID::update()
{
if (store != -1)
{
int move = (pcsrc == 1);
int state = branchAddr[store][2];
switch (state) //FSM
{
case 0: {if (move) state = state; else state = state + 1; break; }
case 1: {if (move) state = state - 1; else state = state + 1; break; }
case 2: {if (move) state = state - 1; else state = state + 1; break; }
case 3: {if (move) state = state - 1; else state = state; break; }
}
branchAddr[store][2] = state;
}
}
int ID::getAddress()
{
return address;
}
void ID::checkBranch()
{
int branch = controls >> 7 & 1;
pcsrc = Nequal & branch;
}
void ID::setRt()
{
int x = instruction >> 16;
rt = x & 31; //instr[20:16]
}
void ID::setRd()
{
int x = instruction >> 11;
rd = x & 31; //instr[15:11]
}
void ID::setRs()
{
int x = instruction >> 21;
rs = x & 31; //instr[15:11]
}
int ID::getSE()
{
return signextension;
}
int ID::get_regwrite()
{
return regwrite;
}
/*void ID::setPCplus1(int pcplus1)
{
this->pcplus1 = pcplus1;
}*/
void ID::readBoth()
{
int RD1 = regs[rs];
int RD2 = regs[rt];
if (RD1 <= RD2)
Nequal = true;
else
Nequal = false;
rd1 = RD1;
rd2 = RD2;
}
void ID::se()
{
int imm = instruction & 0xFFFF;
int sign = imm >> 14;
if (sign != 0)
{
int y = 0xFFFF0000;
signextension = y | imm;
}
else
{
int y = 0x00000000;
signextension = y | imm;
}
}
void ID::setControls()
{
// given array of 11 bits to control
// regDst, regwrite, aluctl 3 bits, branch, memwrite, jump, memtoreg, alusrc, jalC, jrC , halt
int func = instruction & 0x3F;
unsigned int opcode = instruction >> 26;
if (opcode == 0)
{
switch (func)
{
case 32: controls = 6144; //add
break;
case 38: controls = 7168; //xor
break;
case 42: controls = 7936; //slt
break;
case 8: controls = 1538; //jr
}
}
else
if (opcode == 2) controls = 32; //jr
else if (opcode == 3) controls = 2084; //jal
else if (opcode == 8) controls = 2056; //addi
else if (opcode == 35) controls = 2072; //lw
else if (opcode == 43) controls = 72; //sw
else if (opcode == 7) controls = 640; //bne
else if (opcode == 63) controls = 1; //HALT (stop simulation)
regwrite = (controls >> 11) & 1;
}
int ID::getRt()
{
return rt;
}
int ID::getRd()
{
return rd;
}
int ID::getRs()
{
return rs;
}
int ID::getControls()
{
return controls;
}
int ID::getRD1()
{
return rd1;
}
int ID::getRD2()
{
return rd2;
}
int ID::getMTR() // get memtoreg
{
return ((controls >> 4) & 1);
}
int ID::getPCsrc()
{
return pcsrc;
}
void ID::checkJump()
{
unsigned int opc = instruction >> 26;
unsigned int func = instruction & 63;
//unsigned int jC = instruction >> 5 & 1;
if (opc == 2) //j
{
jump = true;
flush = true;
address = (instruction & 67108863) - 1;
}
else
{
if (opc == 14) // jump procedure
{
jump = true;
flush = true;
int x = (instruction & 67108863);
address = x -1;
S.push(pcplus1);
}
else
{
if (opc == 15) // return procedure
{
//jump = true;
if (S.size() > 0)
{
address = S.top();
S.pop();
flush = true;
}
}
}
if (opc == 3)
{
int dest;
dest = 31;
int x = (instruction & 67108863);
address = x - 1;
regs[dest] = pcplus1;
flush = true;
}
else
if (opc == 0 && (func == 8))
{
address = regs[rs];
flush = true;
}
}
}