all repos — mgba @ c3259da78db245fd77901cd16a8ccd8014f80072

mGBA Game Boy Advance Emulator

src/arm/decoder-arm.c (view raw)

  1/* Copyright (c) 2013-2014 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include <mgba/internal/arm/decoder.h>
  7
  8#include <mgba/internal/arm/decoder-inlines.h>
  9#include <mgba/internal/arm/emitter-arm.h>
 10#include <mgba/internal/arm/isa-inlines.h>
 11
 12#define ADDR_MODE_1_SHIFT(OP) \
 13	info->op3.reg = opcode & 0x0000000F; \
 14	info->op3.shifterOp = ARM_SHIFT_ ## OP; \
 15	info->operandFormat |= ARM_OPERAND_REGISTER_3; \
 16	if (opcode & 0x00000010) { \
 17		info->op3.shifterReg = (opcode >> 8) & 0xF; \
 18		++info->iCycles; \
 19		info->operandFormat |= ARM_OPERAND_SHIFT_REGISTER_3; \
 20	} else { \
 21		info->op3.shifterImm = (opcode >> 7) & 0x1F; \
 22		info->operandFormat |= ARM_OPERAND_SHIFT_IMMEDIATE_3; \
 23	}
 24
 25#define ADDR_MODE_1_LSL \
 26	ADDR_MODE_1_SHIFT(LSL) \
 27	if (!info->op3.shifterImm) { \
 28		info->operandFormat &= ~ARM_OPERAND_SHIFT_IMMEDIATE_3; \
 29		info->op3.shifterOp = ARM_SHIFT_NONE; \
 30	}
 31
 32#define ADDR_MODE_1_LSR ADDR_MODE_1_SHIFT(LSR)
 33#define ADDR_MODE_1_ASR ADDR_MODE_1_SHIFT(ASR)
 34#define ADDR_MODE_1_ROR \
 35	ADDR_MODE_1_SHIFT(ROR) \
 36	if (!info->op3.shifterImm) { \
 37		info->op3.shifterOp = ARM_SHIFT_RRX; \
 38	}
 39
 40#define ADDR_MODE_1_IMM \
 41	int rotate = (opcode & 0x00000F00) >> 7; \
 42	int immediate = opcode & 0x000000FF; \
 43	info->op3.immediate = ROR(immediate, rotate); \
 44	info->operandFormat |= ARM_OPERAND_IMMEDIATE_3;
 45
 46#define ADDR_MODE_2_SHIFT(OP) \
 47	info->memory.format |= ARM_MEMORY_REGISTER_OFFSET | ARM_MEMORY_SHIFTED_OFFSET; \
 48	info->memory.offset.shifterOp = ARM_SHIFT_ ## OP; \
 49	info->memory.offset.shifterImm = (opcode >> 7) & 0x1F; \
 50	info->memory.offset.reg = opcode & 0x0000000F;
 51
 52#define ADDR_MODE_2_LSL \
 53	ADDR_MODE_2_SHIFT(LSL) \
 54	if (!info->memory.offset.shifterImm) { \
 55		info->memory.format &= ~ARM_MEMORY_SHIFTED_OFFSET; \
 56		info->memory.offset.shifterOp = ARM_SHIFT_NONE; \
 57	}
 58
 59#define ADDR_MODE_2_LSR ADDR_MODE_2_SHIFT(LSR) \
 60	if (!info->memory.offset.shifterImm) { \
 61		info->memory.offset.shifterImm = 32; \
 62	}
 63
 64#define ADDR_MODE_2_ASR ADDR_MODE_2_SHIFT(ASR) \
 65	if (!info->memory.offset.shifterImm) { \
 66		info->memory.offset.shifterImm = 32; \
 67	}
 68
 69#define ADDR_MODE_2_ROR \
 70	ADDR_MODE_2_SHIFT(ROR) \
 71	if (!info->memory.offset.shifterImm) { \
 72		info->memory.offset.shifterOp = ARM_SHIFT_RRX; \
 73	}
 74
 75#define ADDR_MODE_2_IMM \
 76	info->memory.format |= ARM_MEMORY_IMMEDIATE_OFFSET; \
 77	info->memory.offset.immediate = opcode & 0x00000FFF;
 78
 79#define ADDR_MODE_3_REG \
 80	info->memory.format |= ARM_MEMORY_REGISTER_OFFSET; \
 81	info->memory.offset.reg = opcode & 0x0000000F;
 82
 83#define ADDR_MODE_3_IMM \
 84	info->memory.format |= ARM_MEMORY_IMMEDIATE_OFFSET; \
 85	info->memory.offset.immediate = (opcode & 0x0000000F) | ((opcode & 0x00000F00) >> 4);
 86
 87#define DEFINE_DECODER_ARM(NAME, MNEMONIC, BODY) \
 88	static void _ARMDecode ## NAME (uint32_t opcode, struct ARMInstructionInfo* info) { \
 89		UNUSED(opcode); \
 90		info->mnemonic = ARM_MN_ ## MNEMONIC; \
 91		BODY; \
 92	}
 93
 94#define DEFINE_ALU_DECODER_EX_ARM(NAME, MNEMONIC, S, SHIFTER, OTHER_AFFECTED, SKIPPED) \
 95	DEFINE_DECODER_ARM(NAME, MNEMONIC, \
 96		info->op1.reg = (opcode >> 12) & 0xF; \
 97		info->op2.reg = (opcode >> 16) & 0xF; \
 98		info->operandFormat = ARM_OPERAND_REGISTER_1 | \
 99			OTHER_AFFECTED | \
100			ARM_OPERAND_REGISTER_2; \
101		info->affectsCPSR = S; \
102		SHIFTER; \
103		if (SKIPPED == 1) { \
104			info->op1 = info->op2; \
105			info->op2 = info->op3; \
106			info->operandFormat >>= 8; \
107		} else if (SKIPPED == 2) { \
108			info->op2 = info->op3; \
109			info->operandFormat |= info->operandFormat >> 8; \
110			info->operandFormat &= ~ARM_OPERAND_3; \
111		} \
112		if (info->op1.reg == ARM_PC && (OTHER_AFFECTED & ARM_OPERAND_AFFECTED_1)) { \
113			info->branchType = ARM_BRANCH_INDIRECT; \
114		})
115
116#define DEFINE_ALU_DECODER_ARM(NAME, SKIPPED) \
117	DEFINE_ALU_DECODER_EX_ARM(NAME ## _LSL, NAME, 0, ADDR_MODE_1_LSL, ARM_OPERAND_AFFECTED_1, SKIPPED) \
118	DEFINE_ALU_DECODER_EX_ARM(NAME ## S_LSL, NAME, 1, ADDR_MODE_1_LSL, ARM_OPERAND_AFFECTED_1, SKIPPED) \
119	DEFINE_ALU_DECODER_EX_ARM(NAME ## _LSR, NAME, 0, ADDR_MODE_1_LSR, ARM_OPERAND_AFFECTED_1, SKIPPED) \
120	DEFINE_ALU_DECODER_EX_ARM(NAME ## S_LSR, NAME, 1, ADDR_MODE_1_LSR, ARM_OPERAND_AFFECTED_1, SKIPPED) \
121	DEFINE_ALU_DECODER_EX_ARM(NAME ## _ASR, NAME, 0, ADDR_MODE_1_ASR, ARM_OPERAND_AFFECTED_1, SKIPPED) \
122	DEFINE_ALU_DECODER_EX_ARM(NAME ## S_ASR, NAME, 1, ADDR_MODE_1_ASR, ARM_OPERAND_AFFECTED_1, SKIPPED) \
123	DEFINE_ALU_DECODER_EX_ARM(NAME ## _ROR, NAME, 0, ADDR_MODE_1_ROR, ARM_OPERAND_AFFECTED_1, SKIPPED) \
124	DEFINE_ALU_DECODER_EX_ARM(NAME ## S_ROR, NAME, 1, ADDR_MODE_1_ROR, ARM_OPERAND_AFFECTED_1, SKIPPED) \
125	DEFINE_ALU_DECODER_EX_ARM(NAME ## I, NAME, 0, ADDR_MODE_1_IMM, ARM_OPERAND_AFFECTED_1, SKIPPED) \
126	DEFINE_ALU_DECODER_EX_ARM(NAME ## SI, NAME, 1, ADDR_MODE_1_IMM, ARM_OPERAND_AFFECTED_1, SKIPPED)
127
128#define DEFINE_ALU_DECODER_S_ONLY_ARM(NAME) \
129	DEFINE_ALU_DECODER_EX_ARM(NAME ## _LSL, NAME, 1, ADDR_MODE_1_LSL, ARM_OPERAND_NONE, 1) \
130	DEFINE_ALU_DECODER_EX_ARM(NAME ## _LSR, NAME, 1, ADDR_MODE_1_LSR, ARM_OPERAND_NONE, 1) \
131	DEFINE_ALU_DECODER_EX_ARM(NAME ## _ASR, NAME, 1, ADDR_MODE_1_ASR, ARM_OPERAND_NONE, 1) \
132	DEFINE_ALU_DECODER_EX_ARM(NAME ## _ROR, NAME, 1, ADDR_MODE_1_ROR, ARM_OPERAND_NONE, 1) \
133	DEFINE_ALU_DECODER_EX_ARM(NAME ## I, NAME, 1, ADDR_MODE_1_IMM, ARM_OPERAND_NONE, 1)
134
135#define DEFINE_MULTIPLY_DECODER_EX_ARM(NAME, MNEMONIC, S, OTHER_AFFECTED) \
136	DEFINE_DECODER_ARM(NAME, MNEMONIC, \
137		info->op1.reg = (opcode >> 16) & 0xF; \
138		info->op2.reg = opcode & 0xF; \
139		info->op3.reg = (opcode >> 8) & 0xF; \
140		info->op4.reg = (opcode >> 12) & 0xF; \
141		info->operandFormat = ARM_OPERAND_REGISTER_1 | \
142			ARM_OPERAND_AFFECTED_1 | \
143			ARM_OPERAND_REGISTER_2 | \
144			ARM_OPERAND_REGISTER_3 | \
145			OTHER_AFFECTED; \
146		info->affectsCPSR = S; \
147		if (info->op1.reg == ARM_PC) { \
148			info->branchType = ARM_BRANCH_INDIRECT; \
149		})
150
151#define DEFINE_LONG_MULTIPLY_DECODER_EX_ARM(NAME, MNEMONIC, S) \
152	DEFINE_DECODER_ARM(NAME, MNEMONIC, \
153		info->op1.reg = (opcode >> 12) & 0xF; \
154		info->op2.reg = (opcode >> 16) & 0xF; \
155		info->op3.reg = opcode & 0xF; \
156		info->op4.reg = (opcode >> 8) & 0xF; \
157		info->operandFormat = ARM_OPERAND_REGISTER_1 | \
158			ARM_OPERAND_AFFECTED_1 | \
159			ARM_OPERAND_REGISTER_2 | \
160			ARM_OPERAND_AFFECTED_2 | \
161			ARM_OPERAND_REGISTER_3 | \
162			ARM_OPERAND_REGISTER_4; \
163		info->affectsCPSR = S; \
164		if (info->op1.reg == ARM_PC) { \
165			info->branchType = ARM_BRANCH_INDIRECT; \
166		})
167
168#define DEFINE_MULTIPLY_DECODER_ARM(NAME, OTHER_AFFECTED) \
169	DEFINE_MULTIPLY_DECODER_EX_ARM(NAME, NAME, 0, OTHER_AFFECTED) \
170	DEFINE_MULTIPLY_DECODER_EX_ARM(NAME ## S, NAME, 1, OTHER_AFFECTED)
171
172#define DEFINE_LONG_MULTIPLY_DECODER_ARM(NAME) \
173	DEFINE_LONG_MULTIPLY_DECODER_EX_ARM(NAME, NAME, 0) \
174	DEFINE_LONG_MULTIPLY_DECODER_EX_ARM(NAME ## S, NAME, 1)
175
176#define DEFINE_LOAD_STORE_DECODER_EX_ARM(NAME, MNEMONIC, ADDRESSING_MODE, ADDRESSING_DECODING, CYCLES, TYPE, OTHER_AFFECTED) \
177	DEFINE_DECODER_ARM(NAME, MNEMONIC, \
178		info->op1.reg = (opcode >> 12) & 0xF; \
179		info->memory.baseReg = (opcode >> 16) & 0xF; \
180		info->memory.width = TYPE; \
181		info->operandFormat = ARM_OPERAND_REGISTER_1 | \
182			OTHER_AFFECTED | \
183			ARM_OPERAND_MEMORY_2; \
184		info->memory.format = ARM_MEMORY_REGISTER_BASE | ADDRESSING_MODE; \
185		ADDRESSING_DECODING; \
186		if (info->op1.reg == ARM_PC && (OTHER_AFFECTED & ARM_OPERAND_AFFECTED_1)) { \
187			info->branchType = ARM_BRANCH_INDIRECT; \
188		} \
189		if ((info->memory.format & (ARM_MEMORY_WRITEBACK | ARM_MEMORY_REGISTER_OFFSET)) == (ARM_MEMORY_WRITEBACK | ARM_MEMORY_REGISTER_OFFSET) && \
190		    info->memory.offset.reg == ARM_PC) { \
191			info->branchType = ARM_BRANCH_INDIRECT; \
192		} \
193		CYCLES;)
194
195#define DEFINE_LOAD_STORE_DECODER_SET_ARM(NAME, MNEMONIC, ADDRESSING_MODE, CYCLES, TYPE, OTHER_AFFECTED) \
196	DEFINE_LOAD_STORE_DECODER_EX_ARM(NAME, MNEMONIC, \
197		ARM_MEMORY_POST_INCREMENT | \
198		ARM_MEMORY_WRITEBACK | \
199		ARM_MEMORY_OFFSET_SUBTRACT, \
200		ADDRESSING_MODE, CYCLES, TYPE, OTHER_AFFECTED) \
201	DEFINE_LOAD_STORE_DECODER_EX_ARM(NAME ## U, MNEMONIC, \
202		ARM_MEMORY_POST_INCREMENT | \
203		ARM_MEMORY_WRITEBACK, \
204		ADDRESSING_MODE, CYCLES, TYPE, OTHER_AFFECTED) \
205	DEFINE_LOAD_STORE_DECODER_EX_ARM(NAME ## P, MNEMONIC, \
206		ARM_MEMORY_OFFSET_SUBTRACT, \
207		ADDRESSING_MODE, CYCLES, TYPE, OTHER_AFFECTED) \
208	DEFINE_LOAD_STORE_DECODER_EX_ARM(NAME ## PW, MNEMONIC, \
209		ARM_MEMORY_PRE_INCREMENT | \
210		ARM_MEMORY_WRITEBACK | \
211		ARM_MEMORY_OFFSET_SUBTRACT, \
212		ADDRESSING_MODE, CYCLES, TYPE, OTHER_AFFECTED) \
213	DEFINE_LOAD_STORE_DECODER_EX_ARM(NAME ## PU, MNEMONIC, \
214		0, \
215		ADDRESSING_MODE, CYCLES, TYPE, OTHER_AFFECTED) \
216	DEFINE_LOAD_STORE_DECODER_EX_ARM(NAME ## PUW, MNEMONIC, \
217		ARM_MEMORY_WRITEBACK, \
218		ADDRESSING_MODE, CYCLES, TYPE, OTHER_AFFECTED) \
219
220#define DEFINE_LOAD_STORE_MODE_2_DECODER_ARM(NAME, MNEMONIC, CYCLES, TYPE, OTHER_AFFECTED) \
221	DEFINE_LOAD_STORE_DECODER_SET_ARM(NAME ## _LSL_, MNEMONIC, ADDR_MODE_2_LSL, CYCLES, TYPE, OTHER_AFFECTED) \
222	DEFINE_LOAD_STORE_DECODER_SET_ARM(NAME ## _LSR_, MNEMONIC, ADDR_MODE_2_LSR, CYCLES, TYPE, OTHER_AFFECTED) \
223	DEFINE_LOAD_STORE_DECODER_SET_ARM(NAME ## _ASR_, MNEMONIC, ADDR_MODE_2_ASR, CYCLES, TYPE, OTHER_AFFECTED) \
224	DEFINE_LOAD_STORE_DECODER_SET_ARM(NAME ## _ROR_, MNEMONIC, ADDR_MODE_2_ROR, CYCLES, TYPE, OTHER_AFFECTED) \
225	DEFINE_LOAD_STORE_DECODER_SET_ARM(NAME ## I, MNEMONIC, ADDR_MODE_2_IMM, CYCLES, TYPE, OTHER_AFFECTED)
226
227#define DEFINE_LOAD_STORE_MODE_3_DECODER_ARM(NAME, MNEMONIC, CYCLES, TYPE, OTHER_AFFECTED) \
228	DEFINE_LOAD_STORE_DECODER_SET_ARM(NAME, MNEMONIC, ADDR_MODE_3_REG, CYCLES, TYPE, OTHER_AFFECTED) \
229	DEFINE_LOAD_STORE_DECODER_SET_ARM(NAME ## I, MNEMONIC, ADDR_MODE_3_IMM, CYCLES, TYPE, OTHER_AFFECTED)
230
231#define DEFINE_LOAD_STORE_T_DECODER_SET_ARM(NAME, MNEMONIC, ADDRESSING_MODE, CYCLES, TYPE, OTHER_AFFECTED) \
232	DEFINE_LOAD_STORE_DECODER_EX_ARM(NAME, MNEMONIC, \
233		ARM_MEMORY_POST_INCREMENT | \
234		ARM_MEMORY_WRITEBACK | \
235		ARM_MEMORY_OFFSET_SUBTRACT, \
236		ADDRESSING_MODE, CYCLES, TYPE, OTHER_AFFECTED) \
237	DEFINE_LOAD_STORE_DECODER_EX_ARM(NAME ## U, MNEMONIC, \
238		ARM_MEMORY_POST_INCREMENT | \
239		ARM_MEMORY_WRITEBACK, \
240		ADDRESSING_MODE, CYCLES, TYPE, OTHER_AFFECTED)
241
242#define DEFINE_LOAD_STORE_T_DECODER_ARM(NAME, MNEMONIC, CYCLES, TYPE, OTHER_AFFECTED) \
243	DEFINE_LOAD_STORE_T_DECODER_SET_ARM(NAME ## _LSL_, MNEMONIC, ADDR_MODE_2_LSL, CYCLES, TYPE, OTHER_AFFECTED) \
244	DEFINE_LOAD_STORE_T_DECODER_SET_ARM(NAME ## _LSR_, MNEMONIC, ADDR_MODE_2_LSR, CYCLES, TYPE, OTHER_AFFECTED) \
245	DEFINE_LOAD_STORE_T_DECODER_SET_ARM(NAME ## _ASR_, MNEMONIC, ADDR_MODE_2_ASR, CYCLES, TYPE, OTHER_AFFECTED) \
246	DEFINE_LOAD_STORE_T_DECODER_SET_ARM(NAME ## _ROR_, MNEMONIC, ADDR_MODE_2_ROR, CYCLES, TYPE, OTHER_AFFECTED) \
247	DEFINE_LOAD_STORE_T_DECODER_SET_ARM(NAME ## I, MNEMONIC, ADDR_MODE_2_IMM, CYCLES, TYPE, OTHER_AFFECTED)
248
249#define DEFINE_LOAD_STORE_MULTIPLE_DECODER_EX_ARM(NAME, MNEMONIC, DIRECTION, WRITEBACK) \
250	DEFINE_DECODER_ARM(NAME, MNEMONIC, \
251		info->memory.baseReg = (opcode >> 16) & 0xF; \
252		info->op1.immediate = opcode & 0x0000FFFF; \
253		if (info->op1.immediate & (1 << ARM_PC)) { \
254			info->branchType = ARM_BRANCH_INDIRECT; \
255		} \
256		info->operandFormat = ARM_OPERAND_MEMORY_1; \
257		info->memory.format = ARM_MEMORY_REGISTER_BASE | \
258			WRITEBACK | \
259			ARM_MEMORY_ ## DIRECTION;)
260
261
262#define DEFINE_LOAD_STORE_MULTIPLE_DECODER_ARM(NAME) \
263	DEFINE_LOAD_STORE_MULTIPLE_DECODER_EX_ARM(NAME ## DA,   NAME, DECREMENT_AFTER, 0) \
264	DEFINE_LOAD_STORE_MULTIPLE_DECODER_EX_ARM(NAME ## DAW,  NAME, DECREMENT_AFTER, ARM_MEMORY_WRITEBACK) \
265	DEFINE_LOAD_STORE_MULTIPLE_DECODER_EX_ARM(NAME ## DB,   NAME, DECREMENT_BEFORE, 0) \
266	DEFINE_LOAD_STORE_MULTIPLE_DECODER_EX_ARM(NAME ## DBW,  NAME, DECREMENT_BEFORE, ARM_MEMORY_WRITEBACK) \
267	DEFINE_LOAD_STORE_MULTIPLE_DECODER_EX_ARM(NAME ## IA,   NAME, INCREMENT_AFTER, 0) \
268	DEFINE_LOAD_STORE_MULTIPLE_DECODER_EX_ARM(NAME ## IAW,  NAME, INCREMENT_AFTER, ARM_MEMORY_WRITEBACK) \
269	DEFINE_LOAD_STORE_MULTIPLE_DECODER_EX_ARM(NAME ## IB,   NAME, INCREMENT_BEFORE, 0) \
270	DEFINE_LOAD_STORE_MULTIPLE_DECODER_EX_ARM(NAME ## IBW,  NAME, INCREMENT_BEFORE, ARM_MEMORY_WRITEBACK) \
271	DEFINE_LOAD_STORE_MULTIPLE_DECODER_EX_ARM(NAME ## SDA,  NAME, DECREMENT_AFTER, ARM_MEMORY_SPSR_SWAP) \
272	DEFINE_LOAD_STORE_MULTIPLE_DECODER_EX_ARM(NAME ## SDAW, NAME, DECREMENT_AFTER, ARM_MEMORY_WRITEBACK | ARM_MEMORY_SPSR_SWAP) \
273	DEFINE_LOAD_STORE_MULTIPLE_DECODER_EX_ARM(NAME ## SDB,  NAME, DECREMENT_BEFORE, ARM_MEMORY_SPSR_SWAP) \
274	DEFINE_LOAD_STORE_MULTIPLE_DECODER_EX_ARM(NAME ## SDBW, NAME, DECREMENT_BEFORE, ARM_MEMORY_WRITEBACK | ARM_MEMORY_SPSR_SWAP) \
275	DEFINE_LOAD_STORE_MULTIPLE_DECODER_EX_ARM(NAME ## SIA,  NAME, INCREMENT_AFTER, ARM_MEMORY_SPSR_SWAP) \
276	DEFINE_LOAD_STORE_MULTIPLE_DECODER_EX_ARM(NAME ## SIAW, NAME, INCREMENT_AFTER, ARM_MEMORY_WRITEBACK | ARM_MEMORY_SPSR_SWAP) \
277	DEFINE_LOAD_STORE_MULTIPLE_DECODER_EX_ARM(NAME ## SIB,  NAME, INCREMENT_BEFORE, ARM_MEMORY_SPSR_SWAP) \
278	DEFINE_LOAD_STORE_MULTIPLE_DECODER_EX_ARM(NAME ## SIBW, NAME, INCREMENT_BEFORE, ARM_MEMORY_WRITEBACK | ARM_MEMORY_SPSR_SWAP)
279
280#define DEFINE_SWP_DECODER_ARM(NAME, TYPE) \
281	DEFINE_DECODER_ARM(NAME, SWP, \
282		info->memory.baseReg = (opcode >> 16) & 0xF; \
283		info->op1.reg = (opcode >> 12) & 0xF; \
284		info->op2.reg = opcode & 0xF; \
285		info->operandFormat = ARM_OPERAND_REGISTER_1 | \
286			ARM_OPERAND_AFFECTED_1 | \
287			ARM_OPERAND_REGISTER_2 | \
288			ARM_OPERAND_MEMORY_3; \
289		info->memory.format = ARM_MEMORY_REGISTER_BASE; \
290		info->memory.width = TYPE;)
291
292DEFINE_ALU_DECODER_ARM(ADD, 0)
293DEFINE_ALU_DECODER_ARM(ADC, 0)
294DEFINE_ALU_DECODER_ARM(AND, 0)
295DEFINE_ALU_DECODER_ARM(BIC, 0)
296DEFINE_ALU_DECODER_S_ONLY_ARM(CMN)
297DEFINE_ALU_DECODER_S_ONLY_ARM(CMP)
298DEFINE_ALU_DECODER_ARM(EOR, 0)
299DEFINE_ALU_DECODER_ARM(MOV, 2)
300DEFINE_ALU_DECODER_ARM(MVN, 2)
301DEFINE_ALU_DECODER_ARM(ORR, 0)
302DEFINE_ALU_DECODER_ARM(RSB, 0)
303DEFINE_ALU_DECODER_ARM(RSC, 0)
304DEFINE_ALU_DECODER_ARM(SBC, 0)
305DEFINE_ALU_DECODER_ARM(SUB, 0)
306DEFINE_ALU_DECODER_S_ONLY_ARM(TEQ)
307DEFINE_ALU_DECODER_S_ONLY_ARM(TST)
308
309// TOOD: Estimate cycles
310DEFINE_MULTIPLY_DECODER_ARM(MLA, ARM_OPERAND_REGISTER_4)
311DEFINE_MULTIPLY_DECODER_ARM(MUL, ARM_OPERAND_NONE)
312
313DEFINE_LONG_MULTIPLY_DECODER_ARM(SMLAL)
314DEFINE_LONG_MULTIPLY_DECODER_ARM(SMULL)
315DEFINE_LONG_MULTIPLY_DECODER_ARM(UMLAL)
316DEFINE_LONG_MULTIPLY_DECODER_ARM(UMULL)
317
318// Begin load/store definitions
319
320DEFINE_LOAD_STORE_MODE_2_DECODER_ARM(LDR, LDR, LOAD_CYCLES, ARM_ACCESS_WORD, ARM_OPERAND_AFFECTED_1)
321DEFINE_LOAD_STORE_MODE_2_DECODER_ARM(LDRB, LDR, LOAD_CYCLES, ARM_ACCESS_BYTE, ARM_OPERAND_AFFECTED_1)
322DEFINE_LOAD_STORE_MODE_3_DECODER_ARM(LDRH, LDR, LOAD_CYCLES, ARM_ACCESS_HALFWORD, ARM_OPERAND_AFFECTED_1)
323DEFINE_LOAD_STORE_MODE_3_DECODER_ARM(LDRSB, LDR, LOAD_CYCLES, ARM_ACCESS_SIGNED_BYTE, ARM_OPERAND_AFFECTED_1)
324DEFINE_LOAD_STORE_MODE_3_DECODER_ARM(LDRSH, LDR, LOAD_CYCLES, ARM_ACCESS_SIGNED_HALFWORD, ARM_OPERAND_AFFECTED_1)
325DEFINE_LOAD_STORE_MODE_2_DECODER_ARM(STR, STR, STORE_CYCLES, ARM_ACCESS_WORD, ARM_OPERAND_NONE)
326DEFINE_LOAD_STORE_MODE_2_DECODER_ARM(STRB, STR, STORE_CYCLES, ARM_ACCESS_BYTE, ARM_OPERAND_NONE)
327DEFINE_LOAD_STORE_MODE_3_DECODER_ARM(STRH, STR, STORE_CYCLES, ARM_ACCESS_HALFWORD, ARM_OPERAND_NONE)
328
329DEFINE_LOAD_STORE_T_DECODER_ARM(LDRBT, LDR, LOAD_CYCLES, ARM_ACCESS_TRANSLATED_BYTE, ARM_OPERAND_AFFECTED_1)
330DEFINE_LOAD_STORE_T_DECODER_ARM(LDRT, LDR, LOAD_CYCLES, ARM_ACCESS_TRANSLATED_WORD, ARM_OPERAND_AFFECTED_1)
331DEFINE_LOAD_STORE_T_DECODER_ARM(STRBT, STR, STORE_CYCLES, ARM_ACCESS_TRANSLATED_BYTE, ARM_OPERAND_NONE)
332DEFINE_LOAD_STORE_T_DECODER_ARM(STRT, STR, STORE_CYCLES, ARM_ACCESS_TRANSLATED_WORD, ARM_OPERAND_NONE)
333
334DEFINE_LOAD_STORE_MULTIPLE_DECODER_ARM(LDM)
335DEFINE_LOAD_STORE_MULTIPLE_DECODER_ARM(STM)
336
337DEFINE_SWP_DECODER_ARM(SWP, ARM_ACCESS_WORD)
338DEFINE_SWP_DECODER_ARM(SWPB, ARM_ACCESS_BYTE)
339
340// End load/store definitions
341
342// Begin branch definitions
343
344DEFINE_DECODER_ARM(B, B,
345	int32_t offset = opcode << 8;
346	info->op1.immediate = offset >> 6;
347	info->operandFormat = ARM_OPERAND_IMMEDIATE_1;
348	info->branchType = ARM_BRANCH;)
349
350DEFINE_DECODER_ARM(BL, BL,
351	int32_t offset = opcode << 8;
352	info->op1.immediate = offset >> 6;
353	info->operandFormat = ARM_OPERAND_IMMEDIATE_1;
354	info->branchType = ARM_BRANCH_LINKED;)
355
356DEFINE_DECODER_ARM(BX, BX,
357	info->op1.reg = opcode & 0x0000000F;
358	info->operandFormat = ARM_OPERAND_REGISTER_1;
359	info->branchType = ARM_BRANCH_INDIRECT;)
360
361// End branch definitions
362
363// Begin coprocessor definitions
364
365DEFINE_DECODER_ARM(CDP, ILL, info->operandFormat = ARM_OPERAND_NONE;)
366DEFINE_DECODER_ARM(LDC, ILL, info->operandFormat = ARM_OPERAND_NONE;)
367DEFINE_DECODER_ARM(STC, ILL, info->operandFormat = ARM_OPERAND_NONE;)
368DEFINE_DECODER_ARM(MCR, ILL, info->operandFormat = ARM_OPERAND_NONE;)
369DEFINE_DECODER_ARM(MRC, ILL, info->operandFormat = ARM_OPERAND_NONE;)
370
371// Begin miscellaneous definitions
372
373DEFINE_DECODER_ARM(BKPT, BKPT,
374	info->operandFormat = ARM_OPERAND_NONE;
375	info->traps = 1;) // Not strictly in ARMv4T, but here for convenience
376DEFINE_DECODER_ARM(ILL, ILL,
377	info->operandFormat = ARM_OPERAND_NONE;
378	info->traps = 1;) // Illegal opcode
379
380DEFINE_DECODER_ARM(MSR, MSR,
381	info->affectsCPSR = 1;
382	info->op1.reg = ARM_CPSR;
383	info->op1.psrBits = (opcode >> 16) & ARM_PSR_MASK;
384	info->op2.reg = opcode & 0x0000000F;
385	info->operandFormat = ARM_OPERAND_REGISTER_1 |
386		ARM_OPERAND_AFFECTED_1 |
387		ARM_OPERAND_REGISTER_2;)
388
389DEFINE_DECODER_ARM(MSRR, MSR,
390	info->op1.reg = ARM_SPSR;
391	info->op1.psrBits = (opcode >> 16) & ARM_PSR_MASK;
392	info->op2.reg = opcode & 0x0000000F;
393	info->operandFormat = ARM_OPERAND_REGISTER_1 |
394		ARM_OPERAND_AFFECTED_1 |
395		ARM_OPERAND_REGISTER_2;)
396
397DEFINE_DECODER_ARM(MRS, MRS,
398	info->affectsCPSR = 1;
399	info->op1.reg = (opcode >> 12) & 0xF;
400	info->op2.reg = ARM_CPSR;
401	info->op2.psrBits = 0;
402	info->operandFormat = ARM_OPERAND_REGISTER_1 |
403		ARM_OPERAND_AFFECTED_1 |
404		ARM_OPERAND_REGISTER_2;)
405
406DEFINE_DECODER_ARM(MRSR, MRS,
407	info->op1.reg = (opcode >> 12) & 0xF;
408	info->op2.reg = ARM_SPSR;
409	info->op2.psrBits = 0;
410	info->operandFormat = ARM_OPERAND_REGISTER_1 |
411		ARM_OPERAND_AFFECTED_1 |
412		ARM_OPERAND_REGISTER_2;)
413
414DEFINE_DECODER_ARM(MSRI, MSR,
415	int rotate = (opcode & 0x00000F00) >> 7;
416	int32_t operand = ROR(opcode & 0x000000FF, rotate);
417	info->affectsCPSR = 1;
418	info->op1.reg = ARM_CPSR;
419	info->op1.psrBits = (opcode >> 16) & ARM_PSR_MASK;
420	info->op2.immediate = operand;
421	info->operandFormat = ARM_OPERAND_REGISTER_1 |
422		ARM_OPERAND_AFFECTED_1 |
423		ARM_OPERAND_IMMEDIATE_2;)
424
425DEFINE_DECODER_ARM(MSRRI, MSR,
426	int rotate = (opcode & 0x00000F00) >> 7;
427	int32_t operand = ROR(opcode & 0x000000FF, rotate);
428	info->op1.reg = ARM_SPSR;
429	info->op1.psrBits = (opcode >> 16) & ARM_PSR_MASK;
430	info->op2.immediate = operand;
431	info->operandFormat = ARM_OPERAND_REGISTER_1 |
432		ARM_OPERAND_AFFECTED_1 |
433		ARM_OPERAND_IMMEDIATE_2;)
434
435DEFINE_DECODER_ARM(SWI, SWI,
436	info->op1.immediate = opcode & 0xFFFFFF;
437	info->operandFormat = ARM_OPERAND_IMMEDIATE_1;
438	info->traps = 1;)
439
440typedef void (*ARMDecoder)(uint32_t opcode, struct ARMInstructionInfo* info);
441
442static const ARMDecoder _armDecoderTable[0x1000] = {
443	DECLARE_ARM_EMITTER_BLOCK(_ARMDecode)
444};
445
446void ARMDecodeARM(uint32_t opcode, struct ARMInstructionInfo* info) {
447	memset(info, 0, sizeof(*info));
448	info->execMode = MODE_ARM;
449	info->opcode = opcode;
450	info->branchType = ARM_BRANCH_NONE;
451	info->condition = opcode >> 28;
452	info->sInstructionCycles = 1;
453	ARMDecoder decoder = _armDecoderTable[((opcode >> 16) & 0xFF0) | ((opcode >> 4) & 0x00F)];
454	decoder(opcode, info);
455}