-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharviss.h
2496 lines (2305 loc) · 78.7 KB
/
arviss.h
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
/**
* Arviss - A Risc-V Instruction Set Simulator.
*/
#pragma once
#include <stdbool.h>
#include <stdint.h>
#define CACHE_LINES 64
#define CACHE_LINE_LENGTH 32
// Opcodes.
typedef enum
{
opLUI = 0b0110111,
opAUIPC = 0b0010111,
opJAL = 0b1101111,
opJALR = 0b1100111,
opBRANCH = 0b1100011,
opLOAD = 0b0000011,
opSTORE = 0b0100011,
opOPIMM = 0b0010011,
opOP = 0b0110011,
opMISCMEM = 0b0001111,
opSYSTEM = 0b1110011,
opLOADFP = 0b0000111, // RV32F
opSTOREFP = 0b0100111, // RV32F
opOPFP = 0b1010011, // RV32F
opMADD = 0b1000011, // RV32F
opMSUB = 0b1000111, // RV32F
opNMSUB = 0b1001011, // RV32F
opNMADD = 0b1001111, // RV32F
} ArvissOpcode;
// Types of machine mode traps. See privileged spec, table 3.6: machine cause register (mcause) values after trap.
typedef enum
{
// Non-interrupt traps.
trINSTRUCTION_MISALIGNED = 0,
trINSTRUCTION_ACCESS_FAULT = 1,
trILLEGAL_INSTRUCTION = 2,
trBREAKPOINT = 3,
trLOAD_ADDRESS_MISALIGNED = 4,
trLOAD_ACCESS_FAULT = 5,
trSTORE_ADDRESS_MISALIGNED = 6,
trSTORE_ACCESS_FAULT = 7,
trENVIRONMENT_CALL_FROM_U_MODE = 8,
trENVIRONMENT_CALL_FROM_S_MODE = 9,
trRESERVED_10 = 10,
trENVIRONMENT_CALL_FROM_M_MODE = 11,
trINSTRUCTION_PAGE_FAULT = 12,
trRESERVERD_14 = 14,
trSTORE_PAGE_FAULT = 15,
trNOT_IMPLEMENTED_YET = 24, // Technically this is the first item reserved for custom use.
// Interrupts (bit 31 is set).
trUSER_SOFTWARE_INTERRUPT = (int)(0x80000000 + 0),
trSUPERVISOR_SOFTWARE_INTERRUPT = 0x80000000 + 1,
trRESERVED_INT_2 = 0x80000000 + 2,
trMACHINE_SOFTWARE_INTERRUPT = 0x80000000 + 3,
trUSER_TIMER_INTERRUPT = 0x80000000 + 4,
trSUPERVISOR_TIMER_INTERRUPT = 0x80000000 + 5,
trRESERVED_INT_6 = 0x80000000 + 6,
trMACHINE_TIMER_INTERRUPT = 0x80000000 + 7
} ArvissTrapType;
typedef struct
{
ArvissTrapType mcause; // TODO: interrupts.
uint32_t mtval;
} ArvissTrap;
// The result of an Arviss operation.
typedef enum
{
rtOK,
rtTRAP
} ArvissResultType;
typedef struct
{
ArvissResultType type;
ArvissTrap trap;
} ArvissResult;
// ABI names for integer registers.
// See: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc#register-convention
typedef enum
{
abiZERO = 0,
abiRA = 1,
abiSP = 2,
abiGP = 3,
abiTP = 4,
abiT0 = 5,
abiT1 = 6,
abiT2 = 7,
abiS0 = 8,
abiS1 = 9,
abiA0 = 10,
abiA1 = 11,
abiA2 = 12,
abiA3 = 13,
abiA4 = 14,
abiA5 = 15,
abiA6 = 16,
abiA7 = 17,
abiS2 = 18,
abiS3 = 19,
abiS4 = 20,
abiS5 = 21,
abiS6 = 22,
abiS7 = 23,
abiS8 = 24,
abiS9 = 25,
abiS10 = 26,
abiS11 = 27,
abiT3 = 28,
abiT4 = 29,
abiT5 = 30,
abiT6 = 31
} ArvissIntReg;
// ABI names for floating point registers.
// See: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc#register-convention
typedef enum
{
abiFT0 = 0,
abiFT1 = 1,
abiFT2 = 2,
abiFT3 = 3,
abiFT4 = 4,
abiFT5 = 5,
abiFT6 = 6,
abiFT7 = 7,
abiFS0 = 8,
abiFS1 = 9,
abiFA0 = 10,
abiFA1 = 11,
abiFA2 = 12,
abiFA3 = 13,
abiFA4 = 14,
abiFA5 = 15,
abiFA6 = 16,
abiFA7 = 17,
abiFS2 = 18,
abiFS3 = 19,
abiFS4 = 20,
abiFS5 = 21,
abiFS6 = 22,
abiFS7 = 23,
abiFS8 = 24,
abiFS9 = 25,
abiFS10 = 26,
abiFS11 = 27,
abiFT8 = 28,
abiFT9 = 29,
abiFT10 = 30,
abiFT11 = 31
} ArvissFloatReg;
typedef enum
{
rmRNE = 0b000,
rmRTZ = 0b001,
rmRDN = 0b010,
rmRUP = 0b011,
rmRMM = 0b100,
rmRSVD5 = 0b101,
rmRSVD6 = 0b110,
rmDYN = 0b111
} ArvissRoundingMode;
/**
* A handle to an Arviss CPU.
*/
typedef struct ArvissCpu ArvissCpu;
/**
* An arbitrary, caller supplied token that an Arviss CPU passes to the bus callbacks.
*/
typedef struct
{
void* t;
} BusToken;
// Codes returned by bus operations.
typedef enum
{
bcOK,
bcLOAD_ACCESS_FAULT,
bcSTORE_ACCESS_FAULT
} BusCode;
/**
* Signatures of bus callbacks.
*/
typedef uint8_t (*BusRead8Fn)(BusToken token, uint32_t addr, BusCode* busCode);
typedef uint16_t (*BusRead16Fn)(BusToken token, uint32_t addr, BusCode* busCode);
typedef uint32_t (*BusRead32Fn)(BusToken token, uint32_t addr, BusCode* busCode);
typedef void (*BusWrite8Fn)(BusToken token, uint32_t addr, uint8_t byte, BusCode* busCode);
typedef void (*BusWrite16Fn)(BusToken token, uint32_t addr, uint16_t halfword, BusCode* busCode);
typedef void (*BusWrite32Fn)(BusToken token, uint32_t addr, uint32_t word, BusCode* busCode);
/**
* The bus is how an Arviss CPU interacts with the rest of the system. It has a number of callbacks, and a caller-supplied token
* that is passed to them on invocation.
*/
typedef struct
{
BusToken token;
BusRead8Fn Read8;
BusRead16Fn Read16;
BusRead32Fn Read32;
BusWrite8Fn Write8;
BusWrite16Fn Write16;
BusWrite32Fn Write32;
} Bus;
typedef enum
{
execIllegalInstruction,
execFetchDecodeReplace,
execLui,
execAuipc,
execJal,
execJalr,
execBeq,
execBne,
execBlt,
execBge,
execBltu,
execBgeu,
execLb,
execLh,
execLw,
execLbu,
execLhu,
execSb,
execSh,
execSw,
execAddi,
execSlti,
execSltiu,
execXori,
execOri,
execAndi,
execSlli,
execSrli,
execSrai,
execAdd,
execSub,
execMul,
execSll,
execMulh,
execSlt,
execMulhsu,
execSltu,
execMulhu,
execXor,
execDiv,
execSrl,
execSra,
execDivu,
execOr,
execRem,
execAnd,
execRemu,
execFence,
execFenceI,
execEcall,
execEbreak,
execUret,
execSret,
execMret,
execFlw,
execFsw,
execFmaddS,
execFmsubS,
execFnmsubS,
execFnmaddS,
execFaddS,
execFsubS,
execFmulS,
execFdivS,
execFsqrtS,
execFsgnjS,
execFsgnjnS,
execFsgnjxS,
execFminS,
execFmaxS,
execFcvtWS,
execFcvtWuS,
execFmvXW,
execFclassS,
execFeqS,
execFltS,
execFleS,
execFcvtSW,
execFcvtSWu,
execFmvWX
} ExecFn;
typedef struct DecodedInstruction DecodedInstruction;
struct DecodedInstruction
{
ExecFn opcode; // The instruction that this function executes. TODO: need to call it something other than opcode?
union
{
struct
{
uint32_t cacheLine; // The instruction's cache line.
uint32_t index; // The instruction's index in the cache line.
} fdr;
struct
{
uint8_t rd; // Destination register.
int32_t imm; // Immediate operand.
} rd_imm;
struct
{
uint8_t rd; // Destination register.
uint8_t rs1; // Source register.
} rd_rs1;
struct
{
uint8_t rd; // Destination register.
uint8_t rs1; // Source register.
int32_t imm; // Immediate operand.
} rd_rs1_imm;
struct
{
uint8_t rd; // Destination register.
uint8_t rs1; // First source register.
uint8_t rs2; // Second source register.
} rd_rs1_rs2;
struct
{
uint8_t rs1; // First source register.
uint8_t rs2; // Second source register.
int32_t imm; // Immediate operand.
} rs1_rs2_imm;
struct
{
uint8_t rd; // Destination register.
uint8_t rs1; // First source register.
uint8_t rs2; // Second source register.
uint8_t rs3; // Third source register.
uint8_t rm; // Rounding mode.
} rd_rs1_rs2_rs3_rm;
struct
{
uint8_t rd; // Destination register.
uint8_t rs1; // Source register.
uint8_t rm; // Rounding mode.
} rd_rs1_rm;
struct
{
uint8_t rd; // Destination register.
uint8_t rs1; // First source register.
uint8_t rs2; // Second source register.
uint8_t rm; // Rounding mode.
} rd_rs1_rs2_rm;
uint32_t ins; // Instruction.
};
};
// Decoded instructions are written to cache lines in the decoded instruction cache. Arviss then executes these decoded
// instructions.
typedef struct
{
struct CacheLine
{
uint32_t owner; // The address that owns this cache line.
DecodedInstruction instructions[CACHE_LINE_LENGTH]; // The cache line itself.
bool isValid; // True if the cache line is valid.
} line[CACHE_LINES];
} DecodedInstructionCache;
// An Arviss CPU.
struct ArvissCpu
{
ArvissResult result; // The result of the last operation.
BusCode busCode; // The result of the last bus operation.
uint32_t pc; // The program counter.
uint32_t xreg[32]; // Regular registers, x0-x31.
uint32_t mepc; // The machine exception program counter.
uint32_t mcause; // The machine cause register.
uint32_t mtval; // The machine trap value register.
float freg[32]; // Floating point registers, f0-f31.
uint32_t fcsr; // Floating point control and status register.
Bus bus; // The address bus.
DecodedInstructionCache cache; // The decoded instruction cache.
int retired; // Instructions retired in the most recent call to ArvissRun().
};
#ifdef __cplusplus
extern "C" {
#endif
static inline ArvissResult ArvissMakeOk(void)
{
// Not using designated initializers as...
// a) C++ < 20 doesn't support them
// b) C++ 20 doesn't like the cast
// return (ArvissResult){ .type = rtOK };
ArvissResult r;
r.type = rtOK;
return r;
}
static inline ArvissResult ArvissMakeTrap(ArvissTrapType trap, uint32_t value)
{
ArvissResult r;
r.type = rtTRAP;
r.trap.mcause = trap;
r.trap.mtval = value;
return r;
}
static inline bool ArvissResultIsTrap(ArvissResult result)
{
return result.type == rtTRAP;
}
static inline ArvissTrap ArvissResultAsTrap(ArvissResult result)
{
return result.trap;
}
/**
* Resets the given Arviss CPU.
* @param cpu the CPU to reset.
*/
void ArvissReset(ArvissCpu* cpu);
/**
* Initialises the given Arviss CPU and provides it with its bus.
* @param cpu the CPU.
* @param bus the bus that the CPU should use to interact with the rest of the system.
*/
static inline void ArvissInit(ArvissCpu* cpu, Bus* bus)
{
ArvissReset(cpu);
cpu->bus = *bus;
}
/**
* Decodes and executes a single Arviss instruction.
* @param cpu the CPU.
* @param instruction the instruction to execute.
* @return an ArvissResult indicating the state of the CPU after executing the instruction.
*/
ArvissResult ArvissExecute(ArvissCpu* cpu, uint32_t instruction);
/**
* Runs count instructions on the CPU.
* @param cpu the CPU.
* @param count how many instructions to run.
* @return an ArvissResult indicating the state of the CPU after attempting to run count instructions.
*/
ArvissResult ArvissRun(ArvissCpu* cpu, int count);
/**
* Reads the given X register.
* @param cpu the CPU.
* @param reg which X register to read (0 - 31).
* @return the content of the X register.
*/
static inline uint32_t ArvissReadXReg(ArvissCpu* cpu, int reg)
{
return cpu->xreg[reg];
}
/**
* Writes to the given X register.
* @param cpu the CPU.
* @param reg which X register to write to (0 - 31).
* @param value the value to write.
*/
static inline void ArvissWriteXReg(ArvissCpu* cpu, int reg, uint32_t value)
{
cpu->xreg[reg] = value;
}
/**
* Reads the given F register.
* @param cpu the CPU.
* @param reg which F register to read (0 - 31).
* @return the content of the F register.
*/
static inline float ArvissReadFReg(ArvissCpu* cpu, int reg)
{
return cpu->freg[reg];
}
/**
* Writes to the given F register.
* @param cpu the CPU.
* @param reg which F register to write to (0 - 31).
* @param value the value to write.
*/
static inline void ArvissWriteFReg(ArvissCpu* cpu, int reg, float value)
{
cpu->freg[reg] = value;
}
/**
* Performs an MRET instruction on the CPU. Use this when returning from a machine-mode trap.
* @param cpu the CPU.
*/
void ArvissMret(ArvissCpu* cpu);
#ifdef __cplusplus
}
#endif
#ifdef ARVISS_IMPLEMENTATION
#include <math.h>
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#if defined(ARVISS_TRACE_ENABLED)
#include <stdio.h>
#define TRACE(...) \
do \
{ \
printf(__VA_ARGS__); \
} while (0)
#else
#define TRACE(...) \
do \
{ \
} while (0)
#endif
#if defined(ARVISS_TRACE_ENABLED)
// The ABI names of the integer registers x0-x31.
static char* abiNames[] = {"zero", "ra", "sp", "gp", "tp", "t0", "t1", "t2", "s0", "s1", "a0", "a1", "a2", "a3", "a4", "a5",
"a6", "a7", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10", "s11", "t3", "t4", "t5", "t6"};
// The ABI names of the floating point registers f0-f31.
static char* fabiNames[] = {"ft0", "ft1", "ft2", "ft3", "ft4", "ft5", "ft6", "ft7", "fs0", "fs1", "fa0",
"fa1", "fa2", "fa3", "fa4", "fa5", "fa6", "fa7", "fs2", "fs3", "fs4", "fs5",
"fs6", "fs7", "fs8", "fs9", "fs10", "fs11", "ft8", "ft9", "ft10", "ft11"};
static char* roundingModes[] = {"rne", "rtz", "rdn", "rup", "rmm", "reserved5", "reserved6", "dyn"};
#endif
static void RunOne(ArvissCpu* cpu, DecodedInstruction* ins);
static DecodedInstruction ArvissDecode(uint32_t instruction);
static inline float U32AsFloat(const uint32_t a)
{
union
{
uint32_t a;
float b;
} u;
u.a = a;
return u.b;
}
static inline uint32_t FloatAsU32(const float a)
{
union
{
float a;
uint32_t b;
} u;
u.a = a;
return u.b;
}
static inline uint32_t BoolAsU32(const bool b)
{
return b ? 1 : 0;
}
// --- Bus access ------------------------------------------------------------------------------------------------------------------
static inline uint8_t Read8(Bus* bus, uint32_t addr, BusCode* busCode)
{
return bus->Read8(bus->token, addr, busCode);
}
static inline uint16_t Read16(Bus* bus, uint32_t addr, BusCode* busCode)
{
return bus->Read16(bus->token, addr, busCode);
}
static inline uint32_t Read32(Bus* bus, uint32_t addr, BusCode* busCode)
{
return bus->Read32(bus->token, addr, busCode);
}
static inline void Write8(Bus* bus, uint32_t addr, uint8_t byte, BusCode* busCode)
{
bus->Write8(bus->token, addr, byte, busCode);
}
static inline void Write16(Bus* bus, uint32_t addr, uint16_t halfword, BusCode* busCode)
{
bus->Write16(bus->token, addr, halfword, busCode);
}
static inline void Write32(Bus* bus, uint32_t addr, uint32_t word, BusCode* busCode)
{
bus->Write32(bus->token, addr, word, busCode);
}
// --- Execution -------------------------------------------------------------------------------------------------------------------
//
// Functions in this section execute decoded instructions. Instruction execution is separate from decoding, as this allows an
// instruction to be fetched and decoded once, then placed in the decoded instruction cache where it can be executed several times.
// This mitigates the cost of decoding, as decoded instructions are already in a form that is easy to execute.
static inline ArvissResult TakeTrap(ArvissCpu* cpu, ArvissResult result)
{
ArvissTrap trap = ArvissResultAsTrap(result);
cpu->mepc = cpu->pc; // Save the program counter in the machine exception program counter.
cpu->mcause = trap.mcause; // mcause <- reason for trap.
cpu->mtval = trap.mtval; // mtval <- exception specific information.
return result;
}
static inline ArvissResult CreateTrap(ArvissCpu* cpu, ArvissTrapType trap, uint32_t value)
{
ArvissResult result = ArvissMakeTrap(trap, value);
return TakeTrap(cpu, result);
}
inline static void Exec_IllegalInstruction(ArvissCpu* cpu, const DecodedInstruction* ins)
{
cpu->result = CreateTrap(cpu, trILLEGAL_INSTRUCTION, ins->ins);
}
inline static void Exec_FetchDecodeReplace(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// Reconstitute the address given the cache line and index.
const uint32_t cacheLine = ins->fdr.cacheLine;
const uint32_t index = ins->fdr.index;
struct CacheLine* line = &cpu->cache.line[cacheLine];
const uint32_t owner = line->owner;
const uint32_t addr = owner * 4 * CACHE_LINE_LENGTH + index * 4;
// Fetch a word from memory at the address.
uint32_t instruction = Read32(&cpu->bus, addr, &cpu->busCode);
// Decode it, save the result in the cache, then execute it.
if (cpu->busCode == bcOK)
{
// Decode the instruction and save it in the cache. All instructions are decodable into something executable, because
// all illegal instructions become Exec_IllegalInstruction, which is itself executable.
DecodedInstruction decoded = ArvissDecode(instruction);
line->instructions[index] = decoded;
// Execute the decoded instruction.
RunOne(cpu, &decoded);
}
else
{
cpu->result = ArvissMakeTrap(trINSTRUCTION_ACCESS_FAULT, addr);
}
}
inline static void Exec_Lui(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- imm_u, pc += 4
TRACE("LUI %s, %d\n", abiNames[ins->rd_imm.rd], ins->rd_imm.imm >> 12);
cpu->xreg[ins->rd_imm.rd] = ins->rd_imm.imm;
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Auipc(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- pc + imm_u, pc += 4
TRACE("AUIPC %s, %d\n", abiNames[ins->rd_imm.rd], ins->rd_imm.imm >> 12);
cpu->xreg[ins->rd_imm.rd] = cpu->pc + ins->rd_imm.imm;
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Jal(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- pc + 4, pc <- pc + imm_j
TRACE("JAL %s, %d\n", abiNames[ins->rd_imm.rd], ins->rd_imm.imm);
cpu->xreg[ins->rd_imm.rd] = cpu->pc + 4;
cpu->pc += ins->rd_imm.imm;
cpu->xreg[0] = 0;
}
inline static void Exec_Jalr(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- pc + 4, pc <- (rs1 + imm_i) & ~1
TRACE("JALR %s, %s, %d\n", abiNames[ins->rd_rs1_imm.rd], abiNames[ins->rd_rs1_imm.rs1], ins->rd_rs1_imm.imm);
uint32_t rs1Before = cpu->xreg[ins->rd_rs1_imm.rs1]; // Because rd and rs1 might be the same register.
cpu->xreg[ins->rd_rs1_imm.rd] = cpu->pc + 4;
cpu->pc = (rs1Before + ins->rd_rs1_imm.imm) & ~1;
cpu->xreg[0] = 0;
}
inline static void Exec_Beq(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// pc <- pc + ((rs1 == rs2) ? imm_b : 4)
TRACE("BEQ %s, %s, %d\n", abiNames[ins->rs1_rs2_imm.rs1], abiNames[ins->rs1_rs2_imm.rs2], ins->rs1_rs2_imm.imm);
cpu->pc += ((cpu->xreg[ins->rs1_rs2_imm.rs1] == cpu->xreg[ins->rs1_rs2_imm.rs2]) ? ins->rs1_rs2_imm.imm : 4);
}
inline static void Exec_Bne(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// pc <- pc + ((rs1 != rs2) ? imm_b : 4)
TRACE("BNE %s, %s, %d\n", abiNames[ins->rs1_rs2_imm.rs1], abiNames[ins->rs1_rs2_imm.rs2], ins->rs1_rs2_imm.imm);
cpu->pc += ((cpu->xreg[ins->rs1_rs2_imm.rs1] != cpu->xreg[ins->rs1_rs2_imm.rs2]) ? ins->rs1_rs2_imm.imm : 4);
}
inline static void Exec_Blt(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// pc <- pc + ((rs1 < rs2) ? imm_b : 4)
TRACE("BLT %s, %s, %d\n", abiNames[ins->rs1_rs2_imm.rs1], abiNames[ins->rs1_rs2_imm.rs2], ins->rs1_rs2_imm.imm);
cpu->pc += (((int32_t)cpu->xreg[ins->rs1_rs2_imm.rs1] < (int32_t)cpu->xreg[ins->rs1_rs2_imm.rs2]) ? ins->rs1_rs2_imm.imm : 4);
}
inline static void Exec_Bge(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// pc <- pc + ((rs1 >= rs2) ? imm_b : 4)
TRACE("BGE %s, %s, %d\n", abiNames[ins->rs1_rs2_imm.rs1], abiNames[ins->rs1_rs2_imm.rs2], ins->rs1_rs2_imm.imm);
cpu->pc += (((int32_t)cpu->xreg[ins->rs1_rs2_imm.rs1] >= (int32_t)cpu->xreg[ins->rs1_rs2_imm.rs2]) ? ins->rs1_rs2_imm.imm : 4);
}
inline static void Exec_Bltu(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// pc <- pc + ((rs1 < rs2) ? imm_b : 4)
TRACE("BLTU %s, %s, %d\n", abiNames[ins->rs1_rs2_imm.rs1], abiNames[ins->rs1_rs2_imm.rs2], ins->rs1_rs2_imm.imm);
cpu->pc += ((cpu->xreg[ins->rs1_rs2_imm.rs1] < cpu->xreg[ins->rs1_rs2_imm.rs2]) ? ins->rs1_rs2_imm.imm : 4);
}
inline static void Exec_Bgeu(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// pc <- pc + ((rs1 >= rs2) ? imm_b : 4)
TRACE("BGEU %s, %s, %d\n", abiNames[ins->rs1_rs2_imm.rs1], abiNames[ins->rs1_rs2_imm.rs2], ins->rs1_rs2_imm.imm);
cpu->pc += ((cpu->xreg[ins->rs1_rs2_imm.rs1] >= cpu->xreg[ins->rs1_rs2_imm.rs2]) ? ins->rs1_rs2_imm.imm : 4);
}
inline static void Exec_Lb(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- sx(m8(rs1 + imm_i)), pc += 4
TRACE("LB %s, %d(%s)\n", abiNames[ins->rd_rs1_imm.rd], ins->rd_rs1_imm.imm, abiNames[ins->rd_rs1_imm.rs1]);
uint8_t byte = Read8(&cpu->bus, cpu->xreg[ins->rd_rs1_imm.rs1] + ins->rd_rs1_imm.imm, &cpu->busCode);
if (cpu->busCode != bcOK)
{
cpu->result = TakeTrap(cpu, ArvissMakeTrap(trLOAD_ACCESS_FAULT, cpu->xreg[ins->rd_rs1_imm.rs1] + ins->rd_rs1_imm.imm));
return;
}
cpu->xreg[ins->rd_rs1_imm.rd] = (int32_t)(int16_t)(int8_t)byte;
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Lh(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- sx(m16(rs1 + imm_i)), pc += 4
TRACE("LH %s, %d(%s)\n", abiNames[ins->rd_rs1_imm.rd], ins->rd_rs1_imm.imm, abiNames[ins->rd_rs1_imm.rs1]);
uint16_t halfword = Read16(&cpu->bus, cpu->xreg[ins->rd_rs1_imm.rs1] + ins->rd_rs1_imm.imm, &cpu->busCode);
if (cpu->busCode != bcOK)
{
cpu->result = TakeTrap(cpu, ArvissMakeTrap(trLOAD_ACCESS_FAULT, cpu->xreg[ins->rd_rs1_imm.rs1] + ins->rd_rs1_imm.imm));
return;
}
cpu->xreg[ins->rd_rs1_imm.rd] = (int32_t)(int16_t)halfword;
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Lw(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- sx(m32(rs1 + imm_i)), pc += 4
TRACE("LW %s, %d(%s)\n", abiNames[ins->rd_rs1_imm.rd], ins->rd_rs1_imm.imm, abiNames[ins->rd_rs1_imm.rs1]);
uint32_t word = Read32(&cpu->bus, cpu->xreg[ins->rd_rs1_imm.rs1] + ins->rd_rs1_imm.imm, &cpu->busCode);
if (cpu->busCode != bcOK)
{
cpu->result = TakeTrap(cpu, ArvissMakeTrap(trLOAD_ACCESS_FAULT, cpu->xreg[ins->rd_rs1_imm.rs1] + ins->rd_rs1_imm.imm));
return;
}
cpu->xreg[ins->rd_rs1_imm.rd] = (int32_t)word;
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Lbu(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- zx(m8(rs1 + imm_i)), pc += 4
TRACE("LBU x%d, %d(x%d)\n", ins->rd_rs1_imm.rd, ins->rd_rs1_imm.imm, ins->rd_rs1_imm.rs1);
uint8_t byte = Read8(&cpu->bus, cpu->xreg[ins->rd_rs1_imm.rs1] + ins->rd_rs1_imm.imm, &cpu->busCode);
if (cpu->busCode != bcOK)
{
cpu->result = TakeTrap(cpu, ArvissMakeTrap(trLOAD_ACCESS_FAULT, cpu->xreg[ins->rd_rs1_imm.rs1] + ins->rd_rs1_imm.imm));
return;
}
cpu->xreg[ins->rd_rs1_imm.rd] = byte;
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Lhu(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- zx(m16(rs1 + imm_i)), pc += 4
TRACE("LHU %s, %d(%s)\n", abiNames[ins->rd_rs1_imm.rd], ins->rd_rs1_imm.imm, abiNames[ins->rd_rs1_imm.rs1]);
uint16_t halfword = Read16(&cpu->bus, cpu->xreg[ins->rd_rs1_imm.rs1] + ins->rd_rs1_imm.imm, &cpu->busCode);
if (cpu->busCode != bcOK)
{
cpu->result = TakeTrap(cpu, ArvissMakeTrap(trLOAD_ACCESS_FAULT, cpu->xreg[ins->rd_rs1_imm.rs1] + ins->rd_rs1_imm.imm));
return;
}
cpu->xreg[ins->rd_rs1_imm.rd] = halfword;
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Sb(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// m8(rs1 + imm_s) <- rs2[7:0], pc += 4
TRACE("SB %s, %d(%s)\n", abiNames[ins->rs1_rs2_imm.rs2], ins->rs1_rs2_imm.imm, abiNames[ins->rs1_rs2_imm.rs1]);
Write8(&cpu->bus, cpu->xreg[ins->rs1_rs2_imm.rs1] + ins->rs1_rs2_imm.imm, cpu->xreg[ins->rs1_rs2_imm.rs2] & 0xff,
&cpu->busCode);
if (cpu->busCode != bcOK)
{
cpu->result = TakeTrap(cpu, ArvissMakeTrap(trSTORE_ACCESS_FAULT, cpu->xreg[ins->rs1_rs2_imm.rs1] + ins->rs1_rs2_imm.imm));
return;
}
cpu->pc += 4;
}
inline static void Exec_Sh(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// m16(rs1 + imm_s) <- rs2[15:0], pc += 4
TRACE("SH %s, %d(%s)\n", abiNames[ins->rs1_rs2_imm.rs2], ins->rs1_rs2_imm.imm, abiNames[ins->rs1_rs2_imm.rs1]);
Write16(&cpu->bus, cpu->xreg[ins->rs1_rs2_imm.rs1] + ins->rs1_rs2_imm.imm, cpu->xreg[ins->rs1_rs2_imm.rs2] & 0xffff,
&cpu->busCode);
if (cpu->busCode != bcOK)
{
cpu->result = TakeTrap(cpu, ArvissMakeTrap(trSTORE_ACCESS_FAULT, cpu->xreg[ins->rs1_rs2_imm.rs1] + ins->rs1_rs2_imm.imm));
return;
}
cpu->pc += 4;
}
inline static void Exec_Sw(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// m32(rs1 + imm_s) <- rs2[31:0], pc += 4
TRACE("SW %s, %d(%s)\n", abiNames[ins->rs1_rs2_imm.rs2], ins->rs1_rs2_imm.imm, abiNames[ins->rs1_rs2_imm.rs1]);
Write32(&cpu->bus, cpu->xreg[ins->rs1_rs2_imm.rs1] + ins->rs1_rs2_imm.imm, cpu->xreg[ins->rs1_rs2_imm.rs2], &cpu->busCode);
if (cpu->busCode != bcOK)
{
cpu->result = TakeTrap(cpu, ArvissMakeTrap(trSTORE_ACCESS_FAULT, cpu->xreg[ins->rs1_rs2_imm.rs1] + ins->rs1_rs2_imm.imm));
return;
}
cpu->pc += 4;
}
inline static void Exec_Addi(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- rs1 + imm_i, pc += 4
TRACE("ADDI %s, %s, %d\n", abiNames[ins->rd_rs1_imm.rd], abiNames[ins->rd_rs1_imm.rs1], ins->rd_rs1_imm.imm);
cpu->xreg[ins->rd_rs1_imm.rd] = cpu->xreg[ins->rd_rs1_imm.rs1] + ins->rd_rs1_imm.imm;
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Slti(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- (rs1 < imm_i) ? 1 : 0, pc += 4
TRACE("SLTI %s, %s, %d\n", abiNames[ins->rd_rs1_imm.rd], abiNames[ins->rd_rs1_imm.rs1], ins->rd_rs1_imm.imm);
cpu->xreg[ins->rd_rs1_imm.rd] = ((int32_t)cpu->xreg[ins->rd_rs1_imm.rs1] < ins->rd_rs1_imm.imm) ? 1 : 0;
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Sltiu(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- (rs1 < imm_i) ? 1 : 0, pc += 4
TRACE("SLTIU %s, %s, %d\n", abiNames[ins->rd_rs1_imm.rd], abiNames[ins->rd_rs1_imm.rs1], ins->rd_rs1_imm.imm);
cpu->xreg[ins->rd_rs1_imm.rd] = (cpu->xreg[ins->rd_rs1_imm.rs1] < (uint32_t)ins->rd_rs1_imm.imm) ? 1 : 0;
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Xori(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- rs1 ^ imm_i, pc += 4
TRACE("XORI %s, %s, %d\n", abiNames[ins->rd_rs1_imm.rd], abiNames[ins->rd_rs1_imm.rs1], ins->rd_rs1_imm.imm);
cpu->xreg[ins->rd_rs1_imm.rd] = cpu->xreg[ins->rd_rs1_imm.rs1] ^ ins->rd_rs1_imm.imm;
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Ori(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- rs1 | imm_i, pc += 4
TRACE("ORI %s, %s, %d\n", abiNames[ins->rd_rs1_imm.rd], abiNames[ins->rd_rs1_imm.rs1], ins->rd_rs1_imm.imm);
cpu->xreg[ins->rd_rs1_imm.rd] = cpu->xreg[ins->rd_rs1_imm.rs1] | ins->rd_rs1_imm.imm;
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Andi(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- rs1 & imm_i, pc += 4
TRACE("ANDI %s, %s, %d\n", abiNames[ins->rd_rs1_imm.rd], abiNames[ins->rd_rs1_imm.rs1], ins->rd_rs1_imm.imm);
cpu->xreg[ins->rd_rs1_imm.rd] = cpu->xreg[ins->rd_rs1_imm.rs1] & ins->rd_rs1_imm.imm;
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Slli(ArvissCpu* cpu, const DecodedInstruction* ins)
{
TRACE("SLLI %s, %s, %d\n", abiNames[ins->rd_rs1_imm.rd], abiNames[ins->rd_rs1_imm.rs1], ins->rd_rs1_imm.imm);
cpu->xreg[ins->rd_rs1_imm.rd] = cpu->xreg[ins->rd_rs1_imm.rs1] << ins->rd_rs1_imm.imm;
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Srli(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- rs1 >> shamt_i, pc += 4
TRACE("SRLI %s, %s, %d\n", abiNames[ins->rd_rs1_imm.rd], abiNames[ins->rd_rs1_imm.rs1], ins->rd_rs1_imm.imm);
cpu->xreg[ins->rd_rs1_imm.rd] = cpu->xreg[ins->rd_rs1_imm.rs1] >> ins->rd_rs1_imm.imm;
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Srai(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- sx(rs1) >> shamt_i, pc += 4
TRACE("SRAI %s, %s, %d\n", abiNames[ins->rd_rs1_imm.rd], abiNames[ins->rd_rs1_imm.rs1], ins->rd_rs1_imm.imm);
cpu->xreg[ins->rd_rs1_imm.rd] = (int32_t)cpu->xreg[ins->rd_rs1_imm.rs1] >> ins->rd_rs1_imm.imm;
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Add(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- rs1 + rs2, pc += 4
TRACE("ADD %s, %s, %s\n", abiNames[ins->rd_rs1_rs2.rd], abiNames[ins->rd_rs1_rs2.rs1], abiNames[ins->rd_rs1_rs2.rs2]);
cpu->xreg[ins->rd_rs1_rs2.rd] = cpu->xreg[ins->rd_rs1_rs2.rs1] + cpu->xreg[ins->rd_rs1_rs2.rs2];
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Sub(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- rs1 - rs2, pc += 4
TRACE("SUB %s, %s, %s\n", abiNames[ins->rd_rs1_rs2.rd], abiNames[ins->rd_rs1_rs2.rs1], abiNames[ins->rd_rs1_rs2.rs2]);
cpu->xreg[ins->rd_rs1_rs2.rd] = cpu->xreg[ins->rd_rs1_rs2.rs1] - cpu->xreg[ins->rd_rs1_rs2.rs2];
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Mul(ArvissCpu* cpu, const DecodedInstruction* ins)
{
TRACE("MUL %s, %s, %s\n", abiNames[ins->rd_rs1_rs2.rd], abiNames[ins->rd_rs1_rs2.rs1], abiNames[ins->rd_rs1_rs2.rs2]);
cpu->xreg[ins->rd_rs1_rs2.rd] = cpu->xreg[ins->rd_rs1_rs2.rs1] * cpu->xreg[ins->rd_rs1_rs2.rs2];
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Sll(ArvissCpu* cpu, const DecodedInstruction* ins)
{
// rd <- rs1 << (rs2 % XLEN), pc += 4
TRACE("SLL %s, %s, %s\n", abiNames[ins->rd_rs1_rs2.rd], abiNames[ins->rd_rs1_rs2.rs1], abiNames[ins->rd_rs1_rs2.rs2]);
cpu->xreg[ins->rd_rs1_rs2.rd] = cpu->xreg[ins->rd_rs1_rs2.rs1] << (cpu->xreg[ins->rd_rs1_rs2.rs2] % 32);
cpu->pc += 4;
cpu->xreg[0] = 0;
}
inline static void Exec_Mulh(ArvissCpu* cpu, const DecodedInstruction* ins)
{
TRACE("MULH %s, %s, %s\n", abiNames[ins->rd_rs1_rs2.rd], abiNames[ins->rd_rs1_rs2.rs1], abiNames[ins->rd_rs1_rs2.rs2]);
int64_t t = (int64_t)(int32_t)cpu->xreg[ins->rd_rs1_rs2.rs1] * (int64_t)(int32_t)cpu->xreg[ins->rd_rs1_rs2.rs2];
cpu->xreg[ins->rd_rs1_rs2.rd] = t >> 32;
cpu->pc += 4;
cpu->xreg[0] = 0;