src/arm/decoder.h (view raw)
1#ifndef ARM_DECODER_H
2#define ARM_DECODER_H
3
4#include <stdint.h>
5
6// Bit 0: a register is involved with this operand
7// Bit 1: an immediate is invovled with this operand
8// Bit 2: a memory access is invovled with this operand
9// Bit 3: the destination of this operand is affected by this opcode
10// Bit 4: this operand is shifted by a register
11// Bit 5: this operand is shifted by an immediate
12enum ARMOperandFormat {
13 ARM_OPERAND_NONE = 0x00000000,
14 ARM_OPERAND_REGISTER_1 = 0x00000001,
15 ARM_OPERAND_IMMEDIATE_1 = 0x00000002,
16 ARM_OPERAND_MEMORY_1 = 0x00000004,
17 ARM_OPERAND_AFFECTED_1 = 0x00000008,
18 ARM_OPERAND_SHIFT_REGISTER_1 = 0x00000010,
19 ARM_OPERAND_SHIFT_IMMEDIATE_1 = 0x00000020,
20 ARM_OPERAND_1 = 0x000000FF,
21
22 ARM_OPERAND_REGISTER_2 = 0x00000100,
23 ARM_OPERAND_IMMEDIATE_2 = 0x00000200,
24 ARM_OPERAND_MEMORY_2 = 0x00000400,
25 ARM_OPERAND_AFFECTED_2 = 0x00000800,
26 ARM_OPERAND_SHIFT_REGISTER_2 = 0x00001000,
27 ARM_OPERAND_SHIFT_IMMEDIATE_2 = 0x00002000,
28 ARM_OPERAND_2 = 0x0000FF00,
29
30 ARM_OPERAND_REGISTER_3 = 0x00010000,
31 ARM_OPERAND_IMMEDIATE_3 = 0x00020000,
32 ARM_OPERAND_MEMORY_3 = 0x00040000,
33 ARM_OPERAND_AFFECTED_3 = 0x00080000,
34 ARM_OPERAND_SHIFT_REGISTER_3 = 0x00100000,
35 ARM_OPERAND_SHIFT_IMMEDIATE_3 = 0x00200000,
36 ARM_OPERAND_3 = 0x00FF0000,
37
38 ARM_OPERAND_REGISTER_4 = 0x01000000,
39 ARM_OPERAND_IMMEDIATE_4 = 0x02000000,
40 ARM_OPERAND_MEMORY_4 = 0x04000000,
41 ARM_OPERAND_AFFECTED_4 = 0x08000000,
42 ARM_OPERAND_SHIFT_REGISTER_4 = 0x10000000,
43 ARM_OPERAND_SHIFT_IMMEDIATE_4 = 0x20000000,
44 ARM_OPERAND_4 = 0xFF000000
45};
46
47enum ARMMemoryFormat {
48 ARM_MEMORY_REGISTER_BASE = 0x0001,
49 ARM_MEMORY_IMMEDIATE_OFFSET = 0x0002,
50 ARM_MEMORY_REGISTER_OFFSET = 0x0004,
51 ARM_MEMORY_SHIFTED_OFFSET = 0x0008,
52 ARM_MEMORY_PRE_INCREMENT = 0x0010,
53 ARM_MEMORY_POST_INCREMENT = 0x0020,
54 ARM_MEMORY_OFFSET_SUBTRACT = 0x0040,
55 ARM_MEMORY_WRITEBACK = 0x0080,
56 ARM_MEMORY_DECREMENT_AFTER = 0x0000,
57 ARM_MEMORY_INCREMENT_AFTER = 0x0100,
58 ARM_MEMORY_DECREMENT_BEFORE = 0x0200,
59 ARM_MEMORY_INCREMENT_BEFORE = 0x0300,
60};
61
62#define MEMORY_FORMAT_TO_DIRECTION(F) (((F) >> 8) & 0x7)
63
64enum ARMCondition {
65 ARM_CONDITION_EQ = 0x0,
66 ARM_CONDITION_NE = 0x1,
67 ARM_CONDITION_CS = 0x2,
68 ARM_CONDITION_CC = 0x3,
69 ARM_CONDITION_MI = 0x4,
70 ARM_CONDITION_PL = 0x5,
71 ARM_CONDITION_VS = 0x6,
72 ARM_CONDITION_VC = 0x7,
73 ARM_CONDITION_HI = 0x8,
74 ARM_CONDITION_LS = 0x9,
75 ARM_CONDITION_GE = 0xA,
76 ARM_CONDITION_LT = 0xB,
77 ARM_CONDITION_GT = 0xC,
78 ARM_CONDITION_LE = 0xD,
79 ARM_CONDITION_AL = 0xE,
80 ARM_CONDITION_NV = 0xF
81};
82
83enum ARMShifterOperation {
84 ARM_SHIFT_NONE = 0,
85 ARM_SHIFT_LSL,
86 ARM_SHIFT_LSR,
87 ARM_SHIFT_ASR,
88 ARM_SHIFT_ROR,
89 ARM_SHIFT_RRX
90};
91
92union ARMOperand {
93 struct {
94 uint8_t reg;
95 enum ARMShifterOperation shifterOp;
96 union {
97 uint8_t shifterReg;
98 uint8_t shifterImm;
99 };
100 };
101 int32_t immediate;
102};
103
104enum ARMMemoryAccessType {
105 ARM_ACCESS_WORD = 4,
106 ARM_ACCESS_HALFWORD = 2,
107 ARM_ACCESS_SIGNED_HALFWORD = 10,
108 ARM_ACCESS_BYTE = 1,
109 ARM_ACCESS_SIGNED_BYTE = 9
110};
111
112struct ARMMemoryAccess {
113 uint8_t baseReg;
114 uint16_t format;
115 union ARMOperand offset;
116 enum ARMMemoryAccessType width;
117};
118
119enum ARMMnemonic {
120 ARM_MN_ILL = 0,
121 ARM_MN_ADC,
122 ARM_MN_ADD,
123 ARM_MN_AND,
124 ARM_MN_ASR,
125 ARM_MN_B,
126 ARM_MN_BIC,
127 ARM_MN_BKPT,
128 ARM_MN_BL,
129 ARM_MN_BLH,
130 ARM_MN_BX,
131 ARM_MN_CMN,
132 ARM_MN_CMP,
133 ARM_MN_EOR,
134 ARM_MN_LDM,
135 ARM_MN_LDR,
136 ARM_MN_LSL,
137 ARM_MN_LSR,
138 ARM_MN_MLA,
139 ARM_MN_MOV,
140 ARM_MN_MUL,
141 ARM_MN_MVN,
142 ARM_MN_NEG,
143 ARM_MN_ORR,
144 ARM_MN_ROR,
145 ARM_MN_RSB,
146 ARM_MN_RSC,
147 ARM_MN_SBC,
148 ARM_MN_SMLAL,
149 ARM_MN_SMULL,
150 ARM_MN_STM,
151 ARM_MN_STR,
152 ARM_MN_SUB,
153 ARM_MN_SWI,
154 ARM_MN_TEQ,
155 ARM_MN_TST,
156 ARM_MN_UMLAL,
157 ARM_MN_UMULL,
158
159 ARM_MN_MAX
160};
161
162struct ARMInstructionInfo {
163 uint32_t opcode;
164 enum ARMMnemonic mnemonic;
165 union ARMOperand op1;
166 union ARMOperand op2;
167 union ARMOperand op3;
168 union ARMOperand op4;
169 struct ARMMemoryAccess memory;
170 int operandFormat;
171 int branches;
172 int traps;
173 int affectsCPSR;
174 int condition;
175 int sDataCycles;
176 int nDataCycles;
177 int sInstructionCycles;
178 int nInstructionCycles;
179 int iCycles;
180 int cCycles;
181};
182
183void ARMDecodeThumb(uint16_t opcode, struct ARMInstructionInfo* info);
184int ARMDisassembleThumb(uint16_t opcode, uint32_t pc, char* buffer, int blen);
185
186#endif