forked from IchigoJam/asm15
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpuemu.html
executable file
·1827 lines (1806 loc) · 55.5 KB
/
cpuemu.html
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
<!DOCTYPE html><html><head><meta charset="utf-8"/>
<meta name="viewport" content="target-densitydpi=low-dpi, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, width=device-width"/>
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black"/><!-- default / black / black-translucent -->
<meta name="format-detection" content="telephone=no"/>
<title>cpuemu - Alpha1</title>
<meta property="og:image" content="cpuemu.png">
<link rel="apple-touch-icon" href="cpuemu.png"/>
<style>
body {
font-family: sans-serif;
}
</style>
<script src="lib/fukuno.js"></script>
<script src="readmcode.js"></script>
<script src="ijfont.js"></script>
<script>"use strict";
var MEM = function() {
return {
ram: [],
check: function(ad, len) {
for (var i = 0; i < len; i++) {
if (isNaN(this.ram[ad + i])) return false;
}
return true;
},
get: function(ad) {
if (isNaN(this.ram[ad])) return 0;
return this.ram[ad];
},
set: function(ad, n) {
this.ram[ad] = n & 0xff;
if (this.notify) this.notify(ad);
},
get16: function(ad) {
var n = this.get(ad);
n |= this.get(ad + 1) << 8;
return n;
},
set16: function(ad, n) {
this.set(ad, n & 0xff);
this.set(ad + 1, (n >> 8) & 0xff);
},
get32: function(ad) {
var n = this.get16(ad);
n |= this.get16(ad + 2) << 16;
if (n < 0) n += 0x100000000;
return n;
},
set32: function(ad, n) {
this.set16(ad, n & 0xffff);
this.set16(ad + 2, (n >> 16) & 0xffff);
},
};
}
var CPU_M0 = function() {
this.r = []; // var:0-7, sp:13, link:14, pc:15
this.status = {
get: function() {
var res = 0;
res |= (this.n ? (1 << 31) : 0);
res |= (this.z ? (1 << 30) : 0);
res |= (this.c ? (1 << 29) : 0);
res |= (this.v ? (1 << 28) : 0);
res |= (this.q ? (1 << 27) : 0);
res |= ((this.it & 3) << 25);
res |= (this.j ? (1 << 24) : 0);
res |= ((this.ge & 0xf) << 16);
res |= ((this.it & 0xfc) << 8);
res |= (this.E ? (1 << 9) : 0);
res |= (this.A ? (1 << 8) : 0);
res |= (this.I ? (1 << 7) : 0);
res |= (this.F ? (1 << 6) : 0);
res |= (this.t ? (1 << 5) : 0);
res |= (this.mode & 0x1f);
return res & 0xffffffff;
},
set: function(n) {
this.n = (n & (1 << 31)) != 0;
this.z = (n & (1 << 30)) != 0;
this.c = (n & (1 << 29)) != 0;
this.v = (n & (1 << 28)) != 0;
this.q = (n & (1 << 27)) != 0;
this.it = (((n >> 8) & 0xfc) | ((n >> 25) & 3));
this.j = (n & (1 << 24)) != 0;
this.ge = ((n >> 16) & 0xf);
this.E = (n & (1 << 9)) != 0;
this.A = (n & (1 << 8)) != 0;
this.I = (n & (1 << 7)) != 0;
this.F = (n & (1 << 6)) != 0;
this.t = (n & (1 << 5)) != 0;
this.mode = n & 0x1f;
}
};
this.clockCount = 0;
this.reset = function() {
for (var i = 0; i < 16; i++)
this.r[i] = 0;
this.status.set(0);
this.clockCount = 0;
};
this.reset();
this.mem = new MEM();
this.step = function() {
var iaddr = this.r[15];
if (iaddr === 0xDEADBEEE) { // 0xDEADBEEF & 0xfffffffe
// instead of stopping execution at RET
return 0;
}
if (iaddr % 2 != 0) {
alert ("unaligned instructon read #" + iaddr.toString(16));
return 0;
}
if (!this.mem.check(iaddr, 2) && get("uninitialized_memory").value === "halt") {
alert("read instruction from uninitialized memory #" + iaddr.toString(16));
return 0;
}
var code = this.mem.get16(iaddr);
// this.code = code;
this.r[15] += 2;
if (this.r[15] >= 0x100000000) this.r[15] -= 0x100000000;
// console.log(code.toString(2));
// alert(code.toString(2));
try {
var chk = function(bin) {
var n = parseInt(bin, 2);
var c = code >> (16 - bin.length);
return c == n;
};
var getu = function(start, n) {
return (code >> start) & ((1 << n) - 1);
};
var getn = function(start, n) {
var res = (code >> start) & ((1 << n) - 1);
if ((res >> (n - 1)) != 0)
return res - (1 << n);
return res;
};
var norm = function(n) {
var v = 0x100000000;
while (n >= v) n -= v;
while (n < 0) n += v;
return n;
};
this.checkmem = function(addr, n, iswrite) {
if (addr % n != 0) {
throw "unaligned memory " + (iswrite ? "write" : "read") +
" #" + addr.toString(16);
}
if (!iswrite && !this.mem.check(addr, n) &&
get("uninitialized_memory").value === "halt") {
throw "read from uninitialized memory #" + addr.toString(16);
}
};
this.setflg = function(n) {
this.status.z = n == 0;
this.status.n = (n >> 31) != 0;
return n;
};
this.addsetflg = function(n, m, carry = false) {
n = norm(n);
m = norm(m);
var c = carry ? 1 : 0;
var r = norm(n + m + c);
var s = 0x80000000;
this.status.c = n + m + c > 0xffffffff;
this.status.v = (n & s) == (m & s) && (n & s) != (r & s);
return this.setflg(r);
};
this.subsetflg = function(n, m, carry = false) {
n = norm(n);
m = norm(m);
var c = carry ? 1 : 0;
var r = norm(n - m - c);
var s = 0x80000000;
this.status.c = carry ? (n > m) : (n >= m);
this.status.v = (n & s) != (m & s) && (n & s) != (r & s);
return this.setflg(r);
};
if (chk("00100")) { // Rd = u8
this.clockCount++;
this.r[getu(8, 3)] = this.setflg(getu(0, 8));
} else if (chk("01000110")) { // Rd = Rm
this.clockCount++;
var dstidx = getu(0, 3) | (getu(7, 1) << 3);
var srcidx = getu(3, 4);
var srcvalue;
if (srcidx == 15) { // PC
srcvalue = norm(this.r[srcidx] + 2); // PC + 4 = (PC + 2) + 2
} else {
srcvalue = this.r[srcidx];
}
if (dstidx == 15) { // PC
this.clockCount += 2;
this.r[dstidx] = norm(srcvalue & 0xfffffffe);
} else {
this.r[dstidx] = srcvalue;
}
} else if (chk("00110")) { // Rd += u8
this.clockCount++;
this.r[getu(8, 3)] = this.addsetflg(this.r[getu(8, 3)], getu(0, 8));
} else if (chk("00111")) { // Rd -= u8
this.clockCount++;
this.r[getu(8, 3)] = this.subsetflg(this.r[getu(8, 3)], getu(0, 8));
} else if (chk("10100")) { // Rd = PC + u8
this.clockCount++;
this.r[getu(8, 3)] = norm((this.r[15] + 2 + (getu(0, 8) << 2)) & 0xfffffffc);
} else if (chk("01000100")) { // Rd += Rm
this.clockCount++;
var dstidx = getu(0, 3) | (getu(7, 1) << 3);
var srcidx = getu(3, 4);
if (srcidx == 15 && dstidx == 15) {
throw "Rd += Rm with both PC isn't allowed";
}
var srcvalue;
if (srcidx == 15) { // PC
srcvalue = norm(this.r[srcidx] + 2); // PC + 4 = (PC + 2) + 2
} else {
srcvalue = this.r[srcidx];
}
if (dstidx == 15) { // PC
this.clockCount += 2;
this.r[dstidx] = norm((this.r[dstidx] + 2 + srcvalue) & 0xfffffffe);
} else {
this.r[dstidx] = norm(this.r[dstidx] + srcvalue);
}
} else if (chk("0001110")) { // Rd = Rn + u3
this.clockCount++;
this.r[getu(0, 3)] = this.addsetflg(this.r[getu(3, 3)], getu(6, 3));
} else if (chk("0001111")) { // Rd = Rn - u3
this.clockCount++;
this.r[getu(0, 3)] = this.subsetflg(this.r[getu(3, 3)], getu(6, 3));
} else if (chk("0001100")) { // Rd = Rn + Rm
this.clockCount++;
this.r[getu(0, 3)] = this.addsetflg(this.r[getu(3, 3)], this.r[getu(6, 3)]);
} else if (chk("0001101")) { // Rd = Rn - Rm
this.clockCount++;
this.r[getu(0, 3)] = this.subsetflg(this.r[getu(3, 3)], this.r[getu(6, 3)]);
} else if (chk("0100001001")) { // Rd = -Rm
this.clockCount++;
this.r[getu(0, 3)] = this.subsetflg(0, this.r[getu(3, 3)]);
} else if (chk("0100001101")) { // Rd *= Rm
this.clockCount++;
// don't do this.r[getu(0, 3)] * this.r[getu(3, 3)], or the result will be inaccurate
var ret = 0;
var val = this.r[getu(0, 3)];
var mult = this.r[getu(3, 3)];
for (var i = 0; i < 32; i++) {
if (mult & 1) ret = norm(ret + val);
val = norm(val + val);
mult >>>= 1;
}
this.r[getu(0, 3)] = this.setflg(ret);
} else if (chk("00000")) { // Rd = Rm << u5
this.clockCount++;
var v = this.r[getu(3, 3)];
var s = getu(6, 5); // 0 <= s <= 31
this.r[getu(0, 3)] = this.setflg(norm(v << s));
if (s != 0) this.status.c = ((v >> (32 - s)) & 1) != 0;
} else if (chk("00001")) { // Rd = Rm >> u5
this.clockCount++;
var v = this.r[getu(3, 3)];
var s = getu(6, 5); // 1 <= s <= 32
if (s == 0) {
this.r[getu(0, 3)] = this.setflg(0);
this.status.c = ((v >> 31) & 1) != 0;
} else {
this.r[getu(0, 3)] = this.setflg(norm(v >>> s));
this.status.c = ((v >> (s - 1)) & 1) != 0;
}
} else if (chk("0100000010")) { // Rd <<= Rs
this.clockCount++;
var v = this.r[getu(0, 3)];
var s = this.r[getu(3, 3)];
if (0 <= s && s < 32) {
this.r[getu(0, 3)] = this.setflg(norm(v << s));
if (s != 0) this.status.c = ((v >> (32 - s)) & 1) != 0;
} else {
this.r[getu(0, 3)] = this.setflg(0);
this.status.c = s == 32 && (v & 1) != 0;
}
} else if (chk("0100000011")) { // Rd >>= Rs
this.clockCount++;
var v = this.r[getu(0, 3)];
var s = this.r[getu(3, 3)];
if (0 <= s && s < 32) {
this.r[getu(0, 3)] = this.setflg(norm(v >>> s));
if (s != 0) this.status.c = ((v >> (s - 1)) & 1) != 0;
} else {
this.r[getu(0, 3)] = this.setflg(0);
this.status.c = s == 32 && ((v >> 31) & 1) != 0;
}
} else if (chk("0100001111")) { // Rd = ~Rm
this.clockCount++;
this.r[getu(0, 3)] = this.setflg(norm(~this.r[getu(3, 3)]));
} else if (chk("0100000000")) { // Rd &= Rm
this.clockCount++;
this.r[getu(0, 3)] = this.setflg(norm(this.r[getu(0, 3)] & this.r[getu(3, 3)]));
} else if (chk("0100001100")) { // Rd |= Rm
this.clockCount++;
this.r[getu(0, 3)] = this.setflg(norm(this.r[getu(0, 3)] | this.r[getu(3, 3)]));
} else if (chk("0100000001")) { // Rd ^= Rm
this.clockCount++;
this.r[getu(0, 3)] = this.setflg(norm(this.r[getu(0, 3)] ^ this.r[getu(3, 3)]));
} else if (chk("01111")) { // Rd = [Rn + u5]
this.clockCount += 2;
var addr = norm(this.r[getu(3, 3)] + getu(6, 5));
this.checkmem(addr, 1, false);
this.r[getu(0, 3)] = this.mem.get(addr);
} else if (chk("10001")) { // Rd = [Rn + u5]W
this.clockCount += 2;
var addr = norm(this.r[getu(3, 3)] + getu(6, 5) * 2);
this.checkmem(addr, 2, false);
this.r[getu(0, 3)] = this.mem.get16(addr);
} else if (chk("01101")) { // Rd = [Rn + u5]L
this.clockCount += 2;
var addr = norm(this.r[getu(3, 3)] + getu(6, 5) * 4);
this.checkmem(addr, 4, false);
this.r[getu(0, 3)] = this.mem.get32(addr);
} else if (chk("01001")) { // Rd = [PC + u8]L
this.clockCount += 2;
var addr = norm((this.r[15] + 2 + getu(0, 8) * 4) & 0xfffffffc);
this.checkmem(addr, 4, false);
this.r[getu(8, 3)] = this.mem.get32(addr);
} else if (chk("0101110")) { // Rd = [Rn + Rm]
this.clockCount += 2;
var addr = norm(this.r[getu(3, 3)] + this.r[getu(6, 3)]);
this.checkmem(addr, 1, false);
this.r[getu(0, 3)] = this.mem.get(addr);
} else if (chk("0101011")) { // Rd = [Rn + Rm]C
this.clockCount += 2;
var addr = norm(this.r[getu(3, 3)] + this.r[getu(6, 3)]);
this.checkmem(addr, 1, false);
var value = this.mem.get(addr);
if (value & 0x80) value = norm(value | 0xffffff00);
this.r[getu(0, 3)] = value;
} else if (chk("0101101")) { // Rd = [Rn + Rm]W
this.clockCount += 2;
var addr = norm(this.r[getu(3, 3)] + this.r[getu(6, 3)]);
this.checkmem(addr, 2, false);
this.r[getu(0, 3)] = this.mem.get16(addr);
} else if (chk("0101111")) { // Rd = [Rn + Rm]S
this.clockCount += 2;
var addr = norm(this.r[getu(3, 3)] + this.r[getu(6, 3)]);
this.checkmem(addr, 2, false);
var value = this.mem.get16(addr);
if (value & 0x8000) value = norm(value | 0xffff0000);
this.r[getu(0, 3)] = value;
} else if (chk("0101100")) { // Rd = [Rn + Rm]L
this.clockCount += 2;
var addr = norm(this.r[getu(3, 3)] + this.r[getu(6, 3)]);
this.checkmem(addr, 4, false);
this.r[getu(0, 3)] = this.mem.get32(addr);
} else if (chk("01110")) { // [Rn + u5] = Rd
this.clockCount += 2;
var addr = norm(this.r[getu(3, 3)] + getu(6, 5));
this.checkmem(addr, 1, true);
this.mem.set(addr, this.r[getu(0, 3)]);
} else if (chk("10000")) { // [Rn + u5]W = Rd
this.clockCount += 2;
var addr = norm(this.r[getu(3, 3)] + getu(6, 5) * 2);
this.checkmem(addr, 2, true);
this.mem.set16(addr, this.r[getu(0, 3)]);
} else if (chk("01100")) { // [Rn + u5]L = Rd
this.clockCount += 2;
var addr = norm(this.r[getu(3, 3)] + getu(6, 5) * 4);
this.checkmem(addr, 4, true);
this.mem.set32(addr, this.r[getu(0, 3)]);
} else if (chk("0101010")) { // [Rn + Rm] = Rd
this.clockCount += 2;
var addr = norm(this.r[getu(3, 3)] + this.r[getu(6, 3)]);
this.checkmem(addr, 1, true);
this.mem.set(addr, this.r[getu(0, 3)]);
} else if (chk("0101001")) { // [Rn + Rm]W = Rd
this.clockCount += 2;
var addr = norm(this.r[getu(3, 3)] + this.r[getu(6, 3)]);
this.checkmem(addr, 2, true);
this.mem.set16(addr, this.r[getu(0, 3)]);
} else if (chk("0101000")) { // [Rn + Rm]L = Rd
this.clockCount += 2;
var addr = norm(this.r[getu(3, 3)] + this.r[getu(6, 3)]);
this.checkmem(addr, 4, true);
this.mem.set32(addr, this.r[getu(0, 3)]);
} else if (chk("00101")) { // Rn - u8
this.clockCount++;
this.subsetflg(this.r[getu(8, 3)], getu(0, 8));
} else if (chk("01000101")) { // Rn - Rm
this.clockCount++;
var nidx = (getu(7, 1) << 3) | getu(0, 3);
var midx = getu(3, 4);
if (nidx == 15 || midx == 15) {
throw "using R15 in Rn - Rm is prohibited";
}
this.subsetflg(this.r[nidx], this.r[midx]);
} else if (chk("0100001010")) { // Rn - Rm
this.clockCount++;
this.subsetflg(this.r[getu(0, 3)], this.r[getu(3, 3)]);
} else if (chk("0100001011")) { // Rn + Rm
this.clockCount++;
this.addsetflg(this.r[getu(0, 3)], this.r[getu(3, 3)]);
} else if (chk("0100001000")) { // Rn & Rm
this.clockCount++;
this.setflg(norm(this.r[getu(0, 3)] & this.r[getu(3, 3)]));
} else if (chk("11011110")) { // Unused Opcode
throw "Unused Opcode";
} else if (chk("11011111")) { // SVC u8
throw "SVC " + getu(0, 8) + " is executed";
} else if (chk("1101")) { // IF cond GOTO n8
this.clockCount++;
var taken = false;
switch (getu(9, 3)) {
case 0: taken = this.status.z; break;
case 1: taken = this.status.c; break;
case 2: taken = this.status.n; break;
case 3: taken = this.status.v; break;
case 4: taken = !this.status.z && this.status.c; break;
case 5: taken = this.status.n === this.status.v; break;
case 6: taken = !this.status.z && this.status.n === this.status.v; break;
case 7: taken = true; break; // this is BKPT / Unpredictable
}
if (getu(8, 1)) taken = !taken;
if (taken) {
this.clockCount += 2;
// not +2 because PC+=2 is done already
this.r[15] = norm(this.r[15] + ((getn(0, 8) + 1) << 1));
}
} else if (chk("11100")) { // GOTO n11
this.clockCount += 3;
// not +2 because PC+=2 is done already
this.r[15] = norm(this.r[15] + ((getn(0, 11) + 1) << 1));
} else if (chk("01000111")) { // GOTO Rm / GOSUB Rm
this.clockCount += 3;
var isgosub = getu(7, 1) === 1;
var iname = isgosub ? "GOSUB" : "GOTO";
if (getu(0, 3) !== 0) throw "instruction like " + iname + " Rm, but last 3 bits are not `000";
var srcidx = getu(3, 4);
if (srcidx === 13) throw "using SP for " + iname + " Rm is prohibited";
if (srcidx === 15) throw "using PC for " + iname + " Rm is prohibited";
if ((this.r[srcidx] & 1) === 0) {
// if (!isgosub && srcidx == 14) { // RET
// alert("ret");
// return 0;
// }
throw "0 is passed as the LSB of the operand of " + iname + " Rm";
}
if (isgosub) this.r[14] = norm(this.r[15] | 1);
this.r[15] = norm(this.r[srcidx] & 0xfffffffe);
} else if (chk("11110")) { // GOSUB n22
this.clockCount++;
var upper = getn(0, 11);
if (!this.mem.check(this.r[15], 2) && get("uninitialized_memory").value === "halt") {
alert("read instruction from uninitialized memory #" + this.r[15].toString(16));
return 0;
}
var inst2 = this.mem.get16(this.r[15]);
if (((inst2 >> 11) & 0x1f) !== 0x1f) {
throw "partial GOSUB n22 instruction";
}
this.clockCount += 3;
var lower = inst2 & 0x7ff;
var delta = (upper << 11) | lower;
this.r[14] = norm((this.r[15] + 2) | 1);
this.r[15] = norm(this.r[15] + 2 + delta * 2);
} else if (chk("1011010")) { // PUSH {regs}
this.clockCount++;
var reglist = getu(0, 9);
var sp = this.r[13];
if (reglist & 0x100) {
this.clockCount++;
this.checkmem(sp = norm(sp - 4), 4, true);
this.mem.set32(sp, this.r[14]);
}
for (var i = 7; i >= 0; i--) {
if ((reglist >> i) & 1) {
this.clockCount++;
this.checkmem(sp = norm(sp - 4), 4, true);
this.mem.set32(sp, this.r[i]);
}
}
this.r[13] = sp;
} else if (chk("1011110")) { // POP {regs}
this.clockCount++;
var reglist = getu(0, 9);
var sp = this.r[13];
for (var i = 0; i <= 7; i++) {
if ((reglist >> i) & 1) {
this.clockCount++;
this.checkmem(sp, 4, false);
this.r[i] = this.mem.get32(sp);
sp = norm(sp + 4);
}
}
if (reglist & 0x100) {
this.clockCount += 4; // +1 for reglist and +3 for jump
this.checkmem(sp, 4, false);
var newpc = this.mem.get32(sp);
sp = norm(sp + 4);
this.r[13] = sp;
if (newpc & 1) {
this.r[15] = norm(newpc & 0xfffffffe);
} else {
throw "LSB of POPed value to PC is 0";
}
} else {
this.r[13] = sp;
}
} else if (chk("101100000")) { // SP += u7
this.clockCount++;
this.r[13] = norm(this.r[13] + getu(0, 7) * 4);
} else if (chk("101100001")) { // SP -= u7
this.clockCount++;
this.r[13] = norm(this.r[13] - getu(0, 7) * 4);
} else if (chk("10101")) { // Rd = SP + u8
this.clockCount++;
this.r[getu(8, 3)] = norm(this.r[13] + getu(0, 8) * 4);
} else if (chk("10011")) { // Rd = [SP + u8]L
this.clockCount += 2;
var addr = norm(this.r[13] + getu(0, 8) * 4);
this.checkmem(addr, 4, false);
this.r[getu(8, 3)] = this.mem.get32(addr);
} else if (chk("10010")) { // [SP + u8]L = Rd
this.clockCount += 2;
var addr = norm(this.r[13] + getu(0, 8) * 4);
this.checkmem(addr, 4, true);
this.mem.set32(addr, this.r[getu(8, 3)]);
} else if (chk("1011101000")) { // Rd = REV(Rm)
this.clockCount++;
var v = this.r[getu(3, 3)];
v = ((v >>> 8) & 0x00ff00ff) | ((v << 8) & 0xff00ff00);
v = ((v >>> 16) & 0x0000ffff) | ((v << 16) & 0xffff0000);
this.r[getu(0, 3)] = norm(v);
} else if (chk("1011101001")) { // Rd = REV16(Rm)
this.clockCount++;
var v = this.r[getu(3, 3)];
v = ((v >>> 8) & 0x00ff00ff) | ((v << 8) & 0xff00ff00);
this.r[getu(0, 3)] = norm(v);
} else if (chk("1011101011")) { // Rd = REVSH(Rm)
this.clockCount++;
var v = this.r[getu(3, 3)];
v = ((v >>> 8) & 0x000000ff) | ((v << 8) & 0x0000ff00);
if (v & 0x8000) v |= 0xffff0000;
this.r[getu(0, 3)] = norm(v);
} else if (chk("1011001001")) { // Rd = SXTB(Rm)
this.clockCount++;
var v = this.r[getu(3, 3)] & 0xff;
if (v & 0x80) v |= 0xffffff00;
this.r[getu(0, 3)] = norm(v);
} else if (chk("1011001000")) { // Rd = SXTH(Rm)
this.clockCount++;
var v = this.r[getu(3, 3)] & 0xffff;
if (v & 0x8000) v |= 0xffff0000;
this.r[getu(0, 3)] = norm(v);
} else if (chk("1011001011")) { // Rd = UXTB(Rm)
this.clockCount++;
this.r[getu(0, 3)] = norm(this.r[getu(3, 3)] & 0xff);
} else if (chk("1011001010")) { // Rd = UXTH(Rm)
this.clockCount++;
this.r[getu(0, 3)] = norm(this.r[getu(3, 3)] & 0xffff);
} else if (chk("00010")) { // Rd = ASR(Rm, u5)
this.clockCount++;
var v = this.r[getu(3, 3)];
var s = getu(6, 5); // 1 <= s <= 32
if (s == 0) {
this.r[getu(0, 3)] = this.setflg(norm(v >> 31));
this.status.c = ((v >> 31) & 1) != 0;
} else {
this.r[getu(0, 3)] = this.setflg(norm(v >> s));
this.status.c = ((v >> (s - 1)) & 1) != 0;
}
} else if (chk("0100000100")) { // ASR Rd, Rs
this.clockCount++;
var v = this.r[getu(0, 3)];
var s = this.r[getu(3, 3)];
if (0 <= s && s < 32) {
this.r[getu(0, 3)] = this.setflg(norm(v >> s));
if (s != 0) this.status.c = ((v >> (s - 1)) & 1) != 0;
} else {
this.r[getu(0, 3)] = this.setflg(norm(v >> 31));
this.status.c = ((v >> 31) & 1) != 0;
}
} else if (chk("0100001110")) { // BIC Rd, Rm (Rd &= ~Rm)
this.clockCount++;
this.r[getu(0, 3)] = this.setflg(norm(this.r[getu(0, 3)] & ~this.r[getu(3, 3)]));
} else if (chk("0100000111")) { // ROR Rd, Rs
this.clockCount++;
var v = this.r[getu(0, 3)];
var s = this.r[getu(3, 3)];
var s2 = s % 32;
if (s2 == 0) {
this.r[getu(0, 3)] = this.setflg(v);
if (s != 0) this.status.c = ((v >> 31) & 1) != 0;
} else {
this.r[getu(0, 3)] = this.setflg(norm((v >>> s2) | (v << (32 - s2))));
this.status.c = ((v >> (s2 - 1)) & 1) != 0;
}
} else if (chk("0100000101")) { // ADC Rd, Rm
this.clockCount++;
this.r[getu(0, 3)] = this.addsetflg(this.r[getu(0, 3)], this.r[getu(3, 3)], this.status.c);
} else if (chk("0100000110")) { // SBC Rd, Rm
this.clockCount++;
this.r[getu(0, 3)] = this.subsetflg(this.r[getu(0, 3)], this.r[getu(3, 3)], !this.status.c);
} else if (chk("11001")) { // LDM Rn, {regs}
this.clockCount++;
var regs = getu(0, 8);
if (regs == 0) {
throw "LDM with empty regs";
}
var dstidx = getu(8, 3);
var addr = this.r[dstidx];
for (var i = 0; i < 8; i++) {
if ((regs >> i) & 1) {
this.clockCount++;
this.checkmem(addr, 4, false);
this.r[i] = this.mem.get32(addr);
addr = norm(addr + 4);
}
}
if (!((regs >> dstidx) & 1)) {
this.r[dstidx] = addr;
}
} else if (chk("11000")) { // STM Rn, {regs}
this.clockCount++;
var regs = getu(0, 8);
if (regs == 0) {
throw "STM with empty regs";
}
var dstidx = getu(8, 3);
if (((regs >> dstidx) & 1) && (regs & ((1 << dstidx) - 1))) {
throw "STM with invalid combination of Rn and regs";
}
var addr = this.r[dstidx];
for (var i = 0; i < 8; i++) {
if ((regs >> i) & 1) {
this.clockCount++;
this.checkmem(addr, 4, true);
this.mem.set32(addr, this.r[i]);
addr = norm(addr + 4);
}
}
this.r[dstidx] = addr;
} else if (chk("10111110")) { // BKPT u8
throw "BKPT " + getu(0, 8) + " is executed";
// SVC u8 is not here but before IF cond GOTO n8 because they have common prefix
} else {
if (get("unsupported_op").value === "halt") {
throw "unsupported instruction";
}
}
} catch(e) {
var inst = code.toString(2);
while (inst.length < 16) inst = "0" + inst;
alert("" + e + "\n\ninstruction address: #" + iaddr.toString(16) +
"\ninstruction: " + inst);
return 0;
}
// dump(this.r);
return 1;
};
};
var CPU_RV32C = function() {
this.r = [];
this.pc = 0;
this.instCount = 0;
this.reset = function() {
for (var i = 0; i < 32; i++)
this.r[i] = 0;
this.pc = 0;
this.instCount = 0;
};
this.reset();
this.mem = new MEM();
this.step = function() {
var iaddr = this.pc;
if (iaddr === 0xDEADBEEE) { // 0xDEADBEEF & 0xfffffffe
// instead of stopping execution at RET
return 0;
}
// if (iaddr % 2 != 0) {
// alert ("unaligned instructon read #" + iaddr.toString(16));
// return 0;
// }
if (!this.mem.check(iaddr, 2) && get("uninitialized_memory").value === "halt") {
alert("read instruction from uninitialized memory #" + iaddr.toString(16));
return 0;
}
var code = this.mem.get16(iaddr);
if ((code & 3) == 3) { // 32-bit instruction
this.pc += 2;
if (this.pc >= 0x100000000) this.pc -= 0x100000000;
if (!this.mem.check(this.pc, 2) && get("uninitialized_memory").value === "halt") {
alert("read instruction from uninitialized memory #" + this.pc.toString(16));
return 0;
}
code = code | (this.mem.get16(this.pc) << 16);
}
// this.code = code;
this.pc += 2;
this.instCount++;
if (this.pc >= 0x100000000) this.pc -= 0x100000000;
// console.log(code.toString(2));
// alert(code.toString(2));
try {
var getu = function(start, n) {
return (code >> start) & ((1 << n) - 1);
};
var getn = function(start, n) {
var res = (code >> start) & ((1 << n) - 1);
if ((res >> (n - 1)) != 0)
return res - (1 << n);
return res;
};
var getus = function(poses) {
var res = 0;
for (var i = 0; i < poses.length; i++) {
res |= getu(poses[i][0], poses[i][1]) << poses[i][2];
}
return res;
};
var getns = function(poses) {
var res = 0;
var maxPos = 1;
for (var i = 0; i < poses.length; i++) {
res |= getu(poses[i][0], poses[i][1]) << poses[i][2];
var pos = poses[i][1] + poses[i][2];
if (pos > maxPos) maxPos = pos;
}
if ((res >> (maxPos - 1)) != 0)
return res - (1 << maxPos);
return res;
};
var norm = function(n) {
var v = 0x100000000;
while (n >= v) n -= v;
while (n < 0) n += v;
return n;
};
var s = function(v) {
if (v >= 0 && (v & 0x80000000)) return v - 0x100000000;
return v;
};
var checkmem = (function(mem) {
return function(addr, n, iswrite) {
// if (addr % n != 0) {
// throw "unaligned memory " + (iswrite ? "write" : "read") +
// " #" + addr.toString(16);
// }
if (!iswrite && !mem.check(addr, n) &&
get("uninitialized_memory").value === "halt") {
throw "read from uninitialized memory #" + addr.toString(16);
}
};
})(this.mem);
var unsupported = function() {
if (get("unsupported_op").value === "halt") {
throw "unsupported instruction";
}
};
var reserved = function() {
throw "reserved";
};
switch(getu(0, 2)) {
case 0x0:
{
var rd = getu(2, 3) + 8;
var rs1 = getu(7, 3) + 8;
var offset = getus([[10,3,3],[6,1,2],[5,1,6]]);
var addr = norm(this.r[rs1] + offset);
switch (getu(13, 3)) {
case 0x0: // c.addi4spn
{
var u = getus([[11,2,4],[7,4,6],[6,1,2],[5,1,3]]);
if (u == 0) reserved();
this.r[rd] = norm(this.r[2] + u);
}
break;
case 0x2: // c.lw
checkmem(addr, 4, false);
this.r[rd] = this.mem.get32(addr);
break;
case 0x6: // c.sw
checkmem(addr, 4, true);
this.mem.set32(addr, this.r[rd]);
break;
default:
unsupported();
break;
}
}
break;
case 0x1:
{
var imm = getns([[12,1,5],[2,5,0]]);
var rd = getu(7, 5);
switch (getu(13, 3)) {
case 0x0: // c.nop / c.addi
this.r[rd] = norm(this.r[rd] + imm);
break;
case 0x01: // c.jal
{
var offset = getns([[12,1,11],[11,1,4],[9,2,8],[8,1,10],[7,1,6],[6,1,7],[3,3,1],[2,1,5]]);
this.r[1] = this.pc;
this.pc = norm(this.pc - 2 + offset);
}
break;
case 0x02: // c.li
this.r[rd] = imm;
break;
case 0x03:
if (rd == 2) { // c.addi16sp
var offset = getns([[12,1,9],[6,1,4],[5,1,6],[3,2,7],[2,1,5]]);
this.r[2] = norm(this.r[2] + offset);
} else { // c.lui
this.r[rd] = norm((imm << 12) & 0xffffffff);
}
break;
case 0x04:
{
var rdd = getu(7, 3) + 8;
switch (getu(10, 2)) {
case 0x0: // c.srli
if (imm & 0x20) reserved();
this.r[rdd] = norm(this.r[rdd] >>> imm);
break;
case 0x1: // c.srai
if (imm & 0x20) reserved();
this.r[rdd] = norm(this.r[rdd] >> imm);
break;
case 0x2: // c.andi
this.r[rdd] = norm(this.r[rdd] & imm);
break;
case 0x3:
if (getu(12, 1) == 0) {
var rs2d = getu(2, 3) + 8;
switch (getu(5, 2)) {
case 0x0: // c.sub
this.r[rdd] = norm(this.r[rdd] - this.r[rs2d]);
break;
case 0x1: // c.xor
this.r[rdd] = norm(this.r[rdd] ^ this.r[rs2d]);
break;
case 0x2: // c.or
this.r[rdd] = norm(this.r[rdd] | this.r[rs2d]);
break;
case 0x3: // c.and
this.r[rdd] = norm(this.r[rdd] & this.r[rs2d]);
break;
default:
unsupported();
break;
}
} else {
reserved();
}
break;
default:
unsupported();
break;
}
}
break;
case 0x05: // c.j
{
var offset = getns([[12,1,11],[11,1,4],[9,2,8],[8,1,10],[7,1,6],[6,1,7],[3,3,1],[2,1,5]]);
this.pc = norm(this,pc - 2 + offset);
}
break;
case 0x06: case 0x07:
{
var rsd = getu(7, 3) + 8;
var offset = getns([[12,1,8],[10,2,3],[5,2,6],[3,2,1],[2,1,5]]);
if (getu(13, 3) === 0x06) { // c.beqz
if (this.r[rsd] === 0) {
this.pc = norm(this.pc - 2 + offset);
}
} else { // c.bnez
if (this.r[rsd] !== 0) {
this.pc = norm(this.pc - 2 + offset);
}
}
}
break;
default:
unsupported();
break;
}
}
break;
case 0x2:
switch (getu(13, 3)) {
case 0x0: // c.slli
{
if (getu(12, 1) != 0) reserved();
var rd = getu(7, 5);
var imm = getu(2, 5);
this.r[rd] = norm(this.r[rd] << imm);
}
break;
case 0x2: // c.lwsp
{
var offset = getus([[12,1,5],[4,3,2],[2,2,6]]);
var rd = getu(7, 5);
var addr = norm(this.r[2] + offset);
checkmem(addr, 4, false);
this.r[rd] = this.mem.get32(addr);
}
break;
case 0x4:
{
var rd = getu(7, 5);
var rs = getu(2, 5);
if (getu(12, 1) === 0) {
if (rs === 0) { // c.jr
if (rd === 0) reserved();
this.pc = norm(this.r[rd] & 0xfffffffe);
} else { // c.mv
this.r[rd] = this.r[rs];
}
} else {
if (rs === 0) {
if (rd === 0) { // c.ebreak
throw "C.EBREAK is executed";
} else { // c.jalr
this.r[1] = this.pc;
this.pc = norm(this.r[rd] & 0xfffffffe);
}
} else { // c.add
this.r[rd] = norm(this.r[rd] + this.r[rs]);
}
}
}
break;
case 0x6: // c.swsp
{
var offset = getus([[9,4,2],[7,2,6]]);
var rs = getu(2, 5);
var addr = norm(this.r[2] + offset);
checkmem(addr, 4, true);
this.mem.set32(addr, this.r[rs]);
}
break;
default:
unsupported();
break;
}
break;
case 0x3:
{
var rd = getu(7, 5);
var funct3 = getu(12, 3);
var rs1 = getu(15, 5);
var rs2 = getu(20, 5);
switch (getu(0, 7)) {
case 0x37: // lui
this.r[rd] = norm(code & 0xfffff000);
break;
case 0x17: // auipc
this.r[rd] = norm(this.pc - 4 + (code & 0xfffff000));
break;
case 0x6F: //jal
{
var offset = getns([[31,1,20],[21,10,1],[20,1,11],[12,8,12]]);
this.r[rd] = this.pc;
this.pc = norm(this.pc - 4 + offset);
}
break;
case 0x67:
if (funct3 === 0) { // jalr
this.r[rd] = this.pc;
this.pc = norm((this.r[rs1] + getn(20, 12)) & 0xfffffffe);
} else {
unsupported();
}
break;
case 0x63:
{
var offset = getns([[31,1,12],[25,6,5],[8,4,1],[7,1,11]]);
var dest = norm(this.pc - 4 + offset);
switch (funct3) {
case 0: // beq
if (this.r[rs1] === this.r[rs2]) this.pc = dest;
break;
case 1: // bne
if (this.r[rs1] !== this.r[rs2]) this.pc = dest;
break;
case 4: // blt
if (s(this.r[rs1]) < s(this.r[rs2])) this.pc = dest;
break;
case 5: // bge
if (s(this.r[rs1]) >= s(this.r[rs2])) this.pc = dest;
break;
case 6: // bltu
if (this.r[rs1] < this.r[rs2]) this.pc = dest;
break;
case 7: // bgeu
if (this.r[rs1] >= this.r[rs2]) this.pc = dest;