-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPU.java
More file actions
716 lines (569 loc) · 33 KB
/
Copy pathCPU.java
File metadata and controls
716 lines (569 loc) · 33 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
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
public class CPU {
//Instance variables
private long cycles; //Holds the number of cycles that has occurred
private boolean halt; //halt status
private long error; //error status
private long op1Value; //final value container of op 1 used for CPU operations
private long op2Value; //final value container of op 2 used for CPU operations
private long op1Address; //address container used to determine value 1
private long op2Address; //address container used to determine value 2
//Constructor - set everything to 0
public CPU() {
this.cycles = 0;
this.halt = false;
this.error = SystemConstants.OK;
this.op1Value = 0;
this.op2Value = 0;
this.op1Address = 0;
this.op2Address = 0;
}
//CPU cycle method/switch statement - performs CPU cycles until an error occurs or halt is issued
public void cycle() {
//Cycle the CPU until state is set to halt or an error is set
//Original condition: !halt && error >= 0
//Debugging condition: !halt && error >= 0 && cycles < 1 <- specify the # of cycles you want
while (!halt && error >= 0) {
updateRegisters();
long[] operands = parseOperands(); //Parse each digit from the 6 digit number in the IR
//[0] = opCode
//[1] = op1Mode
//[2] = op1GPR
//[3] = op2Mode
//[4] = op2GPR
if (operands[1] <= 6 && operands[3] <= 6 && operands[1] >= 0 && operands[3] >= 0 && operands[2] <= 8 && operands[4] <= 8 && operands[2] >= 0 && operands[4] >= 0) {
long status;
long result;
switch ((int)operands[0]) {
//Issue a halt: this mode will stop the CPU
case 0: //halt
this.halt = true;
System.out.printf("%n$ %d HALT: Halt encountered.%n", error);
Main.incrementClock(SystemConstants.TIME_HALT);
break;
// MATH/MOVE MODES 1-5:
// Go to each one to read more
//
// 1: Addition
//
// 2: Subtraction
//
// 3: Multiplication
//
// 4: Division
//
// 5: MOVE
//Addition mode: Add value 1 and value 2 together after setting them.
// Then, based on what mode op1 is, write the difference to a location or discard it
case 1: //add
//Set value 1
status = fetchOperand(operands[1], operands[2], 1, 1);
if (status < 0) {
break;
}
//Set value 2
status = fetchOperand(operands[3], operands[4], 2, 2);
if (status < 0) {
break;
}
//Add both values
result = this.op1Value + this.op2Value;
//If op1Mode = register mode
if (operands[1] == SystemConstants.FETCH_REGISTER) {
//Store the sum in the GPR specified by op1GPR
Main.setGPR(operands[2], result);
} else if (operands[1] == SystemConstants.FETCH_IMMEDIATE) {
//Do not store the sum anywhere and report an error
this.error = SystemConstants.ERROR_IMMEDIATE_DESTINATION;
System.out.printf("%n$ ERROR %d IMMEDIATE DESTINATION: Destination operand cannot be the immediate value when adding.%n", error);
} else {
//When in ANY other mode, store the sum in the address
//specified in the op1Address variable
Main.setHypoMemory(this.op1Address, result);
}
Main.incrementClock(SystemConstants.TIME_ADD);
break;
//Subtraction mode: Subtract value 2 from value 1 after fetching them.
// Then, based on what mode op1 is, write the difference to a location or discard it
case 2: //subtract
//Set value 1
status = fetchOperand(operands[1], operands[2], 1, 1);
if (status < 0) {
break;
}
//Set value 2
status = fetchOperand(operands[3], operands[4], 2, 2);
if (status < 0) {
break;
}
//Subtract value 2 from value 1
result = this.op1Value - this.op2Value;
//If op1Mode = register mode
if (operands[1] == SystemConstants.FETCH_REGISTER) {
//Store the difference in the GPR specified by op1GPR
Main.setGPR(operands[2], result);
} else if (operands[1] == SystemConstants.FETCH_IMMEDIATE) {
//Do not store the sum anywhere and report an error
this.error = SystemConstants.ERROR_IMMEDIATE_DESTINATION;
System.out.printf("%n$ ERROR %d IMMEDIATE DESTINATION: Destination operand cannot be the immediate value when subtracting.%n", error);
} else {
//When in ANY other mode, store the sum in the address
//specified in the op1Address variable
Main.setHypoMemory(this.op1Address, result);
}
Main.incrementClock(SystemConstants.TIME_SUBTRACT);
break;
//Multiplication mode: multiply value 1 by value 2 after fetching them.
// Then, based on what mode op1 is, write the result to a location or discard it
case 3: //multiply
//Set value 1
status = fetchOperand(operands[1], operands[2], 1, 1);
if (status < 0) {
break;
}
//Set value 1
status = fetchOperand(operands[3], operands[4], 2, 2);
if (status < 0) {
break;
}
//Find their product
result = this.op1Value * this.op2Value;
//If op1Mode = register mode
if (operands[1] == SystemConstants.FETCH_REGISTER) {
//Store the result in the GPR specified by op1GPR
Main.setGPR(operands[2], result);
} else if (operands[1] == SystemConstants.FETCH_IMMEDIATE) {
//Do not store the result anywhere and report an error
this.error = SystemConstants.ERROR_IMMEDIATE_DESTINATION;
System.out.printf("%n$ ERROR %d IMMEDIATE DESTINATION: Destination operand cannot be the immediate value when multiplying.%n", error);
} else {
//When in ANY other mode, store the result in the address
//specified in the op1Address variable
Main.setHypoMemory(this.op1Address, result);
}
Main.incrementClock(SystemConstants.TIME_MULTIPLY);
break;
//Division mode: divide value 1 by value 2 after fetching them.
// Then, based on what mode op1 is, write the result to a location or discard it
case 4: //division
//Set value 1
status = fetchOperand(operands[1], operands[2], 1, 1);
if (status < 0) {
break;
}
//Set value 1
status = fetchOperand(operands[3], operands[4], 2, 2);
if (status < 0) {
break;
}
//Check value 2 for 0, as division by 0 is impossible
if (op2Value != 0) {
result = this.op1Value / this.op2Value;
} else {
this.error = SystemConstants.ERROR_FATAL_RUNTIME;
System.out.printf("%n$ FATAL RUNTIME ERROR %d: Cannot divide by zero.%n", error);
break;
}
//If op1Mode = register mode
if (operands[1] == SystemConstants.FETCH_REGISTER) {
//Store the result in the GPR specified by op1GPR
Main.setGPR(operands[2], result);
} else if (operands[1] == SystemConstants.FETCH_IMMEDIATE) {
//Do not store the result anywhere and report an error
this.error = SystemConstants.ERROR_IMMEDIATE_DESTINATION;
System.out.printf("%n$ ERROR %d IMMEDIATE DESTINATION: Destination operand cannot be the immediate value when dividing.%n", error);
} else {
//When in ANY other mode, store the result in the address
//specified in the op1Address variable
Main.setHypoMemory(this.op1Address, result);
}
Main.incrementClock(SystemConstants.TIME_DIVIDE);
break;
//Move mode: set value 1 equal to whatever value 2 is after fetching them
// Then, based on what mode op1 is, write value 1 to a location or discard it
case 5: //move
//Set value 1
status = fetchOperand(operands[1], operands[2], 1, 1);
if (status < 0) {
break;
}
//Set value 2
status = fetchOperand(operands[3], operands[4], 2, 2);
if (status < 0) {
break;
}
this.op1Value = this.op2Value;
//If op1Mode = register mode
if (operands[1] == SystemConstants.FETCH_REGISTER) {
//Store value 1 in the GPR specified by op1GPR
Main.setGPR(operands[2], this.op1Value);
} else if (operands[1] == SystemConstants.FETCH_IMMEDIATE) {
//Do not store value 1 anywhere and report an error
this.error = SystemConstants.ERROR_IMMEDIATE_DESTINATION;
System.out.printf("%n$ ERROR %d IMMEDIATE DESTINATION: Destination operand cannot be the immediate value when moving.%n", error);
} else {
//When in ANY other mode, store value 1 in the address
//specified in the op1Address variable
Main.setHypoMemory(this.op1Address, this.op1Value);
}
Main.incrementClock(SystemConstants.TIME_MOVE);
break;
// BRANCH MODES 6-9
//
// 6: Assumes 1 input - looks at the value stored at the memory address that the PC is pointing to
// - So it will look at the next line and assume a memory address is stored there
// - Then it will jump to the memory location specified
//
// 7: Looks at the value of operand 1 & value stored at memory address that PC points to
// - One of the next lines must specify an address to jump to
// - IF value 1 is negative and nonzero, reset the PC to a specified value
// - otherwise, skip the next line and proceed
//
// 8: Looks at the value of operand 1 & value stored at memory address that PC points to
// - One of the next lines must specify an address to jump to
// - IF value 1 is positive and nonzero, reset the PC to a specified value
// - otherwise, skip the next line and proceed
//
// 8: Looks at the value of operand 1 & value stored at memory address that PC points to
// - One of the next lines must specify an address to jump to
// - IF value 1 is ZERO, reset the PC to a specified value
// - otherwise, skip the next line and proceed
case 6: //branch/jump instruction
//Set the PC to the next value in the memory address that the PC points too
Main.setPC(Main.getHypoMemory(Main.getPC()));
Main.incrementClock(SystemConstants.TIME_BRANCH);
break;
case 7: //branch on minus
//Set value 1 and only value 1 - NOT both values
status = fetchOperand(operands[1], operands[2], 1, 1);
if (status < 0) {
break;
}
//This assumes that the next line contains an address
// If value 1 is negative and nonzero
if (this.op1Value < 0) {
//Go to the next line and get that address
//THEN set the PC to that address
Main.setPC(Main.getHypoMemory(Main.getPC()));
} else {
//Otherwise, skip the next line and proceed
Main.incrementPC();
}
Main.incrementClock(SystemConstants.TIME_BRANCH_MINUS);
break;
case 8: //branch on plus
//Set value 1 and only value 1 - NOT both values
status = fetchOperand(operands[1], operands[2], 1, 1);
if (status < 0) {
break;
}
//This assumes that the next line contains an address
// If value 1 is positive and nonzero
if (this.op1Value > 0) {
//Go to the next line and assume it contains an address
//get that address
//THEN set the PC to that address
//This is the location the program will jump to
//if value 1 is positive and nonzero
Main.setPC(Main.getHypoMemory(Main.getPC()));
} else {
//Otherwise, skip the next line and proceed
Main.incrementPC();
}
Main.incrementClock(SystemConstants.TIME_BRANCH_PLUS);
break;
case 9: //branch on zero
//Set value 1 and only value 1 - NOT both values
status = fetchOperand(operands[1], operands[2], 1, 1);
if (status < 0) {
break;
}
//This assumes that the next line contains an address
// If value 1 IS ZERO
if (this.op1Value == 0) {
//Go to the next line and get that address
//THEN set the PC to that address
Main.setPC(Main.getHypoMemory(Main.getPC()));
} else {
//Otherwise, skip the next line and proceed
Main.incrementPC();
}
Main.incrementClock(SystemConstants.TIME_BRANCH_PLUS);
break;
// STACK MODES 10-11 - not yet implemented
//
// 10.
//
// 11.
//
case 10: //push if stack is not full
status = fetchOperand(operands[1], operands[2], 1, 1);
if (status < 0) {
break;
}
System.out.printf("%n$ Case 10: push to stack.%n");
break;
case 11: //pop if stack is not empty
status = fetchOperand(operands[1], operands[2], 1, 1);
if (status < 0) {
break;
}
System.out.printf("%n$ Case 11: pop from stack.%n");
break;
// SYSTEM MODE 12 - not yet implemented
//
case 12:
long systemCallID = Main.getHypoMemory(Main.getPC());
Main.incrementPC();
System.out.printf("%n$ Case 12: System call.%n");
break;
default:
this.error = SystemConstants.ERROR_INVALID_OPCODE;
System.out.printf("%n$ ERROR %d INVALID OPCODE: Opcode %d does not exist.%n", error, operands[0]);
break;
}
} else {
this.error = SystemConstants.ERROR_INVALID_OPERANDS;
System.out.printf("%n$ ERROR %d INVALID ADDRESS: One of the operands contain an invalid mode or attempts to access an invalid GPR number.%n", error);
}
printOperands(operands);
this.cycles++;
//DEBUGGING
//this.halt = true;
//String cycleNumber = String.format("Cycle number %d", cycles);
//Main.dumpMemory(cycleNumber, 0, 100);
}
}
//Updates MAR, MBR, IR, and PC at the beginning of every CPU cycle
public void updateRegisters() {
long programCounter = Main.getPC();
//Proceed only if 0 <= PC <= 2999 - Non negative and within the valid program area
if (0 <= programCounter && programCounter <= SystemConstants.VALID_PROGRAM_AREA) {
Main.setMAR(programCounter); //set MAR to the value of the PC
Main.incrementPC(); //PC now contains the addr of the next word while MAR has the current word
long word = Main.getHypoMemory(Main.getMAR()); //Retrieve word from memory using address stored in MAR
Main.setMBR(word); //Load the word into the MBR
} else {
this.error = SystemConstants.ERROR_INVALID_ADDRESS;
System.out.printf("%n$ ERROR %d INVALID ADDRESS: The program counter points to a negative address or an address outside the valid program area.%n", error);
}
Main.setIR(Main.getMBR()); //Get the MBR value and store it in the IR
}
//Splits 5-6 digit line from the IR into an array of 5 separate values
public long[] parseOperands() {
//Separate the opcode from the word and leave the rest in remainder
//Opcode tells CPU what mode it should be in
long opCode = (Main.getIR()) / 10000; //extracts the first digit from the word. example: 15060 / 10000 = 1.506 = opCode 1
long remainder = (Main.getIR()) % 10000; //extracts everything else except the first digit... so 5060
//Extract information about the operands... for example, in 5060, op1=50 op2=60
//Each one of the operands are split into 2 parts, first digit being the "mode" for that operand
//Second digit of the operand is one of the GPRs (or usually 0 if a GPR is not used in that instance)
//Mathematically extract the first digit of op1 (op1Mode)
long op1Mode = remainder / 1000;
remainder = remainder % 1000;
//Mathematically extract the second digit of op1 (op1GPR)
long op1GPR = remainder / 100;
remainder = remainder % 100;
//Mathematically extract the first digit of op2 (op2Mode)
long op2Mode = remainder / 10;
remainder = remainder % 10;
//Mathematically extract the second digit of op2 (op2GPR)
long op2GPR = remainder / 1;
//Return all the operands as an array
long[] operands = {opCode, op1Mode, op1GPR, op2Mode, op2GPR};
return operands;
}
//Simple array printing method to print the array of operands for debugging
public void printOperands(long[] operands) {
System.out.printf("%nOperands array: ");
for (int i = 0; i < 5; i++) {
System.out.printf("%d ", operands[i]);
}
System.out.printf("%n");
}
//Method/switch statement that can set value 1 or value 2 that the CPU uses for operations
//based on the mode passed into it
public long fetchOperand(long mode, long gpr, int operandAddr, int operandValue) {
//mode: long describing which fetch mode to use
//gpr: long describing which GPR to access
//operandAddr: integer describing which address variable to access, 1 or 2
//operandValue: integer describing which value variable to access, 1 or 2
switch((int)mode) {
// GPR MODES 1-4: these modes deal with GPRs
//
// 1: Read and write to a GPR when performing math operations or moves (op1)
//
// 2: A GPR contains a memory location. Retrieve your value from there
// - Read and write to that location when performing math operations or moves (op1)
//
// 3: A GPR contains a memory location. Retrieve your value from there
// - Read and write to that location when performing math operations or moves (op1)
// - Increment that GPR's value after using it
//
// 4: A GPR contains a memory location. Retrieve your value from there
// - Read and write to that location when performing math operations or moves (op1)
// - Decrement that GPR's value after using it
//Register mode: This mode retrieves from a GPR but will also lock you into writing
//to the same GPR for the current cycle (op1).
case 1: //register mode - This mode will go to the specified GPR and retrieve its value
setOpAddress(operandAddr, -200); //Set the specified op address var to a negative num
//Set the specified op value var to the value that is in the specified GPR
setOpValue(operandValue, Main.getGPR(gpr));
break;
//Register deferred mode: This mode retrieves a location from a GPR
//but will also lock you into writing to the same memory location for the current cycle (op1).
//Use it when you want to fetch a memory location from a GPR
case 2: //register deferred mode - "Op addr in GPR and value in memory"
//In this case, there is something inside the GPR specified by the var gpr
//Retrieve that value
//Put it inside op address 1 or 2
setOpAddress(operandAddr, Main.getGPR(gpr));
//When in fetch mode 2, the assumption is that the
//specified GPR contains another memory location...
if (Main.isValidProgramArea(getOpAddress(operandAddr))) {
//Go to the memory address that the GPR points to
//Get whatever is stored there and set it as value either 1 or 2
setOpValue(operandValue, Main.getHypoMemory(operandAddr));
} else {
this.error = SystemConstants.ERROR_INVALID_ADDRESS;
System.out.printf("%n$ ERROR %d INVALID ADDRESS: GPR %d is invalid in case 2.%n", error, operandAddr);
return this.error;
}
break;
//Autoincrement mode: SAME AS MODE 2 - This mode retrieves a location from a GPR
//but will also lock you into writing to the same memory location for the current cycle (op1).
//Use it when you want to fetch a memory location from a GPR
//However, after retrieving an address from said GPR, move the contained address to the one after it
//So now the GPR will contain the address directly after the address it previously contained
case 3: //autoincrement mode (Op addr in GPR and value in memory)
//In this case, there is something inside the GPR specified by the var gpr
//Retrieve that value
//Put it inside op address 1 or 2
setOpAddress(operandAddr, Main.getGPR(gpr));
//When in fetch mode 3, the assumption is that the
//specified GPR contains another memory location...
if (Main.isValidProgramArea(getOpAddress(operandAddr))) {
//Go to the memory address that the GPR points to
//Get whatever is stored there and set it as value either 1 or 2
setOpValue(operandValue, Main.getHypoMemory(getOpAddress(operandAddr)));
//Now, increment the value of that GPR by 1
//So essentially, move the address contained by the GPR to the next address after it
Main.setGPR(gpr, (Main.getGPR(gpr))+1);
} else {
this.error = SystemConstants.ERROR_INVALID_ADDRESS;
System.out.printf("%n$ ERROR %d INVALID ADDRESS: GPR %d is invalid in case 3.%n", error, operandAddr);
}
break;
//Autodecrement mode: SAME AS MODE 3 - This mode retrieves a location from a GPR
//but will also lock you into writing to the same memory location for the current cycle (op1).
//Use it when you want to fetch a memory location from a GPR
//However, after retrieving an address from said GPR, move the contained address to the one before it
//So now the GPR will contain the address directly before the address it previously contained
case 4: //autodecrement mode
Main.setGPR(gpr, (Main.getGPR(gpr))-1); //decrement register value by 1
//In this case, there is something inside the GPR specified by the var gpr
//Retrieve that value
//Put it inside op address 1 or 2
setOpAddress(operandAddr, Main.getGPR(gpr)); //Set address var 1 or 2 to the contents of said GPR
if (Main.isValidProgramArea(getOpAddress(operandAddr))) {
setOpValue(operandValue, Main.getHypoMemory(getOpAddress(operandAddr)));
} else {
this.error = SystemConstants.ERROR_INVALID_ADDRESS;
System.out.printf("%n$ ERROR %d INVALID ADDRESS: GPR %d is invalid in case 4.%n", error, operandAddr);
}
break;
// LINE MODES 5-6: these modes deal with the following line
//
// 5: The next line contains a memory location. Retrieve your value from there
// - Read and write to that location when performing math operations or moves (op1)
//
// 6: The next line contains the value you want to use. Retrieve it directly from there
// - Cannot write to the next line when performing math operations or moves (op1)
//Direct mode: this mode looks at the next line. It assumes whatever value contained in the memory address
// in the next line is another memory address. Then, it will go to that location and retrieve whatever
// value is there.
// This mode will lock you into writing to the same memory location for the current cycle (op1).
// Use it when you want to fetch from a remote memory location anywhere
case 5: //direct mode - "Op address is in the instruction that is in the program counter"
//Do need to check if PC value is valid because validation is already performed when updating the PC value
//In this case, the PC points to a memory location
//Retrieve the contents of that memory location
//Put it inside op address 1 or 2
setOpAddress(operandAddr, Main.getHypoMemory(Main.getPC()));
Main.incrementPC(); // -> make sure the PC is moved forward since it should always point to the next place
//When in fetch mode 5, the assumption is that the
//location specified by the PC contains another memory location...
if (Main.isValidProgramArea(getOpAddress(operandAddr))) {
//Go to the memory address in the PC (AKA the next line)
//Get whatever is stored there and set it as value either 1 or 2
setOpValue(operandValue, Main.getHypoMemory(getOpAddress(operandAddr)));
} else {
this.error = SystemConstants.ERROR_INVALID_ADDRESS;
System.out.printf("%n$ ERROR %d INVALID ADDRESS: Op address %d contains %d in case 5.%n", error, operandAddr, getOpAddress(operandAddr));
}
break;
//Immediate mode: this mode looks at the next line. It assumes whatever value contained in the memory address
// in the next line is the value you want to use.
// Use it when you want to fetch a value directly from the next line
// This mode locks you from writing (op1) because the CPU will not allow writing in immediate mode.
case 6: //immediate mode - "Op value is in the instruction"
//Do need to check if PC value is valid because validation is already performed when updating the PC value
setOpAddress(operandAddr, -300);
//In this case, the program counter contains a memory location
//Go to that memory location and retrieve the contents of what is stored there
//Place it inside op value 1 or 2
//When using mode 6, the assumption is that the
//location specified by the PC contains a value
//This is why we do not need to check if that number is
//in the valid program area, as it is a number and not an address
setOpValue(operandValue, Main.getHypoMemory(Main.getPC()));
Main.incrementPC();
break;
default: //invalid mode
this.error = SystemConstants.ERROR_INVALID_FETCH_MODE;
System.out.printf("%n$ ERROR %d INVALID FETCH MODE: Fetch mode %d does not exist.%n", error, mode);
break;
}
return this.error;
}
//Accessors
public long getOpAddress(int operandAddr) {
if (operandAddr == 1) {
return this.op1Address;
} else if (operandAddr == 2) {
return this.op2Address;
} else {
return -9000;
}
}
public long getOpValue(int operandValue) {
if (operandValue == 1) {
return this.op1Value;
} else if (operandValue == 2) {
return this.op2Value;
} else {
return -9000;
}
}
public long getCycles() {
return this.cycles;
}
//Mutators
public void setOpAddress(int operandAddr, long value) {
if (operandAddr == 1) {
this.op1Address = value;
} else if (operandAddr == 2) {
this.op2Address = value;
}
}
public void setOpValue(int operandValue, long value) {
if (operandValue == 1) {
this.op1Value = value;
} else if (operandValue == 2) {
this.op2Value = value;
}
}
//Boolean methods
public boolean isValidGPR(long gpr) {
return gpr >= 0 && gpr <= 8;
}
}