1 #ifndef _RISCV_DECODE_H
2 #define _RISCV_DECODE_H
4 #define __STDC_LIMIT_MACROS
9 #ifdef RISCV_ENABLE_64BIT
10 # define support_64bit 1
12 # define support_64bit 0
15 #ifdef RISCV_ENABLE_FPU
22 typedef int int128_t
__attribute__((mode(TI
)));
23 typedef unsigned int uint128_t
__attribute__((mode(TI
)));
25 typedef int64_t sreg_t
;
26 typedef uint64_t reg_t
;
27 typedef uint64_t freg_t
;
29 const int OPCODE_BITS
= 7;
30 const int JTYPE_OPCODE_BITS
= 5;
32 const int GPR_BITS
= 8*sizeof(reg_t
);
33 const int GPRID_BITS
= 5;
34 const int NGPR
= 1 << GPRID_BITS
;
36 const int FPR_BITS
= 64;
37 const int FPRID_BITS
= 5;
38 const int NFPR
= 1 << FPRID_BITS
;
40 const int IMM_BITS
= 12;
41 const int TARGET_BITS
= 27;
42 const int SHAMT_BITS
= 6;
43 const int FUNCT_BITS
= 3;
44 const int FUNCTR_BITS
= 7;
45 const int FFUNCT_BITS
= 5;
46 const int BIGIMM_BITS
= 20;
47 const int BRANCH_ALIGN_BITS
= 1;
48 const int JUMP_ALIGN_BITS
= 1;
50 #define SR_ET 0x0000000000000001ULL
51 #define SR_PS 0x0000000000000004ULL
52 #define SR_S 0x0000000000000008ULL
53 #define SR_EF 0x0000000000000010ULL
54 #define SR_UX 0x0000000000000020ULL
55 #define SR_SX 0x0000000000000040ULL
56 #define SR_IM 0x000000000000FF00ULL
57 #define SR_ZERO ~(SR_ET | SR_PS | SR_S | SR_EF | SR_UX | SR_SX | SR_IM)
65 #define FSR_RD_SHIFT 5
66 #define FSR_RD (0x3 << FSR_RD_SHIFT)
74 #define FSR_AEXC_SHIFT 0
75 #define FSR_NVA (FPEXC_NV << FSR_AEXC_SHIFT)
76 #define FSR_OFA (FPEXC_OF << FSR_AEXC_SHIFT)
77 #define FSR_UFA (FPEXC_UF << FSR_AEXC_SHIFT)
78 #define FSR_DZA (FPEXC_DZ << FSR_AEXC_SHIFT)
79 #define FSR_NXA (FPEXC_NX << FSR_AEXC_SHIFT)
80 #define FSR_AEXC (FSR_NVA | FSR_OFA | FSR_UFA | FSR_DZA | FSR_NXA)
82 #define FSR_ZERO ~(FSR_RD | FSR_AEXC)
84 // note: bit fields are in little-endian order
87 signed imm12
: IMM_BITS
;
88 unsigned funct
: FUNCT_BITS
;
89 unsigned rs1
: GPRID_BITS
;
90 unsigned rdi
: GPRID_BITS
;
91 unsigned opcode
: OPCODE_BITS
;
96 unsigned target
: TARGET_BITS
;
97 unsigned jump_opcode
: JTYPE_OPCODE_BITS
;
102 unsigned rdr
: GPRID_BITS
;
103 unsigned functr
: FUNCTR_BITS
;
104 unsigned funct
: FUNCT_BITS
;
105 unsigned rs1
: GPRID_BITS
;
106 unsigned rs2
: GPRID_BITS
;
107 unsigned opcode
: OPCODE_BITS
;
112 unsigned bigimm
: BIGIMM_BITS
;
113 unsigned rdi
: GPRID_BITS
;
114 unsigned opcode
: OPCODE_BITS
;
119 unsigned rdr
: FPRID_BITS
;
120 unsigned rs3
: FPRID_BITS
;
121 unsigned ffunct
: FFUNCT_BITS
;
122 unsigned rs1
: FPRID_BITS
;
123 unsigned rs2
: FPRID_BITS
;
124 unsigned opcode
: OPCODE_BITS
;
139 class trace_writeback
142 trace_writeback(reg_t
* _rf
, int _rd
) : rf(_rf
), rd(_rd
) {}
144 reg_t
operator = (reg_t rhs
)
146 printf("R[%x] <= %llx\n",rd
,(long long)rhs
);
156 #define do_writeback(rf,rd) trace_writeback(rf,rd)
158 #define do_writeback(rf,rd) rf[rd]
161 // helpful macros, etc
162 #define RS1 R[insn.rtype.rs1]
163 #define RS2 R[insn.rtype.rs2]
164 #define RDR do_writeback(R,insn.rtype.rdr)
165 #define RDI do_writeback(R,insn.itype.rdi)
166 #define FRS1 FR[insn.ftype.rs1]
167 #define FRS2 FR[insn.ftype.rs2]
168 #define FRS3 FR[insn.ftype.rs3]
169 #define FRDR FR[insn.ftype.rdr]
170 #define FRDI FR[insn.itype.rdi]
171 #define BIGIMM insn.btype.bigimm
172 #define SIMM insn.itype.imm12
173 #define SHAMT (insn.itype.imm12 & 0x3F)
174 #define SHAMTW (insn.itype.imm12 & 0x1F)
175 #define TARGET insn.jtype.target
176 #define BRANCH_TARGET (npc + (SIMM << BRANCH_ALIGN_BITS))
177 #define JUMP_TARGET ((npc & ~((1<<(TARGET_BITS+JUMP_ALIGN_BITS))-1)) + (TARGET << JUMP_ALIGN_BITS))
178 #define RM ((insn.ftype.ffunct >> 1) & 3)
180 #define require_supervisor if(!(sr & SR_S)) throw trap_privileged_instruction
181 #define require64 if(gprlen != 64) throw trap_illegal_instruction
182 #define require_fp if(!(sr & SR_EF)) throw trap_fp_disabled
183 #define cmp_trunc(reg) (reg_t(reg) << (64-gprlen))
184 #define set_fp_exceptions ({ set_fsr(fsr | \
185 (softfloat_exceptionFlags << FSR_AEXC_SHIFT)); \
186 softfloat_exceptionFlags = 0; })
188 static inline sreg_t
sext32(int32_t arg
)