-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputer.java
284 lines (261 loc) · 18 KB
/
computer.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
package ComputerArchitecture;
/*
Jan Karl Galia
Comp. Architecture
HW 6: Computer
*/
public class computer {
private Bit halt = new Bit(0);
private final memory Memory = new memory();
private Longword PC = new Longword(); //Address
private Longword currentInstruction = new Longword();//Stores the value from fetch()
private final Longword addTwo = new Longword(); //Adding 2 Bytes to PC
private final Longword addFour = new Longword(); //Adding 4 Bytes to PC
private final Bit bitTrue = new Bit(1); //To set longword Bits to true
private final Bit bitFalse = new Bit(0); //To set longword Bits to false
private Longword[] register = new Longword[16];//Registers, Register[i] is 32 Bits
private Longword op1 = new Longword();
private Longword op2 = new Longword();
private final Longword mask = new Longword(); //1111
private Longword result = new Longword();//Stores Result
private Longword preloadAddressTrack = new Longword();//For preload method
private Bit[] compareResult = new Bit[2];//Compare Result Bits for Branching(Used in store)
private Longword mask1s = new Longword(); // Mask of 1s
private Longword signedAddress = new Longword();
private Longword SP = new Longword();
private Longword returnAddress = new Longword();
public void run() {
//Initializing Stack Pointer to Byte 1020 (Bit 8160)
for (int i = 5; i <= 12; i++) SP.setBit(i, bitTrue);
mask1s = mask1s.not();//For Branching
//Compare Result Bits for Branching (Used in Store)
compareResult[0] = new Bit();
compareResult[1] = new Bit();
for (int i = 0; i < 4; i++) mask.setBit(i, bitTrue); //For Decode
while (!halt.getValue()) {//Loop Runs when halt == false
fetch();
decode();
execute();
store();
}
if (halt.getValue()) {
System.out.println("-----------COMPUTER HALTED----------------");
}
}
public void fetch() {//Fetching Op Code
if (PC.getSigned() >= 8160) halt.set(true); //Stops when PC is about reach 8192
System.out.println("PC:"+ PC.getSigned()+" Instructions In PC: "+ Memory.read(PC).leftShift(16).rightShift(16).toStringConvert());
if (PC.getSigned() < 0) {
System.out.println("Memory is negative -- Halting Computer");
halt.set(true);
}
if (PC.getSigned() >= 0) {
currentInstruction = Memory.read(PC); //Reads the memory of the address given by PC---> PC > 0, PC > 16, PC >32
PC = rippleAdder.add(addTwo, PC); //Incrementing PC by 2 Bytes
}
// System.out.println("PC: " + PC.getSigned());
// System.out.println("Test SP: " + SP.getSigned());
}
public void decode() {
//Compare (0100)
if (!currentInstruction.getBit(12).getValue() && !currentInstruction.getBit(13).getValue() && currentInstruction.getBit(14).getValue() && !currentInstruction.getBit(15).getValue()) {
if (register[currentInstruction.leftShift(24).rightShift(28).and(mask).getSigned()] != null)
op1.copy(register[currentInstruction.leftShift(24).rightShift(28).and(mask).getSigned()]); //Rx
if (register[currentInstruction.leftShift(28).rightShift(28).and(mask).getSigned()] != null)
op2.copy(register[currentInstruction.leftShift(28).rightShift(28).and(mask).getSigned()]);//Ry
}
//If other instruction
else if (!(!currentInstruction.getBit(12).getValue() && currentInstruction.getBit(13).getValue() && !currentInstruction.getBit(14).getValue() && !currentInstruction.getBit(15).getValue())) //!Interrupt 0 & 1
if (!(!currentInstruction.getBit(12).getValue() && currentInstruction.getBit(13).getValue() && currentInstruction.getBit(14).getValue() && !currentInstruction.getBit(15).getValue()))//!Push, Pop, Call, Return
if (!(currentInstruction.getBit(12).getValue() && !currentInstruction.getBit(13).getValue() && currentInstruction.getBit(14).getValue() && !currentInstruction.getBit(15).getValue()))//!Branch
if (!(currentInstruction.getBit(12).getValue() && currentInstruction.getBit(13).getValue() && !currentInstruction.getBit(14).getValue() && !currentInstruction.getBit(15).getValue())) //!Jump
if (!(!currentInstruction.getBit(12).getValue() && !currentInstruction.getBit(13).getValue() && !currentInstruction.getBit(14).getValue() && !currentInstruction.getBit(15).getValue())) //!Halt
if (!(currentInstruction.getBit(12).getValue() && !currentInstruction.getBit(13).getValue() && !currentInstruction.getBit(14).getValue() && !currentInstruction.getBit(15).getValue()))//!Move
{
if (register[currentInstruction.leftShift(20).rightShift(28).and(mask).getSigned()] != null)
op1.copy(register[currentInstruction.leftShift(20).rightShift(28).and(mask).getSigned()]); //Left Shift by 20, Right shift by 28 bits, then and with mask to get first register
if (register[currentInstruction.leftShift(24).rightShift(28).and(mask).getSigned()] != null)
op2.copy(register[currentInstruction.leftShift(24).rightShift(28).and(mask).getSigned()]);//Left Shift by 24, Right shift by 28 bits, then and with mask to get second register
}
}
public void execute() {
result = new Longword();//Resets result for holding new instruction every loop
//------------------------------------------------------------------------
//INTERRUPT 0: Print all the registers to the screen
if ((!currentInstruction.getBit(12).getValue() && currentInstruction.getBit(13).getValue() && !currentInstruction.getBit(14).getValue() && !currentInstruction.getBit(15).getValue() && !currentInstruction.getBit(0).getValue())) {
for (int i = 0; i < 16; i++) {
if (register[i] == null) {//If register is not defined
System.out.println("Register[" + i + "] is NULL");
} else System.out.println("Register[" + i + "] is " + register[i].toStringConvert());
}
}//INTERRUPT 1: Print all 1024 Bytes of memory to the screen
if ((!currentInstruction.getBit(12).getValue() && currentInstruction.getBit(13).getValue() && !currentInstruction.getBit(14).getValue() && !currentInstruction.getBit(15).getValue() && currentInstruction.getBit(0).getValue())) {
Longword addressTracker = new Longword();//For printing all Bytes
Longword add32 = new Longword(); //Adds 32 Bits to address tracker
add32.setBit(5, bitTrue); //Adding to address every iteration
for (int i = 0; i < 256; i++) { //256 Longwords
System.out.println(Memory.read(addressTracker).toStringConvert());
addressTracker = rippleAdder.add(addressTracker, add32);
}
}
//---------------------------------------------------------------------------
if ((!currentInstruction.getBit(12).getValue() && !currentInstruction.getBit(13).getValue() && !currentInstruction.getBit(14).getValue() && !currentInstruction.getBit(15).getValue())) {// HALT!!!!!!
halt.set(true);//Sets halt bit to true
return;//Ends Method
}
//In execute, pass the control bits (the opcode) into the ALU along with op1 and op2.
Bit Bits[] = new Bit[4];
String opCode = "";//Converting Bit[] into string of 1,0 and sets it into opCode
int track = 3;
for (int i = 0; i < 4; i++) {//Extracting ALU bits in currentInstruction
if (currentInstruction.leftShift(16).rightShift(28).getBit(track).getValue()) {
Bits[i] = new Bit(1);
opCode = opCode + "1";
} else if (!currentInstruction.leftShift(16).rightShift(28).getBit(track).getValue()) {
Bits[i] = new Bit(0);
opCode = opCode + "0";
}
track--;
}
if (!currentInstruction.getBit(12).getValue() && !currentInstruction.getBit(13).getValue() && currentInstruction.getBit(14).getValue() && !currentInstruction.getBit(15).getValue()) {//If compare then subtract
Bits[0].set(true);
Bits[1].set(true);
Bits[2].set(true);
Bits[3].set(true);
}
if (!halt.getValue()) {
if (!Bits[0].getValue() && !Bits[1].getValue() && !Bits[2].getValue() && Bits[3].getValue()) { //For Move Instruction
result.copy(currentInstruction);
if (currentInstruction.getBit(7).getValue()) {//If negative then set rest of result 32 Bit to 1s
for (int i = 8; i < 32; i++)
result.setBit(i, bitTrue);
}
if (!currentInstruction.getBit(7).getValue()) {//If positive then set rest of result 32 Bit to 1s
for (int i = 8; i < 32; i++) result.setBit(i, bitFalse);
}
} else {//For any other instruction
result = new ALU().doOp(Bits, op1, op2);// Create a new longword called result and put the result from the ALU into the result longword.Calls the ALU
}
}
//Branch(0101)
if (currentInstruction.getBit(12).getValue() && !currentInstruction.getBit(13).getValue() && currentInstruction.getBit(14).getValue() && !currentInstruction.getBit(15).getValue()) {
//Signed Address
//If positive (sign = 0)
if (!currentInstruction.getBit(9).getValue()) {
signedAddress = currentInstruction.leftShift(22).rightShift(22);
}
//If negative(sign = 1)
if (currentInstruction.getBit(9).getValue()) {
signedAddress = currentInstruction.leftShift(22).rightShift(22).or(mask1s.leftShift(10));
}
}
}
public void store() {
//Copies result into destination register
if (!halt.getValue()) {
//Push, Pop, Call, Return
if (!currentInstruction.getBit(12).getValue() && currentInstruction.getBit(13).getValue() && currentInstruction.getBit(14).getValue() && !currentInstruction.getBit(15).getValue()) {
//Push(00) Bit 11, Bit 10
if (!currentInstruction.getBit(10).getValue() && !currentInstruction.getBit(11).getValue()) {
Memory.write(SP, register[currentInstruction.leftShift(28).rightShift(28).getSigned()]); //Write into Stack
System.out.println("Push Stack Value: "+Memory.read(SP).getSigned()+" SP: "+SP.getSigned());
SP.copy(rippleAdder.sub(SP, addFour));//Subtract 4 Bytes to SP
}
//Pop(01)
if (currentInstruction.getBit(10).getValue() && !currentInstruction.getBit(11).getValue()) {
SP.copy(rippleAdder.add(SP, addFour));//Add 4 Bytes to SP
register[currentInstruction.leftShift(28).rightShift(28).getSigned()] = Memory.read(SP); //Reads Stack
System.out.println("Popping Stack Value: "+Memory.read(SP).getSigned()+" SP: "+SP.getSigned());
Memory.write(SP, new Longword()); //Converts data in current SP to 0s
}
//Call(10)
if (!currentInstruction.getBit(10).getValue() && currentInstruction.getBit(11).getValue()) {
Memory.write(SP, PC); //Pushes the address into the stack
System.out.println("Calling Stack Value: "+Memory.read(SP).getSigned()+" SP: "+SP.getSigned());
SP.copy(rippleAdder.sub(SP, addFour));//Subtract 4 Bytes to SP
PC.copy(currentInstruction.leftShift(22).rightShift(22));//Call then Jumps
}
//Return(11)
if (currentInstruction.getBit(10).getValue() && currentInstruction.getBit(11).getValue()) {
SP.copy(rippleAdder.add(SP, addFour));//Add 4 Bytes to SP
System.out.println("Returning Stack Value: "+Memory.read(SP).getSigned()+" SP: "+SP.getSigned());
register[currentInstruction.leftShift(28).rightShift(28).getSigned()] = (Memory.read(SP)); //pops
Memory.write(SP, new Longword()); //Converts data in current SP to 0s
PC.copy(register[currentInstruction.leftShift(28).rightShift(28).getSigned()]);//Jumping back to address
}
}
//Branch
else if (currentInstruction.getBit(12).getValue() && !currentInstruction.getBit(13).getValue() && currentInstruction.getBit(14).getValue() && !currentInstruction.getBit(15).getValue()) {
//Branch or Jump to signed address
//If Equal, CurrentInstruction[11] == true, and CurrentInstruction[11] == compareResult[1]
if (currentInstruction.getBit(11).getValue() && compareResult[1].getValue()) {
PC = rippleAdder.add(PC, signedAddress);//Branching to Address
System.out.println("Branching to: " + PC.toStringConvert());
}
//If NotEqual, CurrentInstrution[11] == false, and CurrentInstruction[10] == compareResult[0]
//If Greater than
else if (!currentInstruction.getBit(11).getValue() && !compareResult[1].getValue() && currentInstruction.getBit(10).getValue() && compareResult[0].getValue()) {
PC = rippleAdder.add(PC, signedAddress);//Branching to Address
System.out.println("Branching to: " + PC.toStringConvert());
}
//If less than
else if (!currentInstruction.getBit(11).getValue() && !compareResult[1].getValue() && !currentInstruction.getBit(10).getValue() && !compareResult[0].getValue()) {
PC = rippleAdder.add(PC, signedAddress);//Branching to Address
System.out.println("Branching to: " + PC.toStringConvert());
}
//Else, Don't Branch or Jump
}
//Jump Instruction(0011)
else if (currentInstruction.getBit(12).getValue() && currentInstruction.getBit(13).getValue() && !currentInstruction.getBit(14).getValue() && !currentInstruction.getBit(15).getValue()) {
PC.copy(currentInstruction.leftShift(20).rightShift(20));//Gets the address value to jump to
System.out.println("Jumping to Address: " + currentInstruction.leftShift(20).rightShift(20).toStringConvert());
}//If compare instruction
else if (!currentInstruction.getBit(12).getValue() && !currentInstruction.getBit(13).getValue() && currentInstruction.getBit(14).getValue() && !currentInstruction.getBit(15).getValue()) {
//Bit 0: (Negative)0 ifLessThan, (Positive)1 ifGreaterThan ; either one if equal
if (result.getSigned() < 0) compareResult[0].set(false);
else if (result.getSigned() > 0) compareResult[0].set(true);
//Bit 1: 0 if not equal, 1 if equal
if (result.getSigned() != 0) compareResult[1].set(false);
else compareResult[1].set(true);
}
//If move instruction
else if (currentInstruction.getBit(12).getValue() && !currentInstruction.getBit(13).getValue() && !currentInstruction.getBit(14).getValue() && !currentInstruction.getBit(15).getValue()) {
register[currentInstruction.leftShift(20).rightShift(28).and(mask).getSigned()] = result;
} else { //If other Instruction
register[currentInstruction.leftShift(28).rightShift(28).and(mask).getSigned()] = result;//Left Shift by 24, Right shift by 28 bits, then and with mask to get second register
}
}
}
public void preload(String program[]) {
Longword hold = new Longword(); //Holds the instructions
int numInstructions = program.length;//Gives us number of instructions
addTwo.setBit(4, bitTrue); //Adds 16 Bits, 2 Bytes for PC
addFour.setBit(5, bitTrue); //Adds 32 Bits, 4 Bytes for SP
int biti = 0;
for (int j = 0; j < numInstructions; j++) {
biti = 0;
program[j] = program[j].replaceAll("\\s", "");//Gets rid of white spaces from program strings
for (int i = 15; i >= 0; i--) {//Copies each instruction into hold
if (program[j].charAt(i) == '0') hold.setBit(biti, bitFalse);
if (program[j].charAt(i) == '1') hold.setBit(biti, bitTrue);
biti++;
}
Memory.write(preloadAddressTrack, hold);
preloadAddressTrack = rippleAdder.add(preloadAddressTrack, addTwo);//Increases address by 2 Bytes
//Preloading address if Call and Return are called
// If Call(011010)
if (!hold.getBit(15).getValue() && hold.getBit(14).getValue() && hold.getBit(13).getValue()
&& !hold.getBit(12).getValue() && hold.getBit(11).getValue() && !hold.getBit(10).getValue()) {//Call
returnAddress.copy(preloadAddressTrack);
// System.out.println("Call:"+preloadAddressTrack.getSigned()); //Testing
preloadAddressTrack.copy(hold.leftShift(22).rightShift(22));//Preload jumps to the address indicated by call
}
//If Return(011011)
if (!hold.getBit(15).getValue() && hold.getBit(14).getValue() && hold.getBit(13).getValue()
&& !hold.getBit(12).getValue() && hold.getBit(11).getValue() && hold.getBit(10).getValue()) {
// System.out.println("Return:"+preloadAddressTrack.getSigned()); //Testing
preloadAddressTrack= returnAddress;
}
}
}
}