-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipes.c
599 lines (487 loc) · 19.3 KB
/
pipes.c
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
#include "pipes.h"
#include "mips32.h"
#include "consts.h"
#include "stdio.h"
#include "mem.h"
static struct preg_if_id if_id;
static struct preg_id_exe id_exe;
static struct preg_exe_mem exe_mem;
static struct preg_mem_wb mem_wb;
/* Code attempts to model the processor layout on page 320 (Patterson & Hennessy)
* As closly as possible (within reasonable limits)
*/
static bool flush; // Signal IF to fetch a NOP
static bool jump; // Control signal to PC mux (left)
static uint32_t jump_target; // Jump address bus
// Run cycle
int cycle() {
int ret;
// NOTE : These are lines, unlike the pipeline registers they fall to low when not held high
// In the real world all states happen in parallel (thats the point)
jump = false;
flush = false;
// Run pipeline
interp_wb();
if ((ret = interp_mem())) return ret;
if ((ret = interp_exe())) return ret;
if ((ret = interp_id())) return ret;
if ((ret = interp_if())) return ret;
// Hazard detection
forward();
// Update PC (could be moved to IF)
if (jump) {
#ifdef DEBUG
printf("DEBUG : Cycle : Perform jump : Target=[0x%x]\n", jump_target);
if (jump_target % 4 != 0) {
printf("DEBUG : Cycle : Jump to invalid address!\n");
exit(-1);
}
#endif
PC = jump_target;
}
return ret;
}
// PIPELINE : Instruction fetch
int interp_if() {
D printf("DEBUG : Pipeline : Instruction Fetch\n");
// Check if should flush
if (flush) {
D printf("DEBUG - Insert NOP\n");
if_id.inst = 0x0;
return 0;
}
// Read instruction at PC
if_id.next_pc = PC + INSTRUCTION_SIZE;
int ret = inst_read(PC, &if_id.inst);
if(ret) return ret;
// Check Load-use hazards
// NOTE : This is more strict than it need be, for instance an I-Type instruction will
// use RS as an operand and RT as destination - and it does not matter what value the destination holds.
D printf("DEBUG - Loaded instruction 0x%08x\n", if_id.inst);
if (id_exe.mem_read) {
if (GET_RS(if_id.inst) == id_exe.rt) {
D printf("DEBUG - Load-use hazard detected for RS\n");
if_id.inst = 0x0;
return 0;
} else if (GET_RT(if_id.inst) == id_exe.rt) {
D printf("DEBUG - Load-use hazard detected for RT\n");
if_id.inst = 0x0;
return 0;
}
}
// "Branch hazards"
// NOTE : Since we branch in ID, we need to detect if the branch depends on a result from EX or MEM
// If this is the case we insert a bubble
if (GET_OPCODE(if_id.inst) == OPCODE_BEQ || GET_OPCODE(if_id.inst) == OPCODE_BNE) {
if (GET_RS(if_id.inst) == id_exe.rt && id_exe.rt != 0) {
D printf("DEBUG - Branch hazard RS\n");
if_id.inst = 0x0;
return 0;
} else if (GET_RT(if_id.inst) == id_exe.rt && id_exe.rt != 0) {
D printf("DEBUG - Branch hazard RT\n");
if_id.inst = 0x0;
return 0;
}
if (GET_RS(if_id.inst) == exe_mem.rt && exe_mem.rt != 0) {
D printf("DEBUG - Branch hazard RS\n");
if_id.inst = 0x0;
return 0;
} else if (GET_RT(if_id.inst) == exe_mem.rt && exe_mem.rt != 0) {
D printf("DEBUG - Branch hazard RS\n");
if_id.inst = 0x0;
return 0;
}
}
// Carry next PC
PC = if_id.next_pc;
instr_cnt++;
D printf("DEBUG - Next PC 0x%x\n", PC);
return 0;
}
// CONTROL :
int interp_control() {
// Set control signal
switch(GET_OPCODE(if_id.inst)) {
// Memory
case OPCODE_LW:
D printf("DEBUG - LW instruction\n");
// Control
id_exe.mem_read = true;
id_exe.mem_write = false;
id_exe.reg_write = true;
id_exe.alu_src = true;
id_exe.mem_to_reg = true;
id_exe.funct = FUNCT_ADD;
id_exe.reg_dst = GET_RT(if_id.inst);
break;
case OPCODE_SW:
D printf("DEBUG - SW instruction\n");
// Control
id_exe.mem_read = false;
id_exe.mem_write = true;
id_exe.reg_write = false;
id_exe.alu_src = true;
id_exe.mem_to_reg = false;
id_exe.funct = FUNCT_ADD;
id_exe.reg_dst = GET_RT(if_id.inst);
break;
// R-Type instructions
case OPCODE_R:
D printf("DEBUG - R-type instruction\n");
// Control
id_exe.mem_read = false;
id_exe.mem_write = false;
id_exe.reg_write = true;
id_exe.alu_src = false;
id_exe.mem_to_reg = false;
id_exe.funct = GET_FUNCT(if_id.inst);
id_exe.reg_dst = GET_RD(if_id.inst);
break;
// Arithmetic
case OPCODE_ADDIU:
D printf("DEBUG - Add Immediate (ignore overflow)\n");
// Control
id_exe.mem_read = false;
id_exe.mem_write = false;
id_exe.reg_write = true;
id_exe.alu_src = true; // Source is imm
id_exe.mem_to_reg = false; // We want the output of the ALU
id_exe.funct = FUNCT_ADDU; // Add no overflow
id_exe.reg_dst = GET_RT(if_id.inst); // Destination is in RT
break;
case OPCODE_ADDI:
D printf("DEBUG - Add Immediate (check overflow)\n");
// Control
id_exe.mem_read = false;
id_exe.mem_write = false;
id_exe.reg_write = true;
id_exe.alu_src = true; // Source is imm
id_exe.mem_to_reg = false; // We want the output of the ALU
id_exe.funct = FUNCT_ADD; // Add with overflow
id_exe.reg_dst = GET_RT(if_id.inst); // Destination is in RT
break;
case OPCODE_SLTI:
D printf("DEBUG - Set on Less Than Immediate (signed)\n");
// Control
id_exe.mem_read = false;
id_exe.mem_write = false;
id_exe.reg_write = true;
id_exe.alu_src = true; // Source is imm
id_exe.mem_to_reg = false; // We want the output of the ALU
id_exe.funct = FUNCT_SLT; // Set on less than (signed)
id_exe.reg_dst = GET_RT(if_id.inst); // Destination is in RT
break;
case OPCODE_SLTIU:
D printf("DEBUG - Set on Less Than Immediate (unsigned)\n");
// Control
id_exe.mem_read = false;
id_exe.mem_write = false;
id_exe.reg_write = true;
id_exe.alu_src = true; // Source is imm
id_exe.mem_to_reg = false; // We want the output of the ALU
id_exe.funct = FUNCT_SLTU; // Set on less than (signed)
id_exe.reg_dst = GET_RT(if_id.inst); // Destination is in RT
break;
case OPCODE_ANDI:
D printf("DEBUG - AND with Immediate\n");
// Control
id_exe.mem_read = false;
id_exe.mem_write = false;
id_exe.reg_write = true;
id_exe.mem_to_reg = false; // We want the output of the ALU
// Insert lower 16 bits of immediate into RT
id_exe.alu_src = false;
id_exe.rt_value = id_exe.sign_ext_imm & LS_16B;
id_exe.rt = 0;
id_exe.funct = FUNCT_AND; // Bitwise AND
id_exe.reg_dst = GET_RT(if_id.inst); // Destination is in RT
break;
case OPCODE_ORI:
D printf("DEBUG - OR with Immediate\n");
// Control
id_exe.mem_read = false;
id_exe.mem_write = false;
id_exe.reg_write = true;
id_exe.mem_to_reg = false; // We want the output of the ALU
// Insert lower 16 bits of immediate into RT
id_exe.alu_src = false;
id_exe.rt_value = id_exe.sign_ext_imm & LS_16B;
id_exe.rt = 0;
id_exe.funct = FUNCT_OR; // Bitwise OR
id_exe.reg_dst = GET_RT(if_id.inst); // Destination is in RT
break;
case OPCODE_LUI:
D printf("DEBUG - Load Upper Immediate\n");
// Control
id_exe.mem_read = false;
id_exe.mem_write = false;
id_exe.reg_write = true;
id_exe.mem_to_reg = false; // We want the output of the ALU
id_exe.alu_src = true; // Source is imm
id_exe.funct = FUNCT_SLL; // Logical shift
id_exe.shamt = 16; // Shift by 16
id_exe.reg_dst = GET_RT(if_id.inst); // Destination is in RT
break;
// Branching
case OPCODE_BEQ:
D printf("DEBUG - Branch on Equal [%d]\n", (id_exe.rs_value == id_exe.rt_value) & 1);
// JUMP
if (id_exe.rs_value == id_exe.rt_value) {
// TODO : Detect overflow
jump = true;
jump_target = if_id.next_pc + (id_exe.sign_ext_imm << 2);
}
// Bubble
id_exe.mem_read = false;
id_exe.mem_write = false;
id_exe.reg_write = false;
break;
case OPCODE_BNE:
D printf("DEBUG - Branch on Not Equal [%d]\n", (id_exe.rs_value != id_exe.rt_value) & 1);
// JUMP
if (id_exe.rs_value != id_exe.rt_value) {
// TODO : Detect overflow
jump = true;
jump_target = if_id.next_pc + (id_exe.sign_ext_imm << 2);
}
// Bubble
id_exe.mem_read = false;
id_exe.mem_write = false;
id_exe.reg_write = false;
break;
case OPCODE_J:
D printf("DEBUG - Jump\n");
// JUMP
jump = true;
jump_target = (if_id.next_pc & MS_4B) | (GET_ADDRESS(if_id.inst) << 2);
// Bubble
id_exe.mem_read = false;
id_exe.mem_write = false;
id_exe.reg_write = false;
break;
case OPCODE_JAL:
D printf("DEBUG - Jump And Link\n");
// JUMP
jump = true;
jump_target = (if_id.next_pc & MS_4B) | (GET_ADDRESS(if_id.inst) << 2);
// Write to RA
id_exe.mem_read = false;
id_exe.mem_write = false;
id_exe.reg_write = true;
id_exe.mem_to_reg = false;
id_exe.alu_src = false;
D printf("DEBUG - Write 0x%x + 0x4 = 0x%x to RA\n", if_id.next_pc, if_id.next_pc + 0x4);
id_exe.reg_dst = r_ra;
id_exe.funct = FUNCT_ADD;
id_exe.rs_value = if_id.next_pc;
id_exe.rt_value = 0x4;
id_exe.rt = 0; // Fucking hazard detection
break;
default:
D printf("DEBUG - Unknown opcode 0x%08x\n", GET_OPCODE(if_id.inst));
return ERROR_UNKNOWN_OPCODE;
}
// Debugging
D printf("DEBUG - MemRead = [%d]\n", id_exe.mem_read);
D printf("DEBUG - MemWrite = [%d]\n", id_exe.mem_write);
D printf("DEBUG - RegWrite = [%d]\n", id_exe.reg_write);
D printf("DEBUG - ALUSrc = [%d]\n", id_exe.alu_src);
D printf("DEBUG - MemToReg = [%d]\n", id_exe.mem_to_reg);
D printf("DEBUG - RegDst = [%d]\n", id_exe.reg_dst);
D printf("DEBUG - FUNCT = [0x%x]\n", id_exe.funct);
D printf("DEBUG - Imm : 0x%x\n", id_exe.sign_ext_imm);
return 0;
}
// PIPELINE : Instruction decode
int interp_id() {
D printf("DEBUG : Pipeline : Instruction Decode\n");
D printf("DEBUG - Instruction = [0x%08x]\n", if_id.inst);
// Load fields from instruction
id_exe.rt = GET_RT(if_id.inst);
id_exe.rs = GET_RS(if_id.inst);
id_exe.rs_value = regs[GET_RS(if_id.inst)];
id_exe.rt_value = regs[GET_RT(if_id.inst)];
id_exe.sign_ext_imm = SIGN_EXTEND(GET_IMM(if_id.inst));
id_exe.shamt = GET_SHAMT(if_id.inst);
// Set control signals
return interp_control();
}
// ALU
int alu() {
// Select operands
uint32_t op1 = id_exe.rs_value;
uint32_t op2 = id_exe.alu_src ? id_exe.sign_ext_imm : id_exe.rt_value;
D printf("DEBUG - RT = [%d]\n", id_exe.rt);
D printf("DEBUG - RD = [%d]\n", id_exe.rs);
D printf("DEBUG - SrcVal1 = [0x%x]\n", op1);
D printf("DEBUG - SrcVal2 = [0x%x]\n", op2);
D printf("DEBUG - ALUSrc = [%d]\n", id_exe.alu_src);
// Complete compuation
switch(id_exe.funct) {
case FUNCT_JR:
D printf("DEBUG - OP = [FUNCT_JR] (Jump Register)\n");
// NOTE : Realistically this would not happen in the ALU
// Jump
jump = true;
jump_target = op1;
// NOP next IF
flush = true;
// Pass bubble
exe_mem.mem_read = false;
exe_mem.mem_write = false;
exe_mem.reg_write = false;
break;
case FUNCT_AND:
D printf("DEBUG - OP = [FUNCT_AND] (Bitwise AND)\n");
exe_mem.alu_res = op1 & op2;
break;
case FUNCT_NOR:
D printf("DEBUG - OP = [FUNCT_NOR] (Bitwise NOT OR)\n");
exe_mem.alu_res = !(op1 | op2);
break;
case FUNCT_OR:
D printf("DEBUG - OP = [FUNCT_OR] (Bitwise OR)\n");
exe_mem.alu_res = op1 | op2;
break;
case FUNCT_SLL:
D printf("DEBUG - OP = [FUNCT_SLL] (Shift left)\n");
exe_mem.alu_res = op2 << id_exe.shamt;
break;
case FUNCT_SLT:
D printf("DEBUG - OP = [FUNCT_SLT] (Set on less than - signed)\n");
exe_mem.alu_res = (int32_t) op1 < (int32_t) op2 ? 1 : 0;
break;
case FUNCT_SLTU:
D printf("DEBUG - OP = [FUNCT_SLTU] (Set on less than - unsigned)\n");
exe_mem.alu_res = op1 < op2 ? 1 : 0;
break;
case FUNCT_SRL:
D printf("DEBUG - OP = [FUNCT_SRL] (Shift right)\n");
exe_mem.alu_res = op2 >> id_exe.shamt;
break;
case FUNCT_SUB:
D printf("DEBUG - OP = [FUNCT_SUB] (Subtraction - check overflow)\n");
exe_mem.alu_res = op1 - op2;
if (EXT64I(op1) - EXT64I(op2) != EXT64I(exe_mem.alu_res)) return ERROR_OVERFLOW;
break;
case FUNCT_SUBU:
D printf("DEBUG - OP = [FUNCT_SUBU] (Subtraction - no overflow)\n");
exe_mem.alu_res = op1 - op2;
break;
case FUNCT_ADD:
D printf("DEBUG - OP = [FUNCT_ADD] (Add - check overflow)\n");
exe_mem.alu_res = op1 + op2;
if (EXT64I(op1) + EXT64I(op2) != EXT64I(exe_mem.alu_res)) return ERROR_OVERFLOW;
break;
case FUNCT_ADDU:
D printf("DEBUG - OP = [FUNCT_ADDU] (Add - no overflow)\n");
exe_mem.alu_res = op1 + op2;
break;
case FUNCT_SYSCALL:
D printf("DEBUG - OP = [FUNCT_SYSCALL] (Syscall)\n");
return SIG_HALT_PROGRAM;
default:
D printf("DEBUG - OP = UNKNOWN\n");
return ERROR_UNKNOWN_FUNCT;
}
D printf("DEBUG - AluRes = [0x%x]\n", exe_mem.alu_res);
return 0;
}
// PIPELINE : Execute
int interp_exe() {
D printf("DEBUG : Pipeline : Execution\n");
// Carry forward
exe_mem.mem_read = id_exe.mem_read;
exe_mem.mem_write = id_exe.mem_write;
exe_mem.reg_write = id_exe.reg_write;
exe_mem.mem_to_reg = id_exe.mem_to_reg;
exe_mem.rt = id_exe.rt;
exe_mem.rt_value = id_exe.rt_value;
exe_mem.reg_dst = id_exe.reg_dst;
// Debugging
D printf("DEBUG - MemRead = [%d]\n", exe_mem.mem_read);
D printf("DEBUG - MemWrite = [%d]\n", exe_mem.mem_write);
D printf("DEBUG - RegWrite = [%d]\n", exe_mem.reg_write);
// Perform ALU operations
return alu();
}
// PIPELINE : Memory
int interp_mem() {
D printf("DEBUG : Pipeline : Memory\n");
D printf("DEBUG - ALURes = [0x%x]\n", exe_mem.alu_res);
// Carry forward
mem_wb.reg_write = exe_mem.reg_write;
mem_wb.mem_to_reg = exe_mem.mem_to_reg;
mem_wb.rt = exe_mem.rt;
mem_wb.alu_res = exe_mem.alu_res;
mem_wb.reg_dst = exe_mem.reg_dst;
// Read from memory
if (exe_mem.mem_read) {
int ret = data_read(exe_mem.alu_res, &mem_wb.read_data);
if(ret != 0) return ret;
D printf("DEBUG - Read value 0x%x from address 0x%x [LW]\n", mem_wb.read_data, exe_mem.alu_res);
}
// Write to memory
if (exe_mem.mem_write) {
int ret = data_write(exe_mem.alu_res, exe_mem.rt_value); // TODO : Check return value
if(ret != 0) return ret;
D printf("DEBUG - Store value 0x%x into 0x%x [SW]\n", exe_mem.rt_value, exe_mem.alu_res);
}
return 0;
}
// PIPELINE : Writeback
void interp_wb() {
D printf("DEBUG : Pipeline : Writeback\n");
D printf("DEBUG - ALURes = [0x%x]\n", mem_wb.alu_res);
if (!mem_wb.reg_write) return;
if (!mem_wb.reg_dst) return;
D printf("DEBUG - Writing to register bank\n");
D printf("DEBUG - MemToReg = [%d]\n", mem_wb.mem_to_reg);
regs[mem_wb.reg_dst] = mem_wb.mem_to_reg ? mem_wb.read_data : mem_wb.alu_res;
D printf("DEBUG - Wrote 0x%x to register %d\n", regs[mem_wb.reg_dst], mem_wb.reg_dst);
return;
}
// FORWARDING
int forward() {
D printf("DEBUG : Hazard detection\n");
// NOTE : We cannot terminate the function after detecting a single hazard,
// although MIPS instructions can only write to 1 register, we might need to
// forward one operand from MEM and one from WB!
// Detect EX hazards
if (exe_mem.reg_write && exe_mem.reg_dst != 0) {
// Check RS
if(exe_mem.reg_dst == id_exe.rs) {
D printf("DEBUG - EX hazard : Forward ALURes to RSValue [%d]\n", exe_mem.reg_dst);
id_exe.rs_value = exe_mem.alu_res;
}
// Check RT
if (exe_mem.reg_dst == id_exe.rt) {
D printf("DEBUG - EX hazard : Forward ALURes to RTValue [%d]\n", exe_mem.reg_dst);
id_exe.rt_value = exe_mem.alu_res;
}
}
// Detect MEM hazards
if (mem_wb.reg_write && mem_wb.reg_dst != 0) {
// Check RS
if (mem_wb.reg_dst == id_exe.rs && exe_mem.reg_dst != id_exe.rs) {
if (mem_wb.mem_to_reg) {
D printf("DEBUG - MEM hazard : Forward ReadData to RSValue [%d]\n", mem_wb.reg_dst);
id_exe.rs_value = mem_wb.read_data;
} else {
D printf("DEBUG - MEM hazard : Forward ALURes to RSValue [%d]\n", mem_wb.reg_dst);
id_exe.rs_value = mem_wb.alu_res;
}
}
// Check RT
if (mem_wb.reg_dst == id_exe.rt && exe_mem.reg_dst != id_exe.rt) {
if (mem_wb.mem_to_reg) {
D printf("DEBUG - MEM hazard : Forward ReadData to RTValue [%d]\n", mem_wb.reg_dst);
id_exe.rt_value = mem_wb.read_data;
} else {
D printf("DEBUG - MEM hazard : Forward ALURes to RTValue [%d]\n", mem_wb.reg_dst);
id_exe.rt_value = mem_wb.alu_res;
}
}
}
return 0;
}