-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTomasulo_withBranchPredictor.cpp
More file actions
1155 lines (1106 loc) · 34.9 KB
/
Copy pathTomasulo_withBranchPredictor.cpp
File metadata and controls
1155 lines (1106 loc) · 34.9 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
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<bits/stdc++.h>
using namespace std;
/* dynamicBranchPrediction1 class implements direct mapping between branch instruction address and saturated counter
in branchTargetBuffer we have 64 rows and 2 columns
index of each row can be obtained using last 6 bit of the instruction address
For a given row
1 column represent counter which can vary from 0 to 4
2 column represent tag
The implementation is quite close to direct mapped caches
if counter is 0, then it means that entry is invalid
This means that instead of having a seperate valid bit, we are using the value of 0 in counter to show the invalid bit
*/
/*
Replacement policy -
if found a new branch instruction that is taken and it is not present in the branch target buffer
Then simply add it into the branch target buffer at required location and make the value of counter 1
*/
/*
Call doDynamicPrediction function at the start of fetch cycle to predict it
call updateBranchTargetBuffer function at the end of execution cycle to update the branch target buffer
*/
class dynamicBranchPrediction1{
private :
int branchTargetBuffer[64][2];
public :
bool doDynamicPrediction(int n){
int i;
int lsb = 0;
int mul = 1;
for(i = 0; i < 6; i++){
if(n%2 == 1){
lsb = lsb + mul;
}
n = (int)(n/2);
mul = mul * 2;
}
int msb = n - lsb;
/* The variable msb is used for tag */
if(branchTargetBuffer[lsb][0] == 0){
return(false);
}else if(branchTargetBuffer[lsb][1] == msb){
if(branchTargetBuffer[lsb][0] < 2){
return(false);
}else{
return(true);
}
}else{
return(false);
}
}
void updateBranchTargetBuffer(bool val, int n){
int i;
int lsb = 0;
int mul = 1;
for(i = 0; i < 6; i++){
if(n%2 == 1){
lsb = lsb + mul;
}
n = (int)(n/2);
mul = mul * 2;
}
int msb = n - lsb;
/* The variable msb is used for tag */
if(val == true){
if(branchTargetBuffer[lsb][1] == msb && branchTargetBuffer[lsb][0] < 3){
branchTargetBuffer[lsb][0]++;
}else{
branchTargetBuffer[lsb][1] = msb;
branchTargetBuffer[lsb][0] = 1;
}
}else{
if(branchTargetBuffer[lsb][1] == msb && branchTargetBuffer[lsb][0] > 0){
branchTargetBuffer[lsb][0]--;
}
}
}
};
/* dynamicBranchPrediction2 class implements 4-way set associative mapping between branch instruction address and saturated counter
in branchTargetBuffer we have 64 rows and 4 columns
index of each row can be obtained using last 6 bit of the instruction address
For a given row each column a seperate branch instruction address identified by tag
A given row and column can have two values using pair (stl)
.first - for storing counter
.second - for storing tag
The implementation is quite close to 4-way set associative caches
if counter is 0, then it means that entry is invalid
This means that instead of having a seperate valid bit, we are using the value of 0 in counter to show the invalid bit
*/
/*
replacement policy -
if found a new branch instruction that is taken and it is not present in the branch target buffer
Then simply add it into the branch target buffer if place is empty
Otherwise, replace that element in the required row which has lowest value of counter
Put the value of counter for newly added element to be 1
*/
/*
Call doDynamicPrediction function at the start of fetch cycle to predict it
call updateBranchTargetBuffer function at the end of execution cycle to update the branch target buffer
*/
class dynamicBranchPrediction2{
private :
pair<int, int> branchTargetBuffer[64][4];
public :
bool doDynamicPrediction(int n){
int i;
int lsb = 0;
int mul = 1;
for(i = 0; i < 6; i++){
if(n%2 == 1){
lsb = lsb + mul;
}
n = (int)(n/2);
mul = mul * 2;
}
int msb = n - lsb;
/* The variable msb is used for tag */
for(i = 0; i < 4; i++){
if(branchTargetBuffer[lsb][i].second == msb){
break;
}
}
if(i == 4){
return(false);
}else if(branchTargetBuffer[lsb][i].first < 2){
return(false);
}else{
return(true);
}
}
void updateBranchTargetBuffer(bool val, int n){
int i;
int lsb = 0;
int mul = 1;
for(i = 0; i < 6; i++){
if(n%2 == 1){
lsb = lsb + mul;
}
n = (int)(n/2);
mul = mul * 2;
}
int msb = n - lsb;
/* The variable msb is used for tag */
for(i = 0; i < 4; i++){
if(branchTargetBuffer[lsb][i].second == msb){
break;
}
}
if(i != 4){
if(val == true && branchTargetBuffer[lsb][i].first < 3){
branchTargetBuffer[lsb][i].first++;
}else if(val == false && branchTargetBuffer[lsb][i].first > 0){
branchTargetBuffer[lsb][i].first--;
}
}else if(val == true){
int j = 0;
for(i = 0; i < 3; i++){
if(branchTargetBuffer[lsb][i].first < branchTargetBuffer[lsb][j].first){
j = i;
}
}
branchTargetBuffer[lsb][j].first = 1;
branchTargetBuffer[lsb][j].second = msb;
}
}
};
class Reservation_Station
{
public:
/* data */
bool isBusy;
int Qj, Qk, Vj, Vk, lat, op, result, instNum, ISSUE_Lat, WRITEBACK_Lat;
bool resultReady;
Reservation_Station(){
isBusy = false;
op = 0;
lat = 0;
result = 0;
resultReady = false;
Qj = 1;
Qk = 1;
Vj = 0;
Vk = 0;
instNum = 100000;
this->ISSUE_Lat = 0;
WRITEBACK_Lat = 0;
}
Reservation_Station(int OP, int RSoperandAvail){
isBusy = false;
op = OP;
lat = 0;
result = 0;
resultReady = false;
Qj = RSoperandAvail;
Qk = RSoperandAvail;
Vj = 0;
Vk = 0;
instNum = 100000;
ISSUE_Lat = 0;
WRITEBACK_Lat = 0;
}
~Reservation_Station();
};
Reservation_Station::~Reservation_Station()
{
}
class Instruction
{
public:
int rd, rs, rt, op, issueClock, executeClockBegin, executeClockEnd, writebackClock, imm;
bool branch_pre, branch_state;
int branch_target;
Instruction(){
rd = 0;
rs = 0;
rt = 0;
op = 0;
issueClock = 0;
executeClockBegin = 0;
executeClockEnd = 0;
writebackClock = 0;
imm=0;
branch_pre=false;
branch_state=false;
branch_target=0;
}
Instruction(int RD, int RS, int RT, int OP, int imm=0){
rd=RD;
rs=RS;
rt=RT;
op=OP;
issueClock=0;
executeClockBegin = 0;
executeClockEnd = 0;
writebackClock = 0;
this->imm=imm;
branch_pre=false;
branch_state=false;
branch_target=0;
}
~Instruction();
};
Instruction::~Instruction()
{
}
class RegisterStatus
{
public:
//bool busy;
int Qi;
RegisterStatus(){
//busy=false;
Qi=0;
}
RegisterStatus(int a){
//busy=false;
Qi=a;
}
~RegisterStatus( );
};
RegisterStatus::~RegisterStatus( )
{
}
class MemoryStatus{
public:
//bool busy;
int Qi;
MemoryStatus(){
//busy=false;
Qi=0;
}
MemoryStatus(int add){
//busy=false;
Qi=add;
}
~MemoryStatus(){}
};
const int Mem_size=64;
vector<int> Memory(Mem_size);
vector<MemoryStatus> MemStatus(Mem_size);
const int Num_ADD_RS=4;
const int Num_MUL_RS=3;
const int Num_DIV_RS=2;
const int Num_AND_RS=4;
const int Num_OR_RS=4;
const int Num_XOR_RS=3;
const int Num_NOT_RS=4;
const int Num_LSL_RS=4;
const int Num_LSR_RS=4;
const int Num_Load_RS=1;
const int Num_Store_RS=1;
const int Num_Branch_RS=1;
const int Tot_RS=Num_ADD_RS+Num_MUL_RS+Num_DIV_RS+Num_AND_RS+Num_OR_RS+Num_XOR_RS+Num_NOT_RS+Num_LSL_RS+Num_LSR_RS+Num_Load_RS+Num_Store_RS+Num_Branch_RS;
const int AddOp=0;
const int SubOp=1;
const int MulOp=2;
const int DivOp=3;
const int ANDop=4;
const int OROp=5;
const int XOROp=6;
const int NOTOp=7;
const int LSLOp=8;
const int LSROp=9;
const int ModOp=10;
const int LoadOp=11;
const int StoreOp=12;
const int BrNEOp=13;
const int AddLat=4;
const int MulLat=16;
const int DivLat=32;
const int ANDLat=2;
const int ORLat=2;
const int XORLat=3;
const int NOTLat=1;
const int LSLLat=4;
const int LSRLat=4;
const int LoadLat=10;
const int StoreLat=10;
const int BranchLat=8;
const int ISSUE_Lat=1;
const int WRITE_BACK_Lat=1;
const int Num_reg_count=13; //F0 to F12, F0 set to ZERO_REG
int clock_=0;
bool Done=false;
bool INSTR_left=true;
int Total_Instr=0, Tot_Write_Back=0;
int currINST_ISSUE=1;
const int ZERO_REG=0;
const int RegStatusEmpty=1000;
const int OperandAvail=1001;
const int OperantINIT=1002;
const int MemoryStatusEmpty=1003;
bool branch_pass=false;
bool branch_taken_predicted=false;
int ADD_beg, ADD_end;
int MUL_beg, MUL_end;
int DIV_beg, DIV_end;
int AND_beg, AND_end;
int OR_beg, OR_end;
int XOR_beg, XOR_end;
int NOT_beg, NOT_end;
int LSL_beg, LSL_end;
int LSR_beg, LSR_end;
int Load_beg, Load_end;
int Store_beg, Store_end;
int Branch_beg, Branch_end;
vector<Instruction> INSTR_vector;
vector<int> Dup_RegStatus(Num_reg_count);
Instruction I_input;
Instruction I_issue;
int Branch_call_address;
int ISSUE(Instruction I, Reservation_Station* RS, vector<RegisterStatus> &RegStatus, vector<int> &Registers, dynamicBranchPrediction1 d1);
void EXECUTE(Reservation_Station* RS, vector<RegisterStatus> &RegStatus, vector<int> &Registers, dynamicBranchPrediction1 d1);
void WRITE_BACK(Reservation_Station* RS, vector<RegisterStatus> &RegStatus, vector<int> &Registers, dynamicBranchPrediction1 d1);
void format_INSTR(string str, Instruction &I);
void Print_RS(Reservation_Station*);
void Print_INSTR();
void Destroy_Waiting(Reservation_Station* RS, vector<RegisterStatus> &RegStatus, vector<int> &Registers);
void Update_Waiting(Reservation_Station* RS, vector<RegisterStatus> &RegStatus, vector<int> &Registers);
int main(){
freopen("input_br.txt", "r", stdin);
freopen("output_br.txt", "w", stdout);
INSTR_vector.clear();
string str;
vector<int> Registers(Num_reg_count);
//INITIAL VALUES OF REGISTERS
for(int i=0; i<Num_reg_count ; ++i){
cin>>Registers[i];
}
Registers[0]=ZERO_REG;
vector<RegisterStatus> RegStatus(Num_reg_count);
for(int i=0; i<Num_reg_count; ++i){
RegStatus[i]=RegisterStatus(RegStatusEmpty);
}
//Reservation Stations
Reservation_Station* RS=new Reservation_Station[Tot_RS];
ADD_beg=0;
for(int i=0; i<Num_ADD_RS; ++i){
RS[i]=Reservation_Station(AddOp, OperantINIT);
}
int sum=Num_ADD_RS;
ADD_end=sum;
MUL_beg=sum;
for(int i=0; i<Num_MUL_RS; ++i){
RS[sum+i]=Reservation_Station(MulOp, OperantINIT);
}
sum+=Num_MUL_RS;
MUL_end=sum;
DIV_beg=sum;
for(int i=0; i<Num_DIV_RS; ++i){
RS[sum+i]=Reservation_Station(DivOp, OperantINIT);
}
sum+=Num_DIV_RS;
DIV_end=sum;
AND_beg=sum;
for(int i=0; i<Num_AND_RS; ++i){
RS[sum+i]=Reservation_Station(ANDop, OperantINIT);
}
sum+=Num_AND_RS;
AND_end=sum;
OR_beg=sum;
for(int i=0; i<Num_OR_RS; i++){
RS[sum+i]=Reservation_Station(OROp, OperantINIT);
}
sum+=Num_OR_RS;
OR_end=sum;
XOR_beg=sum;
for(int i=0; i<Num_XOR_RS; ++i){
RS[sum+i]=Reservation_Station(XOROp, OperantINIT);
}
sum+=Num_XOR_RS;
XOR_end=sum;
NOT_beg=sum;
for(int i=0; i<Num_NOT_RS; ++i){
RS[sum+i]=Reservation_Station(NOTOp, OperantINIT);
}
sum+=Num_NOT_RS;
NOT_end=sum;
LSL_beg=sum;
for(int i=0; i<Num_LSL_RS; ++i){
RS[sum+i]=Reservation_Station(LSLOp, OperantINIT);
}
sum+=Num_LSL_RS;
LSL_end=sum;
LSR_beg=sum;
for(int i=0; i<Num_LSR_RS; ++i){
RS[sum+i]=Reservation_Station(LSROp, OperantINIT);
}
sum+=Num_LSR_RS;
LSR_end=sum;
Load_beg=sum;
for(int i=0; i<Num_Load_RS; ++i){
RS[sum+i]=Reservation_Station(LoadOp, OperantINIT);
}
sum+=Num_Load_RS;
Load_end=sum;
Store_beg=sum;
for(int i=0; i<Num_Store_RS; ++i){
RS[sum+i]=Reservation_Station(StoreOp, OperantINIT);
}
sum+=Num_Store_RS;
Store_end=sum;
Branch_beg=sum;
for(int i=0; i<Num_Branch_RS; ++i){
RS[sum+i]=Reservation_Station(BrNEOp, OperantINIT);
}
Branch_end=sum+Num_Branch_RS;
for(int i=0; i<Mem_size; ++i){
MemStatus[i]=MemoryStatus(MemoryStatusEmpty);
}
cin>>Total_Instr;
cin.ignore();
for(int i=0; i<Total_Instr;){
if(getline(cin, str)){
format_INSTR(str, I_input);
INSTR_vector.push_back(I_input);
i++;
}
}
dynamicBranchPrediction1 d1;
cout<<"*****************************************\n";
cout<<"INTIAL_STATUS\n";
cout<<"Registers: ";
for(int i=0; i<Num_reg_count; ++i){
cout<<Registers[i]<<" ";
}
cout<<endl;
cout<<"Memory: \n";
for(int i=0; i<Mem_size; ++i){
cout<<i<<": "<<Memory[i]<<"| ";
}
cout<<endl;
do{
clock_++;
if(currINST_ISSUE<=Total_Instr){
I_issue=INSTR_vector[currINST_ISSUE-1];
if(ISSUE(I_issue, RS, RegStatus, Registers, d1)==2){
currINST_ISSUE++;
}
}
EXECUTE(RS, RegStatus, Registers, d1);
WRITE_BACK(RS, RegStatus, Registers, d1);
if((currINST_ISSUE==(Total_Instr+1) && INSTR_vector[Total_Instr-1].writebackClock!=0)){
Done=true;
}
//Print_RS(RS);
//Print_INSTR();
}while(!Done);
cout<<"*****************************************\n";
cout<<"Clock: "<<clock_<<endl;
Print_INSTR();
cout<<"*****************************************\n";
cout<<"FINAL_STATUS\n";
cout<<"Registers: ";
for(int i=0; i<Num_reg_count; ++i){
cout<<Registers[i]<<" ";
}
cout<<endl;
cout<<"Memory: \n";
for(int i=0; i<Mem_size; ++i){
cout<<i<<": "<<Memory[i]<<"| ";
}
cout<<"\n*****************************************\n";
return 0;
}
int ISSUE(Instruction I, Reservation_Station* RS, vector<RegisterStatus> &RegStatus, vector<int> &Registers, dynamicBranchPrediction1 d1){
int r=I.op;
bool rsAvail=false;
switch (r)
{
case AddOp:
case SubOp:
for(int i=ADD_beg; i<ADD_end; ++i){
if(!RS[i].isBusy){
r=i;
RS[i].op=I.op;
rsAvail=true;
break;
}
}
if(!rsAvail)
return 1;
break;
case MulOp:
for(int i=MUL_beg; i<MUL_end; ++i){
if(!RS[i].isBusy){
r=i;
RS[i].op=I.op;
rsAvail=true;
break;
}
}
if(!rsAvail)
return 1;
break;
case DivOp:
case ModOp:
for(int i=DIV_beg; i<DIV_end; ++i){
if(!RS[i].isBusy){
r=i;
RS[i].op=I.op;
rsAvail=true;
break;
}
}
if(!rsAvail)
return 1;
break;
case ANDop:
for(int i=AND_beg; i<AND_end; ++i){
if(!RS[i].isBusy){
r=i;
RS[i].op=I.op;
rsAvail=true;
break;
}
}
if(!rsAvail)
return 1;
break;
case OROp:
for(int i=OR_beg; i<OR_end; ++i){
if(!RS[i].isBusy){
r=i;
RS[i].op=I.op;
rsAvail=true;
break;
}
}
if(!rsAvail)
return 1;
break;
case XOROp:
for(int i=XOR_beg; i<XOR_beg; ++i){
if(!RS[i].isBusy){
r=i;
RS[i].op=I.op;
rsAvail=true;
break;
}
}
if(!rsAvail)
return 1;
break;
case NOTOp:
for(int i=NOT_beg; i<NOT_end; ++i){
if(!RS[i].isBusy){
r=i;
RS[i].op=I.op;
rsAvail=true;
break;
}
}
if(!rsAvail)
return 1;
break;
case LSLOp:
for(int i=LSL_beg; i<LSL_end; ++i){
if(!RS[i].isBusy){
r=i;
RS[i].op=I.op;
rsAvail=true;
break;
}
}
if(!rsAvail)
return 1;
break;
case LSROp:
for(int i=LSR_beg; i<LSR_end; ++i){
if(!RS[i].isBusy){
r=i;
RS[i].op=I.op;
rsAvail=true;
break;
}
}
if(!rsAvail)
return 1;
break;
case LoadOp:
for(int i=Load_beg; i<Load_end; ++i){
if(!RS[i].isBusy){
r=i;
RS[i].op=I.op;
rsAvail=true;
break;
}
}
if(!rsAvail)
return 1;
if(MemStatus[INSTR_vector[currINST_ISSUE-1].rs + INSTR_vector[currINST_ISSUE-1].rt].Qi==MemoryStatusEmpty){
RS[r].Qk=OperandAvail;
}
else{
RS[r].Qk=MemStatus[INSTR_vector[currINST_ISSUE-1].rs + INSTR_vector[currINST_ISSUE-1].rt].Qi;
}
RS[r].Vj=INSTR_vector[currINST_ISSUE-1].rs;
RS[r].Vk=INSTR_vector[currINST_ISSUE-1].rt;
RS[r].Qj=OperandAvail;
RS[r].isBusy=true;
RS[r].ISSUE_Lat=0;
RS[r].instNum=currINST_ISSUE-1;
INSTR_vector[currINST_ISSUE-1].issueClock=clock_;
RegStatus[INSTR_vector[currINST_ISSUE-1].rd].Qi=r;
if(branch_pass) INSTR_vector[currINST_ISSUE-1].branch_pre=true;
return 2;
case StoreOp:
for(int i=Store_beg; i<Store_end; ++i){
if(!RS[i].isBusy){
r=i;
RS[i].op=I.op;
rsAvail=true;
break;
}
}
if(!rsAvail)
return 1;
//Dest=rs+rt
//Source=rd
if(RegStatus[INSTR_vector[currINST_ISSUE-1].rd].Qi==RegStatusEmpty){
RS[r].Vj=Registers[INSTR_vector[currINST_ISSUE-1].rd];
RS[r].Qj=OperandAvail;
}
else{
RS[r].Qj=RegStatus[INSTR_vector[currINST_ISSUE-1].rd].Qi;
}
RS[r].Vk=INSTR_vector[currINST_ISSUE-1].rs+INSTR_vector[currINST_ISSUE-1].rt;
RS[r].Qk=OperandAvail;
RS[r].isBusy=true;
RS[r].ISSUE_Lat=0;
RS[r].instNum=currINST_ISSUE-1;
INSTR_vector[currINST_ISSUE-1].issueClock=clock_;
MemStatus[INSTR_vector[currINST_ISSUE-1].rs+ INSTR_vector[currINST_ISSUE-1].rt].Qi=r;
if(branch_pass) INSTR_vector[currINST_ISSUE-1].branch_pre=true;
return 2;
case BrNEOp:
if(branch_pass){
return 1;
}
for(int i=Branch_beg; i<Branch_end; ++i){
if(!RS[i].isBusy){
r=i;
RS[i].op=I.op;
rsAvail=true;
break;
}
}
if(!rsAvail) return 1;
branch_taken_predicted=d1.doDynamicPrediction(4*(currINST_ISSUE)-1);
branch_pass=true;
Branch_call_address=currINST_ISSUE;
for(int i=0; i<Num_reg_count; ++i){
Dup_RegStatus[i]=RegStatus[i].Qi;
}
break;
default:
break;
}
if(RegStatus[INSTR_vector[currINST_ISSUE-1].rs].Qi == RegStatusEmpty){
RS[r].Vj = Registers[INSTR_vector[currINST_ISSUE-1].rs];
RS[r].Qj = OperandAvail;
}
else{
RS[r].Qj = RegStatus[INSTR_vector[currINST_ISSUE-1].rs].Qi;
}
if(INSTR_vector[currINST_ISSUE-1].imm!=0){
RS[r].Vk=INSTR_vector[currINST_ISSUE-1].imm;
RS[r].Qk=OperandAvail;
}
else if(RegStatus[INSTR_vector[currINST_ISSUE-1].rt].Qi == RegStatusEmpty){
RS[r].Vk = Registers[INSTR_vector[currINST_ISSUE-1].rt];
RS[r].Qk = OperandAvail;
}
else{
RS[r].Qk = RegStatus[INSTR_vector[currINST_ISSUE-1].rt].Qi;
}
RS[r].isBusy=true;
RS[r].ISSUE_Lat=0;
RS[r].instNum=currINST_ISSUE-1;
INSTR_vector[currINST_ISSUE-1].issueClock=clock_;
if(branch_pass) INSTR_vector[currINST_ISSUE-1].branch_pre=true;
if(!INSTR_vector[currINST_ISSUE-1].branch_state) RegStatus[INSTR_vector[currINST_ISSUE-1].rd].Qi=r;
else if(branch_taken_predicted){
currINST_ISSUE=INSTR_vector[currINST_ISSUE-1].branch_target-1;
}
return 2;
}
void EXECUTE(Reservation_Station* RS, vector<RegisterStatus> &RegStatus, vector<int> &Registers, dynamicBranchPrediction1 d1){
bool startExec=false;
for(int i=0; i<Tot_RS; ++i){
startExec=false;
if(RS[i].isBusy){
if(RS[i].ISSUE_Lat>=ISSUE_Lat){
if(RS[i].Qj==OperandAvail && RS[i].Qk==OperandAvail){
if(INSTR_vector[RS[i].instNum].executeClockBegin==0){
INSTR_vector[RS[i].instNum].executeClockBegin=clock_;
}
switch (RS[i].op)
{
case AddOp:
if(RS[i].lat==AddLat){
RS[i].result=RS[i].Vj+RS[i].Vk;
startExec=true;
}
break;
case SubOp:
if(RS[i].lat==AddLat){
RS[i].result=RS[i].Vj-RS[i].Vk;
startExec=true;
}
break;
case MulOp:
if(RS[i].lat==MulLat){
RS[i].result=RS[i].Vj*RS[i].Vk;
startExec=true;
}
break;
case DivOp:
if(RS[i].lat==DivLat){
RS[i].result=RS[i].Vj/RS[i].Vk;
startExec=true;
}
break;
case ANDop:
if(RS[i].lat==ANDLat){
RS[i].result=(RS[i].Vj&RS[i].Vk);
startExec=true;
}
break;
case OROp:
if(RS[i].lat==ORLat){
RS[i].result=(RS[i].Vj|RS[i].Vk);
startExec=true;
}
break;
case XOROp:
if(RS[i].lat==XORLat){
RS[i].result=(RS[i].Vj^RS[i].Vk);
startExec=true;
}
break;
case NOTOp:
if(RS[i].lat==NOTLat){
RS[i].result=!(RS[i].Vj);
startExec=true;
}
break;
case LSLOp:
if(RS[i].lat==LSLLat){
RS[i].result=RS[i].Vj<<RS[i].Vk;
startExec=true;
}
break;
case LSROp:
if(RS[i].lat==LSRLat){
RS[i].result=RS[i].Vj>>RS[i].Vk;
startExec=true;
}
break;
case ModOp:
if(RS[i].lat==DivLat){
RS[i].result=RS[i].Vj%RS[i].Vk;
startExec=true;
}
break;
case LoadOp:
if(RS[i].lat==LoadLat){
RS[i].result=Memory[RS[i].Vj+RS[i].Vk];
startExec=true;
}
break;
case StoreOp:
if(RS[i].lat==StoreLat){
RS[i].result=Registers[RS[i].Vj];
startExec=true;
}
break;
case BrNEOp:
if(RS[i].lat==BranchLat){
RS[i].result=(RS[i].Vj != RS[i].Vk);
d1.updateBranchTargetBuffer(Branch_call_address, RS[i].result==branch_taken_predicted);
cout<<"Result of Branch Prediction of Instruction:"<<RS[i].instNum<<"------- Predicted: "<<branch_taken_predicted<<" Outcome: "<<RS[i].result<<endl;
if(branch_taken_predicted!=RS[i].result){
Destroy_Waiting(RS, RegStatus, Registers);
currINST_ISSUE=(RS[i].result)?(INSTR_vector[RS[i].instNum].branch_target):(Branch_call_address+1);
}
else{
Update_Waiting(RS, RegStatus, Registers);
}
branch_pass=false;
startExec=true;
}
break;
default:
break;
}
if(startExec){
RS[i].resultReady=true;
RS[i].lat=0;
RS[i].ISSUE_Lat=0;
INSTR_vector[RS[i].instNum].executeClockEnd=clock_;
}
else{
RS[i].lat++;
}
}
}
else{
RS[i].ISSUE_Lat++;
}
}
}
}
void WRITE_BACK(Reservation_Station* RS, vector<RegisterStatus> &RegStatus, vector<int> &Registers, dynamicBranchPrediction1 d1){
for(int i=0; i<Tot_RS; ++i){
if(RS[i].resultReady){
if(INSTR_vector[RS[i].instNum].branch_pre) continue;
if(RS[i].WRITEBACK_Lat==WRITE_BACK_Lat){
if(INSTR_vector[RS[i].instNum].writebackClock==0){
INSTR_vector[RS[i].instNum].writebackClock=clock_;
}
if(RS[i].op==StoreOp){
for(int j=0; j<Mem_size; ++j){
if(MemStatus[j].Qi==i){
Memory[j]=RS[i].result;
MemStatus[j]=MemoryStatusEmpty;
}
}
for(int j=Load_beg; j<Load_end; ++j){
if(RS[j].Qk==i){
RS[j].Qk=OperandAvail;
}
}
}
else if(RS[i].op!=BrNEOp){
for(int j=0; j<Num_reg_count; ++j){
if(RegStatus[j].Qi==i){
Registers[j]=RS[i].result;
RegStatus[j].Qi=RegStatusEmpty;
}
}
for(int j=0; j<Tot_RS; ++j){
if(RS[j].Qj==i){
RS[j].Vj=RS[i].result;
RS[j].Qj=OperandAvail;
}
if(RS[j].Qk==i){
RS[j].Qk=OperandAvail;
RS[j].Vk=RS[i].result;
}
}
}
RS[i].resultReady=false;
RS[i].isBusy=false;
RS[i].Qj=OperantINIT;
RS[i].Qk=OperantINIT;
RS[i].Vj=0;
RS[i].Vk=0;
RS[i].WRITEBACK_Lat=0;
Tot_Write_Back++;
}
else{
RS[i].WRITEBACK_Lat++;
}
}
}
}
void format_INSTR(string s, Instruction &I){
string opcode="";
int i=0;
for(; i<s.length(); ++i){
if(s[i]!=' '){
opcode+=s[i];
}
else{
break;
}
}
int OP=0;
if(opcode=="ADD"){
OP=0;
}
else if(opcode=="SUB"){
OP=1;
}
else if(opcode=="MUL"){
OP=2;
}
else if(opcode=="DIV"){
OP=3;
}
else if(opcode=="AND"){
OP=4;
}
else if(opcode=="OR"){
OP=5;
}
else if(opcode=="XOR"){
OP=6;
}
else if(opcode=="NOT"){
OP=7;
}
else if(opcode=="LSL"){
OP=8;
}
else if(opcode=="LSR"){
OP=9;
}
else if(opcode=="MOD"){
OP=ModOp;
}
else if(opcode=="LOAD" || opcode=="STORE"){
OP=(opcode=="LOAD")?(LoadOp):(StoreOp);
//rd, int, int