8faa154bae9632a43a42e7b002c9d0f9f539a935
[openocd.git] / src / target / riscv / opcodes.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include "encoding.h"
4
5 #define ZERO 0
6 #define T0 5
7 #define S0 8
8 #define S1 9
9
10 static uint32_t bits(uint32_t value, unsigned int hi, unsigned int lo)
11 {
12 return (value >> lo) & ((1 << (hi+1-lo)) - 1);
13 }
14
15 static uint32_t bit(uint32_t value, unsigned int b)
16 {
17 return (value >> b) & 1;
18 }
19
20 static uint32_t inst_rd(uint32_t r) __attribute__ ((unused));
21 static uint32_t inst_rd(uint32_t r)
22 {
23 return bits(r, 4, 0) << 7;
24 }
25
26 static uint32_t inst_rs1(uint32_t r) __attribute__ ((unused));
27 static uint32_t inst_rs1(uint32_t r)
28 {
29 return bits(r, 4, 0) << 15;
30 }
31
32 static uint32_t inst_rs2(uint32_t r) __attribute__ ((unused));
33 static uint32_t inst_rs2(uint32_t r)
34 {
35 return bits(r, 4, 0) << 20;
36 }
37
38 static uint32_t imm_i(uint32_t imm) __attribute__ ((unused));
39 static uint32_t imm_i(uint32_t imm)
40 {
41 return bits(imm, 11, 0) << 20;
42 }
43
44 static uint32_t imm_s(uint32_t imm) __attribute__ ((unused));
45 static uint32_t imm_s(uint32_t imm)
46 {
47 return (bits(imm, 4, 0) << 7) | (bits(imm, 11, 5) << 25);
48 }
49
50 static uint32_t imm_b(uint32_t imm) __attribute__ ((unused));
51 static uint32_t imm_b(uint32_t imm)
52 {
53 return (bit(imm, 11) << 7) | (bits(imm, 4, 1) << 8) | (bits(imm, 10, 5) << 25) | (bit(imm, 12) << 31);
54 }
55
56 static uint32_t imm_u(uint32_t imm) __attribute__ ((unused));
57 static uint32_t imm_u(uint32_t imm)
58 {
59 return bits(imm, 31, 12) << 12;
60 }
61
62 static uint32_t imm_j(uint32_t imm) __attribute__ ((unused));
63 static uint32_t imm_j(uint32_t imm)
64 {
65 return (bits(imm, 19, 12) << 12) | (bit(imm, 11) << 20) | (bits(imm, 10, 1) << 21) | (bit(imm, 20) << 31);
66 }
67
68 static uint32_t jal(unsigned int rd, uint32_t imm) __attribute__ ((unused));
69 static uint32_t jal(unsigned int rd, uint32_t imm)
70 {
71 return imm_j(imm) | inst_rd(rd) | MATCH_JAL;
72 }
73
74 static uint32_t csrsi(unsigned int csr, uint16_t imm) __attribute__ ((unused));
75 static uint32_t csrsi(unsigned int csr, uint16_t imm)
76 {
77 return imm_i(csr) | inst_rs1(imm) | MATCH_CSRRSI;
78 }
79
80 static uint32_t sw(unsigned int src, unsigned int base, uint16_t offset) __attribute__ ((unused));
81 static uint32_t sw(unsigned int src, unsigned int base, uint16_t offset)
82 {
83 return imm_s(offset) | inst_rs2(src) | inst_rs1(base) | MATCH_SW;
84 }
85
86 static uint32_t sd(unsigned int src, unsigned int base, uint16_t offset) __attribute__ ((unused));
87 static uint32_t sd(unsigned int src, unsigned int base, uint16_t offset)
88 {
89 return imm_s(offset) | inst_rs2(src) | inst_rs1(base) | MATCH_SD;
90 }
91
92 static uint32_t sh(unsigned int src, unsigned int base, uint16_t offset) __attribute__ ((unused));
93 static uint32_t sh(unsigned int src, unsigned int base, uint16_t offset)
94 {
95 return imm_s(offset) | inst_rs2(src) | inst_rs1(base) | MATCH_SH;
96 }
97
98 static uint32_t sb(unsigned int src, unsigned int base, uint16_t offset) __attribute__ ((unused));
99 static uint32_t sb(unsigned int src, unsigned int base, uint16_t offset)
100 {
101 return imm_s(offset) | inst_rs2(src) | inst_rs1(base) | MATCH_SB;
102 }
103
104 static uint32_t ld(unsigned int rd, unsigned int base, uint16_t offset) __attribute__ ((unused));
105 static uint32_t ld(unsigned int rd, unsigned int base, uint16_t offset)
106 {
107 return imm_i(offset) | inst_rs1(base) | inst_rd(rd) | MATCH_LD;
108 }
109
110 static uint32_t lw(unsigned int rd, unsigned int base, uint16_t offset) __attribute__ ((unused));
111 static uint32_t lw(unsigned int rd, unsigned int base, uint16_t offset)
112 {
113 return imm_i(offset) | inst_rs1(base) | inst_rd(rd) | MATCH_LW;
114 }
115
116 static uint32_t lh(unsigned int rd, unsigned int base, uint16_t offset) __attribute__ ((unused));
117 static uint32_t lh(unsigned int rd, unsigned int base, uint16_t offset)
118 {
119 return imm_i(offset) | inst_rs1(base) | inst_rd(rd) | MATCH_LH;
120 }
121
122 static uint32_t lb(unsigned int rd, unsigned int base, uint16_t offset) __attribute__ ((unused));
123 static uint32_t lb(unsigned int rd, unsigned int base, uint16_t offset)
124 {
125 return imm_i(offset) | inst_rs1(base) | inst_rd(rd) | MATCH_LB;
126 }
127
128 static uint32_t csrw(unsigned int source, unsigned int csr) __attribute__ ((unused));
129 static uint32_t csrw(unsigned int source, unsigned int csr)
130 {
131 return imm_i(csr) | inst_rs1(source) | MATCH_CSRRW;
132 }
133
134 static uint32_t addi(unsigned int dest, unsigned int src, uint16_t imm) __attribute__ ((unused));
135 static uint32_t addi(unsigned int dest, unsigned int src, uint16_t imm)
136 {
137 return imm_i(imm) | inst_rs1(src) | inst_rd(dest) | MATCH_ADDI;
138 }
139
140 static uint32_t csrr(unsigned int rd, unsigned int csr) __attribute__ ((unused));
141 static uint32_t csrr(unsigned int rd, unsigned int csr)
142 {
143 return imm_i(csr) | inst_rd(rd) | MATCH_CSRRS;
144 }
145
146 static uint32_t csrrs(unsigned int rd, unsigned int rs, unsigned int csr) __attribute__ ((unused));
147 static uint32_t csrrs(unsigned int rd, unsigned int rs, unsigned int csr)
148 {
149 return imm_i(csr) | inst_rs1(rs) | inst_rd(rd) | MATCH_CSRRS;
150 }
151
152 static uint32_t csrrw(unsigned int rd, unsigned int rs, unsigned int csr) __attribute__ ((unused));
153 static uint32_t csrrw(unsigned int rd, unsigned int rs, unsigned int csr)
154 {
155 return imm_i(csr) | inst_rs1(rs) | inst_rd(rd) | MATCH_CSRRW;
156 }
157
158 static uint32_t csrrci(unsigned int rd, unsigned int zimm, unsigned int csr) __attribute__ ((unused));
159 static uint32_t csrrci(unsigned int rd, unsigned int zimm, unsigned int csr)
160 {
161 return imm_i(csr) | inst_rs1(zimm) | inst_rd(rd) | MATCH_CSRRCI;
162 }
163
164 static uint32_t csrrsi(unsigned int rd, unsigned int zimm, unsigned int csr) __attribute__ ((unused));
165 static uint32_t csrrsi(unsigned int rd, unsigned int zimm, unsigned int csr)
166 {
167 return imm_i(csr) | inst_rs1(zimm) | inst_rd(rd) | MATCH_CSRRSI;
168 }
169
170 static uint32_t fsw(unsigned int src, unsigned int base, uint16_t offset) __attribute__ ((unused));
171 static uint32_t fsw(unsigned int src, unsigned int base, uint16_t offset)
172 {
173 return imm_s(offset) | inst_rs2(src) | inst_rs1(base) | MATCH_FSW;
174 }
175
176 static uint32_t fsd(unsigned int src, unsigned int base, uint16_t offset) __attribute__ ((unused));
177 static uint32_t fsd(unsigned int src, unsigned int base, uint16_t offset)
178 {
179 return imm_s(offset) | inst_rs2(src) | inst_rs1(base) | MATCH_FSD;
180 }
181
182 static uint32_t flw(unsigned int dest, unsigned int base, uint16_t offset) __attribute__ ((unused));
183 static uint32_t flw(unsigned int dest, unsigned int base, uint16_t offset)
184 {
185 return imm_i(offset) | inst_rs1(base) | inst_rd(dest) | MATCH_FLW;
186 }
187
188 static uint32_t fld(unsigned int dest, unsigned int base, uint16_t offset) __attribute__ ((unused));
189 static uint32_t fld(unsigned int dest, unsigned int base, uint16_t offset)
190 {
191 return imm_i(offset) | inst_rs1(base) | inst_rd(dest) | MATCH_FLD;
192 }
193
194 static uint32_t fmv_x_w(unsigned dest, unsigned src) __attribute__ ((unused));
195 static uint32_t fmv_x_w(unsigned dest, unsigned src)
196 {
197 return inst_rs1(src) | inst_rd(dest) | MATCH_FMV_X_W;
198 }
199
200 static uint32_t fmv_x_d(unsigned dest, unsigned src) __attribute__ ((unused));
201 static uint32_t fmv_x_d(unsigned dest, unsigned src)
202 {
203 return inst_rs1(src) | inst_rd(dest) | MATCH_FMV_X_D;
204 }
205
206 static uint32_t fmv_w_x(unsigned dest, unsigned src) __attribute__ ((unused));
207 static uint32_t fmv_w_x(unsigned dest, unsigned src)
208 {
209 return inst_rs1(src) | inst_rd(dest) | MATCH_FMV_W_X;
210 }
211
212 static uint32_t fmv_d_x(unsigned dest, unsigned src) __attribute__ ((unused));
213 static uint32_t fmv_d_x(unsigned dest, unsigned src)
214 {
215 return inst_rs1(src) | inst_rd(dest) | MATCH_FMV_D_X;
216 }
217
218 static uint32_t ebreak(void) __attribute__ ((unused));
219 static uint32_t ebreak(void)
220 {
221 return MATCH_EBREAK;
222 }
223 static uint32_t ebreak_c(void) __attribute__ ((unused));
224 static uint32_t ebreak_c(void)
225 {
226 return MATCH_C_EBREAK;
227 }
228
229 static uint32_t wfi(void) __attribute__ ((unused));
230 static uint32_t wfi(void) { return MATCH_WFI; }
231
232 static uint32_t fence_i(void) __attribute__ ((unused));
233 static uint32_t fence_i(void)
234 {
235 return MATCH_FENCE_I;
236 }
237
238 static uint32_t lui(unsigned int dest, uint32_t imm) __attribute__ ((unused));
239 static uint32_t lui(unsigned int dest, uint32_t imm)
240 {
241 return imm_u(imm) | inst_rd(dest) | MATCH_LUI;
242 }
243
244 /*
245 static uint32_t csrci(unsigned int csr, uint16_t imm) __attribute__ ((unused));
246 static uint32_t csrci(unsigned int csr, uint16_t imm)
247 {
248 return (csr << 20) |
249 (bits(imm, 4, 0) << 15) |
250 MATCH_CSRRCI;
251 }
252
253 static uint32_t li(unsigned int dest, uint16_t imm) __attribute__ ((unused));
254 static uint32_t li(unsigned int dest, uint16_t imm)
255 {
256 return addi(dest, 0, imm);
257 }
258
259 static uint32_t fsd(unsigned int src, unsigned int base, uint16_t offset) __attribute__ ((unused));
260 static uint32_t fsd(unsigned int src, unsigned int base, uint16_t offset)
261 {
262 return (bits(offset, 11, 5) << 25) |
263 (bits(src, 4, 0) << 20) |
264 (base << 15) |
265 (bits(offset, 4, 0) << 7) |
266 MATCH_FSD;
267 }
268
269 static uint32_t ori(unsigned int dest, unsigned int src, uint16_t imm) __attribute__ ((unused));
270 static uint32_t ori(unsigned int dest, unsigned int src, uint16_t imm)
271 {
272 return (bits(imm, 11, 0) << 20) |
273 (src << 15) |
274 (dest << 7) |
275 MATCH_ORI;
276 }
277
278 static uint32_t nop(void) __attribute__ ((unused));
279 static uint32_t nop(void)
280 {
281 return addi(0, 0, 0);
282 }
283 */
284
285 static uint32_t xori(unsigned int dest, unsigned int src, uint16_t imm) __attribute__ ((unused));
286 static uint32_t xori(unsigned int dest, unsigned int src, uint16_t imm)
287 {
288 return imm_i(imm) | inst_rs1(src) | inst_rd(dest) | MATCH_XORI;
289 }
290
291 static uint32_t srli(unsigned int dest, unsigned int src, uint8_t shamt) __attribute__ ((unused));
292 static uint32_t srli(unsigned int dest, unsigned int src, uint8_t shamt)
293 {
294 return inst_rs2(shamt) | inst_rs1(src) | inst_rd(dest) | MATCH_SRLI;
295 }
296
297 static uint32_t fence(void) __attribute__((unused));
298 static uint32_t fence(void)
299 {
300 return MATCH_FENCE;
301 }
302
303 static uint32_t auipc(unsigned int dest) __attribute__((unused));
304 static uint32_t auipc(unsigned int dest)
305 {
306 return MATCH_AUIPC | inst_rd(dest);
307 }
308
309 static uint32_t vsetvli(unsigned int dest, unsigned int src, uint16_t imm) __attribute__((unused));
310 static uint32_t vsetvli(unsigned int dest, unsigned int src, uint16_t imm)
311 {
312 return (bits(imm, 10, 0) << 20) | inst_rs1(src) | inst_rd(dest) | MATCH_VSETVLI;
313 }
314
315 static uint32_t vmv_x_s(unsigned int rd, unsigned int vs2) __attribute__((unused));
316 static uint32_t vmv_x_s(unsigned int rd, unsigned int vs2)
317 {
318 return inst_rs2(vs2) | inst_rd(rd) | MATCH_VMV_X_S;
319 }
320
321 static uint32_t vmv_s_x(unsigned int vd, unsigned int vs2) __attribute__((unused));
322 static uint32_t vmv_s_x(unsigned int vd, unsigned int rs1)
323 {
324 return inst_rs1(rs1) | inst_rd(vd) | MATCH_VMV_S_X;
325 }
326
327 static uint32_t vslide1down_vx(unsigned int vd, unsigned int vs2,
328 unsigned int rs1, unsigned int vm) __attribute__((unused));
329 static uint32_t vslide1down_vx(unsigned int vd, unsigned int vs2,
330 unsigned int rs1, unsigned int vm)
331 {
332 return ((vm & 1) << 25) | inst_rs2(vs2) | inst_rs1(rs1) | inst_rd(vd) | MATCH_VSLIDE1DOWN_VX;
333 }
334

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)