-
Notifications
You must be signed in to change notification settings - Fork 61
/
inst_src.js
1612 lines (1358 loc) · 64.5 KB
/
inst_src.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
function check_HTIF() {
var toHostVal = RISCV.priv_reg[PCR["CSR_TOHOST"]["num"]];
// check toHost, output to JS console, clear it
if (toHostVal.high_ != 0 || toHostVal.low_ != 0){
//console.log("Output on toHost:");
//console.log(stringLongHex(RISCV.priv_reg[PCR["CSR_TOHOST"]["num"]]));
// check device / cmd
var device = (toHostVal.getHighBits() >> 24) & 0xFF;
var cmd = (toHostVal.getHighBits() >> 16) & 0xFF;
var payload = new Long(toHostVal.getLowBits(), toHostVal.getHighBits() & 0xFFFF);
if (device == 0x1) {
// terminal, but ignore the enumeration
if (cmd == 0x0) {
// this is read
// we don't actually handle them here
} else if (cmd == 0x1) {
// this is a write
postMessage({"type": "t", "d": payload.getLowBits() & 0xFF});
} else if (cmd == 0xFF) {
// write "bcd" (block character device) to pbuf here
var addr = payload.shiftRightUnsigned(8); // hardcoded from log2(MAX_COMMANDS [256])
var what = payload.getLowBits() & 0xFF;
if (what == 0xFF) {
var toWrite = "bcd";
}
if (what == 0x0) {
var toWrite = "read";
}
if (what == 0x1) {
var toWrite = "write";
}
for (var i = 0; i < toWrite.length; i++) {
RISCV.memory[(addr.getLowBits() + i) >> 2] &= ~((0xFF) << ((i & 0x3) << 3));
RISCV.memory[(addr.getLowBits() + i) >> 2] |= (toWrite.charCodeAt(i) & 0xFF) << ((i & 0x3) << 3);
}
// null term if null term would go in new word
RISCV.memory[(addr.getLowBits() + toWrite.length) >> 2] &= ~((0xFF) << (((addr.getLowBits() + toWrite.length) & 0x3) << 3));
RISCV.priv_reg[PCR["CSR_FROMHOST"]["num"]] = Long.ONE;
} else {
throw new RISCVError("Other term features not yet implemented " + stringIntHex(cmd));
}
} else if (cmd == 0xFF) {
// try to override enumeration
//if (device == 0x0) {
// // need to write "bcd" to pbuf here
var addr = payload.shiftRightUnsigned(8); // hardcoded from log2(MAX_COMMANDS [256])
var what = payload.getLowBits() & 0xFF;
if (what == 0xFF) {
var toWrite = "";
}
/* for (var i = 0; i < toWrite.length; i++) {
if (((addr.getLowBits() + i) & 0x3) == 0x0) {
RISCV.memory[(addr.getLowBits() + i) >> 2] = 0x0;
}
RISCV.memory[(addr.getLowBits() + i) >> 2] |= (toWrite.charCodeAt(i) & 0xFF) << ((i & 0x3) << 3);
}
// null term if null term would go in new word
if (((addr.getLowBits() + toWrite.length) & 0x3) == 0x0) {
RISCV.memory[(addr.getLowBits() + toWrite.length) >> 2] &= 0xFFFFFF00;
}
*/
for (var i = 0; i < toWrite.length; i++) {
RISCV.memory[(addr.getLowBits() + i) >> 2] &= ~((0xFF) << ((i & 0x3) << 3));
RISCV.memory[(addr.getLowBits() + i) >> 2] |= (toWrite.charCodeAt(i) & 0xFF) << ((i & 0x3) << 3);
}
// null term if null term would go in new word
RISCV.memory[(addr.getLowBits() + toWrite.length) >> 2] &= ~((0xFF) << (((addr.getLowBits() + toWrite.length) & 0x3) << 3));
/* for (var i = 0; i < toWrite.length; i++) {
RISCV.memory[addr.getLowBits() + i] = toWrite.charCodeAt(i) & 0xFF;
}
RISCV.memory[addr.getLowBits() + toWrite.length] = 0x00;*/
RISCV.priv_reg[PCR["CSR_FROMHOST"]["num"]] = Long.ONE;
} else {
// unknown device, crash
console.log("device " + stringIntHex(device));
console.log("cmd " + stringIntHex(cmd));
console.log("payload " + stringIntHex(payload));
console.log("current PC " + stringIntHex(RISCV.pc));
console.log("last PC " + stringIntHex(RISCV.oldpc));
throw new RISCVError("unknown device/command combo");
}
RISCV.priv_reg[PCR["CSR_TOHOST"]["num"]] = Long.ZERO;
}
}
function signExtLT32_64(quantity) {
return new Long(quantity|0, quantity >> 31);
}
// sign extend a < 32 bit number to 64 bits based on bit
function signExtLT32_64_v(quantity, bit) {
// bits numbered 31, 30, .... 2, 1, 0
bitval = ((quantity|0) >> bit) & 0x00000001;
if (bitval === 0) {
return new Long(quantity|0, 0x00000000);
} else if (bitval === 1) {
mask = 0x80000000;
mask = mask >> (31-bit)
return new Long((quantity | mask), 0xFFFFFFFF);
} else {
throw new RISCVError("ERR in signext");
}
}
// Takes instruction obj and CPU obj as args, performs computation on given CPU
function runInstruction(raw) { //, RISCV) {
// force x0 (zero) to zero
// RISCV.gen_reg[0] = Long.ZERO;
var op = inst.get_opcode();
switch(op) {
// I-TYPE, opcode: 0b0010011
case 0x13:
var funct3 = inst.get_funct3();
switch(funct3) {
// ADDI
case 0x0:
RISCV.gen_reg[inst.get_rd()] = RISCV.gen_reg[inst.get_rs1()].add(signExtLT32_64(inst.get_I_imm()));
RISCV.pc += 4;
break;
// SLLI
case 0x1:
RISCV.gen_reg[inst.get_rd()] = (RISCV.gen_reg[inst.get_rs1()]).shiftLeft(inst.get_I_imm() & 0x003F);
RISCV.pc += 4;
break;
// SLTI
case 0x2:
if ((RISCV.gen_reg[inst.get_rs1()]).lessThan(signExtLT32_64(inst.get_I_imm()))) {
RISCV.gen_reg[inst.get_rd()] = Long.ONE;
} else {
RISCV.gen_reg[inst.get_rd()] = Long.ZERO;
}
RISCV.pc += 4;
break;
// SLTIU, need to check signExt here
case 0x3:
if (long_less_than_unsigned(RISCV.gen_reg[inst.get_rs1()], signExtLT32_64(inst.get_I_imm()))) {
RISCV.gen_reg[inst.get_rd()] = Long.ONE;
} else {
RISCV.gen_reg[inst.get_rd()] = Long.ZERO;
}
RISCV.pc += 4;
break;
// XORI
case 0x4:
RISCV.gen_reg[inst.get_rd()] = (RISCV.gen_reg[inst.get_rs1()]).xor(signExtLT32_64(inst.get_I_imm()));
RISCV.pc += 4;
break;
// SRLI and SRAI
case 0x5:
var aldiff = (inst.get_I_imm() >>> 6);
if (aldiff === 0) {
// SRLI
RISCV.gen_reg[inst.get_rd()] = (RISCV.gen_reg[inst.get_rs1()]).shiftRightUnsigned(inst.get_I_imm() & 0x003F);
} else {
// SRAI
RISCV.gen_reg[inst.get_rd()] = (RISCV.gen_reg[inst.get_rs1()]).shiftRight(inst.get_I_imm() & 0x003F);
}
RISCV.pc += 4;
break;
// ORI
case 0x6:
RISCV.gen_reg[inst.get_rd()] = (RISCV.gen_reg[inst.get_rs1()]).or(signExtLT32_64(inst.get_I_imm()));
RISCV.pc += 4;
break;
// ANDI
case 0x7:
RISCV.gen_reg[inst.get_rd()] = (RISCV.gen_reg[inst.get_rs1()]).and(signExtLT32_64(inst.get_I_imm()));
RISCV.pc += 4;
break;
default:
throw new RISCVTrap("Illegal Instruction");
break;
}
break;
// R-TYPE, opcode: 0b0110011
case 0x33:
var funct10 = (inst.get_funct7() << 3) | inst.get_funct3();
switch(funct10) {
// ADD
case 0x0:
RISCV.gen_reg[inst.get_rd()] = (RISCV.gen_reg[inst.get_rs1()]).add(RISCV.gen_reg[inst.get_rs2()]);
RISCV.pc += 4;
break;
// SUB
case 0x100:
RISCV.gen_reg[inst.get_rd()] = (RISCV.gen_reg[inst.get_rs1()]).subtract(RISCV.gen_reg[inst.get_rs2()]);
RISCV.pc += 4;
break;
// SLL
case 0x1:
RISCV.gen_reg[inst.get_rd()] = (RISCV.gen_reg[inst.get_rs1()]).shiftLeft((RISCV.gen_reg[inst.get_rs2()]).getLowBits() & 0x3F);
RISCV.pc += 4;
break;
// SLT
case 0x2:
if ((RISCV.gen_reg[inst.get_rs1()]).lessThan(RISCV.gen_reg[inst.get_rs2()])) {
RISCV.gen_reg[inst.get_rd()] = Long.ONE;
} else {
RISCV.gen_reg[inst.get_rd()] = Long.ZERO;
}
RISCV.pc += 4;
break;
// SLTU
case 0x3:
if (long_less_than_unsigned(RISCV.gen_reg[inst.get_rs1()], RISCV.gen_reg[inst.get_rs2()])) {
RISCV.gen_reg[inst.get_rd()] = Long.ONE;
} else {
RISCV.gen_reg[inst.get_rd()] = Long.ZERO;
}
RISCV.pc += 4;
break;
// XOR
case 0x4:
RISCV.gen_reg[inst.get_rd()] = (RISCV.gen_reg[inst.get_rs1()]).xor(RISCV.gen_reg[inst.get_rs2()]);
RISCV.pc += 4;
break;
// SRL
case 0x5:
RISCV.gen_reg[inst.get_rd()] = (RISCV.gen_reg[inst.get_rs1()]).shiftRightUnsigned((RISCV.gen_reg[inst.get_rs2()]).getLowBits() & 0x3F);
RISCV.pc += 4;
break;
// SRA
case 0x105:
RISCV.gen_reg[inst.get_rd()] = (RISCV.gen_reg[inst.get_rs1()]).shiftRight((RISCV.gen_reg[inst.get_rs2()]).getLowBits() & 0x3F);
RISCV.pc += 4;
break;
// OR
case 0x6:
RISCV.gen_reg[inst.get_rd()] = (RISCV.gen_reg[inst.get_rs1()]).or(RISCV.gen_reg[inst.get_rs2()]);
RISCV.pc += 4;
break;
// AND
case 0x7:
RISCV.gen_reg[inst.get_rd()] = (RISCV.gen_reg[inst.get_rs1()]).and(RISCV.gen_reg[inst.get_rs2()]);
RISCV.pc += 4;
break;
// MUL
case 0x8:
RISCV.gen_reg[inst.get_rd()] = (RISCV.gen_reg[inst.get_rs1()]).multiply(RISCV.gen_reg[inst.get_rs2()]);
RISCV.pc += 4;
break;
// MULH
case 0x9:
// plan: long -> string -> bignum -> do the mult
// then divide by 2^64 (equiv to right shift by 64 bits)
// then bignum -> string -> Long.fromString()
var big1 = BigInteger(RISCV.gen_reg[inst.get_rs1()].toString(10));
var big2 = BigInteger(RISCV.gen_reg[inst.get_rs2()].toString(10));
var bigres = big1.multiply(big2);
var bigdiv = BigInteger("18446744073709551616"); // 2^64
var bigresf = bigres.divide(bigdiv);
// need to fix one-off error for negative nums when doing this shift
if (bigres.isNegative()) {
bigresf = bigresf.subtract(BigInteger("1"));
}
bigresf = bigresf.toString(10);
var result = Long.fromString(bigresf, 10);
RISCV.gen_reg[inst.get_rd()] = result;
RISCV.pc += 4;
break;
// MULHSU
case 0xA:
var l1 = RISCV.gen_reg[inst.get_rs1()];
var l2 = RISCV.gen_reg[inst.get_rs2()];
var l2neg = (l2.getHighBits() & 0x80000000) != 0;
var big1 = BigInteger(l1);
if (l2neg) {
l2 = new Long(l2.getLowBits(), l2.getHighBits() & 0x7FFFFFFF);
var big2 = BigInteger(l2);
big2 = big2.add(BigInteger("9223372036854775808")); // 2^63
} else {
var big2 = BigInteger(l2);
}
var bigres = big1.multiply(big2);
var bigdiv = BigInteger("18446744073709551616"); // 2^64
var bigresf = bigres.divide(bigdiv);
// need to fix one-off error for negative nums when doing this shift
if (bigres.isNegative()) {
bigresf = bigresf.subtract(BigInteger("1"));
}
// now we have the upper 64 bits of result, signed
bigresf = bigresf.toString(10);
RISCV.gen_reg[inst.get_rd()] = Long.fromString(bigresf, 10);
RISCV.pc += 4;
break;
// MULHU
case 0xB:
// plan: long -determine/fix signs -> string -> bignum -> do the mult
// then divide by 2^64 (equiv to right shift by 64 bits)
// then bignum -> string -> Long.fromString()
var l1 = RISCV.gen_reg[inst.get_rs1()];
var l2 = RISCV.gen_reg[inst.get_rs2()];
var l1neg = (l1.getHighBits() & 0x80000000) != 0;
var l2neg = (l2.getHighBits() & 0x80000000) != 0;
if (l1neg) {
l1 = new Long(l1.getLowBits(), l1.getHighBits() & 0x7FFFFFFF);
var big1 = BigInteger(l1);
big1 = big1.add(BigInteger("9223372036854775808"));
} else {
var big1 = BigInteger(l1);
}
if (l2neg) {
l2 = new Long(l2.getLowBits(), l2.getHighBits() & 0x7FFFFFFF);
var big2 = BigInteger(l2);
big2 = big2.add(BigInteger("9223372036854775808")); // 2^63
} else {
var big2 = BigInteger(l2);
}
var bigres = big1.multiply(big2);
var bigdiv = BigInteger("18446744073709551616"); // 2^64
var bigresf = bigres.divide(bigdiv);
var bigsub = BigInteger("9223372036854775808"); // 2^63
if (bigresf.compare(bigsub) >= 0) {
// need to subtract bigsub, manually set MSB
bigresf = bigresf.subtract(bigsub);
bigresf = bigresf.toString(10)
var res = Long.fromString(bigresf, 10);
res = new Long(res.getLowBits(), res.getHighBits()|0x80000000);
} else {
bigresf = bigresf.toString(10);
var res = Long.fromString(bigresf, 10);
}
RISCV.gen_reg[inst.get_rd()] = res;
RISCV.pc += 4;
break;
// DIV
case 0xC:
if (RISCV.gen_reg[inst.get_rs2()].isZero()) {
// divide by zero, result is all ones
RISCV.gen_reg[inst.get_rd()] = new Long(0xFFFFFFFF, 0xFFFFFFFF);
} else if (RISCV.gen_reg[inst.get_rs1()].equals(new Long(0x0, 0x80000000)) && RISCV.gen_reg[inst.get_rs1()].equals(new Long(0xFFFFFFFF, 0xFFFFFFFF))) {
// divide most negative num by -1 -> signed overflow
// set result to dividend
RISCV.gen_reg[inst.get_rd()] = RISCV.gen_reg[inst.get_rs1()];
} else {
// actual division
RISCV.gen_reg[inst.get_rd()] = RISCV.gen_reg[inst.get_rs1()].div(RISCV.gen_reg[inst.get_rs2()]);
}
RISCV.pc += 4;
break;
// DIVU
case 0xD:
var l1 = RISCV.gen_reg[inst.get_rs1()];
var l2 = RISCV.gen_reg[inst.get_rs2()];
if (l2.isZero()) {
//div by zero
RISCV.gen_reg[inst.get_rd()] = new Long(0xFFFFFFFF, 0xFFFFFFFF);
RISCV.pc += 4;
break;
}
var l1neg = (l1.getHighBits() & 0x80000000) != 0;
var l2neg = (l2.getHighBits() & 0x80000000) != 0;
if (l1neg) {
l1 = new Long(l1.getLowBits(), l1.getHighBits() & 0x7FFFFFFF);
var big1 = BigInteger(l1);
big1 = big1.add(BigInteger("9223372036854775808"));
} else {
var big1 = BigInteger(l1);
}
if (l2neg) {
l2 = new Long(l2.getLowBits(), l2.getHighBits() & 0x7FFFFFFF);
var big2 = BigInteger(l2);
big2 = big2.add(BigInteger("9223372036854775808")); // 2^63
} else {
var big2 = BigInteger(l2);
}
var bigresf = big1.divide(big2);
var bigsub = BigInteger("9223372036854775808"); // 2^63
if (bigresf.compare(bigsub) >= 0) {
// need to subtract bigsub, manually set MSB
bigresf = bigresf.subtract(bigsub);
bigresf = bigresf.toString(10)
var res = Long.fromString(bigresf, 10);
res = new Long(res.getLowBits(), res.getHighBits()|0x80000000);
} else {
bigresf = bigresf.toString(10);
var res = Long.fromString(bigresf, 10);
}
RISCV.gen_reg[inst.get_rd()] = res;
RISCV.pc += 4;
break;
// REM
case 0xE:
if (RISCV.gen_reg[inst.get_rs2()].isZero()) {
// rem (divide) by zero, result is dividend
RISCV.gen_reg[inst.get_rd()] = RISCV.gen_reg[inst.get_rs1()];
} else if (RISCV.gen_reg[inst.get_rs1()].equals(new Long(0x0, 0x80000000)) && RISCV.gen_reg[inst.get_rs1()].equals(new Long(0xFFFFFFFF, 0xFFFFFFFF))) {
// rem (divide) most negative num by -1 -> signed overflow
// set result to dividend
RISCV.gen_reg[inst.get_rd()] = Long.ZERO;
} else {
// actual rem
RISCV.gen_reg[inst.get_rd()] = RISCV.gen_reg[inst.get_rs1()].modulo(RISCV.gen_reg[inst.get_rs2()]);
}
RISCV.pc += 4;
break;
// REMU
case 0xF:
var l1 = RISCV.gen_reg[inst.get_rs1()];
var l2 = RISCV.gen_reg[inst.get_rs2()];
if (l2.isZero()) {
//div by zero
RISCV.gen_reg[inst.get_rd()] = l1;
RISCV.pc += 4;
break;
}
var l1neg = (l1.getHighBits() & 0x80000000) != 0;
var l2neg = (l2.getHighBits() & 0x80000000) != 0;
if (l1neg) {
l1 = new Long(l1.getLowBits(), l1.getHighBits() & 0x7FFFFFFF);
var big1 = BigInteger(l1);
big1 = big1.add(BigInteger("9223372036854775808"));
} else {
var big1 = BigInteger(l1);
}
if (l2neg) {
l2 = new Long(l2.getLowBits(), l2.getHighBits() & 0x7FFFFFFF);
var big2 = BigInteger(l2);
big2 = big2.add(BigInteger("9223372036854775808")); // 2^63
} else {
var big2 = BigInteger(l2);
}
var bigresf = big1.remainder(big2);
var bigsub = BigInteger("9223372036854775808"); // 2^63
if (bigresf.compare(bigsub) >= 0) {
// need to subtract bigsub, manually set MSB
bigresf = bigresf.subtract(bigsub);
bigresf = bigresf.toString(10)
var res = Long.fromString(bigresf, 10);
res = new Long(res.getLowBits(), res.getHighBits()|0x80000000);
} else {
bigresf = bigresf.toString(10);
var res = Long.fromString(bigresf, 10);
}
RISCV.gen_reg[inst.get_rd()] = res;
RISCV.pc += 4;
break;
default:
throw new RISCVTrap("Illegal Instruction");
break;
}
break;
// L-TYPE (LUI) - opcode: 0b0110111
case 0x37:
RISCV.gen_reg[inst.get_rd()] = signExtLT32_64(inst.get_U_imm());
RISCV.pc += 4;
break;
// L-TYPE (AUIPC) - opcode: 0b0010111
case 0x17:
RISCV.gen_reg[inst.get_rd()] = signExtLT32_64(inst.get_U_imm() + (RISCV.pc & 0xFFFFF000));
if ((RISCV.gen_reg[inst.get_rd()].getLowBitsUnsigned() & 0xFF000000) == 0x55000000) {
RISCV.gen_reg[inst.get_rd()] = new Long(RISCV.gen_reg[inst.get_rd()].getLowBitsUnsigned(), 0x155);
}
RISCV.pc += 4;
break;
// J-TYPE (JAL) - opcode: 0b1101111
case 0x6F:
RISCV.gen_reg[inst.get_rd()] = signExtLT32_64(RISCV.pc + 4);
RISCV.pc = (RISCV.pc|0) + inst.get_J_imm();
break;
// B-TYPE (Branches) - opcode: 0b1100011
case 0x63:
var funct3 = inst.get_funct3();
switch(funct3) {
// BEQ
case 0x0:
if ((RISCV.gen_reg[inst.get_rs1()]).equals(RISCV.gen_reg[inst.get_rs2()])) {
RISCV.pc = (RISCV.pc|0) + inst.get_B_imm();
} else {
RISCV.pc += 4;
}
break;
// BNE
case 0x1:
if ((RISCV.gen_reg[inst.get_rs1()]).notEquals(RISCV.gen_reg[inst.get_rs2()])) {
RISCV.pc = (RISCV.pc|0) + inst.get_B_imm();
} else {
RISCV.pc += 4;
}
break;
// BLT
case 0x4:
if ((RISCV.gen_reg[inst.get_rs1()]).lessThan(RISCV.gen_reg[inst.get_rs2()])) {
RISCV.pc = (RISCV.pc|0) + inst.get_B_imm();
} else {
RISCV.pc += 4;
}
break;
// BGE
case 0x5:
if ((RISCV.gen_reg[inst.get_rs1()]).greaterThanOrEqual(RISCV.gen_reg[inst.get_rs2()])) {
RISCV.pc = (RISCV.pc|0) + inst.get_B_imm();
} else {
RISCV.pc += 4;
}
break;
// BLTU
case 0x6:
if (long_less_than_unsigned(RISCV.gen_reg[inst.get_rs1()], RISCV.gen_reg[inst.get_rs2()])) {
RISCV.pc = (RISCV.pc|0) + inst.get_B_imm();
} else {
RISCV.pc += 4;
}
break;
// BGEU
case 0x7:
if (!long_less_than_unsigned(RISCV.gen_reg[inst.get_rs1()], RISCV.gen_reg[inst.get_rs2()])) {
RISCV.pc = (RISCV.pc|0) + inst.get_B_imm();
} else {
RISCV.pc += 4;
}
break;
default:
throw new RISCVTrap("Illegal Instruction");
break;
}
break;
// I-TYPES (JALR)
case 0x67:
var funct3 = inst.get_funct3();
if (funct3 == 0x0) {
RISCV.gen_reg[inst.get_rd()] = signExtLT32_64(RISCV.pc + 4);
RISCV.pc = inst.get_I_imm() + (RISCV.gen_reg[inst.get_rs1()].getLowBits()|0);
} else {
throw new RISCVTrap("Illegal Instruction");
}
break;
// Loads
case 0x3:
var funct3 = inst.get_funct3();
switch(funct3) {
// LB
case 0x0:
var addr = (RISCV.gen_reg[inst.get_rs1()]).add(Long.fromNumber2(inst.get_I_imm()|0));
var fetch = RISCV.load_byte_from_mem(addr);
if (RISCV.excpTrigg) {
return;
}
RISCV.gen_reg[inst.get_rd()] = signExtLT32_64_v(fetch, 7);
RISCV.pc += 4;
break;
// LH
case 0x1:
var addr = (RISCV.gen_reg[inst.get_rs1()]).add(Long.fromNumber2(inst.get_I_imm()|0));
var fetch = RISCV.load_half_from_mem(addr);
if (RISCV.excpTrigg) {
return;
}
RISCV.gen_reg[inst.get_rd()] = signExtLT32_64_v(fetch, 15);
RISCV.pc += 4;
break;
// LW
case 0x2:
var addr = (RISCV.gen_reg[inst.get_rs1()]).add(Long.fromNumber2(inst.get_I_imm()|0));
var fetch = RISCV.load_word_from_mem(addr);
if (RISCV.excpTrigg) {
return;
}
RISCV.gen_reg[inst.get_rd()] = signExtLT32_64(fetch);
RISCV.pc += 4;
break;
// LD
case 0x3:
var addr = (RISCV.gen_reg[inst.get_rs1()]).add(Long.fromNumber2(inst.get_I_imm()|0));
var fetch = RISCV.load_double_from_mem(addr)
if (RISCV.excpTrigg) {
return;
}
// unlike load_half/byte/word_from_mem, double returns Long
RISCV.gen_reg[inst.get_rd()] = fetch;
RISCV.pc += 4;
break;
// LBU
case 0x4:
var addr = (RISCV.gen_reg[inst.get_rs1()]).add(Long.fromNumber2(inst.get_I_imm()|0));
var fetch = RISCV.load_byte_from_mem(addr);
if (RISCV.excpTrigg) {
return;
}
RISCV.gen_reg[inst.get_rd()] = new Long(fetch & 0x000000FF, 0x0);
RISCV.pc += 4;
break;
// LHU
case 0x5:
var addr = (RISCV.gen_reg[inst.get_rs1()]).add(Long.fromNumber2(inst.get_I_imm()|0));
var fetch = RISCV.load_half_from_mem(addr);
if (RISCV.excpTrigg) {
return;
}
RISCV.gen_reg[inst.get_rd()] = new Long(fetch & 0x0000FFFF, 0x0);
RISCV.pc += 4;
break;
// LWU
case 0x6:
var addr = (RISCV.gen_reg[inst.get_rs1()]).add(Long.fromNumber2(inst.get_I_imm()|0));
var fetch = RISCV.load_word_from_mem(addr);
if (RISCV.excpTrigg) {
return;
}
RISCV.gen_reg[inst.get_rd()] = new Long(fetch, 0x0);
RISCV.pc += 4;
break;
default:
throw new RISCVTrap("Illegal Instruction");
break;
}
break;
// Stores
case 0x23:
var funct3 = inst.get_funct3();
switch(funct3) {
// SB
case 0x0:
var addr = (RISCV.gen_reg[inst.get_rs1()]).add(Long.fromNumber2(inst.get_S_imm()|0));
RISCV.store_byte_to_mem(addr, RISCV.gen_reg[inst.get_rs2()].getLowBits());
if (RISCV.excpTrigg) {
return;
}
RISCV.pc += 4;
break;
// SH
case 0x1:
var addr = (RISCV.gen_reg[inst.get_rs1()]).add(Long.fromNumber2(inst.get_S_imm()|0));
RISCV.store_half_to_mem(addr, RISCV.gen_reg[inst.get_rs2()].getLowBits());
if (RISCV.excpTrigg) {
return;
}
RISCV.pc += 4;
break;
// SW
case 0x2:
var addr = (RISCV.gen_reg[inst.get_rs1()]).add(Long.fromNumber2(inst.get_S_imm()|0));
RISCV.store_word_to_mem(addr, RISCV.gen_reg[inst.get_rs2()].getLowBits());
if (RISCV.excpTrigg) {
return;
}
RISCV.pc += 4;
break;
// SD
case 0x3:
var addr = (RISCV.gen_reg[inst.get_rs1()]).add(Long.fromNumber2(inst.get_S_imm()|0));
RISCV.store_double_to_mem(addr, RISCV.gen_reg[inst.get_rs2()]);
if (RISCV.excpTrigg) {
return;
}
RISCV.pc += 4;
break;
default:
throw new RISCVTrap("Illegal Instruction");
break;
}
break;
// FENCE instructions - NOPS for this imp
case 0x0F:
var funct3 = inst.get_funct3();
if (funct3 == 0x1) {
// FENCE.I is no-op in this implementation
RISCV.pc += 4;
} else if (funct3 == 0x0) {
// FENCE is no-op in this implementation
RISCV.pc += 4;
} else {
throw new RISCVTrap("Illegal Instruction");
}
break;
// R-TYPES (continued): System instructions
case 0x73:
var superfunct = inst.get_funct3() | inst.get_rs2() << 3 | inst.get_funct7() << 8;
switch(superfunct) {
// SCALL
case 0x0:
RISCV.excpTrigg = new RISCVTrap("System Call");
return;
break;
// SBREAK
case 0x8:
RISCV.excpTrigg = new RISCVTrap("Breakpoint");
return;
RISCV.pc += 4;
break;
// SRET
case 0x4000:
// [todo] - need to check for supervisor?
// first, confirm that we're in supervisor mode
// if ((RISCV.priv_reg[PCR["CSR_STATUS"]["num"]] & SR["SR_S"]) == 0) {
// throw new RISCVTrap("Privileged Instruction");
// }
// do eret stuff here
var oldsr = RISCV.priv_reg[PCR["CSR_STATUS"]["num"]];
// set SR[S] = SR[PS], don't touch SR[PS]
if ((oldsr & SR["SR_PS"]) != 0) {
// PS is set
oldsr = oldsr | SR["SR_S"];
} else {
oldsr = oldsr & (~SR["SR_S"]);
}
// set EI
if ((oldsr & SR["SR_PEI"]) != 0) {
oldsr = oldsr | SR["SR_EI"];
} else {
oldsr = oldsr & (~SR["SR_EI"]);
}
// store updated SR back:
RISCV.priv_reg[PCR["CSR_STATUS"]["num"]] = oldsr;
// set pc to value stored in EPC
RISCV.pc = RISCV.priv_reg[PCR["CSR_EPC"]["num"]].getLowBits();
// RISCV.pc += 4;
break;
// RDCYCLE
case 0x6002:
RISCV.gen_reg[inst.get_rd()] = new Long(RISCV.priv_reg[PCR["CSR_CYCLE"]["num"]], 0x0);
RISCV.pc += 4;
break;
// RDTIME
case 0x600A:
// places #ms since cpu boot in rd. against spec
// but the best we can reasonably do with js
var nowtime = new Date();
nowtime = nowtime.getTime();
// need to be careful here: the subtraction needs to be
// done as a float to cut down to reasonable number of
// bits, then or with zero to get close by int value
var result = nowtime - RISCV.priv_reg[PCR["CSR_TIME"]["num"]].toNumber();
RISCV.gen_reg[inst.get_rd()] = Long.fromNumber(result);
RISCV.pc += 4;
break;
// RDINSTRET
case 0x6012:
// for our purposes, this is the same as RDCYCLE:
RISCV.gen_reg[inst.get_rd()] = RISCV.priv_reg[PCR["CSR_INSTRET"]["num"]];
RISCV.pc += 4;
break;
default:
// if none of the above are triggered, try handling as CSR inst
var funct3 = inst.get_funct3();
//var rd = RISCV.gen_reg[inst.get_rd()];
var rs1 = RISCV.gen_reg[inst.get_rs1()];
switch(funct3) {
// [todo] - currently does not perform permission check
// CSRRW
case 0x1:
var timm = inst.get_CSR_imm();
if (timm == 0x3 || timm == 0x2 || timm == 0x1) {
RISCV.excpTrigg = new RISCVTrap("Floating-Point Disabled");
return;
}
var temp = RISCV.priv_reg[inst.get_CSR_imm()];
if (typeof temp === "number") {
RISCV.gen_reg[inst.get_rd()] = new Long(temp, 0x0);
temp = rs1.getLowBitsUnsigned();
} else {
//temp is a long
RISCV.gen_reg[inst.get_rd()] = temp;
temp = rs1;
}
RISCV.set_pcr(inst.get_CSR_imm(), temp);
if (inst.get_CSR_imm() == PCR["CSR_FATC"]["num"]) {
TLB = new Uint32Array(TLBSIZE);
ITLB = new Uint32Array(ITLBSIZE);
ITLBstuff = new Uint32Array(ITLBSIZE);
// console.log("flushing TLB from CSRRW");
// console.log("Current ASID is " + stringIntHex(RISCV.priv_reg[PCR["CSR_ASID"]["num"]]));
}
RISCV.pc += 4;
// if toHost is written, do stuff:
check_HTIF();
break;
// CSRRS
case 0x2:
var timm = inst.get_CSR_imm();
if ((timm == 0x3 || timm == 0x2 || timm == 0x1) && (inst.get_rs1() == 0x0)) {
RISCV.excpTrigg = new RISCVTrap("Floating-Point Disabled");
return;
}
var temp = RISCV.priv_reg[inst.get_CSR_imm()];
if (typeof temp === "number") {
RISCV.gen_reg[inst.get_rd()] = new Long(temp, 0x0);
temp = temp | rs1.getLowBitsUnsigned();
} else {
//temp is a long
RISCV.gen_reg[inst.get_rd()] = temp;
temp = temp.or(rs1);
}
RISCV.set_pcr(inst.get_CSR_imm(), temp);
RISCV.pc += 4;
// if toHost is written, do stuff:
check_HTIF();
break;
// CSRRC
case 0x3:
var temp = RISCV.priv_reg[inst.get_CSR_imm()];
if (typeof temp === "number") {
RISCV.gen_reg[inst.get_rd()] = new Long(temp, 0x0);
temp = temp & ~(rs1.getLowBitsUnsigned());
} else {
//temp is a long
RISCV.gen_reg[inst.get_rd()] = temp;
temp = temp.and(rs1.not());
}
RISCV.set_pcr(inst.get_CSR_imm(), temp);
RISCV.pc += 4;
// if toHost is written, do stuff:
check_HTIF();
break;
// CSRRWI
case 0x5:
var temp = RISCV.priv_reg[inst.get_CSR_imm()];
var tempbak = temp;
if (typeof temp === "number") {
RISCV.gen_reg[inst.get_rd()] = new Long(temp, 0x0);
temp = inst.get_rs1() & 0x0000001F;
} else {
//temp is a long
RISCV.gen_reg[inst.get_rd()] = temp;
temp = new Long(inst.get_rs1() & 0x0000001F, 0x0);
}
RISCV.set_pcr(inst.get_CSR_imm(), temp);
if (inst.get_CSR_imm() == PCR["CSR_FATC"]["num"]) {
TLB = new Uint32Array(TLBSIZE);
ITLB = new Uint32Array(ITLBSIZE);
ITLBstuff = new Uint32Array(ITLBSIZE);
// console.log("flushing TLB from CSRRWI");
// console.log("Current ASID is " + stringIntHex(RISCV.priv_reg[PCR["CSR_ASID"]["num"]]));
// console.log("Value written to FATC is " + stringIntHex(tempbak));
}
RISCV.pc += 4;
// if toHost is written, do stuff:
check_HTIF();
break;
// CSRRSI
case 0x6:
var temp = RISCV.priv_reg[inst.get_CSR_imm()];
if (typeof temp === "number") {
RISCV.gen_reg[inst.get_rd()] = new Long(temp, 0x0);
temp = temp | (inst.get_rs1() & 0x0000001F);
} else {
//temp is a long
RISCV.gen_reg[inst.get_rd()] = temp;
temp = temp.or(new Long(inst.get_rs1() & 0x0000001F, 0x0));
}
RISCV.set_pcr(inst.get_CSR_imm(), temp);
RISCV.pc += 4;
// if toHost is written, do stuff:
check_HTIF();
break;
// CSRRCI
case 0x7:
var temp = RISCV.priv_reg[inst.get_CSR_imm()];
if (typeof temp === "number") {
RISCV.gen_reg[inst.get_rd()] = new Long(temp, 0x0);
temp = temp & ~(inst.get_rs1() & 0x0000001F);