Coverage Report

Created: 2024-05-21 06:29

/src/binutils-gdb/opcodes/m68hc11-dis.c
Line
Count
Source (jump to first uncovered line)
1
/* m68hc11-dis.c -- Motorola 68HC11 & 68HC12 disassembly
2
   Copyright (C) 1999-2024 Free Software Foundation, Inc.
3
   Written by Stephane Carrez (stcarrez@nerim.fr)
4
   XGATE and S12X added by James Murray (jsm@jsm-net.demon.co.uk)
5
6
   This file is part of the GNU opcodes library.
7
8
   This library is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 3, or (at your option)
11
   any later version.
12
13
   It is distributed in the hope that it will be useful, but WITHOUT
14
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
16
   License for more details.
17
18
   You should have received a copy of the GNU General Public License
19
   along with this program; if not, write to the Free Software
20
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21
   MA 02110-1301, USA.  */
22
23
#include "sysdep.h"
24
#include <stdio.h>
25
26
#include "opcode/m68hc11.h"
27
#include "disassemble.h"
28
29
43.8k
#define PC_REGNUM 3
30
31
static const char *const reg_name[] =
32
{
33
  "X", "Y", "SP", "PC"
34
};
35
36
static const char *const reg_src_table[] =
37
{
38
  "A", "B", "CCR", "TMP3", "D", "X", "Y", "SP"
39
};
40
41
static const char *const reg_dst_table[] =
42
{
43
  "A", "B", "CCR", "TMP2", "D", "X", "Y", "SP"
44
};
45
46
281k
#define OP_PAGE_MASK (M6811_OP_PAGE2|M6811_OP_PAGE3|M6811_OP_PAGE4)
47
48
49
static int
50
read_memory (bfd_vma memaddr, bfd_byte* buffer, int size,
51
             struct disassemble_info* info)
52
459k
{
53
459k
  int status;
54
55
  /* Get first byte.  Only one at a time because we don't know the
56
     size of the insn.  */
57
459k
  status = (*info->read_memory_func) (memaddr, buffer, size, info);
58
459k
  if (status != 0)
59
121
    {
60
121
      (*info->memory_error_func) (status, memaddr, info);
61
121
      return -1;
62
121
    }
63
459k
  return 0;
64
459k
}
65
66
67
/* Read the 68HC12 indexed operand byte and print the corresponding mode.
68
   Returns the number of bytes read or -1 if failure.  */
69
static int
70
print_indexed_operand (bfd_vma memaddr, struct disassemble_info* info,
71
           int* indirect, int mov_insn, int pc_offset,
72
           bfd_vma endaddr, int arch)
