forked from hulkholden/n64js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
r4300.js
executable file
·4095 lines (3373 loc) · 133 KB
/
r4300.js
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
if (typeof n64js === 'undefined') {
var n64js = {};
}
(function () {'use strict';
var kDebugTLB = 0;
var kDebugDynarec = 0;
var hitCounts = {};
var fragmentMap = {};
var fragmentInvalidationEvents = [];
var kHotFragmentThreshold = 500;
var k1Shift32 = 4294967296.0;
var SR_IE = 0x00000001;
var SR_EXL = 0x00000002;
var SR_ERL = 0x00000004;
var SR_KSU_KER = 0x00000000;
var SR_KSU_SUP = 0x00000008;
var SR_KSU_USR = 0x00000010;
var SR_KSU_MASK = 0x00000018;
var SR_UX = 0x00000020;
var SR_SX = 0x00000040;
var SR_KX = 0x00000080;
var SR_IBIT1 = 0x00000100;
var SR_IBIT2 = 0x00000200;
var SR_IBIT3 = 0x00000400;
var SR_IBIT4 = 0x00000800;
var SR_IBIT5 = 0x00001000;
var SR_IBIT6 = 0x00002000;
var SR_IBIT7 = 0x00004000;
var SR_IBIT8 = 0x00008000;
var SR_IMASK0 = 0x0000ff00;
var SR_IMASK1 = 0x0000fe00;
var SR_IMASK2 = 0x0000fc00;
var SR_IMASK3 = 0x0000f800;
var SR_IMASK4 = 0x0000f000;
var SR_IMASK5 = 0x0000e000;
var SR_IMASK6 = 0x0000c000;
var SR_IMASK7 = 0x00008000;
var SR_IMASK8 = 0x00000000;
var SR_IMASK = 0x0000ff00;
var SR_DE = 0x00010000;
var SR_CE = 0x00020000;
var SR_CH = 0x00040000;
var SR_SR = 0x00100000;
var SR_TS = 0x00200000;
var SR_BEV = 0x00400000;
var SR_ITS = 0x01000000;
var SR_RE = 0x02000000;
var SR_FR = 0x04000000;
var SR_RP = 0x08000000;
var SR_CU0 = 0x10000000;
var SR_CU1 = 0x20000000;
var SR_CU2 = 0x40000000;
var SR_CU3 = 0x80000000;
var SR_CUMASK = 0xf0000000;
var CAUSE_BD = 0x80000000;
var CAUSE_CEMASK = 0x30000000;
var CAUSE_CESHIFT = 28;
var CAUSE_SW1 = 0x00000100;
var CAUSE_SW2 = 0x00000200;
var CAUSE_IP3 = 0x00000400;
var CAUSE_IP4 = 0x00000800;
var CAUSE_IP5 = 0x00001000;
var CAUSE_IP6 = 0x00002000;
var CAUSE_IP7 = 0x00004000;
var CAUSE_IP8 = 0x00008000;
var CAUSE_IPMASK = 0x0000FF00;
var CAUSE_IPSHIFT = 8;
var CAUSE_EXCMASK = 0x0000007C;
var CAUSE_EXCSHIFT = 2;
var EXC_INT = 0;
var EXC_MOD = 4;
var EXC_RMISS = 8;
var EXC_WMISS = 12;
var EXC_RADE = 16;
var EXC_WADE = 20;
var EXC_IBE = 24;
var EXC_DBE = 28;
var EXC_SYSCALL = 32;
var EXC_BREAK = 36;
var EXC_II = 40;
var EXC_CPU = 44;
var EXC_OV = 48;
var EXC_TRAP = 52;
var EXC_VCEI = 56;
var EXC_FPE = 60;
var EXC_WATCH = 92;
var EXC_VCED = 124;
var FPCSR_RM_RN = 0x00000000;
var FPCSR_RM_RZ = 0x00000001;
var FPCSR_RM_RP = 0x00000002;
var FPCSR_RM_RM = 0x00000003;
var FPCSR_FI = 0x00000004;
var FPCSR_FU = 0x00000008;
var FPCSR_FO = 0x00000010;
var FPCSR_FZ = 0x00000020;
var FPCSR_FV = 0x00000040;
var FPCSR_EI = 0x00000080;
var FPCSR_EU = 0x00000100;
var FPCSR_EO = 0x00000200;
var FPCSR_EZ = 0x00000400;
var FPCSR_EV = 0x00000800;
var FPCSR_CI = 0x00001000;
var FPCSR_CU = 0x00002000;
var FPCSR_CO = 0x00004000;
var FPCSR_CZ = 0x00008000;
var FPCSR_CV = 0x00010000;
var FPCSR_CE = 0x00020000;
var FPCSR_C = 0x00800000;
var FPCSR_FS = 0x01000000;
var FPCSR_RM_MASK = 0x00000003;
var TLBHI_VPN2MASK = 0xffffe000;
var TLBHI_VPN2SHIFT = 13;
var TLBHI_PIDMASK = 0xff;
var TLBHI_PIDSHIFT = 0;
var TLBHI_NPID = 255;
var TLBLO_PFNMASK = 0x3fffffc0;
var TLBLO_PFNSHIFT = 6;
var TLBLO_CACHMASK = 0x38;
var TLBLO_CACHSHIFT = 3;
var TLBLO_UNCACHED = 0x10;
var TLBLO_NONCOHRNT = 0x18;
var TLBLO_EXLWR = 0x28;
var TLBLO_D = 0x4;
var TLBLO_V = 0x2;
var TLBLO_G = 0x1;
var TLBINX_PROBE = 0x80000000;
var TLBINX_INXMASK = 0x3f;
var TLBINX_INXSHIFT = 0;
var TLBRAND_RANDMASK = 0x3f;
var TLBRAND_RANDSHIFT = 0;
var TLBWIRED_WIREDMASK = 0x3f;
var TLBCTXT_BASEMASK = 0xff800000;
var TLBCTXT_BASESHIFT = 23;
var TLBCTXT_BASEBITS = 9;
var TLBCTXT_VPNMASK = 0x7ffff0;
var TLBCTXT_VPNSHIFT = 4;
var TLBPGMASK_4K = 0x00000000;
var TLBPGMASK_16K = 0x00006000;
var TLBPGMASK_64K = 0x0001e000;
var TLBPGMASK_256K = 0x0007e000;
var TLBPGMASK_1M = 0x001fe000;
var TLBPGMASK_4M = 0x007fe000;
var TLBPGMASK_16M = 0x01ffe000;
var kStuffToDoHalt = 1<<0;
var kStuffToDoCheckInterrupts = 1<<1;
var kStuffToDoBreakout = 1<<2;
var kVIIntrCycles = 62500;
var kEventVbl = 0;
var kEventCompare = 1;
var kEventRunForCycles = 2;
function TLBEntry() {
this.pagemask = 0;
this.hi = 0;
this.pfne = 0;
this.pfno = 0;
this.mask = 0;
this.global = 0;
}
TLBEntry.prototype = {
update : function(index, pagemask, hi, entrylo0, entrylo1) {
if (kDebugTLB) {
n64js.log('TLB update: index=' + index +
', pagemask=' + n64js.toString32(pagemask) +
', entryhi=' + n64js.toString32(hi) +
', entrylo0=' + n64js.toString32(entrylo0) +
', entrylo1=' + n64js.toString32(entrylo1)
);
switch (pagemask) {
case TLBPGMASK_4K: n64js.log(' 4k Pagesize'); break;
case TLBPGMASK_16K: n64js.log(' 16k Pagesize'); break;
case TLBPGMASK_64K: n64js.log(' 64k Pagesize'); break;
case TLBPGMASK_256K: n64js.log(' 256k Pagesize'); break;
case TLBPGMASK_1M: n64js.log(' 1M Pagesize'); break;
case TLBPGMASK_4M: n64js.log(' 4M Pagesize'); break;
case TLBPGMASK_16M: n64js.log(' 16M Pagesize'); break;
default: n64js.log(' Unknown Pagesize'); break;
}
}
this.pagemask = pagemask;
this.hi = hi;
this.pfne = entrylo0;
this.pfno = entrylo1;
this.global = (entrylo0 & entrylo1 & TLBLO_G);
this.mask = pagemask | (~TLBHI_VPN2MASK);
this.mask2 = this.mask>>>1;
this.vpnmask = (~this.mask)>>>0;
this.vpn2mask = this.vpnmask>>>1;
this.addrcheck = (hi & this.vpnmask)>>>0;
this.pfnehi = (this.pfne << TLBLO_PFNSHIFT) & this.vpn2mask;
this.pfnohi = (this.pfno << TLBLO_PFNSHIFT) & this.vpn2mask;
switch (this.pagemask) {
case TLBPGMASK_4K: this.checkbit = 0x00001000; break;
case TLBPGMASK_16K: this.checkbit = 0x00004000; break;
case TLBPGMASK_64K: this.checkbit = 0x00010000; break;
case TLBPGMASK_256K: this.checkbit = 0x00040000; break;
case TLBPGMASK_1M: this.checkbit = 0x00100000; break;
case TLBPGMASK_4M: this.checkbit = 0x00400000; break;
case TLBPGMASK_16M: this.checkbit = 0x01000000; break;
default: // shouldn't happen!
this.checkbit = 0; break;
}
}
};
function CPU0() {
this.ram = undefined; // bound to in reset n64js.getRamU8Array();
this.gprLoMem = new ArrayBuffer(32*4);
this.gprHiMem = new ArrayBuffer(32*4);
this.gprLoBytes = new Uint8Array(this.gprLoMem); // Used to help form addresses without causing deopts or generating HeapNumbers.
this.gprLo = new Uint32Array(this.gprLoMem);
this.gprHi = new Uint32Array(this.gprHiMem);
this.gprLo_signed = new Int32Array(this.gprLoMem);
this.gprHi_signed = new Int32Array(this.gprHiMem);
this.controlMem = new ArrayBuffer(32*4);
this.control = new Uint32Array(this.controlMem);
this.control_signed = new Int32Array(this.controlMem);
this.pc = 0;
this.delayPC = 0;
this.nextPC = 0; // Set to the next expected PC before an op executes. Ops can update this to change control flow without branch delay (e.g. likely branches, ERET)
this.branchTarget = 0; // Set to indicate a branch has been taken. Sets the delayPC for the subsequent op.
var UT_VEC = 0x80000000;
var XUT_VEC = 0x80000080;
var ECC_VEC = 0x80000100;
var E_VEC = 0x80000180;
this.stuffToDo = 0; // used to flag r4300 to cease execution
this.events = [];
this.multHiMem = new ArrayBuffer(2*4);
this.multLoMem = new ArrayBuffer(2*4);
this.multHi = new Uint32Array(this.multHiMem);
this.multLo = new Uint32Array(this.multLoMem);
this.multHi_signed = new Int32Array(this.multHiMem);
this.multLo_signed = new Int32Array(this.multLoMem);
this.getCount = function() {
return this.control[this.kControlCount];
}
this.tlbEntries = [];
for (var i = 0; i < 32; ++i) {
this.tlbEntries.push(new TLBEntry());
}
this.getGPR_s64 = function (r) {
return (this.gprHi_signed[r] * k1Shift32) + this.gprLo[r];
}
this.getGPR_u64 = function (r) {
return (this.gprHi[r] * k1Shift32) + this.gprLo[r];
}
this.setGPR_s64 = function (r, v) {
this.gprHi_signed[r] = Math.floor( v / k1Shift32 );
this.gprLo_signed[r] = v;
}
this.reset = function () {
hitCounts = {};
fragmentMap = {};
fragmentInvalidationEvents = [];
this.ram = n64js.getRamU8Array();
for (var i = 0; i < 32; ++i) {
this.gprLo[i] = 0;
this.gprHi[i] = 0;
this.control[i] = 0;
}
for (var i = 0; i < 32; ++i) {
this.tlbEntries[i].update(i, 0, 0x80000000, 0, 0);
}
this.pc = 0;
this.delayPC = 0;
this.nextPC = 0;
this.branchTarget = 0;
this.stuffToDo = 0;
this.events = [];
this.multLo[0] = this.multLo[1] = 0;
this.multHi[0] = this.multHi[1] = 0;
this.control[this.kControlRand] = 32-1;
this.control[this.kControlSR] = 0x70400004;
this.control[this.kControlConfig] = 0x0006e463;
this.addEvent(kEventVbl, kVIIntrCycles);
};
this.breakExecution = function () {
this.stuffToDo |= kStuffToDoHalt;
}
this.speedHack = function () {
var next_instruction = n64js.readMemoryInternal32(this.pc + 4);
if (next_instruction === 0) {
if (this.events.length > 0) {
// Ignore the kEventRunForCycles event
var run_countdown = 0;
if (this.events[0].type === kEventRunForCycles && this.events.length > 1) {
run_countdown += this.events[0].countdown;
this.events.splice(0,1);
}
var to_skip = run_countdown + this.events[0].countdown - 1;
//n64js.log('speedhack: skipping ' + to_skip + ' cycles');
this.control[this.kControlCount] += to_skip;
this.events[0].countdown = 1;
// Re-add the kEventRunForCycles event
if (run_countdown) {
this.addEvent(kEventRunForCycles, run_countdown);
}
} else {
n64js.log('no events');
}
} else {
n64js.log('next instruction does something');
}
}
this.updateCause3 = function () {
if (n64js.miInterruptsUnmasked()) {
this.control[this.kControlCause] |= CAUSE_IP3;
if (this.checkForUnmaskedInterrupts()) {
this.stuffToDo |= kStuffToDoCheckInterrupts;
}
} else {
this.control[this.kControlCause] &= ~CAUSE_IP3;
}
checkCauseIP3Consistent();
}
this.setSR = function (value) {
var old_value = this.control[this.kControlSR];
if ((old_value & SR_FR) !== (value & SR_FR)) {
n64js.log('Changing FPU to ' + ((value & SR_FR) ? '64bit' : '32bit' ));
}
this.control[this.kControlSR] = value;
setCop1Enable( (value & SR_CU1) !== 0 );
if (this.checkForUnmaskedInterrupts()) {
this.stuffToDo |= kStuffToDoCheckInterrupts;
}
};
this.checkForUnmaskedInterrupts = function () {
var sr = this.control[this.kControlSR];
// Ensure ERL/EXL are clear and IE is set
if ((sr & (SR_EXL | SR_ERL | SR_IE)) === SR_IE) {
// Check if interrupts are actually pending, and wanted
var cause = this.control[this.kControlCause];
if ((sr & cause & CAUSE_IPMASK) !== 0) {
return true;
}
}
return false;
}
this.throwTLBException = function (address, exc_code, vec) {
this.control[this.kControlBadVAddr] = address;
this.control[this.kControlContext] &= 0xff800000;
this.control[this.kControlContext] |= ((address >>> 13) << 4);
this.control[this.kControlEntryHi] &= 0x00001fff;
this.control[this.kControlEntryHi] |= (address & 0xfffffe000);
// XXXX check we're not inside exception handler before snuffing CAUSE reg?
this.setException( CAUSE_EXCMASK, exc_code );
this.nextPC = vec;
}
this.throwTLBReadMiss = function (address) { this.throwTLBException(address, EXC_RMISS, UT_VEC); }
this.throwTLBWriteMiss = function (address) { this.throwTLBException(address, EXC_WMISS, UT_VEC); }
this.throwTLBReadInvalid = function (address) { this.throwTLBException(address, EXC_RMISS, E_VEC); }
this.throwTLBWriteInvalid = function (address) { this.throwTLBException(address, EXC_WMISS, E_VEC); }
this.throwCop1Unusable = function () {
// XXXX check we're not inside exception handler before snuffing CAUSE reg?
this.setException( CAUSE_EXCMASK|CAUSE_CEMASK, EXC_CPU | 0x10000000 );
this.nextPC = E_VEC;
}
this.handleInterrupt = function () {
if (this.checkForUnmaskedInterrupts()) {
this.setException( CAUSE_EXCMASK, EXC_INT );
// this is handled outside of the main dispatch loop, so need to update pc directly
this.pc = E_VEC;
this.delayPC = 0;
} else {
n64js.assert(false, "Was expecting an unmasked interrupt - something wrong with kStuffToDoCheckInterrupts?");
}
}
this.setException = function (mask, exception) {
this.control[this.kControlCause] &= ~mask;
this.control[this.kControlCause] |= exception
this.control[this.kControlSR] |= SR_EXL;
this.control[this.kControlEPC] = this.pc;
if (this.delayPC) {
this.control[this.kControlCause] |= CAUSE_BD;
this.control[this.kControlEPC] -= 4;
} else {
this.control[this.kControlCause] &= ~CAUSE_BD;
}
}
this.setCompare = function (value) {
this.control[this.kControlCause] &= ~CAUSE_IP8;
if (value === this.control[this.kControlCompare]) {
// just clear the IP8 flag
} else {
if (value != 0) {
var count = this.control[this.kControlCount];
if (value > count) {
var delta = value - count;
this.removeEventsOfType(kEventCompare);
this.addEvent(kEventCompare, delta);
} else {
n64js.warn('setCompare underflow - was' + n64js.toString32(count) + ', setting to ' + value);
}
}
}
this.control[this.kControlCompare] = value;
};
function Event(type, countdown) {
this.type = type;
this.countdown = countdown;
}
Event.prototype = {
getName : function () {
switch(this.type) {
case kEventVbl: return 'Vbl';
case kEventCompare: return 'Compare';
case kEventRunForCycles: return 'Run';
}
return '?';
}
}
this.addEvent = function(type, countdown) {
n64js.assert( countdown >0, "Countdown is invalid" );
for (var i = 0; i < this.events.length; ++i) {
var event = this.events[i];
if (countdown <= event.countdown) {
event.countdown -= countdown;
this.events.splice(i, 0, new Event(type, countdown));
return;
}
countdown -= event.countdown;
}
this.events.push(new Event(type, countdown));
}
this.removeEventsOfType = function (type) {
for (var i = 0; i < this.events.length; ++i) {
if (this.events[i].type == type) {
// Add this countdown on to the subsequent event
if ((i+1) < this.events.length) {
this.events[i+1].countdown += this.events[i].countdown;
}
this.events.splice(i, 1);
return;
}
}
};
this.hasEvent = function(type) {
for (var i = 0; i < this.events.length; ++i) {
if (this.events[i].type == type) {
return true;
}
}
return false;
}
this.getRandom = function () {
var wired = this.control[this.kControlWired] & 0x1f;
var random = Math.floor(Math.random() * (32-wired)) + wired;
n64js.assert(random >= wired && random <= 31, "Ooops - random should be in range " + wired + "..31, but got " + random);
return random;
}
function setTLB(cpu, index) {
var pagemask = cpu.control[cpu.kControlPageMask];
var entryhi = cpu.control[cpu.kControlEntryHi];
var entrylo1 = cpu.control[cpu.kControlEntryLo1];
var entrylo0 = cpu.control[cpu.kControlEntryLo0];
cpu.tlbEntries[index].update(index, pagemask, entryhi, entrylo0, entrylo1);
}
this.tlbWriteIndex = function () {
setTLB(this, this.control[this.kControlIndex] & 0x1f);
}
this.tlbWriteRandom = function () {
setTLB(this, this.getRandom());
}
this.tlbRead = function () {
var index = this.control[this.kControlIndex] & 0x1f;
var tlb = this.tlbEntries[index];
this.control[this.kControlPageMask] = tlb.mask;
this.control[this.kControlEntryHi ] = tlb.hi;
this.control[this.kControlEntryLo0] = tlb.pfne | tlb.global;
this.control[this.kControlEntryLo1] = tlb.pfno | tlb.global;
if (kDebugTLB) {
n64js.log('TLB Read Index ' + n64js.toString8(index) + '.');
n64js.log(' PageMask: ' + n64js.toString32(this.control[this.kControlPageMask]));
n64js.log(' EntryHi: ' + n64js.toString32(this.control[this.kControlEntryHi]));
n64js.log(' EntryLo0: ' + n64js.toString32(this.control[this.kControlEntryLo0]));
n64js.log(' EntryLo1: ' + n64js.toString32(this.control[this.kControlEntryLo1]));
}
}
this.tlbProbe = function () {
var entryhi = this.control[this.kControlEntryHi];
var entryhi_vpn2 = entryhi & TLBHI_VPN2MASK;
var entryhi_pid = entryhi & TLBHI_PIDMASK;
for (var i = 0; i < 32; ++i) {
var tlb = this.tlbEntries[i];
if ( (tlb.hi & TLBHI_VPN2MASK) === entryhi_vpn2) {
if (((tlb.hi & TLBHI_PIDMASK) === entryhi_pid) ||
tlb.global) {
if (kDebugTLB) {
n64js.log('TLB Probe. EntryHi:' + n64js.toString32(entryhi) + '. Found matching TLB entry - ' + n64js.toString8(i));
}
this.control[this.kControlIndex] = i;
return;
}
}
}
if (kDebugTLB) {
n64js.log('TLB Probe. EntryHi:' + n64js.toString32(entryhi) + ". Didn't find matching entry");
}
this.control[this.kControlIndex] = TLBINX_PROBE;
}
this.tlbFindEntry = function (address) {
for(var count = 0; count < 32; ++count) {
// NB should put mru cache here
var i = count;
var tlb = this.tlbEntries[i];
if ((address & tlb.vpnmask) === tlb.addrcheck) {
if (!tlb.global) {
var ehi = this.control[this.kControlEntryHi];
if ((tlb.hi & TLBHI_PIDMASK) !== (ehi & TLBHI_PIDMASK) ) {
// Entries ASID must match
continue;
}
}
return tlb;
}
}
return null;
}
this.translateReadInternal = function (address) {
var tlb = this.tlbFindEntry(address);
if (tlb) {
var valid;
var physical_addr;
if (address & tlb.checkbit) {
valid = (tlb.pfno & TLBLO_V) !== 0;
physical_addr = tlb.pfnohi | (address & tlb.mask2);
} else {
valid = (tlb.pfne & TLBLO_V) !== 0;
physical_addr = tlb.pfnehi | (address & tlb.mask2);
}
if (valid)
return physical_addr;
return 0;
}
return 0;
}
this.translateRead = function (address) {
var tlb = this.tlbFindEntry(address);
if (tlb) {
var valid;
var physical_addr;
if (address & tlb.checkbit) {
valid = (tlb.pfno & TLBLO_V) !== 0;
physical_addr = tlb.pfnohi | (address & tlb.mask2);
} else {
valid = (tlb.pfne & TLBLO_V) !== 0;
physical_addr = tlb.pfnehi | (address & tlb.mask2);
}
if (valid)
return physical_addr;
this.throwTLBReadInvalid(address);
return 0;
}
this.throwTLBReadMiss(address);
return 0;
}
this.translateWrite = function (address) {
var tlb = this.tlbFindEntry(address);
if (tlb) {
var valid;
var physical_addr;
if (address & tlb.checkbit) {
valid = (tlb.pfno & TLBLO_V) !== 0;
physical_addr = tlb.pfnohi | (address & tlb.mask2);
} else {
valid = (tlb.pfne & TLBLO_V) !== 0;
physical_addr = tlb.pfnehi | (address & tlb.mask2);
}
if (valid)
return physical_addr;
this.throwTLBWriteInvalid(address);
return 0;
}
this.throwTLBWriteMiss(address);
return 0;
}
// General purpose register constants
this.kRegister_r0 = 0x00;
this.kRegister_at = 0x01;
this.kRegister_v0 = 0x02;
this.kRegister_v1 = 0x03;
this.kRegister_a0 = 0x04;
this.kRegister_a1 = 0x05;
this.kRegister_a2 = 0x06;
this.kRegister_a3 = 0x07;
this.kRegister_t0 = 0x08;
this.kRegister_t1 = 0x09;
this.kRegister_t2 = 0x0a;
this.kRegister_t3 = 0x0b;
this.kRegister_t4 = 0x0c;
this.kRegister_t5 = 0x0d;
this.kRegister_t6 = 0x0e;
this.kRegister_t7 = 0x0f;
this.kRegister_s0 = 0x10;
this.kRegister_s1 = 0x11;
this.kRegister_s2 = 0x12;
this.kRegister_s3 = 0x13;
this.kRegister_s4 = 0x14;
this.kRegister_s5 = 0x15;
this.kRegister_s6 = 0x16;
this.kRegister_s7 = 0x17;
this.kRegister_t8 = 0x18;
this.kRegister_t9 = 0x19;
this.kRegister_k0 = 0x1a;
this.kRegister_k1 = 0x1b;
this.kRegister_gp = 0x1c;
this.kRegister_sp = 0x1d;
this.kRegister_s8 = 0x1e;
this.kRegister_ra = 0x1f;
// Control register constants
this.kControlIndex = 0;
this.kControlRand = 1;
this.kControlEntryLo0 = 2;
this.kControlEntryLo1 = 3;
this.kControlContext = 4;
this.kControlPageMask = 5;
this.kControlWired = 6;
//...
this.kControlBadVAddr = 8;
this.kControlCount = 9;
this.kControlEntryHi = 10;
this.kControlCompare = 11;
this.kControlSR = 12;
this.kControlCause = 13;
this.kControlEPC = 14;
this.kControlPRId = 15;
this.kControlConfig = 16;
this.kControlLLAddr = 17;
this.kControlWatchLo = 18;
this.kControlWatchHi = 19;
//...
this.kControlECC = 26;
this.kControlCacheErr = 27;
this.kControlTagLo = 28;
this.kControlTagHi = 29;
this.kControlErrorEPC = 30;
};
function CPU1() {
this.control = new Uint32Array(32);
this.mem = new ArrayBuffer(32 * 4); // 32 32-bit regs
this.float32 = new Float32Array(this.mem);
this.float64 = new Float64Array(this.mem);
this.int32 = new Int32Array(this.mem);
this.uint32 = new Uint32Array(this.mem);
this.reset = function () {
for (var i = 0; i < 32; ++i) {
this.control[i] = 0;
this.int32[i] = 0;
}
this.control[0] = 0x00000511;
}
this.setCondition = function (v) {
if (v)
this.control[31] |= FPCSR_C;
else
this.control[31] &= ~FPCSR_C;
}
this.store_64 = function (i, lo, hi) {
this.int32[i+0] = lo;
this.int32[i+1] = hi;
}
this.load_f64 = function (i) {
return this.float64[i>>1];
}
this.load_s64_as_double = function (i) {
return (this.int32[i+1] * k1Shift32) + this.int32[i];
}
this.store_f64 = function (i, v) {
this.float64[i>>1] = v;
}
};
// Expose the cpu state
var cpu0 = new CPU0();
var cpu1 = new CPU1();
n64js.cpu0 = cpu0;
n64js.cpu1 = cpu1;
function fd(i) { return (i>>> 6)&0x1f; }
function fs(i) { return (i>>>11)&0x1f; }
function ft(i) { return (i>>>16)&0x1f; }
function copop(i) { return (i>>>21)&0x1f; }
function offset(i) { return ((i&0xffff)<<16)>>16; }
function sa(i) { return (i>>> 6)&0x1f; }
function rd(i) { return (i>>>11)&0x1f; }
function rt(i) { return (i>>>16)&0x1f; }
function rs(i) { return (i>>>21)&0x1f; }
function op(i) { return (i>>>26)&0x1f; }
function tlbop(i) { return i&0x3f; }
function cop1_func(i) { return i&0x3f; }
function cop1_bc(i) { return (i>>>16)&0x3; }
function target(i) { return (i )&0x3ffffff; }
function imm(i) { return (i )&0xffff; }
function imms(i) { return ((i&0xffff)<<16)>>16; } // treat immediate value as signed
function base(i) { return (i>>>21)&0x1f; }
function memaddr(i) {
return cpu0.gprLo[base(i)] + imms(i);
}
function branchAddress(pc,i) { return ((pc+4) + (offset(i)*4))>>>0; }
//function branchAddress(pc,i) { return (((pc>>>2)+1) + offset(i))<<2; } // NB: convoluted calculation to avoid >>>0 (deopt)
function jumpAddress(pc,i) { return ((pc&0xf0000000) | (target(i)*4))>>>0; }
function performBranch(new_pc) {
//if (new_pc < 0) {
// n64js.log('Oops, branching to negative address: ' + new_pc);
// throw 'Oops, branching to negative address: ' + new_pc;
//}
cpu0.branchTarget = new_pc;
}
function setSignExtend(r,v) {
cpu0.gprLo[r] = v;
cpu0.gprHi_signed[r] = v >> 31;
}
function setZeroExtend(r, v) {
cpu0.gprLo[r] = v;
cpu0.gprHi_signed[r] = 0;
}
function setHiLoSignExtend(arr, v) {
arr[0] = v;
arr[1] = v >> 31;
}
function setHiLoZeroExtend(arr, v) {
arr[0] = v;
arr[1] = 0;
}
function genSrcRegLo(i) {
if (i === 0)
return '0';
return 'rlo[' + i + ']';
}
function genSrcRegHi(i) {
if (i === 0)
return '0';
return 'rhi[' + i + ']';
}
//
// Memory access routines.
//
// These are out of line so that the >>>0 doesn't cause a shift-i deopt in the body of the calling function
function lwu_slow(addr) { return n64js.readMemoryU32(addr>>>0); }
function lhu_slow(addr) { return n64js.readMemoryU16(addr>>>0); }
function lbu_slow(addr) { return n64js.readMemoryU8( addr>>>0); }
function lw_slow(addr) { return n64js.readMemoryS32(addr>>>0); }
function lh_slow(addr) { return n64js.readMemoryS16(addr>>>0); }
function lb_slow(addr) { return n64js.readMemoryS8( addr>>>0); }
function sw_slow(addr, value) { n64js.writeMemory32(addr>>>0, value); }
function sh_slow(addr, value) { n64js.writeMemory16(addr>>>0, value); }
function sb_slow(addr, value) { n64js.writeMemory8( addr>>>0, value); }
function load_u8(ram, addr) {
if (addr < -2139095040) {
var phys = (addr + 0x80000000) | 0; // NB: or with zero ensures we return an SMI if possible.
return ram[phys];
}
return lbu_slow(addr);
}
function load_s8(ram, addr) {
if (addr < -2139095040) {
var phys = (addr + 0x80000000) | 0; // NB: or with zero ensures we return an SMI if possible.
return (ram[phys] << 24) >> 24;
}
return lb_slow(addr);
}
function load_u16(ram, addr) {
if (addr < -2139095040) {
var phys = (addr + 0x80000000) | 0; // NB: or with zero ensures we return an SMI if possible.
return (ram[phys] << 8) | ram[phys+1];
}
return lhu_slow(addr);
}
function load_s16(ram, addr) {
if (addr < -2139095040) {
var phys = (addr + 0x80000000) | 0; // NB: or with zero ensures we return an SMI if possible.
return ((ram[phys] << 24) | (ram[phys+1] << 16)) >> 16;
}
return lh_slow(addr);
}
function load_u32(ram, addr) {
if (addr < -2139095040) {
var phys = (addr + 0x80000000) | 0; // NB: or with zero ensures we return an SMI if possible.
return ((ram[phys] << 24) | (ram[phys+1] << 16) | (ram[phys+2] << 8) | ram[phys+3]) >>> 0;
}
return lw_slow(addr);
}
function load_s32(ram, addr) {
if (addr < -2139095040) {
var phys = (addr + 0x80000000) | 0; // NB: or with zero ensures we return an SMI if possible.
return ((ram[phys] << 24) | (ram[phys+1] << 16) | (ram[phys+2] << 8) | ram[phys+3]) | 0;
}
return lw_slow(addr);
}
function store_8(ram, addr, value) {
if (addr < -2139095040) {
var phys = (addr + 0x80000000) | 0; // NB: or with zero ensures we return an SMI if possible.
ram[phys] = value;
} else {
sb_slow(addr, value);
}
}
function store_16(ram, addr, value) {
if (addr < -2139095040) {
var phys = (addr + 0x80000000) | 0; // NB: or with zero ensures we return an SMI if possible.
ram[phys ] = value >> 8;
ram[phys+1] = value;
} else {
sh_slow(addr, value);
}
}
function store_32(ram, addr, value) {
if (addr < -2139095040) {
var phys = (addr + 0x80000000) | 0; // NB: or with zero ensures we return an SMI if possible.
ram[phys+0] = value >> 24;
ram[phys+1] = value >> 16;
ram[phys+2] = value >> 8;
ram[phys+3] = value;
} else {
sw_slow(addr, value);
}
}
function unimplemented(pc,i) {
var r = n64js.disassembleOp(pc,i);
var e = 'Unimplemented op ' + n64js.toString32(i) + ' : ' + r.disassembly + '<br>';
$('#output').append(e);
throw e;
}
function executeUnknown(i) {
throw 'Unknown op: ' + n64js.toString32(cpu0.pc) + ', ' + n64js.toString32(i);