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