-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathissue.cpp
332 lines (320 loc) · 11.7 KB
/
issue.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
#include "issue.h"
Issue::Issue(
deque<Instruction> & dInstructionQueue,
vector<RSStatus> & rsUnitInt,
vector<RSStatus> & rsUnitLoad,
vector<RSStatus> & rsUnitStore,
vector<RSStatus> & rsUnitFpAdd,
vector<RSStatus> & rsUnitFpMult,
vector<RSStatus> & rsUnitFpDiv,
vector<RSStatus> & rsUnitBu,
deque<ROBStatus> & rob,
const int nw,
const int nr,
const bool debugMode
) :
dInstructionQueue(dInstructionQueue),
rsUnitInt(rsUnitInt),
rsUnitLoad(rsUnitLoad),
rsUnitStore(rsUnitStore),
rsUnitFpAdd(rsUnitFpAdd),
rsUnitFpMult(rsUnitFpMult),
rsUnitFpDiv(rsUnitFpDiv),
rsUnitBu(rsUnitBu),
rob(rob),
nw(nw),
nr(nr),
debugMode(debugMode)
{
this->stageType = StageType::ISSUE;
if (debugMode) {
printStageType();
cout << "\nNW in Issue stage = " << nw;
cout << "\nNR in Issue stage = " << nr;
cout << "\n";
}
};
bool Issue::insertInstructionInReservationStation(
vector<RSStatus> & inpReservationStation,
int inpReservationStationCount,
Instruction inpInstruction
) {
if (inpReservationStation.size() >= inpReservationStationCount) {
// reservation station is full,
// stall!
return false;
}
// create new rs entry, set to busy
RSStatus rsEntry;
rsEntry.instruction = inpInstruction;
rsEntry.busy = true;
inpReservationStation.push_back(rsEntry);
return true;
}
RSStatus Issue::getReservationStationForInstruction(
Instruction inpInstruction
) {
InstructionType instrType = inpInstruction.opcode;
switch(instrType) {
case InstructionType::FLD:
for(auto rs : rsUnitLoad) {
if (rs.instruction == inpInstruction) return rs;
}
case InstructionType::FSD:
for(auto rs : rsUnitStore) {
if (rs.instruction == inpInstruction) return rs;
}
case InstructionType::ADD:
for(auto rs : rsUnitInt) {
if (rs.instruction == inpInstruction) return rs;
}
case InstructionType::ADDI:
for(auto rs : this->rsUnitInt) {
if (rs.instruction == inpInstruction) return rs;
}
case InstructionType::SLT:
for(auto rs : this->rsUnitInt) {
if (rs.instruction == inpInstruction) return rs;
}
case InstructionType::FADD:
for(auto rs : this->rsUnitFpAdd) {
if (rs.instruction == inpInstruction) return rs;
}
case InstructionType::FSUB:
for(auto rs : this->rsUnitFpAdd) {
if (rs.instruction == inpInstruction) return rs;
}
case InstructionType::FMUL:
for(auto rs : this->rsUnitFpMult) {
if (rs.instruction == inpInstruction) return rs;
}
case InstructionType::FDIV:
for(auto rs : this->rsUnitFpDiv) {
if (rs.instruction == inpInstruction) return rs;
}
case InstructionType::BNE:
for(auto rs : this->rsUnitBu) {
if (rs.instruction == inpInstruction) return rs;
}
default:
throw invalid_argument(
"the provided InstructionType was not recognized. Check to ensure that input parameters are not null"
);
}
}
int Issue::getReservationStationIndex(
vector<RSStatus> inpRsUnit,
RSStatus inpReservationStation
) {
for (int i = 0; i < inpRsUnit.size(); i++) {
if (inpRsUnit.at(i).instruction == inpReservationStation.instruction)
return i;
}
throw invalid_argument(
"reservation station does not exist in the unit. Check to ensure that input parameters are not null"
);
}
ROBStatus & Issue::getROBStatusEntryForInstruction(
Instruction inpInstruction
) {
for(auto & entry : this->rob) {
if (entry.instruction == inpInstruction) return entry;
}
throw invalid_argument(
"rob status entry does not exist that contains the instruction. Check to ensure that input parameters are not null"
);
}
int Issue::getROBStatusEntryIndex(
deque<ROBStatus> inpRob,
ROBStatus inpEntry
) {
for (int i = 0; i < inpRob.size(); i++) {
if (inpRob.at(i).instruction == inpEntry.instruction)
return i;
}
throw invalid_argument(
"rob status entry does not exist in the buffer. Check to ensure that input parameters are not null"
);
}
vector<RSStatus> & Issue::getReservationStationUnitFromInstructionType(InstructionType inpInstrType) {
switch (inpInstrType) {
case InstructionType::FLD: return rsUnitLoad;
case InstructionType::FSD: return rsUnitStore;
case InstructionType::ADD: return rsUnitInt;
case InstructionType::ADDI: return rsUnitInt;
case InstructionType::SLT: return rsUnitInt;
case InstructionType::FADD: return rsUnitFpAdd;
case InstructionType::FSUB: return rsUnitFpAdd;
case InstructionType::FMUL: return rsUnitFpMult;
case InstructionType::FDIV: return rsUnitFpDiv;
case InstructionType::BNE: return rsUnitBu;
}
}
string Issue::generateROBStatusEntryName(Instruction inpInstr) {
int unitSize = getReservationStationUnitFromInstructionType(inpInstr.opcode).size();
return getRSNameFromInstructionType(inpInstr.opcode) + to_string(unitSize);
}
void Issue::updateReservationStation(Instruction inpInstr, RSStatus inpRs){
// replace rs in corresponding unit
vector<RSStatus> & rsu = getReservationStationUnitFromInstructionType(inpInstr.opcode);
int index = getReservationStationIndex(
rsu,
inpRs
);
rsu[index] = inpRs;
}
bool Issue::dispatch() {
cout << "\ni_dispatch called (nw=" << nw << " nr=" << nr << ")=\n";
for (int i = 0; i < nw; i++) {
if (dInstructionQueue.empty() || rob.size() >= nr) {
// no instructions are available from decode stage,
// or if reorder buffer is full
// stall!
if (rob.size() == this->nr) {
cout << "\nstall i because rob size == nr \n";
return false;
}
if (dInstructionQueue.empty() && debugMode) cout << "\nstall i because dInstrQueue empty\n";
return true; // don't count dInstrQueue empty as a stall
} else {
Instruction iInstr = this->dInstructionQueue.front();
// attempt to insert current instruction into corresponding reservation station
bool success = false;
switch(iInstr.opcode) {
case InstructionType::FLD:
success = insertInstructionInReservationStation(
rsUnitLoad,
RS_COUNT_LOAD,
iInstr
);
break;
case InstructionType::FSD:
success = insertInstructionInReservationStation(
rsUnitStore,
RS_COUNT_STORE,
iInstr
);
break;
case InstructionType::ADD:
success = insertInstructionInReservationStation(
rsUnitInt,
RS_COUNT_INT,
iInstr
);
break;
case InstructionType::ADDI:
success = insertInstructionInReservationStation(
rsUnitInt,
RS_COUNT_INT,
iInstr
);
break;
case InstructionType::SLT:
success = insertInstructionInReservationStation(
rsUnitInt,
RS_COUNT_INT,
iInstr
);
break;
case InstructionType::FADD:
success = insertInstructionInReservationStation(
rsUnitFpAdd,
RS_COUNT_FPADD,
iInstr
);
break;
case InstructionType::FSUB:
success = insertInstructionInReservationStation(
rsUnitFpAdd,
RS_COUNT_FPADD,
iInstr
);
break;
case InstructionType::FMUL:
success = insertInstructionInReservationStation(
rsUnitFpMult,
RS_COUNT_FPMULT,
iInstr
);
break;
case InstructionType::FDIV:
success = insertInstructionInReservationStation(
rsUnitFpDiv,
RS_COUNT_FPDIV,
iInstr
);
break;
case InstructionType::BNE:
success = insertInstructionInReservationStation(
rsUnitBu,
RS_COUNT_BU,
iInstr
);
break;
default:
throw invalid_argument(
"i_dispatch: the provided InstructionType was not recognized. Check to ensure that input parameters are not null"
);
}
if (!success) {
// reservation stations full,
// stall!
cout << "\nstall i because rs insert was unsuccessful (" << getRSNameFromInstructionType(iInstr.opcode) << " full)\n";
return false;
}
// rs contains new entry for current instruction, remove from dispatch instr queue
this->dInstructionQueue.pop_front();
// insert entry into ROB
ROBStatus robEntry;
robEntry.instruction = iInstr;
robEntry.status = InstructionStatusType::ISSUED;
robEntry.busy = true;
robEntry.entryName = generateROBStatusEntryName(iInstr);
this->rob.push_back(robEntry);
// update rs entry
RSStatus iInstrRs = getReservationStationForInstruction(iInstr);
iInstrRs.destination = robEntry.entryName;
// Vj, Qj
if (!iInstr.rs.empty()) {
// update Vj
iInstrRs.vj = iInstr.rs;
// update Qj
for (auto & entry : rob) {
if (
entry.instruction.rd == iInstr.rs &&
entry.status != InstructionStatusType::WRITING_RESULT
) {
iInstrRs.qj = entry.entryName;
break;
}
}
}
// Vk, Qk
if (!iInstr.rt.empty()) {
// update vk
iInstrRs.vk = iInstr.rt;
// update qk
for (auto & entry : rob) {
if (
entry.instruction.rd == iInstr.rt &&
entry.status != InstructionStatusType::WRITING_RESULT
) {
iInstrRs.qk = entry.entryName;
break;
}
}
}
// Immediate
if (
iInstr.opcode == InstructionType::ADDI ||
iInstr.opcode == InstructionType::FLD ||
iInstr.opcode == InstructionType::FSD
) {
iInstrRs.a = iInstr.immediate;
}
// replace rs in corresponding unit
updateReservationStation(iInstr, iInstrRs);
}
}
return true;
}