all repos — mgba @ 89c49f15d0880596436a567f300693f01ab2c47f

mGBA Game Boy Advance Emulator

src/arm/isa-thumb.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-thumb.h>
  7
  8#include <mgba/internal/arm/isa-inlines.h>
  9#include <mgba/internal/arm/emitter-thumb.h>
 10
 11// Instruction definitions
 12// Beware pre-processor insanity
 13
 14#define THUMB_ADDITION_S(M, N, D) \
 15	cpu->cpsr.flags = 0; \
 16	cpu->cpsr.n = ARM_SIGN(D); \
 17	cpu->cpsr.z = !(D); \
 18	cpu->cpsr.c = ARM_CARRY_FROM(M, N, D); \
 19	cpu->cpsr.v = ARM_V_ADDITION(M, N, D);
 20
 21#define THUMB_SUBTRACTION_S(M, N, D) \
 22	cpu->cpsr.flags = 0; \
 23	cpu->cpsr.n = ARM_SIGN(D); \
 24	cpu->cpsr.z = !(D); \
 25	cpu->cpsr.c = ARM_BORROW_FROM(M, N, D); \
 26	cpu->cpsr.v = ARM_V_SUBTRACTION(M, N, D);
 27
 28#define THUMB_NEUTRAL_S(M, N, D) \
 29	cpu->cpsr.n = ARM_SIGN(D); \
 30	cpu->cpsr.z = !(D);
 31
 32#define THUMB_ADDITION(D, M, N) \
 33	int n = N; \
 34	int m = M; \
 35	D = M + N; \
 36	THUMB_ADDITION_S(m, n, D)
 37
 38#define THUMB_SUBTRACTION(D, M, N) \
 39	int n = N; \
 40	int m = M; \
 41	D = M - N; \
 42	THUMB_SUBTRACTION_S(m, n, D)
 43
 44#define THUMB_PREFETCH_CYCLES (1 + cpu->memory.activeSeqCycles16)
 45
 46#define THUMB_LOAD_POST_BODY \
 47	currentCycles += cpu->memory.activeNonseqCycles16 - cpu->memory.activeSeqCycles16;
 48
 49#define THUMB_STORE_POST_BODY \
 50	currentCycles += cpu->memory.activeNonseqCycles16 - cpu->memory.activeSeqCycles16;
 51
 52#define DEFINE_INSTRUCTION_THUMB(NAME, BODY) \
 53	static void _ThumbInstruction ## NAME (struct ARMCore* cpu, uint16_t opcode) {  \
 54		int currentCycles = THUMB_PREFETCH_CYCLES; \
 55		BODY; \
 56		cpu->cycles += currentCycles; \
 57	}
 58
 59#define DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(NAME, BODY) \
 60	DEFINE_INSTRUCTION_THUMB(NAME, \
 61		int immediate = (opcode >> 6) & 0x001F; \
 62		int rd = opcode & 0x0007; \
 63		int rm = (opcode >> 3) & 0x0007; \
 64		BODY;)
 65
 66DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(LSL1,
 67	if (!immediate) {
 68		cpu->gprs[rd] = cpu->gprs[rm];
 69	} else {
 70		cpu->cpsr.c = (cpu->gprs[rm] >> (32 - immediate)) & 1;
 71		cpu->gprs[rd] = cpu->gprs[rm] << immediate;
 72	}
 73	THUMB_NEUTRAL_S( , , cpu->gprs[rd]);)
 74
 75DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(LSR1,
 76	if (!immediate) {
 77		cpu->cpsr.c = ARM_SIGN(cpu->gprs[rm]);
 78		cpu->gprs[rd] = 0;
 79	} else {
 80		cpu->cpsr.c = (cpu->gprs[rm] >> (immediate - 1)) & 1;
 81		cpu->gprs[rd] = ((uint32_t) cpu->gprs[rm]) >> immediate;
 82	}
 83	THUMB_NEUTRAL_S( , , cpu->gprs[rd]);)
 84
 85DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(ASR1, 
 86	if (!immediate) {
 87		cpu->cpsr.c = ARM_SIGN(cpu->gprs[rm]);
 88		if (cpu->cpsr.c) {
 89			cpu->gprs[rd] = 0xFFFFFFFF;
 90		} else {
 91			cpu->gprs[rd] = 0;
 92		}
 93	} else {
 94		cpu->cpsr.c = (cpu->gprs[rm] >> (immediate - 1)) & 1;
 95		cpu->gprs[rd] = cpu->gprs[rm] >> immediate;
 96	}
 97	THUMB_NEUTRAL_S( , , cpu->gprs[rd]);)
 98
 99DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(LDR1, cpu->gprs[rd] = cpu->memory.load32(cpu, cpu->gprs[rm] + immediate * 4, &currentCycles); THUMB_LOAD_POST_BODY;)
100DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(LDRB1, cpu->gprs[rd] = cpu->memory.load8(cpu, cpu->gprs[rm] + immediate, &currentCycles); THUMB_LOAD_POST_BODY;)
101DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(LDRH1, cpu->gprs[rd] = cpu->memory.load16(cpu, cpu->gprs[rm] + immediate * 2, &currentCycles); THUMB_LOAD_POST_BODY;)
102DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(STR1, cpu->memory.store32(cpu, cpu->gprs[rm] + immediate * 4, cpu->gprs[rd], &currentCycles); THUMB_STORE_POST_BODY;)
103DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(STRB1, cpu->memory.store8(cpu, cpu->gprs[rm] + immediate, cpu->gprs[rd], &currentCycles); THUMB_STORE_POST_BODY;)
104DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(STRH1, cpu->memory.store16(cpu, cpu->gprs[rm] + immediate * 2, cpu->gprs[rd], &currentCycles); THUMB_STORE_POST_BODY;)
105
106#define DEFINE_DATA_FORM_1_INSTRUCTION_THUMB(NAME, BODY) \
107	DEFINE_INSTRUCTION_THUMB(NAME, \
108		int rm = (opcode >> 6) & 0x0007; \
109		int rd = opcode & 0x0007; \
110		int rn = (opcode >> 3) & 0x0007; \
111		BODY;)
112
113DEFINE_DATA_FORM_1_INSTRUCTION_THUMB(ADD3, THUMB_ADDITION(cpu->gprs[rd], cpu->gprs[rn], cpu->gprs[rm]))
114DEFINE_DATA_FORM_1_INSTRUCTION_THUMB(SUB3, THUMB_SUBTRACTION(cpu->gprs[rd], cpu->gprs[rn], cpu->gprs[rm]))
115
116#define DEFINE_DATA_FORM_2_INSTRUCTION_THUMB(NAME, BODY) \
117	DEFINE_INSTRUCTION_THUMB(NAME, \
118		int immediate = (opcode >> 6) & 0x0007; \
119		int rd = opcode & 0x0007; \
120		int rn = (opcode >> 3) & 0x0007; \
121		BODY;)
122
123DEFINE_DATA_FORM_2_INSTRUCTION_THUMB(ADD1, THUMB_ADDITION(cpu->gprs[rd], cpu->gprs[rn], immediate))
124DEFINE_DATA_FORM_2_INSTRUCTION_THUMB(SUB1, THUMB_SUBTRACTION(cpu->gprs[rd], cpu->gprs[rn], immediate))
125
126#define DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(NAME, BODY) \
127	DEFINE_INSTRUCTION_THUMB(NAME, \
128		int rd = (opcode >> 8) & 0x0007; \
129		int immediate = opcode & 0x00FF; \
130		BODY;)
131
132DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(ADD2, THUMB_ADDITION(cpu->gprs[rd], cpu->gprs[rd], immediate))
133DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(CMP1, int aluOut = cpu->gprs[rd] - immediate; THUMB_SUBTRACTION_S(cpu->gprs[rd], immediate, aluOut))
134DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(MOV1, cpu->gprs[rd] = immediate; THUMB_NEUTRAL_S(, , cpu->gprs[rd]))
135DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(SUB2, THUMB_SUBTRACTION(cpu->gprs[rd], cpu->gprs[rd], immediate))
136
137#define DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(NAME, BODY) \
138	DEFINE_INSTRUCTION_THUMB(NAME, \
139		int rd = opcode & 0x0007; \
140		int rn = (opcode >> 3) & 0x0007; \
141		BODY;)
142
143DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(AND, cpu->gprs[rd] = cpu->gprs[rd] & cpu->gprs[rn]; THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
144DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(EOR, cpu->gprs[rd] = cpu->gprs[rd] ^ cpu->gprs[rn]; THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
145DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(LSL2,
146	int rs = cpu->gprs[rn] & 0xFF;
147	if (rs) {
148		if (rs < 32) {
149			cpu->cpsr.c = (cpu->gprs[rd] >> (32 - rs)) & 1;
150			cpu->gprs[rd] <<= rs;
151		} else {
152			if (rs > 32) {
153				cpu->cpsr.c = 0;
154			} else {
155				cpu->cpsr.c = cpu->gprs[rd] & 0x00000001;
156			}
157			cpu->gprs[rd] = 0;
158		}
159	}
160	THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
161
162DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(LSR2,
163	int rs = cpu->gprs[rn] & 0xFF;
164	if (rs) {
165		if (rs < 32) {
166			cpu->cpsr.c = (cpu->gprs[rd] >> (rs - 1)) & 1;
167			cpu->gprs[rd] = (uint32_t) cpu->gprs[rd] >> rs;
168		} else {
169			if (rs > 32) {
170				cpu->cpsr.c = 0;
171			} else {
172				cpu->cpsr.c = ARM_SIGN(cpu->gprs[rd]);
173			}
174			cpu->gprs[rd] = 0;
175		}
176	}
177	THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
178
179DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(ASR2,
180	int rs = cpu->gprs[rn] & 0xFF;
181	if (rs) {
182		if (rs < 32) {
183			cpu->cpsr.c = (cpu->gprs[rd] >> (rs - 1)) & 1;
184			cpu->gprs[rd] >>= rs;
185		} else {
186			cpu->cpsr.c = ARM_SIGN(cpu->gprs[rd]);
187			if (cpu->cpsr.c) {
188				cpu->gprs[rd] = 0xFFFFFFFF;
189			} else {
190				cpu->gprs[rd] = 0;
191			}
192		}
193	}
194	THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
195
196DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(ADC,
197	int n = cpu->gprs[rn];
198	int d = cpu->gprs[rd];
199	cpu->gprs[rd] = d + n + cpu->cpsr.c;
200	THUMB_ADDITION_S(d, n, cpu->gprs[rd]);)
201
202DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(SBC,
203	int n = cpu->gprs[rn] + !cpu->cpsr.c;
204	int d = cpu->gprs[rd];
205	cpu->gprs[rd] = d - n;
206	THUMB_SUBTRACTION_S(d, n, cpu->gprs[rd]);)
207DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(ROR,
208	int rs = cpu->gprs[rn] & 0xFF;
209	if (rs) {
210		int r4 = rs & 0x1F;
211		if (r4 > 0) {
212			cpu->cpsr.c = (cpu->gprs[rd] >> (r4 - 1)) & 1;
213			cpu->gprs[rd] = ROR(cpu->gprs[rd], r4);
214		} else {
215			cpu->cpsr.c = ARM_SIGN(cpu->gprs[rd]);
216		}
217	}
218	THUMB_NEUTRAL_S( , , cpu->gprs[rd]);)
219DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(TST, int32_t aluOut = cpu->gprs[rd] & cpu->gprs[rn]; THUMB_NEUTRAL_S(cpu->gprs[rd], cpu->gprs[rn], aluOut))
220DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(NEG, THUMB_SUBTRACTION(cpu->gprs[rd], 0, cpu->gprs[rn]))
221DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(CMP2, int32_t aluOut = cpu->gprs[rd] - cpu->gprs[rn]; THUMB_SUBTRACTION_S(cpu->gprs[rd], cpu->gprs[rn], aluOut))
222DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(CMN, int32_t aluOut = cpu->gprs[rd] + cpu->gprs[rn]; THUMB_ADDITION_S(cpu->gprs[rd], cpu->gprs[rn], aluOut))
223DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(ORR, cpu->gprs[rd] = cpu->gprs[rd] | cpu->gprs[rn]; THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
224DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(MUL, ARM_WAIT_MUL(cpu->gprs[rd]); cpu->gprs[rd] *= cpu->gprs[rn]; THUMB_NEUTRAL_S( , , cpu->gprs[rd]); currentCycles += cpu->memory.activeNonseqCycles16 - cpu->memory.activeSeqCycles16)
225DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(BIC, cpu->gprs[rd] = cpu->gprs[rd] & ~cpu->gprs[rn]; THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
226DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(MVN, cpu->gprs[rd] = ~cpu->gprs[rn]; THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
227
228#define DEFINE_INSTRUCTION_WITH_HIGH_EX_THUMB(NAME, H1, H2, BODY) \
229	DEFINE_INSTRUCTION_THUMB(NAME, \
230		int rd = (opcode & 0x0007) | H1; \
231		int rm = ((opcode >> 3) & 0x0007) | H2; \
232		BODY;)
233
234#define DEFINE_INSTRUCTION_WITH_HIGH_THUMB(NAME, BODY) \
235	DEFINE_INSTRUCTION_WITH_HIGH_EX_THUMB(NAME ## 00, 0, 0, BODY) \
236	DEFINE_INSTRUCTION_WITH_HIGH_EX_THUMB(NAME ## 01, 0, 8, BODY) \
237	DEFINE_INSTRUCTION_WITH_HIGH_EX_THUMB(NAME ## 10, 8, 0, BODY) \
238	DEFINE_INSTRUCTION_WITH_HIGH_EX_THUMB(NAME ## 11, 8, 8, BODY)
239
240DEFINE_INSTRUCTION_WITH_HIGH_THUMB(ADD4,
241	cpu->gprs[rd] += cpu->gprs[rm];
242	if (rd == ARM_PC) {
243		currentCycles += ThumbWritePC(cpu);
244	})
245
246DEFINE_INSTRUCTION_WITH_HIGH_THUMB(CMP3, int32_t aluOut = cpu->gprs[rd] - cpu->gprs[rm]; THUMB_SUBTRACTION_S(cpu->gprs[rd], cpu->gprs[rm], aluOut))
247DEFINE_INSTRUCTION_WITH_HIGH_THUMB(MOV3,
248	cpu->gprs[rd] = cpu->gprs[rm];
249	if (rd == ARM_PC) {
250		currentCycles += ThumbWritePC(cpu);
251	})
252
253#define DEFINE_IMMEDIATE_WITH_REGISTER_THUMB(NAME, BODY) \
254	DEFINE_INSTRUCTION_THUMB(NAME, \
255		int rd = (opcode >> 8) & 0x0007; \
256		int immediate = (opcode & 0x00FF) << 2; \
257		BODY;)
258
259DEFINE_IMMEDIATE_WITH_REGISTER_THUMB(LDR3, cpu->gprs[rd] = cpu->memory.load32(cpu, (cpu->gprs[ARM_PC] & 0xFFFFFFFC) + immediate, &currentCycles); THUMB_LOAD_POST_BODY;)
260DEFINE_IMMEDIATE_WITH_REGISTER_THUMB(LDR4, cpu->gprs[rd] = cpu->memory.load32(cpu, cpu->gprs[ARM_SP] + immediate, &currentCycles); THUMB_LOAD_POST_BODY;)
261DEFINE_IMMEDIATE_WITH_REGISTER_THUMB(STR3, cpu->memory.store32(cpu, cpu->gprs[ARM_SP] + immediate, cpu->gprs[rd], &currentCycles); THUMB_STORE_POST_BODY;)
262
263DEFINE_IMMEDIATE_WITH_REGISTER_THUMB(ADD5, cpu->gprs[rd] = (cpu->gprs[ARM_PC] & 0xFFFFFFFC) + immediate)
264DEFINE_IMMEDIATE_WITH_REGISTER_THUMB(ADD6, cpu->gprs[rd] = cpu->gprs[ARM_SP] + immediate)
265
266#define DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(NAME, BODY) \
267	DEFINE_INSTRUCTION_THUMB(NAME, \
268		int rm = (opcode >> 6) & 0x0007; \
269		int rd = opcode & 0x0007; \
270		int rn = (opcode >> 3) & 0x0007; \
271		BODY;)
272
273DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(LDR2, cpu->gprs[rd] = cpu->memory.load32(cpu, cpu->gprs[rn] + cpu->gprs[rm], &currentCycles); THUMB_LOAD_POST_BODY;)
274DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(LDRB2, cpu->gprs[rd] = cpu->memory.load8(cpu, cpu->gprs[rn] + cpu->gprs[rm], &currentCycles); THUMB_LOAD_POST_BODY;)
275DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(LDRH2, cpu->gprs[rd] = cpu->memory.load16(cpu, cpu->gprs[rn] + cpu->gprs[rm], &currentCycles); THUMB_LOAD_POST_BODY;)
276DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(LDRSB, cpu->gprs[rd] = ARM_SXT_8(cpu->memory.load8(cpu, cpu->gprs[rn] + cpu->gprs[rm], &currentCycles)); THUMB_LOAD_POST_BODY;)
277DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(LDRSH, rm = cpu->gprs[rn] + cpu->gprs[rm]; cpu->gprs[rd] = rm & 1 ? ARM_SXT_8(cpu->memory.load16(cpu, rm, &currentCycles)) : ARM_SXT_16(cpu->memory.load16(cpu, rm, &currentCycles)); THUMB_LOAD_POST_BODY;)
278DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(STR2, cpu->memory.store32(cpu, cpu->gprs[rn] + cpu->gprs[rm], cpu->gprs[rd], &currentCycles); THUMB_STORE_POST_BODY;)
279DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(STRB2, cpu->memory.store8(cpu, cpu->gprs[rn] + cpu->gprs[rm], cpu->gprs[rd], &currentCycles); THUMB_STORE_POST_BODY;)
280DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(STRH2, cpu->memory.store16(cpu, cpu->gprs[rn] + cpu->gprs[rm], cpu->gprs[rd], &currentCycles); THUMB_STORE_POST_BODY;)
281
282#define DEFINE_LOAD_STORE_MULTIPLE_THUMB(NAME, RN, LS, DIRECTION, PRE_BODY, WRITEBACK) \
283	DEFINE_INSTRUCTION_THUMB(NAME, \
284		int rn = RN; \
285		UNUSED(rn); \
286		int rs = opcode & 0xFF; \
287		int32_t address = cpu->gprs[RN]; \
288		PRE_BODY; \
289		address = cpu->memory. LS ## Multiple(cpu, address, rs, LSM_ ## DIRECTION, &currentCycles); \
290		WRITEBACK;)
291
292DEFINE_LOAD_STORE_MULTIPLE_THUMB(LDMIA,
293	(opcode >> 8) & 0x0007,
294	load,
295	IA,
296	,
297	THUMB_LOAD_POST_BODY;
298	if (!rs) {
299		currentCycles += ThumbWritePC(cpu);
300	}
301	if (!((1 << rn) & rs)) {
302		cpu->gprs[rn] = address;
303	})
304
305DEFINE_LOAD_STORE_MULTIPLE_THUMB(STMIA,
306	(opcode >> 8) & 0x0007,
307	store,
308	IA,
309	,
310	THUMB_STORE_POST_BODY;
311	cpu->gprs[rn] = address;)
312
313#define DEFINE_CONDITIONAL_BRANCH_THUMB(COND) \
314	DEFINE_INSTRUCTION_THUMB(B ## COND, \
315		if (ARM_COND_ ## COND) { \
316			int8_t immediate = opcode; \
317			cpu->gprs[ARM_PC] += (int32_t) immediate << 1; \
318			currentCycles += ThumbWritePC(cpu); \
319		})
320
321DEFINE_CONDITIONAL_BRANCH_THUMB(EQ)
322DEFINE_CONDITIONAL_BRANCH_THUMB(NE)
323DEFINE_CONDITIONAL_BRANCH_THUMB(CS)
324DEFINE_CONDITIONAL_BRANCH_THUMB(CC)
325DEFINE_CONDITIONAL_BRANCH_THUMB(MI)
326DEFINE_CONDITIONAL_BRANCH_THUMB(PL)
327DEFINE_CONDITIONAL_BRANCH_THUMB(VS)
328DEFINE_CONDITIONAL_BRANCH_THUMB(VC)
329DEFINE_CONDITIONAL_BRANCH_THUMB(LS)
330DEFINE_CONDITIONAL_BRANCH_THUMB(HI)
331DEFINE_CONDITIONAL_BRANCH_THUMB(GE)
332DEFINE_CONDITIONAL_BRANCH_THUMB(LT)
333DEFINE_CONDITIONAL_BRANCH_THUMB(GT)
334DEFINE_CONDITIONAL_BRANCH_THUMB(LE)
335
336DEFINE_INSTRUCTION_THUMB(ADD7, cpu->gprs[ARM_SP] += (opcode & 0x7F) << 2)
337DEFINE_INSTRUCTION_THUMB(SUB4, cpu->gprs[ARM_SP] -= (opcode & 0x7F) << 2)
338
339DEFINE_LOAD_STORE_MULTIPLE_THUMB(POP,
340	ARM_SP,
341	load,
342	IA,
343	,
344	THUMB_LOAD_POST_BODY;
345	cpu->gprs[ARM_SP] = address)
346
347DEFINE_LOAD_STORE_MULTIPLE_THUMB(POPR,
348	ARM_SP,
349	load,
350	IA,
351	rs |= 1 << ARM_PC,
352	THUMB_LOAD_POST_BODY;
353	cpu->gprs[ARM_SP] = address;
354	currentCycles += ThumbWritePC(cpu);)
355
356DEFINE_LOAD_STORE_MULTIPLE_THUMB(PUSH,
357	ARM_SP,
358	store,
359	DB,
360	,
361	THUMB_STORE_POST_BODY;
362	cpu->gprs[ARM_SP] = address)
363
364DEFINE_LOAD_STORE_MULTIPLE_THUMB(PUSHR,
365	ARM_SP,
366	store,
367	DB,
368	rs |= 1 << ARM_LR,
369	THUMB_STORE_POST_BODY;
370	cpu->gprs[ARM_SP] = address)
371
372DEFINE_INSTRUCTION_THUMB(ILL, ARM_ILL)
373DEFINE_INSTRUCTION_THUMB(BKPT, cpu->irqh.bkpt16(cpu, opcode & 0xFF);)
374DEFINE_INSTRUCTION_THUMB(B,
375	int16_t immediate = (opcode & 0x07FF) << 5;
376	cpu->gprs[ARM_PC] += (((int32_t) immediate) >> 4);
377	currentCycles += ThumbWritePC(cpu);)
378
379DEFINE_INSTRUCTION_THUMB(BL1,
380	int16_t immediate = (opcode & 0x07FF) << 5;
381	cpu->gprs[ARM_LR] = cpu->gprs[ARM_PC] + (((int32_t) immediate) << 7);)
382
383DEFINE_INSTRUCTION_THUMB(BL2,
384	uint16_t immediate = (opcode & 0x07FF) << 1;
385	uint32_t pc = cpu->gprs[ARM_PC];
386	cpu->gprs[ARM_PC] = cpu->gprs[ARM_LR] + immediate;
387	cpu->gprs[ARM_LR] = pc - 1;
388	currentCycles += ThumbWritePC(cpu);)
389
390DEFINE_INSTRUCTION_THUMB(BX,
391	int rm = (opcode >> 3) & 0xF;
392	_ARMSetMode(cpu, cpu->gprs[rm] & 0x00000001);
393	int misalign = 0;
394	if (rm == ARM_PC) {
395		misalign = cpu->gprs[rm] & 0x00000002;
396	}
397	cpu->gprs[ARM_PC] = (cpu->gprs[rm] & 0xFFFFFFFE) - misalign;
398	if (cpu->executionMode == MODE_THUMB) {
399		currentCycles += ThumbWritePC(cpu);
400	} else {
401		currentCycles += ARMWritePC(cpu);
402	})
403
404DEFINE_INSTRUCTION_THUMB(SWI, cpu->irqh.swi16(cpu, opcode & 0xFF))
405
406const ThumbInstruction _thumbTable[0x400] = {
407	DECLARE_THUMB_EMITTER_BLOCK(_ThumbInstruction)
408};