forked from Nilanshrajput/AC2MC-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
631 lines (584 loc) · 15.9 KB
/
main.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
#include<iostream>
#include<fstream>
#include<sstream>
#include<string.h>
#include<bitset>
#include"RType.h"
#include"IType.h"
#include"SBType.h"
#include"SType.h"
#include"UJType.h"
#include"UType.h"
#include"ALU.h"
#include"IAG.h"
#include"Decode.h"
#include"Fetch.h"
#include"MUX_Y.h"
#include"RegistryFile.h"
#include"RegUpdate.h"
#include"MemoryAccess.h"
#include"InterStateBuffers.h"
#include"Assembler.h"
#include"CacheMemory.h"
using namespace std;
void findLabels(string,vector<string>&,vector<int>&);
void memory(InterStateBuffers &,MemoryAccess & ,MUX_Y &,Cache &);
void writeBack(InterStateBuffers &, RegUpdate &, Registry_File &);
void print(int i, InterStateBuffers &, Registry_File &);
void updateISB(InterStateBuffers &);
void updateAfterDecoder(InterStateBuffers &);
void updateIfStall(InterStateBuffers &);
void printSummary(InterStateBuffers &);
int main(){
RType rTypeInsObj;
IType iTypeInsObj;
SBType sbTypeInsObj;
SType sTypeInsObj;
UJType ujTypeInsObj;
UType uTypeInsObj;
InterStateBuffers isb;
vector<string> labelNames;
vector<int> labelLineNumber;
int insType;
string inputFileName = "input1.txt";
string outputFileName = "machineCode.txt";
string basicCodeFileName = "basicCode.txt";
// Directory path to source instruction text files
string dir = "./instructions/";
rTypeInsObj.initialise(dir + "RType.txt");
iTypeInsObj.initialise(dir + "IType.txt");
sbTypeInsObj.initialise(dir + "SBType.txt");
sTypeInsObj.initialise(dir + "SType.txt");
ujTypeInsObj.initialise(dir + "UJType.txt");
uTypeInsObj.initialise(dir + "UType.txt");
MemoryAccess memAccess;
assembler_initiate(memAccess);
ifstream iFile(inputFileName.c_str(), ios :: in);
ofstream oFile(outputFileName.c_str());
ofstream oFile2(basicCodeFileName.c_str());
cout<<"\n:::::::::: RISC V SIMULATOR :::::::::::::\n\n"<<endl;
//Find All Labels in the input file
findLabels(inputFileName,labelNames,labelLineNumber);
if(!iFile.is_open()) cout<<"Error in reading input file";
else{
int lineNo=0;
bitset<32> machineCode;
string line;
while(getline(iFile,line)) {
// Skip Label descriptions
size_t found = line.find(':');
if(found != string::npos) continue;
lineNo++;
// replacing sp with x2
for(int i=1;i<(line.size()-2);i++){
if(line[i]=='s' &&line[i+1]=='p'&&(line[i-1]==' '||line[i-1]==','||line[i-1]=='(')&&(line[i+2]==' ' ||line[i+2]==','||line[i+2]==')'||line[i+2]=='\n')){
line[i]='x';
line[i+1]='2';
}
}
// replacing labels
for(int i=0;i<labelNames.size();i++){
size_t found = line.find(labelNames[i]);
if(found != string::npos){
string newline;
for(int j=0;j<found;j++) newline.push_back(line[j]);
int offset = labelLineNumber[i] - lineNo -i;
// offset *= 4; // Multiplying offset by 4
ostringstream numStr;
numStr << offset;
string intStr = numStr.str();
line = newline + intStr;
}
}
if(rTypeInsObj.isPresent(line)) {
machineCode = rTypeInsObj.decode(line);
insType = 1;
}
else if(iTypeInsObj.isPresent(line)) {
machineCode = iTypeInsObj.decode(line);
insType = 2;
}
else if(sbTypeInsObj.isPresent(line)) {
machineCode = sbTypeInsObj.decode(line);
insType = 3;
}
else if(sTypeInsObj.isPresent(line)) {
machineCode = sTypeInsObj.decode(line);
insType = 4;
}
else if(ujTypeInsObj.isPresent(line)) {
machineCode = ujTypeInsObj.decode(line);
insType = 5;
}
else if(uTypeInsObj.isPresent(line)) {
machineCode = uTypeInsObj.decode(line);
insType = 6;
}
else {
cout<<"ERROR !! Instuction not identified : "<<line<<endl;
machineCode = bitset<32>(0);
insType = -1;
}
oFile <<lineNo<<" "<< machineCode <<" "<< insType << endl;
oFile2 <<lineNo<<" "<< line << endl;
}
oFile<<lineNo+1<<" 0 0"<<endl;
}
iFile.close();
oFile.close();
oFile2.close();
char cacheChoice;
cout<<"Enable cache ? (y/n) : ";
cin>>cacheChoice;
int cs=512,bs=4;
if(cacheChoice=='y'||cacheChoice=='Y'){
isb.enableCache = true;
cout<<" Enter cache size in Bytes (recommended size - 512 B) : ";
cin>>cs;
cout<<" Enter cache block size in Bytes (recommended size - 4 B) : ";
cin>>bs;
}
else isb.enableCache = true;
Registry_File rFile;
Fetch fetch;
MUX_Y muxy;
Decode decode;
RegUpdate regUpdate;
ALU alu;
IAG iag;
Cache cache(cs,bs,1,1);
decode.initialise();
cout<<">>> Modes <<<"<<endl;
cout<<" 1) Without Pipelining\n 2) With Pipelining but no data forwarding\n";
cout<<" 3) With Pipelining and data forwarding\n\n";
cout<<" Enter your choice : ";
int ch;
bool runStepByStep = false;
cin>>ch;
if(ch ==1){
isb.enablePipe = false;
cout<<" Show register value after every cycle ? (y/n) ";
char c;
cin>>c;
if(c=='y'||c=='Y') isb.printRegFile = true;
else isb.printRegFile = false;
isb.printISB = false;
cout<<" Run step by step ? (y/n) ";
cin>>c;
if(c=='y'||c=='Y') runStepByStep = true;
else runStepByStep = false;
}
else if(ch == 2){
isb.enablePipe = true;
isb.enableDF = false;
char c;
cout<<" Show register value after every cycle ? (y/n) ";
cin>>c;
if(c=='y'||c=='Y') isb.printRegFile = true;
else isb.printRegFile = false;
cout<<" Show inter state buffer values after every cycle ? (y/n) ";
cin>>c;
if(c=='y'||c=='Y') isb.printISB = true;
else isb.printISB = false;
}
else if(ch == 3) {
isb.enablePipe = true;
isb.enableDF = true;
char c;
cout<<" Show register value after every cycle ? (y/n) ";
cin>>c;
if(c=='y'||c=='Y') isb.printRegFile = true;
else isb.printRegFile = false;
cout<<" Show inter state buffer values after every cycle ? (y/n) ";
cin>>c;
if(c=='y'||c=='Y') isb.printISB = true;
else isb.printISB = false;
}
else{ cout<<" invalid choice !\n"; return 0;}
// If pipeline is disabled
if(!isb.enablePipe){
int i = 0;
char k;
while(1){
i++;
if(runStepByStep){
k = 'n';
while(k != 'r' && k != 'R'){
cout<<"\n RUN CYCLE NUMBER : "<<i<<" ? "<<" ( enter 'r' to run & 'e' to terminate) "<<endl;
cin>>k;
}
}
fetch.get(isb,rFile);
if(isb.IR.readInt() == 0 || k == 'e' || k == 'E')
break;
decode.decoder(isb,rFile);
alu.compute(isb);
memory(isb, memAccess, muxy,cache);
writeBack(isb, regUpdate, rFile);
iag.step(isb,alu);
isb.resetAll();
if(isb.printRegFile) print(i,isb,rFile);
}
isb.totalCycles = i-1;
cout<<"\n\n---------------- Code executed succesfully ----------------\n\n"<<endl;
cout<<" Final register values :\n";
rFile.print();
cout<<" Summary :\n";
printSummary(isb);
}
// If pipeline is enabled with data forwarding
if(isb.enablePipe && isb.enableDF){
bool end = false;
int i = 0,j=0;
while(1){
i++;
isb.isMispred = false;
if(end)
j++;
if(j >= 4)
break;
if(i==1){
if(!end){
fetch.get(isb,rFile);
if(!isb.hazard_type) iag.update(isb);
else iag.jumpPC(isb,isb.branch_address);
}
updateISB(isb);
}
else if(i==2) {
decode.decoder(isb,rFile);
if(isb.stall){
updateIfStall(isb);
continue;
}
updateAfterDecoder(isb);
if(isb.isMispred) iag.jumpPC(isb,isb.nextPC);
if(!end){
if(isb.hazard_type == 2){
iag.jumpPC(isb, isb.branch_address);
}
fetch.get(isb,rFile);
updateISB(isb);
if(!isb.hazard_type) iag.update(isb);
else iag.jumpPC(isb,isb.branch_address);
}
if(end) updateISB(isb);
}
else if(i==3) {
if(!isb.stall) alu.compute(isb);
decode.decoder(isb,rFile);
if(isb.stall){
updateIfStall(isb);
continue;
}
updateAfterDecoder(isb);
if(isb.isMispred) iag.jumpPC(isb,isb.nextPC);
if(!end){
if(isb.hazard_type == 2){
iag.jumpPC(isb, isb.branch_address);
}
fetch.get(isb,rFile);
updateISB(isb);
if(!isb.hazard_type) iag.update(isb);
else iag.jumpPC(isb,isb.branch_address);
}
if(end) updateISB(isb);
}
else if(i==4) {
memory(isb, memAccess, muxy,cache);
if(!isb.stall) alu.compute(isb);
decode.decoder(isb,rFile);
if(isb.stall){
updateIfStall(isb);
continue;
}
updateAfterDecoder(isb);
if(isb.isMispred) iag.jumpPC(isb,isb.nextPC);
if(!end){
if(isb.hazard_type == 2){
iag.jumpPC(isb, isb.branch_address);
}
fetch.get(isb,rFile);
updateISB(isb);
if(!isb.hazard_type) iag.update(isb);
else iag.jumpPC(isb,isb.branch_address);
}
if(end) updateISB(isb);
}
else{
writeBack(isb, regUpdate, rFile);
memory(isb, memAccess, muxy,cache);
if(!isb.stall) alu.compute(isb);
decode.decoder(isb,rFile);
if(isb.stall){
updateIfStall(isb);
continue;
}
updateAfterDecoder(isb);
if(isb.isMispred) iag.jumpPC(isb,isb.nextPC);
if(!end){
if(isb.hazard_type == 2){
iag.jumpPC(isb, isb.branch_address);
}
fetch.get(isb,rFile);
updateISB(isb);
if(!isb.hazard_type) iag.update(isb);
else iag.jumpPC(isb,isb.branch_address);
}
if(end) updateISB(isb);
}
if(isb.IR.readInt() == 0 )
end = true;
if(isb.printRegFile || isb.printISB) print(i,isb,rFile);
}
isb.totalCycles = i-1;
cout<<"\n\n---------------- Code executed succesfully ----------------\n\n"<<endl;
cout<<" Final register values :\n";
rFile.print();
cout<<" Summary :\n";
printSummary(isb);
}
// If pipeline is enabled without data forwarding
if(isb.enablePipe && !isb.enableDF){
bool end = false, skipExecute = false;
int i = 0,j=0;
while(1){
i++;
isb.isMispred = false;
if(end)
j++;
if(j >= 4)
break;
if(i==1){
if(!end){
fetch.get(isb,rFile);
if(!isb.hazard_type) iag.update(isb);
else iag.jumpPC(isb,isb.branch_address);
}
updateISB(isb);
}
else if(i==2) {
decode.decoder(isb,rFile);
if(isb.stall){
updateIfStall(isb);
continue;
}
updateAfterDecoder(isb);
if(isb.isMispred) iag.jumpPC(isb,isb.nextPC);
if(!end){
if(isb.hazard_type == 2){
iag.jumpPC(isb, isb.branch_address);
}
fetch.get(isb,rFile);
updateISB(isb);
if(!isb.hazard_type) iag.update(isb);
else iag.jumpPC(isb,isb.branch_address);
}
if(end) updateISB(isb);
}
else if(i==3) {
if(!isb.stall) alu.compute(isb);
decode.decoder(isb,rFile);
if(isb.stall){
updateIfStall(isb);
continue;
}
updateAfterDecoder(isb);
if(isb.isMispred) iag.jumpPC(isb,isb.nextPC);
if(!end){
if(isb.hazard_type == 2){
iag.jumpPC(isb, isb.branch_address);
}
fetch.get(isb,rFile);
updateISB(isb);
if(!isb.hazard_type) iag.update(isb);
else iag.jumpPC(isb,isb.branch_address);
}
if(end) updateISB(isb);
}
else if(i==4) {
memory(isb, memAccess, muxy, cache);
if(!isb.stall) alu.compute(isb);
decode.decoder(isb,rFile);
if(isb.stall){
updateIfStall(isb);
continue;
}
updateAfterDecoder(isb);
if(isb.isMispred) iag.jumpPC(isb,isb.nextPC);
if(!end){
if(isb.hazard_type == 2){
iag.jumpPC(isb, isb.branch_address);
}
fetch.get(isb,rFile);
updateISB(isb);
if(!isb.hazard_type) iag.update(isb);
else iag.jumpPC(isb,isb.branch_address);
}
if(end) updateISB(isb);
}
else{
writeBack(isb, regUpdate, rFile);
memory(isb, memAccess, muxy, cache);
if(!isb.stall) alu.compute(isb);
decode.decoder(isb,rFile);
if(isb.stall){
updateIfStall(isb);
continue;
}
updateAfterDecoder(isb);
if(isb.isMispred) iag.jumpPC(isb,isb.nextPC);
if(!end){
if(isb.hazard_type == 2){
iag.jumpPC(isb, isb.branch_address);
}
fetch.get(isb,rFile);
updateISB(isb);
if(!isb.hazard_type) iag.update(isb);
else iag.jumpPC(isb,isb.branch_address);
}
if(end) updateISB(isb);
}
if(isb.IR.readInt() == 0 )
end = true;
if(isb.printRegFile || isb.printISB) print(i,isb,rFile);
}
isb.totalCycles = i-1;
cout<<"\n\n---------------- Code executed succesfully ----------------\n\n"<<endl;
cout<<" Final register values :\n";
rFile.print();
cout<<" Summary :\n";
printSummary(isb);
}
return 0;
}
void findLabels(string inputFileName, vector<string> &labelNames, vector<int> &labelLineNumber){
ifstream iFile(inputFileName.c_str());
if(iFile.is_open()){
int lineNo = 0;
string line;
while(getline(iFile,line)) {
lineNo++;
size_t found = line.find(':');
if(found != string::npos){
string labelName;
int i=0;
while(!isalnum(line[i])) i++;
while(line[i]!=':' && i<line.size()) labelName.push_back(line[i++]);
labelNames.push_back(labelName);
labelLineNumber.push_back(lineNo);
}
}
}
iFile.close();
}
void memory(InterStateBuffers &isb,MemoryAccess &memAccess ,MUX_Y &muxy,Cache &cache){
if(!isb.enablePipe){
if(isb.isMem == true){
if(isb.insType == 4){
if(isb.enableCache) cache.WriteCache(memAccess,isb,1);
else memAccess.writeWord(isb);
muxy.MUX_Y_SELECT = 1;
}
else {
if(isb.enableCache) cache.ReadCache(memAccess,isb,1);
else memAccess.readWord(isb);
muxy.MUX_Y_SELECT = 2; // for getting register val from memory
}
}
else if(isb.isjalr == true || isb.insType == 5){
muxy.MUX_Y_SELECT = 3;
}
else
muxy.MUX_Y_SELECT = 1;
isb.RY.writeInt(muxy.output(isb));
}
else{
if(isb.isMemM == true){
if(isb.insTypeM == 4){
if(isb.enableCache) cache.WriteCache(memAccess,isb,1);
else memAccess.writeWord(isb);
muxy.MUX_Y_SELECT = 1;
}
else {
if(isb.enableCache) cache.ReadCache(memAccess,isb,1);
else memAccess.readWord(isb);
muxy.MUX_Y_SELECT = 2; // for getting register val from memory
}
}
else if(isb.isjalrM == true || isb.insTypeM == 5){
muxy.MUX_Y_SELECT = 3;
}
else
muxy.MUX_Y_SELECT = 1;
isb.RY.writeInt(muxy.output(isb));
}
}
void writeBack(InterStateBuffers &isb, RegUpdate ®Update, Registry_File &rFile){
if(!isb.enablePipe){
if(isb.write_back_location != -1){
regUpdate.update(isb,rFile, isb.write_back_location);
}
}
else{
if(isb.wblocW != -1){
regUpdate.update(isb,rFile, isb.wblocW);
}
}
}
void updateISB(InterStateBuffers &isb){
isb.wblocW = isb.wblocM;
isb.wblocM = isb.wblocE;
isb.wblocE = isb.wblocD;
isb.returnAddW = isb.returnAddM;
isb.returnAddM = isb.returnAddE;
isb.returnAddE = isb.returnAddD;
isb.returnAddD = isb.return_address;
isb.insTypeW = isb.insTypeM;
isb.insTypeM = isb.insTypeE;
isb.insTypeE = isb.insTypeD;
isb.insTypeD = isb.insType;
isb.isjalrW = isb.isjalrM;
isb.isjalrM = isb.isjalrE;
isb.isjalrE = isb.isjalrD;
isb.isMemW = isb.isMemM;
isb.isMemM = isb.isMemE;
isb.isMemE = isb.isMemD;
}
void print(int i, InterStateBuffers &isb, Registry_File &rFile){
cout<<"===== < Cycle "<<i<<" > ====="<<endl;
if(isb.printRegFile) rFile.print();
if(isb.printISB) isb.printAll();
cout<<endl;
}
void printSummary(InterStateBuffers &isb){
cout<<" Total Cycles \t\t:\t"<<isb.totalCycles<<endl;
cout<<" Total Stalls \t\t:\t"<<isb.numStall<<endl;
cout<<" Total Misprediction \t:\t"<<isb.mispredNumber<<endl;
cout<<" Total Instruction Cache access count \t:\t"<<isb.accesscount<<endl;
cout<<" Total Instruction Cache hit count \t:\t"<<isb.hitcount<<endl;
cout<<" Total Instruction Cache cold miss \t:\t"<<isb.coldmiss<<endl;
cout<<" Total Data Cache access count \t:\t"<<isb.accesses_data<<endl;
cout<<" Total Data Cache hit count \t:\t"<<isb.hits_data<<endl;
cout<<" Total Data Cache cold miss \t:\t"<<isb.cold_misses_data<<endl;
cout<<" Total Data Cache conflict miss \t:\t"<<isb.conflict_misses_data<<endl;
cout<<" Total Data Cache capacity miss \t:\t"<<isb.capacity_misses_data<<endl;
}
void updateAfterDecoder(InterStateBuffers &isb){
isb.wblocD = isb.write_back_location;
isb.isjalrD = isb.isjalr;
isb.isMemD = isb.isMem;
}
void updateIfStall(InterStateBuffers &isb){
isb.wblocW = isb.wblocM;
isb.wblocM = isb.wblocE;
isb.wblocE = -1;
isb.insTypeW = isb.insTypeM;
isb.insTypeM = isb.insTypeE;
isb.returnAddW = isb.returnAddM;
isb.returnAddM = isb.returnAddE;
isb.isjalrW = isb.isjalrM;
isb.isjalrM = isb.isjalrE;
isb.isMemW = isb.isMemM;
isb.isMemM = isb.isMemE;
}