all repos — mgba @ fbba3efb344df982f32cbf9cf3252b193263a9d3

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