riscv-isa-sim
opcodes.h
1 #include "encoding.h"
2 
3 #define ZERO 0
4 #define T0 5
5 #define S0 8
6 #define S1 9
7 
8 static uint32_t bits(uint32_t value, unsigned int hi, unsigned int lo) {
9  return (value >> lo) & ((1 << (hi+1-lo)) - 1);
10 }
11 
12 static uint32_t bit(uint32_t value, unsigned int b) {
13  return (value >> b) & 1;
14 }
15 
16 static uint32_t jal(unsigned int rd, uint32_t imm) __attribute__ ((unused));
17 static uint32_t jal(unsigned int rd, uint32_t imm) {
18  return (bit(imm, 20) << 31) |
19  (bits(imm, 10, 1) << 21) |
20  (bit(imm, 11) << 20) |
21  (bits(imm, 19, 12) << 12) |
22  (rd << 7) |
23  MATCH_JAL;
24 }
25 
26 static uint32_t csrsi(unsigned int csr, uint16_t imm) __attribute__ ((unused));
27 static uint32_t csrsi(unsigned int csr, uint16_t imm) {
28  return (csr << 20) |
29  (bits(imm, 4, 0) << 15) |
30  MATCH_CSRRSI;
31 }
32 
33 static uint32_t sw(unsigned int src, unsigned int base, uint16_t offset) __attribute__ ((unused));
34 static uint32_t sw(unsigned int src, unsigned int base, uint16_t offset)
35 {
36  return (bits(offset, 11, 5) << 25) |
37  (src << 20) |
38  (base << 15) |
39  (bits(offset, 4, 0) << 7) |
40  MATCH_SW;
41 }
42 
43 static uint32_t sd(unsigned int src, unsigned int base, uint16_t offset) __attribute__ ((unused));
44 static uint32_t sd(unsigned int src, unsigned int base, uint16_t offset)
45 {
46  return (bits(offset, 11, 5) << 25) |
47  (src << 20) |
48  (base << 15) |
49  (bits(offset, 4, 0) << 7) |
50  MATCH_SD;
51 }
52 
53 static uint32_t sh(unsigned int src, unsigned int base, uint16_t offset) __attribute__ ((unused));
54 static uint32_t sh(unsigned int src, unsigned int base, uint16_t offset)
55 {
56  return (bits(offset, 11, 5) << 25) |
57  (src << 20) |
58  (base << 15) |
59  (bits(offset, 4, 0) << 7) |
60  MATCH_SH;
61 }
62 
63 static uint32_t sb(unsigned int src, unsigned int base, uint16_t offset) __attribute__ ((unused));
64 static uint32_t sb(unsigned int src, unsigned int base, uint16_t offset)
65 {
66  return (bits(offset, 11, 5) << 25) |
67  (src << 20) |
68  (base << 15) |
69  (bits(offset, 4, 0) << 7) |
70  MATCH_SB;
71 }
72 
73 static uint32_t ld(unsigned int rd, unsigned int base, uint16_t offset) __attribute__ ((unused));
74 static uint32_t ld(unsigned int rd, unsigned int base, uint16_t offset)
75 {
76  return (bits(offset, 11, 0) << 20) |
77  (base << 15) |
78  (bits(rd, 4, 0) << 7) |
79  MATCH_LD;
80 }
81 
82 static uint32_t lw(unsigned int rd, unsigned int base, uint16_t offset) __attribute__ ((unused));
83 static uint32_t lw(unsigned int rd, unsigned int base, uint16_t offset)
84 {
85  return (bits(offset, 11, 0) << 20) |
86  (base << 15) |
87  (bits(rd, 4, 0) << 7) |
88  MATCH_LW;
89 }
90 
91 static uint32_t lh(unsigned int rd, unsigned int base, uint16_t offset) __attribute__ ((unused));
92 static uint32_t lh(unsigned int rd, unsigned int base, uint16_t offset)
93 {
94  return (bits(offset, 11, 0) << 20) |
95  (base << 15) |
96  (bits(rd, 4, 0) << 7) |
97  MATCH_LH;
98 }
99 
100 static uint32_t lb(unsigned int rd, unsigned int base, uint16_t offset) __attribute__ ((unused));
101 static uint32_t lb(unsigned int rd, unsigned int base, uint16_t offset)
102 {
103  return (bits(offset, 11, 0) << 20) |
104  (base << 15) |
105  (bits(rd, 4, 0) << 7) |
106  MATCH_LB;
107 }
108 
109 static uint32_t csrw(unsigned int source, unsigned int csr) __attribute__ ((unused));
110 static uint32_t csrw(unsigned int source, unsigned int csr) {
111  return (csr << 20) | (source << 15) | MATCH_CSRRW;
112 }
113 
114 static uint32_t addi(unsigned int dest, unsigned int src, uint16_t imm) __attribute__ ((unused));
115 static uint32_t addi(unsigned int dest, unsigned int src, uint16_t imm)
116 {
117  return (bits(imm, 11, 0) << 20) |
118  (src << 15) |
119  (dest << 7) |
120  MATCH_ADDI;
121 }
122 
123 static uint32_t csrr(unsigned int rd, unsigned int csr) __attribute__ ((unused));
124 static uint32_t csrr(unsigned int rd, unsigned int csr) {
125  return (csr << 20) | (rd << 7) | MATCH_CSRRS;
126 }
127 
128 static uint32_t fsw(unsigned int src, unsigned int base, uint16_t offset) __attribute__ ((unused));
129 static uint32_t fsw(unsigned int src, unsigned int base, uint16_t offset)
130 {
131  return (bits(offset, 11, 5) << 25) |
132  (bits(src, 4, 0) << 20) |
133  (base << 15) |
134  (bits(offset, 4, 0) << 7) |
135  MATCH_FSW;
136 }
137 
138 static uint32_t fsd(unsigned int src, unsigned int base, uint16_t offset) __attribute__ ((unused));
139 static uint32_t fsd(unsigned int src, unsigned int base, uint16_t offset)
140 {
141  return (bits(offset, 11, 5) << 25) |
142  (bits(src, 4, 0) << 20) |
143  (base << 15) |
144  (bits(offset, 4, 0) << 7) |
145  MATCH_FSD;
146 }
147 
148 static uint32_t flw(unsigned int dest, unsigned int base, uint16_t offset) __attribute__ ((unused));
149 static uint32_t flw(unsigned int dest, unsigned int base, uint16_t offset)
150 {
151  return (bits(offset, 11, 0) << 20) |
152  (base << 15) |
153  (bits(dest, 4, 0) << 7) |
154  MATCH_FLW;
155 }
156 
157 static uint32_t fld(unsigned int dest, unsigned int base, uint16_t offset) __attribute__ ((unused));
158 static uint32_t fld(unsigned int dest, unsigned int base, uint16_t offset)
159 {
160  return (bits(offset, 11, 0) << 20) |
161  (base << 15) |
162  (bits(dest, 4, 0) << 7) |
163  MATCH_FLD;
164 }
165 
166 static uint32_t ebreak(void) __attribute__ ((unused));
167 static uint32_t ebreak(void) { return MATCH_EBREAK; }
168 static uint32_t ebreak_c(void) __attribute__ ((unused));
169 static uint32_t ebreak_c(void) { return MATCH_C_EBREAK; }
170 
171 static uint32_t dret(void) __attribute__ ((unused));
172 static uint32_t dret(void) { return MATCH_DRET; }
173 
174 static uint32_t fence_i(void) __attribute__ ((unused));
175 static uint32_t fence_i(void)
176 {
177  return MATCH_FENCE_I;
178 }
179 
180 /*
181 static uint32_t lui(unsigned int dest, uint32_t imm) __attribute__ ((unused));
182 static uint32_t lui(unsigned int dest, uint32_t imm)
183 {
184  return (bits(imm, 19, 0) << 12) |
185  (dest << 7) |
186  MATCH_LUI;
187 }
188 
189 static uint32_t csrci(unsigned int csr, uint16_t imm) __attribute__ ((unused));
190 static uint32_t csrci(unsigned int csr, uint16_t imm) {
191  return (csr << 20) |
192  (bits(imm, 4, 0) << 15) |
193  MATCH_CSRRCI;
194 }
195 
196 static uint32_t li(unsigned int dest, uint16_t imm) __attribute__ ((unused));
197 static uint32_t li(unsigned int dest, uint16_t imm)
198 {
199  return addi(dest, 0, imm);
200 }
201 
202 static uint32_t fsd(unsigned int src, unsigned int base, uint16_t offset) __attribute__ ((unused));
203 static uint32_t fsd(unsigned int src, unsigned int base, uint16_t offset)
204 {
205  return (bits(offset, 11, 5) << 25) |
206  (bits(src, 4, 0) << 20) |
207  (base << 15) |
208  (bits(offset, 4, 0) << 7) |
209  MATCH_FSD;
210 }
211 
212 static uint32_t ori(unsigned int dest, unsigned int src, uint16_t imm) __attribute__ ((unused));
213 static uint32_t ori(unsigned int dest, unsigned int src, uint16_t imm)
214 {
215  return (bits(imm, 11, 0) << 20) |
216  (src << 15) |
217  (dest << 7) |
218  MATCH_ORI;
219 }
220 
221 static uint32_t nop(void) __attribute__ ((unused));
222 static uint32_t nop(void)
223 {
224  return addi(0, 0, 0);
225 }
226 */
227 
228 static uint32_t xori(unsigned int dest, unsigned int src, uint16_t imm) __attribute__ ((unused));
229 static uint32_t xori(unsigned int dest, unsigned int src, uint16_t imm)
230 {
231  return (bits(imm, 11, 0) << 20) |
232  (src << 15) |
233  (dest << 7) |
234  MATCH_XORI;
235 }
236 
237 static uint32_t srli(unsigned int dest, unsigned int src, uint8_t shamt) __attribute__ ((unused));
238 static uint32_t srli(unsigned int dest, unsigned int src, uint8_t shamt)
239 {
240  return (bits(shamt, 4, 0) << 20) |
241  (src << 15) |
242  (dest << 7) |
243  MATCH_SRLI;
244 }
Definition: ebreak.py:1