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