-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtom.cpp
More file actions
296 lines (250 loc) · 9.03 KB
/
Copy pathtom.cpp
File metadata and controls
296 lines (250 loc) · 9.03 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
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#define RS_NO 5
#define REG_NO 8
#define MAX_INST_QUEUE 10
using namespace std;
typedef struct instruction{
int opcode;
int destination;
int op1;
int op2;
} instruction; // Instruction
typedef struct rs{
int busy;
int opcode;
int vj;
int vk;
int qj;
int qk;
int disp;
} rs; // Reservation Station
void printState(vector<instruction>& instruction_queue, vector<rs>& RSs, vector<int>& RAT, vector<int>& RF){
// Printing RSs
cout << " Busy Op Vj Vk Qj Qk Disp\n";
for ( int i = 0; i < RS_NO; i++){
cout << "RS"<< i << " "<< RSs[i].busy << " " << RSs[i].opcode << " " << RSs[i].vj << " " << RSs[i].vk << " " << RSs[i].qj << " " << RSs[i].qk << " " << RSs[i].disp << endl;
}
cout << "------------------------------------------\n";
// Printing RF, RAT
cout << " RF RAT\n";
for ( int i = 0; i < REG_NO; i++){
cout << i << ": " << RF[i] << " " << RAT[i] << "\n";
}
cout << "------------------------------------------\n";
// Printing Instruction Queue
cout << "Instuction Queue\n";
for (int i = 0; i < instruction_queue.size(); i++){
instruction inst = instruction_queue[i];
switch(inst.opcode){
case 0:
cout << "Add R" << inst.destination << ", R" << inst.op1 << ", R" << inst.op2 << "\n";
break;
case 1:
cout << "Sub R" << inst.destination << ", R" << inst.op1 << ", R" << inst.op2 << "\n";
break;
case 2:
cout << "Mult R" << inst.destination << ", R" << inst.op1 << ", R" << inst.op2 << "\n";
break;
case 3:
cout << "Div R" << inst.destination << ", R" << inst.op1 << ", R" << inst.op2 << "\n";
break;
default:
continue;
}
}
cout << endl;
return ;
}
int main(int argc, char** argv){
if (argc == 1){
printf("Usage : ./binary <instructions filename>");
return -1;
}
string filename = string(argv[1]);
ifstream fd;
fd.open(filename);
vector<instruction> instruction_queue; // Instruction Queue
vector<instruction> instruction_queue_extra; // Store extra instruction that did not fit in queue here
vector<rs> RSs(RS_NO); // Reservation Stations
vector<int> RAT(REG_NO); // Register Allocation Table
vector<int> RF(REG_NO); // Register File
////////////////////////////////////////
int no_of_instructions;
int no_of_cycles;
int instructions_pending;
int start, end;
instruction inst;
//////////////////////////////////////////////
// Initialise all relevant containers
// Read from instruction file
fd >> no_of_instructions;
fd >> no_of_cycles;
// cout << no_of_instructions << (no_of_cycles);
instructions_pending = no_of_instructions - min(no_of_instructions,MAX_INST_QUEUE);
// Fill the instructon buffer
for( int i = 0; i < no_of_instructions; i++){
if (i < MAX_INST_QUEUE){
instruction tmp;
fd >> tmp.opcode;
fd >> tmp.destination;
fd >> tmp.op1;
fd >> tmp.op2;
instruction_queue.push_back(tmp);
} else {
instruction tmp;
fd >> tmp.opcode;
fd >> tmp.destination;
fd >> tmp.op1;
fd >> tmp.op2;
instruction_queue_extra.push_back(tmp);
}
}
reverse(instruction_queue.begin(), instruction_queue.end());
reverse(instruction_queue_extra.begin(), instruction_queue_extra.end());
// Read initial register values
for (int i = 0; i < REG_NO; i++){
fd >> RF[i];
}
fd.close();
// Initialise RSs and RAT
for(int i = 0; i < RS_NO; i++){
RSs[i].busy = 0;
RSs[i].disp = -1;
RSs[i].vj = -1;
RSs[i].vk = -1;
RSs[i].qj = -1;
RSs[i].qk = -1;
}
for (int i = 0; i < REG_NO; i++){
RAT[i] = -1;
}
///////////////////////////////////////////////////////////////
// Simulation
for(int i = 0; i < no_of_cycles; i++){
cout << "----------------------------------------\n";
cout << "Cycle " << i << endl;
// Print current system state
printState(instruction_queue,RSs,RAT,RF);
// Checking for instruction to dispatch - ADD/SUB
for (int j = 0; j < 3; j++){
if (RSs[j].busy && (RSs[j].vj != -1) && (RSs[j].vk != -1) && (RSs[j].disp == -1) ){
// Check if execution unit is busy or not
int tmp = false;
for (int k = 0; k < 3; k++){
if ( (RSs[k].busy) && (RSs[k].disp != -1) )
tmp = true;
}
if (tmp)
break;
RSs[j].disp = i;
break;
}
}
// Checking for instruction to dispatch - MUL/DIV
for (int j = 3; j < 5; j++){
if (RSs[j].busy && (RSs[j].vj != -1) && (RSs[j].vk != -1) && (RSs[j].disp == -1) ){
// Check if execution unit is busy or not
int tmp = false;
for (int k = 3; k < 5; k++){
if ((RSs[k].busy) && (RSs[k].disp != -1))
tmp = true;
}
if (tmp)
break;
RSs[j].disp = i;
break;
}
}
// Check if an instruction can be issued
if (instruction_queue.size() != 0){
inst = instruction_queue.back();
if (inst.opcode == 0 || inst.opcode == 1) {
start = 0;
end = 3;
} else {
start = 3;
end = 5;
}
// Issue if vacant resevation stations are left
for (int j = start; j < end; j++){
if (!RSs[j].busy){
RSs[j].opcode = inst.opcode;
RSs[j].busy = 1;
if (RAT[inst.op1] == -1)
RSs[j].vj = RF[inst.op1];
else
RSs[j].qj = RAT[inst.op1];
if (RAT[inst.op2] == -1)
RSs[j].vk = RF[inst.op2];
else
RSs[j].qk = RAT[inst.op2];
RAT[inst.destination] = j;
instruction_queue.pop_back();
break;
}
}
}
// Execution completion
for (int j = RS_NO-1; j >= 0; j--){
if ( (RSs[j].busy && (RSs[j].disp != -1) ) && ( ( (RSs[j].opcode == 0 || RSs[j].opcode == 1) && (RSs[j].disp + 2 <= i) ) || ( (RSs[j].opcode == 2) && (RSs[j].disp + 10 <= i) ) || ( (RSs[j].opcode == 3) && (RSs[j].disp + 40 <= i) ) ) ){
int tmp;
switch (RSs[j].opcode){
case 0:
tmp = RSs[j].vj + RSs[j].vk;
break;
case 1:
tmp = RSs[j].vj - RSs[j].vk;
break;
case 2:
tmp = RSs[j].vj * RSs[j].vk;
break;
case 3:
tmp = RSs[j].vj / RSs[j].vk;
break;
default:
continue;
}
// Capturing
for (int k = 0; k < RS_NO; k++){
if( (k != j) && ( RSs[k].busy && ( (RSs[k].qj == j) || (RSs[k].qk == j)) )){
if(RSs[k].qj == j){
RSs[k].qj = -1;
RSs[k].vj = tmp;
}
if(RSs[k].qk == j){
RSs[k].qk = -1;
RSs[k].vk = tmp;
}
}
}
// Commit
for (int k = 0; k < REG_NO; k++){
if (j == RAT[k]){
RF[k] = tmp;
RAT[k] = -1;
break;
}
}
// Clearing the completed instruction entry in RS
RSs[j].busy = 0;
RSs[j].disp = -1;
RSs[j].vj = -1;
RSs[j].vk = -1;
RSs[j].qj = -1;
RSs[j].qk = -1;
break;
}
}
// Load new instruction into the instruction buffer
if (instruction_queue_extra.size() != 0){
inst = instruction_queue_extra.back();
instruction_queue.insert(instruction_queue.begin(),inst);
instruction_queue_extra.pop_back();
}
}
}