-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe.c
More file actions
714 lines (620 loc) · 22.3 KB
/
pipe.c
File metadata and controls
714 lines (620 loc) · 22.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
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
#include "pipe.h"
#include "shell.h"
#include "mips.h"
#include "cache.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
//#define DEBUG
/* debug */
void print_op(Pipe_Op *op)
{
if (op)
printf("OP (PC=%08x inst=%08x) src1=R%d (%08x) src2=R%d (%08x) dst=R%d valid %d (%08x) br=%d taken=%d dest=%08x mem=%d addr=%08x\n",
op->pc, op->instruction, op->reg_src1, op->reg_src1_value, op->reg_src2, op->reg_src2_value, op->reg_dst, op->reg_dst_value_ready,
op->reg_dst_value, op->is_branch, op->branch_taken, op->branch_dest, op->is_mem, op->mem_addr);
else
printf("(null)\n");
}
/* global pipeline state */
Pipe_State pipe;
void pipe_init()
{ //freopen("testout_cache.txt","wt+",stdout);
init_cache();
memset(&pipe, 0, sizeof(Pipe_State));
pipe.PC = 0x00400000;
}
void pipe_cycle()
{
#ifdef DEBUG
printf("\n\n----\n\nPIPELINE:\n");
printf("DCODE: "); print_op(pipe.decode_op);
printf("EXEC : "); print_op(pipe.execute_op);
printf("MEM : "); print_op(pipe.mem_op);
printf("WB : "); print_op(pipe.wb_op);
printf("\n");
#endif
pipe_stage_wb();
pipe_stage_mem();
pipe_stage_execute();
pipe_stage_decode();
pipe_stage_fetch();
/* handle branch recoveries */
if (pipe.branch_recover) {
#ifdef DEBUG
printf("branch recovery: new dest %08x flush %d stages\n", pipe.branch_dest, pipe.branch_flush);
#endif
pipe.PC = pipe.branch_dest;
if (pipe.branch_flush >= 2) {
if (pipe.decode_op) free(pipe.decode_op);
pipe.decode_op = NULL;
}
if (pipe.branch_flush >= 3) {
if (pipe.execute_op) free(pipe.execute_op);
pipe.execute_op = NULL;
}
if (pipe.branch_flush >= 4) {
if (pipe.mem_op) free(pipe.mem_op);
pipe.mem_op = NULL;
}
if (pipe.branch_flush >= 5) {
if (pipe.wb_op) free(pipe.wb_op);
pipe.wb_op = NULL;
}
pipe.branch_recover = 0;
pipe.branch_dest = 0;
pipe.branch_flush = 0;
stat_squash++;
}
}
void pipe_recover(int flush, uint32_t dest)
{
/* if there is already a recovery scheduled, it must have come from a later
* stage (which executes older instructions), hence that recovery overrides
* our recovery. Simply return in this case. */
if (pipe.branch_recover) return;
/* schedule the recovery. This will be done once all pipeline stages simulate the current cycle. */
pipe.branch_recover = 1;
pipe.branch_flush = flush;
pipe.branch_dest = dest;
}
void pipe_stage_wb()
{
/* if there is no instruction in this pipeline stage, we are done */
if (!pipe.wb_op)
return;
/* grab the op out of our input slot */
Pipe_Op *op = pipe.wb_op;
pipe.wb_op = NULL;
/* if this instruction writes a register, do so now */
if (op->reg_dst != -1 && op->reg_dst != 0) {
pipe.REGS[op->reg_dst] = op->reg_dst_value;
#ifdef DEBUG
printf("R%d = %08x\n", op->reg_dst, op->reg_dst_value);
#endif
}
/* if this was a syscall, perform action */
if (op->opcode == OP_SPECIAL && op->subop == SUBOP_SYSCALL) {
if (op->reg_src1_value == 0xA) {
pipe.PC = op->pc; /* fetch will do pc += 4, then we stop with correct PC */
RUN_BIT = 0;
}
}
/* free the op */
free(op);
stat_inst_retire++;
}
void pipe_stage_mem()
{
/* if there is no instruction in this pipeline stage, we are done */
if (!pipe.mem_op)
return;
/* grab the op out of our input slot */
Pipe_Op *op = pipe.mem_op;
uint32_t val = 0;
if (op->is_mem) {
if(!is_waiting_data) {
val = cache_read_32(op->mem_addr & ~3);
} else {
is_waiting_data--;
return;
}
}
switch (op->opcode) {
case OP_LW:
case OP_LH:
case OP_LHU:
case OP_LB:
case OP_LBU:
{
/* extract needed value */
op->reg_dst_value_ready = 1;
if (op->opcode == OP_LW) {
op->reg_dst_value = val;
}
else if (op->opcode == OP_LH || op->opcode == OP_LHU) {
if (op->mem_addr & 2)
val = (val >> 16) & 0xFFFF;
else
val = val & 0xFFFF;
if (op->opcode == OP_LH)
val |= (val & 0x8000) ? 0xFFFF8000 : 0;
op->reg_dst_value = val;
}
else if (op->opcode == OP_LB || op->opcode == OP_LBU) {
switch (op->mem_addr & 3) {
case 0:
val = val & 0xFF;
break;
case 1:
val = (val >> 8) & 0xFF;
break;
case 2:
val = (val >> 16) & 0xFF;
break;
case 3:
val = (val >> 24) & 0xFF;
break;
}
if (op->opcode == OP_LB)
val |= (val & 0x80) ? 0xFFFFFF80 : 0;
op->reg_dst_value = val;
}
}
break;
case OP_SB:
switch (op->mem_addr & 3) {
case 0: val = (val & 0xFFFFFF00) | ((op->mem_value & 0xFF) << 0); break;
case 1: val = (val & 0xFFFF00FF) | ((op->mem_value & 0xFF) << 8); break;
case 2: val = (val & 0xFF00FFFF) | ((op->mem_value & 0xFF) << 16); break;
case 3: val = (val & 0x00FFFFFF) | ((op->mem_value & 0xFF) << 24); break;
}
if(!is_waiting_data) {
cache_write_32(op->mem_addr & ~3, val);
} else {
--is_waiting_data;
return;
}
break;
case OP_SH:
#ifdef DEBUG
printf("SH: addr %08x val %04x old word %08x\n", op->mem_addr, op->mem_value & 0xFFFF, val);
#endif
if (op->mem_addr & 2)
val = (val & 0x0000FFFF) | (op->mem_value) << 16;
else
val = (val & 0xFFFF0000) | (op->mem_value & 0xFFFF);
#ifdef DEBUG
printf("new word %08x\n", val);
#endif
if(!is_waiting_data) {
cache_write_32(op->mem_addr & ~3, val);
} else {
--is_waiting_data;
return;
}
break;
case OP_SW:
val = op->mem_value;
if(!is_waiting_data) {
cache_write_32(op->mem_addr & ~3, val);
} else {
--is_waiting_data;
return;
}
break;
}
/* clear stage input and transfer to next stage */
pipe.mem_op = NULL;
pipe.wb_op = op;
}
void pipe_stage_execute()
{
/* if a multiply/divide is in progress, decrement cycles until value is ready */
if (pipe.multiplier_stall > 0)
pipe.multiplier_stall--;
/* if downstream stall, return (and leave any input we had) */
if (pipe.mem_op != NULL)
return;
/* if no op to execute, return */
if (pipe.execute_op == NULL)
return;
/* grab op and read sources */
Pipe_Op *op = pipe.execute_op;
/* read register values, and check for bypass; stall if necessary */
int stall = 0;
if (op->reg_src1 != -1) {
if (op->reg_src1 == 0)
op->reg_src1_value = 0;
else if (pipe.mem_op && pipe.mem_op->reg_dst == op->reg_src1) {
if (!pipe.mem_op->reg_dst_value_ready)
stall = 1;
else
op->reg_src1_value = pipe.mem_op->reg_dst_value;
}
else if (pipe.wb_op && pipe.wb_op->reg_dst == op->reg_src1) {
op->reg_src1_value = pipe.wb_op->reg_dst_value;
}
else
op->reg_src1_value = pipe.REGS[op->reg_src1];
}
if (op->reg_src2 != -1) {
if (op->reg_src2 == 0)
op->reg_src2_value = 0;
else if (pipe.mem_op && pipe.mem_op->reg_dst == op->reg_src2) {
if (!pipe.mem_op->reg_dst_value_ready)
stall = 1;
else
op->reg_src2_value = pipe.mem_op->reg_dst_value;
}
else if (pipe.wb_op && pipe.wb_op->reg_dst == op->reg_src2) {
op->reg_src2_value = pipe.wb_op->reg_dst_value;
}
else
op->reg_src2_value = pipe.REGS[op->reg_src2];
}
/* if bypassing requires a stall (e.g. use immediately after load),
* return without clearing stage input */
if (stall)
return;
/* execute the op */
switch (op->opcode) {
case OP_SPECIAL:
op->reg_dst_value_ready = 1;
switch (op->subop) {
case SUBOP_SLL:
op->reg_dst_value = op->reg_src2_value << op->shamt;
break;
case SUBOP_SLLV:
op->reg_dst_value = op->reg_src2_value << op->reg_src1_value;
break;
case SUBOP_SRL:
op->reg_dst_value = op->reg_src2_value >> op->shamt;
break;
case SUBOP_SRLV:
op->reg_dst_value = op->reg_src2_value >> op->reg_src1_value;
break;
case SUBOP_SRA:
op->reg_dst_value = (int32_t)op->reg_src2_value >> op->shamt;
break;
case SUBOP_SRAV:
op->reg_dst_value = (int32_t)op->reg_src2_value >> op->reg_src1_value;
break;
case SUBOP_JR:
case SUBOP_JALR:
op->reg_dst_value = op->pc + 4;
op->branch_dest = op->reg_src1_value;
op->branch_taken = 1;
break;
case SUBOP_MULT:
{
/* we set a result value right away; however, we will
* model a stall if the program tries to read the value
* before it's ready (or overwrite HI/LO). Also, if
* another multiply comes down the pipe later, it will
* update the values and re-set the stall cycle count
* for a new operation.
*/
int64_t val = (int64_t)((int32_t)op->reg_src1_value) * (int64_t)((int32_t)op->reg_src2_value);
uint64_t uval = (uint64_t)val;
pipe.HI = (uval >> 32) & 0xFFFFFFFF;
pipe.LO = (uval >> 0) & 0xFFFFFFFF;
/* four-cycle multiplier latency */
pipe.multiplier_stall = 4;
}
break;
case SUBOP_MULTU:
{
uint64_t val = (uint64_t)op->reg_src1_value * (uint64_t)op->reg_src2_value;
pipe.HI = (val >> 32) & 0xFFFFFFFF;
pipe.LO = (val >> 0) & 0xFFFFFFFF;
/* four-cycle multiplier latency */
pipe.multiplier_stall = 4;
}
break;
case SUBOP_DIV:
if (op->reg_src2_value != 0) {
int32_t val1 = (int32_t)op->reg_src1_value;
int32_t val2 = (int32_t)op->reg_src2_value;
int32_t div, mod;
div = val1 / val2;
mod = val1 % val2;
pipe.LO = div;
pipe.HI = mod;
} else {
// really this would be a div-by-0 exception
pipe.HI = pipe.LO = 0;
}
/* 32-cycle divider latency */
pipe.multiplier_stall = 32;
break;
case SUBOP_DIVU:
if (op->reg_src2_value != 0) {
pipe.HI = (uint32_t)op->reg_src1_value % (uint32_t)op->reg_src2_value;
pipe.LO = (uint32_t)op->reg_src1_value / (uint32_t)op->reg_src2_value;
} else {
/* really this would be a div-by-0 exception */
pipe.HI = pipe.LO = 0;
}
/* 32-cycle divider latency */
pipe.multiplier_stall = 32;
break;
case SUBOP_MFHI:
/* stall until value is ready */
if (pipe.multiplier_stall > 0)
return;
op->reg_dst_value = pipe.HI;
break;
case SUBOP_MTHI:
/* stall to respect WAW dependence */
if (pipe.multiplier_stall > 0)
return;
pipe.HI = op->reg_src1_value;
break;
case SUBOP_MFLO:
/* stall until value is ready */
if (pipe.multiplier_stall > 0)
return;
op->reg_dst_value = pipe.LO;
break;
case SUBOP_MTLO:
/* stall to respect WAW dependence */
if (pipe.multiplier_stall > 0)
return;
pipe.LO = op->reg_src1_value;
break;
case SUBOP_ADD:
case SUBOP_ADDU:
op->reg_dst_value = op->reg_src1_value + op->reg_src2_value;
break;
case SUBOP_SUB:
case SUBOP_SUBU:
op->reg_dst_value = op->reg_src1_value - op->reg_src2_value;
break;
case SUBOP_AND:
op->reg_dst_value = op->reg_src1_value & op->reg_src2_value;
break;
case SUBOP_OR:
op->reg_dst_value = op->reg_src1_value | op->reg_src2_value;
break;
case SUBOP_NOR:
op->reg_dst_value = ~(op->reg_src1_value | op->reg_src2_value);
break;
case SUBOP_XOR:
op->reg_dst_value = op->reg_src1_value ^ op->reg_src2_value;
break;
case SUBOP_SLT:
op->reg_dst_value = ((int32_t)op->reg_src1_value <
(int32_t)op->reg_src2_value) ? 1 : 0;
break;
case SUBOP_SLTU:
op->reg_dst_value = (op->reg_src1_value < op->reg_src2_value) ? 1 : 0;
break;
}
break;
case OP_BRSPEC:
switch (op->subop) {
case BROP_BLTZ:
case BROP_BLTZAL:
if ((int32_t)op->reg_src1_value < 0) op->branch_taken = 1;
break;
case BROP_BGEZ:
case BROP_BGEZAL:
if ((int32_t)op->reg_src1_value >= 0) op->branch_taken = 1;
break;
}
break;
case OP_BEQ:
if (op->reg_src1_value == op->reg_src2_value) op->branch_taken = 1;
break;
case OP_BNE:
if (op->reg_src1_value != op->reg_src2_value) op->branch_taken = 1;
break;
case OP_BLEZ:
if ((int32_t)op->reg_src1_value <= 0) op->branch_taken = 1;
break;
case OP_BGTZ:
if ((int32_t)op->reg_src1_value > 0) op->branch_taken = 1;
break;
case OP_ADDI:
case OP_ADDIU:
op->reg_dst_value_ready = 1;
op->reg_dst_value = op->reg_src1_value + op->se_imm16;
break;
case OP_SLTI:
op->reg_dst_value_ready = 1;
op->reg_dst_value = (int32_t)op->reg_src1_value < (int32_t)op->se_imm16 ? 1 : 0;
break;
case OP_SLTIU:
op->reg_dst_value_ready = 1;
op->reg_dst_value = (uint32_t)op->reg_src1_value < (uint32_t)op->se_imm16 ? 1 : 0;
break;
case OP_ANDI:
op->reg_dst_value_ready = 1;
op->reg_dst_value = op->reg_src1_value & op->imm16;
break;
case OP_ORI:
op->reg_dst_value_ready = 1;
op->reg_dst_value = op->reg_src1_value | op->imm16;
break;
case OP_XORI:
op->reg_dst_value_ready = 1;
op->reg_dst_value = op->reg_src1_value ^ op->imm16;
break;
case OP_LUI:
op->reg_dst_value_ready = 1;
op->reg_dst_value = op->imm16 << 16;
break;
case OP_LW:
case OP_LH:
case OP_LHU:
case OP_LB:
case OP_LBU:
op->mem_addr = op->reg_src1_value + op->se_imm16;
break;
case OP_SW:
case OP_SH:
case OP_SB:
op->mem_addr = op->reg_src1_value + op->se_imm16;
op->mem_value = op->reg_src2_value;
break;
}
/* handle branch recoveries at this point */
if (op->branch_taken)
pipe_recover(3, op->branch_dest);
/* remove from upstream stage and place in downstream stage */
pipe.execute_op = NULL;
pipe.mem_op = op;
}
void pipe_stage_decode()
{
/* if downstream stall, return (and leave any input we had) */
if (pipe.execute_op != NULL)
return;
/* if no op to decode, return */
if (pipe.decode_op == NULL)
return;
/* grab op and remove from stage input */
Pipe_Op *op = pipe.decode_op;
pipe.decode_op = NULL;
/* set up info fields (source/dest regs, immediate, jump dest) as necessary */
uint32_t opcode = (op->instruction >> 26) & 0x3F;
uint32_t rs = (op->instruction >> 21) & 0x1F;
uint32_t rt = (op->instruction >> 16) & 0x1F;
uint32_t rd = (op->instruction >> 11) & 0x1F;
uint32_t shamt = (op->instruction >> 6) & 0x1F;
uint32_t funct1 = (op->instruction >> 0) & 0x1F;
uint32_t funct2 = (op->instruction >> 0) & 0x3F;
uint32_t imm16 = (op->instruction >> 0) & 0xFFFF;
uint32_t se_imm16 = imm16 | ((imm16 & 0x8000) ? 0xFFFF8000 : 0);
uint32_t targ = (op->instruction & ((1UL << 26) - 1)) << 2;
op->opcode = opcode;
op->imm16 = imm16;
op->se_imm16 = se_imm16;
op->shamt = shamt;
switch (opcode) {
case OP_SPECIAL:
/* all "SPECIAL" insts are R-types that use the ALU and both source
* regs. Set up source regs and immediate value. */
op->reg_src1 = rs;
op->reg_src2 = rt;
op->reg_dst = rd;
op->subop = funct2;
if (funct2 == SUBOP_SYSCALL) {
op->reg_src1 = 2; // v0
op->reg_src2 = 3; // v1
}
if (funct2 == SUBOP_JR || funct2 == SUBOP_JALR) {
op->is_branch = 1;
op->branch_cond = 0;
}
break;
case OP_BRSPEC:
/* branches that have -and-link variants come here */
op->is_branch = 1;
op->reg_src1 = rs;
op->reg_src2 = rt;
op->is_branch = 1;
op->branch_cond = 1; /* conditional branch */
op->branch_dest = op->pc + 4 + (se_imm16 << 2);
op->subop = rt;
if (rt == BROP_BLTZAL || rt == BROP_BGEZAL) {
/* link reg */
op->reg_dst = 31;
op->reg_dst_value = op->pc + 4;
op->reg_dst_value_ready = 1;
}
break;
case OP_JAL:
op->reg_dst = 31;
op->reg_dst_value = op->pc + 4;
op->reg_dst_value_ready = 1;
op->branch_taken = 1;
/* fallthrough */
case OP_J:
op->is_branch = 1;
op->branch_cond = 0;
op->branch_taken = 1;
op->branch_dest = (op->pc & 0xF0000000) | targ;
break;
case OP_BEQ:
case OP_BNE:
case OP_BLEZ:
case OP_BGTZ:
/* ordinary conditional branches (resolved after execute) */
op->is_branch = 1;
op->branch_cond = 1;
op->branch_dest = op->pc + 4 + (se_imm16 << 2);
op->reg_src1 = rs;
op->reg_src2 = rt;
break;
case OP_ADDI:
case OP_ADDIU:
case OP_SLTI:
case OP_SLTIU:
/* I-type ALU ops with sign-extended immediates */
op->reg_src1 = rs;
op->reg_dst = rt;
break;
case OP_ANDI:
case OP_ORI:
case OP_XORI:
case OP_LUI:
/* I-type ALU ops with non-sign-extended immediates */
op->reg_src1 = rs;
op->reg_dst = rt;
break;
case OP_LW:
case OP_LH:
case OP_LHU:
case OP_LB:
case OP_LBU:
case OP_SW:
case OP_SH:
case OP_SB:
/* memory ops */
op->is_mem = 1;
op->reg_src1 = rs;
if (opcode == OP_LW || opcode == OP_LH || opcode == OP_LHU || opcode == OP_LB || opcode == OP_LBU) {
/* load */
op->mem_write = 0;
op->reg_dst = rt;
}
else {
/* store */
op->mem_write = 1;
op->reg_src2 = rt;
}
break;
}
/* we will handle reg-read together with bypass in the execute stage */
/* place op in downstream slot */
pipe.execute_op = op;
}
uint32_t readed_inst ;
void pipe_stage_fetch()
{
/* if pipeline is stalled (our output slot is not empty), return */
if (pipe.decode_op != NULL)
return;
/* Allocate an op and send it down the pipeline. */
Pipe_Op *op = malloc(sizeof(Pipe_Op));
memset(op, 0, sizeof(Pipe_Op));
op->reg_src1 = op->reg_src2 = op->reg_dst = -1;
if(!is_waiting) {
readed_inst = cache_read_inst(pipe.PC);
}
if(!is_waiting) {
op->instruction = readed_inst;
op->pc = pipe.PC;
pipe.decode_op = op;
/* update PC */
pipe.PC += 4;
} else {
--is_waiting;
op->instruction = 0x00000025;
pipe.decode_op = op;
}
stat_inst_fetch++;
}