Coverage Report

Created: 2025-07-18 06:43

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