-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScoreboard.java
More file actions
217 lines (216 loc) · 7.45 KB
/
Scoreboard.java
File metadata and controls
217 lines (216 loc) · 7.45 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package scoreboard;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Logger;
import java.util.logging.Level;
/**
*
* @author lovelinanand
*/
public class Scoreboard {
private static final Logger LOGGER = Logger.getGlobal();
public static int cycle;
private InstructionsList itTable;
private RegisterList rTable;
public UnitsList uTable;
public InstructionQueue iQueue;
public int totalInstructions;
public int issueWidth;
public int cacheMiss;
public Integer[] dumpCycleNo;
public String orderType = "inorder";
public Scoreboard(String inputFile, String orderType){
LOGGER.info("Initializing Scoreboard");
this.itTable = new InstructionsList(orderType);
this.loadInstructions(inputFile);
this.checkConfig();
this.rTable = new RegisterList();
this.uTable = new UnitsList(this.cacheMiss);
this.iQueue = new InstructionQueue();
this.orderType = orderType;
this.loadRegisters();
cycle = 0;
}
public void checkConfig(){
this.cacheMiss = this.itTable.getCacheMiss();
this.issueWidth = this.itTable.getIssueWidth();
this.dumpCycleNo = this.itTable.getDump();
}
public void loadRegisters(){
for(Integer key : this.itTable.keyList()){
Instructions ins = this.itTable.get(key);
if(ins.getSource1() != null){
this.rTable.create(ins.getSource1());
}
if(ins.getSource2() != null){
this.rTable.create(ins.getSource2());
}
if(ins.getDestination() != null){
this.rTable.create(ins.getDestination());
}
}
}
public void loadInstructions(String inputFile){
LOGGER.fine("Loading Instructions");
BufferedReader br = null;
String currentLine;
int count = 0;
try{
br = new BufferedReader(new FileReader(inputFile));
while((currentLine = br.readLine()) != null){
this.itTable.add(currentLine, count);
count ++;
}
this.totalInstructions = count;
}catch(IOException E){
System.out.println("Error in reading file");
E.printStackTrace();
System.exit(0);
}
}
public boolean issueInorder(){
boolean instructionIssued = false;
Instructions nextIns = this.itTable.next();
if(nextIns != null){
LOGGER.fine("Next instruction to be issue is "+ nextIns);
if(this.uTable.canIssue(nextIns)){
LOGGER.fine("Functional unit is free "+ nextIns);
if(!this.rTable.isDependant(nextIns)){
LOGGER.fine(nextIns + " Dependancy not present, can be issued");
this.issueInstruction(nextIns);
instructionIssued = true;
}else{
LOGGER.fine(nextIns +" Dependancy present, cannot issue");
}
}else{
LOGGER.fine(nextIns +" Function unit is full, cannot issue");
}
}else{
LOGGER.fine("No instructions available");
}
return instructionIssued;
}
public boolean issueOutorder(){
boolean isIssued = false;
for(Instructions nextIn : this.iQueue.queue){
if(this.uTable.canIssue(nextIn)){
LOGGER.fine("Functional unit available, can issue");
if(!this.iQueue.isAntiDependant(nextIn)){
LOGGER.fine("No anti dependancy, checking true dependancy");
if(!this.rTable.isDependant(nextIn)){
LOGGER.fine("No true ore WAW dependancy");
this.issueInstruction(nextIn);
this.iQueue.removeIns(nextIn);
isIssued = true;
break;
}else{
LOGGER.fine("True dependancy or WAW dependancy");
}
}else{
LOGGER.fine("Anti dependancy");
}
}else{
LOGGER.fine("Functional unit not available, cannot issue");
}
}
return isIssued;
}
public boolean isInOrder(){
return this.orderType.equals("inorder");
}
private boolean dispatchInstruction() {
if(this.isInOrder()){
return false;
}
boolean instructionDispatched = false;
Instructions nextIns = this.itTable.nextDispatch();
if(nextIns != null){
LOGGER.fine("Next instruction to be dispatched is"+nextIns);
if(this.iQueue.canBeDispatched(nextIns)){
this.iQueue.dispatch(nextIns);
instructionDispatched = true;
}else{
LOGGER.fine("Instruction cannot be dispatched, has dependancy");
}
}else{
LOGGER.fine("No instructions available to dispatch");
}
return instructionDispatched;
}
public boolean issueInorOutofOrder(){
if(this.isInOrder()){
return this.issueInorder();
}else{
return this.issueOutorder();
}
}
public void checkNextInstruction(){
int count = 0;
while(count < this.issueWidth){
if(!this.issueInorOutofOrder()){
break;
}
count ++;
}
}
public void issueInstruction(Instructions instr){
this.uTable.issue(instr);
this.rTable.setBusy(instr);
}
public void advanceClock(){
this.uTable.advanceClock(rTable);
LOGGER.fine("Advancing clock");
}
public void dumpScoreboard(){
for(Integer value : this.dumpCycleNo){
if(value == cycle){
this.uTable.dump();
this.rTable.dump();
this.itTable.dump();
}
}
}
public void start(){
LOGGER.info("Starting the scoreboard process");
LOGGER.info("CacheMiss: "+this.cacheMiss + " IssueWidth: "+ this.issueWidth);
while(this.itTable.isCompleted() != true){
System.out.println("-----------------------");
System.out.println("Cycle " + cycle);
System.out.println("-----------------------");
this.advanceClock();
this.checkNextInstruction();
this.dispatchInstruction();
this.dumpScoreboard();
cycle ++ ;
}
}
public static void setDebug(){
LOGGER.setUseParentHandlers(false);
Handler systemOut = new ConsoleHandler();
systemOut.setLevel( Level.ALL );
LOGGER.addHandler( systemOut );
LOGGER.setLevel( Level.ALL );
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// setDebug();
if(args.length != 2){
System.out.println("Usage: ./scoreboard <input_txt> <order_type>");
System.exit(0);
}
String input_txt = args[0];
String order_type = args[1];
Scoreboard scorebd = new Scoreboard(input_txt, order_type);
scorebd.start();
}
}