1 |
5 |
sergeykhbr |
/*
|
2 |
|
|
* Copyright 2018 Sergey Khabarov, sergeykhbr@gmail.com
|
3 |
|
|
*
|
4 |
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
|
|
* you may not use this file except in compliance with the License.
|
6 |
|
|
* You may obtain a copy of the License at
|
7 |
|
|
*
|
8 |
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
9 |
|
|
*
|
10 |
|
|
* Unless required by applicable law or agreed to in writing, software
|
11 |
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
|
|
* See the License for the specific language governing permissions and
|
14 |
|
|
* limitations under the License.
|
15 |
|
|
*/
|
16 |
|
|
|
17 |
|
|
#ifndef __DEBUGGER_COMMON_ARM_ISA_H__
|
18 |
|
|
#define __DEBUGGER_COMMON_ARM_ISA_H__
|
19 |
|
|
|
20 |
|
|
#include <inttypes.h>
|
21 |
|
|
|
22 |
|
|
namespace debugger {
|
23 |
|
|
|
24 |
|
|
static const uint64_t EXT_SIGN_5 = 0xFFFFFFFFFFFFFFF0LL;
|
25 |
|
|
static const uint64_t EXT_SIGN_6 = 0xFFFFFFFFFFFFFFE0LL;
|
26 |
|
|
static const uint64_t EXT_SIGN_8 = 0xFFFFFFFFFFFFFF80LL;
|
27 |
|
|
static const uint64_t EXT_SIGN_9 = 0xFFFFFFFFFFFFFF00LL;
|
28 |
|
|
static const uint64_t EXT_SIGN_11 = 0xFFFFFFFFFFFFF800LL;
|
29 |
|
|
static const uint64_t EXT_SIGN_12 = 0xFFFFFFFFFFFFF000LL;
|
30 |
|
|
static const uint64_t EXT_SIGN_16 = 0xFFFFFFFFFFFF0000LL;
|
31 |
|
|
static const uint64_t EXT_SIGN_32 = 0xFFFFFFFF00000000LL;
|
32 |
|
|
|
33 |
|
|
/** opcodes:
|
34 |
|
|
0000 = AND - Rd:= Op1 AND Op2
|
35 |
|
|
0001 = EOR - Rd:= Op1 EOR Op2
|
36 |
|
|
0010 = SUB - Rd:= Op1 - Op2
|
37 |
|
|
0011 = RSB - Rd:= Op2 - Op1
|
38 |
|
|
0100 = ADD - Rd:= Op1 + Op2
|
39 |
|
|
0101 = ADC - Rd:= Op1 + Op2 + C
|
40 |
|
|
0110 = SBC - Rd:= Op1 - Op2 + C
|
41 |
|
|
0111 = RSC - Rd:= Op2 - Op1 + C
|
42 |
|
|
1000 = TST - set condition codes on Op1 AND Op2
|
43 |
|
|
1001 = TEQ - set condition codes on Op1 EOR Op2
|
44 |
|
|
1010 = CMP - set condition codes on Op1 - Op2
|
45 |
|
|
1011 = CMN - set condition codes on Op1 + Op2
|
46 |
|
|
1100 = ORR - Rd:= Op1 OR Op2
|
47 |
|
|
1101 = MOV - Rd:= Op2
|
48 |
|
|
1110 = BIC - Rd:= Op1 AND NOT Op2
|
49 |
|
|
1111 = MVN - Rd:= NOT Op2
|
50 |
|
|
*/
|
51 |
|
|
union DataProcessingType {
|
52 |
|
|
struct reg_bits_type {
|
53 |
|
|
uint32_t rm : 4; // [3:0] 2-nd operand register
|
54 |
|
|
uint32_t sh_sel : 1; // [4] 0=shift amount in [11:7], 1=Rs in [11:8]
|
55 |
|
|
uint32_t sh_type : 2; // [6:5] 0=logic left; 1=logic right;
|
56 |
|
|
// 2=arith right; 3=rotate right
|
57 |
|
|
uint32_t shift : 5; // [11:7] shift applied to Rm
|
58 |
|
|
uint32_t rd : 4; // [15:12]
|
59 |
|
|
uint32_t rn : 4; // [19:16] 1-st operand register
|
60 |
|
|
uint32_t S : 1; // [20]. 0=do not alter condition code
|
61 |
|
|
uint32_t opcode : 4; // [24:21]
|
62 |
|
|
uint32_t I : 1; // [25] = 0 for register instruction
|
63 |
|
|
uint32_t zero : 2; // [27:26] = 00b
|
64 |
|
|
uint32_t cond : 4; // [31:28]
|
65 |
|
|
} reg_bits;
|
66 |
|
|
struct imm_bits_type {
|
67 |
|
|
uint32_t imm : 8; // [7:0]
|
68 |
|
|
uint32_t rotate : 4; // [11:8] rotate applied to imm
|
69 |
|
|
uint32_t rd : 4; // [15:12]
|
70 |
|
|
uint32_t rn : 4; // [19:16]
|
71 |
|
|
uint32_t S : 1; // [20]. 0=do not alter condition code
|
72 |
|
|
uint32_t opcode : 4; // [24:21]
|
73 |
|
|
uint32_t I : 1; // [25] = 1 for immediate instruction
|
74 |
|
|
uint32_t zero : 2; // [27:26] = 00b
|
75 |
|
|
uint32_t cond : 4; // [31:28]
|
76 |
|
|
} imm_bits;
|
77 |
|
|
struct mrs_bits_type {
|
78 |
|
|
uint32_t zero12 : 12; // [11:0] = 0
|
79 |
|
|
uint32_t rd : 4; // [15:12] destination
|
80 |
|
|
uint32_t mask : 4; // [21:16]
|
81 |
|
|
uint32_t b21_20 : 2; // [21:20] 00b=MRS; 10=MSR
|
82 |
|
|
uint32_t ps : 1; // [22] 0=CPSR; 1=SPSR_<cur_mod>
|
83 |
|
|
uint32_t b27_23 : 5; // [27:23] contant 00010b
|
84 |
|
|
uint32_t cond : 4; // [31:28]
|
85 |
|
|
} mrs_bits;
|
86 |
|
|
struct mov_bits_type {
|
87 |
|
|
uint32_t imm12 : 12;
|
88 |
|
|
uint32_t rd : 4;
|
89 |
|
|
uint32_t imm4 : 4;
|
90 |
|
|
uint32_t b27_20 : 8;
|
91 |
|
|
uint32_t cond : 4;
|
92 |
|
|
} mov_bits;
|
93 |
|
|
uint32_t value;
|
94 |
|
|
};
|
95 |
|
|
|
96 |
|
|
union SingleDataTransferType {
|
97 |
|
|
struct reg_bits_type {
|
98 |
|
|
uint32_t rm : 4; // [3:0] offset register
|
99 |
|
|
uint32_t sh_sel : 8; // [11:4] shift applied to Rm
|
100 |
|
|
uint32_t rd : 4; // [15:12]
|
101 |
|
|
uint32_t rn : 4; // [19:16]
|
102 |
|
|
uint32_t L : 1; // [20] = 1 load; 0 store
|
103 |
|
|
uint32_t W : 1; // [21] = 1 wr addr into base; 0 no write-back
|
104 |
|
|
uint32_t B : 1; // [22] = 1 byte; 0 word
|
105 |
|
|
uint32_t U : 1; // [23] = 1 add offset; 0 subtruct offset
|
106 |
|
|
uint32_t P : 1; // [24] = 1 pre; 0 post
|
107 |
|
|
uint32_t I : 1; // [25] = 1 for immediate instruction
|
108 |
|
|
uint32_t zeroone : 2; // [27:26] = 01b
|
109 |
|
|
uint32_t cond : 4; // [31:28]
|
110 |
|
|
} reg_bits;
|
111 |
|
|
struct imm_bits_type {
|
112 |
|
|
uint32_t imm : 12; // [11:0]
|
113 |
|
|
uint32_t rd : 4; // [15:12]
|
114 |
|
|
uint32_t rn : 4; // [19:16]
|
115 |
|
|
uint32_t L : 1; // [20] = 1 load; 0 store
|
116 |
|
|
uint32_t W : 1; // [21] = 1 wr addr into base; 0 no write-back
|
117 |
|
|
uint32_t B : 1; // [22] = 1 byte; 0 word
|
118 |
|
|
uint32_t U : 1; // [23] = 1 add offset; 0 subtruct offset
|
119 |
|
|
uint32_t P : 1; // [24] = 1 pre / 0 post
|
120 |
|
|
uint32_t I : 1; // [25] = 1 for immediate instruction
|
121 |
|
|
uint32_t zeroone : 2; // [27:26] = 01b
|
122 |
|
|
uint32_t cond : 4; // [31:28]
|
123 |
|
|
} imm_bits;
|
124 |
|
|
uint32_t value;
|
125 |
|
|
};
|
126 |
|
|
|
127 |
|
|
union HWordSignedDataTransferType {
|
128 |
|
|
struct reg_bits_type {
|
129 |
|
|
uint32_t rm : 4; // [3:0] offset register
|
130 |
|
|
uint32_t b4 : 1; // [4] =1
|
131 |
|
|
uint32_t h : 1; // [5] 0=byte; 1=half-word
|
132 |
|
|
uint32_t s : 1; // [6] 0=/unsigned; 1=signed
|
133 |
|
|
uint32_t b7 : 1; // [7] =1
|
134 |
|
|
uint32_t imm_h : 4; // [11:8] zero/immediate high nibble
|
135 |
|
|
uint32_t rd : 4; // [15:12]
|
136 |
|
|
uint32_t rn : 4; // [19:16]
|
137 |
|
|
uint32_t L : 1; // [20] = 1 load; 0 store
|
138 |
|
|
uint32_t W : 1; // [21] = 1 wr addr into base; 0 no write-back
|
139 |
|
|
uint32_t reg_imm : 1; // [22] = 0=reg offset; 1=imm offset
|
140 |
|
|
uint32_t U : 1; // [23] = 1 add offset; 0 subtruct offset
|
141 |
|
|
uint32_t P : 1; // [24] = 1 pre; 0 post
|
142 |
|
|
uint32_t zero3 : 3; // [27:25] = 000b
|
143 |
|
|
uint32_t cond : 4; // [31:28]
|
144 |
|
|
} bits;
|
145 |
|
|
uint32_t value;
|
146 |
|
|
};
|
147 |
|
|
|
148 |
|
|
union CoprocessorTransferType {
|
149 |
|
|
struct bits_type {
|
150 |
|
|
uint32_t crm : 4; // [3:0] Coproc. operand register
|
151 |
|
|
uint32_t one : 1; // 1b
|
152 |
|
|
uint32_t cp_nfo : 3; // [11:5] Coproc. information
|
153 |
|
|
uint32_t cp_num : 4; // [11:8] Coproc. number
|
154 |
|
|
uint32_t rd : 4; // [15:12] Dest. register
|
155 |
|
|
uint32_t crn : 4; // [19:16] Coproc.src/dest reg.
|
156 |
|
|
uint32_t L : 1; // [20] 1 load; 0 store
|
157 |
|
|
uint32_t mode : 3; // [23:21] Coproc. operation mode
|
158 |
|
|
uint32_t opcode : 4; // [27:24] = 1110b
|
159 |
|
|
uint32_t cond : 4; // [31:28]
|
160 |
|
|
} bits;
|
161 |
|
|
uint32_t value;
|
162 |
|
|
};
|
163 |
|
|
|
164 |
|
|
union PsrTransferType {
|
165 |
|
|
struct reg_bits_type {
|
166 |
|
|
uint32_t rm : 4; // [3:0] source reg
|
167 |
|
|
uint32_t zero : 8; // [15:4] =00000000b
|
168 |
|
|
uint32_t rd : 4; // [15:12]
|
169 |
|
|
uint32_t bitmask : 4; // [19:16]
|
170 |
|
|
uint32_t b21_20 : 18; // [21:20] =10b
|
171 |
|
|
uint32_t Pd : 1; // [23] = 0=CPSR; 1=SPSR_mode
|
172 |
|
|
uint32_t b24_23 : 2; // [24:23] = 10b
|
173 |
|
|
uint32_t I : 1; // [25] = 1 for immediate instruction
|
174 |
|
|
uint32_t b27_26 : 2; // [27:26] = 00b
|
175 |
|
|
uint32_t cond : 4; // [31:28]
|
176 |
|
|
} reg_bits;
|
177 |
|
|
struct imm_bits_type {
|
178 |
|
|
uint32_t imm : 8; // [7:0]
|
179 |
|
|
uint32_t rotate : 4; // [11:8] shift applied to imm
|
180 |
|
|
uint32_t rd : 4; // [15:12]
|
181 |
|
|
uint32_t bitmask : 4; // [19:16]
|
182 |
|
|
uint32_t b21_20 : 18; // [21:20] =10b
|
183 |
|
|
uint32_t Pd : 1; // [23] = 0=CPSR; 1=SPSR_mode
|
184 |
|
|
uint32_t b24_23 : 2; // [24:23] = 10b
|
185 |
|
|
uint32_t I : 1; // [25] = 1 for immediate instruction
|
186 |
|
|
uint32_t b27_26 : 2; // [27:26] = 00b
|
187 |
|
|
uint32_t cond : 4; // [31:28]
|
188 |
|
|
} imm_bits;
|
189 |
|
|
uint32_t value;
|
190 |
|
|
};
|
191 |
|
|
|
192 |
|
|
union BranchType {
|
193 |
|
|
struct bits_type {
|
194 |
|
|
uint32_t offset : 24; // [23:0] offset
|
195 |
|
|
uint32_t L : 1; // [24] 0 branch; 1 branch with link
|
196 |
|
|
uint32_t opcode : 3; // [27:25] = 101b
|
197 |
|
|
uint32_t cond : 4; // [31:28]
|
198 |
|
|
} bits;
|
199 |
|
|
uint32_t value;
|
200 |
|
|
};
|
201 |
|
|
|
202 |
|
|
union BlockDataTransferType {
|
203 |
|
|
struct bits_type {
|
204 |
|
|
uint32_t reglist : 16; // [15:0] Register list
|
205 |
|
|
uint32_t rn : 4; // [19:16] base register
|
206 |
|
|
uint32_t L : 1; // [20] 0=load; 1=store
|
207 |
|
|
uint32_t W : 1; // [21] 0=no write-back; 1=write adr into base
|
208 |
|
|
uint32_t S : 1; // [22] PSR & force user bit
|
209 |
|
|
uint32_t U : 1; // [23] 0=down; 1=up adr. increment
|
210 |
|
|
uint32_t P : 1; // [24] 0=post; 1=pre-increment
|
211 |
|
|
uint32_t b27_25 : 3; // [27:25] = 100b
|
212 |
|
|
uint32_t cond : 4; // [31:28]
|
213 |
|
|
} bits;
|
214 |
|
|
uint32_t value;
|
215 |
|
|
};
|
216 |
|
|
|
217 |
|
|
union SignExtendType {
|
218 |
|
|
struct bits_type {
|
219 |
|
|
uint32_t rm : 4; // [3:0]
|
220 |
|
|
uint32_t b7_4 : 4; // [7:4] 0111b
|
221 |
|
|
uint32_t sbz : 2; // [9:8]
|
222 |
|
|
uint32_t rotate : 2; // [11:10] 0=0; 1=ror8; 2=ror16; 3=ror24
|
223 |
|
|
uint32_t rd : 4; // [15:12]
|
224 |
|
|
uint32_t rn : 4; // [19:16]
|
225 |
|
|
uint32_t b27_20 : 8; // [27:16] = 01101110b
|
226 |
|
|
uint32_t cond : 4; // [31:28]
|
227 |
|
|
} bits;
|
228 |
|
|
uint32_t value;
|
229 |
|
|
};
|
230 |
|
|
|
231 |
|
|
union MulType {
|
232 |
|
|
struct mul_bits_type {
|
233 |
|
|
uint32_t rm : 4; //[3:0]
|
234 |
|
|
uint32_t b7_4 : 4; //[7:4] = 1001b
|
235 |
|
|
uint32_t rs : 4; //[11:8]
|
236 |
|
|
uint32_t rn : 4; //[15:12]
|
237 |
|
|
uint32_t rd : 4; //[19:16]
|
238 |
|
|
uint32_t S : 1; //[20]
|
239 |
|
|
uint32_t A : 1; //[21]
|
240 |
|
|
uint32_t b27_22 : 6; //[27:22]
|
241 |
|
|
uint32_t cond : 4; //[31:28]
|
242 |
|
|
} bits;
|
243 |
|
|
uint32_t value;
|
244 |
|
|
};
|
245 |
|
|
|
246 |
|
|
union MulLongType {
|
247 |
|
|
struct mull_bits_type {
|
248 |
|
|
uint32_t rm : 4; //[3:0]
|
249 |
|
|
uint32_t b7_4 : 4; //[7:4] = 1001b
|
250 |
|
|
uint32_t rs : 4; //[11:8]
|
251 |
|
|
uint32_t rdlo : 4; //[15:12]
|
252 |
|
|
uint32_t rdhi : 4; //[19:16]
|
253 |
|
|
uint32_t S : 1; //[20] 0=do not alter condition codes
|
254 |
|
|
uint32_t A : 1; //[21] 0=mul only; 1=mul + accumulate
|
255 |
|
|
uint32_t U : 1; //[22] 0=unsigned; 1=signed
|
256 |
|
|
uint32_t b27_22 : 5; //[27:21] = 00001b
|
257 |
|
|
uint32_t cond : 4; //[31:28]
|
258 |
|
|
} bits;
|
259 |
|
|
uint32_t value;
|
260 |
|
|
};
|
261 |
|
|
|
262 |
|
|
|
263 |
|
|
union ProgramStatusRegsiterType {
|
264 |
|
|
struct bits_type {
|
265 |
|
|
uint32_t M : 5; // [4:0] CPU mode: 0x13=supervisor
|
266 |
|
|
uint32_t T : 1; // [5] 0=ARM mode; 1=Thumb mode
|
267 |
|
|
uint32_t F : 1; // [6] 1=FIQ disable; 0=FIQ enable
|
268 |
|
|
uint32_t I : 1; // [7] 1=IRQ disable; 0=IRQ enable
|
269 |
|
|
uint32_t A : 1; // [8] 1=disable imprecise data aborts
|
270 |
|
|
uint32_t E : 1; // [9] Endianess
|
271 |
|
|
uint32_t b15_10 : 6; // [15:10] reserved
|
272 |
|
|
uint32_t GE : 4; // [19:16] Greater than or Equal
|
273 |
|
|
uint32_t b23_20 : 4; // [23:20] reserved
|
274 |
|
|
uint32_t J : 1; // [24] 1=Jazelle ISA; 0=reserved
|
275 |
|
|
uint32_t b26_25 : 2; // [26:25] reserved
|
276 |
|
|
uint32_t Q : 1; // [27] overflow in DSP instruction
|
277 |
|
|
uint32_t V : 1; // [28] overflow flag
|
278 |
|
|
uint32_t C : 1; // [29] carry flag
|
279 |
|
|
uint32_t Z : 1; // [30] zero flag
|
280 |
|
|
uint32_t N : 1; // [31] negative flag
|
281 |
|
|
} u;
|
282 |
|
|
uint32_t value;
|
283 |
|
|
};
|
284 |
|
|
|
285 |
|
|
static const char *const IREGS_NAMES[] = {
|
286 |
|
|
"r0", // [0]
|
287 |
|
|
"r1", // [1]
|
288 |
|
|
"r2", // [2]
|
289 |
|
|
"r3", // [3]
|
290 |
|
|
"r4", // [4]
|
291 |
|
|
"r5", // [5]
|
292 |
|
|
"r6", // [6]
|
293 |
|
|
"r7", // [7] fp in THUMB mode
|
294 |
|
|
"r8", // [8]
|
295 |
|
|
"r9", // [9]
|
296 |
|
|
"r10", // [10]
|
297 |
|
|
"fp", // [11] frame pointer
|
298 |
|
|
"r12", // [12]
|
299 |
|
|
"sp", // [13] stack pointer
|
300 |
|
|
"lr", // [14] link register
|
301 |
|
|
"pc", // [15] instruction pointer
|
302 |
|
|
"cpsr", // [16] Current Prog. Status Reg (all modes)
|
303 |
|
|
"spsr", // [17] Saved Prog. Status Reg
|
304 |
|
|
};
|
305 |
|
|
|
306 |
|
|
enum EConditionSuffix {
|
307 |
|
|
Cond_EQ, // equal
|
308 |
|
|
Cond_NE, // not equal
|
309 |
|
|
Cond_CS, // unsigned higer or same
|
310 |
|
|
Cond_CC, // unsigned lower
|
311 |
|
|
Cond_MI, // negative
|
312 |
|
|
Cond_PL, // positive or zero
|
313 |
|
|
Cond_VS, // Overflow
|
314 |
|
|
Cond_VC, // no overflow
|
315 |
|
|
Cond_HI, // unsigned higher
|
316 |
|
|
Cond_LS, // unsigned lower or same
|
317 |
|
|
Cond_GE, // greater or equal
|
318 |
|
|
Cond_LT, // less than
|
319 |
|
|
Cond_GT, // greater than
|
320 |
|
|
Cond_LE, // less tha or equal
|
321 |
|
|
Cond_AL, // always
|
322 |
|
|
};
|
323 |
|
|
|
324 |
|
|
enum ERegNames {
|
325 |
|
|
Reg_r0,
|
326 |
|
|
Reg_r1, // [1] Return address
|
327 |
|
|
Reg_r2, // [2] Stack pointer
|
328 |
|
|
Reg_r3, // [3] Global pointer
|
329 |
|
|
Reg_r4, // [4] Thread pointer
|
330 |
|
|
Reg_r5, // [5] Temporaries 0 s3
|
331 |
|
|
Reg_r6, // [6] Temporaries 1 s4
|
332 |
|
|
Reg_r7, // [7] Temporaries 2 s5
|
333 |
|
|
Reg_r8, // [8] s0/fp Saved register/frame pointer
|
334 |
|
|
Reg_r9, // [9] Saved register 1
|
335 |
|
|
Reg_r10, // [10] Function argumentes 0
|
336 |
|
|
Reg_r11, // [11] Function argumentes 1
|
337 |
|
|
Reg_fe, // [12] Function argumentes 2
|
338 |
|
|
Reg_sp, // [13] Function argumentes 3
|
339 |
|
|
Reg_lr, // [14] Function argumentes 4
|
340 |
|
|
Reg_pc, // [15] instruction pointer
|
341 |
|
|
Reg_cpsr, // [16] Current Prog. Status Reg (all modes)
|
342 |
|
|
Reg_spsr, // [17] Saved Prog. Status Reg
|
343 |
|
|
Reg_rsrv18,
|
344 |
|
|
Reg_rsrv19,
|
345 |
|
|
Reg_rsrv20,
|
346 |
|
|
Reg_rsrv21,
|
347 |
|
|
Reg_rsrv22,
|
348 |
|
|
Reg_rsrv23,
|
349 |
|
|
Reg_rsrv24,
|
350 |
|
|
Reg_rsrv25,
|
351 |
|
|
Reg_rsrv26,
|
352 |
|
|
Reg_rsrv27,
|
353 |
|
|
Reg_rsrv28,
|
354 |
|
|
Reg_rsrv29,
|
355 |
|
|
Reg_rsrv30,
|
356 |
|
|
Reg_rsrv31,
|
357 |
|
|
Reg_Total
|
358 |
|
|
};
|
359 |
|
|
|
360 |
|
|
|
361 |
|
|
} // namespace debugger
|
362 |
|
|
|
363 |
|
|
#endif // __DEBUGGER_COMMON_ARM_ISA_H__
|