-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathriscv.awk
More file actions
1071 lines (950 loc) · 29.2 KB
/
riscv.awk
File metadata and controls
1071 lines (950 loc) · 29.2 KB
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
# SPDX-FileCopyrightText: © 2022 Olivia Reynolds
# SPDX-License-Identifier: GPL-3.0-or-later
function base64_decode(input, output, output_index, BASE64_TABLE, ix, n){
BASE64_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
gsub(/=/, "A", input)
for(ix = 1; ix <= length(input); ix+=4){
n = lshift(index(BASE64_TABLE, substr(input, ix, 1)) - 1, 18)
n += lshift(index(BASE64_TABLE, substr(input, ix+1, 1)) - 1, 12)
n += lshift(index(BASE64_TABLE, substr(input, ix+2, 1)) - 1, 6)
n += index(BASE64_TABLE, substr(input, ix+3, 1)) - 1
output[output_index] = and(rshift(n, 16), 255)
output[output_index+1] = and(rshift(n, 8), 255)
output[output_index+2] = and(n, 255)
output_index += 3
}
return (length(input) / 4) * 3
}
function dlab(){
rshift(MEMORY[SERIAL_START + 3], 7)
}
function serial_read(offset){
if(offset == 1 && dlab() == 0){
return 0
}
else if(offset == 5){
# LSR
return lshift(1, 5) + lshift(1, 6)
}
return MEMORY[SERIAL_START + offset]
}
function serial_write(offset, value){
if(offset == 0 && dlab() == 0){
# THB
printf "%c", value
fflush()
}
else{
MEMORY[SERIAL_START + offset] = value
}
}
function clint_read(offset, bytes){
if(offset == 0x4000 && bytes == 8){
# mtimecmp
return TIMECMP
}
else if(offset == 0xbff8 && bytes == 8){
# mtime
return TIME
}
}
function clint_write(offset, value, bytes){
if(offset == 0){
set_interrupt(3, value != 0)
}
if(offset == 0x4000 && bytes == 8){
# mtimecmp
TIMECMP = value
}
else if(offset == 0xbff8 && bytes == 8){
# mtime
TIME = value
}
}
function read_csr(ix){
if(ix == 3073){
# time
return read_memory(CLINT_START + 0xbff8, 8)
}
return CSR[ix]
}
function write_csr(ix, value){
if(ix == 3073){
# time
# read-only
return
}
CSR[ix] = fit_register(value)
}
function fit_register(x){
if(x >= 0){
return and(x, 0xffffffffffffffff)
}
return -and(-x, 0xffffffffffffffff)
}
function fit_half_register(x){
if(x >= 0){
return and(x, 0xffffffff)
}
return -and(-x, 0xffffffff)
}
function read_register(ix){
if(ix == 0) return 0
return REGISTERS[ix]
}
function read_half_register(ix){
return de_twos_complement(get_bits(re_twos_complement(read_register(ix), 64), 0, 31), 32)
}
function write_register(ix, value){
if(ix == 0) return
value = fit_register(value)
REGISTERS[ix] = value
}
function write_half_register(ix, value){
if(ix == 0) return
value = fit_half_register(value)
REGISTERS[ix] = value
}
function read_memory(offset, bytes, n, ix){
if(bytes == 1 && offset >= SERIAL_START && offset <= SERIAL_END){
return serial_read(offset - SERIAL_START)
}
if(offset >= CLINT_START && offset <= CLINT_END){
return clint_read(offset - CLINT_START, bytes)
}
if(!(offset in MEMORY)){
return 0
}
n = 0
for(ix = 0; ix < bytes; ix++){
n += lshift(MEMORY[offset + ix], ix * 8)
}
return n
}
function write_memory(offset, value, bytes, ix){
value = re_twos_complement(value, bytes * 8)
if(bytes == 1 && offset >= SERIAL_START && offset <= SERIAL_END){
return serial_write(offset - SERIAL_START, value)
}
if(offset >= CLINT_START && offset <= CLINT_END){
return clint_write(offset - CLINT_START, value, bytes)
}
for(ix = 0; ix < bytes; ix++){
MEMORY[offset+ix] = and(rshift(value, ix * 8), 255)
}
}
function re_twos_complement(value, bits){
if(value < 0){
value += 2 * lshift(1, bits - 1)
}
return value
}
function de_twos_complement(value, bits){
if(value < 0){
print "invalid arg to de_twos_complement"
die()
}
if(and(value, lshift(1, bits - 1)) != 0){
value -= 2 * lshift(1, bits - 1)
}
return value
}
function get_bits(instruction, start_bit, end_bit){
if(instruction < 0){
print "invalid arg to get_bits"
die()
}
return and(rshift(instruction, start_bit), lshift(1, end_bit - start_bit + 1) - 1)
}
function get_bit(instruction, bit){
return get_bits(instruction, bit, bit)
}
function set_bit(value, ix, bit){
if(bit == 0){
return and(value, xor(0xffffffffffffffff, lshift(1, ix)))
}
else{
return or(value, lshift(1, ix))
}
}
function i_immediate(instruction){
return de_twos_complement(get_bits(instruction, 20, 31), 12)
}
function s_immediate(instruction){
return de_twos_complement(get_bits(instruction, 7, 11) + lshift(get_bits(instruction, 25, 31), 5), 12)
}
function b_immediate(instruction){
return de_twos_complement(lshift(get_bits(instruction, 8, 11), 1) + lshift(get_bits(instruction, 25, 30), 5) + lshift(get_bits(instruction, 7, 7), 11) + lshift(get_bits(instruction, 31, 31), 12), 13)
}
function u_immediate(instruction){
return lshift(get_bits(instruction, 12, 31), 12)
}
function j_immediate(instruction){
return de_twos_complement(lshift(get_bits(instruction, 21, 30), 1) + lshift(get_bits(instruction, 20, 20), 11) + lshift(get_bits(instruction, 12, 19), 12) + lshift(get_bits(instruction, 31, 31), 20), 21)
}
function die(){
printf "PC: %x\n", PC
exit 1
}
function set_interrupt(irq, value, mip){
mip = re_twos_complement(read_csr(0x344), 64)
if(value){
mip = set_bit(mip, irq, 1)
}
else{
mip = set_bit(mip, irq, 0)
}
write_csr(0x344, de_twos_complement(mip, 64))
}
function pend_interrupt(irq){
set_interrupt(irq, 1)
}
function unpend_interrupt(irq){
set_interrupt(irq, 0)
}
function process_interrupts(mstatus, interrupts, irq, mtvec, base, old_pc){
mstatus = re_twos_complement(read_csr(0x300), 64)
# Check mstatus.MIE
if(and(mstatus, 8) == 0){
return
}
interrupts = and(read_csr(0x304), read_csr(0x344))
for(irq = 0; irq < 64; irq++){
if(and(interrupts, lshift(1, irq)) == 0) continue
# MCAUSE
write_csr(0x342, de_twos_complement(or(lshift(1, 63), irq), 64))
mstatus = set_bit(mstatus, 7, 1)
mstatus = set_bit(mstatus, 3, 0)
# set previous privilege to M
mstatus = set_bit(mstatus, 11, 1)
mstatus = set_bit(mstatus, 12, 1)
write_csr(0x300, de_twos_complement(mstatus, 64))
# MEPC
write_csr(0x341, de_twos_complement(PC, 64))
# Jump!
mtvec = re_twos_complement(read_csr(0x305), 64)
base = and(mtvec, 0xfffffffffffffffc)
old_pc = PC
if(get_bits(mtvec, 0, 1) == 0){
PC = base
}
else{
PC = base + (irq * 4)
}
break
}
}
function fetch(n){
n = read_memory(PC, 4)
PC += 4
return n
}
function execute(instruction, opcode, rd, rs1, rs2, funct3, imm, handled){
opcode = and(instruction, 127)
rd = and(rshift(instruction, 7), 31)
rs1 = and(rshift(instruction, 15), 31)
rs2 = and(rshift(instruction, 20), 31)
funct3 = and(rshift(instruction, 12), 7)
handled = 0
if(opcode == 3){
imm = i_immediate(instruction)
off = read_register(rs1) + imm
if(funct3 == 0){
# LB
write_register(rd, de_twos_complement(read_memory(off, 1), 8))
handled = 1
}
else if(funct3 == 1){
# LH
write_register(rd, de_twos_complement(read_memory(off, 2), 16))
handled = 1
}
else if(funct3 == 2){
# LW
write_register(rd, de_twos_complement(read_memory(off, 4), 32))
handled = 1
}
else if(funct3 == 3){
# LD
write_register(rd, de_twos_complement(read_memory(off, 8), 64))
handled = 1
}
else if(funct3 == 4){
# LBU
write_register(rd, read_memory(off, 1))
handled = 1
}
else if(funct3 == 5){
# LHU
write_register(rd, read_memory(off, 2))
handled = 1
}
else if(funct3 == 6){
# LWU
write_register(rd, read_memory(off, 4))
handled = 1
}
}
else if(opcode == 15){
if(funct3 == 0){
# FENCE
# noop
handled = 1
}
else if(funct3 == 1){
# FENCE.I
# noop
handled = 1
}
}
else if(opcode == 19){
funct6 = get_bits(instruction, 26, 31)
if(funct3 == 0){
# ADDI
imm = i_immediate(instruction)
write_register(rd, read_register(rs1) + imm)
handled = 1
}
else if(funct3 == 1){
if(funct6 == 0){
# SLLI
shift = get_bits(instruction, 20, 25)
write_register(rd, de_twos_complement(and(lshift(re_twos_complement(read_register(rs1), 64), shift), 0xffffffffffffffff), 64))
handled = 1
}
else{
print "funct6: " funct6
}
}
else if(funct3 == 2){
# SLTI
imm = i_immediate(instruction)
if(read_register(rs1) < imm){
write_register(rd, 1)
}
else{
write_register(rd, 0)
}
handled = 1
}
else if(funct3 == 3){
# SLTIU
imm = i_immediate(instruction)
if(re_twos_complement(read_register(rs1), 64) < re_twos_complement(imm, 12)){
write_register(rd, 1)
}
else{
write_register(rd, 0)
}
handled = 1
}
else if(funct3 == 4){
# XORI
imm = re_twos_complement(i_immediate(instruction), 64)
write_register(rd, de_twos_complement(xor(re_twos_complement(read_register(rs1), 64), imm), 64))
handled = 1
}
else if(funct3 == 5){
shift = get_bits(instruction, 20, 25)
if(funct6 == 0){
# SRLI
write_register(rd, de_twos_complement(rshift(re_twos_complement(read_register(rs1), 64), shift), 64))
handled = 1
}
else if(funct6 == 16){
# SRAI
write_register(rd, de_twos_complement(rshift(re_twos_complement(read_register(rs1), 64), shift), 64 - shift))
handled = 1
}
else{
print "funct6: " funct6
}
}
else if(funct3 == 6){
# ORI
imm = re_twos_complement(i_immediate(instruction), 64)
write_register(rd, de_twos_complement(or(re_twos_complement(read_register(rs1), 64), imm), 64))
handled = 1
}
else if(funct3 == 7){
# ANDI
imm = re_twos_complement(i_immediate(instruction), 64)
write_register(rd, de_twos_complement(and(re_twos_complement(read_register(rs1), 64), imm), 64))
handled = 1
}
}
else if(opcode == 23){
# AUIPC
imm = de_twos_complement(u_immediate(instruction), 32)
write_register(rd, imm + (PC - 4))
handled = 1
}
else if(opcode == 27){
funct7 = get_bits(instruction, 25, 31)
imm = i_immediate(instruction)
if(funct3 == 0){
# ADDIW
write_half_register(rd, read_half_register(rs1) + imm)
handled = 1
}
else if(funct3 == 1){
if(funct7 == 0){
# SLLIW
shift = get_bits(instruction, 20, 25)
write_half_register(rd, de_twos_complement(lshift(re_twos_complement(read_half_register(rs1), 32), shift), 32))
handled = 1
}
else{
print "funct7 " funct7
}
}
else if(funct3 == 5){
shift = get_bits(instruction, 20, 25)
if(funct7 == 0){
# SRLIW
write_half_register(rd, de_twos_complement(rshift(re_twos_complement(read_half_register(rs1), 32), shift), 32))
handled = 1
}
else if(funct7 == 32){
# SRAIW
write_half_register(rd, de_twos_complement(rshift(re_twos_complement(read_half_register(rs1), 32), shift), 32 - shift))
handled = 1
}
else{
print "funct7:" funct7
}
}
}
else if(opcode == 35){
imm = s_immediate(instruction)
off = read_register(rs1) + imm
val = read_register(rs2)
if(funct3 == 0){
# SB
write_memory(off, val, 1)
handled = 1
}
else if(funct3 == 1){
# SH
write_memory(off, val, 2)
handled = 1
}
else if(funct3 == 2){
# SW
write_memory(off, val, 4)
handled = 1
}
else if(funct3 == 3){
# SD
write_memory(off, val, 8)
handled = 1
}
}
else if(opcode == 47){
funct5 = get_bits(instruction, 27, 31)
if(funct3 == 2){
width = 4
}
else if(funct3 == 3){
width = 8
}
else{
print "Unknown width for atomic op"
}
if(funct5 == 0){
# AMOADD.*
off = read_register(rs1)
val = de_twos_complement(read_memory(off, width), width * 8)
if(width == 4){
val2 = read_half_register(rs2)
write_half_register(rd, val)
}
else{
val2 = read_register(rs2)
write_register(rd, val)
}
val += val2
write_memory(off, val, width)
handled = 1
}
else if(funct5 == 1){
# AMOSWAP.D
off = read_register(rs1)
val = read_memory(off, width)
if(width == 4){
val2 = read_half_register(rs2)
write_half_register(rd, val)
}
else{
val2 = read_register(rs2)
write_register(rd, val)
}
write_memory(off, val2, width)
handled = 1
}
else if(funct5 == 2){
# LR.*
val = de_twos_complement(read_memory(read_register(rs1), width), width * 8)
if(width == 4){
write_half_register(rd, val)
}
else{
write_register(rd, val)
}
handled = 1
}
else if(funct5 == 3){
# SC.*
if(width == 4){
val = read_half_register(rs2)
}
else{
val = read_register(rs2)
}
write_memory(read_register(rs1), val, width)
write_register(rd, 0)
handled = 1
}
else if(funct5 == 8){
# AMOOR.*
off = read_register(rs1)
val = read_memory(off, width)
if(width == 4){
val2 = de_twos_complement(or(val, re_twos_complement(read_half_register(rs2), 32)), 32)
write_half_register(rd, val)
}
else{
val2 = de_twos_complement(or(val, re_twos_complement(read_register(rs2), 64)), 64)
write_register(rd, val)
}
write_memory(off, val2, width)
handled = 1
}
else if(funct5 == 12){
# AMOAND.W
off = read_register(rs1)
val = read_memory(off, width)
if(width == 4){
val2 = de_twos_complement(and(val, re_twos_complement(read_half_register(rs2), 32)), 32)
write_half_register(rd, val)
}
else{
val2 = de_twos_complement(and(val, re_twos_complement(read_register(rs2), 64)), 64)
write_register(rd, val)
}
write_memory(off, val2, width)
handled = 1
}
else{
print "funct5:" funct5
}
}
else if(opcode == 51){
funct7 = get_bits(instruction, 25, 31);
if(funct3 == 0){
if(funct7 == 0){
# ADD
write_register(rd, read_register(rs1) + read_register(rs2))
handled = 1
}
else if(funct7 == 1){
# MUL
write_register(rd, read_register(rs1) * read_register(rs2))
handled = 1
}
else if(funct7 == 32){
# SUB
write_register(rd, read_register(rs1) - read_register(rs2))
handled = 1
}
else{
print "funct7 " funct7
}
}
else if(funct3 == 1){
if(funct7 == 0){
# SLL
write_register(rd, de_twos_complement(lshift(re_twos_complement(read_register(rs1), 64), get_bits(re_twos_complement(read_register(rs2), 64), 0, 5)), 64))
handled = 1
}
else{
print "funct7 " funct7
}
}
else if(funct3 == 2){
if(funct7 == 0){
# SLT
if(read_register(rs1) < read_register(rs2)){
write_register(rd, 1)
}
else{
write_register(rd, 0)
}
handled = 1
}
else{
print "funct7 " funct7
}
}
else if(funct3 == 3){
if(funct7 == 0){
# SLTU
if(re_twos_complement(read_register(rs1), 64) < re_twos_complement(read_register(rs2), 64)){
write_register(rd, 1)
}
else{
write_register(rd, 0)
}
handled = 1
}
else if(funct7 == 1){
# MULHU
write_register(rd, rshift(re_twos_complement(read_register(rs1), 64) * re_twos_complement(read_register(rs2), 64), 64))
handled = 1
}
else{
print "funct7 " funct7
}
}
else if(funct3 == 4){
if(funct7 == 0){
# XOR
write_register(rd, de_twos_complement(xor(re_twos_complement(read_register(rs1), 64), re_twos_complement(read_register(rs2), 64)), 64))
handled = 1
}
else if(funct7 == 1){
# DIV
write_register(rd, de_twos_complement(read_register(rs1) / read_register(rs2), 64))
handled = 1
}
else{
print "funct7 " funct7
}
}
else if(funct3 == 5){
if(funct7 == 0){
# SRL
write_register(rd, de_twos_complement(rshift(re_twos_complement(read_register(rs1), 64), get_bits(re_twos_complement(read_register(rs2), 64), 0, 5)), 64))
handled = 1
}
else if(funct7 == 1){
# DIVU
write_register(rd, de_twos_complement(re_twos_complement(read_register(rs1), 64) / re_twos_complement(read_register(rs2), 64), 64))
handled = 1
}
else if(funct7 == 32){
# SRA
shift = get_bits(re_twos_complement(read_register(rs2), 64), 0, 5)
write_register(rd, de_twos_complement(rshift(re_twos_complement(read_register(rs1), 64), shift), 64 - shift))
handled = 1
}
else{
print "funct7 " funct7
}
}
else if(funct3 == 6){
if(funct7 == 0){
# OR
write_register(rd, de_twos_complement(or(re_twos_complement(read_register(rs1), 64), re_twos_complement(read_register(rs2), 64)), 64))
handled = 1
}
else{
print "funct7 " funct7
}
}
else if(funct3 == 7){
if(funct7 == 0){
# AND
write_register(rd, de_twos_complement(and(re_twos_complement(read_register(rs1), 64), re_twos_complement(read_register(rs2), 64)), 64))
handled = 1
}
else if(funct7 == 1){
# REMU
write_register(rd, re_twos_complement(read_register(rs1), 64) % re_twos_complement(read_register(rs2), 64))
handled = 1
}
else{
print "funct7 " funct7
}
}
}
else if(opcode == 55){
# LUI
imm = u_immediate(instruction)
write_register(rd, de_twos_complement(imm, 32))
handled = 1
}
else if(opcode == 59){
funct7 = get_bits(instruction, 25, 31)
if(funct3 == 0){
if(funct7 == 0){
# ADDW
write_half_register(rd, read_half_register(rs1) + read_half_register(rs2))
handled = 1
}
else if(funct7 == 1){
# MULW
write_half_register(rd, read_half_register(rs1) * read_half_register(rs2))
handled = 1
}
else if(funct7 == 32){
# SUBW
write_half_register(rd, read_half_register(rs1) - read_half_register(rs2))
handled = 1
}
else{
print "funct7:" funct7
}
}
else if(funct3 == 1){
if(funct7 == 0){
# SLLW
write_half_register(rd, lshift(read_half_register(rs1), get_bits(read_half_register(rs2), 0, 5)))
handled = 1
}
else{
print "funct7:" funct7
}
}
else if(funct3 == 4){
if(funct7 == 1){
# DIVW
write_half_register(rd, read_half_register(rs1) / read_half_register(rs2))
handled = 1
}
else{
print "funct7:" funct7
}
}
else if(funct3 == 5){
if(funct7 == 0){
# SRLW
shift = re_twos_complement(read_half_register(rs2), 32)
write_half_register(rd, de_twos_complement(rshift(re_twos_complement(read_half_register(rs1), 32), shift), 32))
handled = 1
}
else if(funct7 == 1){
# DIVUW
write_half_register(rd, de_twos_complement(re_twos_complement(read_half_register(rs1), 32) / re_twos_complement(read_half_register(rs2), 32), 64))
handled = 1
}
else if(funct7 == 32){
# SRAW
shift = re_twos_complement(read_half_register(rs2), 32)
write_half_register(rd, de_twos_complement(rshift(re_twos_complement(read_half_register(rs1), 32), shift), 32 - shift))
handled = 1
}
else{
print "funct7:" funct7
}
}
else if(funct3 == 6){
if(funct7 == 1){
# REMW
write_half_register(rd, read_half_register(rs1) % read_half_register(rs2))
handled = 1
}
else{
print "funct7:" funct7
}
}
else if(funct3 == 7){
if(funct7 == 1){
# REMUW
write_half_register(rd, re_twos_complement(read_half_register(rs1), 32) % re_twos_complement(read_half_register(rs2), 32))
handled = 1
}
else{
print "funct7:" funct7
}
}
}
else if(opcode == 99){
imm = b_immediate(instruction)
if(funct3 == 0){
# BEQ
if(read_register(rs1) == read_register(rs2)){
PC += (imm - 4)
}
handled = 1
}
else if(funct3 == 1){
# BNE
if(read_register(rs1) != read_register(rs2)){
PC += (imm - 4)
}
handled = 1
}
else if(funct3 == 4){
# BLT
if(read_register(rs1) < read_register(rs2)){
PC += (imm - 4)
}
handled = 1
}
else if(funct3 == 5){
# BGE
if(read_register(rs1) >= read_register(rs2)){
PC += (imm - 4)
}
handled = 1
}
else if(funct3 == 6){
# BLTU
if(re_twos_complement(read_register(rs1), 64) < re_twos_complement(read_register(rs2), 64)){
PC += (imm - 4)
}
handled = 1
}
else if(funct3 == 7){
# BGEU
if(re_twos_complement(read_register(rs1), 64) >= re_twos_complement(read_register(rs2), 64)){
PC += (imm - 4)
}
handled = 1
}
}
else if(opcode == 103){
# JALR
imm = i_immediate(instruction)
target = and(read_register(rs1) + imm, 0xffffffffffffffff - 1)
write_register(rd, PC)
PC = target
handled = 1
}
else if(opcode == 111){
# JAL
imm = j_immediate(instruction)
write_register(rd, PC)
PC += (imm - 4)
handled = 1
}
else if(opcode == 115){
imm = i_immediate(instruction)
if(funct3 == 0){
if(imm == 1){
# EBREAK
printf "[%x] Break!\n", PC
HALT = 1
handled = 1
}
else if(imm == 261){
# WFI
# As the only interrupt that can arise other than software is the timer, we just trigger that now.
# This will need to change if the serial etc gets interrupts
TIME = TIMECMP
handled = 1
}
else if(imm == 770){
# MRET
mstatus = re_twos_complement(read_csr(0x300), 64)
mstatus = set_bit(mstatus, 3, get_bit(mstatus, 7))
mstatus = set_bit(mstatus, 7, 0)
write_csr(0x300, de_twos_complement(mstatus, 64))
PC = re_twos_complement(read_csr(0x341), 64)
handled = 1
}
else{
print "imm:" imm
}
}
else if(funct3 == 1){
# CSRRW
val = read_csr(imm)
write_csr(imm, read_register(rs1))
write_register(rd, val)
handled = 1
}
else if(funct3 == 2){
# CSRRS
val = read_csr(imm)
mask = re_twos_complement(read_register(rs1), 64)
write_register(rd, val)
write_csr(imm, de_twos_complement(or(re_twos_complement(val, 64), mask), 64))
handled = 1
}
else if(funct3 == 3){
# CSRRC
val = read_csr(imm)
mask = re_twos_complement(compl(re_twos_complement(read_register(rs1), 64)), 64)
write_register(rd, val)
write_csr(imm, de_twos_complement(and(val, mask), 64))
handled = 1
}
else if(funct3 == 5){
# CSRRWI
val = read_csr(imm)
write_csr(imm, rs1)
write_register(rd, val)
handled = 1
}
else if(funct3 == 6){
# CSRRSI
val = read_csr(imm)
write_register(rd, val)
write_csr(imm, de_twos_complement(or(re_twos_complement(val, 64), rs1), 64))
handled = 1
}
else if(funct3 == 7){
# CSRRCI
val = read_csr(imm)
write_register(rd, val)
write_csr(imm, de_twos_complement(and(re_twos_complement(val, 64), re_twos_complement(compl(rs1), 64)), 64))
handled = 1
}
}
if(handled == 0){
print "Unhandled instruction: " instruction
printf "PC: %x\n", PC
print "Op: " opcode
print "funct3: " funct3