forked from hulkholden/n64js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
n64.js
3086 lines (2585 loc) · 95.1 KB
/
n64.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 SP_MEM_ADDR_REG = 0x00;
var SP_DRAM_ADDR_REG = 0x04;
var SP_RD_LEN_REG = 0x08;
var SP_WR_LEN_REG = 0x0C;
var SP_STATUS_REG = 0x10;
var SP_DMA_FULL_REG = 0x14;
var SP_DMA_BUSY_REG = 0x18;
var SP_SEMAPHORE_REG = 0x1C;
var SP_CLR_HALT = 0x0000001;
var SP_SET_HALT = 0x0000002;
var SP_CLR_BROKE = 0x0000004;
var SP_CLR_INTR = 0x0000008;
var SP_SET_INTR = 0x0000010;
var SP_CLR_SSTEP = 0x0000020;
var SP_SET_SSTEP = 0x0000040;
var SP_CLR_INTR_BREAK = 0x0000080;
var SP_SET_INTR_BREAK = 0x0000100;
var SP_CLR_SIG0 = 0x0000200;
var SP_SET_SIG0 = 0x0000400;
var SP_CLR_SIG1 = 0x0000800;
var SP_SET_SIG1 = 0x0001000;
var SP_CLR_SIG2 = 0x0002000;
var SP_SET_SIG2 = 0x0004000;
var SP_CLR_SIG3 = 0x0008000;
var SP_SET_SIG3 = 0x0010000;
var SP_CLR_SIG4 = 0x0020000;
var SP_SET_SIG4 = 0x0040000;
var SP_CLR_SIG5 = 0x0080000;
var SP_SET_SIG5 = 0x0100000;
var SP_CLR_SIG6 = 0x0200000;
var SP_SET_SIG6 = 0x0400000;
var SP_CLR_SIG7 = 0x0800000;
var SP_SET_SIG7 = 0x1000000;
var SP_STATUS_HALT = 0x0001;
var SP_STATUS_BROKE = 0x0002;
var SP_STATUS_DMA_BUSY = 0x0004;
var SP_STATUS_DMA_FULL = 0x0008;
var SP_STATUS_IO_FULL = 0x0010;
var SP_STATUS_SSTEP = 0x0020;
var SP_STATUS_INTR_BREAK = 0x0040;
var SP_STATUS_SIG0 = 0x0080;
var SP_STATUS_SIG1 = 0x0100;
var SP_STATUS_SIG2 = 0x0200;
var SP_STATUS_SIG3 = 0x0400;
var SP_STATUS_SIG4 = 0x0800;
var SP_STATUS_SIG5 = 0x1000;
var SP_STATUS_SIG6 = 0x2000;
var SP_STATUS_SIG7 = 0x4000;
var SP_STATUS_YIELD = SP_STATUS_SIG0;
var SP_STATUS_YIELDED = SP_STATUS_SIG1;
var SP_STATUS_TASKDONE = SP_STATUS_SIG2;
// DP Command
var DPC_START_REG = 0x00;
var DPC_END_REG = 0x04;
var DPC_CURRENT_REG = 0x08;
var DPC_STATUS_REG = 0x0C;
var DPC_CLOCK_REG = 0x10;
var DPC_BUFBUSY_REG = 0x14;
var DPC_PIPEBUSY_REG = 0x18;
var DPC_TMEM_REG = 0x1C;
var DPC_CLR_XBUS_DMEM_DMA = 0x0001;
var DPC_SET_XBUS_DMEM_DMA = 0x0002;
var DPC_CLR_FREEZE = 0x0004;
var DPC_SET_FREEZE = 0x0008;
var DPC_CLR_FLUSH = 0x0010;
var DPC_SET_FLUSH = 0x0020;
var DPC_CLR_TMEM_CTR = 0x0040;
var DPC_CLR_PIPE_CTR = 0x0080;
var DPC_CLR_CMD_CTR = 0x0100;
var DPC_CLR_CLOCK_CTR = 0x0200;
var DPC_STATUS_XBUS_DMEM_DMA = 0x001;
var DPC_STATUS_FREEZE = 0x002;
var DPC_STATUS_FLUSH = 0x004;
var DPC_STATUS_START_GCLK = 0x008;
var DPC_STATUS_TMEM_BUSY = 0x010;
var DPC_STATUS_PIPE_BUSY = 0x020;
var DPC_STATUS_CMD_BUSY = 0x040;
var DPC_STATUS_CBUF_READY = 0x080;
var DPC_STATUS_DMA_BUSY = 0x100;
var DPC_STATUS_END_VALID = 0x200;
var DPC_STATUS_START_VALID = 0x400;
// DP Span
var DPS_TBIST_REG = 0x00;
var DPS_TEST_MODE_REG = 0x04;
var DPS_BUFTEST_ADDR_REG = 0x08;
var DPS_BUFTEST_DATA_REG = 0x0C;
var DPS_TBIST_CHECK = 0x01;
var DPS_TBIST_GO = 0x02;
var DPS_TBIST_CLEAR = 0x04;
var DPS_TBIST_DONE = 0x004;
var DPS_TBIST_FAILED = 0x7F8;
// MIPS Interface
var MI_MODE_REG = 0x00;
var MI_VERSION_REG = 0x04;
var MI_INTR_REG = 0x08;
var MI_INTR_MASK_REG = 0x0C;
var MI_CLR_INIT = 0x0080;
var MI_SET_INIT = 0x0100;
var MI_CLR_EBUS = 0x0200;
var MI_SET_EBUS = 0x0400;
var MI_CLR_DP_INTR = 0x0800;
var MI_CLR_RDRAM = 0x1000;
var MI_SET_RDRAM = 0x2000;
var MI_MODE_INIT = 0x0080;
var MI_MODE_EBUS = 0x0100;
var MI_MODE_RDRAM = 0x0200;
var MI_INTR_MASK_CLR_SP = 0x0001;
var MI_INTR_MASK_SET_SP = 0x0002;
var MI_INTR_MASK_CLR_SI = 0x0004;
var MI_INTR_MASK_SET_SI = 0x0008;
var MI_INTR_MASK_CLR_AI = 0x0010;
var MI_INTR_MASK_SET_AI = 0x0020;
var MI_INTR_MASK_CLR_VI = 0x0040;
var MI_INTR_MASK_SET_VI = 0x0080;
var MI_INTR_MASK_CLR_PI = 0x0100;
var MI_INTR_MASK_SET_PI = 0x0200;
var MI_INTR_MASK_CLR_DP = 0x0400;
var MI_INTR_MASK_SET_DP = 0x0800;
var MI_INTR_MASK_SP = 0x01;
var MI_INTR_MASK_SI = 0x02;
var MI_INTR_MASK_AI = 0x04;
var MI_INTR_MASK_VI = 0x08;
var MI_INTR_MASK_PI = 0x10;
var MI_INTR_MASK_DP = 0x20;
var MI_INTR_SP = 0x01;
var MI_INTR_SI = 0x02;
var MI_INTR_AI = 0x04;
var MI_INTR_VI = 0x08;
var MI_INTR_PI = 0x10;
var MI_INTR_DP = 0x20;
// Video Interface
var VI_STATUS_REG = 0x00;
var VI_ORIGIN_REG = 0x04;
var VI_WIDTH_REG = 0x08;
var VI_INTR_REG = 0x0C;
var VI_CURRENT_REG = 0x10;
var VI_BURST_REG = 0x14;
var VI_V_SYNC_REG = 0x18;
var VI_H_SYNC_REG = 0x1C;
var VI_LEAP_REG = 0x20;
var VI_H_START_REG = 0x24;
var VI_V_START_REG = 0x28;
var VI_V_BURST_REG = 0x2C;
var VI_X_SCALE_REG = 0x30;
var VI_Y_SCALE_REG = 0x34;
var VI_CONTROL_REG = VI_STATUS_REG;
var VI_DRAM_ADDR_REG = VI_ORIGIN_REG;
var VI_H_WIDTH_REG = VI_WIDTH_REG;
var VI_V_INTR_REG = VI_INTR_REG;
var VI_V_CURRENT_LINE_REG = VI_CURRENT_REG;
var VI_TIMING_REG = VI_BURST_REG;
var VI_H_SYNC_LEAP_REG = VI_LEAP_REG;
var VI_H_VIDEO_REG = VI_H_START_REG;
var VI_V_VIDEO_REG = VI_V_START_REG;
// Audio Interface
var AI_DRAM_ADDR_REG = 0x00;
var AI_LEN_REG = 0x04;
var AI_CONTROL_REG = 0x08;
var AI_STATUS_REG = 0x0C;
var AI_DACRATE_REG = 0x10;
var AI_BITRATE_REG = 0x14;
// Peripheral Interface
var PI_DRAM_ADDR_REG = 0x00;
var PI_CART_ADDR_REG = 0x04;
var PI_RD_LEN_REG = 0x08;
var PI_WR_LEN_REG = 0x0C;
var PI_STATUS_REG = 0x10;
var PI_BSD_DOM1_LAT_REG = 0x14;
var PI_BSD_DOM1_PWD_REG = 0x18;
var PI_BSD_DOM1_PGS_REG = 0x1C;
var PI_BSD_DOM1_RLS_REG = 0x20;
var PI_BSD_DOM2_LAT_REG = 0x24;
var PI_BSD_DOM2_PWD_REG = 0x28;
var PI_BSD_DOM2_PGS_REG = 0x2C;
var PI_BSD_DOM2_RLS_REG = 0x30;
// Values read from status reg
var PI_STATUS_DMA_BUSY = 0x01;
var PI_STATUS_IO_BUSY = 0x02;
var PI_STATUS_DMA_IO_BUSY = 0x03;
var PI_STATUS_ERROR = 0x04;
// Values written to status reg
var PI_STATUS_RESET = 0x01;
var PI_STATUS_CLR_INTR = 0x02;
var PI_DOM1_ADDR1 = 0x06000000;
var PI_DOM1_ADDR2 = 0x10000000;
var PI_DOM1_ADDR3 = 0x1FD00000;
var PI_DOM2_ADDR1 = 0x05000000;
var PI_DOM2_ADDR2 = 0x08000000;
function IsDom1Addr1( address ) { return address >= PI_DOM1_ADDR1 && address < PI_DOM2_ADDR2; }
function IsDom1Addr2( address ) { return address >= PI_DOM1_ADDR2 && address < 0x1FBFFFFF; }
function IsDom1Addr3( address ) { return address >= PI_DOM1_ADDR3 && address < 0x7FFFFFFF; }
function IsDom2Addr1( address ) { return address >= PI_DOM2_ADDR1 && address < PI_DOM1_ADDR1; }
function IsDom2Addr2( address ) { return address >= PI_DOM2_ADDR2 && address < PI_DOM1_ADDR2; }
// RDRAM Interface
var RI_MODE_REG = 0x00;
var RI_CONFIG_REG = 0x04;
var RI_CURRENT_LOAD_REG = 0x08;
var RI_SELECT_REG = 0x0C;
var RI_REFRESH_REG = 0x10;
var RI_COUNT_REG = RI_REFRESH_REG;
var RI_LATENCY_REG = 0x14;
var RI_RERROR_REG = 0x18;
var RI_WERROR_REG = 0x1C;
var RI_LAST_REG = RI_WERROR_REG;
// Serial Interface
var SI_DRAM_ADDR_REG = 0x00;
var SI_PIF_ADDR_RD64B_REG = 0x04;
var SI_PIF_ADDR_WR64B_REG = 0x10;
var SI_STATUS_REG = 0x18;
var SI_STATUS_DMA_BUSY = 0x0001;
var SI_STATUS_RD_BUSY = 0x0002;
var SI_STATUS_DMA_ERROR = 0x0008;
var SI_STATUS_INTERRUPT = 0x1000;
function AssertException(message) { this.message = message; }
AssertException.prototype.toString = function () {
return 'AssertException: ' + this.message;
}
function assert(e,m) {
if (!e) {
throw new AssertException(m);
}
}
n64js.assert = assert;
n64js.log = function (s) {
$output.append(toString32(n64js.cpu0.pc) + ': ' + s + '<br>');
$output.scrollTop($output[0].scrollHeight);
}
n64js.check = function(e, m) {
if (!e) {
n64js.log(m);
}
}
n64js.warn = function(m) {
n64js.log(m);
}
n64js.halt = function (msg) {
running = false;
n64js.cpu0.breakExecution();
n64js.log('<span style="color:red">' + msg + '</span>');
n64js.displayError(msg);
}
n64js.displayWarning = function (message) {
var $alert = $('<div class="alert"><button class="close" data-dismiss="alert">×</button><strong>Warning!</strong> ' + message + '</div>');
$('#alerts').append($alert);
}
n64js.displayError = function (message) {
var $alert = $('<div class="alert alert-error"><button class="close" data-dismiss="alert">×</button><strong>Error!</strong> ' + message + '</div>');
$('#alerts').append($alert);
}
// Similar to halt, but just relinquishes control to the system
n64js.returnControlToSystem = function() {
n64js.cpu0.breakExecution();
}
n64js.isRunning = function () {
return running;
}
n64js.startRunning = function () {
running = true;
}
n64js.toggleRun = function () {
running = !running;
return running;
}
n64js.setOutputElement = function ($e) {
$output = $e;
}
n64js.setDebugElements = function ($debug, $stat, $regs, $disasm) {
$debugContent = $debug;
$status = $stat;
$registers = $regs;
$disassembly = $disasm;
}
n64js.setDynarecElements = function ($dr) {
$dynarecContent = $dr;
}
n64js.setMemoryElement = function ($e) {
$memoryContent = $e;
}
n64js.down = function () {
disasmAddress += 4;
n64js.refreshDisplay();
}
n64js.up = function () {
disasmAddress -= 4;
n64js.refreshDisplay();
}
n64js.pageDown = function () {
disasmAddress += 64;
n64js.refreshDisplay();
}
n64js.pageUp = function () {
disasmAddress -= 64;
n64js.refreshDisplay();
}
function makeLabelColor(address) {
var i = (address>>>2); // Lowest bits are always 0
var hash = (i>>>16) ^ ((i&0xffff) * 2803);
var r = (hash )&0x1f;
var g = (hash>>> 5)&0x1f;
var b = (hash>>>10)&0x1f;
var h = (hash>>>15)&0x3;
r = (r*4);
g = (g*4);
b = (b*4);
if (h === 0) {
r*=2; g*=2;
} else if (h === 1) {
g*=2; b*=2;
} else if (h === 2) {
b*=2; r*=2
} else {
r*=2;g*=2;b*=2;
}
return '#' + n64js.toHex(r,8) + n64js.toHex(g,8) + n64js.toHex(b,8);
}
var lastCycles;
var lastPC = -1;
var recentMemoryAccesses = [];
var lastMemoryAccessAddress;
var lastStore; // When we execute a store instruction, keep track of some details so we can show the value that was written
// access is {reg,offset,mode}
function addRecentMemoryAccess(address, mode, cycle) {
var col = (mode === 'store') ? '#faa' : '#ffa';
if (mode == 'update') {
col = '#afa';
}
var highlights = {};
var aligned_addr = (address&~3)>>>0;
highlights[aligned_addr] = col;
return makeMemoryTable(address, 32, 32, highlights);
}
n64js.refreshDisplay = function () {
if ($dynarecContent.hasClass('active')) {
updateDynarec();
}
if ($debugContent.hasClass('active')) {
updateDebug();
}
if ($memoryContent.hasClass('active')) {
var $mem = $('<pre></pre>');
$mem.append( makeMemoryTable(lastMemoryAccessAddress || 0x80000000, 1024) );
$memoryContent.html($mem);
}
}
function updateDebug() {
var cpu0 = n64js.cpu0;
// If the pc has changed since the last update, recenter the display (e.g. when we take a branch)
if (cpu0.pc !== lastPC) {
disasmAddress = cpu0.pc;
lastPC = cpu0.pc;
}
var cpu_count = cpu0.getCount();
var is_single_step = lastCycles === (cpu_count-1);
lastCycles = cpu_count;
var cur_instr;
var disassembly = n64js.disassemble(disasmAddress - 64, disasmAddress + 64);
var dis_body = disassembly.map(function (a) {
var label_span = a.isJumpTarget ? '<span class="dis-label-target">' : '<span class="dis-label">';
var label = label_span + n64js.toHex(a.instruction.address, 32) + ':</span>';
var t = label + ' ' + n64js.toHex(a.instruction.opcode, 32) + ' ' + a.disassembly;
if (a.instruction.address == cpu0.pc) {
cur_instr = a.instruction;
t = '<span style="background-color: #ffa">' + t + '</span>';
}
return t;
}).join('<br>');
// Keep a small queue showing recent memory accesses
if (is_single_step) {
// Check if we've just stepped over a previous write op, and update the result
if (lastStore) {
if (lastStore.cycle+1 === cpu0.opsExecuted) {
var updated_element = addRecentMemoryAccess(lastStore.address, 'update');
lastStore.element.append(updated_element);
}
lastStore = undefined;
}
if (cur_instr.memory) {
var access = cur_instr.memory;
var new_addr = n64js.cpu0.gprLo[access.reg] + access.offset;
var element = addRecentMemoryAccess(new_addr, access.mode);
if (access.mode === 'store') {
lastStore = {address:new_addr, cycle:cpu0.opsExecuted, element:element};
}
recentMemoryAccesses.push({element:element});
// Nuke anything that happened more than N cycles ago
//while (recentMemoryAccesses.length > 0 && recentMemoryAccesses[0].cycle+10 < cycle)
if (recentMemoryAccesses.length > 4)
recentMemoryAccesses.splice(0,1);
lastMemoryAccessAddress = new_addr;
}
} else {
// Clear the recent memory accesses when running.
recentMemoryAccesses = [];
lastStore = undefined;
}
var labelMap = {
0x80328730: 'setFP',
0x8032b030: 'siIsBusy',
0x80328740: 'siReadPIFControl',
0x80328790: 'siWritePIFControl',
0x80324258: 'mult64',
0x80324158: 'div64'
};
var regColours = {};
var availColours = [
'#F4EEAF', // yellow
'#AFF4BB', // green
'#F4AFBE' // blue
];
if (cur_instr) {
var nextColIdx = 0;
for (var i in cur_instr.srcRegs) {
if (!regColours.hasOwnProperty(i)) {
regColours[i] = availColours[nextColIdx++];
}
}
for (var i in cur_instr.dstRegs) {
if (!regColours.hasOwnProperty(i)) {
regColours[i] = availColours[nextColIdx++];
}
}
}
var $dis = $('<pre>' + dis_body + '</pre>');
$dis.find('.dis-label').each(function (){
var address = parseInt($(this).text(), 16);
if (labelMap.hasOwnProperty(address)) {
$(this).prepend(labelMap[address] + '\n');
}
});
$dis.find('.dis-label-target').each(function (){
var address = parseInt($(this).text(), 16);
if (labelMap.hasOwnProperty(address)) {
$(this).text(labelMap[address]);
}
$(this).css('color', makeLabelColor(address));
$(this).click(function () {
disasmAddress = address;
n64js.refreshDisplay();
});
});
$disassembly.html('');
if (recentMemoryAccesses.length > 0) {
var $recent = $('<pre />');
var fading_cols = ['#bbb', '#999', '#666', '#333'];
for (var i = 0; i < recentMemoryAccesses.length; ++i) {
var element = recentMemoryAccesses[i].element;
element.css('color', fading_cols[i]);
$recent.append(element);
}
$disassembly.append($recent);
}
$disassembly.append($dis);
for (var i in regColours) {
$dis.find('.dis-reg-' + i).css('background-color', regColours[i]);
}
$status.html(makeStatusTable());
var $table0 = $('<table class="register-table"><tbody></tbody></table>');
var $body0 = $table0.find('tbody');
var kRegistersPerRow = 2;
for (var i = 0; i < 32; i+=kRegistersPerRow) {
var $tr = $('<tr />');
for (var r = 0; r < kRegistersPerRow; ++r) {
var name = n64js.cop0gprNames[i+r];
var $td = $('<td>' + name + '</td><td class="fixed">' + toString64(cpu0.gprHi[i+r], cpu0.gprLo[i+r]) + '</td>');
if (regColours.hasOwnProperty(name)) {
$td.attr('bgcolor', regColours[name]);
}
$tr.append($td);
}
$body0.append($tr);
}
$registers[0].html($table0);
var $table1 = $('<table class="register-table"><tbody></tbody></table>');
addCop1($table1.find('tbody'), regColours);
$registers[1].html($table1);
}
// bytes_per_row should be power-of-two
function makeMemoryTable(focus_address, context_bytes, bytes_per_row, highlights) {
bytes_per_row = bytes_per_row || 64;
highlights = highlights || {};
function roundDown(x, a) {
return x & ~(a-1);
}
var s = roundDown(focus_address, bytes_per_row) - roundDown(context_bytes/2, bytes_per_row);
var e = s + context_bytes;
var t = '';
for (var a = s; a < e; a += bytes_per_row) {
var r = toHex(a, 32) + ':';
for (var o = 0; o < bytes_per_row; o += 4) {
var cur_address = a+o >>> 0;
var mem = n64js.readMemoryInternal32(cur_address);
var style = '';
if (highlights.hasOwnProperty(cur_address))
style = ' style="background-color: ' + highlights[cur_address] + '"';
r += ' <span id="mem-' + toHex(cur_address, 32) + '"' + style + '>' + toHex(mem, 32) + '</span>';
}
r += '\n';
t += r;
}
return $('<span>' + t + '</span>');
}
function makeStatusTable() {
var cpu0 = n64js.cpu0;
var $status_table = $('<table class="register-table"><tbody></tbody></table>');
var $status_body = $status_table.find('tbody');
$status_body.append('<tr><td>Ops</td><td class="fixed">' + cpu0.opsExecuted + '</td></tr>');
$status_body.append('<tr><td>PC</td><td class="fixed">' + toString32(cpu0.pc) + '</td><td>delayPC</td><td class="fixed">' + toString32(cpu0.delayPC) + '</td></tr>');
$status_body.append('<tr><td>MultHi</td><td class="fixed">' + toString64(cpu0.multHi[1], cpu0.multHi[0]) +
'</td><td>Cause</td><td class="fixed">' + toString32(n64js.cpu0.control[n64js.cpu0.kControlCause]) + '</td></tr>');
$status_body.append('<tr><td>MultLo</td><td class="fixed">' + toString64(cpu0.multLo[1], cpu0.multLo[0]) +
'</td><td>Count</td><td class="fixed">' + toString32(n64js.cpu0.control[n64js.cpu0.kControlCount]) + '</td></tr>');
$status_body.append('<tr><td></td><td class="fixed">' +
'</td><td>Compare</td><td class="fixed">' + toString32(n64js.cpu0.control[n64js.cpu0.kControlCompare]) + '</td></tr>');
for (var i = 0; i < cpu0.events.length; ++i) {
$status_body.append('<tr><td>Event' + i + '</td><td class="fixed">' + cpu0.events[i].countdown + ', ' + cpu0.events[i].getName() + '</td></tr>');
}
addSR($status_body);
addMipsInterrupts($status_body);
return $status_table;
}
function addCop1($tb, regColours) {
var cpu1 = n64js.cpu1;
for (var i = 0; i < 32; ++i) {
var name = n64js.cop1RegisterNames[i];
if ((i&1) === 0) {
var $td = $('<td>' + name +
'</td><td class="fixed fp-w">' + toString32(cpu1.uint32[i]) +
'</td><td class="fixed fp-s">' + cpu1.float32[i] +
'</td><td class="fixed fp-d">' + cpu1.float64[i/2] +
'</td>' );
} else {
var $td = $('<td>' + name +
'</td><td class="fixed fp-w">' + toString32(cpu1.uint32[i]) +
'</td><td class="fixed fp-s">' + cpu1.float32[i] +
'</td><td>' +
'</td>' );
}
var $tr = $('<tr />');
$tr.append($td);
if (regColours.hasOwnProperty(name)) {
$tr.attr('bgcolor', regColours[name]);
} else if (regColours.hasOwnProperty(name + '-w')) {
$tr.find('.fp-w').attr('bgcolor', regColours[name + '-w']);
} else if (regColours.hasOwnProperty(name + '-s')) {
$tr.find('.fp-s').attr('bgcolor', regColours[name + '-s']);
} else if (regColours.hasOwnProperty(name + '-d')) {
$tr.find('.fp-d').attr('bgcolor', regColours[name + '-d']);
}
$tb.append($tr);
}
}
function addSR($tb) {
var $tr = $('<tr />');
$tr.append( '<td>SR</td>' );
var SR_IE = 0x00000001;
var SR_EXL = 0x00000002;
var SR_ERL = 0x00000004;
var SR_KSU_SUP = 0x00000008;
var SR_KSU_USR = 0x00000010;
var SR_UX = 0x00000020;
var SR_SX = 0x00000040;
var SR_KX = 0x00000080;
var flag_names = ['IE', 'EXL', 'ERL' ];//, '', '', 'UX', 'SX', 'KX' ];
var sr = n64js.cpu0.control[n64js.cpu0.kControlSR];
var $td = $('<td />');
$td.append( toString32(sr) );
$td.append(' ');
for (var i = flag_names.length-1; i >= 0; --i) {
if (flag_names[i]) {
var is_set = (sr & (1<<i)) !== 0;
var $b = $('<span>' + flag_names[i] + '</span>');
if (is_set) {
$b.css('font-weight', 'bold');
}
$td.append($b);
$td.append(' ');
}
}
$tr.append($td);
$tb.append($tr);
}
function addMipsInterrupts($tb) {
var mi_intr_names = ['SP', 'SI', 'AI', 'VI', 'PI', 'DP'];
var mi_intr_live = mi_reg.readU32(MI_INTR_REG);
var mi_intr_mask = mi_reg.readU32(MI_INTR_MASK_REG);
var $tr = $('<tr />');
$tr.append( '<td>MI Intr</td>' );
var $td = $('<td />');
for (var i = 0; i < mi_intr_names.length; ++i) {
var is_set = (mi_intr_live & (1<<i)) !== 0;
var is_enabled = (mi_intr_mask & (1<<i)) !== 0;
var $b = $('<span>' + mi_intr_names[i] + '</span>');
if (is_set) {
$b.css('font-weight', 'bold');
}
if (is_enabled) {
$b.css('background-color', '#AFF4BB');
}
$td.append($b);
$td.append(' ');
}
$tr.append($td);
$tb.append($tr);
}
function log10(x) {
return Math.log(x) / Math.log(10);
}
function updateDynarec() {
var fragmentMap = n64js.getFragmentMap();
var invals = n64js.getFragmentInvalidationEvents();
var histo = {};
// Build a flattened list of all fragments
var fragments_list = [];
for(var i in fragmentMap) {
var fragment = fragmentMap[i];
var v = fragment.executionCount;
var logv = v > 0 ? Math.floor(log10(v)) : 0;
if (histo[logv] === undefined)
histo[logv] = 0;
histo[logv]++;
fragments_list.push(fragment);
}
fragments_list.sort(function (a,b) {
return b.opsCompiled*b.executionCount - a.opsCompiled*a.executionCount;
});
var $t = $('<div />');
var t;
// Histogram showing execution counts
t = '';
t += '<div class="row-fluid">';
t += '<div class="span4"><table class="table table-condensed"><tr><th>Execution Count</th><th>Frequency</th></tr>';
for(var i in histo) {
var v = Number(i);
var range = Math.pow(10, v) + '..' + Math.pow(10, v+1);
t += '<tr><td>' + range + '</td><td>' + histo[i] + '</td></tr>';
}
t += '</table></div>';
t += '</div>';
$t.append(t);
// Table of hot fragments, and the corresponding js
t = '';
t += '<div class="row-fluid">';
t += ' <div class="span6" id="fragments" />';
t += ' <div class="span6" id="fragment_code" />';
t += '</div>';
var $fragment_div = $(t);
createHotFragmentsTable($fragment_div, fragments_list);
$t.append($fragment_div);
// Evictions
if (invals.length > 0) {
t = '';
t += '<div class="row-fluid">';
t += '<div class="span6"><table class="table table-condensed"><tr><th>Address</th><th>Length</th><th>System</th><th>Fragments Removed</th></tr>';
for (var i = 0; i < invals.length; ++i) {
var vals = [
n64js.toString32(invals[i].address),
invals[i].length,
invals[i].system,
invals[i].fragmentsRemoved
];
t += '<tr><td>' + vals.join('</td><td>') + '</td></tr>';
}
t += '</table></div>';
t += '</div>';
$t.append(t);
}
$dynarecContent.html($t);
}
function createHotFragmentsTable($fragment_div, fragments_list) {
var $code = $fragment_div.find('#fragment_code');
var $table = $('<table class="table table-condensed" />');
var columns = ['Address', 'Execution Count', 'Length', 'ExecCount * Length'];
$table.append('<tr><th>' + columns.join('</th><th>') + '</th></tr>');
for (var i = 0; i < fragments_list.length && i < 20; ++i) {
var fragment = fragments_list[i];
var vals = [
n64js.toString32(fragment.entryPC),
fragment.executionCount,
fragment.opsCompiled,
fragment.executionCount * fragment.opsCompiled
];
var $tr = $('<tr><<td>' + vals.join('</td><td>') + '</td></tr>');
$tr.click(
(function (f) {
return function() {
$code.html('<pre>' + f.func.toString() + '</pre>');
};
})(fragment)
);
$table.append($tr);
}
$fragment_div.find('#fragments').append($table);
if (fragments_list.length > 0) {
$code.append('<pre>' + fragments_list[0].func.toString() + '</pre>');
}
}
//
// Memory handlers
//
function Memory(arrayBuffer) {
this.arrayBuffer = arrayBuffer;
this.length = arrayBuffer.byteLength;
this.u8 = new Uint8Array(arrayBuffer);
}
Memory.prototype = {
clear : function () {
var u32s = new Uint32Array(this.arrayBuffer);
for (var i = 0; i < u32s.length; ++i) {
u32s[i] = 0;
}
},
readU32 : function (offset) {
return ((this.u8[offset] << 24) | (this.u8[offset+1] << 16) | (this.u8[offset+2] << 8) | this.u8[offset+3])>>>0;
},
readU16 : function (offset) {
return (this.u8[offset] << 8) | (this.u8[offset+1] );
},
readU8 : function (offset) {
return this.u8[offset];
},
readS32 : function (offset) {
return ((this.u8[offset] << 24) | (this.u8[offset+1] << 16) | (this.u8[offset+2] << 8) | this.u8[offset+3]) | 0;
},
readS16 : function(offset) {
return ((this.u8[offset] << 24) | (this.u8[offset+1] << 16) ) >> 16;
},
readS8 : function(offset) {
return ((this.u8[offset] << 24) ) >> 24;
},
write32 : function (offset, value) {
this.u8[offset+0] = value >> 24;
this.u8[offset+1] = value >> 16;
this.u8[offset+2] = value >> 8;
this.u8[offset+3] = value;
},
write16 : function (offset,value) {
this.u8[offset ] = value >> 8;
this.u8[offset+1] = value;
},
write8 : function (offset,value) {
this.u8[offset] = value;
},
clearBits32 : function (offset, bits) {
var value = this.readU32(offset) & ~bits;
this.write32(offset, value);
return value;
},
setBits32 : function (offset, bits) {
var value = this.readU32(offset) | bits;
this.write32(offset, value);
return value;
},
getBits32 : function (offset, bits) {
return this.readU32(offset) & bits;
}
};
function MemoryCopy(dst, dstoff, src, srcoff, len) {
for (var i = 0; i < len; ++i) {
dst.u8[dstoff+i] = src.u8[srcoff+i];
}
}
function Device(name, mem, rangeStart, rangeEnd) {
this.name = name;
this.mem = mem;
this.u8 = mem ? mem.u8 : null;
this.rangeStart = rangeStart;
this.rangeEnd = rangeEnd;
this.quiet = false;
}
Device.prototype = {
setMem : function(mem) {
this.mem = mem;
this.u8 = mem.u8;
},
calcEA : function (address) {
return address - this.rangeStart;
},
readInternal32 : function (address) {
var ea = this.calcEA(address);
// We need to make sure this doesn't throw, so do a bounds check
if (ea+3 < this.mem.u8.length)
return this.mem.readU32(ea);
return 0xdddddddd;
},
readU32 : function (address) {
if (!this.quiet) n64js.log('Reading from ' + this.name + ': ' + toString32(address) );
var ea = this.calcEA(address);
return this.mem.readU32(ea);
},
readU16 : function (address) {
if (!this.quiet) n64js.log('Reading from ' + this.name + ': ' + toString32(address) );
var ea = this.calcEA(address);
return this.mem.readU16(ea);
},
readU8 : function (address) {
if (!this.quiet) n64js.log('Reading from ' + this.name + ': ' + toString32(address) );
var ea = this.calcEA(address);
return this.mem.readU8(ea);
},
readS32 : function (address) {
if (!this.quiet) n64js.log('Reading from ' + this.name + ': ' + toString32(address) );
var ea = this.calcEA(address);
return this.mem.readS32(ea);
},
readS16 : function (address) {
if (!this.quiet) n64js.log('Reading from ' + this.name + ': ' + toString32(address) );
var ea = this.calcEA(address);
return this.mem.readS16(ea);
},
readS8 : function (address) {
if (!this.quiet) n64js.log('Reading from ' + this.name + ': ' + toString32(address) );
var ea = this.calcEA(address);