-
Notifications
You must be signed in to change notification settings - Fork 0
/
Processor.java
100 lines (80 loc) · 2.05 KB
/
Processor.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
/* a Processor object contains the CPU queue,
* the registers and the current instruction
* counter.
*/
import java.util.ArrayDeque;
import java.util.Vector;
public class Processor {
private int instructionCounter;
// Queue where received values are stored
private ArrayDeque<Integer> queue;
// Registers
private Vector<Integer> registers;
// CPU state, if it is true is listening
private boolean listening;
// CPU identifier
private int id;
// CPU execution state, if it is true all the instructions were executed
private boolean state;
public Processor(int n) {
instructionCounter = 0;
queue = new ArrayDeque<Integer>();
// Initializing registers
registers = new Vector<Integer>(32);
registers.add(0, n);
for (int i = 1; i < 32; i ++) {
registers.add(i, 0);
}
id = n;
listening = false;
state = false;
}
public int getCoreNumber() {
return id;
}
public void setState(boolean x) {
state = x;
}
public boolean done() {
return this.state;
}
public int getInstructionCounter() {
return instructionCounter;
}
public void setInstructionCounter(int x) {
instructionCounter = x;
}
public void incCounter() {
instructionCounter ++;
}
public void enqueueValue(int x) {
queue.add(x);
}
public int dequeueValue() {
return queue.poll();
}
public boolean isQueueEmpty() {
return queue.isEmpty();
}
public void setRegister(int n, int x) {
registers.set(n, x);
}
public int getRegister(int n) {
return registers.get(n);
}
public boolean isListening() {
return listening;
}
public void setListening(boolean x) {
listening = x;
}
public String nonZeroRegisters() {
String s = new String();
for (int i : registers) {
if (i != 0) {
s += i + " ";
}
}
return s;
}
}