forked from NachoSquad/nachos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Processor.java
1321 lines (1153 loc) · 36.4 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
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// PART OF THE MACHINE SIMULATION. DO NOT CHANGE.
package nachos.machine;
import nachos.security.*;
/**
* The <tt>Processor</tt> class simulates a MIPS processor that supports a
* subset of the R3000 instruction set. Specifically, the processor lacks all
* coprocessor support, and can only execute in user mode. Address translation
* information is accessed via the API. The API also allows a kernel to set an
* exception handler to be called on any user mode exception.
*
* <p>
* The <tt>Processor</tt> API is re-entrant, so a single simulated processor
* can be shared by multiple user threads.
*
* <p>
* An instance of a <tt>Processor</tt> also includes pages of physical memory
* accessible to user programs, the size of which is fixed by the constructor.
*/
public final class Processor {
/**
* Allocate a new MIPS processor, with the specified amount of memory.
*
* @param privilege encapsulates privileged access to the Nachos
* machine.
* @param numPhysPages the number of pages of physical memory to
* attach.
*/
public Processor(Privilege privilege, int numPhysPages) {
System.out.print(" processor");
this.privilege = privilege;
privilege.processor = new ProcessorPrivilege();
Class<?> clsKernel = Lib.loadClass(Config.getString("Kernel.kernel"));
Class<?> clsVMKernel = Lib.tryLoadClass("nachos.vm.VMKernel");
usingTLB =
(clsVMKernel != null && clsVMKernel.isAssignableFrom(clsKernel));
this.numPhysPages = numPhysPages;
for (int i=0; i<numUserRegisters; i++)
registers[i] = 0;
mainMemory = new byte[pageSize * numPhysPages];
if (usingTLB) {
translations = new TranslationEntry[tlbSize];
for (int i=0; i<tlbSize; i++)
translations[i] = new TranslationEntry();
}
else {
translations = null;
}
}
/**
* Set the exception handler, called whenever a user exception occurs.
*
* <p>
* When the exception handler is called, interrupts will be enabled, and
* the CPU cause register will specify the cause of the exception (see the
* <tt>exception<i>*</i></tt> constants).
*
* @param exceptionHandler the kernel exception handler.
*/
public void setExceptionHandler(Runnable exceptionHandler) {
this.exceptionHandler = exceptionHandler;
}
/**
* Get the exception handler, set by the last call to
* <tt>setExceptionHandler()</tt>.
*
* @return the exception handler.
*/
public Runnable getExceptionHandler() {
return exceptionHandler;
}
/**
* Start executing instructions at the current PC. Never returns.
*/
public void run() {
Lib.debug(dbgProcessor, "starting program in current thread");
registers[regNextPC] = registers[regPC] + 4;
Machine.autoGrader().runProcessor(privilege);
Instruction inst = new Instruction();
while (true) {
try {
inst.run();
}
catch (MipsException e) {
e.handle();
}
privilege.interrupt.tick(false);
}
}
/**
* Read and return the contents of the specified CPU register.
*
* @param number the register to read.
* @return the value of the register.
*/
public int readRegister(int number) {
Lib.assertTrue(number >= 0 && number < numUserRegisters);
return registers[number];
}
/**
* Write the specified value into the specified CPU register.
*
* @param number the register to write.
* @param value the value to write.
*/
public void writeRegister(int number, int value) {
Lib.assertTrue(number >= 0 && number < numUserRegisters);
if (number != 0)
registers[number] = value;
}
/**
* Test whether this processor uses a software-managed TLB, or single-level
* paging.
*
* <p>
* If <tt>false</tt>, this processor directly supports single-level paging;
* use <tt>setPageTable()</tt>.
*
* <p>
* If <tt>true</tt>, this processor has a software-managed TLB;
* use <tt>getTLBSize()</tt>, <tt>readTLBEntry()</tt>, and
* <tt>writeTLBEntry()</tt>.
*
* <p>
* Using a method associated with the wrong address translation mechanism
* will result in an assertion failure.
*
* @return <tt>true</tt> if this processor has a software-managed TLB.
*/
public boolean hasTLB() {
return usingTLB;
}
/**
* Get the current page table, set by the last call to setPageTable().
*
* @return the current page table.
*/
public TranslationEntry[] getPageTable() {
Lib.assertTrue(!usingTLB);
return translations;
}
/**
* Set the page table pointer. All further address translations will use
* the specified page table. The size of the current address space will be
* determined from the length of the page table array.
*
* @param pageTable the page table to use.
*/
public void setPageTable(TranslationEntry[] pageTable) {
Lib.assertTrue(!usingTLB);
this.translations = pageTable;
}
/**
* Return the number of entries in this processor's TLB.
*
* @return the number of entries in this processor's TLB.
*/
public int getTLBSize() {
Lib.assertTrue(usingTLB);
return tlbSize;
}
/**
* Returns the specified TLB entry.
*
* @param number the index into the TLB.
* @return the contents of the specified TLB entry.
*/
public TranslationEntry readTLBEntry(int number) {
Lib.assertTrue(usingTLB);
Lib.assertTrue(number >= 0 && number < tlbSize);
return new TranslationEntry(translations[number]);
}
/**
* Fill the specified TLB entry.
*
* <p>
* The TLB is fully associative, so the location of an entry within the TLB
* does not affect anything.
*
* @param number the index into the TLB.
* @param entry the new contents of the TLB entry.
*/
public void writeTLBEntry(int number, TranslationEntry entry) {
Lib.assertTrue(usingTLB);
Lib.assertTrue(number >= 0 && number < tlbSize);
translations[number] = new TranslationEntry(entry);
}
/**
* Return the number of pages of physical memory attached to this simulated
* processor.
*
* @return the number of pages of physical memory.
*/
public int getNumPhysPages() {
return numPhysPages;
}
/**
* Return a reference to the physical memory array. The size of this array
* is <tt>pageSize * getNumPhysPages()</tt>.
*
* @return the main memory array.
*/
public byte[] getMemory() {
return mainMemory;
}
/**
* Concatenate a page number and an offset into an address.
*
* @param page the page number. Must be between <tt>0</tt> and
* <tt>(2<sup>32</sup> / pageSize) - 1</tt>.
* @param offset the offset within the page. Must be between <tt>0</tt>
* and
* <tt>pageSize - 1</tt>.
* @return a 32-bit address consisting of the specified page and offset.
*/
public static int makeAddress(int page, int offset) {
Lib.assertTrue(page >= 0 && page < maxPages);
Lib.assertTrue(offset >= 0 && offset < pageSize);
return (page * pageSize) | offset;
}
/**
* Extract the page number component from a 32-bit address.
*
* @param address the 32-bit address.
* @return the page number component of the address.
*/
public static int pageFromAddress(int address) {
return (int) (((long) address & 0xFFFFFFFFL) / pageSize);
}
/**
* Extract the offset component from an address.
*
* @param address the 32-bit address.
* @return the offset component of the address.
*/
public static int offsetFromAddress(int address) {
return (int) (((long) address & 0xFFFFFFFFL) % pageSize);
}
private void finishLoad() {
delayedLoad(0, 0, 0);
}
/**
* Translate a virtual address into a physical address, using either a
* page table or a TLB. Check for alignment, make sure the virtual page is
* valid, make sure a read-only page is not being written, make sure the
* resulting physical page is valid, and then return the resulting physical
* address.
*
* @param vaddr the virtual address to translate.
* @param size the size of the memory reference (must be 1, 2, or 4).
* @param writing <tt>true</tt> if the memory reference is a write.
* @return the physical address.
* @exception MipsException if a translation error occurred.
*/
private int translate(int vaddr, int size, boolean writing)
throws MipsException {
if (Lib.test(dbgProcessor))
System.out.println("\ttranslate vaddr=0x" + Lib.toHexString(vaddr)
+ (writing ? ", write" : ", read..."));
// check alignment
if ((vaddr & (size-1)) != 0) {
Lib.debug(dbgProcessor, "\t\talignment error");
throw new MipsException(exceptionAddressError, vaddr);
}
// calculate virtual page number and offset from the virtual address
int vpn = pageFromAddress(vaddr);
int offset = offsetFromAddress(vaddr);
TranslationEntry entry = null;
// if not using a TLB, then the vpn is an index into the table
if (!usingTLB) {
if (translations == null || vpn >= translations.length ||
translations[vpn] == null ||
!translations[vpn].valid) {
privilege.stats.numPageFaults++;
Lib.debug(dbgProcessor, "\t\tpage fault");
throw new MipsException(exceptionPageFault, vaddr);
}
entry = translations[vpn];
}
// else, look through all TLB entries for matching vpn
else {
for (int i=0; i<tlbSize; i++) {
if (translations[i].valid && translations[i].vpn == vpn) {
entry = translations[i];
break;
}
}
if (entry == null) {
privilege.stats.numTLBMisses++;
Lib.debug(dbgProcessor, "\t\tTLB miss");
throw new MipsException(exceptionTLBMiss, vaddr);
}
}
// check if trying to write a read-only page
if (entry.readOnly && writing) {
Lib.debug(dbgProcessor, "\t\tread-only exception");
throw new MipsException(exceptionReadOnly, vaddr);
}
// check if physical page number is out of range
int ppn = entry.ppn;
if (ppn < 0 || ppn >= numPhysPages) {
Lib.debug(dbgProcessor, "\t\tbad ppn");
throw new MipsException(exceptionBusError, vaddr);
}
// set used and dirty bits as appropriate
entry.used = true;
if (writing)
entry.dirty = true;
int paddr = (ppn*pageSize) + offset;
if (Lib.test(dbgProcessor))
System.out.println("\t\tpaddr=0x" + Lib.toHexString(paddr));
return paddr;
}
/**
* Read </i>size</i> (1, 2, or 4) bytes of virtual memory at <i>vaddr</i>,
* and return the result.
*
* @param vaddr the virtual address to read from.
* @param size the number of bytes to read (1, 2, or 4).
* @return the value read.
* @exception MipsException if a translation error occurred.
*/
private int readMem(int vaddr, int size) throws MipsException {
if (Lib.test(dbgProcessor))
System.out.println("\treadMem vaddr=0x" + Lib.toHexString(vaddr)
+ ", size=" + size);
Lib.assertTrue(size==1 || size==2 || size==4);
int value = Lib.bytesToInt(mainMemory, translate(vaddr, size, false),
size);
if (Lib.test(dbgProcessor))
System.out.println("\t\tvalue read=0x" +
Lib.toHexString(value, size*2));
return value;
}
/**
* Write <i>value</i> to </i>size</i> (1, 2, or 4) bytes of virtual memory
* starting at <i>vaddr</i>.
*
* @param vaddr the virtual address to write to.
* @param size the number of bytes to write (1, 2, or 4).
* @param value the value to store.
* @exception MipsException if a translation error occurred.
*/
private void writeMem(int vaddr, int size, int value)
throws MipsException {
if (Lib.test(dbgProcessor))
System.out.println("\twriteMem vaddr=0x" + Lib.toHexString(vaddr)
+ ", size=" + size + ", value=0x"
+ Lib.toHexString(value, size*2));
Lib.assertTrue(size==1 || size==2 || size==4);
Lib.bytesFromInt(mainMemory, translate(vaddr, size, true), size,
value);
}
/**
* Complete the in progress delayed load and scheduled a new one.
*
* @param nextLoadTarget the target register of the new load.
* @param nextLoadValue the value to be loaded into the new target.
* @param nextLoadMask the mask specifying which bits in the new
* target are to be overwritten. If a bit in
* <tt>nextLoadMask</tt> is 0, then the
* corresponding bit of register
* <tt>nextLoadTarget</tt> will not be written.
*/
private void delayedLoad(int nextLoadTarget, int nextLoadValue,
int nextLoadMask) {
// complete previous delayed load, if not modifying r0
if (loadTarget != 0) {
int savedBits = registers[loadTarget] & ~loadMask;
int newBits = loadValue & loadMask;
registers[loadTarget] = savedBits | newBits;
}
// schedule next load
loadTarget = nextLoadTarget;
loadValue = nextLoadValue;
loadMask = nextLoadMask;
}
/**
* Advance the PC to the next instruction.
*
* <p>
* Transfer the contents of the nextPC register into the PC register, and
* then add 4 to the value in the nextPC register. Same as
* <tt>advancePC(readRegister(regNextPC)+4)</tt>.
*
* <p>
* Use after handling a syscall exception so that the processor will move
* on to the next instruction.
*/
public void advancePC() {
advancePC(registers[regNextPC]+4);
}
/**
* Transfer the contents of the nextPC register into the PC register, and
* then write the nextPC register.
*
* @param nextPC the new value of the nextPC register.
*/
private void advancePC(int nextPC) {
registers[regPC] = registers[regNextPC];
registers[regNextPC] = nextPC;
}
/** Caused by a syscall instruction. */
public static final int exceptionSyscall = 0;
/** Caused by an access to an invalid virtual page. */
public static final int exceptionPageFault = 1;
/** Caused by an access to a virtual page not mapped by any TLB entry. */
public static final int exceptionTLBMiss = 2;
/** Caused by a write access to a read-only virtual page. */
public static final int exceptionReadOnly = 3;
/** Caused by an access to an invalid physical page. */
public static final int exceptionBusError = 4;
/** Caused by an access to a misaligned virtual address. */
public static final int exceptionAddressError = 5;
/** Caused by an overflow by a signed operation. */
public static final int exceptionOverflow = 6;
/** Caused by an attempt to execute an illegal instruction. */
public static final int exceptionIllegalInstruction = 7;
/** The names of the CPU exceptions. */
public static final String exceptionNames[] = {
"syscall ",
"page fault ",
"TLB miss ",
"read-only ",
"bus error ",
"address error",
"overflow ",
"illegal inst "
};
/** Index of return value register 0. */
public static final int regV0 = 2;
/** Index of return value register 1. */
public static final int regV1 = 3;
/** Index of argument register 0. */
public static final int regA0 = 4;
/** Index of argument register 1. */
public static final int regA1 = 5;
/** Index of argument register 2. */
public static final int regA2 = 6;
/** Index of argument register 3. */
public static final int regA3 = 7;
/** Index of the stack pointer register. */
public static final int regSP = 29;
/** Index of the return address register. */
public static final int regRA = 31;
/** Index of the low register, used for multiplication and division. */
public static final int regLo = 32;
/** Index of the high register, used for multiplication and division. */
public static final int regHi = 33;
/** Index of the program counter register. */
public static final int regPC = 34;
/** Index of the next program counter register. */
public static final int regNextPC = 35;
/** Index of the exception cause register. */
public static final int regCause = 36;
/** Index of the exception bad virtual address register. */
public static final int regBadVAddr = 37;
/** The total number of software-accessible CPU registers. */
public static final int numUserRegisters = 38;
/** Provides privilege to this processor. */
private Privilege privilege;
/** MIPS registers accessible to the kernel. */
private int registers[] = new int[numUserRegisters];
/** The registered target of the delayed load currently in progress. */
private int loadTarget = 0;
/** The bits to be modified by the delayed load currently in progress. */
private int loadMask;
/** The value to be loaded by the delayed load currently in progress. */
private int loadValue;
/** <tt>true</tt> if using a software-managed TLB. */
private boolean usingTLB;
/** Number of TLB entries. */
private int tlbSize = 4;
/**
* Either an associative or direct-mapped set of translation entries,
* depending on whether there is a TLB.
*/
private TranslationEntry[] translations;
/** Size of a page, in bytes. */
public static final int pageSize = 0x400;
/** Number of pages in a 32-bit address space. */
public static final int maxPages = (int) (0x100000000L / pageSize);
/** Number of physical pages in memory. */
private int numPhysPages;
/** Main memory for user programs. */
private byte[] mainMemory;
/** The kernel exception handler, called on every user exception. */
private Runnable exceptionHandler = null;
private static final char dbgProcessor = 'p';
private static final char dbgDisassemble = 'm';
private static final char dbgFullDisassemble = 'M';
private class ProcessorPrivilege implements Privilege.ProcessorPrivilege {
public void flushPipe() {
finishLoad();
}
}
private class MipsException extends Exception {
public MipsException(int cause) {
Lib.assertTrue(cause >= 0 && cause < exceptionNames.length);
this.cause = cause;
}
public MipsException(int cause, int badVAddr) {
this(cause);
hasBadVAddr = true;
this.badVAddr = badVAddr;
}
public void handle() {
writeRegister(regCause, cause);
if (hasBadVAddr)
writeRegister(regBadVAddr, badVAddr);
if (Lib.test(dbgDisassemble) || Lib.test(dbgFullDisassemble))
System.out.println("exception: " + exceptionNames[cause]);
finishLoad();
Lib.assertTrue(exceptionHandler != null);
// autograder might not want kernel to know about this exception
if (!Machine.autoGrader().exceptionHandler(privilege))
return;
exceptionHandler.run();
}
private boolean hasBadVAddr = false;
private int cause, badVAddr;
}
private class Instruction {
public void run() throws MipsException {
// hopefully this looks familiar to 152 students?
fetch();
decode();
execute();
writeBack();
}
private boolean test(int flag) {
return Lib.test(flag, flags);
}
private void fetch() throws MipsException {
if ((Lib.test(dbgDisassemble) && !Lib.test(dbgProcessor)) ||
Lib.test(dbgFullDisassemble))
System.out.print("PC=0x" + Lib.toHexString(registers[regPC])
+ "\t");
value = readMem(registers[regPC], 4);
}
private void decode() {
op = Lib.extract(value, 26, 6);
rs = Lib.extract(value, 21, 5);
rt = Lib.extract(value, 16, 5);
rd = Lib.extract(value, 11, 5);
sh = Lib.extract(value, 6, 5);
func = Lib.extract(value, 0, 6);
target = Lib.extract(value, 0, 26);
imm = Lib.extend(value, 0, 16);
Mips info;
switch (op) {
case 0:
info = Mips.specialtable[func];
break;
case 1:
info = Mips.regimmtable[rt];
break;
default:
info = Mips.optable[op];
break;
}
operation = info.operation;
name = info.name;
format = info.format;
flags = info.flags;
mask = 0xFFFFFFFF;
branch = true;
// get memory access size
if (test(Mips.SIZEB))
size = 1;
else if (test(Mips.SIZEH))
size = 2;
else if (test(Mips.SIZEW))
size = 4;
else
size = 0;
// get nextPC
nextPC = registers[regNextPC]+4;
// get dstReg
if (test(Mips.DSTRA))
dstReg = regRA;
else if (format == Mips.IFMT)
dstReg = rt;
else if (format == Mips.RFMT)
dstReg = rd;
else
dstReg = -1;
// get jtarget
if (format == Mips.RFMT)
jtarget = registers[rs];
else if (format == Mips.IFMT)
jtarget = registers[regNextPC] + (imm<<2);
else if (format == Mips.JFMT)
jtarget = (registers[regNextPC]&0xF0000000) | (target<<2);
else
jtarget = -1;
// get imm
if (test(Mips.UNSIGNED)) {
imm &= 0xFFFF;
}
// get addr
addr = registers[rs] + imm;
// get src1
if (test(Mips.SRC1SH))
src1 = sh;
else
src1 = registers[rs];
// get src2
if (test(Mips.SRC2IMM))
src2 = imm;
else
src2 = registers[rt];
if (test(Mips.UNSIGNED)) {
src1 &= 0xFFFFFFFFL;
src2 &= 0xFFFFFFFFL;
}
if (Lib.test(dbgDisassemble) || Lib.test(dbgFullDisassemble))
print();
}
private void print() {
if (Lib.test(dbgDisassemble) && Lib.test(dbgProcessor) &&
!Lib.test(dbgFullDisassemble))
System.out.print("PC=0x" + Lib.toHexString(registers[regPC])
+ "\t");
if (operation == Mips.INVALID) {
System.out.print("invalid: op=" + Lib.toHexString(op, 2) +
" rs=" + Lib.toHexString(rs, 2) +
" rt=" + Lib.toHexString(rt, 2) +
" rd=" + Lib.toHexString(rd, 2) +
" sh=" + Lib.toHexString(sh, 2) +
" func=" + Lib.toHexString(func, 2) +
"\n");
return;
}
int spaceIndex = name.indexOf(' ');
Lib.assertTrue(spaceIndex!=-1 && spaceIndex==name.lastIndexOf(' '));
String instname = name.substring(0, spaceIndex);
char[] args = name.substring(spaceIndex+1).toCharArray();
System.out.print(instname + "\t");
int minCharsPrinted = 0, maxCharsPrinted = 0;
for (int i=0; i<args.length; i++) {
switch (args[i]) {
case Mips.RS:
System.out.print("$" + rs);
minCharsPrinted += 2;
maxCharsPrinted += 3;
if (Lib.test(dbgFullDisassemble)) {
System.out.print("#0x" +
Lib.toHexString(registers[rs]));
minCharsPrinted += 11;
maxCharsPrinted += 11;
}
break;
case Mips.RT:
System.out.print("$" + rt);
minCharsPrinted += 2;
maxCharsPrinted += 3;
if (Lib.test(dbgFullDisassemble) &&
(i!=0 || !test(Mips.DST)) &&
!test(Mips.DELAYEDLOAD)) {
System.out.print("#0x" +
Lib.toHexString(registers[rt]));
minCharsPrinted += 11;
maxCharsPrinted += 11;
}
break;
case Mips.RETURNADDRESS:
if (rd == 31)
continue;
case Mips.RD:
System.out.print("$" + rd);
minCharsPrinted += 2;
maxCharsPrinted += 3;
break;
case Mips.IMM:
System.out.print(imm);
minCharsPrinted += 1;
maxCharsPrinted += 6;
break;
case Mips.SHIFTAMOUNT:
System.out.print(sh);
minCharsPrinted += 1;
maxCharsPrinted += 2;
break;
case Mips.ADDR:
System.out.print(imm + "($" + rs);
minCharsPrinted += 4;
maxCharsPrinted += 5;
if (Lib.test(dbgFullDisassemble)) {
System.out.print("#0x" +
Lib.toHexString(registers[rs]));
minCharsPrinted += 11;
maxCharsPrinted += 11;
}
System.out.print(")");
break;
case Mips.TARGET:
System.out.print("0x" + Lib.toHexString(jtarget));
minCharsPrinted += 10;
maxCharsPrinted += 10;
break;
default:
Lib.assertTrue(false);
}
if (i+1 < args.length) {
System.out.print(", ");
minCharsPrinted += 2;
maxCharsPrinted += 2;
}
else {
// most separation possible is tsi, 5+1+1=7,
// thankfully less than 8 (makes this possible)
Lib.assertTrue(maxCharsPrinted-minCharsPrinted < 8);
// longest string is stj, which is 40-42 chars w/ -d M;
// go for 48
while ((minCharsPrinted%8) != 0) {
System.out.print(" ");
minCharsPrinted++;
maxCharsPrinted++;
}
while (minCharsPrinted < 48) {
System.out.print("\t");
minCharsPrinted += 8;
}
}
}
if (Lib.test(dbgDisassemble) && Lib.test(dbgProcessor) &&
!Lib.test(dbgFullDisassemble))
System.out.print("\n");
}
private void execute() throws MipsException {
int value;
int preserved;
switch (operation) {
case Mips.ADD:
dst = src1 + src2;
break;
case Mips.SUB:
dst = src1 - src2;
break;
case Mips.MULT:
dst = src1 * src2;
registers[regLo] = (int) Lib.extract(dst, 0, 32);
registers[regHi] = (int) Lib.extract(dst, 32, 32);
break;
case Mips.DIV:
try {
registers[regLo] = (int) (src1 / src2);
registers[regHi] = (int) (src1 % src2);
if (registers[regLo]*src2 + registers[regHi] != src1)
throw new ArithmeticException();
}
catch (ArithmeticException e) {
throw new MipsException(exceptionOverflow);
}
break;
case Mips.SLL:
dst = src2 << (src1&0x1F);
break;
case Mips.SRA:
dst = src2 >> (src1&0x1F);
break;
case Mips.SRL:
dst = src2 >>> (src1&0x1F);
break;
case Mips.SLT:
dst = (src1<src2) ? 1 : 0;
break;
case Mips.AND:
dst = src1 & src2;
break;
case Mips.OR:
dst = src1 | src2;
break;
case Mips.NOR:
dst = ~(src1 | src2);
break;
case Mips.XOR:
dst = src1 ^ src2;
break;
case Mips.LUI:
dst = imm << 16;
break;
case Mips.BEQ:
branch = (src1 == src2);
break;
case Mips.BNE:
branch = (src1 != src2);
break;
case Mips.BGEZ:
branch = (src1 >= 0);
break;
case Mips.BGTZ:
branch = (src1 > 0);
break;
case Mips.BLEZ:
branch = (src1 <= 0);
break;
case Mips.BLTZ:
branch = (src1 < 0);
break;
case Mips.JUMP:
break;
case Mips.MFLO:
dst = registers[regLo];
break;
case Mips.MFHI:
dst = registers[regHi];
break;
case Mips.MTLO:
registers[regLo] = (int) src1;
break;
case Mips.MTHI:
registers[regHi] = (int) src1;
break;
case Mips.SYSCALL:
throw new MipsException(exceptionSyscall);
case Mips.LOAD:
value = readMem(addr, size);
if (!test(Mips.UNSIGNED))
dst = Lib.extend(value, 0, size*8);
else
dst = value;
break;
case Mips.LWL:
value = readMem(addr&~0x3, 4);
// LWL shifts the input left so the addressed byte is highest
preserved = (3-(addr&0x3))*8; // number of bits to preserve
mask = -1 << preserved; // preserved bits are 0 in mask
dst = value << preserved; // shift input to correct place
addr &= ~0x3;
break;
case Mips.LWR:
value = readMem(addr&~0x3, 4);
// LWR shifts the input right so the addressed byte is lowest
preserved = (addr&0x3)*8; // number of bits to preserve
mask = -1 >>> preserved; // preserved bits are 0 in mask
dst = value >>> preserved; // shift input to correct place
addr &= ~0x3;
break;
case Mips.STORE:
writeMem(addr, size, (int) src2);
break;
case Mips.SWL:
value = readMem(addr&~0x3, 4);
// SWL shifts highest order byte into the addressed position
preserved = (3-(addr&0x3))*8;
mask = -1 >>> preserved;
dst = src2 >>> preserved;
// merge values
dst = (dst & mask) | (value & ~mask);
writeMem(addr&~0x3, 4, (int) dst);
break;
case Mips.SWR:
value = readMem(addr&~0x3, 4);
// SWR shifts the lowest order byte into the addressed position
preserved = (addr&0x3)*8;
mask = -1 << preserved;
dst = src2 << preserved;