all repos — mgba @ 662feed39045ff0b43d28e97e4c80243a2951518

mGBA Game Boy Advance Emulator

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

   1#include "isa-arm.h"
   2
   3#include "arm.h"
   4#include "isa-inlines.h"
   5
   6enum {
   7	PSR_USER_MASK = 0xF0000000,
   8	PSR_PRIV_MASK = 0x000000CF,
   9	PSR_STATE_MASK = 0x00000020
  10};
  11
  12#define ARM_PREFETCH_CYCLES (1 + cpu->memory->activePrefetchCycles32)
  13
  14// Addressing mode 1
  15static inline void _shiftLSL(struct ARMCore* cpu, uint32_t opcode) {
  16	int rm = opcode & 0x0000000F;
  17	int immediate = (opcode & 0x00000F80) >> 7;
  18	if (!immediate) {
  19		cpu->shifterOperand = cpu->gprs[rm];
  20		cpu->shifterCarryOut = cpu->cpsr.c;
  21	} else {
  22		cpu->shifterOperand = cpu->gprs[rm] << immediate;
  23		cpu->shifterCarryOut = (cpu->gprs[rm] >> (32 - immediate)) & 1;
  24	}
  25}
  26
  27static inline void _shiftLSLR(struct ARMCore* cpu, uint32_t opcode) {
  28	int rm = opcode & 0x0000000F;
  29	int rs = (opcode >> 8) & 0x0000000F;
  30	++cpu->cycles;
  31	int shift = cpu->gprs[rs];
  32	if (rs == ARM_PC) {
  33		shift += 4;
  34	}
  35	shift &= 0xFF;
  36	int32_t shiftVal = cpu->gprs[rm];
  37	if (rm == ARM_PC) {
  38		shiftVal += 4;
  39	}
  40	if (!shift) {
  41		cpu->shifterOperand = shiftVal;
  42		cpu->shifterCarryOut = cpu->cpsr.c;
  43	} else if (shift < 32) {
  44		cpu->shifterOperand = shiftVal << shift;
  45		cpu->shifterCarryOut = (shiftVal >> (32 - shift)) & 1;
  46	} else if (shift == 32) {
  47		cpu->shifterOperand = 0;
  48		cpu->shifterCarryOut = shiftVal & 1;
  49	} else {
  50		cpu->shifterOperand = 0;
  51		cpu->shifterCarryOut = 0;
  52	}
  53}
  54
  55static inline void _shiftLSR(struct ARMCore* cpu, uint32_t opcode) {
  56	int rm = opcode & 0x0000000F;
  57	int immediate = (opcode & 0x00000F80) >> 7;
  58	if (immediate) {
  59		cpu->shifterOperand = ((uint32_t) cpu->gprs[rm]) >> immediate;
  60		cpu->shifterCarryOut = (cpu->gprs[rm] >> (immediate - 1)) & 1;
  61	} else {
  62		cpu->shifterOperand = 0;
  63		cpu->shifterCarryOut = ARM_SIGN(cpu->gprs[rm]);
  64	}
  65}
  66
  67static inline void _shiftLSRR(struct ARMCore* cpu, uint32_t opcode) {
  68	int rm = opcode & 0x0000000F;
  69	int rs = (opcode >> 8) & 0x0000000F;
  70	++cpu->cycles;
  71	int shift = cpu->gprs[rs];
  72	if (rs == ARM_PC) {
  73		shift += 4;
  74	}
  75	shift &= 0xFF;
  76	uint32_t shiftVal = cpu->gprs[rm];
  77	if (rm == ARM_PC) {
  78		shiftVal += 4;
  79	}
  80	if (!shift) {
  81		cpu->shifterOperand = shiftVal;
  82		cpu->shifterCarryOut = cpu->cpsr.c;
  83	} else if (shift < 32) {
  84		cpu->shifterOperand = shiftVal >> shift;
  85		cpu->shifterCarryOut = (shiftVal >> (shift - 1)) & 1;
  86	} else if (shift == 32) {
  87		cpu->shifterOperand = 0;
  88		cpu->shifterCarryOut = shiftVal >> 31;
  89	} else {
  90		cpu->shifterOperand = 0;
  91		cpu->shifterCarryOut = 0;
  92	}
  93}
  94
  95static inline void _shiftASR(struct ARMCore* cpu, uint32_t opcode) {
  96	int rm = opcode & 0x0000000F;
  97	int immediate = (opcode & 0x00000F80) >> 7;
  98	if (immediate) {
  99		cpu->shifterOperand = cpu->gprs[rm] >> immediate;
 100		cpu->shifterCarryOut = (cpu->gprs[rm] >> (immediate - 1)) & 1;
 101	} else {
 102		cpu->shifterCarryOut = ARM_SIGN(cpu->gprs[rm]);
 103		cpu->shifterOperand = cpu->shifterCarryOut;
 104	}
 105}
 106
 107static inline void _shiftASRR(struct ARMCore* cpu, uint32_t opcode) {
 108	int rm = opcode & 0x0000000F;
 109	int rs = (opcode >> 8) & 0x0000000F;
 110	++cpu->cycles;
 111	int shift = cpu->gprs[rs];
 112	if (rs == ARM_PC) {
 113		shift += 4;
 114	}
 115	shift &= 0xFF;
 116	int shiftVal =  cpu->gprs[rm];
 117	if (rm == ARM_PC) {
 118		shiftVal += 4;
 119	}
 120	if (!shift) {
 121		cpu->shifterOperand = shiftVal;
 122		cpu->shifterCarryOut = cpu->cpsr.c;
 123	} else if (shift < 32) {
 124		cpu->shifterOperand = shiftVal >> shift;
 125		cpu->shifterCarryOut = (shiftVal >> (shift - 1)) & 1;
 126	} else if (cpu->gprs[rm] >> 31) {
 127		cpu->shifterOperand = 0xFFFFFFFF;
 128		cpu->shifterCarryOut = 1;
 129	} else {
 130		cpu->shifterOperand = 0;
 131		cpu->shifterCarryOut = 0;
 132	}
 133}
 134
 135static inline void _shiftROR(struct ARMCore* cpu, uint32_t opcode) {
 136	int rm = opcode & 0x0000000F;
 137	int immediate = (opcode & 0x00000F80) >> 7;
 138	if (immediate) {
 139		cpu->shifterOperand = ARM_ROR(cpu->gprs[rm], immediate);
 140		cpu->shifterCarryOut = (cpu->gprs[rm] >> (immediate - 1)) & 1;
 141	} else {
 142		// RRX
 143		cpu->shifterOperand = (cpu->cpsr.c << 31) | (((uint32_t) cpu->gprs[rm]) >> 1);
 144		cpu->shifterCarryOut = cpu->gprs[rm] & 0x00000001;
 145	}
 146}
 147
 148static inline void _shiftRORR(struct ARMCore* cpu, uint32_t opcode) {
 149	int rm = opcode & 0x0000000F;
 150	int rs = (opcode >> 8) & 0x0000000F;
 151	++cpu->cycles;
 152	int shift = cpu->gprs[rs];
 153	if (rs == ARM_PC) {
 154		shift += 4;
 155	}
 156	shift &= 0xFF;
 157	int shiftVal =  cpu->gprs[rm];
 158	if (rm == ARM_PC) {
 159		shiftVal += 4;
 160	}
 161	int rotate = shift & 0x1F;
 162	if (!shift) {
 163		cpu->shifterOperand = shiftVal;
 164		cpu->shifterCarryOut = cpu->cpsr.c;
 165	} else if (rotate) {
 166		cpu->shifterOperand = ARM_ROR(shiftVal, rotate);
 167		cpu->shifterCarryOut = (shiftVal >> (rotate - 1)) & 1;
 168	} else {
 169		cpu->shifterOperand = shiftVal;
 170		cpu->shifterCarryOut = ARM_SIGN(shiftVal);
 171	}
 172}
 173
 174static inline void _immediate(struct ARMCore* cpu, uint32_t opcode) {
 175	int rotate = (opcode & 0x00000F00) >> 7;
 176	int immediate = opcode & 0x000000FF;
 177	if (!rotate) {
 178		cpu->shifterOperand = immediate;
 179		cpu->shifterCarryOut = cpu->cpsr.c;
 180	} else {
 181		cpu->shifterOperand = ARM_ROR(immediate, rotate);
 182		cpu->shifterCarryOut = ARM_SIGN(cpu->shifterOperand);
 183	}
 184}
 185
 186static const ARMInstruction _armTable[0x1000];
 187
 188static ARMInstruction _ARMLoadInstructionARM(struct ARMMemory* memory, uint32_t address, uint32_t* opcodeOut) {
 189	uint32_t opcode = memory->activeRegion[(address & memory->activeMask) >> 2];
 190	*opcodeOut = opcode;
 191	return _armTable[((opcode >> 16) & 0xFF0) | ((opcode >> 4) & 0x00F)];
 192}
 193
 194void ARMStep(struct ARMCore* cpu) {
 195	// TODO
 196	uint32_t opcode;
 197	ARMInstruction instruction = _ARMLoadInstructionARM(cpu->memory, cpu->gprs[ARM_PC] - WORD_SIZE_ARM, &opcode);
 198	cpu->gprs[ARM_PC] += WORD_SIZE_ARM;
 199
 200	int condition = opcode >> 28;
 201	if (condition == 0xE) {
 202		instruction(cpu, opcode);
 203		return;
 204	} else {
 205		switch (condition) {
 206		case 0x0:
 207			if (!ARM_COND_EQ) {
 208				cpu->cycles += ARM_PREFETCH_CYCLES;
 209				return;
 210			}
 211			break;
 212		case 0x1:
 213			if (!ARM_COND_NE) {
 214				cpu->cycles += ARM_PREFETCH_CYCLES;
 215				return;
 216			}
 217			break;
 218		case 0x2:
 219			if (!ARM_COND_CS) {
 220				cpu->cycles += ARM_PREFETCH_CYCLES;
 221				return;
 222			}
 223			break;
 224		case 0x3:
 225			if (!ARM_COND_CC) {
 226				cpu->cycles += ARM_PREFETCH_CYCLES;
 227				return;
 228			}
 229			break;
 230		case 0x4:
 231			if (!ARM_COND_MI) {
 232				cpu->cycles += ARM_PREFETCH_CYCLES;
 233				return;
 234			}
 235			break;
 236		case 0x5:
 237			if (!ARM_COND_PL) {
 238				cpu->cycles += ARM_PREFETCH_CYCLES;
 239				return;
 240			}
 241			break;
 242		case 0x6:
 243			if (!ARM_COND_VS) {
 244				cpu->cycles += ARM_PREFETCH_CYCLES;
 245				return;
 246			}
 247			break;
 248		case 0x7:
 249			if (!ARM_COND_VC) {
 250				cpu->cycles += ARM_PREFETCH_CYCLES;
 251				return;
 252			}
 253			break;
 254		case 0x8:
 255			if (!ARM_COND_HI) {
 256				cpu->cycles += ARM_PREFETCH_CYCLES;
 257				return;
 258			}
 259			break;
 260		case 0x9:
 261			if (!ARM_COND_LS) {
 262				cpu->cycles += ARM_PREFETCH_CYCLES;
 263				return;
 264			}
 265			break;
 266		case 0xA:
 267			if (!ARM_COND_GE) {
 268				cpu->cycles += ARM_PREFETCH_CYCLES;
 269				return;
 270			}
 271			break;
 272		case 0xB:
 273			if (!ARM_COND_LT) {
 274				cpu->cycles += ARM_PREFETCH_CYCLES;
 275				return;
 276			}
 277			break;
 278		case 0xC:
 279			if (!ARM_COND_GT) {
 280				cpu->cycles += ARM_PREFETCH_CYCLES;
 281				return;
 282			}
 283			break;
 284		case 0xD:
 285			if (!ARM_COND_LE) {
 286				cpu->cycles += ARM_PREFETCH_CYCLES;
 287				return;
 288			}
 289			break;
 290		default:
 291			break;
 292		}
 293	}
 294	instruction(cpu, opcode);
 295}
 296
 297// Instruction definitions
 298// Beware pre-processor antics
 299
 300#define NO_EXTEND64(V) (uint64_t)(uint32_t) (V)
 301
 302#define ARM_ADDITION_S(M, N, D) \
 303	if (rd == ARM_PC && _ARMModeHasSPSR(cpu->cpsr.priv)) { \
 304		cpu->cpsr = cpu->spsr; \
 305		_ARMReadCPSR(cpu); \
 306	} else { \
 307		cpu->cpsr.n = ARM_SIGN(D); \
 308		cpu->cpsr.z = !(D); \
 309		cpu->cpsr.c = ARM_CARRY_FROM(M, N, D); \
 310		cpu->cpsr.v = ARM_V_ADDITION(M, N, D); \
 311	}
 312
 313#define ARM_SUBTRACTION_S(M, N, D) \
 314	if (rd == ARM_PC && _ARMModeHasSPSR(cpu->cpsr.priv)) { \
 315		cpu->cpsr = cpu->spsr; \
 316		_ARMReadCPSR(cpu); \
 317	} else { \
 318		cpu->cpsr.n = ARM_SIGN(D); \
 319		cpu->cpsr.z = !(D); \
 320		cpu->cpsr.c = ARM_BORROW_FROM(M, N, D); \
 321		cpu->cpsr.v = ARM_V_SUBTRACTION(M, N, D); \
 322	}
 323
 324#define ARM_NEUTRAL_S(M, N, D) \
 325	if (rd == ARM_PC && _ARMModeHasSPSR(cpu->cpsr.priv)) { \
 326		cpu->cpsr = cpu->spsr; \
 327		_ARMReadCPSR(cpu); \
 328	} else { \
 329		cpu->cpsr.n = ARM_SIGN(D); \
 330		cpu->cpsr.z = !(D); \
 331		cpu->cpsr.c = cpu->shifterCarryOut; \
 332	}
 333
 334#define ARM_NEUTRAL_HI_S(DLO, DHI) \
 335	cpu->cpsr.n = ARM_SIGN(DHI); \
 336	cpu->cpsr.z = !((DHI) | (DLO));
 337
 338#define ADDR_MODE_2_I_TEST (opcode & 0x00000F80)
 339#define ADDR_MODE_2_I ((opcode & 0x00000F80) >> 7)
 340#define ADDR_MODE_2_ADDRESS (address)
 341#define ADDR_MODE_2_RN (cpu->gprs[rn])
 342#define ADDR_MODE_2_RM (cpu->gprs[rm])
 343#define ADDR_MODE_2_IMMEDIATE (opcode & 0x00000FFF)
 344#define ADDR_MODE_2_INDEX(U_OP, M) (cpu->gprs[rn] U_OP M)
 345#define ADDR_MODE_2_WRITEBACK(ADDR) (cpu->gprs[rn] = ADDR)
 346#define ADDR_MODE_2_LSL (cpu->gprs[rm] << ADDR_MODE_2_I)
 347#define ADDR_MODE_2_LSR (ADDR_MODE_2_I_TEST ? ((uint32_t) cpu->gprs[rm]) >> ADDR_MODE_2_I : 0)
 348#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)
 349#define ADDR_MODE_2_ROR (ADDR_MODE_2_I_TEST ? ARM_ROR(cpu->gprs[rm], ADDR_MODE_2_I) : (cpu->cpsr.c << 31) | (((uint32_t) cpu->gprs[rm]) >> 1))
 350
 351#define ADDR_MODE_3_ADDRESS ADDR_MODE_2_ADDRESS
 352#define ADDR_MODE_3_RN ADDR_MODE_2_RN
 353#define ADDR_MODE_3_RM ADDR_MODE_2_RM
 354#define ADDR_MODE_3_IMMEDIATE (((opcode & 0x00000F00) >> 4) | (opcode & 0x0000000F))
 355#define ADDR_MODE_3_INDEX(U_OP, M) ADDR_MODE_2_INDEX(U_OP, M)
 356#define ADDR_MODE_3_WRITEBACK(ADDR) ADDR_MODE_2_WRITEBACK(ADDR)
 357
 358#define ARM_LOAD_POST_BODY \
 359	if (rd == ARM_PC) { \
 360		ARM_WRITE_PC; \
 361	}
 362
 363#define ARM_STORE_POST_BODY \
 364	currentCycles -= ARM_PREFETCH_CYCLES; \
 365	currentCycles += 1 + cpu->memory->activeNonseqCycles32;
 366
 367#define DEFINE_INSTRUCTION_ARM(NAME, BODY) \
 368	static void _ARMInstruction ## NAME (struct ARMCore* cpu, uint32_t opcode) { \
 369		int currentCycles = ARM_PREFETCH_CYCLES; \
 370		BODY; \
 371		cpu->cycles += currentCycles; \
 372	}
 373
 374#define DEFINE_ALU_INSTRUCTION_EX_ARM(NAME, S_BODY, SHIFTER, BODY) \
 375	DEFINE_INSTRUCTION_ARM(NAME, \
 376		int rd = (opcode >> 12) & 0xF; \
 377		int rn = (opcode >> 16) & 0xF; \
 378		UNUSED(rn); \
 379		SHIFTER(cpu, opcode); \
 380		BODY; \
 381		S_BODY; \
 382		if (rd == ARM_PC) { \
 383			if (cpu->executionMode == MODE_ARM) { \
 384				ARM_WRITE_PC; \
 385			} else { \
 386				THUMB_WRITE_PC; \
 387			} \
 388		})
 389
 390#define DEFINE_ALU_INSTRUCTION_ARM(NAME, S_BODY, BODY) \
 391	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _LSL, , _shiftLSL, BODY) \
 392	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## S_LSL, S_BODY, _shiftLSL, BODY) \
 393	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _LSLR, , _shiftLSLR, BODY) \
 394	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## S_LSLR, S_BODY, _shiftLSLR, BODY) \
 395	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _LSR, , _shiftLSR, BODY) \
 396	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## S_LSR, S_BODY, _shiftLSR, BODY) \
 397	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _LSRR, , _shiftLSRR, BODY) \
 398	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## S_LSRR, S_BODY, _shiftLSRR, BODY) \
 399	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _ASR, , _shiftASR, BODY) \
 400	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## S_ASR, S_BODY, _shiftASR, BODY) \
 401	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _ASRR, , _shiftASRR, BODY) \
 402	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## S_ASRR, S_BODY, _shiftASRR, BODY) \
 403	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _ROR, , _shiftROR, BODY) \
 404	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## S_ROR, S_BODY, _shiftROR, BODY) \
 405	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _RORR, , _shiftRORR, BODY) \
 406	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## S_RORR, S_BODY, _shiftRORR, BODY) \
 407	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## I, , _immediate, BODY) \
 408	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## SI, S_BODY, _immediate, BODY)
 409
 410#define DEFINE_ALU_INSTRUCTION_S_ONLY_ARM(NAME, S_BODY, BODY) \
 411	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _LSL, S_BODY, _shiftLSL, BODY) \
 412	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _LSLR, S_BODY, _shiftLSLR, BODY) \
 413	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _LSR, S_BODY, _shiftLSR, BODY) \
 414	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _LSRR, S_BODY, _shiftLSRR, BODY) \
 415	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _ASR, S_BODY, _shiftASR, BODY) \
 416	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _ASRR, S_BODY, _shiftASRR, BODY) \
 417	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _ROR, S_BODY, _shiftROR, BODY) \
 418	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## _RORR, S_BODY, _shiftRORR, BODY) \
 419	DEFINE_ALU_INSTRUCTION_EX_ARM(NAME ## I, S_BODY, _immediate, BODY)
 420
 421#define DEFINE_MULTIPLY_INSTRUCTION_EX_ARM(NAME, BODY, S_BODY) \
 422	DEFINE_INSTRUCTION_ARM(NAME, \
 423		int rd = (opcode >> 12) & 0xF; \
 424		int rdHi = (opcode >> 16) & 0xF; \
 425		int rs = (opcode >> 8) & 0xF; \
 426		int rm = opcode & 0xF; \
 427		UNUSED(rdHi); \
 428		ARM_WAIT_MUL(cpu->gprs[rs]); \
 429		BODY; \
 430		S_BODY; \
 431		if (rd == ARM_PC) { \
 432			ARM_WRITE_PC; \
 433		})
 434
 435#define DEFINE_MULTIPLY_INSTRUCTION_ARM(NAME, BODY, S_BODY) \
 436	DEFINE_MULTIPLY_INSTRUCTION_EX_ARM(NAME, BODY, ) \
 437	DEFINE_MULTIPLY_INSTRUCTION_EX_ARM(NAME ## S, BODY, S_BODY)
 438
 439#define DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME, ADDRESS, WRITEBACK, BODY) \
 440	DEFINE_INSTRUCTION_ARM(NAME, \
 441		uint32_t address; \
 442		int rn = (opcode >> 16) & 0xF; \
 443		int rd = (opcode >> 12) & 0xF; \
 444		int rm = opcode & 0xF; \
 445		UNUSED(rm); \
 446		address = ADDRESS; \
 447		WRITEBACK; \
 448		BODY;)
 449
 450#define DEFINE_LOAD_STORE_INSTRUCTION_SHIFTER_ARM(NAME, SHIFTER, BODY) \
 451	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME, ADDR_MODE_2_RN, ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_INDEX(-, SHIFTER)), BODY) \
 452	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## U, ADDR_MODE_2_RN, ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_INDEX(+, SHIFTER)), BODY) \
 453	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## P, ADDR_MODE_2_INDEX(-, SHIFTER), , BODY) \
 454	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## PW, ADDR_MODE_2_INDEX(-, SHIFTER), ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_ADDRESS), BODY) \
 455	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## PU, ADDR_MODE_2_INDEX(+, SHIFTER), , BODY) \
 456	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## PUW, ADDR_MODE_2_INDEX(+, SHIFTER), ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_ADDRESS), BODY)
 457
 458#define DEFINE_LOAD_STORE_INSTRUCTION_ARM(NAME, BODY) \
 459	DEFINE_LOAD_STORE_INSTRUCTION_SHIFTER_ARM(NAME ## _LSL_, ADDR_MODE_2_LSL, BODY) \
 460	DEFINE_LOAD_STORE_INSTRUCTION_SHIFTER_ARM(NAME ## _LSR_, ADDR_MODE_2_LSR, BODY) \
 461	DEFINE_LOAD_STORE_INSTRUCTION_SHIFTER_ARM(NAME ## _ASR_, ADDR_MODE_2_ASR, BODY) \
 462	DEFINE_LOAD_STORE_INSTRUCTION_SHIFTER_ARM(NAME ## _ROR_, ADDR_MODE_2_ROR, BODY) \
 463	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## I, ADDR_MODE_2_RN, ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_INDEX(-, ADDR_MODE_2_IMMEDIATE)), BODY) \
 464	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IU, ADDR_MODE_2_RN, ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_INDEX(+, ADDR_MODE_2_IMMEDIATE)), BODY) \
 465	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IP, ADDR_MODE_2_INDEX(-, ADDR_MODE_2_IMMEDIATE), , BODY) \
 466	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IPW, ADDR_MODE_2_INDEX(-, ADDR_MODE_2_IMMEDIATE), ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_ADDRESS), BODY) \
 467	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IPU, ADDR_MODE_2_INDEX(+, ADDR_MODE_2_IMMEDIATE), , BODY) \
 468	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IPUW, ADDR_MODE_2_INDEX(+, ADDR_MODE_2_IMMEDIATE), ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_ADDRESS), BODY) \
 469
 470#define DEFINE_LOAD_STORE_MODE_3_INSTRUCTION_ARM(NAME, BODY) \
 471	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME, ADDR_MODE_3_RN, ADDR_MODE_3_WRITEBACK(ADDR_MODE_3_INDEX(-, ADDR_MODE_3_RM)), BODY) \
 472	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## U, ADDR_MODE_3_RN, ADDR_MODE_3_WRITEBACK(ADDR_MODE_3_INDEX(+, ADDR_MODE_3_RM)), BODY) \
 473	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## P, ADDR_MODE_3_INDEX(-, ADDR_MODE_3_RM), , BODY) \
 474	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## PW, ADDR_MODE_3_INDEX(-, ADDR_MODE_3_RM), ADDR_MODE_3_WRITEBACK(ADDR_MODE_3_ADDRESS), BODY) \
 475	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## PU, ADDR_MODE_3_INDEX(+, ADDR_MODE_3_RM), , BODY) \
 476	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## PUW, ADDR_MODE_3_INDEX(+, ADDR_MODE_3_RM), ADDR_MODE_3_WRITEBACK(ADDR_MODE_3_ADDRESS), BODY) \
 477	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## I, ADDR_MODE_3_RN, ADDR_MODE_3_WRITEBACK(ADDR_MODE_3_INDEX(-, ADDR_MODE_3_IMMEDIATE)), BODY) \
 478	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IU, ADDR_MODE_3_RN, ADDR_MODE_3_WRITEBACK(ADDR_MODE_3_INDEX(+, ADDR_MODE_3_IMMEDIATE)), BODY) \
 479	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IP, ADDR_MODE_3_INDEX(-, ADDR_MODE_3_IMMEDIATE), , BODY) \
 480	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IPW, ADDR_MODE_3_INDEX(-, ADDR_MODE_3_IMMEDIATE), ADDR_MODE_3_WRITEBACK(ADDR_MODE_3_ADDRESS), BODY) \
 481	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IPU, ADDR_MODE_3_INDEX(+, ADDR_MODE_3_IMMEDIATE), , BODY) \
 482	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IPUW, ADDR_MODE_3_INDEX(+, ADDR_MODE_3_IMMEDIATE), ADDR_MODE_3_WRITEBACK(ADDR_MODE_3_ADDRESS), BODY) \
 483
 484#define DEFINE_LOAD_STORE_T_INSTRUCTION_SHIFTER_ARM(NAME, SHIFTER, BODY) \
 485	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME, SHIFTER, ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_INDEX(-, ADDR_MODE_2_RM)), BODY) \
 486	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## U, SHIFTER, ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_INDEX(+, ADDR_MODE_2_RM)), BODY) \
 487
 488#define DEFINE_LOAD_STORE_T_INSTRUCTION_ARM(NAME, BODY) \
 489	DEFINE_LOAD_STORE_T_INSTRUCTION_SHIFTER_ARM(NAME ## _LSL_, ADDR_MODE_2_LSL, BODY) \
 490	DEFINE_LOAD_STORE_T_INSTRUCTION_SHIFTER_ARM(NAME ## _LSR_, ADDR_MODE_2_LSR, BODY) \
 491	DEFINE_LOAD_STORE_T_INSTRUCTION_SHIFTER_ARM(NAME ## _ASR_, ADDR_MODE_2_ASR, BODY) \
 492	DEFINE_LOAD_STORE_T_INSTRUCTION_SHIFTER_ARM(NAME ## _ROR_, ADDR_MODE_2_ROR, BODY) \
 493	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## I, ADDR_MODE_2_RN, ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_INDEX(-, ADDR_MODE_2_IMMEDIATE)), BODY) \
 494	DEFINE_LOAD_STORE_INSTRUCTION_EX_ARM(NAME ## IU, ADDR_MODE_2_RN, ADDR_MODE_2_WRITEBACK(ADDR_MODE_2_INDEX(+, ADDR_MODE_2_IMMEDIATE)), BODY) \
 495
 496#define ARM_MS_PRE \
 497	enum PrivilegeMode privilegeMode = cpu->privilegeMode; \
 498	ARMSetPrivilegeMode(cpu, MODE_SYSTEM);
 499
 500#define ARM_MS_POST ARMSetPrivilegeMode(cpu, privilegeMode);
 501
 502#define ADDR_MODE_4_DA uint32_t addr = cpu->gprs[rn]
 503#define ADDR_MODE_4_IA uint32_t addr = cpu->gprs[rn]
 504#define ADDR_MODE_4_DB uint32_t addr = cpu->gprs[rn] - 4
 505#define ADDR_MODE_4_IB uint32_t addr = cpu->gprs[rn] + 4
 506#define ADDR_MODE_4_DAW cpu->gprs[rn] = addr
 507#define ADDR_MODE_4_IAW cpu->gprs[rn] = addr
 508#define ADDR_MODE_4_DBW cpu->gprs[rn] = addr + 4
 509#define ADDR_MODE_4_IBW cpu->gprs[rn] = addr - 4
 510
 511#define ARM_M_INCREMENT(BODY) \
 512	for (m = rs, i = 0; m; m >>= 1, ++i) { \
 513		if (m & 1) { \
 514			BODY; \
 515			addr += 4; \
 516			total += 1; \
 517		} \
 518	}
 519
 520#define ARM_M_DECREMENT(BODY) \
 521	for (m = 0x8000, i = 15; m; m >>= 1, --i) { \
 522		if (rs & m) { \
 523			BODY; \
 524			addr -= 4; \
 525			total += 1; \
 526		} \
 527	}
 528
 529#define DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME, ADDRESS, WRITEBACK, LOOP, S_PRE, S_POST, BODY, POST_BODY) \
 530	DEFINE_INSTRUCTION_ARM(NAME, \
 531		int rn = (opcode >> 16) & 0xF; \
 532		int rs = opcode & 0x0000FFFF; \
 533		int writeback = 1; \
 534		int m; \
 535		int i; \
 536		int total = 0; \
 537		ADDRESS; \
 538		S_PRE; \
 539		LOOP(BODY); \
 540		S_POST; \
 541		currentCycles += cpu->memory->waitMultiple(cpu->memory, addr, total); \
 542		POST_BODY; \
 543		if (writeback) { \
 544			WRITEBACK; \
 545		})
 546
 547
 548#define DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_ARM(NAME, BODY, POST_BODY) \
 549	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## DA,   ADDR_MODE_4_DA,                , ARM_M_DECREMENT, , , BODY, POST_BODY) \
 550	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## DAW,  ADDR_MODE_4_DA, ADDR_MODE_4_DAW, ARM_M_DECREMENT, , , BODY, POST_BODY) \
 551	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## DB,   ADDR_MODE_4_DB,                , ARM_M_DECREMENT, , , BODY, POST_BODY) \
 552	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## DBW,  ADDR_MODE_4_DB, ADDR_MODE_4_DBW, ARM_M_DECREMENT, , , BODY, POST_BODY) \
 553	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## IA,   ADDR_MODE_4_IA,                , ARM_M_INCREMENT, , , BODY, POST_BODY) \
 554	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## IAW,  ADDR_MODE_4_IA, ADDR_MODE_4_IAW, ARM_M_INCREMENT, , , BODY, POST_BODY) \
 555	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## IB,   ADDR_MODE_4_IB,                , ARM_M_INCREMENT, , , BODY, POST_BODY) \
 556	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## IBW,  ADDR_MODE_4_IB, ADDR_MODE_4_IBW, ARM_M_INCREMENT, , , BODY, POST_BODY) \
 557	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## SDA,  ADDR_MODE_4_DA,                , ARM_M_DECREMENT, ARM_MS_PRE, ARM_MS_POST, BODY, POST_BODY) \
 558	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## SDAW, ADDR_MODE_4_DA, ADDR_MODE_4_DAW, ARM_M_DECREMENT, ARM_MS_PRE, ARM_MS_POST, BODY, POST_BODY) \
 559	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## SDB,  ADDR_MODE_4_DB,                , ARM_M_DECREMENT, ARM_MS_PRE, ARM_MS_POST, BODY, POST_BODY) \
 560	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## SDBW, ADDR_MODE_4_DB, ADDR_MODE_4_DBW, ARM_M_DECREMENT, ARM_MS_PRE, ARM_MS_POST, BODY, POST_BODY) \
 561	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## SIA,  ADDR_MODE_4_IA,                , ARM_M_INCREMENT, ARM_MS_PRE, ARM_MS_POST, BODY, POST_BODY) \
 562	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## SIAW, ADDR_MODE_4_IA, ADDR_MODE_4_IAW, ARM_M_INCREMENT, ARM_MS_PRE, ARM_MS_POST, BODY, POST_BODY) \
 563	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## SIB,  ADDR_MODE_4_IB,                , ARM_M_INCREMENT, ARM_MS_PRE, ARM_MS_POST, BODY, POST_BODY) \
 564	DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_EX_ARM(NAME ## SIBW, ADDR_MODE_4_IB, ADDR_MODE_4_IBW, ARM_M_INCREMENT, ARM_MS_PRE, ARM_MS_POST, BODY, POST_BODY)
 565
 566// Begin ALU definitions
 567
 568DEFINE_ALU_INSTRUCTION_ARM(ADD, ARM_ADDITION_S(n, cpu->shifterOperand, cpu->gprs[rd]),
 569	int32_t n = cpu->gprs[rn];
 570	cpu->gprs[rd] = n + cpu->shifterOperand;)
 571
 572DEFINE_ALU_INSTRUCTION_ARM(ADC, ARM_ADDITION_S(n, shifterOperand, cpu->gprs[rd]),
 573	int32_t n = cpu->gprs[rn];
 574	int32_t shifterOperand = cpu->shifterOperand + cpu->cpsr.c;
 575	cpu->gprs[rd] = n + shifterOperand;)
 576
 577DEFINE_ALU_INSTRUCTION_ARM(AND, ARM_NEUTRAL_S(cpu->gprs[rn], cpu->shifterOperand, cpu->gprs[rd]),
 578	cpu->gprs[rd] = cpu->gprs[rn] & cpu->shifterOperand;)
 579
 580DEFINE_ALU_INSTRUCTION_ARM(BIC, ARM_NEUTRAL_S(cpu->gprs[rn], cpu->shifterOperand, cpu->gprs[rd]),
 581	cpu->gprs[rd] = cpu->gprs[rn] & ~cpu->shifterOperand;)
 582
 583DEFINE_ALU_INSTRUCTION_S_ONLY_ARM(CMN, ARM_ADDITION_S(cpu->gprs[rn], cpu->shifterOperand, aluOut),
 584	int32_t aluOut = cpu->gprs[rn] + cpu->shifterOperand;)
 585
 586DEFINE_ALU_INSTRUCTION_S_ONLY_ARM(CMP, ARM_SUBTRACTION_S(cpu->gprs[rn], cpu->shifterOperand, aluOut),
 587	int32_t aluOut = cpu->gprs[rn] - cpu->shifterOperand;)
 588
 589DEFINE_ALU_INSTRUCTION_ARM(EOR, ARM_NEUTRAL_S(cpu->gprs[rn], cpu->shifterOperand, cpu->gprs[rd]),
 590	cpu->gprs[rd] = cpu->gprs[rn] ^ cpu->shifterOperand;)
 591
 592DEFINE_ALU_INSTRUCTION_ARM(MOV, ARM_NEUTRAL_S(cpu->gprs[rn], cpu->shifterOperand, cpu->gprs[rd]),
 593	cpu->gprs[rd] = cpu->shifterOperand;)
 594
 595DEFINE_ALU_INSTRUCTION_ARM(MVN, ARM_NEUTRAL_S(cpu->gprs[rn], cpu->shifterOperand, cpu->gprs[rd]),
 596	cpu->gprs[rd] = ~cpu->shifterOperand;)
 597
 598DEFINE_ALU_INSTRUCTION_ARM(ORR, ARM_NEUTRAL_S(cpu->gprs[rn], cpu->shifterOperand, cpu->gprs[rd]),
 599	cpu->gprs[rd] = cpu->gprs[rn] | cpu->shifterOperand;)
 600
 601DEFINE_ALU_INSTRUCTION_ARM(RSB, ARM_SUBTRACTION_S(cpu->shifterOperand, n, cpu->gprs[rd]),
 602	int32_t n = cpu->gprs[rn];
 603	cpu->gprs[rd] = cpu->shifterOperand - n;)
 604
 605DEFINE_ALU_INSTRUCTION_ARM(RSC, ARM_SUBTRACTION_S(cpu->shifterOperand, n, cpu->gprs[rd]),
 606	int32_t n = cpu->gprs[rn] + !cpu->cpsr.c;
 607	cpu->gprs[rd] = cpu->shifterOperand - n;)
 608
 609DEFINE_ALU_INSTRUCTION_ARM(SBC, ARM_SUBTRACTION_S(n, shifterOperand, cpu->gprs[rd]),
 610	int32_t n = cpu->gprs[rn];
 611	int32_t shifterOperand = cpu->shifterOperand + !cpu->cpsr.c;
 612	cpu->gprs[rd] = n - shifterOperand;)
 613
 614DEFINE_ALU_INSTRUCTION_ARM(SUB, ARM_SUBTRACTION_S(n, cpu->shifterOperand, cpu->gprs[rd]),
 615	int32_t n = cpu->gprs[rn];
 616	cpu->gprs[rd] = n - cpu->shifterOperand;)
 617
 618DEFINE_ALU_INSTRUCTION_S_ONLY_ARM(TEQ, ARM_NEUTRAL_S(cpu->gprs[rn], cpu->shifterOperand, aluOut),
 619	int32_t aluOut = cpu->gprs[rn] ^ cpu->shifterOperand;)
 620
 621DEFINE_ALU_INSTRUCTION_S_ONLY_ARM(TST, ARM_NEUTRAL_S(cpu->gprs[rn], cpu->shifterOperand, aluOut),
 622	int32_t aluOut = cpu->gprs[rn] & cpu->shifterOperand;)
 623
 624// End ALU definitions
 625
 626// Begin multiply definitions
 627
 628DEFINE_MULTIPLY_INSTRUCTION_ARM(MLA, cpu->gprs[rdHi] = cpu->gprs[rm] * cpu->gprs[rs] + cpu->gprs[rd], ARM_NEUTRAL_S(, , cpu->gprs[rdHi]))
 629DEFINE_MULTIPLY_INSTRUCTION_ARM(MUL, cpu->gprs[rdHi] = cpu->gprs[rm] * cpu->gprs[rs], ARM_NEUTRAL_S(cpu->gprs[rm], cpu->gprs[rs], cpu->gprs[rdHi]))
 630
 631DEFINE_MULTIPLY_INSTRUCTION_ARM(SMLAL,
 632	int64_t d = ((int64_t) cpu->gprs[rm]) * ((int64_t) cpu->gprs[rs]);
 633	int32_t dm = cpu->gprs[rd];
 634	int32_t dn = d;
 635	cpu->gprs[rd] = dm + dn;
 636	cpu->gprs[rdHi] = cpu->gprs[rdHi] + (d >> 32) + ARM_CARRY_FROM(dm, dn, cpu->gprs[rd]);,
 637	ARM_NEUTRAL_HI_S(cpu->gprs[rd], cpu->gprs[rdHi]))
 638
 639DEFINE_MULTIPLY_INSTRUCTION_ARM(SMULL,
 640	int64_t d = ((int64_t) cpu->gprs[rm]) * ((int64_t) cpu->gprs[rs]);
 641	cpu->gprs[rd] = d;
 642	cpu->gprs[rdHi] = d >> 32;,
 643	ARM_NEUTRAL_HI_S(cpu->gprs[rd], cpu->gprs[rdHi]))
 644
 645DEFINE_MULTIPLY_INSTRUCTION_ARM(UMLAL,
 646	uint64_t d = NO_EXTEND64(cpu->gprs[rm]) * NO_EXTEND64(cpu->gprs[rs]);
 647	int32_t dm = cpu->gprs[rd];
 648	int32_t dn = d;
 649	cpu->gprs[rd] = dm + dn;
 650	cpu->gprs[rdHi] = cpu->gprs[rdHi] + (d >> 32) + ARM_CARRY_FROM(dm, dn, cpu->gprs[rd]);,
 651	ARM_NEUTRAL_HI_S(cpu->gprs[rd], cpu->gprs[rdHi]))
 652
 653DEFINE_MULTIPLY_INSTRUCTION_ARM(UMULL,
 654	uint64_t d = NO_EXTEND64(cpu->gprs[rm]) * NO_EXTEND64(cpu->gprs[rs]);
 655	cpu->gprs[rd] = d;
 656	cpu->gprs[rdHi] = d >> 32;,
 657	ARM_NEUTRAL_HI_S(cpu->gprs[rd], cpu->gprs[rdHi]))
 658
 659// End multiply definitions
 660
 661// Begin load/store definitions
 662
 663DEFINE_LOAD_STORE_INSTRUCTION_ARM(LDR, cpu->gprs[rd] = cpu->memory->load32(cpu->memory, address, &currentCycles); ARM_LOAD_POST_BODY;)
 664DEFINE_LOAD_STORE_INSTRUCTION_ARM(LDRB, cpu->gprs[rd] = cpu->memory->loadU8(cpu->memory, address, &currentCycles); ARM_LOAD_POST_BODY;)
 665DEFINE_LOAD_STORE_MODE_3_INSTRUCTION_ARM(LDRH, cpu->gprs[rd] = cpu->memory->loadU16(cpu->memory, address, &currentCycles); ARM_LOAD_POST_BODY;)
 666DEFINE_LOAD_STORE_MODE_3_INSTRUCTION_ARM(LDRSB, cpu->gprs[rd] = cpu->memory->load8(cpu->memory, address, &currentCycles); ARM_LOAD_POST_BODY;)
 667DEFINE_LOAD_STORE_MODE_3_INSTRUCTION_ARM(LDRSH, cpu->gprs[rd] = cpu->memory->load16(cpu->memory, address, &currentCycles); ARM_LOAD_POST_BODY;)
 668DEFINE_LOAD_STORE_INSTRUCTION_ARM(STR, cpu->memory->store32(cpu->memory, address, cpu->gprs[rd], &currentCycles); ARM_STORE_POST_BODY;)
 669DEFINE_LOAD_STORE_INSTRUCTION_ARM(STRB, cpu->memory->store8(cpu->memory, address, cpu->gprs[rd], &currentCycles); ARM_STORE_POST_BODY;)
 670DEFINE_LOAD_STORE_MODE_3_INSTRUCTION_ARM(STRH, cpu->memory->store16(cpu->memory, address, cpu->gprs[rd], &currentCycles); ARM_STORE_POST_BODY;)
 671
 672DEFINE_LOAD_STORE_T_INSTRUCTION_ARM(LDRBT,
 673	enum PrivilegeMode priv = cpu->privilegeMode;
 674	ARMSetPrivilegeMode(cpu, MODE_USER);
 675	cpu->gprs[rd] = cpu->memory->loadU8(cpu->memory, address, &currentCycles);
 676	ARMSetPrivilegeMode(cpu, priv);
 677	ARM_LOAD_POST_BODY;)
 678
 679DEFINE_LOAD_STORE_T_INSTRUCTION_ARM(LDRT,
 680	enum PrivilegeMode priv = cpu->privilegeMode;
 681	ARMSetPrivilegeMode(cpu, MODE_USER);
 682	cpu->gprs[rd] = cpu->memory->load32(cpu->memory, address, &currentCycles);
 683	ARMSetPrivilegeMode(cpu, priv);
 684	ARM_LOAD_POST_BODY;)
 685
 686DEFINE_LOAD_STORE_T_INSTRUCTION_ARM(STRBT,
 687	enum PrivilegeMode priv = cpu->privilegeMode;
 688	ARMSetPrivilegeMode(cpu, MODE_USER);
 689	cpu->memory->store32(cpu->memory, address, cpu->gprs[rd], &currentCycles);
 690	ARMSetPrivilegeMode(cpu, priv);
 691	ARM_STORE_POST_BODY;)
 692
 693DEFINE_LOAD_STORE_T_INSTRUCTION_ARM(STRT,
 694	enum PrivilegeMode priv = cpu->privilegeMode;
 695	ARMSetPrivilegeMode(cpu, MODE_USER);
 696	cpu->memory->store8(cpu->memory, address, cpu->gprs[rd], &currentCycles);
 697	ARMSetPrivilegeMode(cpu, priv);
 698	ARM_STORE_POST_BODY;)
 699
 700DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_ARM(LDM,
 701	cpu->gprs[i] = cpu->memory->load32(cpu->memory, addr & 0xFFFFFFFC, 0);,
 702	++currentCycles;
 703	if (rs & 0x8000) {
 704		ARM_WRITE_PC;
 705	}
 706	int rnx = 1 << rn;
 707	if (rnx & rs && ((rnx - 1) & rs)) {
 708		writeback = 0;
 709	})
 710
 711DEFINE_LOAD_STORE_MULTIPLE_INSTRUCTION_ARM(STM,
 712	cpu->memory->store32(cpu->memory, addr, cpu->gprs[i], 0);,
 713	currentCycles -= ARM_PREFETCH_CYCLES)
 714
 715DEFINE_INSTRUCTION_ARM(SWP, ARM_STUB)
 716DEFINE_INSTRUCTION_ARM(SWPB, ARM_STUB)
 717
 718// End load/store definitions
 719
 720// Begin branch definitions
 721
 722DEFINE_INSTRUCTION_ARM(B,
 723	int32_t offset = opcode << 8;
 724	offset >>= 6;
 725	cpu->gprs[ARM_PC] += offset;
 726	ARM_WRITE_PC;)
 727
 728DEFINE_INSTRUCTION_ARM(BL,
 729	int32_t immediate = (opcode & 0x00FFFFFF) << 8;
 730	cpu->gprs[ARM_LR] = cpu->gprs[ARM_PC] - WORD_SIZE_ARM;
 731	cpu->gprs[ARM_PC] += immediate >> 6;
 732	ARM_WRITE_PC;)
 733
 734DEFINE_INSTRUCTION_ARM(BX,
 735	int rm = opcode & 0x0000000F;
 736	_ARMSetMode(cpu, cpu->gprs[rm] & 0x00000001);
 737	cpu->gprs[ARM_PC] = cpu->gprs[rm] & 0xFFFFFFFE;
 738	if (cpu->executionMode == MODE_THUMB) {
 739		THUMB_WRITE_PC;
 740	} else {
 741		ARM_WRITE_PC;
 742	})
 743
 744// End branch definitions
 745
 746// Begin miscellaneous definitions
 747
 748DEFINE_INSTRUCTION_ARM(BKPT, ARM_STUB) // Not strictly in ARMv4T, but here for convenience
 749DEFINE_INSTRUCTION_ARM(ILL, ARM_STUB) // Illegal opcode
 750
 751DEFINE_INSTRUCTION_ARM(MSR,
 752	int c = opcode & 0x00010000;
 753	int f = opcode & 0x00080000;
 754	int32_t operand = cpu->gprs[opcode & 0x0000000F];
 755	int32_t mask = (c ? 0x000000FF : 0) | (f ? 0xFF000000 : 0);
 756	if (mask & PSR_USER_MASK) {
 757		cpu->cpsr.packed = (cpu->cpsr.packed & ~PSR_USER_MASK) | (operand & PSR_USER_MASK);
 758	}
 759	if (cpu->privilegeMode != MODE_USER && (mask & PSR_PRIV_MASK)) {
 760		ARMSetPrivilegeMode(cpu, (enum PrivilegeMode) ((operand & 0x0000000F) | 0x00000010));
 761		cpu->cpsr.packed = (cpu->cpsr.packed & ~PSR_PRIV_MASK) | (operand & PSR_PRIV_MASK);
 762	})
 763
 764DEFINE_INSTRUCTION_ARM(MSRR,
 765	int c = opcode & 0x00010000;
 766	int f = opcode & 0x00080000;
 767	int32_t operand = cpu->gprs[opcode & 0x0000000F];
 768	int32_t mask = (c ? 0x000000FF : 0) | (f ? 0xFF000000 : 0);
 769	mask &= PSR_USER_MASK | PSR_PRIV_MASK | PSR_STATE_MASK;
 770	cpu->spsr.packed = (cpu->spsr.packed & ~mask) | (operand & mask);)
 771
 772DEFINE_INSTRUCTION_ARM(MRS, \
 773	int rd = (opcode >> 12) & 0xF; \
 774	cpu->gprs[rd] = cpu->cpsr.packed;)
 775
 776DEFINE_INSTRUCTION_ARM(MRSR, \
 777	int rd = (opcode >> 12) & 0xF; \
 778	cpu->gprs[rd] = cpu->spsr.packed;)
 779
 780DEFINE_INSTRUCTION_ARM(MSRI,
 781	int c = opcode & 0x00010000;
 782	int f = opcode & 0x00080000;
 783	int rotate = (opcode & 0x00000F00) >> 8;
 784	int32_t operand = ARM_ROR(opcode & 0x000000FF, rotate);
 785	int32_t mask = (c ? 0x000000FF : 0) | (f ? 0xFF000000 : 0);
 786	if (mask & PSR_USER_MASK) {
 787		cpu->cpsr.packed = (cpu->cpsr.packed & ~PSR_USER_MASK) | (operand & PSR_USER_MASK);
 788	}
 789	if (cpu->privilegeMode != MODE_USER && (mask & PSR_PRIV_MASK)) {
 790		ARMSetPrivilegeMode(cpu, (enum PrivilegeMode) ((operand & 0x0000000F) | 0x00000010));
 791		cpu->cpsr.packed = (cpu->cpsr.packed & ~PSR_PRIV_MASK) | (operand & PSR_PRIV_MASK);
 792	})
 793
 794DEFINE_INSTRUCTION_ARM(MSRRI,
 795	int c = opcode & 0x00010000;
 796	int f = opcode & 0x00080000;
 797	int rotate = (opcode & 0x00000F00) >> 8;
 798	int32_t operand = ARM_ROR(opcode & 0x000000FF, rotate);
 799	int32_t mask = (c ? 0x000000FF : 0) | (f ? 0xFF000000 : 0);
 800	mask &= PSR_USER_MASK | PSR_PRIV_MASK | PSR_STATE_MASK;
 801	cpu->spsr.packed = (cpu->spsr.packed & ~mask) | (operand & mask);)
 802
 803DEFINE_INSTRUCTION_ARM(SWI, cpu->board->swi32(cpu->board, opcode & 0xFFFFFF))
 804
 805#define DECLARE_INSTRUCTION_ARM(EMITTER, NAME) \
 806	EMITTER ## NAME
 807
 808#define DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, ALU) \
 809	DO_8(DECLARE_INSTRUCTION_ARM(EMITTER, ALU ## I)), \
 810	DO_8(DECLARE_INSTRUCTION_ARM(EMITTER, ALU ## I))
 811
 812#define DECLARE_ARM_ALU_BLOCK(EMITTER, ALU, EX1, EX2, EX3, EX4) \
 813	DECLARE_INSTRUCTION_ARM(EMITTER, ALU ## _LSL), \
 814	DECLARE_INSTRUCTION_ARM(EMITTER, ALU ## _LSLR), \
 815	DECLARE_INSTRUCTION_ARM(EMITTER, ALU ## _LSR), \
 816	DECLARE_INSTRUCTION_ARM(EMITTER, ALU ## _LSRR), \
 817	DECLARE_INSTRUCTION_ARM(EMITTER, ALU ## _ASR), \
 818	DECLARE_INSTRUCTION_ARM(EMITTER, ALU ## _ASRR), \
 819	DECLARE_INSTRUCTION_ARM(EMITTER, ALU ## _ROR), \
 820	DECLARE_INSTRUCTION_ARM(EMITTER, ALU ## _RORR), \
 821	DECLARE_INSTRUCTION_ARM(EMITTER, ALU ## _LSL), \
 822	DECLARE_INSTRUCTION_ARM(EMITTER, EX1), \
 823	DECLARE_INSTRUCTION_ARM(EMITTER, ALU ## _LSR), \
 824	DECLARE_INSTRUCTION_ARM(EMITTER, EX2), \
 825	DECLARE_INSTRUCTION_ARM(EMITTER, ALU ## _ASR), \
 826	DECLARE_INSTRUCTION_ARM(EMITTER, EX3), \
 827	DECLARE_INSTRUCTION_ARM(EMITTER, ALU ## _ROR), \
 828	DECLARE_INSTRUCTION_ARM(EMITTER, EX4)
 829
 830#define DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, NAME, P, U, W) \
 831	DO_8(DECLARE_INSTRUCTION_ARM(EMITTER, NAME ## I ## P ## U ## W)), \
 832	DO_8(DECLARE_INSTRUCTION_ARM(EMITTER, NAME ## I ## P ## U ## W))
 833
 834#define DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, NAME, P, U, W) \
 835	DECLARE_INSTRUCTION_ARM(EMITTER, NAME ## _LSL_ ## P ## U ## W), \
 836	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 837	DECLARE_INSTRUCTION_ARM(EMITTER, NAME ## _LSR_ ## P ## U ## W), \
 838	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 839	DECLARE_INSTRUCTION_ARM(EMITTER, NAME ## _ASR_ ## P ## U ## W), \
 840	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 841	DECLARE_INSTRUCTION_ARM(EMITTER, NAME ## _ROR_ ## P ## U ## W), \
 842	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 843	DECLARE_INSTRUCTION_ARM(EMITTER, NAME ## _LSL_ ## P ## U ## W), \
 844	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 845	DECLARE_INSTRUCTION_ARM(EMITTER, NAME ## _LSR_ ## P ## U ## W), \
 846	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 847	DECLARE_INSTRUCTION_ARM(EMITTER, NAME ## _ASR_ ## P ## U ## W), \
 848	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 849	DECLARE_INSTRUCTION_ARM(EMITTER, NAME ## _ROR_ ## P ## U ## W), \
 850	DECLARE_INSTRUCTION_ARM(EMITTER, ILL)
 851
 852#define DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, NAME, MODE, W) \
 853	DO_8(DECLARE_INSTRUCTION_ARM(EMITTER, NAME ## MODE ## W)), \
 854	DO_8(DECLARE_INSTRUCTION_ARM(EMITTER, NAME ## MODE ## W))
 855
 856#define DECLARE_ARM_BRANCH_BLOCK(EMITTER, NAME) \
 857	DO_256(DECLARE_INSTRUCTION_ARM(EMITTER, NAME))
 858
 859// TODO: Support coprocessors
 860#define DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, NAME, P, U, W, N) \
 861	DO_8(0), \
 862	DO_8(0)
 863
 864#define DECLARE_ARM_COPROCESSOR_BLOCK(EMITTER, NAME1, NAME2) \
 865	DO_8(DO_8(DO_INTERLACE(0, 0))), \
 866	DO_8(DO_8(DO_INTERLACE(0, 0)))
 867
 868#define DECLARE_ARM_SWI_BLOCK(EMITTER) \
 869	DO_256(DECLARE_INSTRUCTION_ARM(EMITTER, SWI))
 870
 871#define DECLARE_ARM_EMITTER_BLOCK(EMITTER) \
 872	DECLARE_ARM_ALU_BLOCK(EMITTER, AND, MUL, STRH, ILL, ILL), \
 873	DECLARE_ARM_ALU_BLOCK(EMITTER, ANDS, MULS, LDRH, LDRSB, LDRSH), \
 874	DECLARE_ARM_ALU_BLOCK(EMITTER, EOR, MLA, ILL, ILL, ILL), \
 875	DECLARE_ARM_ALU_BLOCK(EMITTER, EORS, MLAS, ILL, ILL, ILL), \
 876	DECLARE_ARM_ALU_BLOCK(EMITTER, SUB, ILL, STRHI, ILL, ILL), \
 877	DECLARE_ARM_ALU_BLOCK(EMITTER, SUBS, ILL, LDRHI, LDRSBI, LDRSHI), \
 878	DECLARE_ARM_ALU_BLOCK(EMITTER, RSB, ILL, ILL, ILL, ILL), \
 879	DECLARE_ARM_ALU_BLOCK(EMITTER, RSBS, ILL, ILL, ILL, ILL), \
 880	DECLARE_ARM_ALU_BLOCK(EMITTER, ADD, UMULL, STRHU, ILL, ILL), \
 881	DECLARE_ARM_ALU_BLOCK(EMITTER, ADDS, UMULLS, LDRHU, LDRSBU, LDRSHU), \
 882	DECLARE_ARM_ALU_BLOCK(EMITTER, ADC, UMLAL, ILL, ILL, ILL), \
 883	DECLARE_ARM_ALU_BLOCK(EMITTER, ADCS, UMLALS, ILL, ILL, ILL), \
 884	DECLARE_ARM_ALU_BLOCK(EMITTER, SBC, SMULL, STRHIU, ILL, ILL), \
 885	DECLARE_ARM_ALU_BLOCK(EMITTER, SBCS, SMULLS, LDRHIU, LDRSBIU, LDRSHIU), \
 886	DECLARE_ARM_ALU_BLOCK(EMITTER, RSC, SMLAL, ILL, ILL, ILL), \
 887	DECLARE_ARM_ALU_BLOCK(EMITTER, RSCS, SMLALS, ILL, ILL, ILL), \
 888	DECLARE_INSTRUCTION_ARM(EMITTER, MRS), \
 889	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 890	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 891	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 892	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 893	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 894	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 895	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 896	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 897	DECLARE_INSTRUCTION_ARM(EMITTER, SWP), \
 898	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 899	DECLARE_INSTRUCTION_ARM(EMITTER, STRHP), \
 900	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 901	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 902	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 903	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 904	DECLARE_ARM_ALU_BLOCK(EMITTER, TST, ILL, LDRHP, LDRSBP, LDRSHP), \
 905	DECLARE_INSTRUCTION_ARM(EMITTER, MSR), \
 906	DECLARE_INSTRUCTION_ARM(EMITTER, BX), \
 907	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 908	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 909	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 910	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 911	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 912	DECLARE_INSTRUCTION_ARM(EMITTER, BKPT), \
 913	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 914	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 915	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 916	DECLARE_INSTRUCTION_ARM(EMITTER, STRHPW), \
 917	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 918	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 919	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 920	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 921	DECLARE_ARM_ALU_BLOCK(EMITTER, TEQ, ILL, LDRHPW, LDRSBPW, LDRSHPW), \
 922	DECLARE_INSTRUCTION_ARM(EMITTER, MRSR), \
 923	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 924	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 925	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 926	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 927	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 928	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 929	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 930	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 931	DECLARE_INSTRUCTION_ARM(EMITTER, SWPB), \
 932	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 933	DECLARE_INSTRUCTION_ARM(EMITTER, STRHIP), \
 934	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 935	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 936	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 937	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 938	DECLARE_ARM_ALU_BLOCK(EMITTER, CMP, ILL, LDRHIP, LDRSBIP, LDRSHIP), \
 939	DECLARE_INSTRUCTION_ARM(EMITTER, MSRR), \
 940	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 941	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 942	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 943	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 944	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 945	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 946	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 947	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 948	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 949	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 950	DECLARE_INSTRUCTION_ARM(EMITTER, STRHIPW), \
 951	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 952	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 953	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 954	DECLARE_INSTRUCTION_ARM(EMITTER, ILL), \
 955	DECLARE_ARM_ALU_BLOCK(EMITTER, CMN, ILL, LDRHIPW, LDRSBIPW, LDRSHIPW), \
 956	DECLARE_ARM_ALU_BLOCK(EMITTER, ORR, SMLAL, STRHPU, ILL, ILL), \
 957	DECLARE_ARM_ALU_BLOCK(EMITTER, ORRS, SMLALS, LDRHPU, LDRSBPU, LDRSHPU), \
 958	DECLARE_ARM_ALU_BLOCK(EMITTER, MOV, SMLAL, STRHPUW, ILL, ILL), \
 959	DECLARE_ARM_ALU_BLOCK(EMITTER, MOVS, SMLALS, LDRHPUW, LDRSBPUW, LDRSHPUW), \
 960	DECLARE_ARM_ALU_BLOCK(EMITTER, BIC, SMLAL, STRHIPU, ILL, ILL), \
 961	DECLARE_ARM_ALU_BLOCK(EMITTER, BICS, SMLALS, LDRHIPU, LDRSBIPU, LDRSHIPU), \
 962	DECLARE_ARM_ALU_BLOCK(EMITTER, MVN, SMLAL, STRHIPUW, ILL, ILL), \
 963	DECLARE_ARM_ALU_BLOCK(EMITTER, MVNS, SMLALS, LDRHIPUW, LDRSBIPUW, LDRSHIPUW), \
 964	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, AND), \
 965	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, ANDS), \
 966	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, EOR), \
 967	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, EORS), \
 968	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, SUB), \
 969	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, SUBS), \
 970	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, RSB), \
 971	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, RSBS), \
 972	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, ADD), \
 973	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, ADDS), \
 974	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, ADC), \
 975	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, ADCS), \
 976	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, SBC), \
 977	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, SBCS), \
 978	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, RSC), \
 979	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, RSCS), \
 980	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, TST), \
 981	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, TST), \
 982	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, MSR), \
 983	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, TEQ), \
 984	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, CMP), \
 985	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, CMP), \
 986	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, MSRR), \
 987	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, CMN), \
 988	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, ORR), \
 989	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, ORRS), \
 990	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, MOV), \
 991	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, MOVS), \
 992	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, BIC), \
 993	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, BICS), \
 994	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, MVN), \
 995	DECLARE_ARM_ALU_IMMEDIATE_BLOCK(EMITTER, MVNS), \
 996	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, STR, , , ), \
 997	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, LDR, , , ), \
 998	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, STRT, , , ), \
 999	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, LDRT, , , ), \
1000	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, STRB, , , ), \
1001	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, LDRB, , , ), \
1002	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, STRBT, , , ), \
1003	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, LDRBT, , , ), \
1004	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, STR, , U, ), \
1005	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, LDR, , U, ), \
1006	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, STRT, , U, ), \
1007	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, LDRT, , U, ), \
1008	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, STRB, , U, ), \
1009	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, LDRB, , U, ), \
1010	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, STRBT, , U, ), \
1011	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, LDRBT, , U, ), \
1012	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, STR, P, , ), \
1013	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, LDR, P, , ), \
1014	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, STR, P, , W), \
1015	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, LDR, P, , W), \
1016	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, STRB, P, , ), \
1017	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, LDRB, P, , ), \
1018	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, STRB, P, , W), \
1019	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, LDRB, P, , W), \
1020	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, STR, P, U, ), \
1021	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, LDR, P, U, ), \
1022	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, STR, P, U, W), \
1023	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, LDR, P, U, W), \
1024	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, STRB, P, U, ), \
1025	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, LDRB, P, U, ), \
1026	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, STRB, P, U, W), \
1027	DECLARE_ARM_LOAD_STORE_IMMEDIATE_BLOCK(EMITTER, LDRB, P, U, W), \
1028	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, STR, , , ), \
1029	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, LDR, , , ), \
1030	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, STRT, , , ), \
1031	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, LDRT, , , ), \
1032	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, STRB, , , ), \
1033	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, LDRB, , , ), \
1034	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, STRBT, , , ), \
1035	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, LDRBT, , , ), \
1036	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, STR, , U, ), \
1037	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, LDR, , U, ), \
1038	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, STRT, , U, ), \
1039	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, LDRT, , U, ), \
1040	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, STRB, , U, ), \
1041	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, LDRB, , U, ), \
1042	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, STRBT, , U, ), \
1043	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, LDRBT, , U, ), \
1044	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, STR, P, , ), \
1045	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, LDR, P, , ), \
1046	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, STR, P, , W), \
1047	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, LDR, P, , W), \
1048	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, STRB, P, , ), \
1049	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, LDRB, P, , ), \
1050	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, STRB, P, , W), \
1051	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, LDRB, P, , W), \
1052	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, STR, P, U, ), \
1053	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, LDR, P, U, ), \
1054	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, STR, P, U, W), \
1055	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, LDR, P, U, W), \
1056	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, STRB, P, U, ), \
1057	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, LDRB, P, U, ), \
1058	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, STRB, P, U, W), \
1059	DECLARE_ARM_LOAD_STORE_BLOCK(EMITTER, LDRB, P, U, W), \
1060	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, STM, DA, ), \
1061	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, LDM, DA, ), \
1062	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, STM, DA, W), \
1063	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, LDM, DA, W), \
1064	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, STMS, DA, ), \
1065	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, LDMS, DA, ), \
1066	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, STMS, DA, W), \
1067	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, LDMS, DA, W), \
1068	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, STM, IA, ), \
1069	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, LDM, IA, ), \
1070	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, STM, IA, W), \
1071	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, LDM, IA, W), \
1072	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, STMS, IA, ), \
1073	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, LDMS, IA, ), \
1074	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, STMS, IA, W), \
1075	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, LDMS, IA, W), \
1076	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, STM, DB, ), \
1077	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, LDM, DB, ), \
1078	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, STM, DB, W), \
1079	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, LDM, DB, W), \
1080	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, STMS, DB, ), \
1081	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, LDMS, DB, ), \
1082	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, STMS, DB, W), \
1083	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, LDMS, DB, W), \
1084	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, STM, IB, ), \
1085	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, LDM, IB, ), \
1086	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, STM, IB, W), \
1087	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, LDM, IB, W), \
1088	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, STMS, IB, ), \
1089	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, LDMS, IB, ), \
1090	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, STMS, IB, W), \
1091	DECLARE_ARM_LOAD_STORE_MULTIPLE_BLOCK(EMITTER, LDMS, IB, W), \
1092	DECLARE_ARM_BRANCH_BLOCK(EMITTER, B), \
1093	DECLARE_ARM_BRANCH_BLOCK(EMITTER, BL), \
1094	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, STC, , , , ), \
1095	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, LDC, , , , ), \
1096	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, STC, , , , W), \
1097	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, LDC, , , , W), \
1098	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, STC, , , N, ), \
1099	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, LDC, , , N, ), \
1100	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, STC, , , N, W), \
1101	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, LDC, , , N, W), \
1102	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, STC, , U, , ), \
1103	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, LDC, , U, , ), \
1104	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, STC, , U, , W), \
1105	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, LDC, , U, , W), \
1106	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, STC, , U, N, ), \
1107	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, LDC, , U, N, ), \
1108	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, STC, , U, N, W), \
1109	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, LDC, , U, N, W), \
1110	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, STC, P, , , ), \
1111	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, LDC, P, , , ), \
1112	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, STC, P, , , W), \
1113	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, LDC, P, , , W), \
1114	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, STC, P, U, N, ), \
1115	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, LDC, P, U, N, ), \
1116	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, STC, P, U, N, W), \
1117	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, LDC, P, U, N, W), \
1118	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, STC, P, , N, ), \
1119	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, LDC, P, , N, ), \
1120	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, STC, P, , N, W), \
1121	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, LDC, P, , N, W), \
1122	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, STC, P, U, N, ), \
1123	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, LDC, P, U, N, ), \
1124	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, STC, P, U, N, W), \
1125	DECLARE_ARM_LOAD_STORE_COPROCESSOR_BLOCK(EMITTER, LDC, P, U, N, W), \
1126	DECLARE_ARM_COPROCESSOR_BLOCK(EMITTER, CDP, MCR), \
1127	DECLARE_ARM_SWI_BLOCK(EMITTER)
1128
1129static const ARMInstruction _armTable[0x1000] = {
1130	DECLARE_ARM_EMITTER_BLOCK(_ARMInstruction)
1131};