all repos — mgba @ 97ce972b2a9366589c660246e527ebcd5341e7ee

mGBA Game Boy Advance Emulator

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