-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
2727 lines (2593 loc) · 70.1 KB
/
main.go
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
package main
import (
"bytes"
"debug/elf"
_ "embed"
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
"strings"
"unicode/utf8"
"unsafe"
"github.com/gdamore/tcell/v2"
)
var MEMORYBASE uint64 = 0x80000000
var DEBUG = false
var debugFile *os.File
const (
FFLAGS = 0x001
FRM = 0x002
FCSR = 0x003
MVENDORID = 0xF11
MARCHID = 0xF12
MIMPID = 0xF13
MHARTID = 0xF14
MSTATUS = 0x300
MISA = 0x301
MEDELEG = 0x302
MIDELEG = 0x303
MIE = 0x304
MTVEC = 0x305
MCOUNTEREN = 0x306
MSCRATCH = 0x340
MEPC = 0x341
MCAUSE = 0x342
MTVAL = 0x343
MIP = 0x344
SSTATUS = 0x100
SEDELEG = 0x102
SIDELEG = 0x103
SIE = 0x104
STVEC = 0x105
SCOUNTEREN = 0x106
SSCRATCH = 0x140
SEPC = 0x141
SCAUSE = 0x142
STVAL = 0x143
SIP = 0x144
SATP = 0x180
USTATUS = 0x000
UIE = 0x004
UTVEC = 0x005
USCRATCH = 0x040
UEPC = 0x041
UCAUSE = 0x042
UTVAL = 0x043
UIP = 0x044
CYCLE = 0xC00
TIME = 0xC01
INSTRET = 0xC02
)
const (
MIP_MEIP = 0x800
MIP_MTIP = 0x080
MIP_MSIP = 0x008
MIP_SEIP = 0x200
MIP_STIP = 0x020
MIP_SSIP = 0x002
)
type Privilege uint8
const (
User Privilege = 0
Supervisor Privilege = 1
Hypervisor Privilege = 2
Machine Privilege = 3
)
type AddressMode uint8
const (
None AddressMode = 0
SV39 AddressMode = 8
SV48 AddressMode = 9
)
type Access uint8
const (
Read Access = 0
Write Access = 1
Execute Access = 2
Unknown Access = 3
)
//go:embed dtb/dtb.dtb
var dtb []byte
type CPU struct {
pc uint64
mem []byte
mem32 []uint32
mem64 []uint64
x [32]int64
f [32]float64
csr [4096]uint64
priv Privilege
mode AddressMode
wfi bool
reservation uint64
reservationSet bool
uart *Uart
plic Plic
clint Clint
disk VirtioBlock
count uint64
}
func NewCPU(mem []byte, pc uint64, disk []byte, screen tcell.Screen) CPU {
cpu := CPU{
pc: pc,
mem: mem,
mem32: *((*[]uint32)(unsafe.Pointer(&mem))),
mem64: *((*[]uint64)(unsafe.Pointer(&mem))),
priv: Machine,
uart: NewUart(screen),
plic: NewPlic(),
clint: NewClint(),
}
cpu.disk = NewVirtioBlock(disk, &cpu)
// TODO: Why?
cpu.x[0xb] = 0x1020
return cpu
}
func (cpu *CPU) run() {
for {
cpu.step()
}
}
func (cpu *CPU) step() {
addr := cpu.pc
ok, trap := cpu.stepInner()
if !ok {
cpu.exception(trap, addr)
}
cpu.count++
cpu.disk.step()
cpu.uart.step(cpu.count)
cpu.clint.step(cpu.count, &cpu.csr[MIP])
cpu.plic.step(cpu.count, cpu.uart.interrupting, cpu.disk.interruptStatus&0b1 == 1, &cpu.csr[MIP])
cpu.interrupt(cpu.pc)
cpu.csr[CYCLE] = cpu.count * 8
}
func (cpu *CPU) stepInner() (bool, Trap) {
if cpu.wfi {
if cpu.readcsr(MIE)&cpu.readcsr(MIP) != 0 {
cpu.wfi = false
}
return true, Trap{}
}
instr, ok, trap := cpu.fetch()
if !ok {
cpu.pc += 4 // TODO: okay?
return false, trap
}
addr := cpu.pc
if DEBUG {
// if cpu.count > 10084750 {
// if cpu.count%100000 == 0 {
var regs []string
for _, r := range cpu.x {
regs = append(regs, fmt.Sprintf("%x", uint64(r)))
}
// memHash := sha1.Sum(cpu.mem)
// csrSlice := cpu.csr[:]
// csrBytes := *((*[]uint8)(unsafe.Pointer(&csrSlice)))
// csrHash := sha1.Sum(csrBytes)
// fmt.Fprintf(debugFile, "%08d -- [%08x]: %08x [%s] mem=%0x csrs=%0x\n", cpu.count, cpu.pc, instr, strings.Join(regs, ", "), memHash, csrHash)
fmt.Fprintf(debugFile, "%08d -- [%08x]: %08x [%s]\n", cpu.count, cpu.pc, instr, strings.Join(regs, ", "))
// }
}
if instr&0b11 == 0b11 {
cpu.pc += 4
} else {
cpu.pc += 2
instr = cpu.decompress(instr & 0xFFFF)
}
ok, trap = cpu.exec(instr, addr)
cpu.x[0] = 0
if !ok {
return false, trap
}
return true, Trap{}
}
func (cpu *CPU) fetch() (uint32, bool, Trap) {
v := uint32(0)
if cpu.pc&0xfff <= 0x1000-4 { // instruction is within a single page
paddr, ok := cpu.virtualToPhysical(cpu.pc, Execute)
if !ok {
return 0, false, Trap{reason: InstructionPageFault, value: cpu.pc}
}
if paddr >= MEMORYBASE && paddr%4 == 0 {
memaddr := paddr - MEMORYBASE
v = uint32(cpu.mem32[memaddr/4])
} else {
for i := uint64(0); i < 4; i++ {
x := cpu.readphysical(paddr + i)
v |= uint32(x) << (i * 8)
}
}
} else { // instruction is across two pages
for i := uint64(0); i < 4; i++ {
paddr, ok := cpu.virtualToPhysical(cpu.pc+i, Execute)
if !ok {
return 0, false, Trap{reason: InstructionPageFault, value: cpu.pc + i}
}
x := cpu.readphysical(paddr)
v |= uint32(x) << (i * 8)
}
}
return v, true, Trap{}
}
type TrapReason uint64
const (
UserSoftwareInterrupt TrapReason = 0x8000000000000000
SupervisorSoftwareInterrupt TrapReason = 0x8000000000000001
HypervisorSoftwareInterrupt TrapReason = 0x8000000000000002
MachineSoftwareInterrupt TrapReason = 0x8000000000000003
UserTimerInterrupt TrapReason = 0x8000000000000004
SupervisorTimerInterrupt TrapReason = 0x8000000000000005
HypervisorTimerInterrupt TrapReason = 0x8000000000000006
MachineTimerInterrupt TrapReason = 0x8000000000000007
UserExternalInterrupt TrapReason = 0x8000000000000008
SupervisorExternalInterrupt TrapReason = 0x8000000000000009
HypervisorExternalInterrupt TrapReason = 0x800000000000000A
MachineExternalInterrupt TrapReason = 0x800000000000000B
InstructionAddressMisaligned TrapReason = 0x0000000000000000
InstructionAccessFault TrapReason = 0x0000000000000001
IllegalInstruction TrapReason = 0x0000000000000002
Breakpoint TrapReason = 0x0000000000000003
LoadAddressMisaligned TrapReason = 0x0000000000000004
LoadAccessFault TrapReason = 0x0000000000000005
StoreAddressMisaligned TrapReason = 0x0000000000000006
StoreAccessFault TrapReason = 0x0000000000000007
EnvironmentCallFromUMode TrapReason = 0x0000000000000008
EnvironmentCallFromSMode TrapReason = 0x0000000000000009
EnvironmentCallFromHMode TrapReason = 0x000000000000000A
EnvironmentCallFromMMode TrapReason = 0x000000000000000B
InstructionPageFault TrapReason = 0x000000000000000C
LoadPageFault TrapReason = 0x000000000000000D
StorePageFault TrapReason = 0x000000000000000F
)
type Trap struct {
reason TrapReason
value uint64
}
func (cpu *CPU) exception(trap Trap, instructionaddr uint64) {
cpu.trap(trap.reason, trap.value, instructionaddr, false)
}
func (cpu *CPU) interrupt(pc uint64) {
minterrupt := cpu.readcsr(MIP) & cpu.readcsr(MIE)
if minterrupt&MIP_MEIP != 0 {
panic("nyi - handle MachineExternalInterrupt")
}
if minterrupt&MIP_MSIP != 0 {
panic("nyi - handle MachineSoftwareInterrupt")
}
if minterrupt&MIP_MTIP != 0 {
traptaken := cpu.trap(MachineTimerInterrupt, cpu.pc, pc, true)
if traptaken {
cpu.writecsr(MIP, cpu.readcsr(MIP)&^MIP_MTIP)
cpu.wfi = false
}
}
if minterrupt&MIP_SEIP != 0 {
traptaken := cpu.trap(SupervisorExternalInterrupt, cpu.pc, pc, true)
if traptaken {
cpu.writecsr(MIP, cpu.readcsr(MIP)&^MIP_SEIP)
cpu.wfi = false
}
}
if minterrupt&MIP_SSIP != 0 {
traptaken := cpu.trap(SupervisorSoftwareInterrupt, cpu.pc, pc, true)
if traptaken {
cpu.writecsr(MIP, cpu.readcsr(MIP)&^MIP_SSIP)
cpu.wfi = false
}
}
if minterrupt&MIP_STIP != 0 {
traptaken := cpu.trap(SupervisorTimerInterrupt, cpu.pc, pc, true)
if traptaken {
cpu.writecsr(MIP, cpu.readcsr(MIP)&^MIP_STIP)
cpu.wfi = false
}
}
}
func (cpu *CPU) trap(reason TrapReason, value, addr uint64, isInterrupt bool) bool {
// if reason != 0x8000000000000007 && reason != 0x8000000000000005 {
// if reason == 0x8000000000000009 {
// fmt.Fprintf(debugFile, "Handling trap: %d, %08x, %08x, %08x\n", cpu.count, reason, value, addr)
// }
var mdeleg, sdeleg uint64
if isInterrupt {
mdeleg, sdeleg = cpu.readcsr(MIDELEG), cpu.readcsr(SIDELEG)
} else {
mdeleg, sdeleg = cpu.readcsr(MEDELEG), cpu.readcsr(SEDELEG)
}
pos := uint64(reason) & 0xFFFF
fromPriv := cpu.priv
var handlePriv Privilege
var status, ie uint64
var epcAddr, causeAddr, tvalAddr, tvecAddr uint16
if (mdeleg>>pos)&1 == 0 {
handlePriv = Machine
status = cpu.readcsr(MSTATUS)
ie = cpu.readcsr(MIE)
epcAddr, causeAddr, tvalAddr, tvecAddr = MEPC, MCAUSE, MTVAL, MTVEC
} else if (sdeleg>>pos)&1 == 0 {
handlePriv = Supervisor
status = cpu.readcsr(SSTATUS)
ie = cpu.readcsr(SIE)
epcAddr, causeAddr, tvalAddr, tvecAddr = SEPC, SCAUSE, STVAL, STVEC
} else {
handlePriv = User
status = cpu.readcsr(USTATUS)
ie = cpu.readcsr(UIE)
epcAddr, causeAddr, tvalAddr, tvecAddr = UEPC, UCAUSE, UTVAL, UTVEC
}
if isInterrupt {
if handlePriv < fromPriv {
return false
} else if handlePriv == fromPriv {
switch fromPriv {
case Machine:
if (status>>3)&1 == 0 {
return false
}
case Supervisor:
if (status>>1)&1 == 0 {
return false
}
case User:
if (status>>0)&1 == 0 {
return false
}
default:
panic("invalid privilege")
}
}
switch reason {
case UserSoftwareInterrupt:
if (ie>>0)&1 == 0 {
return false
}
case SupervisorSoftwareInterrupt:
if (ie>>1)&1 == 0 {
return false
}
case MachineSoftwareInterrupt:
if (ie>>3)&1 == 0 {
return false
}
case UserTimerInterrupt:
if (ie>>4)&1 == 0 {
return false
}
case SupervisorTimerInterrupt:
if (ie>>5)&1 == 0 {
return false
}
case MachineTimerInterrupt:
if (ie>>7)&1 == 0 {
return false
}
case UserExternalInterrupt:
if (ie>>8)&1 == 0 {
return false
}
case SupervisorExternalInterrupt:
if (ie>>9)&1 == 0 {
return false
}
case MachineExternalInterrupt:
if (ie>>11)&1 == 0 {
return false
}
}
}
cpu.priv = handlePriv
cpu.writecsr(epcAddr, addr)
cpu.writecsr(causeAddr, uint64(reason))
cpu.writecsr(tvalAddr, value)
cpu.pc = cpu.readcsr(tvecAddr)
if (cpu.pc & 0b11) != 0 {
panic("vector type address")
cpu.pc = (cpu.pc>>2)<<2 + (4 * (uint64(reason) & 0xFFFF))
}
switch cpu.priv {
case Machine:
status := cpu.readcsr(MSTATUS)
mie := (status >> 3) & 0b1
status = (status &^ 0x1888) | mie<<7 | uint64(fromPriv)<<11
cpu.writecsr(MSTATUS, status)
case Supervisor:
status := cpu.readcsr(SSTATUS)
sie := (status >> 1) & 0b1
status = (status &^ 0x122) | sie<<5 | uint64(fromPriv&1)<<8
cpu.writecsr(SSTATUS, status)
case User:
panic("nyi - user mode exception handler")
default:
panic("invalid privilege")
}
return true
}
type I struct {
imm int32
rs1 uint32
funct3 uint32
rd uint32
}
func parseI(instr uint32) I {
imm := uint32(0)
if (instr>>31)&0b1 == 0b1 {
imm = 0xfffff800
}
return I{
imm: int32(imm | (instr>>20)&0x000007ff),
rs1: (instr >> 15) & 0b11111,
funct3: (instr >> 12) & 0b111,
rd: (instr >> 7) & 0b11111,
}
}
type S struct {
imm int32
rs1 uint32
funct3 uint32
rs2 uint32
}
func parseS(instr uint32) S {
imm := uint32(0)
if (instr>>31)&0b1 == 0b1 {
imm = 0xfffff800
}
return S{
imm: int32(imm | ((instr>>25)&0x3f)<<5 | (instr>>7)&0x1f), // 1 + 6 + 5
rs1: (instr >> 15) & 0b11111, // 5
rs2: (instr >> 20) & 0b11111, // 5
funct3: (instr >> 12) & 0b111, // 3
}
}
type B struct {
imm int32
rs1 uint32
funct3 uint32
rs2 uint32
}
func parseB(instr uint32) B {
imm := uint32(0)
if (instr>>31)&0b1 == 0b1 {
imm = 0xfffff000
}
return B{
imm: int32(imm | ((instr>>25)&0x3f)<<5 | (instr>>7)&0x1e | (instr>>7)&0b1<<11), // 1 + 6 + 4 + 1
rs1: (instr >> 15) & 0b11111, // 5
rs2: (instr >> 20) & 0b11111, // 5
funct3: (instr >> 12) & 0b111, // 3
}
}
type U struct {
imm int64
rd uint32
}
func parseU(instr uint32) U {
imm := uint64(0)
if (instr>>31)&0b1 == 0b1 {
imm = 0xffffffff00000000
}
return U{
imm: int64(imm | uint64(instr)&0xfffff000),
rd: (instr >> 7) & 0b11111,
}
}
type J struct {
imm int32
rd uint32
}
func parseJ(instr uint32) J {
imm := uint32(0)
if (instr>>31)&0b1 == 0b1 {
imm = 0xfff00000
}
return J{
imm: int32(imm | (instr & 0x000ff000) | (instr&0x00100000)>>9 | (instr&0x7fe00000)>>20),
rd: (instr >> 7) & 0b11111,
}
}
type CSR struct {
csr uint32
rs uint32
funct3 uint32
rd uint32
}
func parseCSR(instr uint32) CSR {
return CSR{
csr: (instr >> 20) & 0x00000fff,
rs: (instr >> 15) & 0b11111,
funct3: (instr >> 12) & 0b111,
rd: (instr >> 7) & 0b11111,
}
}
type R struct {
funct7 uint32
rs2 uint32
rs1 uint32
funct3 uint32
rd uint32
}
func parseR(instr uint32) R {
return R{
funct7: (instr >> 25) & 0b1111111,
rs2: (instr >> 20) & 0b11111,
rs1: (instr >> 15) & 0b11111,
funct3: (instr >> 12) & 0b111,
rd: (instr >> 7) & 0b11111,
}
}
func (cpu *CPU) exec(instr uint32, addr uint64) (bool, Trap) {
switch instr & 0x7f {
case 0b0110111: // LUI
op := parseU(instr)
cpu.x[op.rd] = int64(op.imm)
case 0b0010111: // AUIPC
op := parseU(instr)
cpu.x[op.rd] = int64(addr) + op.imm
case 0b1101111: // JAL
op := parseJ(instr)
cpu.x[op.rd] = int64(cpu.pc)
cpu.pc = addr + uint64(int64(op.imm))
case 0b1100111: // JALR
op := parseI(instr)
rd := op.rd
// TODO: Check on this?
// if rd == 0 {
// rd = 1
// }
t := int64(cpu.pc)
cpu.pc = (uint64(cpu.x[op.rs1]+int64(op.imm)) >> 1) << 1
cpu.x[rd] = t
case 0b1100011:
op := parseB(instr)
switch op.funct3 {
case 0b000: // BEQ
if cpu.x[op.rs1] == cpu.x[op.rs2] {
cpu.pc = addr + uint64(op.imm)
}
case 0b001: // BNE
if cpu.x[op.rs1] != cpu.x[op.rs2] {
cpu.pc = addr + uint64(op.imm)
}
case 0b100: // BLT
if cpu.x[op.rs1] < cpu.x[op.rs2] {
cpu.pc = addr + uint64(op.imm)
}
case 0b101: // BGE
if cpu.x[op.rs1] >= cpu.x[op.rs2] {
cpu.pc = addr + uint64(op.imm)
}
case 0b110: // BLTU
if uint64(cpu.x[op.rs1]) < uint64(cpu.x[op.rs2]) {
cpu.pc = addr + uint64(op.imm)
}
case 0b111: // BGEU
if uint64(cpu.x[op.rs1]) >= uint64(cpu.x[op.rs2]) {
cpu.pc = addr + uint64(op.imm)
}
default:
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
case 0b0000011:
op := parseI(instr)
switch op.funct3 {
case 0b000: // LB
data, ok, trap := cpu.readuint8(uint64(cpu.x[op.rs1] + int64(op.imm)))
if !ok {
return false, trap
}
cpu.x[op.rd] = int64(int8(data))
case 0b001: // LH
data, ok, trap := cpu.readuint16(uint64(cpu.x[op.rs1] + int64(op.imm)))
if !ok {
return false, trap
}
cpu.x[op.rd] = int64(int16(data))
case 0b010: // LW
data, ok, trap := cpu.readuint32(uint64(cpu.x[op.rs1] + int64(op.imm)))
if !ok {
return false, trap
}
cpu.x[op.rd] = int64(int32(data))
case 0b100: // LBU
data, ok, trap := cpu.readuint8(uint64(cpu.x[op.rs1] + int64(op.imm)))
if !ok {
return false, trap
}
cpu.x[op.rd] = int64(data)
case 0b101: // LHU
data, ok, trap := cpu.readuint16(uint64(cpu.x[op.rs1] + int64(op.imm)))
if !ok {
return false, trap
}
cpu.x[op.rd] = int64(data)
case 0b011: // LD
data, ok, trap := cpu.readuint64(uint64(cpu.x[op.rs1] + int64(op.imm)))
if !ok {
return false, trap
}
cpu.x[op.rd] = int64(data)
case 0b110: // LWU
data, ok, trap := cpu.readuint32(uint64(cpu.x[op.rs1] + int64(op.imm)))
if !ok {
return false, trap
}
cpu.x[op.rd] = int64(uint64(data))
default:
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
case 0b0100011:
op := parseS(instr)
switch op.funct3 {
case 0b000: // SB
ok, trap := cpu.writeuint8(uint64(cpu.x[op.rs1]+int64(op.imm)), uint8(cpu.x[op.rs2]))
if !ok {
return false, trap
}
case 0b001: // SH
ok, trap := cpu.writeuint16(uint64(cpu.x[op.rs1]+int64(op.imm)), uint16(cpu.x[op.rs2]))
if !ok {
return false, trap
}
case 0b010: // SW
ok, trap := cpu.writeuint32(uint64(cpu.x[op.rs1]+int64(op.imm)), uint32(cpu.x[op.rs2]))
if !ok {
return false, trap
}
case 0b011: // SD
ok, trap := cpu.writeuint64(uint64(cpu.x[op.rs1]+int64(op.imm)), uint64(cpu.x[op.rs2]))
if !ok {
return false, trap
}
default:
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
case 0b0010011:
op := parseI(instr)
switch op.funct3 {
case 0b000: // ADDI
cpu.x[op.rd] = cpu.x[op.rs1] + int64(op.imm)
case 0b010: // SLTI
if cpu.x[op.rs1] < int64(op.imm) {
cpu.x[op.rd] = 1
} else {
cpu.x[op.rd] = 0
}
case 0b011: // SLTIU
if uint64(cpu.x[op.rs1]) < uint64(int64(op.imm)) {
cpu.x[op.rd] = 1
} else {
cpu.x[op.rd] = 0
}
case 0b100: // XORI
cpu.x[op.rd] = cpu.x[op.rs1] ^ int64(op.imm)
case 0b110: // ORI
cpu.x[op.rd] = cpu.x[op.rs1] | int64(op.imm)
case 0b111: // ANDI
cpu.x[op.rd] = cpu.x[op.rs1] & int64(op.imm)
case 0b001: // SLLI
if op.imm>>6 != 0 {
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
cpu.x[op.rd] = cpu.x[op.rs1] << op.imm
case 0b101:
switch op.imm >> 6 {
case 0: // SRLI
cpu.x[op.rd] = int64(uint64(cpu.x[op.rs1]) >> (op.imm & 0b111111))
case 0b010000: // SRAI
cpu.x[op.rd] = cpu.x[op.rs1] >> (op.imm & 0b111111)
default:
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
default:
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
case 0b0110011:
op := parseR(instr)
switch op.funct3 {
case 0b000:
switch op.funct7 {
case 0b0000000: // ADD
cpu.x[op.rd] = cpu.x[op.rs1] + cpu.x[op.rs2]
case 0b0100000: // SUB
cpu.x[op.rd] = cpu.x[op.rs1] - cpu.x[op.rs2]
case 0b0000001: // MUL
cpu.x[op.rd] = cpu.x[op.rs1] * cpu.x[op.rs2]
default:
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
case 0b001:
switch op.funct7 {
case 0: // SLL
cpu.x[op.rd] = cpu.x[op.rs1] << (cpu.x[op.rs2] & 0b111111)
case 1: // MULH
panic("nyi - MULH")
default:
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
case 0b010:
switch op.funct7 {
case 0: // SLT
if cpu.x[op.rs1] < cpu.x[op.rs2] {
cpu.x[op.rd] = 1
} else {
cpu.x[op.rd] = 0
}
case 1: // MULHSU
panic("nyi - MULHSU")
default:
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
case 0b011:
switch op.funct7 {
case 0: // SLTU
if uint64(cpu.x[op.rs1]) < uint64(cpu.x[op.rs2]) {
cpu.x[op.rd] = 1
} else {
cpu.x[op.rd] = 0
}
case 1: // MULHU
a := uint64(cpu.x[op.rs1])
b := uint64(cpu.x[op.rs2])
alo := a & 0xffffffff
ahi := (a >> 32) & 0xffffffff
blo := b & 0xffffffff
bhi := (b >> 32) & 0xffffffff
axbhi := ahi * bhi
axbmid := ahi * blo
bxamid := alo * bhi
axblo := alo * blo
carry := (uint64(uint32(axbmid)) + uint64(uint32(bxamid)) + axblo>>32) >> 32
cpu.x[op.rd] = int64(axbhi + axbmid>>32 + bxamid>>32 + carry)
default:
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
case 0b100:
switch op.funct7 {
case 0: // XOR
cpu.x[op.rd] = cpu.x[op.rs1] ^ cpu.x[op.rs2]
case 1: // DIV
op := parseR(instr)
a1 := cpu.x[op.rs1]
a2 := cpu.x[op.rs2]
if a2 == 0 {
cpu.x[op.rd] = -1
} else if a1 == math.MinInt64 && a2 == -1 {
cpu.x[op.rd] = a1
} else {
cpu.x[op.rd] = a1 / a2
}
default:
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
case 0b101:
switch op.funct7 {
case 0: // SRL
cpu.x[op.rd] = int64(uint64(cpu.x[op.rs1]) >> (cpu.x[op.rs2] & 0b111111))
case 0b0100000: // SRA
cpu.x[op.rd] = cpu.x[op.rs1] >> (cpu.x[op.rs2] & 0b111111)
case 1: // DIVU
op := parseR(instr)
a1 := uint64(cpu.x[op.rs1])
a2 := uint64(cpu.x[op.rs2])
if a2 == 0 {
cpu.x[op.rd] = -1
} else {
cpu.x[op.rd] = int64(a1 / a2)
}
default:
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
case 0b110:
switch op.funct7 {
case 0: // OR
cpu.x[op.rd] = cpu.x[op.rs1] | cpu.x[op.rs2]
case 1: // REM
op := parseR(instr)
a1 := cpu.x[op.rs1]
a2 := cpu.x[op.rs2]
if a2 == 0 {
cpu.x[op.rd] = a1
} else if a1 == math.MinInt64 && a2 == -1 {
cpu.x[op.rd] = 0
} else {
cpu.x[op.rd] = a1 % a2
}
default:
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
case 0b111:
switch op.funct7 {
case 0: // AND
cpu.x[op.rd] = cpu.x[op.rs1] & cpu.x[op.rs2]
case 1: // REMU
op := parseR(instr)
a1 := uint64(cpu.x[op.rs1])
a2 := uint64(cpu.x[op.rs2])
if a2 == 0 {
cpu.x[op.rd] = int64(a1)
} else {
cpu.x[op.rd] = int64(a1 % a2)
}
default:
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
default:
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
case 0b0001111: // FENCE/FENCE.I
// TODO: Is it okay to do nothing?
case 0b1110011:
op := parseCSR(instr)
switch op.funct3 {
case 0b000:
if op.rd != 0 {
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
switch op.csr {
case 0: // ECALL
switch cpu.priv {
case User:
return false, Trap{reason: EnvironmentCallFromUMode, value: addr}
case Supervisor:
return false, Trap{reason: EnvironmentCallFromSMode, value: addr}
case Hypervisor:
return false, Trap{reason: EnvironmentCallFromHMode, value: addr}
case Machine:
return false, Trap{reason: EnvironmentCallFromMMode, value: addr}
default:
panic("invalid CPU privilege")
}
case 1: // EBREAK
return false, Trap{reason: Breakpoint, value: addr}
case 0b000000000010: // URET
panic("nyi - URET")
case 0b000100000010: // SRET
cpu.pc = cpu.readcsr(SEPC)
status := cpu.readcsr(SSTATUS)
cpu.priv = Privilege((status >> 8) & 0b1)
spie := (status >> 5) & 0b1
var mpriv uint64
if cpu.priv == Machine {
mpriv = (status >> 17) & 0b1
}
status = status&^0x20122 | mpriv<<17 | spie<<1 | 1<<5
cpu.writecsr(SSTATUS, status)
case 0b001100000010: // MRET
cpu.pc = cpu.readcsr(MEPC)
status := cpu.readcsr(MSTATUS)
cpu.priv = Privilege((status >> 11) & 0b11)
mpie := (status >> 7) & 0b1
var mpriv uint64
if cpu.priv == Machine {
mpriv = (status >> 17) & 0b1
}
status = status&^0x21888 | mpriv<<17 | mpie<<3 | 1<<7
cpu.writecsr(MSTATUS, status)
case 0b000100000101: // WFI
cpu.wfi = true
default:
switch op.csr >> 5 {
case 0b0001001: // SFENCE.VMA
// TODO: Is it okat to do nothing?
case 0b0010001:
panic("nyi - HFENCE.BVMA")
case 0b1010001:
panic("nyi - HFENCE.GVMA")
default:
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
}
case 0b001: // CSRRW
t := cpu.readcsr(uint16(op.csr))
cpu.writecsr(uint16(op.csr), uint64(cpu.x[op.rs]))
cpu.x[op.rd] = int64(t)
case 0b010: // CSRRS
t := cpu.readcsr(uint16(op.csr))
cpu.writecsr(uint16(op.csr), t|uint64(cpu.x[op.rs]))
cpu.x[op.rd] = int64(t)
case 0b011: // CSRRC
t := cpu.readcsr(uint16(op.csr))
trs := cpu.x[op.rs]
cpu.x[op.rd] = int64(t)
cpu.writecsr(uint16(op.csr), uint64(cpu.x[op.rd] & ^trs))
case 0b101: // CSRRWI
t := cpu.readcsr(uint16(op.csr))
cpu.x[op.rd] = int64(t)
cpu.writecsr(uint16(op.csr), uint64(op.rs))
case 0b110: // CSRRSI
t := cpu.readcsr(uint16(op.csr))
cpu.x[op.rd] = int64(t)
cpu.writecsr(uint16(op.csr), uint64(cpu.x[op.rd]|int64(op.rs)))
case 0b111: // CSRRCI
t := cpu.readcsr(uint16(op.csr))
cpu.x[op.rd] = int64(t)
cpu.writecsr(uint16(op.csr), uint64(cpu.x[op.rd] & ^int64(op.rs)))
default:
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
case 0b0111011:
op := parseR(instr)
switch op.funct3 {
case 0b000:
switch op.funct7 {
case 0b0000000: // ADD
cpu.x[op.rd] = int64(int32(cpu.x[op.rs1]) + int32(cpu.x[op.rs2]))
case 0b0100000: // SUB
cpu.x[op.rd] = int64(int32(cpu.x[op.rs1]) - int32(cpu.x[op.rs2]))
case 1: // MULW
cpu.x[op.rd] = int64(int32(cpu.x[op.rs1]) * int32(cpu.x[op.rs2]))
default:
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
case 0b001:
switch op.funct7 {
case 0:
cpu.x[op.rd] = int64(int32(cpu.x[op.rs1]) << (cpu.x[op.rs2] & 0b11111))
default:
panic(fmt.Sprintf("nyi - invalid instruction %x", instr))
}
case 0b100:
switch op.funct7 {
case 1: // DIVW
op := parseR(instr)
a1 := int32(cpu.x[op.rs1])
a2 := int32(cpu.x[op.rs2])
if a2 == 0 {
cpu.x[op.rd] = -1