73
32.2k
{
74
32.2k
  bfd_byte buffer[4];
75
32.2k
  int reg;
76
32.2k
  int status;
77
32.2k
  bfd_vma val;
78
32.2k
  int pos = 1;
79
80
32.2k
  if (indirect)
81
31.9k
    *indirect = 0;
82
83
32.2k
  status = read_memory (memaddr, &buffer[0], 1, info);
84
32.2k
  if (status != 0)
85
12
    {
86
12
      return status;
87
12
    }
88
89
  /* n,r with 5-bits signed constant.  */
90
32.2k
  if ((buffer[0] & 0x20) == 0)
91
13.9k
    {
92
13.9k
      reg = (buffer[0] >> 6) & 3;
93
13.9k
      val = ((buffer[0] & 0x1f) ^ 0x10) - 0x10;
94
      /* 68HC12 requires an adjustment for movb/movw pc relative modes.  */
95
13.9k
      if (reg == PC_REGNUM && info->mach == bfd_mach_m6812 && mov_insn)
96
0
  val += pc_offset;
97
13.9k
      (*info->fprintf_func) (info->stream, "0x%x,%s",
98
13.9k
           (unsigned) val & 0xffff, reg_name[reg]);
99
100
13.9k
      if (reg == PC_REGNUM)
101
1.49k
  {
102
1.49k
    (* info->fprintf_func) (info->stream, " {");
103
     /* Avoid duplicate 0x from core binutils.  */
104
1.49k
    if (info->symtab_size > 0)
105
0
      (*info->fprintf_func) (info->stream, "0x");
106
1.49k
    (* info->print_address_func) (endaddr + val, info);
107
1.49k
    (* info->fprintf_func) (info->stream, "}");
108
1.49k
  }
109
13.9k
    }
110
111
  /* Auto pre/post increment/decrement.  */
112
18.3k
  else if ((buffer[0] & 0xc0) != 0xc0)
113
13.4k
    {
114
13.4k
      const char *mode;
115
116
13.4k
      reg = (buffer[0] >> 6) & 3;
117
13.4k
      val = buffer[0] & 7;
118
13.4k
      if (buffer[0] & 8)
119
7.36k
  {
120
7.36k
    val = 8 - val;
121
7.36k
    mode = "-";
122
7.36k
  }
123
6.08k
      else
124
6.08k
  {
125
6.08k
    val = val + 1;
126
6.08k
    mode = "+";
127
6.08k
  }
128
13.4k
      (*info->fprintf_func) (info->stream, "%d,%s%s%s",
129
13.4k
           (unsigned) val,
130
13.4k
           buffer[0] & 0x10 ? "" : mode,
131
13.4k
           reg_name[reg], buffer[0] & 0x10 ? mode : "");
132
13.4k
    }
133
134
  /* [n,r] 16-bits offset indexed indirect.  */
135
4.89k
  else if ((buffer[0] & 0x07) == 3)
136
250
    {
137
250
      if ((mov_insn) && (!(arch & cpu9s12x)))
138
0
  {
139
0
    (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
140
0
         buffer[0] & 0x0ff);
141
0
    return 0;
142
0
  }
143
250
      reg = (buffer[0] >> 3) & 0x03;
144
250
      status = read_memory (memaddr + pos, &buffer[0], 2, info);
145
250
      if (status != 0)
146
0
  return status;
147
148
250
      pos += 2;
149
250
      val = (buffer[0] << 8) | buffer[1];
150
250
      (*info->fprintf_func) (info->stream, "[0x%x,%s]",
151
250
           (unsigned) val & 0xffff, reg_name[reg]);
152
250
      if (indirect)
153
244
  *indirect = 1;
154
250
    }
155
156
  /* n,r with 9 and 16 bit signed constant.  */
157
4.64k
  else if ((buffer[0] & 0x4) == 0)
158
2.07k
    {
159
2.07k
      if ((mov_insn) && (!(arch & cpu9s12x)))
160
6
  {
161
6
    (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
162
6
         buffer[0] & 0x0ff);
163
6
    return 0;
164
6
  }
165
166
2.06k
      reg = (buffer[0] >> 3) & 0x03;
167
2.06k
      status = read_memory (memaddr + pos,
168
2.06k
          &buffer[1], (buffer[0] & 0x2 ? 2 : 1), info);
169
2.06k
      if (status != 0)
170
2
  return status;
171
172
2.06k
      if (buffer[0] & 2)
173
1.03k
  {
174
1.03k
    val = (((buffer[1] << 8) | buffer[2]) ^ 0x8000) - 0x8000;
175
1.03k
    pos += 2;
176
1.03k
    endaddr += 2;
177
1.03k
  }
178
1.02k
      else
179
1.02k
  {
180
1.02k
    val = buffer[1] - ((buffer[0] & 1) << 8);
181
1.02k
    pos++;
182
1.02k
    endaddr++;
183
1.02k
  }
184
2.06k
      (*info->fprintf_func) (info->stream, "0x%x,%s",
185
2.06k
           (unsigned) val & 0xffff, reg_name[reg]);
186
2.06k
      if (reg == PC_REGNUM)
187
330
  {
188
330
    (* info->fprintf_func) (info->stream, " {0x");
189
330
    (* info->print_address_func) (endaddr + val, info);
190
330
    (* info->fprintf_func) (info->stream, "}");
191
330
  }
192
2.06k
    }
193
2.57k
  else
194
2.57k
    {
195
2.57k
      reg = (buffer[0] >> 3) & 0x03;
196
2.57k
      switch (buffer[0] & 3)
197
2.57k
  {
198
407
  case 0:
199
407
    (*info->fprintf_func) (info->stream, "A,%s", reg_name[reg]);
200
407
    break;
201
418
  case 1:
202
418
    (*info->fprintf_func) (info->stream, "B,%s", reg_name[reg]);
203
418
    break;
204
694
  case 2:
205
694
    (*info->fprintf_func) (info->stream, "D,%s", reg_name[reg]);
206
694
    break;
207
1.05k
  case 3:
208
1.05k
  default:
209
1.05k
    (*info->fprintf_func) (info->stream, "[D,%s]", reg_name[reg]);
210
1.05k
    if (indirect)
211
1.04k
      *indirect = 1;
212
1.05k
    break;
213
2.57k
  }
214
2.57k
    }
215
216
32.2k
  return pos;
217
32.2k
}
218
219
/* Disassemble one instruction at address 'memaddr'.  Returns the number
220
   of bytes used by that instruction.  */
