insn.h revision 1.2 1 1.2 skrll /* $NetBSD: insn.h,v 1.2 2020/11/04 07:09:45 skrll Exp $ */
2 1.2 skrll
3 1.1 matt /*-
4 1.1 matt * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 1.1 matt * All rights reserved.
6 1.1 matt *
7 1.1 matt * This code is derived from software contributed to The NetBSD Foundation
8 1.1 matt * by Matt Thomas of 3am Software Foundry.
9 1.1 matt *
10 1.1 matt * Redistribution and use in source and binary forms, with or without
11 1.1 matt * modification, are permitted provided that the following conditions
12 1.1 matt * are met:
13 1.1 matt * 1. Redistributions of source code must retain the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer.
15 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 matt * notice, this list of conditions and the following disclaimer in the
17 1.1 matt * documentation and/or other materials provided with the distribution.
18 1.1 matt *
19 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
30 1.1 matt */
31 1.1 matt
32 1.1 matt #ifndef _RISCV_INSN_H_
33 1.1 matt #define _RISCV_INSN_H_
34 1.1 matt
35 1.1 matt union riscv_insn {
36 1.1 matt uint32_t val;
37 1.1 matt struct {
38 1.1 matt unsigned int r_opcode : 7;
39 1.1 matt unsigned int r_rd : 5;
40 1.1 matt unsigned int r_funct3 : 3;
41 1.1 matt unsigned int r_rs1 : 5;
42 1.1 matt unsigned int r_rs2 : 5;
43 1.1 matt unsigned int r_funct7 : 7;
44 1.1 matt } type_r;
45 1.1 matt struct {
46 1.1 matt unsigned int rs_opcode : 7;
47 1.1 matt unsigned int rs_rd : 5;
48 1.1 matt unsigned int rs_funct3 : 3;
49 1.1 matt unsigned int rs_rs1 : 5;
50 1.1 matt unsigned int rs_shmat : 6;
51 1.1 matt unsigned int rs_funct6 : 6;
52 1.1 matt } type_rs;
53 1.1 matt struct {
54 1.1 matt unsigned int ra_opcode : 7;
55 1.1 matt unsigned int ra_rd : 5;
56 1.1 matt unsigned int ra_funct3 : 3;
57 1.1 matt unsigned int ra_rs1 : 5;
58 1.1 matt unsigned int ra_rs2 : 5;
59 1.1 matt unsigned int ra_rl : 1;
60 1.1 matt unsigned int ra_aq : 1;
61 1.1 matt unsigned int ra_funct5 : 6;
62 1.1 matt } type_ra;
63 1.1 matt struct {
64 1.1 matt unsigned int rf_opcode : 7;
65 1.1 matt unsigned int rf_rd : 5;
66 1.1 matt unsigned int rf_rm : 3;
67 1.1 matt unsigned int rf_rs1 : 5;
68 1.1 matt unsigned int rf_rs2 : 5;
69 1.1 matt unsigned int rf_funct2 : 2;
70 1.1 matt unsigned int rf_rs3 : 5;
71 1.1 matt } type_rf;
72 1.1 matt struct {
73 1.1 matt unsigned int i_opcode : 7;
74 1.1 matt unsigned int i_rd : 5;
75 1.1 matt unsigned int i_funct3 : 3;
76 1.1 matt unsigned int i_rs1 : 5;
77 1.1 matt signed int i_imm11to0 : 12;
78 1.1 matt } type_i;
79 1.1 matt struct {
80 1.1 matt unsigned int s_opcode : 7;
81 1.1 matt unsigned int s_imm4_to_0 : 5;
82 1.1 matt unsigned int s_funct3 : 3;
83 1.1 matt unsigned int s_rs1 : 5;
84 1.1 matt unsigned int s_rs2 : 5;
85 1.1 matt signed int s_imm11_to_5 : 7;
86 1.1 matt } type_s;
87 1.1 matt struct {
88 1.1 matt unsigned int sb_opcode : 7;
89 1.1 matt unsigned int sb_imm11 : 1;
90 1.1 matt unsigned int sb_imm4to1 : 4;
91 1.1 matt unsigned int sb_funct3 : 3;
92 1.1 matt unsigned int sb_rs1 : 5;
93 1.1 matt unsigned int sb_rs2 : 5;
94 1.1 matt unsigned int sb_imm10to5 : 6;
95 1.1 matt signed int sb_imm12 : 1;
96 1.1 matt } type_sb;
97 1.1 matt struct {
98 1.1 matt unsigned int u_opcode : 7;
99 1.1 matt unsigned int u_rd : 5;
100 1.1 matt signed int u_imm31to12 : 20;
101 1.1 matt } type_u;
102 1.1 matt struct {
103 1.1 matt unsigned int uj_opcode : 7;
104 1.1 matt unsigned int uj_rd : 5;
105 1.1 matt unsigned int uj_imm19to12 : 9;
106 1.1 matt unsigned int uj_imm11 : 1;
107 1.1 matt unsigned int uj_imm10to1 : 9;
108 1.1 matt signed int uj_imm20 : 1;
109 1.1 matt } type_uj;
110 1.1 matt };
111 1.1 matt
112 1.1 matt #define OPCODE_P(i, x) (((i) & 0b1111111) == ((OPCODE_##x<<2)|0b11))
113 1.1 matt
114 1.1 matt #define OPCODE_LOAD 0b00000
115 1.1 matt #define OPCODE_LOADFP 0b00001
116 1.1 matt #define OPCODE_CUSTOM0 0b00010
117 1.1 matt #define OPCODE_MISCMEM 0b00011
118 1.1 matt #define OPCODE_OPIMM 0b00100
119 1.1 matt #define OPCODE_AUIPC 0b00101
120 1.1 matt #define OPCODE_OPIMM32 0b00110
121 1.1 matt #define OPCODE_X48a 0b00111
122 1.1 matt
123 1.1 matt #define OPCODE_STORE 0b01000
124 1.1 matt #define OPCODE_STOREFP 0b01001
125 1.1 matt #define OPCODE_CUSTOM1 0b01010
126 1.1 matt #define OPCODE_AMO 0b01011
127 1.1 matt #define OPCODE_OP 0b01100
128 1.1 matt #define OPCODE_LUI 0b01101
129 1.1 matt #define OPCODE_OP32 0b01110
130 1.1 matt #define OPCODE_X64 0b01111
131 1.1 matt
132 1.1 matt #define OPCODE_MADD 0b10000 // FMADD.[S,D]
133 1.1 matt #define OPCODE_MSUB 0b10001 // FMSUB.[S,D]
134 1.1 matt #define OPCODE_NMSUB 0b10010 // FNMADD.[S,D]
135 1.1 matt #define OPCODE_NMADD 0b10011 // FNMSUB.[S,D]
136 1.1 matt #define OPCODE_OPFP 0b10100
137 1.1 matt #define OPCODE_rsvd21 0b10101
138 1.1 matt #define OPCODE_CUSTOM2 0b10110
139 1.1 matt #define OPCODE_X48b 0b10111
140 1.1 matt
141 1.1 matt #define OPCODE_BRANCH 0b11000
142 1.1 matt #define OPCODE_JALR 0b11001
143 1.1 matt #define OPCODE_rsvd26 0b11010
144 1.1 matt #define OPCODE_JAL 0b11011
145 1.1 matt #define OPCODE_SYSTEM 0b11100
146 1.1 matt #define OPCODE_rsvd29 0b11101
147 1.1 matt #define OPCODE_CUSTOM3 0b11110
148 1.1 matt #define OPCODE_X80 0b11111
149 1.1 matt
150 1.1 matt // LOAD (0x00000)
151 1.1 matt #define LOAD_LB 0b000
152 1.1 matt #define LOAD_LH 0b001
153 1.1 matt #define LOAD_LW 0b010
154 1.1 matt #define LOAD_LD 0b011 // RV64I
155 1.1 matt #define LOAD_LBU 0b100
156 1.1 matt #define LOAD_LHU 0b101
157 1.1 matt #define LOAD_LWU 0b110 // RV64I
158 1.1 matt
159 1.1 matt // LOADFP (0x00001)
160 1.1 matt #define LOADFP_FLW 0b010
161 1.1 matt #define LOADFP_FLD 0b011
162 1.1 matt
163 1.1 matt // MISCMEM (0x00010)
164 1.1 matt #define MISCMEM_FENCE 0b000
165 1.1 matt #define MISCMEM_FENCEI 0b001
166 1.1 matt
167 1.1 matt // OPIMM (0b00100) and OPIMM32 (0b00110) -- see OP (0b01100)
168 1.1 matt
169 1.1 matt // AUIPC (0b00101) - no functions
170 1.1 matt
171 1.1 matt // STORE (0b01000)
172 1.1 matt #define STORE_SB 0b000
173 1.1 matt #define STORE_SH 0b001
174 1.1 matt #define STORE_SW 0b010
175 1.1 matt #define STORE_SD 0b011 // RV64I
176 1.1 matt
177 1.1 matt // STOREFP (0b01001)
178 1.1 matt #define STOREFP_FSW 0b010
179 1.1 matt #define STOREFP_FSD 0b011
180 1.1 matt
181 1.1 matt // AMO (0b01011)
182 1.1 matt #define AMO_W 0b010
183 1.1 matt #define AMO_D 0b011
184 1.1 matt
185 1.1 matt // AMO funct5
186 1.1 matt #define AMO_ADD 0b00000
187 1.1 matt #define AMO_SWAP 0b00001
188 1.1 matt #define AMO_LR 0b00010
189 1.1 matt #define AMO_SC 0b00011
190 1.1 matt #define AMO_XOR 0b00100
191 1.1 matt #define AMO_OR 0b01000
192 1.1 matt #define AMO_AND 0b01100
193 1.1 matt #define AMO_MIN 0b10000
194 1.1 matt #define AMO_MAX 0b10100
195 1.1 matt #define AMO_MINU 0b11000
196 1.1 matt #define AMO_MAXU 0b11100
197 1.1 matt
198 1.1 matt // OPIMM (0b00100), OPIMM32 (0b00110), OP (0b01100), OP32 (0b01110)
199 1.1 matt #define OP_ADDSUB 0b000
200 1.1 matt #define OP_SLL 0b001
201 1.1 matt #define OP_SLT 0b010
202 1.1 matt #define OP_SLTU 0b011
203 1.1 matt #define OP_XOR 0b100
204 1.1 matt #define OP_SRX 0b101
205 1.1 matt #define OP_OR 0b110
206 1.1 matt #define OP_AND 0b111
207 1.1 matt
208 1.1 matt #define OP_FUNCT6_SRX_SRL 0b000000
209 1.1 matt #define OP_FUNCT6_SRX_SRA 0b010000
210 1.1 matt
211 1.1 matt #define OP_FUNCT7_ADD 0b0000000
212 1.1 matt #define OP_FUNCT7_SUB 0b0100000
213 1.1 matt
214 1.1 matt #define OP_MUL 0b000
215 1.1 matt #define OP_MULH 0b001
216 1.1 matt #define OP_MULHSU 0b010
217 1.1 matt #define OP_MULHU 0b011
218 1.1 matt #define OP_DIV 0b100
219 1.1 matt #define OP_DIVU 0b101
220 1.1 matt #define OP_REM 0b110
221 1.1 matt #define OP_REMU 0b111
222 1.1 matt
223 1.1 matt #define OP_FUNCT7_MULDIV 0b0000001
224 1.1 matt
225 1.1 matt // LUI (0b01101) - no functions
226 1.1 matt
227 1.1 matt // MADD (0b10000)
228 1.1 matt
229 1.1 matt #define MXXX_S 0b00
230 1.1 matt //#define MXXX_S 0b01
231 1.1 matt
232 1.1 matt // MSUB (0b10001)
233 1.1 matt // NMADD (0b10010)
234 1.1 matt // NMSUB (0b10011)
235 1.1 matt // OPFP (0b10100)
236 1.1 matt
237 1.1 matt #define OPFP_ADD 0b00000
238 1.1 matt #define OPFP_SUB 0b00001
239 1.1 matt #define OPFP_MUL 0b00010
240 1.1 matt #define OPFP_DIV 0b00011
241 1.1 matt #define OPFP_SGNJ 0b00100
242 1.1 matt #define OPFP_MINMAX 0b00101
243 1.1 matt #define OPFP_SQRT 0b01011
244 1.1 matt #define OPFP_CMP 0b10100
245 1.1 matt #define OPFP_CVT 0b11000
246 1.1 matt #define OPFP_MV 0b11100
247 1.1 matt #define OPFP_MV 0b11100
248 1.1 matt
249 1.1 matt #define SJGN_SGNJ 0b000
250 1.1 matt #define SJGN_SGNJN 0b001
251 1.1 matt #define SJGN_SGNJX 0b010
252 1.1 matt
253 1.1 matt // BRANCH (0b11000)
254 1.1 matt #define BRANCH_BEQ 0b000
255 1.1 matt #define BRANCH_BNE 0b001
256 1.1 matt #define BRANCH_BLT 0b100
257 1.1 matt #define BRANCH_BGE 0b101
258 1.1 matt #define BRANCH_BLTU 0b110
259 1.1 matt #define BRANCH_BGEU 0b111
260 1.1 matt
261 1.1 matt // JALR (0b11001) - no functions
262 1.1 matt // JAL (0b11011) - no functions
263 1.1 matt // SYSTEM (0b11100)
264 1.1 matt
265 1.1 matt #define SYSTEM_SFUNC 0b000
266 1.1 matt #define SYSTEM_RDREG 0b010
267 1.1 matt
268 1.1 matt #define SFUNC_RS_SCALL 0b00000
269 1.1 matt #define SFUNC_RS_SBREAK 0b00001
270 1.1 matt
271 1.1 matt #define RDREG_LO 0b1100000
272 1.1 matt #define RDREG_HI 0b1100100
273 1.1 matt #define RDREG_RS_CYCLE 0b00000
274 1.1 matt #define RDREG_RS_TIME 0b00001
275 1.1 matt #define RDREG_RS_INSTRET 0b00010
276 1.1 matt
277 1.1 matt #endif /* _RISCV_INSN_H_ */
278