-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
337 lines (303 loc) · 11.7 KB
/
main.cpp
File metadata and controls
337 lines (303 loc) · 11.7 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
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
337
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include "pc.h"
#include "if.h"
#include "ex.h"
#include "id.h"
#include "wb.h"
//mem.h is included in ex.h so cannot be included here
#define DEBUG true //set this to false to disable debugging mode
/*Improvements
Make all classes inherit a 'control' class
A few repeated functions in each class which could be removed by including this
Improve decoding so that it is more global and not specific to each type of instruction
*/
void clearMemory();
void clearRegisters(vector<vector<unsigned>>&IDEX,vector<vector<unsigned>>&EXMEM,vector<vector<unsigned>>&MEMWB,vector<vector<string>>&IFID, unsigned pCount);
void checkHazards(id& Decode, ex& EX, bool& dataHazard, unsigned& stall);
void printStringVector(vector<vector<string>>&vec);
void printUnsignedVector(vector<vector<unsigned>>&vec);
void updateRegisters(vector<vector<unsigned>>&IDEX,vector<vector<unsigned>>&EXMEM,vector<vector<unsigned>>&MEMWB,vector<vector<string>>&IFID, unsigned pCount);
void updateUnsigned(vector<vector<unsigned>>&vec, unsigned rowMax);
void updateString(vector<vector<string>>&vec, unsigned rowMax);
int main(){
int i =0; //Counts number of cycles //Information purpose only
unsigned stall = 0; //Used to determine how many loops to stall for
//Initialising classes
fetch Fetch;
id Decode;
pc PC;
alu ALU;
ex EX;
mem MEM;
wb WB;
if (DEBUG){ //Enabling debug for each class instantiation
Fetch.setDebug();
Decode.setDebug();
ALU.setDebug();
EX.setDebug();
MEM.setDebug();
WB.setDebug();
}
//Initialising registers and memory as vectors
vector<vector<unsigned>> IDEX, EXMEM, MEMWB, DataMemory;
vector<string> InstructionMemory;
vector<vector<string>> IFID;
if(DEBUG){cout << "Initialising registers" << endl;}
updateRegisters(IDEX, EXMEM, MEMWB, IFID, 0); //Initialise all registers for first loop
clearMemory(); //This clears the General_Registers and DataMemory text files to all 0 values
bool dataHazard = false; //Detecting if the program is stalling or not
//Read in all instructions and store into Instruction Memory
if(DEBUG){cout<<"readFile start" << endl;}
Fetch.readInput(InstructionMemory, PC.accessPC());
if(DEBUG){
cout <<endl <<"Reading InstructionMemory" << endl;
for(int i=0;i<InstructionMemory.size();i++){
cout << InstructionMemory[i] << endl;
}
}
if(DEBUG){cout<<"readFile complete" <<endl<< endl;}
//Start pipelining from here
while (!EX.checkEnd()){
//Fetch instruction from memory
if(DEBUG){cout<<endl<<endl<<endl<<" Program Count: "<<PC.accessPC()<<" Hazard status: " << dataHazard<<endl;}
if(DEBUG){cout <<" Number of cycles: " <<i<<endl<<endl<<endl;}
if(DEBUG){ //Check values of key registers
fstream File;
string IN;
File.open("General_Registers.txt");
vector<unsigned> tempMemory;
while(getline(File,IN)){
tempMemory.push_back(stoi(IN));
}
File.close();
cout <<"$t0: " << tempMemory[8] <<" $t1: "<<tempMemory[9]<<" $t2: "<<tempMemory[10]<<" $t3: "<<tempMemory[11]<<" $t4: " << tempMemory[12] << endl;
}
if(DEBUG){ //States of IDEX, EXMEM and MEMWB
//if(DEBUG){cout <<endl<< "IFID:" << endl;}
//if(DEBUG){printStringVector(IFID);}
//if(DEBUG){cout <<endl<< "IDEX:" << endl;}
//if(DEBUG){printUnsignedVector(IDEX);}
//if(DEBUG){cout <<endl<< "EXMEM:" << endl;}
//if(DEBUG){printUnsignedVector(EXMEM);}
//if(DEBUG){cout <<endl<< "MEMWB:" << endl;}
//if(DEBUG){printUnsignedVector(MEMWB);}
}
if (dataHazard == false){ //If no data hazard (stall) fetch next instruction
Fetch.sendNextInstruction(IFID, InstructionMemory, PC.accessPC());
}
//Decode the instruction
if(Decode.checkStall()){
if(DEBUG){cout << "Stalling ID" << endl;}
IFID.push_back(vector<string>());
IFID[PC.accessPC()+1].push_back(IFID[PC.accessPC()][0]);
IFID[PC.accessPC()+1].push_back(IFID[PC.accessPC()][1]);
}
else if(stoi(IFID[PC.accessPC()][1])){
Decode.decode(IFID, IDEX, PC.accessPC());
}
//execute the instruction
EX.updatepCount(PC.accessPC());//Always update EX pCount since it is used for jumps / branches
if(EX.checkStall()){
if(DEBUG){cout << "Stalling EX" << endl;}
IDEX.push_back(vector<unsigned>());
EXMEM.push_back(vector<unsigned>());
for(unsigned i=0;i<IDEX[PC.accessPC()].size();i++){
IDEX[PC.accessPC()+1].push_back(IDEX[PC.accessPC()][i]); //Copy current execute instruction to next row
}
for(unsigned i=0;i<EXMEM[PC.accessPC()].size();i++){
EXMEM[PC.accessPC()+1].push_back(EXMEM[PC.accessPC()][i]); //Copy current memory instruction to next row otherwise next section won't run (out of index)
}
}
else if(IDEX[PC.accessPC()][4]){
EX.labelList = Decode.labelList; //update labels
EX.execute(PC.accessPC(),IDEX,EXMEM,ALU,MEM, InstructionMemory);
}
//Store the result in memory
if(EXMEM[PC.accessPC()][0]){
if(DEBUG){cout <<endl<<endl<<endl<<"//----------Accessing Memory" << endl;}
MEMWB.push_back(vector<unsigned>());
if(EXMEM[PC.accessPC()][4] == 8){ //LW
//Load value at [3] into register, write into address [2] in WB
MEMWB[(PC.accessPC()+1)].push_back(1);//[0]Push back control signal
MEM.load(EXMEM[PC.accessPC()][3], PC.accessPC(), MEMWB, EXMEM); //Access reg2
MEMWB[(PC.accessPC()+1)].push_back(EXMEM[PC.accessPC()][2]); //[2]Push back address of value to write
MEMWB[(PC.accessPC()+1)].push_back(EXMEM[PC.accessPC()][5]); //[3] opcode
}
if(EXMEM[PC.accessPC()][4] == 13){ //SW
MEMWB[PC.accessPC()+1].push_back(0); //[0] //WB control signal
//Store value from register [3] into register [2]
unsigned value = MEM.load(EXMEM[PC.accessPC()][2], PC.accessPC(), MEMWB, EXMEM); //address
cout <<"Value: "<<value << endl;
MEM.store(value, EXMEM[PC.accessPC()][3]);//value, address
}
if (EXMEM[PC.accessPC()][4] == 21){ //opcode == 20 //OUT
MEMWB[(PC.accessPC()+1)].push_back(0); //[0] //Control signal
MEM.write(EXMEM[PC.accessPC()][2]);
}
}
else if(!EXMEM[PC.accessPC()][0]){ //If no memory access required, wait until next cycle for WB
MEMWB.push_back(vector<unsigned>());
MEMWB[(PC.accessPC()+1)].push_back(EXMEM[PC.accessPC()][1]); //[0]//WB control signal
MEMWB[(PC.accessPC()+1)].push_back(EXMEM[PC.accessPC()][2]); //[1]//value
MEMWB[(PC.accessPC()+1)].push_back(EXMEM[PC.accessPC()][3]); //[2]//address
}
if(MEMWB[PC.accessPC()][0]){ //Write back
if(DEBUG){cout<<endl<<endl<<endl<<"//----------Write back start" << endl;}
WB.Write_Back(MEMWB[PC.accessPC()][1],MEMWB[PC.accessPC()][2]); //value, address
}
if(DEBUG){cout <<endl<<endl<<"//----------End of cycle checks" <<endl;}
if (EX.returnpCount() != PC.accessPC()){ //Checking for jumps / branches to setup
PC.setJumpTo(EX.returnpCount());
PC.findWhenToJump();
Fetch.setJump(true, 0); //Disable IF
Decode.setJump(true); //Disable ID
EX.setJump(true); //Disable EX
if(DEBUG){cout << "Branching on "<<PC.whenToJump() <<" cycle" << endl;}
PC.count();
if(DEBUG){cout << "Increased PC" << endl;}
checkHazards(Decode,EX, dataHazard, stall);
updateRegisters(IDEX, EXMEM, MEMWB, IFID, PC.accessPC());
if(DEBUG){cout << "Updated Registers" << endl;}
}
else if(PC.accessPC() == PC.whenToJump()){ //Jumping / Branching
PC.setPC(PC.jumpTo());
PC.setWhenToJump(-1);
//Clear registers and index them to PC
Fetch.setJump(false, PC.accessPC()); //Enable IF again
Decode.setJump(false); //Enable ID
EX.setJump(false); //Enable EX
clearRegisters(IDEX, EXMEM, MEMWB, IFID, PC.accessPC());
if(DEBUG){cout << "Branched to line "<<PC.accessPC() << endl;}
}
else{
PC.count();
if(DEBUG){cout << "Increased PC" << endl;}
checkHazards(Decode,EX, dataHazard, stall);
updateRegisters(IDEX, EXMEM, MEMWB, IFID, PC.accessPC());
if(DEBUG){cout << "Updated Registers" << endl;}
}
i++;
}
if (EX.checkEnd()){
cout << "//----------END OF PROGRAM---------//" << endl;
cout <<endl<<endl<< "Number of cycles: " <<i << endl;
cout <<endl<<endl<< "Number of instructions executed: " <<EX.getExecutions() << endl;
}
return 0;
}
void clearMemory(){ //Clearing text files for initialisation
//Just clear data memory
fstream file;
file.open("DataMemory.txt", fstream::out | fstream::trunc);
file.close();
//Set all of General_Registers lines to 0
file.open("General_Registers.txt", fstream::out | fstream::trunc);
for (int i=0; i<32;i++){
file << 0 << endl; //Sets the 32 general registers to 0
}
file.close();
}
void clearRegisters(vector<vector<unsigned>>&IDEX,vector<vector<unsigned>>&EXMEM,vector<vector<unsigned>>&MEMWB,vector<vector<string>>&IFID, unsigned pCount){
//Clear and update indexes to pCount value
IFID.clear();
IDEX.clear();
EXMEM.clear();
MEMWB.clear();
updateString(IFID, pCount);
updateUnsigned(IDEX, pCount);
updateUnsigned(EXMEM, pCount);
updateUnsigned(MEMWB, pCount);
}
void checkHazards(id& Decode,ex& EX, bool& dataHazard, unsigned& stall){
if(stall > 0){
cout << "Stall > 0" << endl;
Decode.updateStall(true);
EX.updateStall(true);
stall--;
}
if (Decode.checkStallReq() != 0){ //Initial stall set-up
cout << "Initial stall setup" << endl;
stall = Decode.checkStallReq();
Decode.resetStallReq();
dataHazard = true; //Stop IF reading instructions
//Set stall flags on ID and EX
Decode.updateStall(true);
EX.updateStall(true);
}
if(stall == 0){
//Enable sections again
cout <<"Disabling stall" << endl;
dataHazard = false;
Decode.updateStall(false);
EX.updateStall(false);
}
}
void printStringVector(vector<vector<string>>&vec){
for(int i=0;i<vec.size();i++){
cout <<endl <<"Row: "<< i << endl;
for(int y=0;y<vec[i].size();y++){
cout << vec[i][y] << " ";
}
}
cout << endl;
}
void printUnsignedVector(vector<vector<unsigned>>&vec){
for(int i=0;i<vec.size();i++){
cout <<endl <<"Row: "<< i << endl;
for(int y=0;y<vec[i].size();y++){
cout << vec[i][y] << " ";
}
}
cout << endl;
}
void updateRegisters(vector<vector<unsigned>>&IDEX,vector<vector<unsigned>>&EXMEM,vector<vector<unsigned>>&MEMWB,vector<vector<string>>&IFID, unsigned pCount){
//This function will add rows of 0s into registers which aren't being used in the next cycle
//Registers need to have data in them otherwise the program will break when checking control signals
updateString(IFID, pCount);
updateUnsigned(IDEX, pCount);
updateUnsigned(EXMEM, pCount);
updateUnsigned(MEMWB, pCount);
}
void updateUnsigned(vector<vector<unsigned>>&vec, unsigned rowMax){ //rowMax is just pCount
unsigned rowCount = 0;
for(int y=0;y<vec.size();y++){ //Get current number of rows in the register
rowCount++;
}
if(!rowMax){
vec.push_back(vector<unsigned>());
for(int i=0;i<7;i++){
vec[0].push_back(0);
}
}
else if (rowMax >0){
for(int y=rowCount;y<(rowMax+1);y++){ //increase no. rows to match rowMax
vec.push_back(vector<unsigned>());
for(int i=0;i<7;i++){
vec[y].push_back(0);
}
}
}
}
void updateString(vector<vector<string>>&vec, unsigned rowMax){ //Same as updateUnsigned but for a 2d string vector
unsigned rowCount = 0;
for(int y=0;y<vec.size();y++){ //Get current number of rows in the register
rowCount++;
}
if(!rowMax){
vec.push_back(vector<string>());
for(int i=0;i<2;i++){
vec[0].push_back("0");
}
}
if(rowMax>0){ //Only edit IFID if not the first instruction
for(int y=rowCount;y<(rowMax+1);y++){ //increase no. rows to match rowMax
vec.push_back(vector<string>());
for(int i=0;i<2;i++){
vec[y].push_back("0");
}
}
}
}