221
static int
222
print_insn (bfd_vma memaddr, struct disassemble_info* info, int arch)
223
302k
{
224
302k
  int status;
225
302k
  bfd_byte buffer[4];
226
302k
  unsigned int code;
227
302k
  long format, pos, i;
228
302k
  bfd_vma val;
229
302k
  const struct m68hc11_opcode *opcode;
230
231
302k
  if (arch & cpuxgate)
232
60.7k
    {
233
      /* Get two bytes as all XGATE instructions are 16bit.  */
234
60.7k
      status = read_memory (memaddr, buffer, 2, info);
235
60.7k
      if (status != 0)
236
17
  return status;
237
238
60.7k
      format = 0;
239
60.7k
      code = (buffer[0] << 8) + buffer[1];
240
241
      /* Scan the opcode table until we find the opcode
242
   with the corresponding page.  */
243
60.7k
      opcode = m68hc11_opcodes;
244
73.5M
      for (i = 0; i < m68hc11_num_opcodes; i++, opcode++)
245
73.5M
  {
246
73.5M
    if ((opcode->opcode != (code & opcode->xg_mask)) || (opcode->arch != cpuxgate))
247
73.4M
        continue;
248
    /* We have found the opcode.  Extract the operand and print it.  */
249
52.4k
    (*info->fprintf_func) (info->stream, "%s", opcode->name);
250
52.4k
    format = opcode->format;
251
52.4k
    if (format & (M68XG_OP_NONE))
252
7.75k
      {
253
        /* Nothing to print.  */
254
7.75k
      }
255
44.6k
    else if (format & M68XG_OP_IMM3)
256
105
      (*info->fprintf_func) (info->stream, " #0x%x", (code >> 8) & 0x7);
257
44.5k
    else if (format & M68XG_OP_R_R)
258
286
      (*info->fprintf_func) (info->stream, " R%x, R%x",
259
286
           (code >> 8) & 0x7, (code >> 5) & 0x7);
260
44.2k
    else if (format & M68XG_OP_R_R_R)
261
3.08k
      (*info->fprintf_func) (info->stream, " R%x, R%x, R%x",
262
3.08k
           (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
263
41.1k
    else if (format & M68XG_OP_RD_RB_RI)
264
1.58k
      (*info->fprintf_func) (info->stream, " R%x, (R%x, R%x)",
265
1.58k
           (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
266
39.5k
    else if (format & M68XG_OP_RD_RB_RIp)
267
1.19k
      (*info->fprintf_func) (info->stream, " R%x, (R%x, R%x+)",
268
1.19k
           (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
269
38.4k
    else if (format & M68XG_OP_RD_RB_mRI)
270
1.19k
      (*info->fprintf_func) (info->stream, " R%x, (R%x, -R%x)",
271
1.19k
           (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
272
37.2k
    else if (format & M68XG_OP_R_R_OFFS5)
273
6.57k
      (*info->fprintf_func) (info->stream, " R%x, (R%x, #0x%x)",
274
6.57k
           (code >> 8) & 0x7, (code >> 5) & 0x7, code & 0x1f);
275
30.6k
    else if (format & M68XG_OP_R_IMM8)
276
24.5k
      (*info->fprintf_func) (info->stream, " R%x, #0x%02x",
277
24.5k
           (code >> 8) & 0x7, code & 0xff);
278
6.06k
    else if (format & M68XG_OP_R_IMM4)
279
846
      (*info->fprintf_func) (info->stream, " R%x, #0x%x",
280
846
           (code >> 8) & 0x7, (code & 0xf0) >> 4);
281
5.22k
    else if (format & M68XG_OP_REL9)
282
4.02k
      {
283
4.02k
        (*info->fprintf_func) (info->stream, " 0x");
284
4.02k
        val = buffer[1] - ((buffer[0] & 1) << 8);
285
4.02k
        (*info->print_address_func) (memaddr + (val << 1) + 2, info);
286
4.02k
      }
287
1.19k
    else if (format & M68XG_OP_REL10)
288
772
      {
289
772
        (*info->fprintf_func) (info->stream, " 0x");
290
772
        val = (buffer[0] << 8) | buffer[1];
291
772
        val = ((val & 0x3ff) ^ 0x200) - 0x200;
292
772
        (*info->print_address_func) (memaddr + (val << 1) + 2, info);
293
772
      }
294
426
    else if ((code & 0x00ff) == 0x00f8)
295
111
        (*info->fprintf_func) (info->stream, " R%x, CCR", (code >> 8) & 0x7);
296
315
    else if ((code & 0x00ff) == 0x00f9)
297
46
        (*info->fprintf_func) (info->stream, " CCR, R%x", (code >> 8) & 0x7);
298
269
    else if ((code & 0x00ff) == 0x0)
299
0
        (*info->fprintf_func) (info->stream, " R%x, PC", (code >> 8) & 0x7);
300
269
    else if (format & M68XG_OP_R)
301
269
        {
302
        /* Special cases for TFR.  */
303
269
        if ((code & 0xf8ff) == 0x00f8)
304
0
    (*info->fprintf_func) (info->stream, " R%x, CCR", (code >> 8) & 0x7);
305
269
        else if ((code & 0xf8ff) == 0x00f9)
306
0
    (*info->fprintf_func) (info->stream, " CCR, R%x", (code >> 8) & 0x7);
307
269
        else if ((code & 0xf8ff) == 0x00fa)
308
107
    (*info->fprintf_func) (info->stream, " R%x, PC",  (code >> 8) & 0x7);
309
162
        else
310
162
    (*info->fprintf_func) (info->stream, " R%x", (code >> 8) & 0x7);
311
269
      }
312
0
    else
313
      /* Opcode not recognized.  */
314
0
      (*info->fprintf_func) (info->stream, "Not yet handled TEST .byte\t0x%04x", code);
315
52.4k
    return 2;
316
73.5M
  }
317
318
      /* Opcode not recognized.  */
319
8.32k
      (*info->fprintf_func) (info->stream, ".byte\t0x%04x", code);
320
8.32k
      return 2; /* Everything is two bytes.  */
321
60.7k
    }
322
323
  /* HC11 and HC12.  */
324
325
  /* Get first byte.  Only one at a time because we don't know the
326
     size of the insn.  */
327
241k
  status = read_memory (memaddr, buffer, 1, info);
328
241k
  if (status != 0)
329
0
    return status;
330
331
241k
  format = 0;
332
241k
  code = buffer[0];
333
241k
  pos = 0;
334
335
  /* Look for page2,3,4 opcodes.  */
336
241k
  if (code == M6811_OPCODE_PAGE2)
337
1.22k
    {
338
1.22k
      pos++;
339
1.22k
      format = M6811_OP_PAGE2;
340
1.22k
    }
341
240k
  else if (code == M6811_OPCODE_PAGE3 && arch == cpu6811)
342
261
    {
343
261
      pos++;
344
261
      format = M6811_OP_PAGE3;
345
261
    }
346
240k
  else if (code == M6811_OPCODE_PAGE4 && arch == cpu6811)
347
115
    {
348
115
      pos++;
349
115
      format = M6811_OP_PAGE4;
350
115
    }
351
352
  /* We are in page2,3,4; get the real opcode.  */
353
241k
  if (pos == 1)
354
1.60k
    {
355
1.60k
      status = read_memory (memaddr + pos, &buffer[1], 1, info);
356
1.60k
      if (status != 0)
357
1
  return status;
358
359
1.60k
      code = buffer[1];
360
1.60k
    }
361
362
  /* Look first for a 68HC12 alias.  All of them are 2-bytes long and
363
     in page 1.  There is no operand to print.  We read the second byte
364
     only when we have a possible match.  */
365
241k
  if ((arch & cpu6812) && format == 0)
366
220k
    {
367
220k
      int must_read = 1;
368
369
      /* Walk the alias table to find a code1+code2 match.  */
370
4.18M
      for (i = 0; i < m68hc12_num_alias; i++)
371
3.96M
  {
372
3.96M
    if (m68hc12_alias[i].code1 == code)
373
13.5k
      {
374
13.5k
        if (must_read)
375
13.5k
    {
376
13.5k
      status = read_memory (memaddr + pos + 1,
377
13.5k
          &buffer[1], 1, info);
378
13.5k
      if (status != 0)
379
7
        break;
380
381
13.5k
      must_read = 1;
382
13.5k
    }
383
13.5k
        if (m68hc12_alias[i].code2 == (unsigned char) buffer[1])
384
161
    {
385
161
      (*info->fprintf_func) (info->stream, "%s",
386
161
           m68hc12_alias[i].name);
387
161
      return 2;
388
161
    }
389
13.5k
      }
390
3.96M
  }
391
220k
    }
392
393
241k
  pos++;
394
395
  /* Scan the opcode table until we find the opcode
396
     with the corresponding page.  */
397
241k
  opcode = m68hc11_opcodes;
398
116M
  for (i = 0; i < m68hc11_num_opcodes; i++, opcode++)
399
116M
    {
400
116M
      int offset;
401
116M
      int pc_src_offset;
402
116M
      int pc_dst_offset = 0;
403
404
116M
      if ((opcode->arch & arch) == 0)
405
47.3M
  continue;
406
69.1M
      if (opcode->opcode != code)
407
68.8M
  continue;
408
281k
      if ((opcode->format & OP_PAGE_MASK) != format)
409
42.4k
  continue;
410
411
239k
      if (opcode->format & M6812_OP_REG)
412
2.52k
  {
413
2.52k
    int j;
414
2.52k
    int is_jump;
415
416
2.52k
    if (opcode->format & M6811_OP_JUMP_REL)
417
1.78k
      is_jump = 1;
418
743
    else
419
743
      is_jump = 0;
420
421
2.52k
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
422
2.52k
    if (status != 0)
423
7
      {
424
7
        return status;
425
7
      }
426
700k
    for (j = 0; i + j < m68hc11_num_opcodes; j++)
427
700k
      {
428
700k
        if ((opcode[j].arch & arch) == 0)
429
252k
    continue;
430
447k
        if (opcode[j].opcode != code)
431
440k
    continue;
432
7.23k
        if (is_jump)
433
5.91k
    {
434
5.91k
      if (!(opcode[j].format & M6811_OP_JUMP_REL))
435
596
        continue;
436
437
5.31k
      if ((opcode[j].format & M6812_OP_IBCC_MARKER)
438
5.31k
          && (buffer[0] & 0xc0) != 0x80)
439
1.19k
        continue;
440
4.12k
      if ((opcode[j].format & M6812_OP_TBCC_MARKER)
441
4.12k
          && (buffer[0] & 0xc0) != 0x40)
442
600
        continue;
443
3.52k
      if ((opcode[j].format & M6812_OP_DBCC_MARKER)
444
3.52k
          && (buffer[0] & 0xc0) != 0)
445
1.71k
        continue;
446
1.80k
      if ((opcode[j].format & M6812_OP_EQ_MARKER)
447
1.80k
          && (buffer[0] & 0x20) == 0)
448
1.14k
        break;
449
666
      if (!(opcode[j].format & M6812_OP_EQ_MARKER)
450
666
          && (buffer[0] & 0x20) != 0)
451
333
        break;
452
333
      continue;
453
666
    }
454
1.31k
        if (opcode[j].format & M6812_OP_EXG_MARKER && buffer[0] & 0x80)
455
378
    break;
456
941
        if ((opcode[j].format & M6812_OP_SEX_MARKER)
457
941
      && (((buffer[0] & 0x07) >= 3 && (buffer[0] & 7) <= 7))
458
941
      && ((buffer[0] & 0x0f0) <= 0x20))
459
145
    break;
460
796
        if ((opcode[j].format & M6812_OP_SEX_MARKER)
461
796
      && (arch & cpu9s12x)
462
796
      && ((buffer[0] == 0x4d) || (buffer[0] == 0x4e)))
463
0
    break;
464
796
        if (opcode[j].format & M6812_OP_TFR_MARKER
465
796
      && !(buffer[0] & 0x80))
466
217
    break;
467
796
      }
468
2.51k
    if (i + j < m68hc11_num_opcodes)
469
2.21k
      opcode = &opcode[j];
470
2.51k
  }
471
472
      /* We have found the opcode.  Extract the operand and print it.  */
473
239k
      (*info->fprintf_func) (info->stream, "%s", opcode->name);
474
475
239k
      format = opcode->format;
476
239k
      if (format & (M6811_OP_MASK | M6811_OP_BITMASK
477
239k
        | M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
478
125k
  {
479
125k
    (*info->fprintf_func) (info->stream, "\t");
480
125k
  }
481
482
      /* The movb and movw must be handled in a special way...
483
   The source constant 'ii' is not always at the same place.
484
   This is the same for the destination for the post-indexed byte.
485
   The 'offset' is used to do the appropriate correction.
486
487
   offset          offset
488
   for constant     for destination
489
   movb   18 OB ii hh ll       0          0
490
   18 08 xb ii          1          -1
491
   18 08 xb ff ii       2          1  9 bit
492
   18 08 xb ee ff ii    3          1  16 bit
493
   18 0C hh ll hh ll    0          0
494
   18 09 xb hh ll       1          -1
495
   18 0D xb hh ll       0          0
496
   18 0A xb xb          0          0
497
498
   movw   18 03 jj kk hh ll    0          0
499
   18 00 xb jj kk       1          -1
500
   18 04 hh ll hh ll    0          0
501
   18 01 xb hh ll       1          -1
502
   18 05 xb hh ll       0          0
503
   18 02 xb xb          0          0
504
505
   After the source operand is read, the position 'pos' is incremented
506
   this explains the negative offset for destination.
507
508
   movb/movw above are the only instructions with this matching
509
   format.  */
510
239k
      offset = ((format & M6812_OP_IDX_P2)
511
239k
    && (format & (M6811_OP_IMM8 | M6811_OP_IMM16 |
512
294
            M6811_OP_IND16)));
513
514
239k
      if (offset)
515
289
  {
516
    /* Check xb to see position of data.  */
517
289
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
518
289
    if (status != 0)
519
0
      {
520
0
        return status;
521
0
      }
522
523
289
    if (((buffer[0] & 0xe0) == 0xe0) && ((buffer[0] & 0x04) == 0))
524
54
      {
525
        /* 9 or 16 bit.  */
526
54
        if ((buffer[0] & 0x02) == 0)
527
48
    {
528
      /* 9 bit.  */
529
48
      offset = 2;
530
48
    }
531
6
        else
532
6
    {
533
      /* 16 bit.  */
534
6
      offset = 3;
535
6
    }
536
54
      }
537
289
  }
538
539
      /* Operand with one more byte: - immediate, offset,
540
   direct-low address.  */
541
239k
      if (format &
542
239k
    (M6811_OP_IMM8 | M6811_OP_IX | M6811_OP_IY | M6811_OP_DIRECT))
543
32.5k
  {
544
32.5k
    status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
545
32.5k
    if (status != 0)
546
13
      return status;
547
548
    /* This movb/movw is special (see above).  */
549
32.5k
    if (offset < 2)
550
32.5k
      {
551
32.5k
        offset = -offset;
552
32.5k
        pc_dst_offset = 2;
553
32.5k
      }
554
0
    else
555
0
      {
556
0
        offset = -1;
557
0
        pc_dst_offset = 5;
558
0
      }
559
32.5k
    pos++;
560
561
32.5k
    if (format & M6811_OP_IMM8)
562
13.8k
      {
563
13.8k
        (*info->fprintf_func) (info->stream, "#0x%x", (int) buffer[0]);
564
13.8k
        format &= ~M6811_OP_IMM8;
565
        /* Set PC destination offset.  */
566
13.8k
        pc_dst_offset = 1;
567
13.8k
      }
568
18.6k
    else if (format & M6811_OP_IX)
569
2.36k
      {
570
        /* Offsets are in range 0..255, print them unsigned.  */
571
2.36k
        (*info->fprintf_func) (info->stream, "0x%x,x", buffer[0] & 0x0FF);
572
2.36k
        format &= ~M6811_OP_IX;
573
2.36k
      }
574
16.3k
    else if (format & M6811_OP_IY)
575
15
      {
576
15
        (*info->fprintf_func) (info->stream, "0x%x,y", buffer[0] & 0x0FF);
577
15
        format &= ~M6811_OP_IY;
578
15
      }
579
16.3k
    else if (format & M6811_OP_DIRECT)
580
16.3k
      {
581
16.3k
        (*info->fprintf_func) (info->stream, "*");
582
16.3k
        if (info->symtab_size > 0) /* Avoid duplicate 0x. */
583
0
    (*info->fprintf_func) (info->stream, "0x");
584
16.3k
        (*info->print_address_func) (buffer[0] & 0x0FF, info);
585
16.3k
        format &= ~M6811_OP_DIRECT;
586
16.3k
      }
587
32.5k
  }
588
589
238k
#define M6812_DST_MOVE  (M6812_OP_IND16_P2 | M6812_OP_IDX_P2)
590
238k
#define M6812_INDEXED_FLAGS (M6812_OP_IDX|M6812_OP_IDX_1|M6812_OP_IDX_2)
591
      /* Analyze the 68HC12 indexed byte.  */
592
238k
      if (format & M6812_INDEXED_FLAGS)
593
31.9k
  {
594
31.9k
    int indirect;
595
31.9k
    bfd_vma endaddr;
596
597
31.9k
    endaddr = memaddr + pos + 1;
598
31.9k
    if (format & M6811_OP_IND16)
599
0
      endaddr += 2;
600
31.9k
    pc_src_offset = -1;
601
31.9k
    pc_dst_offset = 1;
602
31.9k
    status = print_indexed_operand (memaddr + pos, info, &indirect,
603
31.9k
            (format & M6812_DST_MOVE),
604
31.9k
            pc_src_offset, endaddr, arch);
605
31.9k
    if (status < 0)
606
14
      return status;
607
608
31.9k
    pos += status;
609
610
    /* The indirect addressing mode of the call instruction does
611
       not need the page code.  */
612
31.9k
    if ((format & M6812_OP_PAGE) && indirect)
613
6
      format &= ~M6812_OP_PAGE;
614
31.9k
  }
615
616
      /* 68HC12 dbcc/ibcc/tbcc operands.  */
617
238k
      if ((format & M6812_OP_REG) && (format & M6811_OP_JUMP_REL))
618
1.77k
  {
619
1.77k
    status = read_memory (memaddr + pos, &buffer[0], 2, info);
620
1.77k
    if (status != 0)
621
1
      return status;
622
623
1.77k
    (*info->fprintf_func) (info->stream, "%s,",
624
1.77k
         reg_src_table[buffer[0] & 0x07]);
625
1.77k
    val = buffer[1] - ((buffer[0] & 0x10) << 4);
626
627
1.77k
    pos += 2;
628
1.77k
    (*info->fprintf_func) (info->stream, "0x");
629
1.77k
    (*info->print_address_func) (memaddr + pos + val, info);
630
1.77k
    format &= ~(M6812_OP_REG | M6811_OP_JUMP_REL);
631
1.77k
  }
632
237k
      else if (format & (M6812_OP_REG | M6812_OP_REG_2))
633
740
  {
634
740
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
635
740
    if (status != 0)
636
0
      return status;
637
638
740
    pos++;
639
740
    (*info->fprintf_func) (info->stream, "%s,%s",
640
740
         reg_src_table[(buffer[0] >> 4) & 7],
641
740
         reg_dst_table[(buffer[0] & 7)]);
642
740
  }
643
644
238k
      if (format & (M6811_OP_IMM16 | M6811_OP_IND16))
645
46.0k
  {
646
46.0k
    bfd_vma addr;
647
46.0k
    unsigned page = 0;
648
649
46.0k
    status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
650
46.0k
    if (status != 0)
651
35
      return status;
652
653
45.9k
    if (format & M6812_OP_IDX_P2)
654
289
      offset = -2;
655
45.6k
    else
656
45.6k
      offset = 0;
657
45.9k
    pos += 2;
658
659
45.9k
    addr = val = (buffer[0] << 8) | buffer[1];
660
45.9k
    pc_dst_offset = 2;
661
45.9k
    if (format & M6812_OP_PAGE)
662
453
      {
663
453
        status = read_memory (memaddr + pos + offset, buffer, 1, info);
664
453
        if (status != 0)
665
1
    return status;
666
667
452
        page = buffer[0];
668
452
        if (addr >= M68HC12_BANK_BASE && addr < 0x0c000)
669
145
    addr = (val - M68HC12_BANK_BASE + (page << M68HC12_BANK_SHIFT)
670
145
      + M68HC12_BANK_VIRT);
671
452
      }
672
45.5k
    else if ((arch & cpu6812)
673
45.5k
       && addr >= M68HC12_BANK_BASE && addr < 0x0c000)
674
6.18k
      {
675
6.18k
        unsigned cur_page;
676
6.18k
        bfd_vma vaddr;
677
678
6.18k
        if (memaddr >= M68HC12_BANK_VIRT)
679
1.13k
    cur_page = ((memaddr - M68HC12_BANK_VIRT)
680
1.13k
          >> M68HC12_BANK_SHIFT);
681
5.05k
        else
682
5.05k
    cur_page = 0;
683
684
6.18k
        vaddr = (addr - M68HC12_BANK_BASE
685
6.18k
           + (cur_page << M68HC12_BANK_SHIFT)) + M68HC12_BANK_VIRT;
686
6.18k
        if (!info->symbol_at_address_func (addr, info)
687
6.18k
      && info->symbol_at_address_func (vaddr, info))
688
0
    addr = vaddr;
689
6.18k
      }
690
45.9k
    if (format & M6811_OP_IMM16)
691
5.49k
      {
692
5.49k
        format &= ~M6811_OP_IMM16;
693
5.49k
        (*info->fprintf_func) (info->stream, "#");
694
5.49k
      }
695
40.4k
    else
696
40.4k
      {
697
40.4k
        format &= ~M6811_OP_IND16;
698
40.4k
      }
699
700
    /* Avoid duplicate 0x from core binutils.  */
701
45.9k
    if (info->symtab_size > 0)
702
0
      (*info->fprintf_func) (info->stream, "0x");
703
704
45.9k
    (*info->print_address_func) (addr, info);
705
45.9k
    if (format & M6812_OP_PAGE)
706
452
      {
707
452
        (* info->fprintf_func) (info->stream, " {");
708
        /* Avoid duplicate 0x from core binutils.  */
709
452
        if (info->symtab_size > 0)
710
0
    (*info->fprintf_func) (info->stream, "0x");
711
452
        (* info->print_address_func) (val, info);
712
452
        (* info->fprintf_func) (info->stream, ", 0x%x}", page);
713
452
        format &= ~M6812_OP_PAGE;
714
452
        pos += 1;
715
452
      }
716
45.9k
  }
717
718
238k
      if (format & M6812_OP_IDX_P2)
719
294
  {
720
294
    (*info->fprintf_func) (info->stream, ", ");
721
294
    status = print_indexed_operand (memaddr + pos + offset, info,
722
294
            0, 1, pc_dst_offset,
723
294
            memaddr + pos + offset + 1, arch);
724
294
    if (status < 0)
725
0
      return status;
726
294
    pos += status;
727
294
  }
728
729
238k
      if (format & M6812_OP_IND16_P2)
730
62
  {
731
62
    (*info->fprintf_func) (info->stream, ", ");
732
733
62
    status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
734
62
    if (status != 0)
735
0
      return status;
736
737
62
    pos += 2;
738
739
62
    val = (buffer[0] << 8) | buffer[1];
740
    /* Avoid duplicate 0x from core binutils.  */
741
62
    if (info->symtab_size > 0)
742
0
      (*info->fprintf_func) (info->stream, "0x");
743
62
    (*info->print_address_func) (val, info);
744
62
  }
745
746
      /* M6811_OP_BITMASK and M6811_OP_JUMP_REL must be treated separately
747
   and in that order.  The brset/brclr insn have a bitmask and then
748
   a relative branch offset.  */
749
238k
      if (format & M6811_OP_BITMASK)
750
6.89k
  {
751
6.89k
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
752
6.89k
    if (status != 0)
753
10
      return status;
754
755
6.88k
    pos++;
756
6.88k
    (*info->fprintf_func) (info->stream, ", #0x%02x%s",
757
6.88k
         buffer[0] & 0x0FF,
758
6.88k
         (format & M6811_OP_JUMP_REL ? ", " : ""));
759
6.88k
    format &= ~M6811_OP_BITMASK;
760
6.88k
  }
761
238k
      if (format & M6811_OP_JUMP_REL)
762
15.9k
  {
763
15.9k
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
764
15.9k
    if (status != 0)
765
12
      return status;
766
767
15.9k
    (*info->fprintf_func) (info->stream, "0x");
768
15.9k
    pos++;
769
15.9k
    val = (buffer[0] ^ 0x80) - 0x80;
770
15.9k
    (*info->print_address_func) (memaddr + pos + val, info);
771
15.9k
    format &= ~M6811_OP_JUMP_REL;
772
15.9k
  }
773
222k
      else if (format & M6812_OP_JUMP_REL16)
774
84
  {
775
84
    status = read_memory (memaddr + pos, &buffer[0], 2, info);
776
84
    if (status != 0)
777
2
      return status;
778
779
82
    pos += 2;
780
82
    val = (((buffer[0] << 8) | buffer[1]) ^ 0x8000) - 0x8000;
781
782
82
    (*info->fprintf_func) (info->stream, "0x");
783
82
    (*info->print_address_func) (memaddr + pos + val, info);
784
82
    format &= ~M6812_OP_JUMP_REL16;
785
82
  }
786
787
238k
      if (format & M6812_OP_PAGE)
788
340
  {
789
340
    status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
790
340
    if (status != 0)
791
1
      return status;
792
793
339
    pos += 1;
794
795
339
    val = buffer[0];
796
339
    (*info->fprintf_func) (info->stream, ", 0x%x", (unsigned) val);
797
339
  }
798
799
#ifdef DEBUG
800
      /* Consistency check.  'format' must be 0, so that we have handled
801
   all formats; and the computed size of the insn must match the
802
   opcode table content.  */
803
      if (format & ~(M6811_OP_PAGE4 | M6811_OP_PAGE3 | M6811_OP_PAGE2))
804
  (*info->fprintf_func) (info->stream, "; Error, format: %lx", format);
805
806
      if (pos != opcode->size)
807
  (*info->fprintf_func) (info->stream, "; Error, size: %ld expect %d",
808
             pos, opcode->size);
809
#endif
810
238k
      return pos;
811
238k
    }
812
813
  /* Opcode not recognized.  */
814
2.34k
  if (format == M6811_OP_PAGE2 && arch & cpu6812
815
2.34k
      && ((code >= 0x30 && code <= 0x39) || (code >= 0x40)))
816
164
    (*info->fprintf_func) (info->stream, "trap\t#0x%02x", code & 0x0ff);
817
818
2.17k
  else if (format == M6811_OP_PAGE2)
819
71
    (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
820
71
         M6811_OPCODE_PAGE2, code);
821
2.10k
  else if (format == M6811_OP_PAGE3)
822
259
    (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
823
259
         M6811_OPCODE_PAGE3, code);
824
1.84k
  else if (format == M6811_OP_PAGE4)
825
115
    (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
826
115
         M6811_OPCODE_PAGE4, code);
827
1.73k
  else
828
1.73k
    (*info->fprintf_func) (info->stream, ".byte\t0x%02x", code);
829
830
2.34k
  return pos;
831
241k
}
832
833
/* Disassemble one instruction at address 'memaddr'.  Returns the number
834
   of bytes used by that instruction.  */
835
int
836
print_insn_m68hc11 (bfd_vma memaddr, struct disassemble_info* info)
837
19.9k
{
838
19.9k
  return print_insn (memaddr, info, cpu6811);
839
19.9k
}
840
841
int
842
print_insn_m68hc12 (bfd_vma memaddr, struct disassemble_info* info)
843
135k
{
844
135k
  return print_insn (memaddr, info, cpu6812);
845
135k
}
846
847
int
848
print_insn_m9s12x (bfd_vma memaddr, struct disassemble_info* info)
849
86.4k
{
850
86.4k
  return print_insn (memaddr, info, cpu6812|cpu9s12x);
851
86.4k
}
852
853
int
854
print_insn_m9s12xg (bfd_vma memaddr, struct disassemble_info* info)
855
60.7k
{
856
60.7k
  return print_insn (memaddr, info, cpuxgate);
857
60.7k
}