all repos — mgba @ ec626d723fbb4716a697ad3cac0b0b29943dba00

mGBA Game Boy Advance Emulator

src/arm/isa-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/isa-arm.h>
  7
  8#include <mgba/internal/arm/arm.h>
  9#include <mgba/internal/arm/emitter-arm.h>
 10#include <mgba/internal/arm/isa-inlines.h>
 11#include <mgba-util/math.h>
 12
 13#define PSR_USER_MASK   0xF0000000
 14#define PSR_PRIV_MASK   0x000000CF
 15#define PSR_STATE_MASK  0x00000020
 16
 17// Addressing mode 1
 18static inline void _shiftLSL(struct ARMCore* cpu, uint32_t opcode) {
 19	int rm = opcode & 0x0000000F;
 20	if (opcode & 0x00000010) {
 21		int rs = (opcode >> 8) & 0x0000000F;
 22		++cpu->cycles;
 23		int shift = cpu->gprs[rs];
 24		if (rs == ARM_PC) {
 25			shift += 4;
 26		}
 27		shift &= 0xFF;
 28		int32_t shiftVal = cpu->gprs[rm];
 29		if (rm == ARM_PC) {
 30			shiftVal += 4;
 31		}
 32		if (!shift) {
 33			cpu->shifterOperand = shiftVal;
 34			cpu->shifterCarryOut = cpu->cpsr.c;
 35		} else if (shift < 32) {
 36			cpu->shifterOperand = shiftVal << shift;
 37			cpu->shifterCarryOut = (shiftVal >> (32 - shift)) & 1;
 38		} else if (shift == 32) {
 39			cpu->shifterOperand = 0;
 40			cpu->shifterCarryOut = shiftVal & 1;
 41		} else {
 42			cpu->shifterOperand = 0;
 43			cpu->shifterCarryOut = 0;
 44		}
 45	} else {
 46		int immediate = (opcode & 0x00000F80) >> 7;
 47		if (!immediate) {
 48			cpu->shifterOperand = cpu->gprs[rm];
 49			cpu->shifterCarryOut = cpu->cpsr.c;
 50		} else {
 51			cpu->shifterOperand = cpu->gprs[rm] << immediate;
 52			cpu->shifterCarryOut = (cpu->gprs[rm] >> (32 - immediate)) & 1;
 53		}
 54	}
 55}
 56
 57static inline void _shiftLSR(struct ARMCore* cpu, uint32_t opcode) {
 58	int rm = opcode & 0x0000000F;
 59	if (opcode & 0x00000010) {
 60		int rs = (opcode >> 8) & 0x0000000F;
 61		++cpu->cycles;
 62		int shift = cpu->gprs[rs];
 63		if (rs == ARM_PC) {
 64			shift += 4;
 65		}
 66		shift &= 0xFF;
 67		uint32_t shiftVal = cpu->gprs[rm];
 68		if (rm == ARM_PC) {
 69			shiftVal += 4;
 70		}
 71		if (!shift) {
 72			cpu->shifterOperand = shiftVal;
 73			cpu->shifterCarryOut = cpu->cpsr.c;
 74		} else if (shift < 32) {
 75			cpu->shifterOperand = shiftVal >> shift;
 76			cpu->shifterCarryOut = (shiftVal >> (shift - 1)) & 1;
 77		} else if (shift == 32) {
 78			cpu->shifterOperand = 0;
 79			cpu->shifterCarryOut = shiftVal >> 31;
 80		} else {
 81			cpu->shifterOperand = 0;
 82			cpu->shifterCarryOut = 0;
 83		}
 84	} else {
 85		int immediate = (opcode & 0x00000F80) >> 7;
 86		if (immediate) {
 87			cpu->shifterOperand = ((uint32_t) cpu->gprs[rm]) >> immediate;
 88			cpu->shifterCarryOut = (cpu->gprs[rm] >> (immediate - 1)) & 1;
 89		} else {
 90			cpu->shifterOperand = 0;
 91			cpu->shifterCarryOut = ARM_SIGN(cpu->gprs[rm]);
 92		}
 93	}
 94}
 95
 96static inline void _shiftASR(struct ARMCore* cpu, uint32_t opcode) {
 97	int rm = opcode & 0x0000000F;
 98	if (opcode & 0x00000010) {
 99		int rs = (opcode >> 8) & 0x0000000F;
100		++cpu->cycles;
101		int shift = cpu->gprs[rs];
102		if (rs == ARM_PC) {
103			shift += 4;
104		}
105		shift &= 0xFF;
106		int shiftVal =  cpu->gprs[rm];
107		if (rm == ARM_PC) {
108			shiftVal += 4;
109		}
110		if (!shift) {
111			cpu->shifterOperand = shiftVal;
112			cpu->shifterCarryOut = cpu->cpsr.c;
113		} else if (shift < 32) {
114			cpu->shifterOperand = shiftVal >> shift;
115			cpu->shifterCarryOut = (shiftVal >> (shift - 1)) & 1;
116		} else if (cpu->gprs[rm] >> 31) {
117			cpu->shifterOperand = 0xFFFFFFFF;
118			cpu->shifterCarryOut = 1;
119		} else {
120			cpu->shifterOperand = 0;
121			cpu->shifterCarryOut = 0;
122		}
123	} else {
124		int immediate = (opcode & 0x00000F80) >> 7;
125		if (immediate) {
126			cpu->shifterOperand = cpu->gprs[rm] >> immediate;
127			cpu->shifterCarryOut = (cpu->gprs[rm] >> (immediate - 1)) & 1;
128		} else {
129			cpu->shifterCarryOut = ARM_SIGN(cpu->gprs[rm]);
130			cpu->shifterOperand = cpu->shifterCarryOut;
131		}
132	}
133}
134
135static inline void _shiftROR(struct ARMCore* cpu, uint32_t opcode) {
136	int rm = opcode & 0x0000000F;
137	if (opcode & 0x00000010) {
138		int rs = (opcode >> 8) & 0x0000000F;
139		++cpu->cycles;
140		int shift = cpu->gprs[rs];
141		if (rs == ARM_PC) {
142			shift += 4;
143		}
144		shift &= 0xFF;
145		int shiftVal =  cpu->gprs[rm];
146		if (rm == ARM_PC) {
147			shiftVal += 4;
148		}
149		int rotate = shift & 0x1F;
150		if (!shift) {
151			cpu->shifterOperand = shiftVal;
152			cpu->shifterCarryOut = cpu->cpsr.c;
153		} else if (rotate) {
154			cpu->shifterOperand = ROR(shiftVal, rotate);
155			cpu->shifterCarryOut = (shiftVal >> (rotate - 1)) & 1;
156		} else {
157			cpu->shifterOperand = shiftVal;
158			cpu->shifterCarryOut = ARM_SIGN(shiftVal);
159		}
160	} else {
161		int immediate = (opcode & 0x00000F80) >> 7;
162		if (immediate) {
163			cpu->shifterOperand = ROR(cpu->gprs[rm], immediate);
164			cpu->shifterCarryOut = (cpu->gprs[rm] >> (immediate - 1)) & 1;
165		} else {
166			// RRX
167			cpu->shifterOperand = (cpu->cpsr.c << 31) | (((uint32_t) cpu->gprs[rm]) >> 1);
168			cpu->shifterCarryOut = cpu->gprs[rm] & 0x00000001;
169		}
170	}
171}
172
173static inline void _immediate(struct ARMCore* cpu, uint32_t opcode) {
174	int rotate = (opcode & 0x00000F00) >> 7;
175	int immediate = opcode & 0x000000FF;
176	if (!rotate) {
177		cpu->shifterOperand = immediate;
178		cpu->shifterCarryOut = cpu->cpsr.c;
179	} else {
180		cpu->shifterOperand = ROR(immediate, rotate);
181		cpu->shifterCarryOut = ARM_SIGN(cpu->shifterOperand);
182	}
183}
184
185// Instruction definitions
186// Beware pre-processor antics
187
188ATTRIBUTE_NOINLINE static void _additionS(struct ARMCore* cpu, int32_t m, int32_t n, int32_t d) {
189	cpu->cpsr.flags = 0;
190	cpu->cpsr.n = ARM_SIGN(d);
191	cpu->cpsr.z = !d;
192	cpu->cpsr.c = ARM_CARRY_FROM(m, n, d);
193	cpu->cpsr.v = ARM_V_ADDITION(m, n, d);
194}
195
196ATTRIBUTE_NOINLINE static void _subtractionS(struct ARMCore* cpu, int32_t m, int32_t n, int32_t d) {
197	cpu->cpsr.flags = 0;
198	cpu->cpsr.n = ARM_SIGN(d);
199	cpu->cpsr.z = !d;
200	cpu->cpsr.c = ARM_BORROW_FROM(m, n, d);
201	cpu->cpsr.v = ARM_V_SUBTRACTION(m, n, d);
202}
203
204ATTRIBUTE_NOINLINE static void _neutralS(struct ARMCore* cpu, int32_t d) {
205	cpu->cpsr.n = ARM_SIGN(d);
206	cpu->cpsr.z = !d; \
207	cpu->cpsr.c = cpu->shifterCarryOut; \
208}
209
210#define ARM_ADDITION_S(M, N, D) \
211	if (rd == ARM_PC && _ARMModeHasSPSR(cpu->cpsr.priv)) { \
212		cpu->cpsr = cpu->spsr; \
213		_ARMReadCPSR(cpu); \
214	} else { \
215		_additionS(cpu, M, N, D); \
216	}
217
218#define ARM_SUBTRACTION_S(M, N, D) \
219	if (rd == ARM_PC && _ARMModeHasSPSR(cpu->cpsr.priv)) { \
220		cpu->cpsr = cpu->spsr; \
221		_ARMReadCPSR(cpu); \
222	} else { \
223		_subtractionS(cpu, M, N, D); \
224	}
225
226#define ARM_SUBTRACTION_CARRY_S(M, N, D, C) \
227	if (rd == ARM_PC && _ARMModeHasSPSR(cpu->cpsr.priv)) { \
228		cpu->cpsr = cpu->spsr; \
229		_ARMReadCPSR(cpu); \
230	} else { \
231		cpu->cpsr.n = ARM_SIGN(D); \
232		cpu->cpsr.z = !(D); \
233		cpu->cpsr.c = ARM_BORROW_FROM_CARRY(M, N, D, C); \
234		cpu->cpsr.v = ARM_V_SUBTRACTION(M, N, D); \
235	}
236
237#define ARM_NEUTRAL_S(M, N, D) \
238	if (rd == ARM_PC && _ARMModeHasSPSR(cpu->cpsr.priv)) { \
239		cpu->cpsr = cpu->spsr; \
240		_ARMReadCPSR(cpu); \
241	} else { \
242		_neutralS(cpu, D); \
243	}
244
245#define ARM_NEUTRAL_HI_S(DLO, DHI) \
246	cpu->cpsr.n = ARM_SIGN(DHI); \
247	cpu->cpsr.z = !((DHI) | (DLO));
248
249#define ADDR_MODE_2_I_TEST (opcode & 0x00000F80)
250#define ADDR_MODE_2_I ((opcode & 0x00000F80) >> 7)
251#define ADDR_MODE_2_ADDRESS (address)
252#define ADDR_MODE_2_RN (cpu->gprs[rn])
253#define ADDR_MODE_2_RM (cpu->gprs[rm])
254#define ADDR_MODE_2_IMMEDIATE (opcode & 0x00000FFF)
255#define ADDR_MODE_2_INDEX(U_OP, M) (cpu->gprs[rn] U_OP M)
256#define ADDR_MODE_2_WRITEBACK(ADDR) \
257	cpu->gprs[rn] = ADDR; \
258	if (UNLIKELY(rn == ARM_PC)) { \
259		currentCycles += ARMWritePC(cpu); \
260	}
261
262#define ADDR_MODE_2_WRITEBACK_PRE_STORE(WB)
263#define ADDR_MODE_2_WRITEBACK_POST_STORE(WB) WB
264#define ADDR_MODE_2_WRITEBACK_PRE_LOAD(WB) WB
265#define ADDR_MODE_2_WRITEBACK_POST_LOAD(WB)
266
267#define ADDR_MODE_2_LSL (cpu->gprs[rm] << ADDR_MODE_2_I)
268#define ADDR_MODE_2_LSR (ADDR_MODE_2_I_TEST ? ((uint32_t) cpu->gprs[rm]) >> ADDR_MODE_2_I : 0)
269#define ADDR_MODE_2_ASR (ADDR_MODE_2_I_TEST ? ((int32_t) cpu->gprs[rm]) >> ADDR_MODE_2_I : ((int32_t) cpu->gprs[rm]) >> 31)
270#define ADDR_MODE_2_ROR (ADDR_MODE_2_I_TEST ? ROR(cpu->gprs[rm], ADDR_MODE_2_I) : (cpu->cpsr.c << 31) | (((uint32_t) cpu->gprs[rm]) >> 1))
271
272#define ADDR_MODE_3_ADDRESS ADDR_MODE_2_ADDRESS
273#define ADDR_MODE_3_RN ADDR_MODE_2_RN
274#define ADDR_MODE_3_RM ADDR_MODE_2_RM
275#define ADDR_MODE_3_IMMEDIATE (((opcode & 0x00000F00) >> 4) | (opcode & 0x0000000F))
276#define ADDR_MODE_3_INDEX(U_OP, M) ADDR_MODE_2_INDEX(U_OP, M)
277#define ADDR_MODE_3_WRITEBACK(ADDR) ADDR_MODE_2_WRITEBACK(ADDR)
278#define ADDR_MODE_3_WRITEBACK_64(ADDR) ADDR_MODE_2_WRITEBACK(ADDR + 4)
279
280#define ADDR_MODE_4_WRITEBACK_LDM \
281		if (!((1 << rn) & rs)) { \
282			cpu->gprs[rn] = address; \
283		}
284
285#define ADDR_MODE_4_WRITEBACK_LDMv5 \
286		if (!((1 << rn) & rs) || !(((1 << rn) - 1) & rs)) { \
287			cpu->gprs[rn] = address; \
288		}
289
290#define ADDR_MODE_4_WRITEBACK_STM cpu->gprs[rn] = address;
291
292#define ARM_LOAD_POST_BODY \
293	currentCycles += cpu->memory.activeNonseqCycles32 - cpu->memory.activeSeqCycles32; \
294	if (rd == ARM_PC) { \
295		currentCycles += ARMWritePC(cpu); \
296	}
297
298#define ARM_LOAD_POST_BODY_v5 \
299	currentCycles += cpu->memory.activeNonseqCycles32 - cpu->memory.activeSeqCycles32; \
300	if (rd == ARM_PC) { \
301		_ARMSetMode(cpu, cpu->gprs[ARM_PC] & 0x00000001); \
302		cpu->gprs[ARM_PC] &= 0xFFFFFFFE; \
303		if (cpu->executionMode == MODE_THUMB) { \
304			currentCycles += ThumbWritePC(cpu); \
305		} else { \
306			currentCycles += ARMWritePC(cpu); \
307		} \
308	}
309
310#define ARM_STORE_POST_BODY \
311	currentCycles += cpu->memory.activeNonseqCycles32 - cpu->memory.activeSeqCycles32;
312
313#define DEFINE_INSTRUCTION_ARM(NAME, BODY) \
314	static void _ARMInstruction ## NAME (struct ARMCore* cpu, uint32_t opcode) { \
315		int currentCycles = ARM_PREFETCH_CYCLES; \
316		BODY; \
317		cpu->cycles += currentCycles; \
318	}
319
320#define DEFINE_ALU_INSTRUCTION_EX_ARM(NAME, S_BODY, SHIFTER, BODY) \
321	DEFINE_INSTRUCTION_ARM(NAME, \
322		int rd = (opcode >> 12) & 0xF; \
323		int rn = (opcode >> 16) & 0xF; \
324		int32_t n = cpu->gprs[rn]; \
325		if (UNLIKELY(rn == ARM_PC && (opcode & 0x02000010) == 0x00000010)) { \
326			n += WORD_SIZE_ARM; \
327		} \
328		SHIFTER(cpu, opcode); \
329		BODY; \
330		S_BODY; \
331		if (rd == ARM_PC) { \
332			if (cpu->executionMode == MODE_ARM) { \
333				currentCycles += ARMWritePC(cpu); \
334			} else { \
335				currentCycles += ThumbWritePC(cpu); \
336			} \
337		})
338
339#define DEFINE_ALU_INSTRUCTION_ARM(NAME, S_BODY, BODY) \
340	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _LSL, , _shiftLSL, BODY) \
341	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## S_LSL, S_BODY, _shiftLSL, BODY) \
342	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _LSR, , _shiftLSR, BODY) \
343	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## S_LSR, S_BODY, _shiftLSR, BODY) \
344	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _ASR, , _shiftASR, BODY) \
345	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## S_ASR, S_BODY, _shiftASR, BODY) \
346	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _ROR, , _shiftROR, BODY) \
347	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## S_ROR, S_BODY, _shiftROR, BODY) \
348	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## I, , _immediate, BODY) \
349	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## SI, S_BODY, _immediate, BODY)
350
351#define DEFINE_ALU_INSTRUCTION_S_ONLY_ARM(NAME, S_BODY, BODY) \
352	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _LSL, S_BODY, _shiftLSL, BODY) \
353	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _LSR, S_BODY, _shiftLSR, BODY) \
354	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _ASR, S_BODY, _shiftASR, BODY) \
355	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _ROR, S_BODY, _shiftROR, BODY) \
356	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## I, S_BODY, _immediate, BODY)
357
358#define DEFINE_MULTIPLY_INSTRUCTION_EX_ARM(NAME, BODY, S_BODY) \
359	DEFINE_INSTRUCTION_ARM(NAME, \
360		int rd = (opcode >> 16) & 0xF; \
361		int rs = (opcode >> 8) & 0xF; \
362		int rm = opcode & 0xF; \
363		if (rd == ARM_PC) { \
364			return; \
365		} \
366		ARM_WAIT_MUL(cpu->gprs[rs]); \
367		BODY; \
368		S_BODY; \
369		currentCycles += cpu->memory.activeNonseqCycles32 - cpu->memory.activeSeqCycles32)
370
371#define DEFINE_MULTIPLY_INSTRUCTION_2_EX_ARM(NAME, BODY, S_BODY, WAIT) \
372	DEFINE_INSTRUCTION_ARM(NAME, \
373		int rd = (opcode >> 12) & 0xF; \
374		int rdHi = (opcode >> 16) & 0xF; \
375		int rs = (opcode >> 8) & 0xF; \
376		int rm = opcode & 0xF; \
377		if (rdHi == ARM_PC || rd == ARM_PC) { \
378			return; \
379		} \
380		currentCycles += cpu->memory.stall(cpu, WAIT); \
381		BODY; \
382		S_BODY; \
383		currentCycles += cpu->memory.activeNonseqCycles32 - cpu->memory.activeSeqCycles32)
384
385#define DEFINE_MULTIPLY_INSTRUCTION_ARM(NAME, BODY, S_BODY) \
386	DEFINE_MULTIPLY_INSTRUCTION_EX_ARM(NAME, BODY, ) \
387	DEFINE_MULTIPLY_INSTRUCTION_EX_ARM(NAME ## S, BODY, S_BODY)
388
389#define DEFINE_MULTIPLY_INSTRUCTION_2_ARM(NAME, BODY, S_BODY, WAIT) \
390	DEFINE_MULTIPLY_INSTRUCTION_2_EX_ARM(NAME, BODY, , WAIT) \
391	DEFINE_MULTIPLY_INSTRUCTION_2_EX_ARM(NAME ## S, BODY, S_BODY, WAIT)
392
393#define DEFINE_MULTIPLY_INSTRUCTION_3_ARM(NAME, BODY) \
394	DEFINE_INSTRUCTION_ARM(NAME, \
395		int rd = (opcode >> 16) & 0xF; \
396		int rs = (opcode >> 8) & 0xF; \
397		int rn = (opcode >> 12) & 0xF; \
398		int rm = opcode & 0xF; \
399		UNUSED(rn); \
400		if (rd == ARM_PC) { \
401			return; \
402		} \
403		/* TODO: Timing */ \
404		int32_t x; \
405		int32_t y; \
406		BODY; \
407		currentCycles += cpu->memory.activeNonseqCycles32 - cpu->memory.activeSeqCycles32)
408
409#define DEFINE_MULTIPLY_INSTRUCTION_XY_ARM(NAME, BODY) \
410	DEFINE_MULTIPLY_INSTRUCTION_3_ARM(NAME ## BB, \
411		x = ARM_SXT_16(cpu->gprs[rm]); \
412		y = ARM_SXT_16(cpu->gprs[rs]); \
413		BODY) \
414	DEFINE_MULTIPLY_INSTRUCTION_3_ARM(NAME ## BT, \
415		x = ARM_SXT_16(cpu->gprs[rm]); \
416		y = ARM_SXT_16(cpu->gprs[rs] >> 16); \
417		BODY) \
418	DEFINE_MULTIPLY_INSTRUCTION_3_ARM(NAME ## TB, \
419		x = ARM_SXT_16(cpu->gprs[rm] >> 16); \
420		y = ARM_SXT_16(cpu->gprs[rs]); \
421		BODY) \
422	DEFINE_MULTIPLY_INSTRUCTION_3_ARM(NAME ## TT, \
423		x = ARM_SXT_16(cpu->gprs[rm] >> 16); \
424		y = ARM_SXT_16(cpu->gprs[rs] >> 16); \
425		BODY)
426
427#define DEFINE_MULTIPLY_INSTRUCTION_WY_ARM(NAME, BODY) \
428	DEFINE_MULTIPLY_INSTRUCTION_3_ARM(NAME ## B, \
429		UNUSED(x); \
430		y = ARM_SXT_16(cpu->gprs[rs]); \
431		BODY) \
432	DEFINE_MULTIPLY_INSTRUCTION_3_ARM(NAME ## T, \
433		UNUSED(x); \
434		y = ARM_SXT_16(cpu->gprs[rs] >> 16); \
435		BODY) \
436
437#define DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME, ADDRESS, WRITEBACK, LS, BODY) \
438	DEFINE_INSTRUCTION_ARM(NAME, \
439		uint32_t address; \
440		int rn = (opcode >> 16) & 0xF; \
441		int rd = (opcode >> 12) & 0xF; \
442		int32_t d = cpu->gprs[rd]; \
443		if (UNLIKELY(rd == ARM_PC)) { \
444			d += WORD_SIZE_ARM; \
445		} \
446		int rm = opcode & 0xF; \
447		UNUSED(rm); \
448		address = ADDRESS; \
449		ADDR_MODE_2_WRITEBACK_PRE_ ## LS (WRITEBACK); \
450		BODY; \
451		ADDR_MODE_2_WRITEBACK_POST_ ## LS (WRITEBACK);)
452
453#define DEFINE_LOAD_STORE_INSTRUCTION_SHIFTER_ARM(NAME, SHIFTER, LS, BODY) \
454	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME, ADDR_MODE_2_RN, ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_INDEX(-, SHIFTER)), LS, BODY) \
455	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## U, ADDR_MODE_2_RN, ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_INDEX(+, SHIFTER)), LS, BODY) \
456	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## P, ADDR_MODE_2_INDEX(-, SHIFTER), , LS, BODY) \
457	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## PW, ADDR_MODE_2_INDEX(-, SHIFTER), ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_ADDRESS), LS, BODY) \
458	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## PU, ADDR_MODE_2_INDEX(+, SHIFTER), , LS, BODY) \
459	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## PUW, ADDR_MODE_2_INDEX(+, SHIFTER), ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_ADDRESS), LS, BODY)
460
461#define DEFINE_LOAD_STORE_INSTRUCTION_ARM(NAME, LS, BODY) \
462	DEFINE_LOAD_STORE_INSTRUCTION_SHIFTER_ARM(NAME ## _LSL_, ADDR_MODE_2_LSL, LS, BODY) \
463	DEFINE_LOAD_STORE_INSTRUCTION_SHIFTER_ARM(NAME ## _LSR_, ADDR_MODE_2_LSR, LS, BODY) \
464	DEFINE_LOAD_STORE_INSTRUCTION_SHIFTER_ARM(NAME ## _ASR_, ADDR_MODE_2_ASR, LS, BODY) \
465	DEFINE_LOAD_STORE_INSTRUCTION_SHIFTER_ARM(NAME ## _ROR_, ADDR_MODE_2_ROR, LS, BODY) \
466	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## I, ADDR_MODE_2_RN, ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_INDEX(-, ADDR_MODE_2_IMMEDIATE)), LS, BODY) \
467	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IU, ADDR_MODE_2_RN, ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_INDEX(+, ADDR_MODE_2_IMMEDIATE)), LS, BODY) \
468	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IP, ADDR_MODE_2_INDEX(-, ADDR_MODE_2_IMMEDIATE), , LS, BODY) \
469	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IPW, ADDR_MODE_2_INDEX(-, ADDR_MODE_2_IMMEDIATE), ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_ADDRESS), LS, BODY) \
470	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IPU, ADDR_MODE_2_INDEX(+, ADDR_MODE_2_IMMEDIATE), , LS, BODY) \
471	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IPUW, ADDR_MODE_2_INDEX(+, ADDR_MODE_2_IMMEDIATE), ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_ADDRESS), LS, BODY) \
472
473#define DEFINE_LOAD_STORE_MODE_3_WRITEBACK_WIDTH_INSTRUCTION_ARM(NAME, LS, BODY, WRITEBACK) \
474	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME, ADDR_MODE_3_RN, WRITEBACK(ADDR_MODE_3_INDEX(-, ADDR_MODE_3_RM)), LS, BODY) \
475	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## U, ADDR_MODE_3_RN, WRITEBACK(ADDR_MODE_3_INDEX(+, ADDR_MODE_3_RM)), LS, BODY) \
476	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## P, ADDR_MODE_3_INDEX(-, ADDR_MODE_3_RM), , LS, BODY) \
477	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## PW, ADDR_MODE_3_INDEX(-, ADDR_MODE_3_RM), WRITEBACK(ADDR_MODE_3_ADDRESS), LS, BODY) \
478	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## PU, ADDR_MODE_3_INDEX(+, ADDR_MODE_3_RM), , LS, BODY) \
479	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## PUW, ADDR_MODE_3_INDEX(+, ADDR_MODE_3_RM), ADDR_MODE_3_WRITEBACK(ADDR_MODE_3_ADDRESS), LS, BODY) \
480	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## I, ADDR_MODE_3_RN, WRITEBACK(ADDR_MODE_3_INDEX(-, ADDR_MODE_3_IMMEDIATE)), LS, BODY) \
481	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IU, ADDR_MODE_3_RN, ADDR_MODE_3_WRITEBACK(ADDR_MODE_3_INDEX(+, ADDR_MODE_3_IMMEDIATE)), LS, BODY) \
482	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IP, ADDR_MODE_3_INDEX(-, ADDR_MODE_3_IMMEDIATE), , LS, BODY) \
483	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IPW, ADDR_MODE_3_INDEX(-, ADDR_MODE_3_IMMEDIATE), ADDR_MODE_3_WRITEBACK(ADDR_MODE_3_ADDRESS), LS, BODY) \
484	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IPU, ADDR_MODE_3_INDEX(+, ADDR_MODE_3_IMMEDIATE), , LS, BODY) \
485	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IPUW, ADDR_MODE_3_INDEX(+, ADDR_MODE_3_IMMEDIATE), ADDR_MODE_3_WRITEBACK(ADDR_MODE_3_ADDRESS), LS, BODY) \
486
487#define DEFINE_LOAD_STORE_MODE_3_INSTRUCTION_ARM(NAME, LS, BODY) DEFINE_LOAD_STORE_MODE_3_WRITEBACK_WIDTH_INSTRUCTION_ARM(NAME, LS, BODY, ADDR_MODE_3_WRITEBACK)
488#define DEFINE_LOAD_STORE_MODE_3_DOUBLE_INSTRUCTION_ARM(NAME, LS, BODY) DEFINE_LOAD_STORE_MODE_3_WRITEBACK_WIDTH_INSTRUCTION_ARM(NAME, LS, BODY, ADDR_MODE_3_WRITEBACK_64)
489
490#define DEFINE_LOAD_STORE_T_INSTRUCTION_SHIFTER_ARM(NAME, SHIFTER, LS, BODY) \
491	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME, SHIFTER, ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_INDEX(-, ADDR_MODE_2_RM)), LS, BODY) \
492	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## U, SHIFTER, ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_INDEX(+, ADDR_MODE_2_RM)), LS, BODY) \
493
494#define DEFINE_LOAD_STORE_T_INSTRUCTION_ARM(NAME, LS, BODY) \
495	DEFINE_LOAD_STORE_T_INSTRUCTION_SHIFTER_ARM(NAME ## _LSL_, ADDR_MODE_2_LSL, LS, BODY) \
496	DEFINE_LOAD_STORE_T_INSTRUCTION_SHIFTER_ARM(NAME ## _LSR_, ADDR_MODE_2_LSR, LS, BODY) \
497	DEFINE_LOAD_STORE_T_INSTRUCTION_SHIFTER_ARM(NAME ## _ASR_, ADDR_MODE_2_ASR, LS, BODY) \
498	DEFINE_LOAD_STORE_T_INSTRUCTION_SHIFTER_ARM(NAME ## _ROR_, ADDR_MODE_2_ROR, LS, BODY) \
499	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## I, ADDR_MODE_2_RN, ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_INDEX(-, ADDR_MODE_2_IMMEDIATE)), LS, BODY) \
500	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IU, ADDR_MODE_2_RN, ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_INDEX(+, ADDR_MODE_2_IMMEDIATE)), LS, BODY) \
501
502#define ARM_MS_PRE_store \
503	enum PrivilegeMode privilegeMode = cpu->privilegeMode; \
504	ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
505
506#define ARM_MS_PRE_load \
507	enum PrivilegeMode privilegeMode; \
508	if (!(rs & 0x8000)) { \
509		privilegeMode = cpu->privilegeMode; \
510		ARMSetPrivilegeMode(cpu, MODE_SYSTEM); \
511	}
512
513#define ARM_MS_POST_store ARMSetPrivilegeMode(cpu, privilegeMode);
514
515#define ARM_MS_POST_load \
516	if ((rs & 0x8000) && _ARMModeHasSPSR(cpu->cpsr.priv)) { \
517		cpu->cpsr = cpu->spsr; \
518		_ARMReadCPSR(cpu); \
519	} else { \
520		ARMSetPrivilegeMode(cpu, privilegeMode); \
521	} \
522
523#define DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME, LS, WRITEBACK, S_PRE, S_POST, DIRECTION, POST_BODY) \
524	DEFINE_INSTRUCTION_ARM(NAME, \
525		int rn = (opcode >> 16) & 0xF; \
526		int rs = opcode & 0x0000FFFF; \
527		uint32_t address = cpu->gprs[rn]; \
528		S_PRE; \
529		address = cpu->memory. LS ## Multiple(cpu, address, rs, LSM_ ## DIRECTION, &currentCycles); \
530		WRITEBACK; \
531		S_POST; \
532		POST_BODY;)
533
534#define DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_ARM_NO_S(NAME, LS, POST_BODY) \
535	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## DA,   LS,                               ,           ,            , DA, POST_BODY) \
536	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## DAW,  LS, ADDR_MODE_4_WRITEBACK_ ## NAME,           ,            , DA, POST_BODY) \
537	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## DB,   LS,                               ,           ,            , DB, POST_BODY) \
538	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## DBW,  LS, ADDR_MODE_4_WRITEBACK_ ## NAME,           ,            , DB, POST_BODY) \
539	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## IA,   LS,                               ,           ,            , IA, POST_BODY) \
540	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## IAW,  LS, ADDR_MODE_4_WRITEBACK_ ## NAME,           ,            , IA, POST_BODY) \
541	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## IB,   LS,                               ,           ,            , IB, POST_BODY) \
542	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## IBW,  LS, ADDR_MODE_4_WRITEBACK_ ## NAME,           ,            , IB, POST_BODY) \
543
544#define DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_ARM(NAME, LS, POST_BODY) \
545	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_ARM_NO_S(NAME, LS, POST_BODY) \
546	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## SDA,  LS,                               , ARM_MS_PRE_ ## LS, ARM_MS_POST_ ## LS, DA, POST_BODY) \
547	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## SDAW, LS, ADDR_MODE_4_WRITEBACK_ ## NAME, ARM_MS_PRE_ ## LS, ARM_MS_POST_ ## LS, DA, POST_BODY) \
548	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## SDB,  LS,                               , ARM_MS_PRE_ ## LS, ARM_MS_POST_ ## LS, DB, POST_BODY) \
549	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## SDBW, LS, ADDR_MODE_4_WRITEBACK_ ## NAME, ARM_MS_PRE_ ## LS, ARM_MS_POST_ ## LS, DB, POST_BODY) \
550	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## SIA,  LS,                               , ARM_MS_PRE_ ## LS, ARM_MS_POST_ ## LS, IA, POST_BODY) \
551	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## SIAW, LS, ADDR_MODE_4_WRITEBACK_ ## NAME, ARM_MS_PRE_ ## LS, ARM_MS_POST_ ## LS, IA, POST_BODY) \
552	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## SIB,  LS,                               , ARM_MS_PRE_ ## LS, ARM_MS_POST_ ## LS, IB, POST_BODY) \
553	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## SIBW, LS, ADDR_MODE_4_WRITEBACK_ ## NAME, ARM_MS_PRE_ ## LS, ARM_MS_POST_ ## LS, IB, POST_BODY)
554
555// Begin ALU definitions
556
557DEFINE_ALU_INSTRUCTION_ARM(ADD, ARM_ADDITION_S(n, cpu->shifterOperand, cpu->gprs[rd]),
558	cpu->gprs[rd] = n + cpu->shifterOperand;)
559
560DEFINE_ALU_INSTRUCTION_ARM(ADC, ARM_ADDITION_S(n, cpu->shifterOperand, cpu->gprs[rd]),
561	cpu->gprs[rd] = n + cpu->shifterOperand + cpu->cpsr.c;)
562
563DEFINE_ALU_INSTRUCTION_ARM(AND, ARM_NEUTRAL_S(n, cpu->shifterOperand, cpu->gprs[rd]),
564	cpu->gprs[rd] = n & cpu->shifterOperand;)
565
566DEFINE_ALU_INSTRUCTION_ARM(BIC, ARM_NEUTRAL_S(n, cpu->shifterOperand, cpu->gprs[rd]),
567	cpu->gprs[rd] = n & ~cpu->shifterOperand;)
568
569DEFINE_ALU_INSTRUCTION_S_ONLY_ARM(CMN, ARM_ADDITION_S(n, cpu->shifterOperand, aluOut),
570	int32_t aluOut = n + cpu->shifterOperand;)
571
572DEFINE_ALU_INSTRUCTION_S_ONLY_ARM(CMP, ARM_SUBTRACTION_S(n, cpu->shifterOperand, aluOut),
573	int32_t aluOut = n - cpu->shifterOperand;)
574
575DEFINE_ALU_INSTRUCTION_ARM(EOR, ARM_NEUTRAL_S(n, cpu->shifterOperand, cpu->gprs[rd]),
576	cpu->gprs[rd] = n ^ cpu->shifterOperand;)
577
578DEFINE_ALU_INSTRUCTION_ARM(MOV, ARM_NEUTRAL_S(n, cpu->shifterOperand, cpu->gprs[rd]),
579	cpu->gprs[rd] = cpu->shifterOperand;)
580
581DEFINE_ALU_INSTRUCTION_ARM(MVN, ARM_NEUTRAL_S(n, cpu->shifterOperand, cpu->gprs[rd]),
582	cpu->gprs[rd] = ~cpu->shifterOperand;)
583
584DEFINE_ALU_INSTRUCTION_ARM(ORR, ARM_NEUTRAL_S(n, cpu->shifterOperand, cpu->gprs[rd]),
585	cpu->gprs[rd] = n | cpu->shifterOperand;)
586
587DEFINE_ALU_INSTRUCTION_ARM(RSB, ARM_SUBTRACTION_S(cpu->shifterOperand, n, cpu->gprs[rd]),
588	cpu->gprs[rd] = cpu->shifterOperand - n;)
589
590DEFINE_ALU_INSTRUCTION_ARM(RSC, ARM_SUBTRACTION_CARRY_S(cpu->shifterOperand, n, cpu->gprs[rd], !cpu->cpsr.c),
591	cpu->gprs[rd] = cpu->shifterOperand - n - !cpu->cpsr.c;)
592
593DEFINE_ALU_INSTRUCTION_ARM(SBC, ARM_SUBTRACTION_CARRY_S(n, cpu->shifterOperand, cpu->gprs[rd], !cpu->cpsr.c),
594	cpu->gprs[rd] = n - cpu->shifterOperand - !cpu->cpsr.c;)
595
596DEFINE_ALU_INSTRUCTION_ARM(SUB, ARM_SUBTRACTION_S(n, cpu->shifterOperand, cpu->gprs[rd]),
597	cpu->gprs[rd] = n - cpu->shifterOperand;)
598
599DEFINE_ALU_INSTRUCTION_S_ONLY_ARM(TEQ, ARM_NEUTRAL_S(n, cpu->shifterOperand, aluOut),
600	int32_t aluOut = n ^ cpu->shifterOperand;)
601
602DEFINE_ALU_INSTRUCTION_S_ONLY_ARM(TST, ARM_NEUTRAL_S(n, cpu->shifterOperand, aluOut),
603	int32_t aluOut = n & cpu->shifterOperand;)
604
605// End ALU definitions
606
607// Begin multiply definitions
608
609DEFINE_MULTIPLY_INSTRUCTION_2_ARM(MLA, cpu->gprs[rdHi] = cpu->gprs[rm] * cpu->gprs[rs] + cpu->gprs[rd], ARM_NEUTRAL_S(, , cpu->gprs[rdHi]), 2)
610DEFINE_MULTIPLY_INSTRUCTION_ARM(MUL, cpu->gprs[rd] = cpu->gprs[rm] * cpu->gprs[rs], ARM_NEUTRAL_S(cpu->gprs[rm], cpu->gprs[rs], cpu->gprs[rd]))
611
612DEFINE_MULTIPLY_INSTRUCTION_2_ARM(SMLAL,
613	int64_t d = ((int64_t) cpu->gprs[rm]) * ((int64_t) cpu->gprs[rs]);
614	int32_t dm = cpu->gprs[rd];
615	int32_t dn = d;
616	cpu->gprs[rd] = dm + dn;
617	cpu->gprs[rdHi] = cpu->gprs[rdHi] + (d >> 32) + ARM_CARRY_FROM(dm, dn, cpu->gprs[rd]);,
618	ARM_NEUTRAL_HI_S(cpu->gprs[rd], cpu->gprs[rdHi]), 3)
619
620DEFINE_MULTIPLY_INSTRUCTION_XY_ARM(SMLA,
621	int32_t dn = cpu->gprs[rn]; \
622	int32_t d = x * y; \
623	cpu->gprs[rd] = d + dn; \
624	cpu->cpsr.q = cpu->cpsr.q || ARM_V_ADDITION(d, dn, cpu->gprs[rd]);)
625
626DEFINE_MULTIPLY_INSTRUCTION_XY_ARM(SMUL, cpu->gprs[rd] = x * y;)
627
628DEFINE_MULTIPLY_INSTRUCTION_WY_ARM(SMLAW,
629	int32_t dn = cpu->gprs[rn]; \
630	int32_t d = (((int64_t) cpu->gprs[rm]) * ((int64_t) y)) >> 16; \
631	cpu->gprs[rd] = d + dn; \
632	cpu->cpsr.q = cpu->cpsr.q || ARM_V_ADDITION(d, dn, cpu->gprs[rd]);)
633
634DEFINE_MULTIPLY_INSTRUCTION_WY_ARM(SMULW, cpu->gprs[rd] = (((int64_t) cpu->gprs[rm]) * ((int64_t) y)) >> 16;)
635
636DEFINE_MULTIPLY_INSTRUCTION_2_ARM(SMULL,
637	int64_t d = ((int64_t) cpu->gprs[rm]) * ((int64_t) cpu->gprs[rs]);
638	cpu->gprs[rd] = d;
639	cpu->gprs[rdHi] = d >> 32;,
640	ARM_NEUTRAL_HI_S(cpu->gprs[rd], cpu->gprs[rdHi]), 2)
641
642DEFINE_MULTIPLY_INSTRUCTION_2_ARM(UMLAL,
643	uint64_t d = ARM_UXT_64(cpu->gprs[rm]) * ARM_UXT_64(cpu->gprs[rs]);
644	int32_t dm = cpu->gprs[rd];
645	int32_t dn = d;
646	cpu->gprs[rd] = dm + dn;
647	cpu->gprs[rdHi] = cpu->gprs[rdHi] + (d >> 32) + ARM_CARRY_FROM(dm, dn, cpu->gprs[rd]);,
648	ARM_NEUTRAL_HI_S(cpu->gprs[rd], cpu->gprs[rdHi]), 3)
649
650DEFINE_MULTIPLY_INSTRUCTION_2_ARM(UMULL,
651	uint64_t d = ARM_UXT_64(cpu->gprs[rm]) * ARM_UXT_64(cpu->gprs[rs]);
652	cpu->gprs[rd] = d;
653	cpu->gprs[rdHi] = d >> 32;,
654	ARM_NEUTRAL_HI_S(cpu->gprs[rd], cpu->gprs[rdHi]), 2)
655
656// End multiply definitions
657
658// Begin load/store definitions
659
660DEFINE_LOAD_STORE_INSTRUCTION_ARM(LDR, LOAD, cpu->gprs[rd] = cpu->memory.load32(cpu, address, &currentCycles); ARM_LOAD_POST_BODY;)
661DEFINE_LOAD_STORE_INSTRUCTION_ARM(LDRv5, LOAD, cpu->gprs[rd] = cpu->memory.load32(cpu, address, &currentCycles); ARM_LOAD_POST_BODY_v5;)
662DEFINE_LOAD_STORE_INSTRUCTION_ARM(LDRB, LOAD, cpu->gprs[rd] = cpu->memory.load8(cpu, address, &currentCycles); ARM_LOAD_POST_BODY;)
663DEFINE_LOAD_STORE_MODE_3_DOUBLE_INSTRUCTION_ARM(LDRD, LOAD, cpu->gprs[rd & ~1] = cpu->memory.load32(cpu, address, &currentCycles); cpu->gprs[rd | 1] = cpu->memory.load32(cpu, address + 4, &currentCycles); ARM_LOAD_POST_BODY;)
664DEFINE_LOAD_STORE_MODE_3_INSTRUCTION_ARM(LDRH, LOAD, cpu->gprs[rd] = cpu->memory.load16(cpu, address, &currentCycles); ARM_LOAD_POST_BODY;)
665DEFINE_LOAD_STORE_MODE_3_INSTRUCTION_ARM(LDRSB, LOAD, cpu->gprs[rd] = ARM_SXT_8(cpu->memory.load8(cpu, address, &currentCycles)); ARM_LOAD_POST_BODY;)
666DEFINE_LOAD_STORE_MODE_3_INSTRUCTION_ARM(LDRSH, LOAD, cpu->gprs[rd] = address & 1 ? ARM_SXT_8(cpu->memory.load16(cpu, address, &currentCycles)) : ARM_SXT_16(cpu->memory.load16(cpu, address, &currentCycles)); ARM_LOAD_POST_BODY;)
667DEFINE_LOAD_STORE_INSTRUCTION_ARM(STR, STORE, cpu->memory.store32(cpu, address, d, &currentCycles); ARM_STORE_POST_BODY;)
668DEFINE_LOAD_STORE_INSTRUCTION_ARM(STRB, STORE, cpu->memory.store8(cpu, address, d, &currentCycles); ARM_STORE_POST_BODY;)
669DEFINE_LOAD_STORE_MODE_3_DOUBLE_INSTRUCTION_ARM(STRD, STORE, cpu->memory.store32(cpu, address, cpu->gprs[rd & ~1], &currentCycles); cpu->memory.store32(cpu, address + 4, cpu->gprs[rd | 1], &currentCycles); ARM_STORE_POST_BODY;)
670DEFINE_LOAD_STORE_MODE_3_INSTRUCTION_ARM(STRH, STORE, cpu->memory.store16(cpu, address, d, &currentCycles); ARM_STORE_POST_BODY;)
671
672DEFINE_LOAD_STORE_T_INSTRUCTION_ARM(LDRBT, LOAD,
673	enum PrivilegeMode priv = cpu->privilegeMode;
674	ARMSetPrivilegeMode(cpu, MODE_USER);
675	int32_t r = cpu->memory.load8(cpu, address, &currentCycles);
676	ARMSetPrivilegeMode(cpu, priv);
677	cpu->gprs[rd] = r;
678	ARM_LOAD_POST_BODY;)
679
680DEFINE_LOAD_STORE_T_INSTRUCTION_ARM(LDRT, LOAD,
681	enum PrivilegeMode priv = cpu->privilegeMode;
682	ARMSetPrivilegeMode(cpu, MODE_USER);
683	int32_t r = cpu->memory.load32(cpu, address, &currentCycles);
684	ARMSetPrivilegeMode(cpu, priv);
685	cpu->gprs[rd] = r;
686	ARM_LOAD_POST_BODY;)
687
688DEFINE_LOAD_STORE_T_INSTRUCTION_ARM(STRBT, STORE,
689	enum PrivilegeMode priv = cpu->privilegeMode;
690	int32_t r = cpu->gprs[rd];
691	ARMSetPrivilegeMode(cpu, MODE_USER);
692	cpu->memory.store8(cpu, address, r, &currentCycles);
693	ARMSetPrivilegeMode(cpu, priv);
694	ARM_STORE_POST_BODY;)
695
696DEFINE_LOAD_STORE_T_INSTRUCTION_ARM(STRT, STORE,
697	enum PrivilegeMode priv = cpu->privilegeMode;
698	int32_t r = cpu->gprs[rd];
699	ARMSetPrivilegeMode(cpu, MODE_USER);
700	cpu->memory.store32(cpu, address, r, &currentCycles);
701	ARMSetPrivilegeMode(cpu, priv);
702	ARM_STORE_POST_BODY;)
703
704DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_ARM(LDM,
705	load,
706	currentCycles += cpu->memory.activeNonseqCycles32 - cpu->memory.activeSeqCycles32;
707	if ((rs & 0x8000) || !rs) {
708		if (cpu->executionMode == MODE_THUMB) {
709			currentCycles += ThumbWritePC(cpu);
710		} else {
711			currentCycles += ARMWritePC(cpu);
712		}
713	})
714
715DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_ARM_NO_S(LDMv5,
716	load,
717	currentCycles += cpu->memory.activeNonseqCycles32 - cpu->memory.activeSeqCycles32;
718	if (rs & 0x8000) {
719		_ARMSetMode(cpu, cpu->gprs[ARM_PC] & 0x00000001);
720		cpu->gprs[ARM_PC] &= 0xFFFFFFFE;
721		if (cpu->executionMode == MODE_THUMB) {
722			currentCycles += ThumbWritePC(cpu);
723		} else {
724			currentCycles += ARMWritePC(cpu);
725
726		}
727	})
728
729DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_ARM(STM,
730	store,
731	ARM_STORE_POST_BODY;)
732
733DEFINE_INSTRUCTION_ARM(SWP,
734	int rm = opcode & 0xF;
735	int rd = (opcode >> 12) & 0xF;
736	int rn = (opcode >> 16) & 0xF;
737	int32_t d = cpu->memory.load32(cpu, cpu->gprs[rn], &currentCycles);
738	cpu->memory.store32(cpu, cpu->gprs[rn], cpu->gprs[rm], &currentCycles);
739	cpu->gprs[rd] = d;)
740
741DEFINE_INSTRUCTION_ARM(SWPB,
742	int rm = opcode & 0xF;
743	int rd = (opcode >> 12) & 0xF;
744	int rn = (opcode >> 16) & 0xF;
745	int32_t d = cpu->memory.load8(cpu, cpu->gprs[rn], &currentCycles);
746	cpu->memory.store8(cpu, cpu->gprs[rn], cpu->gprs[rm], &currentCycles);
747	cpu->gprs[rd] = d;)
748
749// End load/store definitions
750
751// Begin branch definitions
752
753DEFINE_INSTRUCTION_ARM(B,
754	int32_t offset = opcode << 8;
755	offset >>= 6;
756	cpu->gprs[ARM_PC] += offset;
757	currentCycles += ARMWritePC(cpu);)
758
759DEFINE_INSTRUCTION_ARM(BL,
760	int32_t immediate = (opcode & 0x00FFFFFF) << 8;
761	cpu->gprs[ARM_LR] = cpu->gprs[ARM_PC] - WORD_SIZE_ARM;
762	cpu->gprs[ARM_PC] += immediate >> 6;
763	currentCycles += ARMWritePC(cpu);)
764
765DEFINE_INSTRUCTION_ARM(BX,
766	int rm = opcode & 0x0000000F;
767	_ARMSetMode(cpu, cpu->gprs[rm] & 0x00000001);
768	cpu->gprs[ARM_PC] = cpu->gprs[rm] & 0xFFFFFFFE;
769	if (cpu->executionMode == MODE_THUMB) {
770		currentCycles += ThumbWritePC(cpu);
771	} else {
772		currentCycles += ARMWritePC(cpu);
773
774	})
775
776DEFINE_INSTRUCTION_ARM(BLX,
777	int32_t immediate = (opcode & 0x00FFFFFF) << 8;
778	cpu->gprs[ARM_LR] = cpu->gprs[ARM_PC] - WORD_SIZE_ARM;
779	cpu->gprs[ARM_PC] += (immediate >> 6) + ((opcode >> 23) & 2);
780	_ARMSetMode(cpu, MODE_THUMB);
781	currentCycles += ThumbWritePC(cpu);)
782
783DEFINE_INSTRUCTION_ARM(BLX2,
784	int rm = opcode & 0x0000000F;
785	int address = cpu->gprs[rm];
786	cpu->gprs[ARM_LR] = cpu->gprs[ARM_PC] - WORD_SIZE_ARM;
787	_ARMSetMode(cpu, address & 0x00000001);
788	cpu->gprs[ARM_PC] = address & 0xFFFFFFFE;
789	if (cpu->executionMode == MODE_THUMB) {
790		currentCycles += ThumbWritePC(cpu);
791	} else {
792		currentCycles += ARMWritePC(cpu);
793	})
794
795// End branch definitions
796
797// Begin coprocessor definitions
798
799#define DEFINE_COPROCESSOR_INSTRUCTION(NAME, BODY) \
800	DEFINE_INSTRUCTION_ARM(NAME, \
801		int op1 = (opcode >> 21) & 7; \
802		int op2 = (opcode >> 5) & 7; \
803		int rd = (opcode >> 12) & 0xF; \
804		int cp = (opcode >> 8) & 0xF; \
805		int crn = (opcode >> 16) & 0xF; \
806		int crm = opcode & 0xF; \
807		UNUSED(op1); \
808		UNUSED(op2); \
809		UNUSED(rd); \
810		UNUSED(crn); \
811		UNUSED(crm); \
812		BODY;)
813
814DEFINE_COPROCESSOR_INSTRUCTION(MRC,
815	if (cp == 15 && cpu->irqh.readCP15) {
816		cpu->gprs[rd] = cpu->irqh.readCP15(cpu, crn, crm, op1, op2);
817	} else {
818		ARM_STUB;
819	})
820
821DEFINE_COPROCESSOR_INSTRUCTION(MCR,
822	if (cp == 15 && cpu->irqh.writeCP15) {
823		cpu->irqh.writeCP15(cpu, crn, crm, op1, op2, cpu->gprs[rd]);
824	} else {
825		ARM_STUB;
826	})
827
828DEFINE_INSTRUCTION_ARM(CDP, ARM_STUB)
829DEFINE_INSTRUCTION_ARM(LDC, ARM_STUB)
830DEFINE_INSTRUCTION_ARM(STC, ARM_STUB)
831
832// Begin miscellaneous definitions
833
834DEFINE_INSTRUCTION_ARM(CLZ,
835	int rm = opcode & 0xF;
836	int rd = (opcode >> 12) & 0xF;
837	cpu->gprs[rd] = clz32(cpu->gprs[rm]);)
838
839DEFINE_INSTRUCTION_ARM(BKPT, cpu->irqh.bkpt32(cpu, ((opcode >> 4) & 0xFFF0) | (opcode & 0xF))); // Not strictly in ARMv4T, but here for convenience
840DEFINE_INSTRUCTION_ARM(ILL, ARM_ILL) // Illegal opcode
841
842DEFINE_INSTRUCTION_ARM(MSR,
843	int c = opcode & 0x00010000;
844	int f = opcode & 0x00080000;
845	int32_t operand = cpu->gprs[opcode & 0x0000000F];
846	int32_t mask = (c ? 0x000000FF : 0) | (f ? 0xFF000000 : 0);
847	if (mask & PSR_USER_MASK) {
848		cpu->cpsr.packed = (cpu->cpsr.packed & ~PSR_USER_MASK) | (operand & PSR_USER_MASK);
849	}
850	if (mask & PSR_STATE_MASK) {
851		cpu->cpsr.packed = (cpu->cpsr.packed & ~PSR_STATE_MASK) | (operand & PSR_STATE_MASK);
852	}
853	if (cpu->privilegeMode != MODE_USER && (mask & PSR_PRIV_MASK)) {
854		ARMSetPrivilegeMode(cpu, (enum PrivilegeMode) ((operand & 0x0000000F) | 0x00000010));
855		cpu->cpsr.packed = (cpu->cpsr.packed & ~PSR_PRIV_MASK) | (operand & PSR_PRIV_MASK);
856	}
857	_ARMReadCPSR(cpu);
858	if (cpu->executionMode == MODE_THUMB) {
859		cpu->prefetch[0] = 0x46C0; // nop
860		cpu->prefetch[1] &= 0xFFFF;
861		cpu->gprs[ARM_PC] += WORD_SIZE_THUMB;
862	} else {
863		LOAD_32(cpu->prefetch[0], (cpu->gprs[ARM_PC] - WORD_SIZE_ARM) & cpu->memory.activeMask, cpu->memory.activeRegion);
864		LOAD_32(cpu->prefetch[1], cpu->gprs[ARM_PC] & cpu->memory.activeMask, cpu->memory.activeRegion);
865	})
866
867DEFINE_INSTRUCTION_ARM(MSRR,
868	int c = opcode & 0x00010000;
869	int f = opcode & 0x00080000;
870	int32_t operand = cpu->gprs[opcode & 0x0000000F];
871	int32_t mask = (c ? 0x000000FF : 0) | (f ? 0xFF000000 : 0);
872	mask &= PSR_USER_MASK | PSR_PRIV_MASK | PSR_STATE_MASK;
873	cpu->spsr.packed = (cpu->spsr.packed & ~mask) | (operand & mask) | 0x00000010;)
874
875DEFINE_INSTRUCTION_ARM(MRS, \
876	int rd = (opcode >> 12) & 0xF; \
877	cpu->gprs[rd] = cpu->cpsr.packed;)
878
879DEFINE_INSTRUCTION_ARM(MRSR, \
880	int rd = (opcode >> 12) & 0xF; \
881	cpu->gprs[rd] = cpu->spsr.packed;)
882
883DEFINE_INSTRUCTION_ARM(MSRI,
884	int c = opcode & 0x00010000;
885	int f = opcode & 0x00080000;
886	int rotate = (opcode & 0x00000F00) >> 7;
887	int32_t operand = ROR(opcode & 0x000000FF, rotate);
888	int32_t mask = (c ? 0x000000FF : 0) | (f ? 0xFF000000 : 0);
889	if (mask & PSR_USER_MASK) {
890		cpu->cpsr.packed = (cpu->cpsr.packed & ~PSR_USER_MASK) | (operand & PSR_USER_MASK);
891	}
892	if (mask & PSR_STATE_MASK) {
893		cpu->cpsr.packed = (cpu->cpsr.packed & ~PSR_STATE_MASK) | (operand & PSR_STATE_MASK);
894	}
895	if (cpu->privilegeMode != MODE_USER && (mask & PSR_PRIV_MASK)) {
896		ARMSetPrivilegeMode(cpu, (enum PrivilegeMode) ((operand & 0x0000000F) | 0x00000010));
897		cpu->cpsr.packed = (cpu->cpsr.packed & ~PSR_PRIV_MASK) | (operand & PSR_PRIV_MASK);
898	}
899	_ARMReadCPSR(cpu);
900	if (cpu->executionMode == MODE_THUMB) {
901		cpu->prefetch[0] = 0x46C0; // nop
902		cpu->prefetch[1] &= 0xFFFF;
903		cpu->gprs[ARM_PC] += WORD_SIZE_THUMB;
904	} else {
905		LOAD_32(cpu->prefetch[0], (cpu->gprs[ARM_PC] - WORD_SIZE_ARM) & cpu->memory.activeMask, cpu->memory.activeRegion);
906		LOAD_32(cpu->prefetch[1], cpu->gprs[ARM_PC] & cpu->memory.activeMask, cpu->memory.activeRegion);
907	})
908
909DEFINE_INSTRUCTION_ARM(MSRRI,
910	int c = opcode & 0x00010000;
911	int f = opcode & 0x00080000;
912	int rotate = (opcode & 0x00000F00) >> 7;
913	int32_t operand = ROR(opcode & 0x000000FF, rotate);
914	int32_t mask = (c ? 0x000000FF : 0) | (f ? 0xFF000000 : 0);
915	mask &= PSR_USER_MASK | PSR_PRIV_MASK | PSR_STATE_MASK;
916	cpu->spsr.packed = (cpu->spsr.packed & ~mask) | (operand & mask) | 0x00000010;)
917
918DEFINE_INSTRUCTION_ARM(SWI, cpu->irqh.swi32(cpu, opcode & 0xFFFFFF))
919
920const ARMInstruction ARMv4InstructionTable[0x1000] = {
921	DECLARE_ARM_EMITTER_BLOCK(_ARMInstruction, 4)
922};
923
924const ARMInstruction ARMv5InstructionTable[0x1000] = {
925	DECLARE_ARM_EMITTER_BLOCK(_ARMInstruction, 5)
926};
927
928const ARMInstruction ARMv5FInstructionTable[0x1000] = {
929	DECLARE_ARM_F_EMITTER_BLOCK(_ARMInstruction, 5)
930};