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