-
Notifications
You must be signed in to change notification settings - Fork 0
/
computer.java
506 lines (434 loc) · 17.1 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
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
import java.sql.SQLOutput;
public class computer
{
//Running bit, if computer is running or not
bit running = new bit(true);
//Program counter
longword PC = new longword();
longword SP = new longword();
//Current Instruction
longword currentInstruction = new longword();
//operator 1
longword op1 = new longword();
//operator 2
longword op2 = new longword();
//Move register
longword movReg = new longword();
//Move Value for the register
longword movValue = new longword();
//Result to store
longword result = new longword();
longword jumpVal = new longword();
longword branchAddress = new longword();
//If we are doing a move operation
boolean movFlag = false;
//If we are dealing with a negative value
boolean negativeFlag = false;
//If we need to store something
boolean storeFlag = false;
//if we need to jump
boolean jumpFlag = false;
boolean conditionalJumpFlag = false;
//Result of move
longword movResult = new longword();
//memory should be private
//Our storage for the computer
private memory storage = new memory();
longword registers[] = new longword[16];
bit comp0 = new bit();
bit comp1 = new bit();
//Computer Default constructor
public computer() throws Exception
{
//initialize SP to 1020
SP.set(1020);
for(int i = 0; i < registers.length; i++)
{
registers[i] = new longword();
}
}
//Run
public void run() throws Exception
{
//While bit is true (running), fetch, decode, execute, store
while(running.getValue())
{
fetch();
decode();
execute();
store();
}
}
public void fetch() throws Exception
{
//Save the current instruction from program counter
//read off PC and then set currentInstruction to PC value
currentInstruction.copy(storage.read(PC));
//Longword of the value two to increment the PC by
longword two = new longword();
two.set(2);
//Increment the PC.
PC = rippleAdder.add(PC, two);
}
//get the values from the registers and prepare for the ALU
public void decode() throws Exception
{
//mask of 0000 0000 0000 0000 0000 0000 0000 1111
//This will get us the first 4 bits we want
//Shift the instruction over however many bits necessary to get the value
longword mask = new longword();
mask.set(15);
longword mask2 = new longword();
mask2.set(255);
//0000 0000 0000 0000 0000 0000 1111 1111
//shift right 24 bits for reg1
//shift right 20 bits for reg2
//to get op1
longword shifted = currentInstruction.rightShift(24);
longword importantBits = shifted.and(mask);
int registerNumber = importantBits.getSigned();
op1 = registers[registerNumber];
//to get op2
longword shifted2 = currentInstruction.rightShift(20);
longword importantBits2 = shifted2.and(mask);
int registerNumber2 = importantBits2.getSigned();
op2 = registers[registerNumber2];
//Get info for mov if its is needed.
//Just a different way of interpreting
movReg = currentInstruction.rightShift(24).and(mask);
movValue = currentInstruction.rightShift(16).and(mask2);
}
public void execute() throws Exception
{
//mask of 0000 0000 0000 0000 0000 0000 0000 1111
longword mask = new longword();
mask.set(15);
//shift right 12 for reg3 (destination)
//Operation is array of 4 bits
bit operation[] = new bit[4];
operation[0] = new bit();
operation[1] = new bit();
operation[2] = new bit();
operation[3] = new bit();
//Getting the first 4 bits and then assigning.
for(int i = 0; i < 4; i++)
{
operation[i].set(currentInstruction.getBit(i).getValue());
}
if(operation[0].getValue() == false && operation[1].getValue() == false && operation[2].getValue() == false && operation[3].getValue() == false)
{
//halt
//Set running to 0
running.set(false);
}
else if(operation[0].getValue() == false && operation[1].getValue() == false && operation[2].getValue() == false && operation[3].getValue() == true)
{
movFlag = true;
//move
//find the register that we need
//write the value into that register.
//If negative value
if(currentInstruction.getBit(8).getValue())
{
negativeFlag = true;
//Negative sum, working backwards
longword negativeSum = new longword();
negativeSum.set(-256);
//operation array
bit[] addOP = {new bit(true), new bit(true), new bit(true), new bit()};
//pass in addition, and two operators
movResult = ALU.doOp(addOP, negativeSum, movValue);
}
}
else if(operation[0].getValue() == false && operation[1].getValue() == false && operation[2].getValue() == true && operation[3].getValue() == false)
{
//Interrupt: 2 types
//Print addresses
if(currentInstruction.getBit(15).getValue())
{
longword currentAddress = new longword();
//– print all 1024 bytes of memory to the screen.
for(int i = 0; i <= 1020; i = i+4)
{
currentAddress.set(i);
System.out.println("Address["+i+"]: "+storage.read(currentAddress));
}
}
else
{
//Print out all the registers
for(int i = 0; i < registers.length; i++)
{
System.out.println("R"+i+": "+registers[i].getSigned());
}
}
}
else if((operation[0].getValue() == false && operation[1].getValue() == false && operation[2].getValue() == true && operation[3].getValue() == true))
{
jumpFlag = true;
longword jumpMask = new longword();
jumpMask.set(4095);
//mask of 0000 0000 0000 0000 0000 1111 1111 1111
jumpVal = currentInstruction.rightShift(16).and(jumpMask);
}
else if((operation[0].getValue() == false && operation[1].getValue() == true && operation[2].getValue() == false && operation[3].getValue() == false))
{
//compare reg xxxx yyyy
//set bits accordingly
longword compareMask = new longword();
//mask of 0000 0000 0000 0000 0000 0000 0000 1111
compareMask.set(15);
//REGISTER YYYY -> SHIFT 16
//REGISTER XXXX -> SHIFT 20
longword registerX = currentInstruction.rightShift(20).and(compareMask);
longword registerY = currentInstruction.rightShift(16).and(compareMask);
int regX = registerX.getSigned();
int regY = registerY.getSigned();
int valueInX = registers[regX].getSigned();
int valueInY = registers[regY].getSigned();
//Bit 0: 0 if X<Y, 1 if X>Y
//Bit 1: 0 if not Equal, 1 if Equal
if(valueInX - valueInY > 0)
{
//X>Y
comp0.set(true);
comp1.set(false);
}
else if(valueInX - valueInY < 0)
{
//X < Y
comp0.set(false);
comp1.set(false);
}
else if(valueInX - valueInY == 0)
{ //X==Y
comp0.set(false);
comp1.set(true);
}
}
else if((operation[0].getValue() == false && operation[1].getValue() == true && operation[2].getValue() == false && operation[3].getValue() == true))
{
//Declaring longword masks
longword addressMask = new longword();
longword signMask = new longword();
longword conditionMask = new longword();
//Setting masks
addressMask.set(511);
signMask.set(1);
conditionMask.set(3);
//sihfting and masking to get the bits we need
longword branchAddressLW = currentInstruction.rightShift(16).and(addressMask);
longword sign = currentInstruction.rightShift(25).and(signMask);
longword conditionalCode = currentInstruction.rightShift(26).and(conditionMask);
//Declaring conditional bits and assigning them for bit compare
bit conditionalB0 = new bit();
bit conditionalB1 = new bit();
//Setting bits
if(conditionalCode.getBit(31).getValue())
{
conditionalB0.set(true);
}
if(conditionalCode.getBit(30).getValue())
{
conditionalB1.set(true);
}
//If the conditional code matches from compare, then go in and then execute the jump
if(conditionalB0.getValue() == comp0.getValue() && conditionalB1.getValue() == comp1.getValue())
{
//Turn the conditional jump flag on
conditionalJumpFlag = true;
//If its a negative address
if(sign.getBit(0).getValue())
{
//Negative sum, working backwards
longword negativeSum = new longword();
negativeSum.set(-512);
//operation array
bit[] addOP = {new bit(true), new bit(true), new bit(true), new bit()};
//pass in addition, and two operators
branchAddress = ALU.doOp(addOP, negativeSum, branchAddressLW);
}
//If not negative address, continue as normal
else
{
branchAddress.set(branchAddressLW.getSigned());
}
}
//Specific for branchIfNotEqual
else if(!conditionalB0.getValue() && !conditionalB1.getValue())
{
//Flip flag on
conditionalJumpFlag = true;
//If negative value, compute the negative sum and continue
if(sign.getBit(0).getValue())
{
//Negative sum, working backwards
longword negativeSum = new longword();
negativeSum.set(-512);
//operation array
bit[] addOP = {new bit(true), new bit(true), new bit(true), new bit()};
//pass in addition, and two operators
branchAddress = ALU.doOp(addOP, negativeSum, branchAddressLW);
}
//Otherwise, continue as normal
else
{
branchAddress.set(branchAddressLW.getSigned());
}
}
else
{
//if conditionals dont match, do nothing
}
}
//0110 opcode for push pop call return
else if(operation[0].getValue() == false && operation[1].getValue() == true && operation[2].getValue() == true && operation[3].getValue() == false)
{
//every time we push, subtract 4
//every time we pop, add 4
//finding our opcode bits
longword opcodeBits = new longword();
opcodeBits.set(3);
//Getting the conditionalCode bits for push pop call return
longword conditionalCode = currentInstruction.rightShift(26).and(opcodeBits);
//new bits to hold those values
bit opCode1 = new bit();
bit opCode2 = new bit();
//Setting bits
if(conditionalCode.getBit(31).getValue())
{
opCode1.set(true);
//otherwise false;
}
if(conditionalCode.getBit(30).getValue())
{
opCode2.set(true);
//otherwise false;
}
//opCode1 opCode2 == bit combination to determine operation
//Making masks for shifting
longword four = new longword();
four.set(4);
longword registerMask = new longword();
registerMask.set(15);
longword addressMask = new longword();
addressMask.set(1023);
//Right shift and mask to get the 10 bit address we need
int regNum = currentInstruction.rightShift(16).and(registerMask).getSigned();
longword addressJump = currentInstruction.rightShift(16).and(addressMask);
//determine which operation we need to perfor
if(!opCode1.getValue() && !opCode2.getValue())
{
//push operation
//at our stack pointer, push the num in reg to stack
storage.write(SP, registers[regNum]);
//increment stack pointer
SP = rippleAdder.subtract(SP, four);
}
else if(opCode1.getValue() && !opCode2.getValue())
{
//pop operation
//increment stack pointer
SP = rippleAdder.add(SP, four);
//write from stack to register
longword registerValue = storage.read(SP);
registers[regNum] = registerValue;
}
else if(!opCode1.getValue() && opCode2.getValue())
{
//call operation
//combination of push and jump
storage.write(SP, PC);
PC.set(addressJump.getSigned()); //jumps to that address
SP = rippleAdder.subtract(SP, four);
}
else
{
//return
//combination of call and jump
SP = rippleAdder.add(SP, four);
PC = storage.read(SP); //jump to that instruction
}
}
else
{
//calling the ALU to do the operation and save the result
result = ALU.doOp(operation, op1, op2);
storeFlag = true;
}
}
public void store() throws Exception
{
//if we are still running !
//if we have a mov operation
if (movFlag)
{
//if that mov operation contains a negative number
if(negativeFlag)
{
registers[movReg.getSigned()].copy(movResult);
negativeFlag = false;
}
else
{
//otherwise, its a positive number
registers[movReg.getSigned()].copy(movValue);
}
//reset flag
movFlag = false;
}
else if(storeFlag)
{
//mask of 0000 0000 0000 0000 0000 0000 0000 1111
longword mask = new longword();
mask.set(15);
longword shifted = currentInstruction.rightShift(16);
//find where to save the result
int resultReg = shifted.and(mask).getSigned();
//save result into the correct register
registers[resultReg].copy(result);
//reset flag
storeFlag = false;
}
//If we have a jump flag, update PC
else if(jumpFlag)
{
int jumpIntVal = jumpVal.getSigned();
//Jump to that instruction by setting PC
PC.set(jumpIntVal);
jumpFlag = false;
}
//If conditional jump flag, update PC
else if(conditionalJumpFlag)
{
PC = rippleAdder.add(PC, branchAddress);
conditionalJumpFlag = false;
}
else
{
// we dont need to do anything if we are not moving or doing an operation.
//Things like interrupts dont need to store.
}
}
//preload
public void preload(String[] inputs) throws Exception
{
longword currentAddress = new longword();
//hit every input
for(int i = 0; i < inputs.length; i++)
{
longword instructionTemp = new longword();
//start at end and go backwards
for(int j = 15; j >= 0; j--)
{
if(inputs[i].charAt(j) == '1')
{
instructionTemp.setBit(j, new bit(true));
}
//store 2 instructions in every longword
currentAddress.set(i*2);
storage.write(currentAddress, instructionTemp);
}
}
}
}