-
Notifications
You must be signed in to change notification settings - Fork 56
/
FPPipeline.java
518 lines (444 loc) · 19.2 KB
/
FPPipeline.java
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
/*
* FPPipeline.java
*
* This class models a MIPS FPU pipeline that supports multiple outstanding FP operations
* it is used only by the cpu class
* (c) 2006 Massimo Trubia
*
* This file is part of the EduMIPS64 project, and is released under the GNU
* General Public License.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.edumips64.core.fpu;
import org.edumips64.core.fpu.FPPipeline.Constants.FPAdderStatus;
import org.edumips64.core.fpu.FPPipeline.Constants.FPMultiplierStatus;
import org.edumips64.core.is.InstructionInterface;
import java.util.*;
/** This class models a MIPS FPU pipeline that supports multiple outstanding FP operations
* it is used only by the cpu class
* @author Massimo Trubia
*/
public class FPPipeline {
//FPU functional units
public static class Constants {
public enum FPAdderStatus {A1, A2, A3, A4};
public enum FPMultiplierStatus {M1, M2, M3, M4, M5, M6, M7};
public enum FPDividerStatus {DIVIDER};
public static List<String> fparithmetic = new ArrayList<>(Arrays.asList("ADD.D",
"SUB.D", "DIV.D", "MUL.D"));
}
private Divider divider;
private Multiplier multiplier;
private Adder adder;
private int nInstructions; //used for understanding if the fpPipe is empty or not
//FPPipeline in the same order by which they has entered
public FPPipeline() {
// Instantiating functional units objects.
nInstructions = 0;
divider = new Divider();
divider.reset();
multiplier = new Multiplier();
multiplier.reset();
adder = new Adder();
adder.reset();
}
public int size() {
return nInstructions;
}
public String toString() {
String output = "";
output += adder.toString();
output += multiplier.toString();
output += divider.toString();
return output;
}
/** Returns true if the specified functional unit is filled by an instruction, false when the contrary happens.
* No controls are carried out on the legality of parameters, for mistaken parameters false is returned
* @param funcUnit The functional unit to check. Legal values are "ADDER", "MULTIPLIER", "DIVIDER"
* @param stage The integer that refers to the stage of the functional unit.
* ADDER [1,4], MULTIPLIER [1,7], DIVIDER [any] */
public boolean isFuncUnitFilled(String funcUnit, int stage) {
if (funcUnit.compareToIgnoreCase("ADDER") == 0)
switch (stage) {
case 1:
return (adder.getFuncUnit().get(Constants.FPAdderStatus.A1) != null);
case 2:
return (adder.getFuncUnit().get(Constants.FPAdderStatus.A2) != null);
case 3:
return (adder.getFuncUnit().get(Constants.FPAdderStatus.A3) != null);
case 4:
return (adder.getFuncUnit().get(Constants.FPAdderStatus.A4) != null);
}
if (funcUnit.compareToIgnoreCase("MULTIPLIER") == 0)
switch (stage) {
case 1:
return (multiplier.getFuncUnit().get(Constants.FPMultiplierStatus.M1) != null);
case 2:
return (multiplier.getFuncUnit().get(Constants.FPMultiplierStatus.M2) != null);
case 3:
return (multiplier.getFuncUnit().get(Constants.FPMultiplierStatus.M3) != null);
case 4:
return (multiplier.getFuncUnit().get(Constants.FPMultiplierStatus.M4) != null);
case 5:
return (multiplier.getFuncUnit().get(Constants.FPMultiplierStatus.M5) != null);
case 6:
return (multiplier.getFuncUnit().get(Constants.FPMultiplierStatus.M6) != null);
case 7:
return (multiplier.getFuncUnit().get(Constants.FPMultiplierStatus.M7) != null);
}
return funcUnit.compareToIgnoreCase("DIVIDER") == 0 && (divider.getFuncUnit() != null);
}
/** Returns the instruction of the specified functional unit , null if it is empty.
* No controls are carried out on the legality of parameters, for mistaken parameters null is returned
* @param funcUnit The functional unit to check. Legal values are "ADDER", "MULTIPLIER", "DIVIDER"
* @param stage The integer that refers to the stage of the functional unit.
* ADDER [1,4], MULTIPLIER [1,7], DIVIDER [any] */
public InstructionInterface getInstructionByFuncUnit(String funcUnit, int stage) {
if (funcUnit.compareToIgnoreCase("ADDER") == 0) {
Map<FPAdderStatus, InstructionInterface> adderFu = adder.getFuncUnit();
switch (stage) {
case 1:
return adderFu.get(Constants.FPAdderStatus.A1);
case 2:
return adderFu.get(Constants.FPAdderStatus.A2);
case 3:
return adderFu.get(Constants.FPAdderStatus.A3);
case 4:
return adderFu.get(Constants.FPAdderStatus.A4);
}
}
if (funcUnit.compareToIgnoreCase("MULTIPLIER") == 0) {
Map<FPMultiplierStatus, InstructionInterface> multiplierFu = multiplier.getFuncUnit();
switch (stage) {
case 1:
return multiplierFu.get(Constants.FPMultiplierStatus.M1);
case 2:
return multiplierFu.get(Constants.FPMultiplierStatus.M2);
case 3:
return multiplierFu.get(Constants.FPMultiplierStatus.M3);
case 4:
return multiplierFu.get(Constants.FPMultiplierStatus.M4);
case 5:
return multiplierFu.get(Constants.FPMultiplierStatus.M5);
case 6:
return multiplierFu.get(Constants.FPMultiplierStatus.M6);
case 7:
return multiplierFu.get(Constants.FPMultiplierStatus.M7);
}
}
if (funcUnit.compareToIgnoreCase("DIVIDER") == 0) {
return divider.getFuncUnit();
}
return null;
}
/** Inserts the passed instruction into the right functional unit. If no errors occur
* 0 is returned, else, if we want to insert an ADD.fmt, MUL.fmt, SUB.fmt and
* the first place of the adder or multiplier is filled by other
* instructions 1 is returned, else, if we want to insert a DIV.fmt and the
* divider is full 2 is returned and the CPU raises a StructuralException.
* If an integer instruction is passed at the method 3 is returned
*/
public int putInstruction(InstructionInterface instr, boolean simulation) { //throws InputStructuralHazardException
if (instr != null && Constants.fparithmetic.contains(instr.getName())) {
String instrName = instr.getName();
if ((instrName.compareToIgnoreCase("ADD.D") == 0) || (instrName.compareToIgnoreCase("SUB.D") == 0))
if (adder.putInstruction(instr, simulation) == -1) {
return 1;
}
if (instrName.compareToIgnoreCase("MUL.D") == 0)
if (multiplier.putInstruction(instr, simulation) == -1) {
return 1;
}
if (instrName.compareToIgnoreCase("DIV.D") == 0)
if (divider.putInstruction(instr, simulation) == -1) {
return 2;
}
if (!simulation) {
nInstructions++;
}
return 0;
}
return 3;
}
// Returns the completed FP instruction. If more than one instruction completed, it will return them in the following
// order: 1. divider; 2. multiplier; 3. adder. If no instruction is complete, it'll return null.
// The returned instruction will be removed from the corresponding FPU unit.
public InstructionInterface getCompletedInstruction() {
InstructionInterface dividerInstruction = divider.getInstruction();
InstructionInterface multiplierInstruction = multiplier.getInstruction();
InstructionInterface adderInstruction = adder.getInstruction();
if (dividerInstruction != null) {
divider.removeLast();
nInstructions--;
return dividerInstruction;
}
if (multiplierInstruction != null) {
multiplier.removeLast();
nInstructions--;
return multiplierInstruction;
}
if (adderInstruction != null) {
adder.removeLast();
nInstructions--;
return adderInstruction;
}
return null;
}
/* Shifts instructions into the functional units and calls the EX() method for instructions in the first step
* this method is called from getCompletedInstruction in order to prepare the pipeline for a new instruction entrance */
public void step() {
//try catch is necessary for handling structural stalls and stalls coming from the EX() method
//adder
adder.step();
//multiplier
multiplier.step();
//divider
divider.step();
}
/** This method is used in order to understand if the fpPipe is not empty and the all CPU halt are disabled*/
public boolean isEmpty() {
return nInstructions == 0;
}
public int getDividerCounter() {
return divider.getCounter();
}
/* Resets the fp pipeline */
public void reset() {
nInstructions = 0;
multiplier.reset();
adder.reset();
divider.reset();
}
//---------------------- FUNCTIONAL UNITS ----------------------------------------
interface FPFunctionalUnit {
int putInstruction(InstructionInterface instr, boolean simulation);
InstructionInterface getInstruction();
void step();
}
/** This class models the 7 steps floating point multiplier*/
private class Multiplier implements FPFunctionalUnit {
private Map<Constants.FPMultiplierStatus, InstructionInterface> multiplier;
Multiplier() {
//Multiplier initialization
multiplier = new HashMap<>();
this.reset();
}
Map<Constants.FPMultiplierStatus, InstructionInterface> getFuncUnit() {
return multiplier;
}
public String toString() {
String output = "";
InstructionInterface instr;
output += "MULTIPLIER\n";
output += ((instr = multiplier.get(Constants.FPMultiplierStatus.M1)) != null) ? instr.getName() + "\n" : "EMPTY\n";
output += ((instr = multiplier.get(Constants.FPMultiplierStatus.M2)) != null) ? instr.getName() + "\n" : "EMPTY\n";
output += ((instr = multiplier.get(Constants.FPMultiplierStatus.M3)) != null) ? instr.getName() + "\n" : "EMPTY\n";
output += ((instr = multiplier.get(Constants.FPMultiplierStatus.M4)) != null) ? instr.getName() + "\n" : "EMPTY\n";
output += ((instr = multiplier.get(Constants.FPMultiplierStatus.M5)) != null) ? instr.getName() + "\n" : "EMPTY\n";
output += ((instr = multiplier.get(Constants.FPMultiplierStatus.M6)) != null) ? instr.getName() + "\n" : "EMPTY\n";
output += ((instr = multiplier.get(Constants.FPMultiplierStatus.M7)) != null) ? instr.getName() + "\n" : "EMPTY\n";
return output;
}
/** Resets the functional unit*/
public void reset() {
multiplier.put(Constants.FPMultiplierStatus.M1, null);
multiplier.put(Constants.FPMultiplierStatus.M2, null);
multiplier.put(Constants.FPMultiplierStatus.M3, null);
multiplier.put(Constants.FPMultiplierStatus.M4, null);
multiplier.put(Constants.FPMultiplierStatus.M5, null);
multiplier.put(Constants.FPMultiplierStatus.M6, null);
multiplier.put(Constants.FPMultiplierStatus.M7, null);
}
/** Inserts the passed instruction in the first position of the functional unit
* if another instruction holds that position a negative number is returned*/
public int putInstruction(InstructionInterface instr, boolean simulation_enabled) {
if (multiplier.get(Constants.FPMultiplierStatus.M1) == null) {
if (!simulation_enabled) {
multiplier.put(Constants.FPMultiplierStatus.M1, instr);
}
return 0;
} else {
return -1;
}
}
/** Returns the last instruction in the functional unit, if any instruction was found
* null is returned, the instruction is not removed from the HashMap */
public InstructionInterface getInstruction() {
return multiplier.get(Constants.FPMultiplierStatus.M7);
}
/** Remove the last instruction in the functional unit*/
void removeLast() {
multiplier.put(Constants.FPMultiplierStatus.M7, null);
}
/* Shifts instructions into the functional unit and calls the EX() method for instructions in the secondary step
* this method is called from getCompletedInstruction in order to prepare the pipeline for a new instruction entrance */
public void step() {
if (multiplier.get(Constants.FPMultiplierStatus.M7) == null) {
multiplier.put(Constants.FPMultiplierStatus.M7, multiplier.get(Constants.FPMultiplierStatus.M6));
multiplier.put(Constants.FPMultiplierStatus.M6, null);
}
if (multiplier.get(Constants.FPMultiplierStatus.M6) == null) {
multiplier.put(Constants.FPMultiplierStatus.M6, multiplier.get(Constants.FPMultiplierStatus.M5));
multiplier.put(Constants.FPMultiplierStatus.M5, null);
}
if (multiplier.get(Constants.FPMultiplierStatus.M5) == null) {
multiplier.put(Constants.FPMultiplierStatus.M5, multiplier.get(Constants.FPMultiplierStatus.M4));
multiplier.put(Constants.FPMultiplierStatus.M4, null);
}
if (multiplier.get(Constants.FPMultiplierStatus.M4) == null) {
multiplier.put(Constants.FPMultiplierStatus.M4, multiplier.get(Constants.FPMultiplierStatus.M3));
multiplier.put(Constants.FPMultiplierStatus.M3, null);
}
if (multiplier.get(Constants.FPMultiplierStatus.M3) == null) {
multiplier.put(Constants.FPMultiplierStatus.M3, multiplier.get(Constants.FPMultiplierStatus.M2));
multiplier.put(Constants.FPMultiplierStatus.M2, null);
}
if (multiplier.get(Constants.FPMultiplierStatus.M2) == null) {
multiplier.put(Constants.FPMultiplierStatus.M2, multiplier.get(Constants.FPMultiplierStatus.M1));
multiplier.put(Constants.FPMultiplierStatus.M1, null);
}
}
}
/** This class models the 4 steps floating point adder*/
private class Adder implements FPFunctionalUnit {
Map<Constants.FPAdderStatus, InstructionInterface> adder;
Adder() {
adder = new HashMap<>();
this.reset();
}
Map<Constants.FPAdderStatus, InstructionInterface> getFuncUnit() {
return adder;
}
public String toString() {
String output = "";
InstructionInterface instr;
output += "ADDER\n";
output += ((instr = adder.get(Constants.FPAdderStatus.A1)) != null) ? instr.getName() + "\n" : "EMPTY\n";
output += ((instr = adder.get(Constants.FPAdderStatus.A2)) != null) ? instr.getName() + "\n" : "EMPTY\n";
output += ((instr = adder.get(Constants.FPAdderStatus.A3)) != null) ? instr.getName() + "\n" : "EMPTY\n";
output += ((instr = adder.get(Constants.FPAdderStatus.A4)) != null) ? instr.getName() + "\n" : "EMPTY\n";
return output;
}
/** Resets the functional unit*/
public void reset() {
adder.put(Constants.FPAdderStatus.A1, null);
adder.put(Constants.FPAdderStatus.A2, null);
adder.put(Constants.FPAdderStatus.A3, null);
adder.put(Constants.FPAdderStatus.A4, null);
}
/** Inserts the passed instruction in the first position of the functional unit
* if another instruction holds that position a negative number is returned*/
public int putInstruction(InstructionInterface instr, boolean simulation) {
if (adder.get(Constants.FPAdderStatus.A1) == null) {
if (!simulation) {
adder.put(Constants.FPAdderStatus.A1, instr);
}
return 0;
} else {
return -1;
}
}
/** Returns the last instruction in the functional unit, if any instruction was found
* null is returned, the instruction is not removed from the HashMap */
public InstructionInterface getInstruction() {
return adder.get(Constants.FPAdderStatus.A4);
}
/** Remove the last instruction in the functional unit*/
void removeLast() {
adder.put(Constants.FPAdderStatus.A4, null);
}
/* Shifts instructions into the functional unit and calls the EX() method for the instruction in the secondary step
* this method is called from getCompletedInstruction in order to prepare the pipeline for a new instruction entrance */
public void step() {
if (adder.get(Constants.FPAdderStatus.A4) == null) {
adder.put(Constants.FPAdderStatus.A4, adder.get(Constants.FPAdderStatus.A3));
adder.put(Constants.FPAdderStatus.A3, null);
}
if (adder.get(Constants.FPAdderStatus.A3) == null) {
adder.put(Constants.FPAdderStatus.A3, adder.get(Constants.FPAdderStatus.A2));
adder.put(Constants.FPAdderStatus.A2, null);
}
if (adder.get(Constants.FPAdderStatus.A2) == null) {
adder.put(Constants.FPAdderStatus.A2, adder.get(Constants.FPAdderStatus.A1));
adder.put(Constants.FPAdderStatus.A1, null);
}
}
}
/** This class models the 24 steps floating point divider, instructions are not pipelined
* and for this reason a structural hazard happens when a DIV.fmt would to enter the FU when
* another DIV.fmt is present */
private class Divider implements FPFunctionalUnit {
InstructionInterface instr;
public int counter;
Divider() {
this.reset();
}
InstructionInterface getFuncUnit() {
return instr;
}
public String toString() {
if (instr != null) {
return "DIVIDER \n " + instr.getName() + " " + counter;
} else {
return "DIVIDER \n " + "EMPTY " + counter;
}
}
/** Inserts the passed instruction in the first position of the functional unit
* if another instruction holds that position a negative number is returned*/
public int putInstruction(InstructionInterface instr, boolean simulation) {
if (this.instr == null) {
if (!simulation) {
this.instr = instr;
this.counter = 24;
}
return 0;
}
return -1;
}
/** Returns the instruction if counter has reached 1 else
* null is returned*/
public InstructionInterface getInstruction() {
if (counter == 1) {
return this.instr;
}
return null;
}
/* Shifts instructions into the functional unit and calls the EX() method for the instruction in the secondary step
* this method is called from getCompletedInstruction in order to prepare the pipeline for a new instruction entrance */
public void step() {
//if counter has reached 0 the instruction was removed by the previous getCompletedInstruction invocation wich called removeLast()
//if counter is a number between 0 and 24 it must be decremented by 1
if (this.instr != null && counter > 0 && counter < 25) {
counter--;
}
//if the divider does not contain instructions anyone operation is carried out
}
/** Resets the functional unit*/
public void reset() {
instr = null;
counter = 0;
}
/** Return the counter of the divider*/
public int getCounter() {
return counter;
}
/** Removes the instruction in the functional unit (improper name for to conform to the others f.u.*/
void removeLast() {
this.instr = null;
this.counter = 0;
}
}
}