Repository navigation
Expand file tree
/
Copy pathPipeline.java
More file actions
601 lines (562 loc) · 21.3 KB
/
Copy pathPipeline.java
File metadata and controls
601 lines (562 loc) · 21.3 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
/**
*
* @author Priyank Kashyap
* NCSU
*/
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.io.*;
import java.util.Collections;
public class Pipeline {
private final int numberOfARFRegs=67;
private final int mWidth;
private final int mROB_Size;
private final int mIQ_Size;
private double mSimulatorTime;
private double mDynamicInstrCount;
private int mElementsInROB;
private boolean trace_file_empty;
private BufferedReader br;
private boolean mPipelineEmpty;
private Map<Integer,rename_table_entry> rename_map_table;
private reorder_buffer_entry [] mROB;
private int mROB_Head, mROB_Tail;
private ArrayList <Instruction> DE,RN,RR,DI, IQ, execute_list, WB, RT;
public Pipeline(proc_params cpu_config, String trace_file) throws FileNotFoundException{
//Initalize internal variables
mWidth = (int) cpu_config.width;
mROB_Size = (int) cpu_config.rob_size;
mIQ_Size = (int) cpu_config.iq_size;
mSimulatorTime = 0;
mDynamicInstrCount = 0;
trace_file_empty = false;
br = new BufferedReader(new FileReader(trace_file));
DE = new ArrayList<Instruction>();
RN = new ArrayList<Instruction>();
RR = new ArrayList<Instruction>();
DI = new ArrayList<Instruction>();
IQ = new ArrayList<Instruction>();
WB = new ArrayList<Instruction>();
RT = new ArrayList<Instruction>();
execute_list = new ArrayList<Instruction>();
//ROB initialization
reorder_buffer_entry empty_rob = new reorder_buffer_entry();
empty_rob.dst =-55;
empty_rob.valid=0;
empty_rob.ready_bit=0;
empty_rob.instr_number=-1;
mROB = new reorder_buffer_entry[mROB_Size];
for(int i =0; i< mROB_Size ; i++){
mROB[i]=empty_rob;
}
mROB_Head =-1;
mROB_Tail=-1;
//RMT Initialization
rename_map_table = new HashMap();
for(int i = 0;i<numberOfARFRegs; i++){
rename_map_table.put(Integer.valueOf(i),null);
}
}
//-------------Pipeline Functions-------------------------------------//
public void fetch() throws IOException{
if(DE.size()==0){
for (int i=0; i<mWidth; i++){
String line=br.readLine();
Instruction temp = new Instruction();
int time_in_fetch=1;
int op_type ,dest, src1, src2; // Variable read from input file
if(line != null){
String [] split = line.split("\\s+"); // split line at whitespace
long pc = Long.parseLong(split[0], 16); // gets address from String split and converts to long. parseLong uses base 16
temp.instr_number = (int)mDynamicInstrCount;
temp.op_type = Integer.parseInt(split[1], 10);
temp.dst_reg = Integer.parseInt(split[2], 10);
temp.src_r1_reg = Integer.parseInt(split[3], 10);
temp.src_r2_reg = Integer.parseInt(split[4], 10);
temp.fe_time = (int)mSimulatorTime;
DE.add(temp);
mDynamicInstrCount++;
}
else{
trace_file_empty = true;
}
}
}
}
public void decode(){
if(DE.size()>0){
for(Instruction e: DE){
if(e != null){
if(e.de_time == 0){
e.de_time = (int) mSimulatorTime;
}
}
}
if(RN.size() == 0){
int mRemove = mWidth;
if(DE.size() < mWidth) mRemove = DE.size();
for(int i=0; i < mRemove ; i++){
Instruction temp = DE.get(i);
RN.add(temp);
}
for(int i=mRemove -1; i >=0; i--){
DE.remove(i);
}
}
else{
return;
}
}
}
public void rename(){
if(RN.size() > 0){
for(Instruction e: RN){
if(e != null){
if(e.rn_time == 0){
e.rn_time = (int) mSimulatorTime;
}
}
}
if((RR.size()== 0) && (mElementsInROB + RN.size() <= mROB_Size)){
int mRemove = mWidth;
if(RN.size() < mWidth) mRemove = RN.size();
for(int i =0; i < mRemove ; i++){
Instruction temp = RN.get(i);
allocateROB_arr(temp);
RR.add(temp);
}
for(int i = mRemove-1; i >= 0; i--){
RN.remove(i);
}
}
}
else{
return;
}
}
public void register_read(){
if(RR.size() > 0){
for(Instruction e: RR){
if(e != null){
if(e.rr_time == 0){
e.rr_time = (int) mSimulatorTime;
}
}
}
if(DI.size() == 0){
int mRemove = mWidth;
if(RR.size() < mWidth) mRemove = RR.size();
for(int i =0; i < mRemove ; i++){
Instruction temp = RR.get(i);
instructionReady(temp);
DI.add(temp);
}
for(int i = mRemove-1; i >=0; i--){
RR.remove(i);
}
}
}
else{
return;
}
}
public void dispatch(){
if(DI.size()>0){
for(Instruction e: DI){
if(e != null){
if(e.di_time == 0){
e.di_time = (int) mSimulatorTime;
}
}
}
if(DI.size() <= (mIQ_Size- IQ.size())){
int removal = mWidth;
if(DI.size() < mWidth) removal=DI.size();
for(int i= 0; i < removal; i++){
Instruction temp= DI.get(i);
IQ.add(temp);
}
for(int i=removal-1; i >= 0; i--){
DI.remove(i);
}
}
}
else{
return;
}
}
public void issue(){
int possibleExec = mWidth;
int currentExec = 0;
if(IQ.size()>0){
for(Instruction e: IQ){
if(e != null){
if(e.is_time == 0){
e.is_time = (int) mSimulatorTime;
}
}
}
for(int i = 0; i <IQ.size()-1; i++){
int oldest_instr = i;
for(int j=i+1; j< IQ.size();j++){
if(IQ.get(j).instr_number < IQ.get(oldest_instr).instr_number){
oldest_instr = j;
}
}
Instruction temp_1 = IQ.get(i);
Instruction temp_2 = IQ.get(oldest_instr);
IQ.set(oldest_instr, temp_1);
IQ.set(i, temp_2);
}
int removal = mIQ_Size;
if(IQ.size() < mIQ_Size) removal =IQ.size();
for(int i = 0; i <removal; i++){
Instruction temp= IQ.get(i);
if(temp.rn_r1_rdy==1 && temp.rn_r2_rdy==1){
if(currentExec == possibleExec){
break;
}
IQ.set(i, null);
execute_list.add(temp);
currentExec++;
}
}
IQ.removeAll(Collections.singleton(null));
}
}
public void execute(){
if(execute_list.size()>0){
for(Instruction i:execute_list){
if(i != null){
if(i.ex_time == 0){
i.ex_time = (int) mSimulatorTime;
if(i.op_type==0){
i.stop_execute_time=i.ex_time;
}
else if(i.op_type==1){
i.stop_execute_time=i.ex_time+1;
}
else if(i.op_type==2){
i.stop_execute_time=i.ex_time+4;
}
}
}
}
int removal = mWidth*5;
if(execute_list.size() < (mWidth*5)) removal = execute_list.size();
for(int i = 0; i <removal; i++){
Instruction temp= execute_list.get(i);
if(temp.stop_execute_time == mSimulatorTime){
execute_list.set(i, null);
wakeup(temp);
WB.add(temp);
}
}
execute_list.removeAll(Collections.singleton(null));
}
}
public void writeback(){
if(WB.size()> 0){
for(Instruction e: WB){
if(e.wb_time == 0){
e.wb_time = (int) mSimulatorTime;
}
}
int removal = mWidth*5;
if(WB.size() < removal) removal = WB.size();
for(int i=0; i < removal; i++){
Instruction temp = WB.get(i);
set_rob_ready(temp);
RT.add(temp);
}
for(int j = removal -1; j >=0; j--){
WB.remove(j);
}
}
}
public void retire(){
int currentExec = 0;
if(RT.size() > 0){
for(Instruction e : RT){
if(e != null){
if(e.rt_time == 0){
e.rt_time = (int) mSimulatorTime;
}
}
}
for(int i = 0; i <RT.size()-1; i++){
int min = i;
for(int j=i+1; j<RT.size();j++){
if(RT.get(j).instr_number < RT.get(min).instr_number){
min = j;
// System.out.println("i :"+ i + " oldest_instr: " + oldest_instr);
}
}
Instruction temp_1 = RT.get(i);
Instruction temp_2 = RT.get(min);
RT.set(min, temp_1);
RT.set(i, temp_2);
}
//loop to move to next stage
int removal = mWidth;
if(RT.size() < mWidth)
removal=RT.size();
for(int i=0; i < removal; i++){
Instruction toRetire = RT.get(i);
if(toRetire != null){
int status = remove_rob(toRetire);
// System.out.println("Instr: "+ toRetire.instr_number + " remove_rob_status: "+ status);
if(status < 0){
RT.removeAll(Collections.singleton(null));
return;
}
else {
if(currentExec == mWidth){
return;
}
RT.set(i,null);
if(toRetire != null) {
if(mSimulatorTime == toRetire.rt_time) toRetire.finish_time = 1;
else toRetire.finish_time = (int) (mSimulatorTime - toRetire.rt_time)+1;
toRetire.display_time();
}
currentExec++;
}
}
}
RT.removeAll(Collections.singleton(null));
}
}
//-------------Control Methods--------------------------------------//
public void advanceTime(){
mSimulatorTime++;
}
public boolean advanceCycle(){
return (mPipelineEmpty && trace_file_empty);
}
public void checkIfPipelineEmpty(){
if(DE.size() ==0 && RN.size()==0 && RR.size()==0 && DI.size() ==0 && IQ.size()==0 && execute_list.size()==0 && WB.size()==0 && RT.size() == 0){
mPipelineEmpty=true;
}
else{
mPipelineEmpty=false;
}
}
//------------------Display Methods--------------------------//
public void displayConfiguration(){
System.out.printf("# === Processor Configuration ===\n");
System.out.printf("# ROB_SIZE = %d\n", mROB_Size);
System.out.printf("# IQ_SIZE = %d\n", mIQ_Size);
System.out.printf("# WIDTH = %d\n", mWidth);
}
public void displaySimulationResults(){
double ipc = (mDynamicInstrCount/mSimulatorTime);
System.out.printf("# === Simulation Results ========\n");
System.out.printf("# Dynamic Instruction Count = %d\n", (int)mDynamicInstrCount);
System.out.printf("# Cycles = %d\n", (int)mSimulatorTime);
System.out.printf("# Instructions Per Cycle (IPC) = %s\n", String.format("%.02f",ipc));
//System.out.println("# Instructions Per Cycle (IPC) = "+ ipc);
}
//-----------------ROB Methods-------------------------------//
public void allocateROB_arr(Instruction addToROB){
reorder_buffer_entry rob_entry = new reorder_buffer_entry();
rob_entry.ready_bit = 0;
rob_entry.instr_number = addToROB.instr_number;
rob_entry.valid = 1;
if(addToROB.dst_reg != -1){
//Add to the queue
rob_entry.dst = addToROB.dst_reg; //modify the dst for the instr
enQueue(rob_entry); //To the tail add the instr
addToROB.rob_dest=mROB_Tail; //instrs knows where in the ROB to look
rename_instruction_src(addToROB); //rename the instr src based on rmt
rename_table_entry rmt_enter = new rename_table_entry(); //Add to RMT
rmt_enter.valid_bit = 1; //create a rmt entry for the dest reg
rmt_enter.rob_tag = mROB_Tail;
rmt_enter.instr_number = rob_entry.instr_number;
rename_map_table.put(Integer.valueOf(rob_entry.dst),rmt_enter);
rename_instruction_dst(addToROB);
}
else{
rob_entry.dst = -1;
enQueue(rob_entry); //To the tail add the instr
addToROB.rob_dest=mROB_Tail; //instrs knows where in the ROB to look
mROB[mROB_Tail]=(rob_entry);
rename_instruction_src(addToROB);
}
}
private void enQueue(reorder_buffer_entry entry){
//ROB is full
if ((mROB_Head == 0 && mROB_Tail == mROB_Size-1) || (mROB_Tail == (mROB_Head-1)%(mROB_Size-1))){
return;
}
else if (mROB_Head == -1) { //insert First Element
mElementsInROB = 1;
mROB_Head = 0;
mROB_Tail = 0;
mROB[mROB_Tail] = entry;
}
//going circular
else if (mROB_Tail == mROB_Size-1 && mROB_Head != 0){
mROB_Tail = 0;
mROB[mROB_Tail] = entry;
mElementsInROB++;
}
else {
mROB_Tail++;
mROB[mROB_Tail] = entry;
mElementsInROB++;
}
}
public void rename_instruction_src(Instruction renameInstr){
rename_table_entry src_renaming;
//R1 renaming
if(renameInstr.src_r1_reg != -1){
src_renaming = rename_map_table.get(Integer.valueOf(renameInstr.src_r1_reg));
if(src_renaming != null){
if(src_renaming.valid_bit == 1){ //Valid entry
renameInstr.rn_r1_reg = src_renaming.rob_tag;
renameInstr.rn_r1_rdy = mROB[src_renaming.rob_tag].ready_bit;
}
else{
renameInstr.rn_r1_reg = -55; //Since the RMT didn't have it therefore ARF has most recent value, 55 denotes that
}
}
else{
renameInstr.rn_r1_reg = -55; //No entry in the RMT, value from ARF
}
}
else{
renameInstr.rn_r1_reg = -1; //No register, immediate value
}
//R2 renaming
if(renameInstr.src_r2_reg != -1){
src_renaming = rename_map_table.get(Integer.valueOf(renameInstr.src_r2_reg));
if(src_renaming != null){
if(src_renaming.valid_bit == 1){
renameInstr.rn_r2_reg = src_renaming.rob_tag;
renameInstr.rn_r2_rdy = mROB[src_renaming.rob_tag].ready_bit;
}
else{
renameInstr.rn_r2_reg = -55; //Since the RMT didn't have it therefore ARF has most recent value, 55 denotes that
}
}
else{
renameInstr.rn_r2_reg = -55; //Since the RMT didn't have it therefore ARF has most recent value, 55 denotes that
}
}
else{
renameInstr.rn_r2_reg = -1; //No register, immediate value
}
}
public void rename_instruction_dst(Instruction renameInstr){
//DST renanme
rename_table_entry src_renaming;
if(renameInstr.dst_reg != -1){
src_renaming = rename_map_table.get(Integer.valueOf(renameInstr.dst_reg));
if(src_renaming != null){
if(src_renaming.valid_bit == 1){
renameInstr.rn_dst_reg = src_renaming.rob_tag;
}
else{
renameInstr.rn_dst_reg = src_renaming.rob_tag;
}
}
}
else{
renameInstr.rn_dst_reg = -1;
}
}
public void instructionReady(Instruction readyInstr){
if( readyInstr.rn_r1_reg ==-1 || readyInstr.rn_r1_reg ==-55){ //both immediate values or both arf values
readyInstr.rn_r1_rdy = 1;
}
else{
if(readyInstr.rn_r1_rdy ==0 ){
readyInstr.rn_r1_rdy = mROB[readyInstr.rn_r1_reg].ready_bit;
}
}
if( readyInstr.rn_r2_reg ==-1 || readyInstr.rn_r2_reg ==-55){ //both immediate values or both arf values
readyInstr.rn_r2_rdy = 1;
}
else{
if(readyInstr.rn_r2_rdy ==0 ){
readyInstr.rn_r2_rdy = mROB[readyInstr.rn_r2_reg].ready_bit;
}
}
}
public void wakeup(Instruction execInstr){
if(execInstr.dst_reg != -1){
for(Instruction e: RR){
if(e.rn_r1_reg==execInstr.rob_dest){
e.rn_r1_rdy =1;
}
if(e.rn_r2_reg==execInstr.rob_dest){
e.rn_r2_rdy =1;
}
}
for(Instruction e: DI){
if(e.rn_r1_reg == execInstr.rob_dest){
e.rn_r1_rdy =1;
}
if(e.rn_r2_reg == execInstr.rob_dest){
e.rn_r2_rdy =1;
}
}
for(Instruction e: IQ){
if(e!=null){
if(e.rn_r1_reg==execInstr.rob_dest){
e.rn_r1_rdy =1;
}
if(e.rn_r2_reg==execInstr.rob_dest){
e.rn_r2_rdy =1;
}
}
}
}
}
private void set_rob_ready(Instruction completedInstruction){
mROB[completedInstruction.rob_dest].ready_bit = 1;
}
private void deQueue(){
//empty queue
if (mROB_Head == -1) {
}
//invalidate the rob entry
mElementsInROB--;
reorder_buffer_entry data = mROB[mROB_Head];
data.valid=0;
data.ready_bit=0;
if (mROB_Head == mROB_Tail){ //empty
mROB_Head = -1;
mROB_Tail = -1;
}
else if (mROB_Head == mROB_Size-1) //loop
mROB_Head = 0;
else
mROB_Head++;
}
private int remove_rob(Instruction toRetire){
reorder_buffer_entry headOfROB = mROB[mROB_Head];
if(headOfROB.ready_bit == 1 && toRetire.instr_number == headOfROB.instr_number){ //instr is ready to retire
if(toRetire.dst_reg != -1){
rename_table_entry rmt_enter_validation = rename_map_table.get(headOfROB.dst);
if(rmt_enter_validation.instr_number == headOfROB.instr_number){
rmt_enter_validation.valid_bit = 0;
}
}
for(Instruction e: RR){
if(e.rn_r1_reg==toRetire.rob_dest){
e.rn_r1_rdy =1;
}
if(e.rn_r2_reg==toRetire.rob_dest){
e.rn_r2_rdy =1;
}
}
deQueue();
return 1;
}
return -1;
}
}