all repos — mgba @ 9f77c323757a3ac8c26867ceb57512cd9773325f

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