all repos — mgba @ c8b2d2753e185dd4e8d8309a0d93b1064d1a31f2

mGBA Game Boy Advance Emulator

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

  1#include "isa-thumb.h"
  2
  3#include "isa-inlines.h"
  4
  5static const ThumbInstruction _thumbTable[0x400];
  6
  7void ThumbStep(struct ARMCore* cpu) {
  8	uint32_t address = cpu->gprs[ARM_PC];
  9	cpu->gprs[ARM_PC] = address + WORD_SIZE_THUMB;
 10	address -= WORD_SIZE_THUMB;
 11	uint16_t opcode = ((uint16_t*) cpu->memory->activeRegion)[(address & cpu->memory->activeMask) >> 1];
 12	ThumbInstruction instruction = _thumbTable[opcode >> 6];
 13	instruction(cpu, opcode);
 14}
 15
 16// Instruction definitions
 17// Beware pre-processor insanity
 18
 19#define THUMB_ADDITION_S(M, N, D) \
 20	cpu->cpsr.n = ARM_SIGN(D); \
 21	cpu->cpsr.z = !(D); \
 22	cpu->cpsr.c = ARM_CARRY_FROM(M, N, D); \
 23	cpu->cpsr.v = ARM_V_ADDITION(M, N, D);
 24
 25#define THUMB_SUBTRACTION_S(M, N, D) \
 26	cpu->cpsr.n = ARM_SIGN(D); \
 27	cpu->cpsr.z = !(D); \
 28	cpu->cpsr.c = ARM_BORROW_FROM(M, N, D); \
 29	cpu->cpsr.v = ARM_V_SUBTRACTION(M, N, D);
 30
 31#define THUMB_NEUTRAL_S(M, N, D) \
 32	cpu->cpsr.n = ARM_SIGN(D); \
 33	cpu->cpsr.z = !(D);
 34
 35#define THUMB_ADDITION(D, M, N) \
 36	int n = N; \
 37	int m = M; \
 38	D = M + N; \
 39	THUMB_ADDITION_S(m, n, D)
 40
 41#define THUMB_SUBTRACTION(D, M, N) \
 42	int n = N; \
 43	int m = M; \
 44	D = M - N; \
 45	THUMB_SUBTRACTION_S(m, n, D)
 46
 47#define APPLY(F, ...) F(__VA_ARGS__)
 48
 49#define COUNT_1(EMITTER, PREFIX, ...) \
 50	EMITTER(PREFIX ## 0, 0, __VA_ARGS__) \
 51	EMITTER(PREFIX ## 1, 1, __VA_ARGS__)
 52
 53#define COUNT_2(EMITTER, PREFIX, ...) \
 54	COUNT_1(EMITTER, PREFIX, __VA_ARGS__) \
 55	EMITTER(PREFIX ## 2, 2, __VA_ARGS__) \
 56	EMITTER(PREFIX ## 3, 3, __VA_ARGS__)
 57
 58#define COUNT_3(EMITTER, PREFIX, ...) \
 59	COUNT_2(EMITTER, PREFIX, __VA_ARGS__) \
 60	EMITTER(PREFIX ## 4, 4, __VA_ARGS__) \
 61	EMITTER(PREFIX ## 5, 5, __VA_ARGS__) \
 62	EMITTER(PREFIX ## 6, 6, __VA_ARGS__) \
 63	EMITTER(PREFIX ## 7, 7, __VA_ARGS__)
 64
 65#define COUNT_4(EMITTER, PREFIX, ...) \
 66	COUNT_3(EMITTER, PREFIX, __VA_ARGS__) \
 67	EMITTER(PREFIX ## 8, 8, __VA_ARGS__) \
 68	EMITTER(PREFIX ## 9, 9, __VA_ARGS__) \
 69	EMITTER(PREFIX ## A, 10, __VA_ARGS__) \
 70	EMITTER(PREFIX ## B, 11, __VA_ARGS__) \
 71	EMITTER(PREFIX ## C, 12, __VA_ARGS__) \
 72	EMITTER(PREFIX ## D, 13, __VA_ARGS__) \
 73	EMITTER(PREFIX ## E, 14, __VA_ARGS__) \
 74	EMITTER(PREFIX ## F, 15, __VA_ARGS__)
 75
 76#define COUNT_5(EMITTER, PREFIX, ...) \
 77	COUNT_4(EMITTER, PREFIX ## 0, __VA_ARGS__) \
 78	EMITTER(PREFIX ## 10, 16, __VA_ARGS__) \
 79	EMITTER(PREFIX ## 11, 17, __VA_ARGS__) \
 80	EMITTER(PREFIX ## 12, 18, __VA_ARGS__) \
 81	EMITTER(PREFIX ## 13, 19, __VA_ARGS__) \
 82	EMITTER(PREFIX ## 14, 20, __VA_ARGS__) \
 83	EMITTER(PREFIX ## 15, 21, __VA_ARGS__) \
 84	EMITTER(PREFIX ## 16, 22, __VA_ARGS__) \
 85	EMITTER(PREFIX ## 17, 23, __VA_ARGS__) \
 86	EMITTER(PREFIX ## 18, 24, __VA_ARGS__) \
 87	EMITTER(PREFIX ## 19, 25, __VA_ARGS__) \
 88	EMITTER(PREFIX ## 1A, 26, __VA_ARGS__) \
 89	EMITTER(PREFIX ## 1B, 27, __VA_ARGS__) \
 90	EMITTER(PREFIX ## 1C, 28, __VA_ARGS__) \
 91	EMITTER(PREFIX ## 1D, 29, __VA_ARGS__) \
 92	EMITTER(PREFIX ## 1E, 30, __VA_ARGS__) \
 93	EMITTER(PREFIX ## 1F, 31, __VA_ARGS__) \
 94
 95#define DEFINE_INSTRUCTION_THUMB(NAME, BODY) \
 96	static void _ThumbInstruction ## NAME (struct ARMCore* cpu, uint16_t opcode) {  \
 97		BODY; \
 98		cpu->cycles += 1 + cpu->memory->activePrefetchCycles16; \
 99	}
100
101#define DEFINE_IMMEDIATE_5_INSTRUCTION_EX_THUMB(NAME, IMMEDIATE, BODY) \
102	DEFINE_INSTRUCTION_THUMB(NAME, \
103		int immediate = IMMEDIATE; \
104		int rd = opcode & 0x0007; \
105		int rm = (opcode >> 3) & 0x0007; \
106		BODY;)
107
108#define DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(NAME, BODY) \
109	COUNT_5(DEFINE_IMMEDIATE_5_INSTRUCTION_EX_THUMB, NAME ## _, BODY)
110
111DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(LSL1,
112	if (!immediate) {
113		cpu->gprs[rd] = cpu->gprs[rm];
114	} else {
115		cpu->cpsr.c = cpu->gprs[rm] & (1 << (32 - immediate));
116		cpu->gprs[rd] = cpu->gprs[rm] << immediate;
117	}
118	THUMB_NEUTRAL_S( , , cpu->gprs[rd]);)
119
120DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(LSR1,
121	if (!immediate) {
122		cpu->cpsr.c = ARM_SIGN(cpu->gprs[rm]);
123		cpu->gprs[rd] = 0;
124	} else {
125		cpu->cpsr.c = cpu->gprs[rm] & (1 << (immediate - 1));
126		cpu->gprs[rd] = ((uint32_t) cpu->gprs[rm]) >> immediate;
127	}
128	THUMB_NEUTRAL_S( , , cpu->gprs[rd]);)
129
130DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(ASR1, 
131	if (!immediate) {
132		cpu->cpsr.c = ARM_SIGN(cpu->gprs[rm]);
133		if (cpu->cpsr.c) {
134			cpu->gprs[rd] = 0xFFFFFFFF;
135		} else {
136			cpu->gprs[rd] = 0;
137		}
138	} else {
139		cpu->cpsr.c = cpu->gprs[rm] & (1 << (immediate - 1));
140		cpu->gprs[rd] = cpu->gprs[rm] >> immediate;
141	}
142	THUMB_NEUTRAL_S( , , cpu->gprs[rd]);)
143
144DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(LDR1, cpu->gprs[rd] = cpu->memory->load32(cpu->memory, cpu->gprs[rm] + immediate * 4))
145DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(LDRB1, cpu->gprs[rd] = cpu->memory->loadU8(cpu->memory, cpu->gprs[rm] + immediate))
146DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(LDRH1, cpu->gprs[rd] = cpu->memory->loadU16(cpu->memory, cpu->gprs[rm] + immediate * 2))
147DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(STR1, cpu->memory->store32(cpu->memory, cpu->gprs[rm] + immediate * 4, cpu->gprs[rd]))
148DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(STRB1, cpu->memory->store8(cpu->memory, cpu->gprs[rm] + immediate, cpu->gprs[rd]))
149DEFINE_IMMEDIATE_5_INSTRUCTION_THUMB(STRH1, cpu->memory->store16(cpu->memory, cpu->gprs[rm] + immediate * 2, cpu->gprs[rd]))
150
151#define DEFINE_DATA_FORM_1_INSTRUCTION_EX_THUMB(NAME, RM, BODY) \
152	DEFINE_INSTRUCTION_THUMB(NAME, \
153		int rm = RM; \
154		int rd = opcode & 0x0007; \
155		int rn = (opcode >> 3) & 0x0007; \
156		BODY;)
157
158#define DEFINE_DATA_FORM_1_INSTRUCTION_THUMB(NAME, BODY) \
159	COUNT_3(DEFINE_DATA_FORM_1_INSTRUCTION_EX_THUMB, NAME ## 3_R, BODY)
160
161DEFINE_DATA_FORM_1_INSTRUCTION_THUMB(ADD, THUMB_ADDITION(cpu->gprs[rd], cpu->gprs[rn], cpu->gprs[rm]))
162DEFINE_DATA_FORM_1_INSTRUCTION_THUMB(SUB, THUMB_SUBTRACTION(cpu->gprs[rd], cpu->gprs[rn], cpu->gprs[rm]))
163
164#define DEFINE_DATA_FORM_2_INSTRUCTION_EX_THUMB(NAME, IMMEDIATE, BODY) \
165	DEFINE_INSTRUCTION_THUMB(NAME, \
166		int immediate = IMMEDIATE; \
167		int rd = opcode & 0x0007; \
168		int rn = (opcode >> 3) & 0x0007; \
169		BODY;)
170
171#define DEFINE_DATA_FORM_2_INSTRUCTION_THUMB(NAME, BODY) \
172	COUNT_3(DEFINE_DATA_FORM_2_INSTRUCTION_EX_THUMB, NAME ## 1_, BODY)
173
174DEFINE_DATA_FORM_2_INSTRUCTION_THUMB(ADD, THUMB_ADDITION(cpu->gprs[rd], cpu->gprs[rn], immediate))
175DEFINE_DATA_FORM_2_INSTRUCTION_THUMB(SUB, THUMB_SUBTRACTION(cpu->gprs[rd], cpu->gprs[rn], immediate))
176
177#define DEFINE_DATA_FORM_3_INSTRUCTION_EX_THUMB(NAME, RD, BODY) \
178	DEFINE_INSTRUCTION_THUMB(NAME, \
179		int rd = RD; \
180		int immediate = opcode & 0x00FF; \
181		BODY;)
182
183#define DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(NAME, BODY) \
184	COUNT_3(DEFINE_DATA_FORM_3_INSTRUCTION_EX_THUMB, NAME ## _R, BODY)
185
186DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(ADD2, THUMB_ADDITION(cpu->gprs[rd], cpu->gprs[rd], immediate))
187DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(CMP1, int aluOut = cpu->gprs[rd] - immediate; THUMB_SUBTRACTION_S(cpu->gprs[rd], immediate, aluOut))
188DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(MOV1, cpu->gprs[rd] = immediate; THUMB_NEUTRAL_S(, , cpu->gprs[rd]))
189DEFINE_DATA_FORM_3_INSTRUCTION_THUMB(SUB2, THUMB_SUBTRACTION(cpu->gprs[rd], cpu->gprs[rd], immediate))
190
191#define DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(NAME, BODY) \
192	DEFINE_INSTRUCTION_THUMB(NAME, \
193		int rd = opcode & 0x0007; \
194		int rn = (opcode >> 3) & 0x0007; \
195		BODY;)
196
197DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(AND, cpu->gprs[rd] = cpu->gprs[rd] & cpu->gprs[rn]; THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
198DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(EOR, cpu->gprs[rd] = cpu->gprs[rd] ^ cpu->gprs[rn]; THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
199DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(LSL2,
200	int rs = cpu->gprs[rn] & 0xFF;
201	if (rs) {
202		if (rs < 32) {
203			cpu->cpsr.c = cpu->gprs[rd] & (1 << (32 - rs));
204			cpu->gprs[rd] <<= rs;
205		} else {
206			if (rs > 32) {
207				cpu->cpsr.c = 0;
208			} else {
209				cpu->cpsr.c = cpu->gprs[rd] & 0x00000001;
210			}
211			cpu->gprs[rd] = 0;
212		}
213	}
214	THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
215
216DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(LSR2,
217	int rs = cpu->gprs[rn] & 0xFF;
218	if (rs) {
219		if (rs < 32) {
220			cpu->cpsr.c = cpu->gprs[rd] & (1 << (rs - 1));
221			cpu->gprs[rd] = (uint32_t) cpu->gprs[rd] >> rs;
222		} else {
223			if (rs > 32) {
224				cpu->cpsr.c = 0;
225			} else {
226				cpu->cpsr.c = ARM_SIGN(cpu->gprs[rd]);
227			}
228			cpu->gprs[rd] = 0;
229		}
230	}
231	THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
232
233DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(ASR2,
234	int rs = cpu->gprs[rn] & 0xFF;
235	if (rs) {
236		if (rs < 32) {
237			cpu->cpsr.c = cpu->gprs[rd] & (1 << (rs - 1));
238			cpu->gprs[rd] >>= rs;
239		} else {
240			cpu->cpsr.c = ARM_SIGN(cpu->gprs[rd]);
241			if (cpu->cpsr.c) {
242				cpu->gprs[rd] = 0xFFFFFFFF;
243			} else {
244				cpu->gprs[rd] = 0;
245			}
246		}
247	}
248	THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
249
250DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(ADC, ARM_STUB)
251DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(SBC, ARM_STUB)
252DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(ROR, ARM_STUB)
253DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(TST, ARM_STUB)
254DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(NEG, THUMB_SUBTRACTION(cpu->gprs[rd], 0, cpu->gprs[rn]))
255DEFINE_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))
256DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(CMN, ARM_STUB)
257DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(ORR, cpu->gprs[rd] = cpu->gprs[rd] | cpu->gprs[rn]; THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
258DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(MUL, cpu->gprs[rd] *= cpu->gprs[rn]; THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
259DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(BIC, cpu->gprs[rd] = cpu->gprs[rd] & ~cpu->gprs[rn]; THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
260DEFINE_DATA_FORM_5_INSTRUCTION_THUMB(MVN, cpu->gprs[rd] = ~cpu->gprs[rn]; THUMB_NEUTRAL_S( , , cpu->gprs[rd]))
261
262#define DEFINE_INSTRUCTION_WITH_HIGH_EX_THUMB(NAME, H1, H2, BODY) \
263	DEFINE_INSTRUCTION_THUMB(NAME, \
264		int rd = opcode & 0x0007 | H1; \
265		int rm = (opcode >> 3) & 0x0007 | H2; \
266		BODY;)
267
268#define DEFINE_INSTRUCTION_WITH_HIGH_THUMB(NAME, BODY) \
269	DEFINE_INSTRUCTION_WITH_HIGH_EX_THUMB(NAME ## 00, 0, 0, BODY) \
270	DEFINE_INSTRUCTION_WITH_HIGH_EX_THUMB(NAME ## 01, 0, 8, BODY) \
271	DEFINE_INSTRUCTION_WITH_HIGH_EX_THUMB(NAME ## 10, 8, 0, BODY) \
272	DEFINE_INSTRUCTION_WITH_HIGH_EX_THUMB(NAME ## 11, 8, 8, BODY)
273
274DEFINE_INSTRUCTION_WITH_HIGH_THUMB(ADD4,
275	cpu->gprs[rd] += cpu->gprs[rm];
276	if (rd == ARM_PC) {
277		THUMB_WRITE_PC;
278	})
279
280DEFINE_INSTRUCTION_WITH_HIGH_THUMB(CMP3, int32_t aluOut = cpu->gprs[rd] - cpu->gprs[rm]; THUMB_SUBTRACTION_S(cpu->gprs[rd], cpu->gprs[rm], aluOut))
281DEFINE_INSTRUCTION_WITH_HIGH_THUMB(MOV3,
282	cpu->gprs[rd] = cpu->gprs[rm];
283	if (rd == ARM_PC) {
284		THUMB_WRITE_PC;
285	})
286
287#define DEFINE_IMMEDIATE_WITH_REGISTER_EX_THUMB(NAME, RD, BODY) \
288	DEFINE_INSTRUCTION_THUMB(NAME, \
289		int rd = RD; \
290		int immediate = (opcode & 0x00FF) << 2; \
291		BODY;)
292
293#define DEFINE_IMMEDIATE_WITH_REGISTER_THUMB(NAME, BODY) \
294	COUNT_3(DEFINE_IMMEDIATE_WITH_REGISTER_EX_THUMB, NAME ## _R, BODY)
295
296DEFINE_IMMEDIATE_WITH_REGISTER_THUMB(LDR3, cpu->gprs[rd] = cpu->memory->load32(cpu->memory, cpu->gprs[ARM_PC] + immediate))
297DEFINE_IMMEDIATE_WITH_REGISTER_THUMB(LDR4, cpu->gprs[rd] = cpu->memory->load32(cpu->memory, cpu->gprs[ARM_SP] + immediate))
298DEFINE_IMMEDIATE_WITH_REGISTER_THUMB(STR3, cpu->memory->store32(cpu->memory, cpu->gprs[ARM_SP] + immediate, cpu->gprs[rd]))
299
300DEFINE_IMMEDIATE_WITH_REGISTER_THUMB(ADD5, cpu->gprs[rd] = (cpu->gprs[ARM_PC] & 0xFFFFFFFC) + immediate)
301DEFINE_IMMEDIATE_WITH_REGISTER_THUMB(ADD6, cpu->gprs[rd] = cpu->gprs[ARM_SP] + immediate)
302
303#define DEFINE_LOAD_STORE_WITH_REGISTER_EX_THUMB(NAME, RM, BODY) \
304	DEFINE_INSTRUCTION_THUMB(NAME, \
305		int rm = RM; \
306		int rd = opcode & 0x0007; \
307		int rn = (opcode >> 3) & 0x0007; \
308		BODY;)
309
310#define DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(NAME, BODY) \
311	COUNT_3(DEFINE_LOAD_STORE_WITH_REGISTER_EX_THUMB, NAME ## _R, BODY)
312
313DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(LDR2, cpu->gprs[rd] = cpu->memory->load32(cpu->memory, cpu->gprs[rn] + cpu->gprs[rm]))
314DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(LDRB2, cpu->gprs[rd] = cpu->memory->loadU8(cpu->memory, cpu->gprs[rn] + cpu->gprs[rm]))
315DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(LDRH2, cpu->gprs[rd] = cpu->memory->loadU16(cpu->memory, cpu->gprs[rn] + cpu->gprs[rm]))
316DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(LDRSB, cpu->gprs[rd] = cpu->memory->load8(cpu->memory, cpu->gprs[rn] + cpu->gprs[rm]))
317DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(LDRSH, cpu->gprs[rd] = cpu->memory->load16(cpu->memory, cpu->gprs[rn] + cpu->gprs[rm]))
318DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(STR2, ARM_STUB)
319DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(STRB2, cpu->memory->store8(cpu->memory, cpu->gprs[rn] + cpu->gprs[rm], cpu->gprs[rd]))
320DEFINE_LOAD_STORE_WITH_REGISTER_THUMB(STRH2, ARM_STUB)
321
322#define DEFINE_LOAD_STORE_MULTIPLE_EX_THUMB(NAME, RN, ADDRESS, LOOP, BODY, OP, PRE_BODY, POST_BODY, WRITEBACK) \
323	DEFINE_INSTRUCTION_THUMB(NAME, \
324		int rn = RN; \
325		int rs = opcode & 0xFF; \
326		int32_t address = ADDRESS; \
327		int m; \
328		int i; \
329		PRE_BODY; \
330		for LOOP { \
331			if (rs & m) { \
332				BODY; \
333				address OP 4; \
334			} \
335		} \
336		POST_BODY; \
337		WRITEBACK;)
338
339#define DEFINE_LOAD_STORE_MULTIPLE_THUMB(NAME, BODY, WRITEBACK) \
340	COUNT_3(DEFINE_LOAD_STORE_MULTIPLE_EX_THUMB, NAME ## _R, cpu->gprs[rn], (m = 0x01, i = 0; i < 8; m <<= 1, ++i), BODY, +=, , , WRITEBACK)
341
342DEFINE_LOAD_STORE_MULTIPLE_THUMB(LDMIA,
343	cpu->gprs[i] = cpu->memory->load32(cpu->memory, address),
344	if (!((1 << rn) & rs)) {
345		cpu->gprs[rn] = address;
346	})
347
348DEFINE_LOAD_STORE_MULTIPLE_THUMB(STMIA,
349	cpu->memory->store32(cpu->memory, address, cpu->gprs[i]),
350	cpu->gprs[rn] = address)
351
352#define DEFINE_CONDITIONAL_BRANCH_THUMB(COND) \
353	DEFINE_INSTRUCTION_THUMB(B ## COND, \
354		if (ARM_COND_ ## COND) { \
355			int8_t immediate = opcode; \
356			cpu->gprs[ARM_PC] += immediate << 1; \
357			THUMB_WRITE_PC; \
358		})
359
360DEFINE_CONDITIONAL_BRANCH_THUMB(EQ)
361DEFINE_CONDITIONAL_BRANCH_THUMB(NE)
362DEFINE_CONDITIONAL_BRANCH_THUMB(CS)
363DEFINE_CONDITIONAL_BRANCH_THUMB(CC)
364DEFINE_CONDITIONAL_BRANCH_THUMB(MI)
365DEFINE_CONDITIONAL_BRANCH_THUMB(PL)
366DEFINE_CONDITIONAL_BRANCH_THUMB(VS)
367DEFINE_CONDITIONAL_BRANCH_THUMB(VC)
368DEFINE_CONDITIONAL_BRANCH_THUMB(LS)
369DEFINE_CONDITIONAL_BRANCH_THUMB(HI)
370DEFINE_CONDITIONAL_BRANCH_THUMB(GE)
371DEFINE_CONDITIONAL_BRANCH_THUMB(LT)
372DEFINE_CONDITIONAL_BRANCH_THUMB(GT)
373DEFINE_CONDITIONAL_BRANCH_THUMB(LE)
374
375DEFINE_INSTRUCTION_THUMB(ADD7, cpu->gprs[ARM_SP] += (opcode & 0x7F) << 2)
376DEFINE_INSTRUCTION_THUMB(SUB4, cpu->gprs[ARM_SP] -= (opcode & 0x7F) << 2)
377
378DEFINE_LOAD_STORE_MULTIPLE_EX_THUMB(POP,
379	opcode & 0x00FF,
380	cpu->gprs[ARM_SP],
381	(m = 0x01, i = 0; i < 8; m <<= 1, ++i),
382	cpu->gprs[i] = cpu->memory->load32(cpu->memory, address),
383	+=,
384	, ,
385	cpu->gprs[ARM_SP] = address)
386
387DEFINE_LOAD_STORE_MULTIPLE_EX_THUMB(POPR,
388	opcode & 0x00FF,
389	cpu->gprs[ARM_SP],
390	(m = 0x01, i = 0; i < 8; m <<= 1, ++i),
391	cpu->gprs[i] = cpu->memory->load32(cpu->memory, address),
392	+=,
393	,
394	cpu->gprs[ARM_PC] = cpu->memory->load32(cpu->memory, address) & 0xFFFFFFFE;
395	address += 4;,
396	cpu->gprs[ARM_SP] = address;
397	THUMB_WRITE_PC;)
398
399DEFINE_LOAD_STORE_MULTIPLE_EX_THUMB(PUSH,
400	opcode & 0x00FF,
401	cpu->gprs[ARM_SP] - 4,
402	(m = 0x80, i = 7; m; m >>= 1, --i),
403	cpu->memory->store32(cpu->memory, address, cpu->gprs[i]),
404	-=,
405	, ,
406	cpu->gprs[ARM_SP] = address + 4)
407
408DEFINE_LOAD_STORE_MULTIPLE_EX_THUMB(PUSHR,
409	opcode & 0x00FF,
410	cpu->gprs[ARM_SP] - 4,
411	(m = 0x80, i = 7; m; m >>= 1, --i),
412	cpu->memory->store32(cpu->memory, address, cpu->gprs[i]),
413	-=,
414	cpu->memory->store32(cpu->memory, address, cpu->gprs[ARM_LR]);
415	address -= 4;,
416	,
417	cpu->gprs[ARM_SP] = address + 4)
418
419DEFINE_INSTRUCTION_THUMB(ILL, ARM_STUB)
420DEFINE_INSTRUCTION_THUMB(BKPT, ARM_STUB)
421DEFINE_INSTRUCTION_THUMB(B,
422	int16_t immediate = (opcode & 0x07FF) << 5;
423	cpu->gprs[ARM_PC] += (((int32_t) immediate) >> 4);
424	THUMB_WRITE_PC;)
425
426DEFINE_INSTRUCTION_THUMB(BL1,
427	int16_t immediate = (opcode & 0x07FF) << 5;
428	cpu->gprs[ARM_LR] = cpu->gprs[ARM_PC] + (((int32_t) immediate) << 7);)
429
430DEFINE_INSTRUCTION_THUMB(BL2,
431	uint16_t immediate = (opcode & 0x07FF) << 1;
432	uint32_t pc = cpu->gprs[ARM_PC];
433	cpu->gprs[ARM_PC] = cpu->gprs[ARM_LR] + immediate;
434	cpu->gprs[ARM_LR] = pc - 1;
435	THUMB_WRITE_PC;)
436
437DEFINE_INSTRUCTION_THUMB(BX,
438	int rm = (opcode >> 3) & 0xF;
439	_ARMSetMode(cpu, cpu->gprs[rm] & 0x00000001);
440	int misalign = 0;
441	if (rm == ARM_PC) {
442		misalign = cpu->gprs[rm] & 0x00000002;
443	}
444	cpu->gprs[ARM_PC] = cpu->gprs[rm] & 0xFFFFFFFE - misalign;
445	if (cpu->executionMode == MODE_THUMB) {
446		THUMB_WRITE_PC;
447	} else {
448		ARM_WRITE_PC;
449	})
450
451DEFINE_INSTRUCTION_THUMB(SWI, cpu->board->swi16(cpu->board, opcode & 0xFF))
452
453#define DECLARE_INSTRUCTION_THUMB(EMITTER, NAME) \
454	EMITTER ## NAME
455
456#define DECLARE_INSTRUCTION_WITH_HIGH_THUMB(EMITTER, NAME) \
457	DECLARE_INSTRUCTION_THUMB(EMITTER, NAME ## 00), \
458	DECLARE_INSTRUCTION_THUMB(EMITTER, NAME ## 01), \
459	DECLARE_INSTRUCTION_THUMB(EMITTER, NAME ## 10), \
460	DECLARE_INSTRUCTION_THUMB(EMITTER, NAME ## 11)
461
462#define DUMMY(X, ...) X,
463#define DUMMY_4(...) \
464	DUMMY(__VA_ARGS__) \
465	DUMMY(__VA_ARGS__) \
466	DUMMY(__VA_ARGS__) \
467	DUMMY(__VA_ARGS__)
468
469#define DECLARE_THUMB_EMITTER_BLOCK(EMITTER) \
470	APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LSL1_)) \
471	APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LSR1_)) \
472	APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, ASR1_)) \
473	APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, ADD3_R)) \
474	APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, SUB3_R)) \
475	APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, ADD1_)) \
476	APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, SUB1_)) \
477	APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, MOV1_R)) \
478	APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, CMP1_R)) \
479	APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, ADD2_R)) \
480	APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, SUB2_R)) \
481	DECLARE_INSTRUCTION_THUMB(EMITTER, AND), \
482	DECLARE_INSTRUCTION_THUMB(EMITTER, EOR), \
483	DECLARE_INSTRUCTION_THUMB(EMITTER, LSL2), \
484	DECLARE_INSTRUCTION_THUMB(EMITTER, LSR2), \
485	DECLARE_INSTRUCTION_THUMB(EMITTER, ASR2), \
486	DECLARE_INSTRUCTION_THUMB(EMITTER, ADC), \
487	DECLARE_INSTRUCTION_THUMB(EMITTER, SBC), \
488	DECLARE_INSTRUCTION_THUMB(EMITTER, ROR), \
489	DECLARE_INSTRUCTION_THUMB(EMITTER, TST), \
490	DECLARE_INSTRUCTION_THUMB(EMITTER, NEG), \
491	DECLARE_INSTRUCTION_THUMB(EMITTER, CMP2), \
492	DECLARE_INSTRUCTION_THUMB(EMITTER, CMN), \
493	DECLARE_INSTRUCTION_THUMB(EMITTER, ORR), \
494	DECLARE_INSTRUCTION_THUMB(EMITTER, MUL), \
495	DECLARE_INSTRUCTION_THUMB(EMITTER, BIC), \
496	DECLARE_INSTRUCTION_THUMB(EMITTER, MVN), \
497	DECLARE_INSTRUCTION_WITH_HIGH_THUMB(EMITTER, ADD4), \
498	DECLARE_INSTRUCTION_WITH_HIGH_THUMB(EMITTER, CMP3), \
499	DECLARE_INSTRUCTION_WITH_HIGH_THUMB(EMITTER, MOV3), \
500	DECLARE_INSTRUCTION_THUMB(EMITTER, BX), \
501	DECLARE_INSTRUCTION_THUMB(EMITTER, BX), \
502	DECLARE_INSTRUCTION_THUMB(EMITTER, ILL), \
503	DECLARE_INSTRUCTION_THUMB(EMITTER, ILL), \
504	APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, LDR3_R)) \
505	APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, STR2_R)) \
506	APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, STRH2_R)) \
507	APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, STRB2_R)) \
508	APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LDRSB_R)) \
509	APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LDR2_R)) \
510	APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LDRH2_R)) \
511	APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LDRB2_R)) \
512	APPLY(COUNT_3, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LDRSH_R)) \
513	APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, STR1_)) \
514	APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LDR1_)) \
515	APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, STRB1_)) \
516	APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LDRB1_)) \
517	APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, STRH1_)) \
518	APPLY(COUNT_5, DUMMY, DECLARE_INSTRUCTION_THUMB(EMITTER, LDRH1_)) \
519	APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, STR3_R)) \
520	APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, LDR4_R)) \
521	APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, ADD5_R)) \
522	APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, ADD6_R)) \
523	DECLARE_INSTRUCTION_THUMB(EMITTER, ADD7), \
524	DECLARE_INSTRUCTION_THUMB(EMITTER, ADD7), \
525	DECLARE_INSTRUCTION_THUMB(EMITTER, SUB4), \
526	DECLARE_INSTRUCTION_THUMB(EMITTER, SUB4), \
527	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, ILL)), \
528	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, ILL)), \
529	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, ILL)), \
530	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, PUSH)), \
531	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, PUSHR)), \
532	DO_8(DECLARE_INSTRUCTION_THUMB(EMITTER, ILL)), \
533	DO_8(DECLARE_INSTRUCTION_THUMB(EMITTER, ILL)), \
534	DO_8(DECLARE_INSTRUCTION_THUMB(EMITTER, ILL)), \
535	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, POP)), \
536	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, POPR)), \
537	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BKPT)), \
538	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, ILL)), \
539	APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, STMIA_R)) \
540	APPLY(COUNT_3, DUMMY_4, DECLARE_INSTRUCTION_THUMB(EMITTER, LDMIA_R)) \
541	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BEQ)), \
542	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BNE)), \
543	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BCS)), \
544	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BCC)), \
545	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BMI)), \
546	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BPL)), \
547	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BVS)), \
548	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BVC)), \
549	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BHI)), \
550	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BLS)), \
551	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BGE)), \
552	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BLT)), \
553	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BGT)), \
554	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BLE)), \
555	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, ILL)), \
556	DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, SWI)), \
557	DO_8(DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, B))), \
558	DO_8(DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, ILL))), \
559	DO_8(DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BL1))), \
560	DO_8(DO_4(DECLARE_INSTRUCTION_THUMB(EMITTER, BL2))) \
561
562static const ThumbInstruction _thumbTable[0x400] = {
563	DECLARE_THUMB_EMITTER_BLOCK(_ThumbInstruction)
564};