Rip out RVC for now
[riscv-isa-sim.git] / riscv / decode.h
1 // See LICENSE for license details.
2
3 #ifndef _RISCV_DECODE_H
4 #define _RISCV_DECODE_H
5
6 #define __STDC_LIMIT_MACROS
7 #include <stdint.h>
8 #include <string.h>
9 #include "pcr.h"
10 #include "config.h"
11
12 typedef int int128_t __attribute__((mode(TI)));
13 typedef unsigned int uint128_t __attribute__((mode(TI)));
14
15 typedef int64_t sreg_t;
16 typedef uint64_t reg_t;
17 typedef uint64_t freg_t;
18
19 const int OPCODE_BITS = 7;
20
21 const int XPRID_BITS = 5;
22 const int NXPR = 1 << XPRID_BITS;
23
24 const int FPR_BITS = 64;
25 const int FPRID_BITS = 5;
26 const int NFPR = 1 << FPRID_BITS;
27
28 const int IMM_BITS = 12;
29 const int IMMLO_BITS = 7;
30 const int TARGET_BITS = 25;
31 const int FUNCT_BITS = 3;
32 const int FUNCTR_BITS = 7;
33 const int FFUNCT_BITS = 2;
34 const int RM_BITS = 3;
35 const int BIGIMM_BITS = 20;
36 const int BRANCH_ALIGN_BITS = 1;
37 const int JUMP_ALIGN_BITS = 1;
38
39 #define FP_RD_NE 0
40 #define FP_RD_0 1
41 #define FP_RD_DN 2
42 #define FP_RD_UP 3
43 #define FP_RD_NMM 4
44
45 #define FSR_RD_SHIFT 5
46 #define FSR_RD (0x7 << FSR_RD_SHIFT)
47
48 #define FPEXC_NX 0x01
49 #define FPEXC_UF 0x02
50 #define FPEXC_OF 0x04
51 #define FPEXC_DZ 0x08
52 #define FPEXC_NV 0x10
53
54 #define FSR_AEXC_SHIFT 0
55 #define FSR_NVA (FPEXC_NV << FSR_AEXC_SHIFT)
56 #define FSR_OFA (FPEXC_OF << FSR_AEXC_SHIFT)
57 #define FSR_UFA (FPEXC_UF << FSR_AEXC_SHIFT)
58 #define FSR_DZA (FPEXC_DZ << FSR_AEXC_SHIFT)
59 #define FSR_NXA (FPEXC_NX << FSR_AEXC_SHIFT)
60 #define FSR_AEXC (FSR_NVA | FSR_OFA | FSR_UFA | FSR_DZA | FSR_NXA)
61
62 #define FSR_ZERO ~(FSR_RD | FSR_AEXC)
63
64 // note: bit fields are in little-endian order
65 struct itype_t
66 {
67 unsigned opcode : OPCODE_BITS;
68 unsigned funct : FUNCT_BITS;
69 signed imm12 : IMM_BITS;
70 unsigned rs1 : XPRID_BITS;
71 unsigned rd : XPRID_BITS;
72 };
73
74 struct btype_t
75 {
76 unsigned opcode : OPCODE_BITS;
77 unsigned funct : FUNCT_BITS;
78 unsigned immlo : IMMLO_BITS;
79 unsigned rs2 : XPRID_BITS;
80 unsigned rs1 : XPRID_BITS;
81 signed immhi : IMM_BITS-IMMLO_BITS;
82 };
83
84 struct jtype_t
85 {
86 unsigned jump_opcode : OPCODE_BITS;
87 signed target : TARGET_BITS;
88 };
89
90 struct rtype_t
91 {
92 unsigned opcode : OPCODE_BITS;
93 unsigned funct : FUNCT_BITS;
94 unsigned functr : FUNCTR_BITS;
95 unsigned rs2 : XPRID_BITS;
96 unsigned rs1 : XPRID_BITS;
97 unsigned rd : XPRID_BITS;
98 };
99
100 struct ltype_t
101 {
102 unsigned opcode : OPCODE_BITS;
103 unsigned bigimm : BIGIMM_BITS;
104 unsigned rd : XPRID_BITS;
105 };
106
107 struct ftype_t
108 {
109 unsigned opcode : OPCODE_BITS;
110 unsigned ffunct : FFUNCT_BITS;
111 unsigned rm : RM_BITS;
112 unsigned rs3 : FPRID_BITS;
113 unsigned rs2 : FPRID_BITS;
114 unsigned rs1 : FPRID_BITS;
115 unsigned rd : FPRID_BITS;
116 };
117
118 union insn_t
119 {
120 itype_t itype;
121 jtype_t jtype;
122 rtype_t rtype;
123 btype_t btype;
124 ltype_t ltype;
125 ftype_t ftype;
126 uint_fast32_t bits;
127 };
128
129 template <class T>
130 class write_port_t
131 {
132 public:
133 write_port_t(T& _t) : t(_t) {}
134 T& operator = (const T& rhs)
135 {
136 return t = rhs;
137 }
138 operator T()
139 {
140 return t;
141 }
142 private:
143 T& t;
144 };
145 template <class T, size_t N, bool zero_reg>
146 class regfile_t
147 {
148 public:
149 void reset()
150 {
151 memset(data, 0, sizeof(data));
152 }
153 write_port_t<T> write_port(size_t i)
154 {
155 if (zero_reg)
156 const_cast<T&>(data[0]) = 0;
157 return write_port_t<T>(data[i]);
158 }
159 const T& operator [] (size_t i) const
160 {
161 if (zero_reg)
162 const_cast<T&>(data[0]) = 0;
163 return data[i];
164 }
165 private:
166 T data[N];
167 };
168
169 #define throw_illegal_instruction \
170 ({ if (utmode) throw trap_vector_illegal_instruction; \
171 else throw trap_illegal_instruction; })
172
173 // helpful macros, etc
174 #define RS1 XPR[insn.rtype.rs1]
175 #define RS2 XPR[insn.rtype.rs2]
176 #define RD XPR.write_port(insn.rtype.rd)
177 #define RA XPR.write_port(1)
178 #define FRS1 FPR[insn.ftype.rs1]
179 #define FRS2 FPR[insn.ftype.rs2]
180 #define FRS3 FPR[insn.ftype.rs3]
181 #define FRD FPR.write_port(insn.ftype.rd)
182 #define BIGIMM insn.ltype.bigimm
183 #define SIMM insn.itype.imm12
184 #define BIMM ((signed)insn.btype.immlo | (insn.btype.immhi << IMMLO_BITS))
185 #define SHAMT (insn.itype.imm12 & 0x3F)
186 #define SHAMTW (insn.itype.imm12 & 0x1F)
187 #define TARGET insn.jtype.target
188 #define BRANCH_TARGET (pc + (BIMM << BRANCH_ALIGN_BITS))
189 #define JUMP_TARGET (pc + (TARGET << JUMP_ALIGN_BITS))
190 #define ITYPE_EADDR sext_xprlen(RS1 + SIMM)
191 #define BTYPE_EADDR sext_xprlen(RS1 + BIMM)
192 #define RM ({ int rm = insn.ftype.rm; \
193 if(rm == 7) rm = (fsr & FSR_RD) >> FSR_RD_SHIFT; \
194 if(rm > 4) throw_illegal_instruction; \
195 rm; })
196
197 #define xpr64 (xprlen == 64)
198
199 #define require_supervisor if(unlikely(!(sr & SR_S))) throw trap_privileged_instruction
200 #define require_xpr64 if(unlikely(!xpr64)) throw_illegal_instruction
201 #define require_xpr32 if(unlikely(xpr64)) throw_illegal_instruction
202 #ifndef RISCV_ENABLE_FPU
203 # define require_fp throw trap_illegal_instruction
204 #else
205 # define require_fp if(unlikely(!(sr & SR_EF))) throw trap_fp_disabled
206 #endif
207 #ifndef RISCV_ENABLE_VEC
208 # define require_vector throw trap_illegal_instruction
209 #else
210 # define require_vector \
211 ({ if(!(sr & SR_EV)) throw trap_vector_disabled; \
212 else if (!utmode && (vecbanks_count < 3)) throw trap_vector_bank; \
213 })
214 #endif
215
216 #define cmp_trunc(reg) (reg_t(reg) << (64-xprlen))
217 #define set_fp_exceptions ({ set_fsr(fsr | \
218 (softfloat_exceptionFlags << FSR_AEXC_SHIFT)); \
219 softfloat_exceptionFlags = 0; })
220
221 #define sext32(x) ((sreg_t)(int32_t)(x))
222 #define zext32(x) ((reg_t)(uint32_t)(x))
223 #define sext_xprlen(x) (((sreg_t)(x) << (64-xprlen)) >> (64-xprlen))
224 #define zext_xprlen(x) (((reg_t)(x) << (64-xprlen)) >> (64-xprlen))
225
226 #define insn_length(x) \
227 (((x) & 0x03) < 0x03 ? 2 : \
228 ((x) & 0x1f) < 0x1f ? 4 : \
229 ((x) & 0x3f) < 0x3f ? 6 : \
230 8)
231
232 #define set_pc(x) \
233 do { if ((x) & 3 /* For now... */) \
234 throw trap_instruction_address_misaligned; \
235 npc = (x); \
236 } while(0)
237
238 // vector stuff
239 #define VL vl
240
241 #define UT_RS1(idx) uts[idx]->XPR[insn.rtype.rs1]
242 #define UT_RS2(idx) uts[idx]->XPR[insn.rtype.rs2]
243 #define UT_RD(idx) uts[idx]->XPR.write_port(insn.rtype.rd)
244 #define UT_RA(idx) uts[idx]->XPR.write_port(1)
245 #define UT_FRS1(idx) uts[idx]->FPR[insn.ftype.rs1]
246 #define UT_FRS2(idx) uts[idx]->FPR[insn.ftype.rs2]
247 #define UT_FRS3(idx) uts[idx]->FPR[insn.ftype.rs3]
248 #define UT_FRD(idx) uts[idx]->FPR.write_port(insn.ftype.rd)
249 #define UT_RM(idx) ((insn.ftype.rm != 7) ? insn.ftype.rm : \
250 ((uts[idx]->fsr & FSR_RD) >> FSR_RD_SHIFT))
251
252 #define UT_LOOP_START for (int i=0;i<VL; i++) {
253 #define UT_LOOP_END }
254 #define UT_LOOP_RS1 UT_RS1(i)
255 #define UT_LOOP_RS2 UT_RS2(i)
256 #define UT_LOOP_RD UT_RD(i)
257 #define UT_LOOP_RA UT_RA(i)
258 #define UT_LOOP_FRS1 UT_FRS1(i)
259 #define UT_LOOP_FRS2 UT_FRS2(i)
260 #define UT_LOOP_FRS3 UT_FRS3(i)
261 #define UT_LOOP_FRD UT_FRD(i)
262 #define UT_LOOP_RM UT_RM(i)
263
264 #define VEC_LOAD(dst, func, inc) \
265 reg_t addr = RS1; \
266 UT_LOOP_START \
267 UT_LOOP_##dst = mmu.func(addr); \
268 addr += inc; \
269 UT_LOOP_END
270
271 #define VEC_STORE(src, func, inc) \
272 reg_t addr = RS1; \
273 UT_LOOP_START \
274 mmu.func(addr, UT_LOOP_##src); \
275 addr += inc; \
276 UT_LOOP_END
277
278 enum vt_command_t
279 {
280 vt_command_stop,
281 };
282
283 #endif