-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPU.java
More file actions
358 lines (329 loc) · 11.9 KB
/
CPU.java
File metadata and controls
358 lines (329 loc) · 11.9 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
import java.math.BigInteger;
import java.util.Arrays;
public class CPU extends Thread {
private PCB Current_Job;
private int cpuId;
private CpuState cpuState;
private String bin;
private String address;
private int reg1_index;
private int reg2_index;
private int reg3_index;
private int Address_Index;
private Register[] register = new Register[16];
private final String[] cache = new String[Driver.cache_size];
private int pc;
private boolean continueExec = true;
private final long start;
private long completion;
private int ioProcesses = 0;
private int jobCount;
private final String[] opcodeArray = {"RD", "WR", "ST", "LW", "MOV", "ADD", "SUB", "MUL", "DIV", "AND",
"OR", "MOVI", "ADDI", "MULI", "DIVI", "LDI", "SLT", "SLTI", "HLT", "NOP", "JMP", "BEQ",
"BNE", "BEZ", "BNZ", "BGZ", "BLZ"};
//initialize start time, id, and cpu state
public CPU (int id) {
this.start = System.currentTimeMillis();
this.cpuId = id;
this.cpuState = CpuState.FREE;
}
// This method loads an instruction at a specified address from RAM.
private String fetch(int index) {
return cache[index].substring(0, 8);
}
private void decode(String hex) {
StringBuilder binString = new StringBuilder();
binString.append(new BigInteger(hex, 16).toString(2));
// Adds leading zeros if bin string is less than 32 chars long
while (binString.toString().length() < 32) {
binString.insert(0, "0");
}
bin = binString.toString();
// Chars 0-1 indicate type of instruction (arithmetic, conditional, etc)
instructionFormat(bin.substring(0, 2));
// Chars 2-7 specify opcode of action. This is converted to decimal and used to index opcodeArray
String opcodeBinary = bin.substring(2, 8);
evaluate(opcodeArray[Integer.parseInt(opcodeBinary, 2)]);
}
// Main thread execution of the CPU class. Each CPU will independently check for remaining jobs and execute them accordingly.
@Override
public void run() {
// Checking if job is completed successfully
while (!Scheduler.hasNext()) {
PCB nextJob = Scheduler.nextJob();
if (nextJob != null) {
jobCount++;
cpuState = CpuState.EXECUTING;
Dispatcher.loadJob(nextJob, this);
nextJob.setRamUsage(MMU.ram_usage());
loadInstructionsToCache();
nextJob.setCacheUsage(getCacheUsage());
while (continueExec && pc < Current_Job.getNumberofInstructions()) {
// Creating an artificial execution time for each instruction
try {
Thread.sleep(Driver.thread_delay);
} catch (InterruptedException e) {
e.printStackTrace(); //prints error message for throawble object
}
String hex = fetch(pc);
pc++;
Current_Job.incrementProgramCounter();
decode(hex);
}
MMU.clear_all(Current_Job.getRamStart(), Current_Job.getRamEnd());
Dispatcher.unloadJob(Current_Job, this);
cpuState = CpuState.FREE;
clearCache();
}
}
completion = System.currentTimeMillis();
}
// Getter/setter methods
void resetProgramCounter() {
this.pc = 0;
this.continueExec = true;
}
void setCurrent_Job(PCB job) {
this.Current_Job = job;
}
PCB getCurrent_Job() {
return Current_Job;
}
void setRegisters(Register[] register) {
this.register = register;
}
Register[] getRegisters() {
return register;
}
public int getCpuId() {
return cpuId;
}
public long getCompletionTime() {
return completion - start;
}
public int getIOProcesses() {
return ioProcesses;
}
public int getJobCount() {
return jobCount;
}
// Clear cache array
void clearCache() {
Arrays.fill(cache, "");
}
// Reads info for the current job from RAM into cache.
void loadInstructionsToCache() {
for (int i = 0; i < Current_Job.getTotalSize(); i++) {
cache[i] = MMU.load_ram(Current_Job.getRamStart() + i);
}
}
// Get the number of currently loaded instructions in the cache.
public int getCacheUsage() {
int usage = 0;
for (String s : cache) {
if (s != null && !s.equals("")) {
usage++;
}
}
return usage;
}
// Initializes register indexes based on instruction format.
private void instructionFormat(String format) {
switch (format) {
case "00": { // ARITHMETIC
String reg1 = bin.substring(8, 12);
reg1_index = Integer.parseInt(reg1, 2);
String reg2 = bin.substring(12, 16);
reg2_index = Integer.parseInt(reg2, 2);
String reg3 = bin.substring(16, 20);
reg3_index = Integer.parseInt(reg3, 2);
break;
}
case "01": // CONDITIONAL
case "11": { // INPUT/OUTPUT
String reg1 = bin.substring(8, 12);
reg1_index = Integer.parseInt(reg1, 2);
String reg2 = bin.substring(12, 16);
reg2_index = Integer.parseInt(reg2, 2);
address = bin.substring(16, 32);
Address_Index = Integer.parseInt(address, 2) / 4; //returns corresponding absolute/physical address
break;
}
case "10": { // UNCONDITIONAL
address = bin.substring(8, 32);
Address_Index = Integer.parseInt(address, 2) / 4;
break;
}
}
}
// Method to provide operations for each opcode.
private void evaluate(String opcode) {
switch (opcode) {
case "RD": {
if (Address_Index == 0) {
register[reg1_index].data = Integer.parseInt(cache[register[reg2_index].data], 16);
} else {
register[reg1_index].data = Integer.parseInt(cache[Address_Index], 16);
}
ioProcesses++;
Current_Job.incrementIoProcesses();
break;
}
case "WR": {
cache[Address_Index] = Integer.toHexString(register[reg1_index].data);
ioProcesses++;
Current_Job.incrementIoProcesses();
break;
}
case "ST": {
if (Address_Index == 0) {
cache[register[reg2_index].data] = Integer.toHexString(register[reg1_index].data);
} else {
cache[Address_Index] = Integer.toHexString(register[reg1_index].data);
}
break;
}
case "LW": {
if (Address_Index == 0) {
register[reg2_index].data = Integer.parseInt(cache[register[reg1_index].data], 16);
} else {
register[reg2_index].data = Integer.parseInt(cache[Address_Index], 16);
}
break;
}
case "MOV": {
register[reg3_index].data = register[reg1_index].data;
break;
}
case "ADD": {
register[reg3_index].data = register[reg1_index].data + register[reg2_index].data;
break;
}
case "SUB": {
register[reg3_index].data = register[reg1_index].data - register[reg2_index].data;
break;
}
case "MUL": {
register[reg3_index].data = register[reg1_index].data * register[reg2_index].data;
break;
}
case "DIV": {
if (register[reg2_index].data != 0) {
register[reg3_index].data = register[reg1_index].data / register[reg2_index].data;
}
break;
}
case "AND": {
if (register[reg1_index].data != 0 && register[reg2_index].data != 0) {
register[reg3_index].data = 1;
} else {
register[reg3_index].data = 0;
}
break;
}
case "OR": {
if (register[reg1_index].data == 1 || register[reg2_index].data == 1) {
register[reg3_index].data = 1;
} else {
register[reg3_index].data = 0;
}
break;
}
case "MOVI": {
register[reg2_index].data = Integer.parseInt(address, 2);
break;
}
case "ADDI": {
register[reg2_index].data++;
break;
}
case "MULI": {
register[reg2_index].data = register[reg2_index].data * Address_Index;
break;
}
case "DIVI": {
if (Address_Index != 0) {
register[reg2_index].data = register[reg2_index].data / Address_Index;
}
break;
}
case "LDI": {
register[reg2_index].data = Address_Index;
break;
}
case "SLT": {
if (register[reg1_index].data < register[reg2_index].data) {
register[reg3_index].data = 1;
} else {
register[reg3_index].data = 0;
}
break;
}
case "SLTI": {
if (register[reg1_index].data < Address_Index) {
register[reg2_index].data = 1;
} else {
register[reg2_index].data = 0;
}
break;
}
case "HLT": {
continueExec = false;
break;
}
case "NOP": {
pc++;
break;
}
case "JMP": {
pc = Address_Index;
break;
}
case "BEQ": {
if (register[reg1_index].data == register[reg2_index].data) {
pc = Address_Index;
}
break;
}
case "BNE": {
if (register[reg1_index].data != register[reg2_index].data) {
pc = Address_Index;
}
break;
}
case "BEZ": {
if (register[reg2_index].data == 0) {
pc = Address_Index;
}
break;
}
case "BNZ": {
if (register[reg1_index].data != 0) {
pc = Address_Index;
}
break;
}
case "BGZ": {
if (register[reg1_index].data > 0) {
pc = Address_Index;
}
break;
}
case "BLZ": {
if (register[reg1_index].data < 0) {
pc = Address_Index;
}
break;
}
}
}
// enum that depicts the current state of the CPU.
public enum CpuState {
FREE,
EXECUTING
}
@Override
public String toString() {
return "ID: " + cpuId + " | State: " + cpuState;
}
}