Debian Codegraph @

arch/M68K/M68KDisassembler.c

raw symbols SCIP
1 /* ======================================================================== */
2 /* ========================= LICENSING & COPYRIGHT ======================== */
3 /* ======================================================================== */
4 /*
5 * MUSASHI
6 * Version 3.4
7 *
8 * A portable Motorola M680x0 processor emulation engine.
9 * Copyright 1998-2001 Karl Stenerud. All rights reserved.
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 */
29
30 /* The code below is based on MUSASHI but has been heavily modified for Capstone by
31 * Daniel Collin <[email protected]> 2015-2019 */
32
33 /* ======================================================================== */
34 /* ================================ INCLUDES ============================== */
35 /* ======================================================================== */
36
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40
41 #include "../../cs_priv.h"
42 #include "../../utils.h"
43
44 #include "../../MCInst.h"
45 #include "../../MCInstrDesc.h"
46 #include "../../MCRegisterInfo.h"
47 #include "M68KInstPrinter.h"
48 #include "M68KDisassembler.h"
49
50 /* ======================================================================== */
51 /* ============================ GENERAL DEFINES =========================== */
52 /* ======================================================================== */
53
54 /* Bit Isolation Functions */
55 #define BIT_0(A) ((A) & 0x00000001)
56 #define BIT_1(A) ((A) & 0x00000002)
57 #define BIT_2(A) ((A) & 0x00000004)
58 #define BIT_3(A) ((A) & 0x00000008)
59 #define BIT_4(A) ((A) & 0x00000010)
60 #define BIT_5(A) ((A) & 0x00000020)
61 #define BIT_6(A) ((A) & 0x00000040)
62 #define BIT_7(A) ((A) & 0x00000080)
63 #define BIT_8(A) ((A) & 0x00000100)
64 #define BIT_9(A) ((A) & 0x00000200)
65 #define BIT_A(A) ((A) & 0x00000400)
66 #define BIT_B(A) ((A) & 0x00000800)
67 #define BIT_C(A) ((A) & 0x00001000)
68 #define BIT_D(A) ((A) & 0x00002000)
69 #define BIT_E(A) ((A) & 0x00004000)
70 #define BIT_F(A) ((A) & 0x00008000)
71 #define BIT_10(A) ((A) & 0x00010000)
72 #define BIT_11(A) ((A) & 0x00020000)
73 #define BIT_12(A) ((A) & 0x00040000)
74 #define BIT_13(A) ((A) & 0x00080000)
75 #define BIT_14(A) ((A) & 0x00100000)
76 #define BIT_15(A) ((A) & 0x00200000)
77 #define BIT_16(A) ((A) & 0x00400000)
78 #define BIT_17(A) ((A) & 0x00800000)
79 #define BIT_18(A) ((A) & 0x01000000)
80 #define BIT_19(A) ((A) & 0x02000000)
81 #define BIT_1A(A) ((A) & 0x04000000)
82 #define BIT_1B(A) ((A) & 0x08000000)
83 #define BIT_1C(A) ((A) & 0x10000000)
84 #define BIT_1D(A) ((A) & 0x20000000)
85 #define BIT_1E(A) ((A) & 0x40000000)
86 #define BIT_1F(A) ((A) & 0x80000000)
87
88 /* These are the CPU types understood by this disassembler */
89 #define TYPE_68000 1
90 #define TYPE_68010 2
91 #define TYPE_68020 4
92 #define TYPE_68030 8
93 #define TYPE_68040 16
94
95 #define M68000_ONLY TYPE_68000
96
97 #define M68010_ONLY TYPE_68010
98 #define M68010_LESS (TYPE_68000 | TYPE_68010)
99 #define M68010_PLUS (TYPE_68010 | TYPE_68020 | TYPE_68030 | TYPE_68040)
100
101 #define M68020_ONLY TYPE_68020
102 #define M68020_LESS (TYPE_68010 | TYPE_68020)
103 #define M68020_PLUS (TYPE_68020 | TYPE_68030 | TYPE_68040)
104
105 #define M68030_ONLY TYPE_68030
106 #define M68030_LESS (TYPE_68010 | TYPE_68020 | TYPE_68030)
107 #define M68030_PLUS (TYPE_68030 | TYPE_68040)
108
109 #define M68040_PLUS TYPE_68040
110
111 enum {
112 M68K_CPU_TYPE_INVALID,
113 M68K_CPU_TYPE_68000,
114 M68K_CPU_TYPE_68010,
115 M68K_CPU_TYPE_68EC020,
116 M68K_CPU_TYPE_68020,
117 M68K_CPU_TYPE_68030, /* Supported by disassembler ONLY */
118 M68K_CPU_TYPE_68040 /* Supported by disassembler ONLY */
119 };
120
121 /* Extension word formats */
122 #define EXT_8BIT_DISPLACEMENT(A) ((A)&0xff)
123 #define EXT_FULL(A) BIT_8(A)
124 #define EXT_EFFECTIVE_ZERO(A) (((A)&0xe4) == 0xc4 || ((A)&0xe2) == 0xc0)
125 #define EXT_BASE_REGISTER_PRESENT(A) (!BIT_7(A))
126 #define EXT_INDEX_REGISTER_PRESENT(A) (!BIT_6(A))
127 #define EXT_INDEX_REGISTER(A) (((A)>>12)&7)
128 #define EXT_INDEX_PRE_POST(A) (EXT_INDEX_PRESENT(A) && (A)&3)
129 #define EXT_INDEX_PRE(A) (EXT_INDEX_PRESENT(A) && ((A)&7) < 4 && ((A)&7) != 0)
130 #define EXT_INDEX_POST(A) (EXT_INDEX_PRESENT(A) && ((A)&7) > 4)
131 #define EXT_INDEX_SCALE(A) (((A)>>9)&3)
132 #define EXT_INDEX_LONG(A) BIT_B(A)
133 #define EXT_INDEX_AR(A) BIT_F(A)
134 #define EXT_BASE_DISPLACEMENT_PRESENT(A) (((A)&0x30) > 0x10)
135 #define EXT_BASE_DISPLACEMENT_WORD(A) (((A)&0x30) == 0x20)
136 #define EXT_BASE_DISPLACEMENT_LONG(A) (((A)&0x30) == 0x30)
137 #define EXT_OUTER_DISPLACEMENT_PRESENT(A) (((A)&3) > 1 && ((A)&0x47) < 0x44)
138 #define EXT_OUTER_DISPLACEMENT_WORD(A) (((A)&3) == 2 && ((A)&0x47) < 0x44)
139 #define EXT_OUTER_DISPLACEMENT_LONG(A) (((A)&3) == 3 && ((A)&0x47) < 0x44)
140
141 #define IS_BITSET(val,b) ((val) & (1 << (b)))
142 #define BITFIELD_MASK(sb,eb) (((1 << ((sb) + 1))-1) & (~((1 << (eb))-1)))
143 #define BITFIELD(val,sb,eb) ((BITFIELD_MASK(sb,eb) & (val)) >> (eb))
144
145 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
146
147 static unsigned int m68k_read_disassembler_16(const m68k_info *info, const uint64_t addr)
148 {
149 const uint16_t v0 = info->code[addr + 0];
150 const uint16_t v1 = info->code[addr + 1];
151 return (v0 << 8) | v1;
152 }
153
154 static unsigned int m68k_read_disassembler_32(const m68k_info *info, const uint64_t addr)
155 {
156 const uint32_t v0 = info->code[addr + 0];
157 const uint32_t v1 = info->code[addr + 1];
158 const uint32_t v2 = info->code[addr + 2];
159 const uint32_t v3 = info->code[addr + 3];
160 return (v0 << 24) | (v1 << 16) | (v2 << 8) | v3;
161 }
162
163 static uint64_t m68k_read_disassembler_64(const m68k_info *info, const uint64_t addr)
164 {
165 const uint64_t v0 = info->code[addr + 0];
166 const uint64_t v1 = info->code[addr + 1];
167 const uint64_t v2 = info->code[addr + 2];
168 const uint64_t v3 = info->code[addr + 3];
169 const uint64_t v4 = info->code[addr + 4];
170 const uint64_t v5 = info->code[addr + 5];
171 const uint64_t v6 = info->code[addr + 6];
172 const uint64_t v7 = info->code[addr + 7];
173 return (v0 << 56) | (v1 << 48) | (v2 << 40) | (v3 << 32) | (v4 << 24) | (v5 << 16) | (v6 << 8) | v7;
174 }
175
176 static unsigned int m68k_read_safe_16(const m68k_info *info, const uint64_t address)
177 {
178 const uint64_t addr = (address - info->baseAddress) & info->address_mask;
179 if (info->code_len < addr + 2) {
180 return 0xaaaa;
181 }
182 return m68k_read_disassembler_16(info, addr);
183 }
184
185 static unsigned int m68k_read_safe_32(const m68k_info *info, const uint64_t address)
186 {
187 const uint64_t addr = (address - info->baseAddress) & info->address_mask;
188 if (info->code_len < addr + 4) {
189 return 0xaaaaaaaa;
190 }
191 return m68k_read_disassembler_32(info, addr);
192 }
193
194 static uint64_t m68k_read_safe_64(const m68k_info *info, const uint64_t address)
195 {
196 const uint64_t addr = (address - info->baseAddress) & info->address_mask;
197 if (info->code_len < addr + 8) {
198 return 0xaaaaaaaaaaaaaaaaLL;
199 }
200 return m68k_read_disassembler_64(info, addr);
201 }
202
203 /* ======================================================================== */
204 /* =============================== PROTOTYPES ============================= */
205 /* ======================================================================== */
206
207 /* make signed integers 100% portably */
208 static int make_int_8(int value);
209 static int make_int_16(int value);
210
211 /* Stuff to build the opcode handler jump table */
212 static void d68000_invalid(m68k_info *info);
213 static int instruction_is_valid(m68k_info *info, const unsigned int word_check);
214
215 typedef struct {
216 void (*instruction)(m68k_info *info); /* handler function */
217 uint16_t word2_mask; /* mask the 2nd word */
218 uint16_t word2_match; /* what to match after masking */
219 } instruction_struct;
220
221 /* ======================================================================== */
222 /* ================================= DATA ================================= */
223 /* ======================================================================== */
224
225 static const instruction_struct g_instruction_table[0x10000];
226
227 /* used by ops like asr, ror, addq, etc */
228 static const uint32_t g_3bit_qdata_table[8] = {8, 1, 2, 3, 4, 5, 6, 7};
229
230 static const uint32_t g_5bit_data_table[32] = {
231 32, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
232 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
233 };
234
235 static const m68k_insn s_branch_lut[] = {
236 M68K_INS_INVALID, M68K_INS_INVALID, M68K_INS_BHI, M68K_INS_BLS,
237 M68K_INS_BCC, M68K_INS_BCS, M68K_INS_BNE, M68K_INS_BEQ,
238 M68K_INS_BVC, M68K_INS_BVS, M68K_INS_BPL, M68K_INS_BMI,
239 M68K_INS_BGE, M68K_INS_BLT, M68K_INS_BGT, M68K_INS_BLE,
240 };
241
242 static const m68k_insn s_dbcc_lut[] = {
243 M68K_INS_DBT, M68K_INS_DBF, M68K_INS_DBHI, M68K_INS_DBLS,
244 M68K_INS_DBCC, M68K_INS_DBCS, M68K_INS_DBNE, M68K_INS_DBEQ,
245 M68K_INS_DBVC, M68K_INS_DBVS, M68K_INS_DBPL, M68K_INS_DBMI,
246 M68K_INS_DBGE, M68K_INS_DBLT, M68K_INS_DBGT, M68K_INS_DBLE,
247 };
248
249 static const m68k_insn s_scc_lut[] = {
250 M68K_INS_ST, M68K_INS_SF, M68K_INS_SHI, M68K_INS_SLS,
251 M68K_INS_SCC, M68K_INS_SCS, M68K_INS_SNE, M68K_INS_SEQ,
252 M68K_INS_SVC, M68K_INS_SVS, M68K_INS_SPL, M68K_INS_SMI,
253 M68K_INS_SGE, M68K_INS_SLT, M68K_INS_SGT, M68K_INS_SLE,
254 };
255
256 static const m68k_insn s_trap_lut[] = {
257 M68K_INS_TRAPT, M68K_INS_TRAPF, M68K_INS_TRAPHI, M68K_INS_TRAPLS,
258 M68K_INS_TRAPCC, M68K_INS_TRAPCS, M68K_INS_TRAPNE, M68K_INS_TRAPEQ,
259 M68K_INS_TRAPVC, M68K_INS_TRAPVS, M68K_INS_TRAPPL, M68K_INS_TRAPMI,
260 M68K_INS_TRAPGE, M68K_INS_TRAPLT, M68K_INS_TRAPGT, M68K_INS_TRAPLE,
261 };
262
263 /* ======================================================================== */
264 /* =========================== UTILITY FUNCTIONS ========================== */
265 /* ======================================================================== */
266
267 #define LIMIT_CPU_TYPES(info, ALLOWED_CPU_TYPES) \
268 do { \
269 if (!(info->type & ALLOWED_CPU_TYPES)) { \
270 d68000_invalid(info); \
271 return; \
272 } \
273 } while (0)
274
275 static unsigned int peek_imm_8(const m68k_info *info) { return (m68k_read_safe_16((info), (info)->pc)&0xff); }
276 static unsigned int peek_imm_16(const m68k_info *info) { return m68k_read_safe_16((info), (info)->pc); }
277 static unsigned int peek_imm_32(const m68k_info *info) { return m68k_read_safe_32((info), (info)->pc); }
278 static unsigned long long peek_imm_64(const m68k_info *info) { return m68k_read_safe_64((info), (info)->pc); }
279
280 static unsigned int read_imm_8(m68k_info *info) { const unsigned int value = peek_imm_8(info); (info)->pc+=2; return value; }
281 static unsigned int read_imm_16(m68k_info *info) { const unsigned int value = peek_imm_16(info); (info)->pc+=2; return value; }
282 static unsigned int read_imm_32(m68k_info *info) { const unsigned int value = peek_imm_32(info); (info)->pc+=4; return value; }
283 static unsigned long long read_imm_64(m68k_info *info) { const unsigned long long value = peek_imm_64(info); (info)->pc+=8; return value; }
284
285 /* Fake a split interface */
286 #define get_ea_mode_str_8(instruction) get_ea_mode_str(instruction, 0)
287 #define get_ea_mode_str_16(instruction) get_ea_mode_str(instruction, 1)
288 #define get_ea_mode_str_32(instruction) get_ea_mode_str(instruction, 2)
289
290 #define get_imm_str_s8() get_imm_str_s(0)
291 #define get_imm_str_s16() get_imm_str_s(1)
292 #define get_imm_str_s32() get_imm_str_s(2)
293
294 #define get_imm_str_u8() get_imm_str_u(0)
295 #define get_imm_str_u16() get_imm_str_u(1)
296 #define get_imm_str_u32() get_imm_str_u(2)
297
298
299 /* 100% portable signed int generators */
300 static int make_int_8(int value)
301 {
302 return (value & 0x80) ? value | ~0xff : value & 0xff;
303 }
304
305 static int make_int_16(int value)
306 {
307 return (value & 0x8000) ? value | ~0xffff : value & 0xffff;
308 }
309
310 static void get_with_index_address_mode(m68k_info *info, cs_m68k_op* op, uint32_t instruction, uint32_t size, bool is_pc)
311 {
312 uint32_t extension = read_imm_16(info);
313
314 op->address_mode = M68K_AM_AREGI_INDEX_BASE_DISP;
315
316 if (EXT_FULL(extension)) {
317 uint32_t preindex;
318 uint32_t postindex;
319
320 op->mem.base_reg = M68K_REG_INVALID;
321 op->mem.index_reg = M68K_REG_INVALID;
322
323 /* Not sure how to deal with this?
324 if (EXT_EFFECTIVE_ZERO(extension)) {
325 strcpy(mode, "0");
326 break;
327 }
328 */
329
330 op->mem.in_disp = EXT_BASE_DISPLACEMENT_PRESENT(extension) ? (EXT_BASE_DISPLACEMENT_LONG(extension) ? read_imm_32(info) : read_imm_16(info)) : 0;
331 op->mem.out_disp = EXT_OUTER_DISPLACEMENT_PRESENT(extension) ? (EXT_OUTER_DISPLACEMENT_LONG(extension) ? read_imm_32(info) : read_imm_16(info)) : 0;
332
333 if (EXT_BASE_REGISTER_PRESENT(extension)) {
334 if (is_pc) {
335 op->mem.base_reg = M68K_REG_PC;
336 } else {
337 op->mem.base_reg = M68K_REG_A0 + (instruction & 7);
338 }
339 }
340
341 if (EXT_INDEX_REGISTER_PRESENT(extension)) {
342 if (EXT_INDEX_AR(extension)) {
343 op->mem.index_reg = M68K_REG_A0 + EXT_INDEX_REGISTER(extension);
344 } else {
345 op->mem.index_reg = M68K_REG_D0 + EXT_INDEX_REGISTER(extension);
346 }
347
348 op->mem.index_size = EXT_INDEX_LONG(extension) ? 1 : 0;
349
350 if (EXT_INDEX_SCALE(extension)) {
351 op->mem.scale = 1 << EXT_INDEX_SCALE(extension);
352 }
353 }
354
355 preindex = (extension & 7) > 0 && (extension & 7) < 4;
356 postindex = (extension & 7) > 4;
357
358 if (preindex) {
359 op->address_mode = is_pc ? M68K_AM_PC_MEMI_PRE_INDEX : M68K_AM_MEMI_PRE_INDEX;
360 } else if (postindex) {
361 op->address_mode = is_pc ? M68K_AM_PC_MEMI_POST_INDEX : M68K_AM_MEMI_POST_INDEX;
362 }
363
364 return;
365 }
366
367 op->mem.index_reg = (EXT_INDEX_AR(extension) ? M68K_REG_A0 : M68K_REG_D0) + EXT_INDEX_REGISTER(extension);
368 op->mem.index_size = EXT_INDEX_LONG(extension) ? 1 : 0;
369
370 if (EXT_8BIT_DISPLACEMENT(extension) == 0) {
371 if (is_pc) {
372 op->mem.base_reg = M68K_REG_PC;
373 op->address_mode = M68K_AM_PCI_INDEX_BASE_DISP;
374 } else {
375 op->mem.base_reg = M68K_REG_A0 + (instruction & 7);
376 }
377 } else {
378 if (is_pc) {
379 op->mem.base_reg = M68K_REG_PC;
380 op->address_mode = M68K_AM_PCI_INDEX_8_BIT_DISP;
381 } else {
382 op->mem.base_reg = M68K_REG_A0 + (instruction & 7);
383 op->address_mode = M68K_AM_AREGI_INDEX_8_BIT_DISP;
384 }
385
386 op->mem.disp = (int8_t)(extension & 0xff);
387 }
388
389 if (EXT_INDEX_SCALE(extension)) {
390 op->mem.scale = 1 << EXT_INDEX_SCALE(extension);
391 }
392 }
393
394 /* Make string of effective address mode */
395 static void get_ea_mode_op(m68k_info *info, cs_m68k_op* op, uint32_t instruction, uint32_t size)
396 {
397 // default to memory
398
399 op->type = M68K_OP_MEM;
400
401 switch (instruction & 0x3f) {
402 case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
403 /* data register direct */
404 op->address_mode = M68K_AM_REG_DIRECT_DATA;
405 op->reg = M68K_REG_D0 + (instruction & 7);
406 op->type = M68K_OP_REG;
407 break;
408
409 case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
410 /* address register direct */
411 op->address_mode = M68K_AM_REG_DIRECT_ADDR;
412 op->reg = M68K_REG_A0 + (instruction & 7);
413 op->type = M68K_OP_REG;
414 break;
415
416 case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
417 /* address register indirect */
418 op->address_mode = M68K_AM_REGI_ADDR;
419 op->reg = M68K_REG_A0 + (instruction & 7);
420 break;
421
422 case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f:
423 /* address register indirect with postincrement */
424 op->address_mode = M68K_AM_REGI_ADDR_POST_INC;
425 op->reg = M68K_REG_A0 + (instruction & 7);
426 break;
427
428 case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
429 /* address register indirect with predecrement */
430 op->address_mode = M68K_AM_REGI_ADDR_PRE_DEC;
431 op->reg = M68K_REG_A0 + (instruction & 7);
432 break;
433
434 case 0x28: case 0x29: case 0x2a: case 0x2b: case 0x2c: case 0x2d: case 0x2e: case 0x2f:
435 /* address register indirect with displacement*/
436 op->address_mode = M68K_AM_REGI_ADDR_DISP;
437 op->mem.base_reg = M68K_REG_A0 + (instruction & 7);
438 op->mem.disp = (int16_t)read_imm_16(info);
439 break;
440
441 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
442 /* address register indirect with index */
443 get_with_index_address_mode(info, op, instruction, size, false);
444 break;
445
446 case 0x38:
447 /* absolute short address */
448 op->address_mode = M68K_AM_ABSOLUTE_DATA_SHORT;
449 op->imm = read_imm_16(info);
450 break;
451
452 case 0x39:
453 /* absolute long address */
454 op->address_mode = M68K_AM_ABSOLUTE_DATA_LONG;
455 op->imm = read_imm_32(info);
456 break;
457
458 case 0x3a:
459 /* program counter with displacement */
460 op->address_mode = M68K_AM_PCI_DISP;
461 op->mem.disp = (int16_t)read_imm_16(info);
462 break;
463
464 case 0x3b:
465 /* program counter with index */
466 get_with_index_address_mode(info, op, instruction, size, true);
467 break;
468
469 case 0x3c:
470 op->address_mode = M68K_AM_IMMEDIATE;
471 op->type = M68K_OP_IMM;
472
473 if (size == 1)
474 op->imm = read_imm_8(info) & 0xff;
475 else if (size == 2)
476 op->imm = read_imm_16(info) & 0xffff;
477 else if (size == 4)
478 op->imm = read_imm_32(info);
479 else
480 op->imm = read_imm_64(info);
481
482 break;
483
484 default:
485 break;
486 }
487 }
488
489 static void set_insn_group(m68k_info *info, m68k_group_type group)
490 {
491 info->groups[info->groups_count++] = (uint8_t)group;
492 }
493
494 static cs_m68k* build_init_op(m68k_info *info, int opcode, int count, int size)
495 {
496 cs_m68k* ext;
497
498 MCInst_setOpcode(info->inst, opcode);
499
500 ext = &info->extension;
501
502 ext->op_count = (uint8_t)count;
503 ext->op_size.type = M68K_SIZE_TYPE_CPU;
504 ext->op_size.cpu_size = size;
505
506 return ext;
507 }
508
509 static void build_re_gen_1(m68k_info *info, bool isDreg, int opcode, uint8_t size)
510 {
511 cs_m68k_op* op0;
512 cs_m68k_op* op1;
513 cs_m68k* ext = build_init_op(info, opcode, 2, size);
514
515 op0 = &ext->operands[0];
516 op1 = &ext->operands[1];
517
518 if (isDreg) {
519 op0->address_mode = M68K_AM_REG_DIRECT_DATA;
520 op0->reg = M68K_REG_D0 + ((info->ir >> 9 ) & 7);
521 } else {
522 op0->address_mode = M68K_AM_REG_DIRECT_ADDR;
523 op0->reg = M68K_REG_A0 + ((info->ir >> 9 ) & 7);
524 }
525
526 get_ea_mode_op(info, op1, info->ir, size);
527 }
528
529 static void build_re_1(m68k_info *info, int opcode, uint8_t size)
530 {
531 build_re_gen_1(info, true, opcode, size);
532 }
533
534 static void build_er_gen_1(m68k_info *info, bool isDreg, int opcode, uint8_t size)
535 {
536 cs_m68k_op* op0;
537 cs_m68k_op* op1;
538 cs_m68k* ext = build_init_op(info, opcode, 2, size);
539
540 op0 = &ext->operands[0];
541 op1 = &ext->operands[1];
542
543 get_ea_mode_op(info, op0, info->ir, size);
544
545 if (isDreg) {
546 op1->address_mode = M68K_AM_REG_DIRECT_DATA;
547 op1->reg = M68K_REG_D0 + ((info->ir >> 9) & 7);
548 } else {
549 op1->address_mode = M68K_AM_REG_DIRECT_ADDR;
550 op1->reg = M68K_REG_A0 + ((info->ir >> 9) & 7);
551 }
552 }
553
554 static void build_rr(m68k_info *info, int opcode, uint8_t size, int imm)
555 {
556 cs_m68k_op* op0;
557 cs_m68k_op* op1;
558 cs_m68k_op* op2;
559 cs_m68k* ext = build_init_op(info, opcode, 2, size);
560
561 op0 = &ext->operands[0];
562 op1 = &ext->operands[1];
563 op2 = &ext->operands[2];
564
565 op0->address_mode = M68K_AM_REG_DIRECT_DATA;
566 op0->reg = M68K_REG_D0 + (info->ir & 7);
567
568 op1->address_mode = M68K_AM_REG_DIRECT_DATA;
569 op1->reg = M68K_REG_D0 + ((info->ir >> 9) & 7);
570
571 if (imm > 0) {
572 ext->op_count = 3;
573 op2->type = M68K_OP_IMM;
574 op2->address_mode = M68K_AM_IMMEDIATE;
575 op2->imm = imm;
576 }
577 }
578
579 static void build_r(m68k_info *info, int opcode, uint8_t size)
580 {
581 cs_m68k_op* op0;
582 cs_m68k_op* op1;
583 cs_m68k* ext = build_init_op(info, opcode, 2, size);
584
585 op0 = &ext->operands[0];
586 op1 = &ext->operands[1];
587
588 op0->address_mode = M68K_AM_REG_DIRECT_DATA;
589 op0->reg = M68K_REG_D0 + ((info->ir >> 9) & 7);
590
591 op1->address_mode = M68K_AM_REG_DIRECT_DATA;
592 op1->reg = M68K_REG_D0 + (info->ir & 7);
593 }
594
595 static void build_imm_ea(m68k_info *info, int opcode, uint8_t size, int imm)
596 {
597 cs_m68k_op* op0;
598 cs_m68k_op* op1;
599 cs_m68k* ext = build_init_op(info, opcode, 2, size);
600
601 op0 = &ext->operands[0];
602 op1 = &ext->operands[1];
603
604 op0->type = M68K_OP_IMM;
605 op0->address_mode = M68K_AM_IMMEDIATE;
606 op0->imm = imm;
607
608 get_ea_mode_op(info, op1, info->ir, size);
609 }
610
611 static void build_3bit_d(m68k_info *info, int opcode, int size)
612 {
613 cs_m68k_op* op0;
614 cs_m68k_op* op1;
615 cs_m68k* ext = build_init_op(info, opcode, 2, size);
616
617 op0 = &ext->operands[0];
618 op1 = &ext->operands[1];
619
620 op0->type = M68K_OP_IMM;
621 op0->address_mode = M68K_AM_IMMEDIATE;
622 op0->imm = g_3bit_qdata_table[(info->ir >> 9) & 7];
623
624 op1->address_mode = M68K_AM_REG_DIRECT_DATA;
625 op1->reg = M68K_REG_D0 + (info->ir & 7);
626 }
627
628 static void build_3bit_ea(m68k_info *info, int opcode, int size)
629 {
630 cs_m68k_op* op0;
631 cs_m68k_op* op1;
632 cs_m68k* ext = build_init_op(info, opcode, 2, size);
633
634 op0 = &ext->operands[0];
635 op1 = &ext->operands[1];
636
637 op0->type = M68K_OP_IMM;
638 op0->address_mode = M68K_AM_IMMEDIATE;
639 op0->imm = g_3bit_qdata_table[(info->ir >> 9) & 7];
640
641 get_ea_mode_op(info, op1, info->ir, size);
642 }
643
644 static void build_mm(m68k_info *info, int opcode, uint8_t size, int imm)
645 {
646 cs_m68k_op* op0;
647 cs_m68k_op* op1;
648 cs_m68k_op* op2;
649 cs_m68k* ext = build_init_op(info, opcode, 2, size);
650
651 op0 = &ext->operands[0];
652 op1 = &ext->operands[1];
653 op2 = &ext->operands[2];
654
655 op0->address_mode = M68K_AM_REGI_ADDR_PRE_DEC;
656 op0->reg = M68K_REG_A0 + (info->ir & 7);
657
658 op1->address_mode = M68K_AM_REGI_ADDR_PRE_DEC;
659 op1->reg = M68K_REG_A0 + ((info->ir >> 9) & 7);
660
661 if (imm > 0) {
662 ext->op_count = 3;
663 op2->type = M68K_OP_IMM;
664 op2->address_mode = M68K_AM_IMMEDIATE;
665 op2->imm = imm;
666 }
667 }
668
669 static void build_ea(m68k_info *info, int opcode, uint8_t size)
670 {
671 cs_m68k* ext = build_init_op(info, opcode, 1, size);
672 get_ea_mode_op(info, &ext->operands[0], info->ir, size);
673 }
674
675 static void build_ea_a(m68k_info *info, int opcode, uint8_t size)
676 {
677 cs_m68k_op* op0;
678 cs_m68k_op* op1;
679 cs_m68k* ext = build_init_op(info, opcode, 2, size);
680
681 op0 = &ext->operands[0];
682 op1 = &ext->operands[1];
683
684 get_ea_mode_op(info, op0, info->ir, size);
685
686 op1->address_mode = M68K_AM_REG_DIRECT_ADDR;
687 op1->reg = M68K_REG_A0 + ((info->ir >> 9) & 7);
688 }
689
690 static void build_ea_ea(m68k_info *info, int opcode, int size)
691 {
692 cs_m68k_op* op0;
693 cs_m68k_op* op1;
694 cs_m68k* ext = build_init_op(info, opcode, 2, size);
695
696 op0 = &ext->operands[0];
697 op1 = &ext->operands[1];
698
699 get_ea_mode_op(info, op0, info->ir, size);
700 get_ea_mode_op(info, op1, (((info->ir>>9) & 7) | ((info->ir>>3) & 0x38)), size);
701 }
702
703 static void build_pi_pi(m68k_info *info, int opcode, int size)
704 {
705 cs_m68k_op* op0;
706 cs_m68k_op* op1;
707 cs_m68k* ext = build_init_op(info, opcode, 2, size);
708
709 op0 = &ext->operands[0];
710 op1 = &ext->operands[1];
711
712 op0->address_mode = M68K_AM_REGI_ADDR_POST_INC;
713 op0->reg = M68K_REG_A0 + (info->ir & 7);
714
715 op1->address_mode = M68K_AM_REGI_ADDR_POST_INC;
716 op1->reg = M68K_REG_A0 + ((info->ir >> 9) & 7);
717 }
718
719 static void build_imm_special_reg(m68k_info *info, int opcode, int imm, int size, m68k_reg reg)
720 {
721 cs_m68k_op* op0;
722 cs_m68k_op* op1;
723 cs_m68k* ext = build_init_op(info, opcode, 2, size);
724
725 op0 = &ext->operands[0];
726 op1 = &ext->operands[1];
727
728 op0->type = M68K_OP_IMM;
729 op0->address_mode = M68K_AM_IMMEDIATE;
730 op0->imm = imm;
731
732 op1->address_mode = M68K_AM_NONE;
733 op1->reg = reg;
734 }
735
736 static void build_relative_branch(m68k_info *info, int opcode, int size, int displacement)
737 {
738 cs_m68k_op* op;
739 cs_m68k* ext = build_init_op(info, opcode, 1, size);
740
741 op = &ext->operands[0];
742
743 op->type = M68K_OP_BR_DISP;
744 op->address_mode = M68K_AM_BRANCH_DISPLACEMENT;
745 op->br_disp.disp = displacement;
746 op->br_disp.disp_size = size;
747
748 set_insn_group(info, M68K_GRP_JUMP);
749 set_insn_group(info, M68K_GRP_BRANCH_RELATIVE);
750 }
751
752 static void build_absolute_jump_with_immediate(m68k_info *info, int opcode, int size, int immediate)
753 {
754 cs_m68k_op* op;
755 cs_m68k* ext = build_init_op(info, opcode, 1, size);
756
757 op = &ext->operands[0];
758
759 op->type = M68K_OP_IMM;
760 op->address_mode = M68K_AM_IMMEDIATE;
761 op->imm = immediate;
762
763 set_insn_group(info, M68K_GRP_JUMP);
764 }
765
766 static void build_bcc(m68k_info *info, int size, int displacement)
767 {
768 build_relative_branch(info, s_branch_lut[(info->ir >> 8) & 0xf], size, displacement);
769 }
770
771 static void build_trap(m68k_info *info, int size, int immediate)
772 {
773 build_absolute_jump_with_immediate(info, s_trap_lut[(info->ir >> 8) & 0xf], size, immediate);
774 }
775
776 static void build_dbxx(m68k_info *info, int opcode, int size, int displacement)
777 {
778 cs_m68k_op* op0;
779 cs_m68k_op* op1;
780 cs_m68k* ext = build_init_op(info, opcode, 2, size);
781
782 op0 = &ext->operands[0];
783 op1 = &ext->operands[1];
784
785 op0->address_mode = M68K_AM_REG_DIRECT_DATA;
786 op0->reg = M68K_REG_D0 + (info->ir & 7);
787
788 op1->type = M68K_OP_BR_DISP;
789 op1->address_mode = M68K_AM_BRANCH_DISPLACEMENT;
790 op1->br_disp.disp = displacement;
791 op1->br_disp.disp_size = M68K_OP_BR_DISP_SIZE_LONG;
792
793 set_insn_group(info, M68K_GRP_JUMP);
794 set_insn_group(info, M68K_GRP_BRANCH_RELATIVE);
795 }
796
797 static void build_dbcc(m68k_info *info, int size, int displacement)
798 {
799 build_dbxx(info, s_dbcc_lut[(info->ir >> 8) & 0xf], size, displacement);
800 }
801
802 static void build_d_d_ea(m68k_info *info, int opcode, int size)
803 {
804 cs_m68k_op* op0;
805 cs_m68k_op* op1;
806 cs_m68k_op* op2;
807 uint32_t extension = read_imm_16(info);
808 cs_m68k* ext = build_init_op(info, opcode, 3, size);
809
810 op0 = &ext->operands[0];
811 op1 = &ext->operands[1];
812 op2 = &ext->operands[2];
813
814 op0->address_mode = M68K_AM_REG_DIRECT_DATA;
815 op0->reg = M68K_REG_D0 + (extension & 7);
816
817 op1->address_mode = M68K_AM_REG_DIRECT_DATA;
818 op1->reg = M68K_REG_D0 + ((extension >> 6) & 7);
819
820 get_ea_mode_op(info, op2, info->ir, size);
821 }
822
823 static void build_bitfield_ins(m68k_info *info, int opcode, int has_d_arg)
824 {
825 uint8_t offset;
826 uint8_t width;
827 cs_m68k_op* op_ea;
828 cs_m68k_op* op1;
829 cs_m68k* ext = build_init_op(info, opcode, 1, 0);
830 uint32_t extension = read_imm_16(info);
831
832 op_ea = &ext->operands[0];
833 op1 = &ext->operands[1];
834
835 if (BIT_B(extension))
836 offset = (extension >> 6) & 7;
837 else
838 offset = (extension >> 6) & 31;
839
840 if (BIT_5(extension))
841 width = extension & 7;
842 else
843 width = (uint8_t)g_5bit_data_table[extension & 31];
844
845 if (has_d_arg) {
846 ext->op_count = 2;
847 op1->address_mode = M68K_AM_REG_DIRECT_DATA;
848 op1->reg = M68K_REG_D0 + ((extension >> 12) & 7);
849 }
850
851 get_ea_mode_op(info, op_ea, info->ir, 1);
852
853 op_ea->mem.bitfield = 1;
854 op_ea->mem.width = width;
855 op_ea->mem.offset = offset;
856 }
857
858 static void build_d(m68k_info *info, int opcode, int size)
859 {
860 cs_m68k* ext = build_init_op(info, opcode, 1, size);
861 cs_m68k_op* op;
862
863 op = &ext->operands[0];
864
865 op->address_mode = M68K_AM_REG_DIRECT_DATA;
866 op->reg = M68K_REG_D0 + (info->ir & 7);
867 }
868
869 static uint16_t reverse_bits(uint32_t v)
870 {
871 uint32_t r = v; // r will be reversed bits of v; first get LSB of v
872 uint32_t s = 16 - 1; // extra shift needed at end
873
874 for (v >>= 1; v; v >>= 1) {
875 r <<= 1;
876 r |= v & 1;
877 s--;
878 }
879
880 return r <<= s; // shift when v's highest bits are zero
881 }
882
883 static uint8_t reverse_bits_8(uint32_t v)
884 {
885 uint32_t r = v; // r will be reversed bits of v; first get LSB of v
886 uint32_t s = 8 - 1; // extra shift needed at end
887
888 for (v >>= 1; v; v >>= 1) {
889 r <<= 1;
890 r |= v & 1;
891 s--;
892 }
893
894 return r <<= s; // shift when v's highest bits are zero
895 }
896
897
898 static void build_movem_re(m68k_info *info, int opcode, int size)
899 {
900 cs_m68k_op* op0;
901 cs_m68k_op* op1;
902 cs_m68k* ext = build_init_op(info, opcode, 2, size);
903
904 op0 = &ext->operands[0];
905 op1 = &ext->operands[1];
906
907 op0->type = M68K_OP_REG_BITS;
908 op0->register_bits = read_imm_16(info);
909
910 get_ea_mode_op(info, op1, info->ir, size);
911
912 if (op1->address_mode == M68K_AM_REGI_ADDR_PRE_DEC)
913 op0->register_bits = reverse_bits(op0->register_bits);
914 }
915
916 static void build_movem_er(m68k_info *info, int opcode, int size)
917 {
918 cs_m68k_op* op0;
919 cs_m68k_op* op1;
920 cs_m68k* ext = build_init_op(info, opcode, 2, size);
921
922 op0 = &ext->operands[0];
923 op1 = &ext->operands[1];
924
925 op1->type = M68K_OP_REG_BITS;
926 op1->register_bits = read_imm_16(info);
927
928 get_ea_mode_op(info, op0, info->ir, size);
929 }
930
931 static void build_imm(m68k_info *info, int opcode, int data)
932 {
933 cs_m68k_op* op;
934 cs_m68k* ext = build_init_op(info, opcode, 1, 0);
935
936 MCInst_setOpcode(info->inst, opcode);
937
938 op = &ext->operands[0];
939
940 op->type = M68K_OP_IMM;
941 op->address_mode = M68K_AM_IMMEDIATE;
942 op->imm = data;
943 }
944
945 static void build_illegal(m68k_info *info, int data)
946 {
947 build_imm(info, M68K_INS_ILLEGAL, data);
948 }
949
950 static void build_invalid(m68k_info *info, int data)
951 {
952 build_imm(info, M68K_INS_INVALID, data);
953 }
954
955 static void build_cas2(m68k_info *info, int size)
956 {
957 uint32_t word3;
958 uint32_t extension;
959 cs_m68k_op* op0;
960 cs_m68k_op* op1;
961 cs_m68k_op* op2;
962 cs_m68k* ext = build_init_op(info, M68K_INS_CAS2, 3, size);
963 int reg_0, reg_1;
964
965 /* cas2 is the only 3 words instruction, word2 and word3 have the same motif bits to check */
966 word3 = peek_imm_32(info) & 0xffff;
967 if (!instruction_is_valid(info, word3))
968 return;
969
970 op0 = &ext->operands[0];
971 op1 = &ext->operands[1];
972 op2 = &ext->operands[2];
973
974 extension = read_imm_32(info);
975
976 op0->address_mode = M68K_AM_NONE;
977 op0->type = M68K_OP_REG_PAIR;
978 op0->reg_pair.reg_0 = ((extension >> 16) & 7) + M68K_REG_D0;
979 op0->reg_pair.reg_1 = (extension & 7) + M68K_REG_D0;
980
981 op1->address_mode = M68K_AM_NONE;
982 op1->type = M68K_OP_REG_PAIR;
983 op1->reg_pair.reg_0 = ((extension >> 22) & 7) + M68K_REG_D0;
984 op1->reg_pair.reg_1 = ((extension >> 6) & 7) + M68K_REG_D0;
985
986 reg_0 = (extension >> 28) & 7;
987 reg_1 = (extension >> 12) & 7;
988
989 op2->address_mode = M68K_AM_NONE;
990 op2->type = M68K_OP_REG_PAIR;
991 op2->reg_pair.reg_0 = reg_0 + (BIT_1F(extension) ? 8 : 0) + M68K_REG_D0;
992 op2->reg_pair.reg_1 = reg_1 + (BIT_F(extension) ? 8 : 0) + M68K_REG_D0;
993 }
994
995 static void build_chk2_cmp2(m68k_info *info, int size)
996 {
997 cs_m68k_op* op0;
998 cs_m68k_op* op1;
999 cs_m68k* ext = build_init_op(info, M68K_INS_CHK2, 2, size);
1000
1001 uint32_t extension = read_imm_16(info);
1002
1003 if (BIT_B(extension))
1004 MCInst_setOpcode(info->inst, M68K_INS_CHK2);
1005 else
1006 MCInst_setOpcode(info->inst, M68K_INS_CMP2);
1007
1008 op0 = &ext->operands[0];
1009 op1 = &ext->operands[1];
1010
1011 get_ea_mode_op(info, op0, info->ir, size);
1012
1013 op1->address_mode = M68K_AM_NONE;
1014 op1->type = M68K_OP_REG;
1015 op1->reg = (BIT_F(extension) ? M68K_REG_A0 : M68K_REG_D0) + ((extension >> 12) & 7);
1016 }
1017
1018 static void build_move16(m68k_info *info, int data[2], int modes[2])
1019 {
1020 cs_m68k* ext = build_init_op(info, M68K_INS_MOVE16, 2, 0);
1021 int i;
1022
1023 for (i = 0; i < 2; ++i) {
1024 cs_m68k_op* op = &ext->operands[i];
1025 const int d = data[i];
1026 const int m = modes[i];
1027
1028 op->type = M68K_OP_MEM;
1029
1030 if (m == M68K_AM_REGI_ADDR_POST_INC || m == M68K_AM_REG_DIRECT_ADDR) {
1031 op->address_mode = m;
1032 op->reg = M68K_REG_A0 + d;
1033 } else {
1034 op->address_mode = m;
1035 op->imm = d;
1036 }
1037 }
1038 }
1039
1040 static void build_link(m68k_info *info, int disp, int size)
1041 {
1042 cs_m68k_op* op0;
1043 cs_m68k_op* op1;
1044 cs_m68k* ext = build_init_op(info, M68K_INS_LINK, 2, size);
1045
1046 op0 = &ext->operands[0];
1047 op1 = &ext->operands[1];
1048
1049 op0->address_mode = M68K_AM_NONE;
1050 op0->reg = M68K_REG_A0 + (info->ir & 7);
1051
1052 op1->address_mode = M68K_AM_IMMEDIATE;
1053 op1->type = M68K_OP_IMM;
1054 op1->imm = disp;
1055 }
1056
1057 static void build_cpush_cinv(m68k_info *info, int op_offset)
1058 {
1059 cs_m68k_op* op0;
1060 cs_m68k_op* op1;
1061 cs_m68k* ext = build_init_op(info, M68K_INS_INVALID, 2, 0);
1062
1063 switch ((info->ir >> 3) & 3) { // scope
1064 // Invalid
1065 case 0:
1066 d68000_invalid(info);
1067 return;
1068 // Line
1069 case 1:
1070 MCInst_setOpcode(info->inst, op_offset + 0);
1071 break;
1072 // Page
1073 case 2:
1074 MCInst_setOpcode(info->inst, op_offset + 1);
1075 break;
1076 // All
1077 case 3:
1078 ext->op_count = 1;
1079 MCInst_setOpcode(info->inst, op_offset + 2);
1080 break;
1081 }
1082
1083 op0 = &ext->operands[0];
1084 op1 = &ext->operands[1];
1085
1086 op0->address_mode = M68K_AM_IMMEDIATE;
1087 op0->type = M68K_OP_IMM;
1088 op0->imm = (info->ir >> 6) & 3;
1089
1090 op1->type = M68K_OP_MEM;
1091 op1->address_mode = M68K_AM_REG_DIRECT_ADDR;
1092 op1->imm = M68K_REG_A0 + (info->ir & 7);
1093 }
1094
1095 static void build_movep_re(m68k_info *info, int size)
1096 {
1097 cs_m68k_op* op0;
1098 cs_m68k_op* op1;
1099 cs_m68k* ext = build_init_op(info, M68K_INS_MOVEP, 2, size);
1100
1101 op0 = &ext->operands[0];
1102 op1 = &ext->operands[1];
1103
1104 op0->reg = M68K_REG_D0 + ((info->ir >> 9) & 7);
1105
1106 op1->address_mode = M68K_AM_REGI_ADDR_DISP;
1107 op1->type = M68K_OP_MEM;
1108 op1->mem.base_reg = M68K_REG_A0 + (info->ir & 7);
1109 op1->mem.disp = (int16_t)read_imm_16(info);
1110 }
1111
1112 static void build_movep_er(m68k_info *info, int size)
1113 {
1114 cs_m68k_op* op0;
1115 cs_m68k_op* op1;
1116 cs_m68k* ext = build_init_op(info, M68K_INS_MOVEP, 2, size);
1117
1118 op0 = &ext->operands[0];
1119 op1 = &ext->operands[1];
1120
1121 op0->address_mode = M68K_AM_REGI_ADDR_DISP;
1122 op0->type = M68K_OP_MEM;
1123 op0->mem.base_reg = M68K_REG_A0 + (info->ir & 7);
1124 op0->mem.disp = (int16_t)read_imm_16(info);
1125
1126 op1->reg = M68K_REG_D0 + ((info->ir >> 9) & 7);
1127 }
1128
1129 static void build_moves(m68k_info *info, int size)
1130 {
1131 cs_m68k_op* op0;
1132 cs_m68k_op* op1;
1133 cs_m68k* ext = build_init_op(info, M68K_INS_MOVES, 2, size);
1134 uint32_t extension = read_imm_16(info);
1135
1136 op0 = &ext->operands[0];
1137 op1 = &ext->operands[1];
1138
1139 if (BIT_B(extension)) {
1140 op0->reg = (BIT_F(extension) ? M68K_REG_A0 : M68K_REG_D0) + ((extension >> 12) & 7);
1141 get_ea_mode_op(info, op1, info->ir, size);
1142 } else {
1143 get_ea_mode_op(info, op0, info->ir, size);
1144 op1->reg = (BIT_F(extension) ? M68K_REG_A0 : M68K_REG_D0) + ((extension >> 12) & 7);
1145 }
1146 }
1147
1148 static void build_er_1(m68k_info *info, int opcode, uint8_t size)
1149 {
1150 build_er_gen_1(info, true, opcode, size);
1151 }
1152
1153 /* ======================================================================== */
1154 /* ========================= INSTRUCTION HANDLERS ========================= */
1155 /* ======================================================================== */
1156 /* Instruction handler function names follow this convention:
1157 *
1158 * d68000_NAME_EXTENSIONS(void)
1159 * where NAME is the name of the opcode it handles and EXTENSIONS are any
1160 * extensions for special instances of that opcode.
1161 *
1162 * Examples:
1163 * d68000_add_er_8(): add opcode, from effective address to register,
1164 * size = byte
1165 *
1166 * d68000_asr_s_8(): arithmetic shift right, static count, size = byte
1167 *
1168 *
1169 * Common extensions:
1170 * 8 : size = byte
1171 * 16 : size = word
1172 * 32 : size = long
1173 * rr : register to register
1174 * mm : memory to memory
1175 * r : register
1176 * s : static
1177 * er : effective address -> register
1178 * re : register -> effective address
1179 * ea : using effective address mode of operation
1180 * d : data register direct
1181 * a : address register direct
1182 * ai : address register indirect
1183 * pi : address register indirect with postincrement
1184 * pd : address register indirect with predecrement
1185 * di : address register indirect with displacement
1186 * ix : address register indirect with index
1187 * aw : absolute word
1188 * al : absolute long
1189 */
1190
1191
1192 static void d68000_invalid(m68k_info *info)
1193 {
1194 build_invalid(info, info->ir);
1195 }
1196
1197 static void d68000_illegal(m68k_info *info)
1198 {
1199 build_illegal(info, info->ir);
1200 }
1201
1202 static void d68000_1010(m68k_info *info)
1203 {
1204 build_invalid(info, info->ir);
1205 }
1206
1207 static void d68000_1111(m68k_info *info)
1208 {
1209 build_invalid(info, info->ir);
1210 }
1211
1212 static void d68000_abcd_rr(m68k_info *info)
1213 {
1214 build_rr(info, M68K_INS_ABCD, 1, 0);
1215 }
1216
1217 static void d68000_abcd_mm(m68k_info *info)
1218 {
1219 build_mm(info, M68K_INS_ABCD, 1, 0);
1220 }
1221
1222 static void d68000_add_er_8(m68k_info *info)
1223 {
1224 build_er_1(info, M68K_INS_ADD, 1);
1225 }
1226
1227 static void d68000_add_er_16(m68k_info *info)
1228 {
1229 build_er_1(info, M68K_INS_ADD, 2);
1230 }
1231
1232 static void d68000_add_er_32(m68k_info *info)
1233 {
1234 build_er_1(info, M68K_INS_ADD, 4);
1235 }
1236
1237 static void d68000_add_re_8(m68k_info *info)
1238 {
1239 build_re_1(info, M68K_INS_ADD, 1);
1240 }
1241
1242 static void d68000_add_re_16(m68k_info *info)
1243 {
1244 build_re_1(info, M68K_INS_ADD, 2);
1245 }
1246
1247 static void d68000_add_re_32(m68k_info *info)
1248 {
1249 build_re_1(info, M68K_INS_ADD, 4);
1250 }
1251
1252 static void d68000_adda_16(m68k_info *info)
1253 {
1254 build_ea_a(info, M68K_INS_ADDA, 2);
1255 }
1256
1257 static void d68000_adda_32(m68k_info *info)
1258 {
1259 build_ea_a(info, M68K_INS_ADDA, 4);
1260 }
1261
1262 static void d68000_addi_8(m68k_info *info)
1263 {
1264 build_imm_ea(info, M68K_INS_ADDI, 1, read_imm_8(info));
1265 }
1266
1267 static void d68000_addi_16(m68k_info *info)
1268 {
1269 build_imm_ea(info, M68K_INS_ADDI, 2, read_imm_16(info));
1270 }
1271
1272 static void d68000_addi_32(m68k_info *info)
1273 {
1274 build_imm_ea(info, M68K_INS_ADDI, 4, read_imm_32(info));
1275 }
1276
1277 static void d68000_addq_8(m68k_info *info)
1278 {
1279 build_3bit_ea(info, M68K_INS_ADDQ, 1);
1280 }
1281
1282 static void d68000_addq_16(m68k_info *info)
1283 {
1284 build_3bit_ea(info, M68K_INS_ADDQ, 2);
1285 }
1286
1287 static void d68000_addq_32(m68k_info *info)
1288 {
1289 build_3bit_ea(info, M68K_INS_ADDQ, 4);
1290 }
1291
1292 static void d68000_addx_rr_8(m68k_info *info)
1293 {
1294 build_rr(info, M68K_INS_ADDX, 1, 0);
1295 }
1296
1297 static void d68000_addx_rr_16(m68k_info *info)
1298 {
1299 build_rr(info, M68K_INS_ADDX, 2, 0);
1300 }
1301
1302 static void d68000_addx_rr_32(m68k_info *info)
1303 {
1304 build_rr(info, M68K_INS_ADDX, 4, 0);
1305 }
1306
1307 static void d68000_addx_mm_8(m68k_info *info)
1308 {
1309 build_mm(info, M68K_INS_ADDX, 1, 0);
1310 }
1311
1312 static void d68000_addx_mm_16(m68k_info *info)
1313 {
1314 build_mm(info, M68K_INS_ADDX, 2, 0);
1315 }
1316
1317 static void d68000_addx_mm_32(m68k_info *info)
1318 {
1319 build_mm(info, M68K_INS_ADDX, 4, 0);
1320 }
1321
1322 static void d68000_and_er_8(m68k_info *info)
1323 {
1324 build_er_1(info, M68K_INS_AND, 1);
1325 }
1326
1327 static void d68000_and_er_16(m68k_info *info)
1328 {
1329 build_er_1(info, M68K_INS_AND, 2);
1330 }
1331
1332 static void d68000_and_er_32(m68k_info *info)
1333 {
1334 build_er_1(info, M68K_INS_AND, 4);
1335 }
1336
1337 static void d68000_and_re_8(m68k_info *info)
1338 {
1339 build_re_1(info, M68K_INS_AND, 1);
1340 }
1341
1342 static void d68000_and_re_16(m68k_info *info)
1343 {
1344 build_re_1(info, M68K_INS_AND, 2);
1345 }
1346
1347 static void d68000_and_re_32(m68k_info *info)
1348 {
1349 build_re_1(info, M68K_INS_AND, 4);
1350 }
1351
1352 static void d68000_andi_8(m68k_info *info)
1353 {
1354 build_imm_ea(info, M68K_INS_ANDI, 1, read_imm_8(info));
1355 }
1356
1357 static void d68000_andi_16(m68k_info *info)
1358 {
1359 build_imm_ea(info, M68K_INS_ANDI, 2, read_imm_16(info));
1360 }
1361
1362 static void d68000_andi_32(m68k_info *info)
1363 {
1364 build_imm_ea(info, M68K_INS_ANDI, 4, read_imm_32(info));
1365 }
1366
1367 static void d68000_andi_to_ccr(m68k_info *info)
1368 {
1369 build_imm_special_reg(info, M68K_INS_ANDI, read_imm_8(info), 1, M68K_REG_CCR);
1370 }
1371
1372 static void d68000_andi_to_sr(m68k_info *info)
1373 {
1374 build_imm_special_reg(info, M68K_INS_ANDI, read_imm_16(info), 2, M68K_REG_SR);
1375 }
1376
1377 static void d68000_asr_s_8(m68k_info *info)
1378 {
1379 build_3bit_d(info, M68K_INS_ASR, 1);
1380 }
1381
1382 static void d68000_asr_s_16(m68k_info *info)
1383 {
1384 build_3bit_d(info, M68K_INS_ASR, 2);
1385 }
1386
1387 static void d68000_asr_s_32(m68k_info *info)
1388 {
1389 build_3bit_d(info, M68K_INS_ASR, 4);
1390 }
1391
1392 static void d68000_asr_r_8(m68k_info *info)
1393 {
1394 build_r(info, M68K_INS_ASR, 1);
1395 }
1396
1397 static void d68000_asr_r_16(m68k_info *info)
1398 {
1399 build_r(info, M68K_INS_ASR, 2);
1400 }
1401
1402 static void d68000_asr_r_32(m68k_info *info)
1403 {
1404 build_r(info, M68K_INS_ASR, 4);
1405 }
1406
1407 static void d68000_asr_ea(m68k_info *info)
1408 {
1409 build_ea(info, M68K_INS_ASR, 2);
1410 }
1411
1412 static void d68000_asl_s_8(m68k_info *info)
1413 {
1414 build_3bit_d(info, M68K_INS_ASL, 1);
1415 }
1416
1417 static void d68000_asl_s_16(m68k_info *info)
1418 {
1419 build_3bit_d(info, M68K_INS_ASL, 2);
1420 }
1421
1422 static void d68000_asl_s_32(m68k_info *info)
1423 {
1424 build_3bit_d(info, M68K_INS_ASL, 4);
1425 }
1426
1427 static void d68000_asl_r_8(m68k_info *info)
1428 {
1429 build_r(info, M68K_INS_ASL, 1);
1430 }
1431
1432 static void d68000_asl_r_16(m68k_info *info)
1433 {
1434 build_r(info, M68K_INS_ASL, 2);
1435 }
1436
1437 static void d68000_asl_r_32(m68k_info *info)
1438 {
1439 build_r(info, M68K_INS_ASL, 4);
1440 }
1441
1442 static void d68000_asl_ea(m68k_info *info)
1443 {
1444 build_ea(info, M68K_INS_ASL, 2);
1445 }
1446
1447 static void d68000_bcc_8(m68k_info *info)
1448 {
1449 build_bcc(info, 1, make_int_8(info->ir));
1450 }
1451
1452 static void d68000_bcc_16(m68k_info *info)
1453 {
1454 build_bcc(info, 2, make_int_16(read_imm_16(info)));
1455 }
1456
1457 static void d68020_bcc_32(m68k_info *info)
1458 {
1459 LIMIT_CPU_TYPES(info, M68020_PLUS);
1460 build_bcc(info, 4, read_imm_32(info));
1461 }
1462
1463 static void d68000_bchg_r(m68k_info *info)
1464 {
1465 build_re_1(info, M68K_INS_BCHG, 1);
1466 }
1467
1468 static void d68000_bchg_s(m68k_info *info)
1469 {
1470 build_imm_ea(info, M68K_INS_BCHG, 1, read_imm_8(info));
1471 }
1472
1473 static void d68000_bclr_r(m68k_info *info)
1474 {
1475 build_re_1(info, M68K_INS_BCLR, 1);
1476 }
1477
1478 static void d68000_bclr_s(m68k_info *info)
1479 {
1480 build_imm_ea(info, M68K_INS_BCLR, 1, read_imm_8(info));
1481 }
1482
1483 static void d68010_bkpt(m68k_info *info)
1484 {
1485 LIMIT_CPU_TYPES(info, M68010_PLUS);
1486 build_absolute_jump_with_immediate(info, M68K_INS_BKPT, 0, info->ir & 7);
1487 }
1488
1489 static void d68020_bfchg(m68k_info *info)
1490 {
1491 LIMIT_CPU_TYPES(info, M68020_PLUS);
1492 build_bitfield_ins(info, M68K_INS_BFCHG, false);
1493 }
1494
1495
1496 static void d68020_bfclr(m68k_info *info)
1497 {
1498 LIMIT_CPU_TYPES(info, M68020_PLUS);
1499 build_bitfield_ins(info, M68K_INS_BFCLR, false);
1500 }
1501
1502 static void d68020_bfexts(m68k_info *info)
1503 {
1504 LIMIT_CPU_TYPES(info, M68020_PLUS);
1505 build_bitfield_ins(info, M68K_INS_BFEXTS, true);
1506 }
1507
1508 static void d68020_bfextu(m68k_info *info)
1509 {
1510 LIMIT_CPU_TYPES(info, M68020_PLUS);
1511 build_bitfield_ins(info, M68K_INS_BFEXTU, true);
1512 }
1513
1514 static void d68020_bfffo(m68k_info *info)
1515 {
1516 LIMIT_CPU_TYPES(info, M68020_PLUS);
1517 build_bitfield_ins(info, M68K_INS_BFFFO, true);
1518 }
1519
1520 static void d68020_bfins(m68k_info *info)
1521 {
1522 cs_m68k* ext = &info->extension;
1523 cs_m68k_op temp;
1524
1525 LIMIT_CPU_TYPES(info, M68020_PLUS);
1526 build_bitfield_ins(info, M68K_INS_BFINS, true);
1527
1528 // a bit hacky but we need to flip the args on only this instruction
1529
1530 temp = ext->operands[0];
1531 ext->operands[0] = ext->operands[1];
1532 ext->operands[1] = temp;
1533 }
1534
1535 static void d68020_bfset(m68k_info *info)
1536 {
1537 LIMIT_CPU_TYPES(info, M68020_PLUS);
1538 build_bitfield_ins(info, M68K_INS_BFSET, false);
1539 }
1540
1541 static void d68020_bftst(m68k_info *info)
1542 {
1543 build_bitfield_ins(info, M68K_INS_BFTST, false);
1544 }
1545
1546 static void d68000_bra_8(m68k_info *info)
1547 {
1548 build_relative_branch(info, M68K_INS_BRA, 1, make_int_8(info->ir));
1549 }
1550
1551 static void d68000_bra_16(m68k_info *info)
1552 {
1553 build_relative_branch(info, M68K_INS_BRA, 2, make_int_16(read_imm_16(info)));
1554 }
1555
1556 static void d68020_bra_32(m68k_info *info)
1557 {
1558 LIMIT_CPU_TYPES(info, M68020_PLUS);
1559 build_relative_branch(info, M68K_INS_BRA, 4, read_imm_32(info));
1560 }
1561
1562 static void d68000_bset_r(m68k_info *info)
1563 {
1564 build_re_1(info, M68K_INS_BSET, 1);
1565 }
1566
1567 static void d68000_bset_s(m68k_info *info)
1568 {
1569 build_imm_ea(info, M68K_INS_BSET, 1, read_imm_8(info));
1570 }
1571
1572 static void d68000_bsr_8(m68k_info *info)
1573 {
1574 build_relative_branch(info, M68K_INS_BSR, 1, make_int_8(info->ir));
1575 }
1576
1577 static void d68000_bsr_16(m68k_info *info)
1578 {
1579 build_relative_branch(info, M68K_INS_BSR, 2, make_int_16(read_imm_16(info)));
1580 }
1581
1582 static void d68020_bsr_32(m68k_info *info)
1583 {
1584 LIMIT_CPU_TYPES(info, M68020_PLUS);
1585 build_relative_branch(info, M68K_INS_BSR, 4, read_imm_32(info));
1586 }
1587
1588 static void d68000_btst_r(m68k_info *info)
1589 {
1590 build_re_1(info, M68K_INS_BTST, 4);
1591 }
1592
1593 static void d68000_btst_s(m68k_info *info)
1594 {
1595 build_imm_ea(info, M68K_INS_BTST, 1, read_imm_8(info));
1596 }
1597
1598 static void d68020_callm(m68k_info *info)
1599 {
1600 LIMIT_CPU_TYPES(info, M68020_ONLY);
1601 build_imm_ea(info, M68K_INS_CALLM, 0, read_imm_8(info));
1602 }
1603
1604 static void d68020_cas_8(m68k_info *info)
1605 {
1606 LIMIT_CPU_TYPES(info, M68020_PLUS);
1607 build_d_d_ea(info, M68K_INS_CAS, 1);
1608 }
1609
1610 static void d68020_cas_16(m68k_info *info)
1611 {
1612 LIMIT_CPU_TYPES(info, M68020_PLUS);
1613 build_d_d_ea(info, M68K_INS_CAS, 2);
1614 }
1615
1616 static void d68020_cas_32(m68k_info *info)
1617 {
1618 LIMIT_CPU_TYPES(info, M68020_PLUS);
1619 build_d_d_ea(info, M68K_INS_CAS, 4);
1620 }
1621
1622 static void d68020_cas2_16(m68k_info *info)
1623 {
1624 build_cas2(info, 2);
1625 }
1626
1627 static void d68020_cas2_32(m68k_info *info)
1628 {
1629 build_cas2(info, 4);
1630 }
1631
1632 static void d68000_chk_16(m68k_info *info)
1633 {
1634 build_er_1(info, M68K_INS_CHK, 2);
1635 }
1636
1637 static void d68020_chk_32(m68k_info *info)
1638 {
1639 LIMIT_CPU_TYPES(info, M68020_PLUS);
1640 build_er_1(info, M68K_INS_CHK, 4);
1641 }
1642
1643 static void d68020_chk2_cmp2_8(m68k_info *info)
1644 {
1645 LIMIT_CPU_TYPES(info, M68020_PLUS);
1646 build_chk2_cmp2(info, 1);
1647 }
1648
1649 static void d68020_chk2_cmp2_16(m68k_info *info)
1650 {
1651 LIMIT_CPU_TYPES(info, M68020_PLUS);
1652 build_chk2_cmp2(info, 2);
1653 }
1654
1655 static void d68020_chk2_cmp2_32(m68k_info *info)
1656 {
1657 LIMIT_CPU_TYPES(info, M68020_PLUS);
1658 build_chk2_cmp2(info, 4);
1659 }
1660
1661 static void d68040_cinv(m68k_info *info)
1662 {
1663 LIMIT_CPU_TYPES(info, M68040_PLUS);
1664 build_cpush_cinv(info, M68K_INS_CINVL);
1665 }
1666
1667 static void d68000_clr_8(m68k_info *info)
1668 {
1669 build_ea(info, M68K_INS_CLR, 1);
1670 }
1671
1672 static void d68000_clr_16(m68k_info *info)
1673 {
1674 build_ea(info, M68K_INS_CLR, 2);
1675 }
1676
1677 static void d68000_clr_32(m68k_info *info)
1678 {
1679 build_ea(info, M68K_INS_CLR, 4);
1680 }
1681
1682 static void d68000_cmp_8(m68k_info *info)
1683 {
1684 build_er_1(info, M68K_INS_CMP, 1);
1685 }
1686
1687 static void d68000_cmp_16(m68k_info *info)
1688 {
1689 build_er_1(info, M68K_INS_CMP, 2);
1690 }
1691
1692 static void d68000_cmp_32(m68k_info *info)
1693 {
1694 build_er_1(info, M68K_INS_CMP, 4);
1695 }
1696
1697 static void d68000_cmpa_16(m68k_info *info)
1698 {
1699 build_ea_a(info, M68K_INS_CMPA, 2);
1700 }
1701
1702 static void d68000_cmpa_32(m68k_info *info)
1703 {
1704 build_ea_a(info, M68K_INS_CMPA, 4);
1705 }
1706
1707 static void d68000_cmpi_8(m68k_info *info)
1708 {
1709 build_imm_ea(info, M68K_INS_CMPI, 1, read_imm_8(info));
1710 }
1711
1712 static void d68020_cmpi_pcdi_8(m68k_info *info)
1713 {
1714 LIMIT_CPU_TYPES(info, M68010_PLUS);
1715 build_imm_ea(info, M68K_INS_CMPI, 1, read_imm_8(info));
1716 }
1717
1718 static void d68020_cmpi_pcix_8(m68k_info *info)
1719 {
1720 LIMIT_CPU_TYPES(info, M68010_PLUS);
1721 build_imm_ea(info, M68K_INS_CMPI, 1, read_imm_8(info));
1722 }
1723
1724 static void d68000_cmpi_16(m68k_info *info)
1725 {
1726 build_imm_ea(info, M68K_INS_CMPI, 2, read_imm_16(info));
1727 }
1728
1729 static void d68020_cmpi_pcdi_16(m68k_info *info)
1730 {
1731 LIMIT_CPU_TYPES(info, M68010_PLUS);
1732 build_imm_ea(info, M68K_INS_CMPI, 2, read_imm_16(info));
1733 }
1734
1735 static void d68020_cmpi_pcix_16(m68k_info *info)
1736 {
1737 LIMIT_CPU_TYPES(info, M68010_PLUS);
1738 build_imm_ea(info, M68K_INS_CMPI, 2, read_imm_16(info));
1739 }
1740
1741 static void d68000_cmpi_32(m68k_info *info)
1742 {
1743 build_imm_ea(info, M68K_INS_CMPI, 4, read_imm_32(info));
1744 }
1745
1746 static void d68020_cmpi_pcdi_32(m68k_info *info)
1747 {
1748 LIMIT_CPU_TYPES(info, M68010_PLUS);
1749 build_imm_ea(info, M68K_INS_CMPI, 4, read_imm_32(info));
1750 }
1751
1752 static void d68020_cmpi_pcix_32(m68k_info *info)
1753 {
1754 LIMIT_CPU_TYPES(info, M68010_PLUS);
1755 build_imm_ea(info, M68K_INS_CMPI, 4, read_imm_32(info));
1756 }
1757
1758 static void d68000_cmpm_8(m68k_info *info)
1759 {
1760 build_pi_pi(info, M68K_INS_CMPM, 1);
1761 }
1762
1763 static void d68000_cmpm_16(m68k_info *info)
1764 {
1765 build_pi_pi(info, M68K_INS_CMPM, 2);
1766 }
1767
1768 static void d68000_cmpm_32(m68k_info *info)
1769 {
1770 build_pi_pi(info, M68K_INS_CMPM, 4);
1771 }
1772
1773 static void make_cpbcc_operand(cs_m68k_op* op, int size, int displacement)
1774 {
1775 op->address_mode = M68K_AM_BRANCH_DISPLACEMENT;
1776 op->type = M68K_OP_BR_DISP;
1777 op->br_disp.disp = displacement;
1778 op->br_disp.disp_size = size;
1779 }
1780
1781 static void d68020_cpbcc_16(m68k_info *info)
1782 {
1783 cs_m68k_op* op0;
1784 cs_m68k* ext;
1785 LIMIT_CPU_TYPES(info, M68020_PLUS);
1786
1787 // FNOP is a special case of FBF
1788 if (info->ir == 0xf280 && peek_imm_16(info) == 0) {
1789 MCInst_setOpcode(info->inst, M68K_INS_FNOP);
1790 info->pc += 2;
1791 return;
1792 }
1793
1794 // these are all in row with the extension so just doing a add here is fine
1795 info->inst->Opcode += (info->ir & 0x2f);
1796
1797 ext = build_init_op(info, M68K_INS_FBF, 1, 2);
1798 op0 = &ext->operands[0];
1799
1800 make_cpbcc_operand(op0, M68K_OP_BR_DISP_SIZE_WORD, make_int_16(read_imm_16(info)));
1801
1802 set_insn_group(info, M68K_GRP_JUMP);
1803 set_insn_group(info, M68K_GRP_BRANCH_RELATIVE);
1804 }
1805
1806 static void d68020_cpbcc_32(m68k_info *info)
1807 {
1808 cs_m68k* ext;
1809 cs_m68k_op* op0;
1810
1811 LIMIT_CPU_TYPES(info, M68020_PLUS);
1812
1813 // these are all in row with the extension so just doing a add here is fine
1814 info->inst->Opcode += (info->ir & 0x2f);
1815
1816 ext = build_init_op(info, M68K_INS_FBF, 1, 4);
1817 op0 = &ext->operands[0];
1818
1819 make_cpbcc_operand(op0, M68K_OP_BR_DISP_SIZE_LONG, read_imm_32(info));
1820
1821 set_insn_group(info, M68K_GRP_JUMP);
1822 set_insn_group(info, M68K_GRP_BRANCH_RELATIVE);
1823 }
1824
1825 static void d68020_cpdbcc(m68k_info *info)
1826 {
1827 cs_m68k* ext;
1828 cs_m68k_op* op0;
1829 cs_m68k_op* op1;
1830 uint32_t ext1, ext2;
1831
1832 LIMIT_CPU_TYPES(info, M68020_PLUS);
1833
1834 ext1 = read_imm_16(info);
1835 ext2 = read_imm_16(info);
1836
1837 // these are all in row with the extension so just doing a add here is fine
1838 info->inst->Opcode += (ext1 & 0x2f);
1839
1840 ext = build_init_op(info, M68K_INS_FDBF, 2, 0);
1841 op0 = &ext->operands[0];
1842 op1 = &ext->operands[1];
1843
1844 op0->reg = M68K_REG_D0 + (info->ir & 7);
1845
1846 make_cpbcc_operand(op1, M68K_OP_BR_DISP_SIZE_WORD, make_int_16(ext2) + 2);
1847
1848 set_insn_group(info, M68K_GRP_JUMP);
1849 set_insn_group(info, M68K_GRP_BRANCH_RELATIVE);
1850 }
1851
1852 static void fmove_fpcr(m68k_info *info, uint32_t extension)
1853 {
1854 cs_m68k_op* special;
1855 cs_m68k_op* op_ea;
1856
1857 int regsel = (extension >> 10) & 0x7;
1858 int dir = (extension >> 13) & 0x1;
1859
1860 cs_m68k* ext = build_init_op(info, M68K_INS_FMOVE, 2, 4);
1861
1862 special = &ext->operands[0];
1863 op_ea = &ext->operands[1];
1864
1865 if (!dir) {
1866 cs_m68k_op* t = special;
1867 special = op_ea;
1868 op_ea = t;
1869 }
1870
1871 get_ea_mode_op(info, op_ea, info->ir, 4);
1872
1873 if (regsel & 4)
1874 special->reg = M68K_REG_FPCR;
1875 else if (regsel & 2)
1876 special->reg = M68K_REG_FPSR;
1877 else if (regsel & 1)
1878 special->reg = M68K_REG_FPIAR;
1879 }
1880
1881 static void fmovem(m68k_info *info, uint32_t extension)
1882 {
1883 cs_m68k_op* op_reglist;
1884 cs_m68k_op* op_ea;
1885 int dir = (extension >> 13) & 0x1;
1886 int mode = (extension >> 11) & 0x3;
1887 uint32_t reglist = extension & 0xff;
1888 cs_m68k* ext = build_init_op(info, M68K_INS_FMOVEM, 2, 0);
1889
1890 op_reglist = &ext->operands[0];
1891 op_ea = &ext->operands[1];
1892
1893 // flip args around
1894
1895 if (!dir) {
1896 cs_m68k_op* t = op_reglist;
1897 op_reglist = op_ea;
1898 op_ea = t;
1899 }
1900
1901 get_ea_mode_op(info, op_ea, info->ir, 0);
1902
1903 switch (mode) {
1904 case 1 : // Dynamic list in dn register
1905 op_reglist->reg = M68K_REG_D0 + ((reglist >> 4) & 7);
1906 break;
1907
1908 case 0 :
1909 op_reglist->address_mode = M68K_AM_NONE;
1910 op_reglist->type = M68K_OP_REG_BITS;
1911 op_reglist->register_bits = reglist << 16;
1912 break;
1913
1914 case 2 : // Static list
1915 op_reglist->address_mode = M68K_AM_NONE;
1916 op_reglist->type = M68K_OP_REG_BITS;
1917 op_reglist->register_bits = ((uint32_t)reverse_bits_8(reglist)) << 16;
1918 break;
1919 }
1920 }
1921
1922 static void d68020_cpgen(m68k_info *info)
1923 {
1924 cs_m68k *ext;
1925 cs_m68k_op* op0;
1926 cs_m68k_op* op1;
1927 bool supports_single_op;
1928 uint32_t next;
1929 int rm, src, dst, opmode;
1930
1931
1932 LIMIT_CPU_TYPES(info, M68020_PLUS);
1933
1934 supports_single_op = true;
1935
1936 next = read_imm_16(info);
1937
1938 rm = (next >> 14) & 0x1;
1939 src = (next >> 10) & 0x7;
1940 dst = (next >> 7) & 0x7;
1941 opmode = next & 0x3f;
1942
1943 // special handling for fmovecr
1944
1945 if (BITFIELD(info->ir, 5, 0) == 0 && BITFIELD(next, 15, 10) == 0x17) {
1946 cs_m68k_op* op0;
1947 cs_m68k_op* op1;
1948 cs_m68k* ext = build_init_op(info, M68K_INS_FMOVECR, 2, 0);
1949
1950 op0 = &ext->operands[0];
1951 op1 = &ext->operands[1];
1952
1953 op0->address_mode = M68K_AM_IMMEDIATE;
1954 op0->type = M68K_OP_IMM;
1955 op0->imm = next & 0x3f;
1956
1957 op1->reg = M68K_REG_FP0 + ((next >> 7) & 7);
1958
1959 return;
1960 }
1961
1962 // deal with extended move stuff
1963
1964 switch ((next >> 13) & 0x7) {
1965 // fmovem fpcr
1966 case 0x4: // FMOVEM ea, FPCR
1967 case 0x5: // FMOVEM FPCR, ea
1968 fmove_fpcr(info, next);
1969 return;
1970
1971 // fmovem list
1972 case 0x6:
1973 case 0x7:
1974 fmovem(info, next);
1975 return;
1976 }
1977
1978 // See comment bellow on why this is being done
1979
1980 if ((next >> 6) & 1)
1981 opmode &= ~4;
1982
1983 // special handling of some instructions here
1984
1985 switch (opmode) {
1986 case 0x00: MCInst_setOpcode(info->inst, M68K_INS_FMOVE); supports_single_op = false; break;
1987 case 0x01: MCInst_setOpcode(info->inst, M68K_INS_FINT); break;
1988 case 0x02: MCInst_setOpcode(info->inst, M68K_INS_FSINH); break;
1989 case 0x03: MCInst_setOpcode(info->inst, M68K_INS_FINTRZ); break;
1990 case 0x04: MCInst_setOpcode(info->inst, M68K_INS_FSQRT); break;
1991 case 0x06: MCInst_setOpcode(info->inst, M68K_INS_FLOGNP1); break;
1992 case 0x08: MCInst_setOpcode(info->inst, M68K_INS_FETOXM1); break;
1993 case 0x09: MCInst_setOpcode(info->inst, M68K_INS_FATANH); break;
1994 case 0x0a: MCInst_setOpcode(info->inst, M68K_INS_FATAN); break;
1995 case 0x0c: MCInst_setOpcode(info->inst, M68K_INS_FASIN); break;
1996 case 0x0d: MCInst_setOpcode(info->inst, M68K_INS_FATANH); break;
1997 case 0x0e: MCInst_setOpcode(info->inst, M68K_INS_FSIN); break;
1998 case 0x0f: MCInst_setOpcode(info->inst, M68K_INS_FTAN); break;
1999 case 0x10: MCInst_setOpcode(info->inst, M68K_INS_FETOX); break;
2000 case 0x11: MCInst_setOpcode(info->inst, M68K_INS_FTWOTOX); break;
2001 case 0x12: MCInst_setOpcode(info->inst, M68K_INS_FTENTOX); break;
2002 case 0x14: MCInst_setOpcode(info->inst, M68K_INS_FLOGN); break;
2003 case 0x15: MCInst_setOpcode(info->inst, M68K_INS_FLOG10); break;
2004 case 0x16: MCInst_setOpcode(info->inst, M68K_INS_FLOG2); break;
2005 case 0x18: MCInst_setOpcode(info->inst, M68K_INS_FABS); break;
2006 case 0x19: MCInst_setOpcode(info->inst, M68K_INS_FCOSH); break;
2007 case 0x1a: MCInst_setOpcode(info->inst, M68K_INS_FNEG); break;
2008 case 0x1c: MCInst_setOpcode(info->inst, M68K_INS_FACOS); break;
2009 case 0x1d: MCInst_setOpcode(info->inst, M68K_INS_FCOS); break;
2010 case 0x1e: MCInst_setOpcode(info->inst, M68K_INS_FGETEXP); break;
2011 case 0x1f: MCInst_setOpcode(info->inst, M68K_INS_FGETMAN); break;
2012 case 0x20: MCInst_setOpcode(info->inst, M68K_INS_FDIV); supports_single_op = false; break;
2013 case 0x21: MCInst_setOpcode(info->inst, M68K_INS_FMOD); supports_single_op = false; break;
2014 case 0x22: MCInst_setOpcode(info->inst, M68K_INS_FADD); supports_single_op = false; break;
2015 case 0x23: MCInst_setOpcode(info->inst, M68K_INS_FMUL); supports_single_op = false; break;
2016 case 0x24: MCInst_setOpcode(info->inst, M68K_INS_FSGLDIV); supports_single_op = false; break;
2017 case 0x25: MCInst_setOpcode(info->inst, M68K_INS_FREM); break;
2018 case 0x26: MCInst_setOpcode(info->inst, M68K_INS_FSCALE); break;
2019 case 0x27: MCInst_setOpcode(info->inst, M68K_INS_FSGLMUL); break;
2020 case 0x28: MCInst_setOpcode(info->inst, M68K_INS_FSUB); supports_single_op = false; break;
2021 case 0x38: MCInst_setOpcode(info->inst, M68K_INS_FCMP); supports_single_op = false; break;
2022 case 0x3a: MCInst_setOpcode(info->inst, M68K_INS_FTST); break;
2023 default:
2024 break;
2025 }
2026
2027 // Some trickery here! It's not documented but if bit 6 is set this is a s/d opcode and then
2028 // if bit 2 is set it's a d. As we already have set our opcode in the code above we can just
2029 // offset it as the following 2 op codes (if s/d is supported) will always be directly after it
2030
2031 if ((next >> 6) & 1) {
2032 if ((next >> 2) & 1)
2033 info->inst->Opcode += 2;
2034 else
2035 info->inst->Opcode += 1;
2036 }
2037
2038 ext = &info->extension;
2039
2040 ext->op_count = 2;
2041 ext->op_size.type = M68K_SIZE_TYPE_CPU;
2042 ext->op_size.cpu_size = 0;
2043
2044 // Special case - adjust direction of fmove
2045 if ((opmode == 0x00) && ((next >> 13) & 0x1) != 0) {
2046 op0 = &ext->operands[1];
2047 op1 = &ext->operands[0];
2048 } else {
2049 op0 = &ext->operands[0];
2050 op1 = &ext->operands[1];
2051 }
2052
2053 if (rm == 0 && supports_single_op && src == dst) {
2054 ext->op_count = 1;
2055 op0->reg = M68K_REG_FP0 + dst;
2056 return;
2057 }
2058
2059 if (rm == 1) {
2060 switch (src) {
2061 case 0x00 :
2062 ext->op_size.cpu_size = M68K_CPU_SIZE_LONG;
2063 get_ea_mode_op(info, op0, info->ir, 4);
2064 break;
2065
2066 case 0x06 :
2067 ext->op_size.cpu_size = M68K_CPU_SIZE_BYTE;
2068 get_ea_mode_op(info, op0, info->ir, 1);
2069 break;
2070
2071 case 0x04 :
2072 ext->op_size.cpu_size = M68K_CPU_SIZE_WORD;
2073 get_ea_mode_op(info, op0, info->ir, 2);
2074 break;
2075
2076 case 0x01 :
2077 ext->op_size.type = M68K_SIZE_TYPE_FPU;
2078 ext->op_size.fpu_size = M68K_FPU_SIZE_SINGLE;
2079 get_ea_mode_op(info, op0, info->ir, 4);
2080 op0->type = M68K_OP_FP_SINGLE;
2081 break;
2082
2083 case 0x05:
2084 ext->op_size.type = M68K_SIZE_TYPE_FPU;
2085 ext->op_size.fpu_size = M68K_FPU_SIZE_DOUBLE;
2086 get_ea_mode_op(info, op0, info->ir, 8);
2087 op0->type = M68K_OP_FP_DOUBLE;
2088 break;
2089
2090 default :
2091 ext->op_size.type = M68K_SIZE_TYPE_FPU;
2092 ext->op_size.fpu_size = M68K_FPU_SIZE_EXTENDED;
2093 break;
2094 }
2095 } else {
2096 op0->reg = M68K_REG_FP0 + src;
2097 }
2098
2099 op1->reg = M68K_REG_FP0 + dst;
2100 }
2101
2102 static void d68020_cprestore(m68k_info *info)
2103 {
2104 cs_m68k* ext;
2105 LIMIT_CPU_TYPES(info, M68020_PLUS);
2106
2107 ext = build_init_op(info, M68K_INS_FRESTORE, 1, 0);
2108 get_ea_mode_op(info, &ext->operands[0], info->ir, 1);
2109 }
2110
2111 static void d68020_cpsave(m68k_info *info)
2112 {
2113 cs_m68k* ext;
2114
2115 LIMIT_CPU_TYPES(info, M68020_PLUS);
2116
2117 ext = build_init_op(info, M68K_INS_FSAVE, 1, 0);
2118 get_ea_mode_op(info, &ext->operands[0], info->ir, 1);
2119 }
2120
2121 static void d68020_cpscc(m68k_info *info)
2122 {
2123 cs_m68k* ext;
2124
2125 LIMIT_CPU_TYPES(info, M68020_PLUS);
2126 ext = build_init_op(info, M68K_INS_FSF, 1, 1);
2127
2128 // these are all in row with the extension so just doing a add here is fine
2129 info->inst->Opcode += (read_imm_16(info) & 0x2f);
2130
2131 get_ea_mode_op(info, &ext->operands[0], info->ir, 1);
2132 }
2133
2134 static void d68020_cptrapcc_0(m68k_info *info)
2135 {
2136 uint32_t extension1;
2137 LIMIT_CPU_TYPES(info, M68020_PLUS);
2138
2139 extension1 = read_imm_16(info);
2140
2141 build_init_op(info, M68K_INS_FTRAPF, 0, 0);
2142
2143 // these are all in row with the extension so just doing a add here is fine
2144 info->inst->Opcode += (extension1 & 0x2f);
2145 }
2146
2147 static void d68020_cptrapcc_16(m68k_info *info)
2148 {
2149 uint32_t extension1, extension2;
2150 cs_m68k_op* op0;
2151 cs_m68k* ext;
2152
2153 LIMIT_CPU_TYPES(info, M68020_PLUS);
2154
2155 extension1 = read_imm_16(info);
2156 extension2 = read_imm_16(info);
2157
2158 ext = build_init_op(info, M68K_INS_FTRAPF, 1, 2);
2159
2160 // these are all in row with the extension so just doing a add here is fine
2161 info->inst->Opcode += (extension1 & 0x2f);
2162
2163 op0 = &ext->operands[0];
2164
2165 op0->address_mode = M68K_AM_IMMEDIATE;
2166 op0->type = M68K_OP_IMM;
2167 op0->imm = extension2;
2168 }
2169
2170 static void d68020_cptrapcc_32(m68k_info *info)
2171 {
2172 uint32_t extension1, extension2;
2173 cs_m68k* ext;
2174 cs_m68k_op* op0;
2175
2176 LIMIT_CPU_TYPES(info, M68020_PLUS);
2177
2178 extension1 = read_imm_16(info);
2179 extension2 = read_imm_32(info);
2180
2181 ext = build_init_op(info, M68K_INS_FTRAPF, 1, 2);
2182
2183 // these are all in row with the extension so just doing a add here is fine
2184 info->inst->Opcode += (extension1 & 0x2f);
2185
2186 op0 = &ext->operands[0];
2187
2188 op0->address_mode = M68K_AM_IMMEDIATE;
2189 op0->type = M68K_OP_IMM;
2190 op0->imm = extension2;
2191 }
2192
2193 static void d68040_cpush(m68k_info *info)
2194 {
2195 LIMIT_CPU_TYPES(info, M68040_PLUS);
2196 build_cpush_cinv(info, M68K_INS_CPUSHL);
2197 }
2198
2199 static void d68000_dbra(m68k_info *info)
2200 {
2201 build_dbxx(info, M68K_INS_DBRA, 0, make_int_16(read_imm_16(info)));
2202 }
2203
2204 static void d68000_dbcc(m68k_info *info)
2205 {
2206 build_dbcc(info, 0, make_int_16(read_imm_16(info)));
2207 }
2208
2209 static void d68000_divs(m68k_info *info)
2210 {
2211 build_er_1(info, M68K_INS_DIVS, 2);
2212 }
2213
2214 static void d68000_divu(m68k_info *info)
2215 {
2216 build_er_1(info, M68K_INS_DIVU, 2);
2217 }
2218
2219 static void d68020_divl(m68k_info *info)
2220 {
2221 uint32_t extension, insn_signed;
2222 cs_m68k* ext;
2223 cs_m68k_op* op0;
2224 cs_m68k_op* op1;
2225 uint32_t reg_0, reg_1;
2226
2227 LIMIT_CPU_TYPES(info, M68020_PLUS);
2228
2229 extension = read_imm_16(info);
2230 insn_signed = 0;
2231
2232 if (BIT_B((extension)))
2233 insn_signed = 1;
2234
2235 ext = build_init_op(info, insn_signed ? M68K_INS_DIVS : M68K_INS_DIVU, 2, 4);
2236
2237 op0 = &ext->operands[0];
2238 op1 = &ext->operands[1];
2239
2240 get_ea_mode_op(info, op0, info->ir, 4);
2241
2242 reg_0 = extension & 7;
2243 reg_1 = (extension >> 12) & 7;
2244
2245 op1->address_mode = M68K_AM_NONE;
2246 op1->type = M68K_OP_REG_PAIR;
2247 op1->reg_pair.reg_0 = reg_0 + M68K_REG_D0;
2248 op1->reg_pair.reg_1 = reg_1 + M68K_REG_D0;
2249
2250 if ((reg_0 == reg_1) || !BIT_A(extension)) {
2251 op1->type = M68K_OP_REG;
2252 op1->reg = M68K_REG_D0 + reg_1;
2253 }
2254 }
2255
2256 static void d68000_eor_8(m68k_info *info)
2257 {
2258 build_re_1(info, M68K_INS_EOR, 1);
2259 }
2260
2261 static void d68000_eor_16(m68k_info *info)
2262 {
2263 build_re_1(info, M68K_INS_EOR, 2);
2264 }
2265
2266 static void d68000_eor_32(m68k_info *info)
2267 {
2268 build_re_1(info, M68K_INS_EOR, 4);
2269 }
2270
2271 static void d68000_eori_8(m68k_info *info)
2272 {
2273 build_imm_ea(info, M68K_INS_EORI, 1, read_imm_8(info));
2274 }
2275
2276 static void d68000_eori_16(m68k_info *info)
2277 {
2278 build_imm_ea(info, M68K_INS_EORI, 2, read_imm_16(info));
2279 }
2280
2281 static void d68000_eori_32(m68k_info *info)
2282 {
2283 build_imm_ea(info, M68K_INS_EORI, 4, read_imm_32(info));
2284 }
2285
2286 static void d68000_eori_to_ccr(m68k_info *info)
2287 {
2288 build_imm_special_reg(info, M68K_INS_EORI, read_imm_8(info), 1, M68K_REG_CCR);
2289 }
2290
2291 static void d68000_eori_to_sr(m68k_info *info)
2292 {
2293 build_imm_special_reg(info, M68K_INS_EORI, read_imm_16(info), 2, M68K_REG_SR);
2294 }
2295
2296 static void d68000_exg_dd(m68k_info *info)
2297 {
2298 build_r(info, M68K_INS_EXG, 4);
2299 }
2300
2301 static void d68000_exg_aa(m68k_info *info)
2302 {
2303 cs_m68k_op* op0;
2304 cs_m68k_op* op1;
2305 cs_m68k* ext = build_init_op(info, M68K_INS_EXG, 2, 4);
2306
2307 op0 = &ext->operands[0];
2308 op1 = &ext->operands[1];
2309
2310 op0->address_mode = M68K_AM_NONE;
2311 op0->reg = M68K_REG_A0 + ((info->ir >> 9) & 7);
2312
2313 op1->address_mode = M68K_AM_NONE;
2314 op1->reg = M68K_REG_A0 + (info->ir & 7);
2315 }
2316
2317 static void d68000_exg_da(m68k_info *info)
2318 {
2319 cs_m68k_op* op0;
2320 cs_m68k_op* op1;
2321 cs_m68k* ext = build_init_op(info, M68K_INS_EXG, 2, 4);
2322
2323 op0 = &ext->operands[0];
2324 op1 = &ext->operands[1];
2325
2326 op0->address_mode = M68K_AM_NONE;
2327 op0->reg = M68K_REG_D0 + ((info->ir >> 9) & 7);
2328
2329 op1->address_mode = M68K_AM_NONE;
2330 op1->reg = M68K_REG_A0 + (info->ir & 7);
2331 }
2332
2333 static void d68000_ext_16(m68k_info *info)
2334 {
2335 build_d(info, M68K_INS_EXT, 2);
2336 }
2337
2338 static void d68000_ext_32(m68k_info *info)
2339 {
2340 build_d(info, M68K_INS_EXT, 4);
2341 }
2342
2343 static void d68020_extb_32(m68k_info *info)
2344 {
2345 LIMIT_CPU_TYPES(info, M68020_PLUS);
2346 build_d(info, M68K_INS_EXTB, 4);
2347 }
2348
2349 static void d68000_jmp(m68k_info *info)
2350 {
2351 cs_m68k* ext = build_init_op(info, M68K_INS_JMP, 1, 0);
2352 set_insn_group(info, M68K_GRP_JUMP);
2353 get_ea_mode_op(info, &ext->operands[0], info->ir, 4);
2354 }
2355
2356 static void d68000_jsr(m68k_info *info)
2357 {
2358 cs_m68k* ext = build_init_op(info, M68K_INS_JSR, 1, 0);
2359 set_insn_group(info, M68K_GRP_JUMP);
2360 get_ea_mode_op(info, &ext->operands[0], info->ir, 4);
2361 }
2362
2363 static void d68000_lea(m68k_info *info)
2364 {
2365 build_ea_a(info, M68K_INS_LEA, 4);
2366 }
2367
2368 static void d68000_link_16(m68k_info *info)
2369 {
2370 build_link(info, read_imm_16(info), 2);
2371 }
2372
2373 static void d68020_link_32(m68k_info *info)
2374 {
2375 LIMIT_CPU_TYPES(info, M68020_PLUS);
2376 build_link(info, read_imm_32(info), 4);
2377 }
2378
2379 static void d68000_lsr_s_8(m68k_info *info)
2380 {
2381 build_3bit_d(info, M68K_INS_LSR, 1);
2382 }
2383
2384 static void d68000_lsr_s_16(m68k_info *info)
2385 {
2386 build_3bit_d(info, M68K_INS_LSR, 2);
2387 }
2388
2389 static void d68000_lsr_s_32(m68k_info *info)
2390 {
2391 build_3bit_d(info, M68K_INS_LSR, 4);
2392 }
2393
2394 static void d68000_lsr_r_8(m68k_info *info)
2395 {
2396 build_r(info, M68K_INS_LSR, 1);
2397 }
2398
2399 static void d68000_lsr_r_16(m68k_info *info)
2400 {
2401 build_r(info, M68K_INS_LSR, 2);
2402 }
2403
2404 static void d68000_lsr_r_32(m68k_info *info)
2405 {
2406 build_r(info, M68K_INS_LSR, 4);
2407 }
2408
2409 static void d68000_lsr_ea(m68k_info *info)
2410 {
2411 build_ea(info, M68K_INS_LSR, 2);
2412 }
2413
2414 static void d68000_lsl_s_8(m68k_info *info)
2415 {
2416 build_3bit_d(info, M68K_INS_LSL, 1);
2417 }
2418
2419 static void d68000_lsl_s_16(m68k_info *info)
2420 {
2421 build_3bit_d(info, M68K_INS_LSL, 2);
2422 }
2423
2424 static void d68000_lsl_s_32(m68k_info *info)
2425 {
2426 build_3bit_d(info, M68K_INS_LSL, 4);
2427 }
2428
2429 static void d68000_lsl_r_8(m68k_info *info)
2430 {
2431 build_r(info, M68K_INS_LSL, 1);
2432 }
2433
2434 static void d68000_lsl_r_16(m68k_info *info)
2435 {
2436 build_r(info, M68K_INS_LSL, 2);
2437 }
2438
2439 static void d68000_lsl_r_32(m68k_info *info)
2440 {
2441 build_r(info, M68K_INS_LSL, 4);
2442 }
2443
2444 static void d68000_lsl_ea(m68k_info *info)
2445 {
2446 build_ea(info, M68K_INS_LSL, 2);
2447 }
2448
2449 static void d68000_move_8(m68k_info *info)
2450 {
2451 build_ea_ea(info, M68K_INS_MOVE, 1);
2452 }
2453
2454 static void d68000_move_16(m68k_info *info)
2455 {
2456 build_ea_ea(info, M68K_INS_MOVE, 2);
2457 }
2458
2459 static void d68000_move_32(m68k_info *info)
2460 {
2461 build_ea_ea(info, M68K_INS_MOVE, 4);
2462 }
2463
2464 static void d68000_movea_16(m68k_info *info)
2465 {
2466 build_ea_a(info, M68K_INS_MOVEA, 2);
2467 }
2468
2469 static void d68000_movea_32(m68k_info *info)
2470 {
2471 build_ea_a(info, M68K_INS_MOVEA, 4);
2472 }
2473
2474 static void d68000_move_to_ccr(m68k_info *info)
2475 {
2476 cs_m68k_op* op0;
2477 cs_m68k_op* op1;
2478 cs_m68k* ext = build_init_op(info, M68K_INS_MOVE, 2, 2);
2479
2480 op0 = &ext->operands[0];
2481 op1 = &ext->operands[1];
2482
2483 get_ea_mode_op(info, op0, info->ir, 1);
2484
2485 op1->address_mode = M68K_AM_NONE;
2486 op1->reg = M68K_REG_CCR;
2487 }
2488
2489 static void d68010_move_fr_ccr(m68k_info *info)
2490 {
2491 cs_m68k_op* op0;
2492 cs_m68k_op* op1;
2493 cs_m68k* ext;
2494
2495 LIMIT_CPU_TYPES(info, M68010_PLUS);
2496
2497 ext = build_init_op(info, M68K_INS_MOVE, 2, 2);
2498
2499 op0 = &ext->operands[0];
2500 op1 = &ext->operands[1];
2501
2502 op0->address_mode = M68K_AM_NONE;
2503 op0->reg = M68K_REG_CCR;
2504
2505 get_ea_mode_op(info, op1, info->ir, 1);
2506 }
2507
2508 static void d68000_move_fr_sr(m68k_info *info)
2509 {
2510 cs_m68k_op* op0;
2511 cs_m68k_op* op1;
2512 cs_m68k* ext = build_init_op(info, M68K_INS_MOVE, 2, 2);
2513
2514 op0 = &ext->operands[0];
2515 op1 = &ext->operands[1];
2516
2517 op0->address_mode = M68K_AM_NONE;
2518 op0->reg = M68K_REG_SR;
2519
2520 get_ea_mode_op(info, op1, info->ir, 2);
2521 }
2522
2523 static void d68000_move_to_sr(m68k_info *info)
2524 {
2525 cs_m68k_op* op0;
2526 cs_m68k_op* op1;
2527 cs_m68k* ext = build_init_op(info, M68K_INS_MOVE, 2, 2);
2528
2529 op0 = &ext->operands[0];
2530 op1 = &ext->operands[1];
2531
2532 get_ea_mode_op(info, op0, info->ir, 2);
2533
2534 op1->address_mode = M68K_AM_NONE;
2535 op1->reg = M68K_REG_SR;
2536 }
2537
2538 static void d68000_move_fr_usp(m68k_info *info)
2539 {
2540 cs_m68k_op* op0;
2541 cs_m68k_op* op1;
2542 cs_m68k* ext = build_init_op(info, M68K_INS_MOVE, 2, 0);
2543
2544 op0 = &ext->operands[0];
2545 op1 = &ext->operands[1];
2546
2547 op0->address_mode = M68K_AM_NONE;
2548 op0->reg = M68K_REG_USP;
2549
2550 op1->address_mode = M68K_AM_NONE;
2551 op1->reg = M68K_REG_A0 + (info->ir & 7);
2552 }
2553
2554 static void d68000_move_to_usp(m68k_info *info)
2555 {
2556 cs_m68k_op* op0;
2557 cs_m68k_op* op1;
2558 cs_m68k* ext = build_init_op(info, M68K_INS_MOVE, 2, 0);
2559
2560 op0 = &ext->operands[0];
2561 op1 = &ext->operands[1];
2562
2563 op0->address_mode = M68K_AM_NONE;
2564 op0->reg = M68K_REG_A0 + (info->ir & 7);
2565
2566 op1->address_mode = M68K_AM_NONE;
2567 op1->reg = M68K_REG_USP;
2568 }
2569
2570 static void d68010_movec(m68k_info *info)
2571 {
2572 uint32_t extension;
2573 m68k_reg reg;
2574 cs_m68k* ext;
2575 cs_m68k_op* op0;
2576 cs_m68k_op* op1;
2577
2578
2579 LIMIT_CPU_TYPES(info, M68010_PLUS);
2580
2581 extension = read_imm_16(info);
2582 reg = M68K_REG_INVALID;
2583
2584 ext = build_init_op(info, M68K_INS_MOVEC, 2, 0);
2585
2586 op0 = &ext->operands[0];
2587 op1 = &ext->operands[1];
2588
2589 switch (extension & 0xfff) {
2590 case 0x000: reg = M68K_REG_SFC; break;
2591 case 0x001: reg = M68K_REG_DFC; break;
2592 case 0x800: reg = M68K_REG_USP; break;
2593 case 0x801: reg = M68K_REG_VBR; break;
2594 case 0x002: reg = M68K_REG_CACR; break;
2595 case 0x802: reg = M68K_REG_CAAR; break;
2596 case 0x803: reg = M68K_REG_MSP; break;
2597 case 0x804: reg = M68K_REG_ISP; break;
2598 case 0x003: reg = M68K_REG_TC; break;
2599 case 0x004: reg = M68K_REG_ITT0; break;
2600 case 0x005: reg = M68K_REG_ITT1; break;
2601 case 0x006: reg = M68K_REG_DTT0; break;
2602 case 0x007: reg = M68K_REG_DTT1; break;
2603 case 0x805: reg = M68K_REG_MMUSR; break;
2604 case 0x806: reg = M68K_REG_URP; break;
2605 case 0x807: reg = M68K_REG_SRP; break;
2606 }
2607
2608 if (BIT_0(info->ir)) {
2609 op0->reg = (BIT_F(extension) ? M68K_REG_A0 : M68K_REG_D0) + ((extension >> 12) & 7);
2610 op1->reg = reg;
2611 } else {
2612 op0->reg = reg;
2613 op1->reg = (BIT_F(extension) ? M68K_REG_A0 : M68K_REG_D0) + ((extension >> 12) & 7);
2614 }
2615 }
2616
2617 static void d68000_movem_pd_16(m68k_info *info)
2618 {
2619 build_movem_re(info, M68K_INS_MOVEM, 2);
2620 }
2621
2622 static void d68000_movem_pd_32(m68k_info *info)
2623 {
2624 build_movem_re(info, M68K_INS_MOVEM, 4);
2625 }
2626
2627 static void d68000_movem_er_16(m68k_info *info)
2628 {
2629 build_movem_er(info, M68K_INS_MOVEM, 2);
2630 }
2631
2632 static void d68000_movem_er_32(m68k_info *info)
2633 {
2634 build_movem_er(info, M68K_INS_MOVEM, 4);
2635 }
2636
2637 static void d68000_movem_re_16(m68k_info *info)
2638 {
2639 build_movem_re(info, M68K_INS_MOVEM, 2);
2640 }
2641
2642 static void d68000_movem_re_32(m68k_info *info)
2643 {
2644 build_movem_re(info, M68K_INS_MOVEM, 4);
2645 }
2646
2647 static void d68000_movep_re_16(m68k_info *info)
2648 {
2649 build_movep_re(info, 2);
2650 }
2651
2652 static void d68000_movep_re_32(m68k_info *info)
2653 {
2654 build_movep_re(info, 4);
2655 }
2656
2657 static void d68000_movep_er_16(m68k_info *info)
2658 {
2659 build_movep_er(info, 2);
2660 }
2661
2662 static void d68000_movep_er_32(m68k_info *info)
2663 {
2664 build_movep_er(info, 4);
2665 }
2666
2667 static void d68010_moves_8(m68k_info *info)
2668 {
2669 LIMIT_CPU_TYPES(info, M68010_PLUS);
2670 build_moves(info, 1);
2671 }
2672
2673 static void d68010_moves_16(m68k_info *info)
2674 {
2675 //uint32_t extension;
2676 LIMIT_CPU_TYPES(info, M68010_PLUS);
2677 build_moves(info, 2);
2678 }
2679
2680 static void d68010_moves_32(m68k_info *info)
2681 {
2682 LIMIT_CPU_TYPES(info, M68010_PLUS);
2683 build_moves(info, 4);
2684 }
2685
2686 static void d68000_moveq(m68k_info *info)
2687 {
2688 cs_m68k_op* op0;
2689 cs_m68k_op* op1;
2690
2691 cs_m68k* ext = build_init_op(info, M68K_INS_MOVEQ, 2, 0);
2692
2693 op0 = &ext->operands[0];
2694 op1 = &ext->operands[1];
2695
2696 op0->type = M68K_OP_IMM;
2697 op0->address_mode = M68K_AM_IMMEDIATE;
2698 op0->imm = (info->ir & 0xff);
2699
2700 op1->address_mode = M68K_AM_REG_DIRECT_DATA;
2701 op1->reg = M68K_REG_D0 + ((info->ir >> 9) & 7);
2702 }
2703
2704 static void d68040_move16_pi_pi(m68k_info *info)
2705 {
2706 int data[] = { info->ir & 7, (read_imm_16(info) >> 12) & 7 };
2707 int modes[] = { M68K_AM_REGI_ADDR_POST_INC, M68K_AM_REGI_ADDR_POST_INC };
2708
2709 LIMIT_CPU_TYPES(info, M68040_PLUS);
2710
2711 build_move16(info, data, modes);
2712 }
2713
2714 static void d68040_move16_pi_al(m68k_info *info)
2715 {
2716 int data[] = { info->ir & 7, read_imm_32(info) };
2717 int modes[] = { M68K_AM_REGI_ADDR_POST_INC, M68K_AM_ABSOLUTE_DATA_LONG };
2718
2719 LIMIT_CPU_TYPES(info, M68040_PLUS);
2720
2721 build_move16(info, data, modes);
2722 }
2723
2724 static void d68040_move16_al_pi(m68k_info *info)
2725 {
2726 int data[] = { read_imm_32(info), info->ir & 7 };
2727 int modes[] = { M68K_AM_ABSOLUTE_DATA_LONG, M68K_AM_REGI_ADDR_POST_INC };
2728
2729 LIMIT_CPU_TYPES(info, M68040_PLUS);
2730
2731 build_move16(info, data, modes);
2732 }
2733
2734 static void d68040_move16_ai_al(m68k_info *info)
2735 {
2736 int data[] = { info->ir & 7, read_imm_32(info) };
2737 int modes[] = { M68K_AM_REG_DIRECT_ADDR, M68K_AM_ABSOLUTE_DATA_LONG };
2738
2739 LIMIT_CPU_TYPES(info, M68040_PLUS);
2740
2741 build_move16(info, data, modes);
2742 }
2743
2744 static void d68040_move16_al_ai(m68k_info *info)
2745 {
2746 int data[] = { read_imm_32(info), info->ir & 7 };
2747 int modes[] = { M68K_AM_ABSOLUTE_DATA_LONG, M68K_AM_REG_DIRECT_ADDR };
2748
2749 LIMIT_CPU_TYPES(info, M68040_PLUS);
2750
2751 build_move16(info, data, modes);
2752 }
2753
2754 static void d68000_muls(m68k_info *info)
2755 {
2756 build_er_1(info, M68K_INS_MULS, 2);
2757 }
2758
2759 static void d68000_mulu(m68k_info *info)
2760 {
2761 build_er_1(info, M68K_INS_MULU, 2);
2762 }
2763
2764 static void d68020_mull(m68k_info *info)
2765 {
2766 uint32_t extension, insn_signed;
2767 cs_m68k* ext;
2768 cs_m68k_op* op0;
2769 cs_m68k_op* op1;
2770 uint32_t reg_0, reg_1;
2771
2772 LIMIT_CPU_TYPES(info, M68020_PLUS);
2773
2774 extension = read_imm_16(info);
2775 insn_signed = 0;
2776
2777 if (BIT_B((extension)))
2778 insn_signed = 1;
2779
2780 ext = build_init_op(info, insn_signed ? M68K_INS_MULS : M68K_INS_MULU, 2, 4);
2781
2782 op0 = &ext->operands[0];
2783 op1 = &ext->operands[1];
2784
2785 get_ea_mode_op(info, op0, info->ir, 4);
2786
2787 reg_0 = extension & 7;
2788 reg_1 = (extension >> 12) & 7;
2789
2790 op1->address_mode = M68K_AM_NONE;
2791 op1->type = M68K_OP_REG_PAIR;
2792 op1->reg_pair.reg_0 = reg_0 + M68K_REG_D0;
2793 op1->reg_pair.reg_1 = reg_1 + M68K_REG_D0;
2794
2795 if (!BIT_A(extension)) {
2796 op1->type = M68K_OP_REG;
2797 op1->reg = M68K_REG_D0 + reg_1;
2798 }
2799 }
2800
2801 static void d68000_nbcd(m68k_info *info)
2802 {
2803 build_ea(info, M68K_INS_NBCD, 1);
2804 }
2805
2806 static void d68000_neg_8(m68k_info *info)
2807 {
2808 build_ea(info, M68K_INS_NEG, 1);
2809 }
2810
2811 static void d68000_neg_16(m68k_info *info)
2812 {
2813 build_ea(info, M68K_INS_NEG, 2);
2814 }
2815
2816 static void d68000_neg_32(m68k_info *info)
2817 {
2818 build_ea(info, M68K_INS_NEG, 4);
2819 }
2820
2821 static void d68000_negx_8(m68k_info *info)
2822 {
2823 build_ea(info, M68K_INS_NEGX, 1);
2824 }
2825
2826 static void d68000_negx_16(m68k_info *info)
2827 {
2828 build_ea(info, M68K_INS_NEGX, 2);
2829 }
2830
2831 static void d68000_negx_32(m68k_info *info)
2832 {
2833 build_ea(info, M68K_INS_NEGX, 4);
2834 }
2835
2836 static void d68000_nop(m68k_info *info)
2837 {
2838 MCInst_setOpcode(info->inst, M68K_INS_NOP);
2839 }
2840
2841 static void d68000_not_8(m68k_info *info)
2842 {
2843 build_ea(info, M68K_INS_NOT, 1);
2844 }
2845
2846 static void d68000_not_16(m68k_info *info)
2847 {
2848 build_ea(info, M68K_INS_NOT, 2);
2849 }
2850
2851 static void d68000_not_32(m68k_info *info)
2852 {
2853 build_ea(info, M68K_INS_NOT, 4);
2854 }
2855
2856 static void d68000_or_er_8(m68k_info *info)
2857 {
2858 build_er_1(info, M68K_INS_OR, 1);
2859 }
2860
2861 static void d68000_or_er_16(m68k_info *info)
2862 {
2863 build_er_1(info, M68K_INS_OR, 2);
2864 }
2865
2866 static void d68000_or_er_32(m68k_info *info)
2867 {
2868 build_er_1(info, M68K_INS_OR, 4);
2869 }
2870
2871 static void d68000_or_re_8(m68k_info *info)
2872 {
2873 build_re_1(info, M68K_INS_OR, 1);
2874 }
2875
2876 static void d68000_or_re_16(m68k_info *info)
2877 {
2878 build_re_1(info, M68K_INS_OR, 2);
2879 }
2880
2881 static void d68000_or_re_32(m68k_info *info)
2882 {
2883 build_re_1(info, M68K_INS_OR, 4);
2884 }
2885
2886 static void d68000_ori_8(m68k_info *info)
2887 {
2888 build_imm_ea(info, M68K_INS_ORI, 1, read_imm_8(info));
2889 }
2890
2891 static void d68000_ori_16(m68k_info *info)
2892 {
2893 build_imm_ea(info, M68K_INS_ORI, 2, read_imm_16(info));
2894 }
2895
2896 static void d68000_ori_32(m68k_info *info)
2897 {
2898 build_imm_ea(info, M68K_INS_ORI, 4, read_imm_32(info));
2899 }
2900
2901 static void d68000_ori_to_ccr(m68k_info *info)
2902 {
2903 build_imm_special_reg(info, M68K_INS_ORI, read_imm_8(info), 1, M68K_REG_CCR);
2904 }
2905
2906 static void d68000_ori_to_sr(m68k_info *info)
2907 {
2908 build_imm_special_reg(info, M68K_INS_ORI, read_imm_16(info), 2, M68K_REG_SR);
2909 }
2910
2911 static void d68020_pack_rr(m68k_info *info)
2912 {
2913 LIMIT_CPU_TYPES(info, M68020_PLUS);
2914 build_rr(info, M68K_INS_PACK, 0, read_imm_16(info));
2915 }
2916
2917 static void d68020_pack_mm(m68k_info *info)
2918 {
2919 LIMIT_CPU_TYPES(info, M68020_PLUS);
2920 build_mm(info, M68K_INS_PACK, 0, read_imm_16(info));
2921 }
2922
2923 static void d68000_pea(m68k_info *info)
2924 {
2925 build_ea(info, M68K_INS_PEA, 4);
2926 }
2927
2928 static void d68000_reset(m68k_info *info)
2929 {
2930 MCInst_setOpcode(info->inst, M68K_INS_RESET);
2931 }
2932
2933 static void d68000_ror_s_8(m68k_info *info)
2934 {
2935 build_3bit_d(info, M68K_INS_ROR, 1);
2936 }
2937
2938 static void d68000_ror_s_16(m68k_info *info)
2939 {
2940 build_3bit_d(info, M68K_INS_ROR, 2);
2941 }
2942
2943 static void d68000_ror_s_32(m68k_info *info)
2944 {
2945 build_3bit_d(info, M68K_INS_ROR, 4);
2946 }
2947
2948 static void d68000_ror_r_8(m68k_info *info)
2949 {
2950 build_r(info, M68K_INS_ROR, 1);
2951 }
2952
2953 static void d68000_ror_r_16(m68k_info *info)
2954 {
2955 build_r(info, M68K_INS_ROR, 2);
2956 }
2957
2958 static void d68000_ror_r_32(m68k_info *info)
2959 {
2960 build_r(info, M68K_INS_ROR, 4);
2961 }
2962
2963 static void d68000_ror_ea(m68k_info *info)
2964 {
2965 build_ea(info, M68K_INS_ROR, 2);
2966 }
2967
2968 static void d68000_rol_s_8(m68k_info *info)
2969 {
2970 build_3bit_d(info, M68K_INS_ROL, 1);
2971 }
2972
2973 static void d68000_rol_s_16(m68k_info *info)
2974 {
2975 build_3bit_d(info, M68K_INS_ROL, 2);
2976 }
2977
2978 static void d68000_rol_s_32(m68k_info *info)
2979 {
2980 build_3bit_d(info, M68K_INS_ROL, 4);
2981 }
2982
2983 static void d68000_rol_r_8(m68k_info *info)
2984 {
2985 build_r(info, M68K_INS_ROL, 1);
2986 }
2987
2988 static void d68000_rol_r_16(m68k_info *info)
2989 {
2990 build_r(info, M68K_INS_ROL, 2);
2991 }
2992
2993 static void d68000_rol_r_32(m68k_info *info)
2994 {
2995 build_r(info, M68K_INS_ROL, 4);
2996 }
2997
2998 static void d68000_rol_ea(m68k_info *info)
2999 {
3000 build_ea(info, M68K_INS_ROL, 2);
3001 }
3002
3003 static void d68000_roxr_s_8(m68k_info *info)
3004 {
3005 build_3bit_d(info, M68K_INS_ROXR, 1);
3006 }
3007
3008 static void d68000_roxr_s_16(m68k_info *info)
3009 {
3010 build_3bit_d(info, M68K_INS_ROXR, 2);
3011 }
3012
3013 static void d68000_roxr_s_32(m68k_info *info)
3014 {
3015 build_3bit_d(info, M68K_INS_ROXR, 4);
3016 }
3017
3018 static void d68000_roxr_r_8(m68k_info *info)
3019 {
3020 build_3bit_d(info, M68K_INS_ROXR, 4);
3021 }
3022
3023 static void d68000_roxr_r_16(m68k_info *info)
3024 {
3025 build_r(info, M68K_INS_ROXR, 2);
3026 }
3027
3028 static void d68000_roxr_r_32(m68k_info *info)
3029 {
3030 build_r(info, M68K_INS_ROXR, 4);
3031 }
3032
3033 static void d68000_roxr_ea(m68k_info *info)
3034 {
3035 build_ea(info, M68K_INS_ROXR, 2);
3036 }
3037
3038 static void d68000_roxl_s_8(m68k_info *info)
3039 {
3040 build_3bit_d(info, M68K_INS_ROXL, 1);
3041 }
3042
3043 static void d68000_roxl_s_16(m68k_info *info)
3044 {
3045 build_3bit_d(info, M68K_INS_ROXL, 2);
3046 }
3047
3048 static void d68000_roxl_s_32(m68k_info *info)
3049 {
3050 build_3bit_d(info, M68K_INS_ROXL, 4);
3051 }
3052
3053 static void d68000_roxl_r_8(m68k_info *info)
3054 {
3055 build_r(info, M68K_INS_ROXL, 1);
3056 }
3057
3058 static void d68000_roxl_r_16(m68k_info *info)
3059 {
3060 build_r(info, M68K_INS_ROXL, 2);
3061 }
3062
3063 static void d68000_roxl_r_32(m68k_info *info)
3064 {
3065 build_r(info, M68K_INS_ROXL, 4);
3066 }
3067
3068 static void d68000_roxl_ea(m68k_info *info)
3069 {
3070 build_ea(info, M68K_INS_ROXL, 2);
3071 }
3072
3073 static void d68010_rtd(m68k_info *info)
3074 {
3075 set_insn_group(info, M68K_GRP_RET);
3076 LIMIT_CPU_TYPES(info, M68010_PLUS);
3077 build_absolute_jump_with_immediate(info, M68K_INS_RTD, 0, read_imm_16(info));
3078 }
3079
3080 static void d68000_rte(m68k_info *info)
3081 {
3082 set_insn_group(info, M68K_GRP_IRET);
3083 MCInst_setOpcode(info->inst, M68K_INS_RTE);
3084 }
3085
3086 static void d68020_rtm(m68k_info *info)
3087 {
3088 cs_m68k* ext;
3089 cs_m68k_op* op;
3090
3091 set_insn_group(info, M68K_GRP_RET);
3092
3093 LIMIT_CPU_TYPES(info, M68020_ONLY);
3094
3095 build_absolute_jump_with_immediate(info, M68K_INS_RTM, 0, 0);
3096
3097 ext = &info->extension;
3098 op = &ext->operands[0];
3099
3100 op->address_mode = M68K_AM_NONE;
3101 op->type = M68K_OP_REG;
3102
3103 if (BIT_3(info->ir)) {
3104 op->reg = M68K_REG_A0 + (info->ir & 7);
3105 } else {
3106 op->reg = M68K_REG_D0 + (info->ir & 7);
3107 }
3108 }
3109
3110 static void d68000_rtr(m68k_info *info)
3111 {
3112 set_insn_group(info, M68K_GRP_RET);
3113 MCInst_setOpcode(info->inst, M68K_INS_RTR);
3114 }
3115
3116 static void d68000_rts(m68k_info *info)
3117 {
3118 set_insn_group(info, M68K_GRP_RET);
3119 MCInst_setOpcode(info->inst, M68K_INS_RTS);
3120 }
3121
3122 static void d68000_sbcd_rr(m68k_info *info)
3123 {
3124 build_rr(info, M68K_INS_SBCD, 1, 0);
3125 }
3126
3127 static void d68000_sbcd_mm(m68k_info *info)
3128 {
3129 build_mm(info, M68K_INS_SBCD, 0, read_imm_16(info));
3130 }
3131
3132 static void d68000_scc(m68k_info *info)
3133 {
3134 cs_m68k* ext = build_init_op(info, s_scc_lut[(info->ir >> 8) & 0xf], 1, 1);
3135 get_ea_mode_op(info, &ext->operands[0], info->ir, 1);
3136 }
3137
3138 static void d68000_stop(m68k_info *info)
3139 {
3140 build_absolute_jump_with_immediate(info, M68K_INS_STOP, 0, read_imm_16(info));
3141 }
3142
3143 static void d68000_sub_er_8(m68k_info *info)
3144 {
3145 build_er_1(info, M68K_INS_SUB, 1);
3146 }
3147
3148 static void d68000_sub_er_16(m68k_info *info)
3149 {
3150 build_er_1(info, M68K_INS_SUB, 2);
3151 }
3152
3153 static void d68000_sub_er_32(m68k_info *info)
3154 {
3155 build_er_1(info, M68K_INS_SUB, 4);
3156 }
3157
3158 static void d68000_sub_re_8(m68k_info *info)
3159 {
3160 build_re_1(info, M68K_INS_SUB, 1);
3161 }
3162
3163 static void d68000_sub_re_16(m68k_info *info)
3164 {
3165 build_re_1(info, M68K_INS_SUB, 2);
3166 }
3167
3168 static void d68000_sub_re_32(m68k_info *info)
3169 {
3170 build_re_1(info, M68K_INS_SUB, 4);
3171 }
3172
3173 static void d68000_suba_16(m68k_info *info)
3174 {
3175 build_ea_a(info, M68K_INS_SUBA, 2);
3176 }
3177
3178 static void d68000_suba_32(m68k_info *info)
3179 {
3180 build_ea_a(info, M68K_INS_SUBA, 4);
3181 }
3182
3183 static void d68000_subi_8(m68k_info *info)
3184 {
3185 build_imm_ea(info, M68K_INS_SUBI, 1, read_imm_8(info));
3186 }
3187
3188 static void d68000_subi_16(m68k_info *info)
3189 {
3190 build_imm_ea(info, M68K_INS_SUBI, 2, read_imm_16(info));
3191 }
3192
3193 static void d68000_subi_32(m68k_info *info)
3194 {
3195 build_imm_ea(info, M68K_INS_SUBI, 4, read_imm_32(info));
3196 }
3197
3198 static void d68000_subq_8(m68k_info *info)
3199 {
3200 build_3bit_ea(info, M68K_INS_SUBQ, 1);
3201 }
3202
3203 static void d68000_subq_16(m68k_info *info)
3204 {
3205 build_3bit_ea(info, M68K_INS_SUBQ, 2);
3206 }
3207
3208 static void d68000_subq_32(m68k_info *info)
3209 {
3210 build_3bit_ea(info, M68K_INS_SUBQ, 4);
3211 }
3212
3213 static void d68000_subx_rr_8(m68k_info *info)
3214 {
3215 build_rr(info, M68K_INS_SUBX, 1, 0);
3216 }
3217
3218 static void d68000_subx_rr_16(m68k_info *info)
3219 {
3220 build_rr(info, M68K_INS_SUBX, 2, 0);
3221 }
3222
3223 static void d68000_subx_rr_32(m68k_info *info)
3224 {
3225 build_rr(info, M68K_INS_SUBX, 4, 0);
3226 }
3227
3228 static void d68000_subx_mm_8(m68k_info *info)
3229 {
3230 build_mm(info, M68K_INS_SUBX, 1, 0);
3231 }
3232
3233 static void d68000_subx_mm_16(m68k_info *info)
3234 {
3235 build_mm(info, M68K_INS_SUBX, 2, 0);
3236 }
3237
3238 static void d68000_subx_mm_32(m68k_info *info)
3239 {
3240 build_mm(info, M68K_INS_SUBX, 4, 0);
3241 }
3242
3243 static void d68000_swap(m68k_info *info)
3244 {
3245 build_d(info, M68K_INS_SWAP, 0);
3246 }
3247
3248 static void d68000_tas(m68k_info *info)
3249 {
3250 build_ea(info, M68K_INS_TAS, 1);
3251 }
3252
3253 static void d68000_trap(m68k_info *info)
3254 {
3255 build_absolute_jump_with_immediate(info, M68K_INS_TRAP, 0, info->ir&0xf);
3256 }
3257
3258 static void d68020_trapcc_0(m68k_info *info)
3259 {
3260 LIMIT_CPU_TYPES(info, M68020_PLUS);
3261 build_trap(info, 0, 0);
3262
3263 info->extension.op_count = 0;
3264 }
3265
3266 static void d68020_trapcc_16(m68k_info *info)
3267 {
3268 LIMIT_CPU_TYPES(info, M68020_PLUS);
3269 build_trap(info, 2, read_imm_16(info));
3270 }
3271
3272 static void d68020_trapcc_32(m68k_info *info)
3273 {
3274 LIMIT_CPU_TYPES(info, M68020_PLUS);
3275 build_trap(info, 4, read_imm_32(info));
3276 }
3277
3278 static void d68000_trapv(m68k_info *info)
3279 {
3280 MCInst_setOpcode(info->inst, M68K_INS_TRAPV);
3281 }
3282
3283 static void d68000_tst_8(m68k_info *info)
3284 {
3285 build_ea(info, M68K_INS_TST, 1);
3286 }
3287
3288 static void d68020_tst_pcdi_8(m68k_info *info)
3289 {
3290 LIMIT_CPU_TYPES(info, M68020_PLUS);
3291 build_ea(info, M68K_INS_TST, 1);
3292 }
3293
3294 static void d68020_tst_pcix_8(m68k_info *info)
3295 {
3296 LIMIT_CPU_TYPES(info, M68020_PLUS);
3297 build_ea(info, M68K_INS_TST, 1);
3298 }
3299
3300 static void d68020_tst_i_8(m68k_info *info)
3301 {
3302 LIMIT_CPU_TYPES(info, M68020_PLUS);
3303 build_ea(info, M68K_INS_TST, 1);
3304 }
3305
3306 static void d68000_tst_16(m68k_info *info)
3307 {
3308 build_ea(info, M68K_INS_TST, 2);
3309 }
3310
3311 static void d68020_tst_a_16(m68k_info *info)
3312 {
3313 LIMIT_CPU_TYPES(info, M68020_PLUS);
3314 build_ea(info, M68K_INS_TST, 2);
3315 }
3316
3317 static void d68020_tst_pcdi_16(m68k_info *info)
3318 {
3319 LIMIT_CPU_TYPES(info, M68020_PLUS);
3320 build_ea(info, M68K_INS_TST, 2);
3321 }
3322
3323 static void d68020_tst_pcix_16(m68k_info *info)
3324 {
3325 LIMIT_CPU_TYPES(info, M68020_PLUS);
3326 build_ea(info, M68K_INS_TST, 2);
3327 }
3328
3329 static void d68020_tst_i_16(m68k_info *info)
3330 {
3331 LIMIT_CPU_TYPES(info, M68020_PLUS);
3332 build_ea(info, M68K_INS_TST, 2);
3333 }
3334
3335 static void d68000_tst_32(m68k_info *info)
3336 {
3337 build_ea(info, M68K_INS_TST, 4);
3338 }
3339
3340 static void d68020_tst_a_32(m68k_info *info)
3341 {
3342 LIMIT_CPU_TYPES(info, M68020_PLUS);
3343 build_ea(info, M68K_INS_TST, 4);
3344 }
3345
3346 static void d68020_tst_pcdi_32(m68k_info *info)
3347 {
3348 LIMIT_CPU_TYPES(info, M68020_PLUS);
3349 build_ea(info, M68K_INS_TST, 4);
3350 }
3351
3352 static void d68020_tst_pcix_32(m68k_info *info)
3353 {
3354 LIMIT_CPU_TYPES(info, M68020_PLUS);
3355 build_ea(info, M68K_INS_TST, 4);
3356 }
3357
3358 static void d68020_tst_i_32(m68k_info *info)
3359 {
3360 LIMIT_CPU_TYPES(info, M68020_PLUS);
3361 build_ea(info, M68K_INS_TST, 4);
3362 }
3363
3364 static void d68000_unlk(m68k_info *info)
3365 {
3366 cs_m68k_op* op;
3367 cs_m68k* ext = build_init_op(info, M68K_INS_UNLK, 1, 0);
3368
3369 op = &ext->operands[0];
3370
3371 op->address_mode = M68K_AM_REG_DIRECT_ADDR;
3372 op->reg = M68K_REG_A0 + (info->ir & 7);
3373 }
3374
3375 static void d68020_unpk_rr(m68k_info *info)
3376 {
3377 LIMIT_CPU_TYPES(info, M68020_PLUS);
3378 build_rr(info, M68K_INS_UNPK, 0, read_imm_16(info));
3379 }
3380
3381 static void d68020_unpk_mm(m68k_info *info)
3382 {
3383 LIMIT_CPU_TYPES(info, M68020_PLUS);
3384 build_mm(info, M68K_INS_UNPK, 0, read_imm_16(info));
3385 }
3386
3387 /* This table is auto-generated. Look in contrib/m68k_instruction_tbl_gen for more info */
3388 #include "M68KInstructionTable.inc"
3389
3390 static int instruction_is_valid(m68k_info *info, const unsigned int word_check)
3391 {
3392 const unsigned int instruction = info->ir;
3393 const instruction_struct *i = &g_instruction_table[instruction];
3394
3395 if ( (i->word2_mask && ((word_check & i->word2_mask) != i->word2_match)) ||
3396 (i->instruction == d68000_invalid) ) {
3397 d68000_invalid(info);
3398 return 0;
3399 }
3400
3401 return 1;
3402 }
3403
3404 static int exists_reg_list(uint16_t *regs, uint8_t count, m68k_reg reg)
3405 {
3406 uint8_t i;
3407
3408 for (i = 0; i < count; ++i) {
3409 if (regs[i] == (uint16_t)reg)
3410 return 1;
3411 }
3412
3413 return 0;
3414 }
3415
3416 static void add_reg_to_rw_list(m68k_info *info, m68k_reg reg, int write)
3417 {
3418 if (reg == M68K_REG_INVALID)
3419 return;
3420
3421 if (write)
3422 {
3423 if (exists_reg_list(info->regs_write, info->regs_write_count, reg))
3424 return;
3425
3426 info->regs_write[info->regs_write_count] = (uint16_t)reg;
3427 info->regs_write_count++;
3428 }
3429 else
3430 {
3431 if (exists_reg_list(info->regs_read, info->regs_read_count, reg))
3432 return;
3433
3434 info->regs_read[info->regs_read_count] = (uint16_t)reg;
3435 info->regs_read_count++;
3436 }
3437 }
3438
3439 static void update_am_reg_list(m68k_info *info, cs_m68k_op *op, int write)
3440 {
3441 switch (op->address_mode) {
3442 case M68K_AM_REG_DIRECT_ADDR:
3443 case M68K_AM_REG_DIRECT_DATA:
3444 add_reg_to_rw_list(info, op->reg, write);
3445 break;
3446
3447 case M68K_AM_REGI_ADDR_POST_INC:
3448 case M68K_AM_REGI_ADDR_PRE_DEC:
3449 add_reg_to_rw_list(info, op->reg, 1);
3450 break;
3451
3452 case M68K_AM_REGI_ADDR:
3453 case M68K_AM_REGI_ADDR_DISP:
3454 add_reg_to_rw_list(info, op->reg, 0);
3455 break;
3456
3457 case M68K_AM_AREGI_INDEX_8_BIT_DISP:
3458 case M68K_AM_AREGI_INDEX_BASE_DISP:
3459 case M68K_AM_MEMI_POST_INDEX:
3460 case M68K_AM_MEMI_PRE_INDEX:
3461 case M68K_AM_PCI_INDEX_8_BIT_DISP:
3462 case M68K_AM_PCI_INDEX_BASE_DISP:
3463 case M68K_AM_PC_MEMI_PRE_INDEX:
3464 case M68K_AM_PC_MEMI_POST_INDEX:
3465 add_reg_to_rw_list(info, op->mem.index_reg, 0);
3466 add_reg_to_rw_list(info, op->mem.base_reg, 0);
3467 break;
3468
3469 // no register(s) in the other addressing modes
3470 default:
3471 break;
3472 }
3473 }
3474
3475 static void update_bits_range(m68k_info *info, m68k_reg reg_start, uint8_t bits, int write)
3476 {
3477 int i;
3478
3479 for (i = 0; i < 8; ++i) {
3480 if (bits & (1 << i)) {
3481 add_reg_to_rw_list(info, reg_start + i, write);
3482 }
3483 }
3484 }
3485
3486 static void update_reg_list_regbits(m68k_info *info, cs_m68k_op *op, int write)
3487 {
3488 uint32_t bits = op->register_bits;
3489 update_bits_range(info, M68K_REG_D0, bits & 0xff, write);
3490 update_bits_range(info, M68K_REG_A0, (bits >> 8) & 0xff, write);
3491 update_bits_range(info, M68K_REG_FP0, (bits >> 16) & 0xff, write);
3492 }
3493
3494 static void update_op_reg_list(m68k_info *info, cs_m68k_op *op, int write)
3495 {
3496 switch ((int)op->type) {
3497 case M68K_OP_REG:
3498 add_reg_to_rw_list(info, op->reg, write);
3499 break;
3500
3501 case M68K_OP_MEM:
3502 update_am_reg_list(info, op, write);
3503 break;
3504
3505 case M68K_OP_REG_BITS:
3506 update_reg_list_regbits(info, op, write);
3507 break;
3508
3509 case M68K_OP_REG_PAIR:
3510 add_reg_to_rw_list(info, op->reg_pair.reg_0, write);
3511 add_reg_to_rw_list(info, op->reg_pair.reg_1, write);
3512 break;
3513 }
3514 }
3515
3516 static void build_regs_read_write_counts(m68k_info *info)
3517 {
3518 int i;
3519
3520 if (!info->extension.op_count)
3521 return;
3522
3523 if (info->extension.op_count == 1) {
3524 update_op_reg_list(info, &info->extension.operands[0], 1);
3525 } else {
3526 // first operand is always read
3527 update_op_reg_list(info, &info->extension.operands[0], 0);
3528
3529 // remaning write
3530 for (i = 1; i < info->extension.op_count; ++i)
3531 update_op_reg_list(info, &info->extension.operands[i], 1);
3532 }
3533 }
3534
3535 static void m68k_setup_internals(m68k_info* info, MCInst* inst, unsigned int pc, unsigned int cpu_type)
3536 {
3537 info->inst = inst;
3538 info->pc = pc;
3539 info->ir = 0;
3540 info->type = cpu_type;
3541 info->address_mask = 0xffffffff;
3542
3543 switch(info->type) {
3544 case M68K_CPU_TYPE_68000:
3545 info->type = TYPE_68000;
3546 info->address_mask = 0x00ffffff;
3547 break;
3548 case M68K_CPU_TYPE_68010:
3549 info->type = TYPE_68010;
3550 info->address_mask = 0x00ffffff;
3551 break;
3552 case M68K_CPU_TYPE_68EC020:
3553 info->type = TYPE_68020;
3554 info->address_mask = 0x00ffffff;
3555 break;
3556 case M68K_CPU_TYPE_68020:
3557 info->type = TYPE_68020;
3558 info->address_mask = 0xffffffff;
3559 break;
3560 case M68K_CPU_TYPE_68030:
3561 info->type = TYPE_68030;
3562 info->address_mask = 0xffffffff;
3563 break;
3564 case M68K_CPU_TYPE_68040:
3565 info->type = TYPE_68040;
3566 info->address_mask = 0xffffffff;
3567 break;
3568 default:
3569 info->address_mask = 0;
3570 return;
3571 }
3572 }
3573
3574 /* ======================================================================== */
3575 /* ================================= API ================================== */
3576 /* ======================================================================== */
3577
3578 /* Disasemble one instruction at pc and store in str_buff */
3579 static unsigned int m68k_disassemble(m68k_info *info, uint64_t pc)
3580 {
3581 MCInst *inst = info->inst;
3582 cs_m68k* ext = &info->extension;
3583 int i;
3584 unsigned int size;
3585
3586 inst->Opcode = M68K_INS_INVALID;
3587
3588 memset(ext, 0, sizeof(cs_m68k));
3589 ext->op_size.type = M68K_SIZE_TYPE_CPU;
3590
3591 for (i = 0; i < M68K_OPERAND_COUNT; ++i)
3592 ext->operands[i].type = M68K_OP_REG;
3593
3594 info->ir = peek_imm_16(info);
3595 if (instruction_is_valid(info, peek_imm_32(info) & 0xffff)) {
3596 info->ir = read_imm_16(info);
3597 g_instruction_table[info->ir].instruction(info);
3598 }
3599
3600 size = info->pc - (unsigned int)pc;
3601 info->pc = (unsigned int)pc;
3602
3603 return size;
3604 }
3605
3606 bool M68K_getInstruction(csh ud, const uint8_t* code, size_t code_len, MCInst* instr, uint16_t* size, uint64_t address, void* inst_info)
3607 {
3608 #ifdef M68K_DEBUG
3609 SStream ss;
3610 #endif
3611 int s;
3612 int cpu_type = M68K_CPU_TYPE_68000;
3613 cs_struct* handle = instr->csh;
3614 m68k_info *info = (m68k_info*)handle->printer_info;
3615
3616 // code len has to be at least 2 bytes to be valid m68k
3617
3618 if (code_len < 2) {
3619 *size = 0;
3620 return false;
3621 }
3622
3623 if (instr->flat_insn->detail) {
3624 memset(instr->flat_insn->detail, 0, offsetof(cs_detail, m68k)+sizeof(cs_m68k));
3625 }
3626
3627 info->groups_count = 0;
3628 info->regs_read_count = 0;
3629 info->regs_write_count = 0;
3630 info->code = code;
3631 info->code_len = code_len;
3632 info->baseAddress = address;
3633
3634 if (handle->mode & CS_MODE_M68K_010)
3635 cpu_type = M68K_CPU_TYPE_68010;
3636 if (handle->mode & CS_MODE_M68K_020)
3637 cpu_type = M68K_CPU_TYPE_68020;
3638 if (handle->mode & CS_MODE_M68K_030)
3639 cpu_type = M68K_CPU_TYPE_68030;
3640 if (handle->mode & CS_MODE_M68K_040)
3641 cpu_type = M68K_CPU_TYPE_68040;
3642 if (handle->mode & CS_MODE_M68K_060)
3643 cpu_type = M68K_CPU_TYPE_68040; // 060 = 040 for now
3644
3645 m68k_setup_internals(info, instr, (unsigned int)address, cpu_type);
3646 s = m68k_disassemble(info, address);
3647
3648 if (s == 0) {
3649 *size = 2;
3650 return false;
3651 }
3652
3653 build_regs_read_write_counts(info);
3654
3655 #ifdef M68K_DEBUG
3656 SStream_Init(&ss);
3657 M68K_printInst(instr, &ss, info);
3658 #endif
3659
3660 // Make sure we always stay within range
3661 if (s > (int)code_len)
3662 *size = (uint16_t)code_len;
3663 else
3664 *size = (uint16_t)s;
3665
3666 return true;
3667 }
3668