Coverage Report

Created: 2023-08-28 06:30

/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-2023 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
84.7k
#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
388k
#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
648k
{
53
648k
  int status;
54
55
  /* Get first byte.  Only one at a time because we don't know the
56
     size of the insn.  */
57
648k
  status = (*info->read_memory_func) (memaddr, buffer, size, info);
58
648k
  if (status != 0)
59
75
    {
60
75
      (*info->memory_error_func) (status, memaddr, info);
61
75
      return -1;
62
75
    }
63
648k
  return 0;
64
648k
}
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
54.4k
{
74
54.4k
  bfd_byte buffer[4];
75
54.4k
  int reg;
76
54.4k
  int status;
77
54.4k
  bfd_vma val;
78
54.4k
  int pos = 1;
79
80
54.4k
  if (indirect)
81
54.2k
    *indirect = 0;
82
83
54.4k
  status = read_memory (memaddr, &buffer[0], 1, info);
84
54.4k
  if (status != 0)
85
19
    {
86
19
      return status;
87
19
    }
88
89
  /* n,r with 5-bits signed constant.  */
90
54.3k
  if ((buffer[0] & 0x20) == 0)
91
27.3k
    {
92
27.3k
      reg = (buffer[0] >> 6) & 3;
93
27.3k
      val = ((buffer[0] & 0x1f) ^ 0x10) - 0x10;
94
      /* 68HC12 requires an adjustment for movb/movw pc relative modes.  */
95
27.3k
      if (reg == PC_REGNUM && info->mach == bfd_mach_m6812 && mov_insn)
96
0
  val += pc_offset;
97
27.3k
      (*info->fprintf_func) (info->stream, "0x%x,%s",
98
27.3k
           (unsigned) val & 0xffff, reg_name[reg]);
99
100
27.3k
      if (reg == PC_REGNUM)
101
5.18k
  {
102
5.18k
    (* info->fprintf_func) (info->stream, " {");
103
     /* Avoid duplicate 0x from core binutils.  */
104
5.18k
    if (info->symtab_size > 0)
105
0
      (*info->fprintf_func) (info->stream, "0x");
106
5.18k
    (* info->print_address_func) (endaddr + val, info);
107
5.18k
    (* info->fprintf_func) (info->stream, "}");
108
5.18k
  }
109
27.3k
    }
110
111
  /* Auto pre/post increment/decrement.  */
112
27.0k
  else if ((buffer[0] & 0xc0) != 0xc0)
113
20.5k
    {
114
20.5k
      const char *mode;
115
116
20.5k
      reg = (buffer[0] >> 6) & 3;
117
20.5k
      val = buffer[0] & 7;
118
20.5k
      if (buffer[0] & 8)
119
10.8k
  {
120
10.8k
    val = 8 - val;
121
10.8k
    mode = "-";
122
10.8k
  }
123
9.67k
      else
124
9.67k
  {
125
9.67k
    val = val + 1;
126
9.67k
    mode = "+";
127
9.67k
  }
128
20.5k
      (*info->fprintf_func) (info->stream, "%d,%s%s%s",
129
20.5k
           (unsigned) val,
130
20.5k
           buffer[0] & 0x10 ? "" : mode,
131
20.5k
           reg_name[reg], buffer[0] & 0x10 ? mode : "");
132
20.5k
    }
133
134
  /* [n,r] 16-bits offset indexed indirect.  */
135
6.50k
  else if ((buffer[0] & 0x07) == 3)
136
345
    {
137
345
      if ((mov_insn) && (!(arch & cpu9s12x)))
138
2
  {
139
2
    (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
140
2
         buffer[0] & 0x0ff);
141
2
    return 0;
142
2
  }
143
343
      reg = (buffer[0] >> 3) & 0x03;
144
343
      status = read_memory (memaddr + pos, &buffer[0], 2, info);
145
343
      if (status != 0)
146
1
  return status;
147
148
342
      pos += 2;
149
342
      val = (buffer[0] << 8) | buffer[1];
150
342
      (*info->fprintf_func) (info->stream, "[0x%x,%s]",
151
342
           (unsigned) val & 0xffff, reg_name[reg]);
152
342
      if (indirect)
153
342
  *indirect = 1;
154
342
    }
155
156
  /* n,r with 9 and 16 bit signed constant.  */
157
6.16k
  else if ((buffer[0] & 0x4) == 0)
158
2.70k
    {
159
2.70k
      if ((mov_insn) && (!(arch & cpu9s12x)))
160
38
  {
161
38
    (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
162
38
         buffer[0] & 0x0ff);
163
38
    return 0;
164
38
  }
165
166
2.66k
      reg = (buffer[0] >> 3) & 0x03;
167
2.66k
      status = read_memory (memaddr + pos,
168
2.66k
          &buffer[1], (buffer[0] & 0x2 ? 2 : 1), info);
169
2.66k
      if (status != 0)
170
1
  return status;
171
172
2.66k
      if (buffer[0] & 2)
173
947
  {
174
947
    val = (((buffer[1] << 8) | buffer[2]) ^ 0x8000) - 0x8000;
175
947
    pos += 2;
176
947
    endaddr += 2;
177
947
  }
178
1.71k
      else
179
1.71k
  {
180
1.71k
    val = buffer[1] - ((buffer[0] & 1) << 8);
181
1.71k
    pos++;
182
1.71k
    endaddr++;
183
1.71k
  }
184
2.66k
      (*info->fprintf_func) (info->stream, "0x%x,%s",
185
2.66k
           (unsigned) val & 0xffff, reg_name[reg]);
186
2.66k
      if (reg == PC_REGNUM)
187
931
  {
188
931
    (* info->fprintf_func) (info->stream, " {0x");
189
931
    (* info->print_address_func) (endaddr + val, info);
190
931
    (* info->fprintf_func) (info->stream, "}");
191
931
  }
192
2.66k
    }
193
3.46k
  else
194
3.46k
    {
195
3.46k
      reg = (buffer[0] >> 3) & 0x03;
196
3.46k
      switch (buffer[0] & 3)
197
3.46k
  {
198
573
  case 0:
199
573
    (*info->fprintf_func) (info->stream, "A,%s", reg_name[reg]);
200
573
    break;
201
488
  case 1:
202
488
    (*info->fprintf_func) (info->stream, "B,%s", reg_name[reg]);
203
488
    break;
204
1.11k
  case 2:
205
1.11k
    (*info->fprintf_func) (info->stream, "D,%s", reg_name[reg]);
206
1.11k
    break;
207
1.28k
  case 3:
208
1.28k
  default:
209
1.28k
    (*info->fprintf_func) (info->stream, "[D,%s]", reg_name[reg]);
210
1.28k
    if (indirect)
211
1.28k
      *indirect = 1;
212
1.28k
    break;
213
3.46k
  }
214
3.46k
    }
215
216
54.3k
  return pos;
217
54.3k
}
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
371k
{
224
371k
  int status;
225
371k
  bfd_byte buffer[4];
226
371k
  unsigned int code;
227
371k
  long format, pos, i;
228
371k
  bfd_vma val;
229
371k
  const struct m68hc11_opcode *opcode;
230
231
371k
  if (arch & cpuxgate)
232
0
    {
233
      /* Get two bytes as all XGATE instructions are 16bit.  */
234
0
      status = read_memory (memaddr, buffer, 2, info);
235
0
      if (status != 0)
236
0
  return status;
237
238
0
      format = 0;
239
0
      code = (buffer[0] << 8) + buffer[1];
240
241
      /* Scan the opcode table until we find the opcode
242
   with the corresponding page.  */
243
0
      opcode = m68hc11_opcodes;
244
0
      for (i = 0; i < m68hc11_num_opcodes; i++, opcode++)
245
0
  {
246
0
    if ((opcode->opcode != (code & opcode->xg_mask)) || (opcode->arch != cpuxgate))
247
0
        continue;
248
    /* We have found the opcode.  Extract the operand and print it.  */
249
0
    (*info->fprintf_func) (info->stream, "%s", opcode->name);
250
0
    format = opcode->format;
251
0
    if (format & (M68XG_OP_NONE))
252
0
      {
253
        /* Nothing to print.  */
254
0
      }
255
0
    else if (format & M68XG_OP_IMM3)
256
0
      (*info->fprintf_func) (info->stream, " #0x%x", (code >> 8) & 0x7);
257
0
    else if (format & M68XG_OP_R_R)
258
0
      (*info->fprintf_func) (info->stream, " R%x, R%x",
259
0
           (code >> 8) & 0x7, (code >> 5) & 0x7);
260
0
    else if (format & M68XG_OP_R_R_R)
261
0
      (*info->fprintf_func) (info->stream, " R%x, R%x, R%x",
262
0
           (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
263
0
    else if (format & M68XG_OP_RD_RB_RI)
264
0
      (*info->fprintf_func) (info->stream, " R%x, (R%x, R%x)",
265
0
           (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
266
0
    else if (format & M68XG_OP_RD_RB_RIp)
267
0
      (*info->fprintf_func) (info->stream, " R%x, (R%x, R%x+)",
268
0
           (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
269
0
    else if (format & M68XG_OP_RD_RB_mRI)
270
0
      (*info->fprintf_func) (info->stream, " R%x, (R%x, -R%x)",
271
0
           (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
272
0
    else if (format & M68XG_OP_R_R_OFFS5)
273
0
      (*info->fprintf_func) (info->stream, " R%x, (R%x, #0x%x)",
274
0
           (code >> 8) & 0x7, (code >> 5) & 0x7, code & 0x1f);
275
0
    else if (format & M68XG_OP_R_IMM8)
276
0
      (*info->fprintf_func) (info->stream, " R%x, #0x%02x",
277
0
           (code >> 8) & 0x7, code & 0xff);
278
0
    else if (format & M68XG_OP_R_IMM4)
279
0
      (*info->fprintf_func) (info->stream, " R%x, #0x%x",
280
0
           (code >> 8) & 0x7, (code & 0xf0) >> 4);
281
0
    else if (format & M68XG_OP_REL9)
282
0
      {
283
0
        (*info->fprintf_func) (info->stream, " 0x");
284
0
        val = buffer[1] - ((buffer[0] & 1) << 8);
285
0
        (*info->print_address_func) (memaddr + (val << 1) + 2, info);
286
0
      }
287
0
    else if (format & M68XG_OP_REL10)
288
0
      {
289
0
        (*info->fprintf_func) (info->stream, " 0x");
290
0
        val = (buffer[0] << 8) | buffer[1];
291
0
        val = ((val & 0x3ff) ^ 0x200) - 0x200;
292
0
        (*info->print_address_func) (memaddr + (val << 1) + 2, info);
293
0
      }
294
0
    else if ((code & 0x00ff) == 0x00f8)
295
0
        (*info->fprintf_func) (info->stream, " R%x, CCR", (code >> 8) & 0x7);
296
0
    else if ((code & 0x00ff) == 0x00f9)
297
0
        (*info->fprintf_func) (info->stream, " CCR, R%x", (code >> 8) & 0x7);
298
0
    else if ((code & 0x00ff) == 0x0)
299
0
        (*info->fprintf_func) (info->stream, " R%x, PC", (code >> 8) & 0x7);
300
0
    else if (format & M68XG_OP_R)
301
0
        {
302
        /* Special cases for TFR.  */
303
0
        if ((code & 0xf8ff) == 0x00f8)
304
0
    (*info->fprintf_func) (info->stream, " R%x, CCR", (code >> 8) & 0x7);
305
0
        else if ((code & 0xf8ff) == 0x00f9)
306
0
    (*info->fprintf_func) (info->stream, " CCR, R%x", (code >> 8) & 0x7);
307
0
        else if ((code & 0xf8ff) == 0x00fa)
308
0
    (*info->fprintf_func) (info->stream, " R%x, PC",  (code >> 8) & 0x7);
309
0
        else
310
0
    (*info->fprintf_func) (info->stream, " R%x", (code >> 8) & 0x7);
311
0
      }
312
0
    else
313
      /* Opcode not recognized.  */
314
0
      (*info->fprintf_func) (info->stream, "Not yet handled TEST .byte\t0x%04x", code);
315
0
    return 2;
316
0
  }
317
318
      /* Opcode not recognized.  */
319
0
      (*info->fprintf_func) (info->stream, ".byte\t0x%04x", code);
320
0
      return 2; /* Everything is two bytes.  */
321
0
    }
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
371k
  status = read_memory (memaddr, buffer, 1, info);
328
371k
  if (status != 0)
329
0
    return status;
330
331
371k
  format = 0;
332
371k
  code = buffer[0];
333
371k
  pos = 0;
334
335
  /* Look for page2,3,4 opcodes.  */
336
371k
  if (code == M6811_OPCODE_PAGE2)
337
986
    {
338
986
      pos++;
339
986
      format = M6811_OP_PAGE2;
340
986
    }
341
370k
  else if (code == M6811_OPCODE_PAGE3 && arch == cpu6811)
342
3
    {
343
3
      pos++;
344
3
      format = M6811_OP_PAGE3;
345
3
    }
346
370k
  else if (code == M6811_OPCODE_PAGE4 && arch == cpu6811)
347
18
    {
348
18
      pos++;
349
18
      format = M6811_OP_PAGE4;
350
18
    }
351
352
  /* We are in page2,3,4; get the real opcode.  */
353
371k
  if (pos == 1)
354
1.00k
    {
355
1.00k
      status = read_memory (memaddr + pos, &buffer[1], 1, info);
356
1.00k
      if (status != 0)
357
1
  return status;
358
359
1.00k
      code = buffer[1];
360
1.00k
    }
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
371k
  if ((arch & cpu6812) && format == 0)
366
358k
    {
367
358k
      int must_read = 1;
368
369
      /* Walk the alias table to find a code1+code2 match.  */
370
6.81M
      for (i = 0; i < m68hc12_num_alias; i++)
371
6.46M
  {
372
6.46M
    if (m68hc12_alias[i].code1 == code)
373
24.9k
      {
374
24.9k
        if (must_read)
375
24.9k
    {
376
24.9k
      status = read_memory (memaddr + pos + 1,
377
24.9k
          &buffer[1], 1, info);
378
24.9k
      if (status != 0)
379
5
        break;
380
381
24.9k
      must_read = 1;
382
24.9k
    }
383
24.9k
        if (m68hc12_alias[i].code2 == (unsigned char) buffer[1])
384
79
    {
385
79
      (*info->fprintf_func) (info->stream, "%s",
386
79
           m68hc12_alias[i].name);
387
79
      return 2;
388
79
    }
389
24.9k
      }
390
6.46M
  }
391
358k
    }
392
393
371k
  pos++;
394
395
  /* Scan the opcode table until we find the opcode
396
     with the corresponding page.  */
397
371k
  opcode = m68hc11_opcodes;
398
177M
  for (i = 0; i < m68hc11_num_opcodes; i++, opcode++)
399
177M
    {
400
177M
      int offset;
401
177M
      int pc_src_offset;
402
177M
      int pc_dst_offset = 0;
403
404
177M
      if ((opcode->arch & arch) == 0)
405
91.3M
  continue;
406
86.4M
      if (opcode->opcode != code)
407
86.0M
  continue;
408
388k
      if ((opcode->format & OP_PAGE_MASK) != format)
409
19.0k
  continue;
410
411
369k
      if (opcode->format & M6812_OP_REG)
412
3.72k
  {
413
3.72k
    int j;
414
3.72k
    int is_jump;
415
416
3.72k
    if (opcode->format & M6811_OP_JUMP_REL)
417
2.65k
      is_jump = 1;
418
1.06k
    else
419
1.06k
      is_jump = 0;
420
421
3.72k
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
422
3.72k
    if (status != 0)
423
1
      {
424
1
        return status;
425
1
      }
426
947k
    for (j = 0; i + j < m68hc11_num_opcodes; j++)
427
947k
      {
428
947k
        if ((opcode[j].arch & arch) == 0)
429
485k
    continue;
430
462k
        if (opcode[j].opcode != code)
431
451k
    continue;
432
10.1k
        if (is_jump)
433
8.27k
    {
434
8.27k
      if (!(opcode[j].format & M6811_OP_JUMP_REL))
435
881
        continue;
436
437
7.38k
      if ((opcode[j].format & M6812_OP_IBCC_MARKER)
438
7.38k
          && (buffer[0] & 0xc0) != 0x80)
439
1.76k
        continue;
440
5.62k
      if ((opcode[j].format & M6812_OP_TBCC_MARKER)
441
5.62k
          && (buffer[0] & 0xc0) != 0x40)
442
666
        continue;
443
4.96k
      if ((opcode[j].format & M6812_OP_DBCC_MARKER)
444
4.96k
          && (buffer[0] & 0xc0) != 0)
445
2.26k
        continue;
446
2.69k
      if ((opcode[j].format & M6812_OP_EQ_MARKER)
447
2.69k
          && (buffer[0] & 0x20) == 0)
448
1.94k
        break;
449
754
      if (!(opcode[j].format & M6812_OP_EQ_MARKER)
450
754
          && (buffer[0] & 0x20) != 0)
451
377
        break;
452
377
      continue;
453
754
    }
454
1.85k
        if (opcode[j].format & M6812_OP_EXG_MARKER && buffer[0] & 0x80)
455
650
    break;
456
1.20k
        if ((opcode[j].format & M6812_OP_SEX_MARKER)
457
1.20k
      && (((buffer[0] & 0x07) >= 3 && (buffer[0] & 7) <= 7))
458
1.20k
      && ((buffer[0] & 0x0f0) <= 0x20))
459
45
    break;
460
1.16k
        if ((opcode[j].format & M6812_OP_SEX_MARKER)
461
1.16k
      && (arch & cpu9s12x)
462
1.16k
      && ((buffer[0] == 0x4d) || (buffer[0] == 0x4e)))
463
0
    break;
464
1.16k
        if (opcode[j].format & M6812_OP_TFR_MARKER
465
1.16k
      && !(buffer[0] & 0x80))
466
373
    break;
467
1.16k
      }
468
3.72k
    if (i + j < m68hc11_num_opcodes)
469
3.38k
      opcode = &opcode[j];
470
3.72k
  }
471
472
      /* We have found the opcode.  Extract the operand and print it.  */
473
369k
      (*info->fprintf_func) (info->stream, "%s", opcode->name);
474
475
369k
      format = opcode->format;
476
369k
      if (format & (M6811_OP_MASK | M6811_OP_BITMASK
477
369k
        | M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
478
219k
  {
479
219k
    (*info->fprintf_func) (info->stream, "\t");
480
219k
  }
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
369k
      offset = ((format & M6812_OP_IDX_P2)
511
369k
    && (format & (M6811_OP_IMM8 | M6811_OP_IMM16 |
512
137
            M6811_OP_IND16)));
513
514
369k
      if (offset)
515
135
  {
516
    /* Check xb to see position of data.  */
517
135
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
518
135
    if (status != 0)
519
0
      {
520
0
        return status;
521
0
      }
522
523
135
    if (((buffer[0] & 0xe0) == 0xe0) && ((buffer[0] & 0x04) == 0))
524
2
      {
525
        /* 9 or 16 bit.  */
526
2
        if ((buffer[0] & 0x02) == 0)
527
0
    {
528
      /* 9 bit.  */
529
0
      offset = 2;
530
0
    }
531
2
        else
532
2
    {
533
      /* 16 bit.  */
534
2
      offset = 3;
535
2
    }
536
2
      }
537
135
  }
538
539
      /* Operand with one more byte: - immediate, offset,
540
   direct-low address.  */
541
369k
      if (format &
542
369k
    (M6811_OP_IMM8 | M6811_OP_IX | M6811_OP_IY | M6811_OP_DIRECT))
543
71.6k
  {
544
71.6k
    status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
545
71.6k
    if (status != 0)
546
8
      return status;
547
548
    /* This movb/movw is special (see above).  */
549
71.6k
    if (offset < 2)
550
71.6k
      {
551
71.6k
        offset = -offset;
552
71.6k
        pc_dst_offset = 2;
553
71.6k
      }
554
0
    else
555
0
      {
556
0
        offset = -1;
557
0
        pc_dst_offset = 5;
558
0
      }
559
71.6k
    pos++;
560
561
71.6k
    if (format & M6811_OP_IMM8)
562
30.0k
      {
563
30.0k
        (*info->fprintf_func) (info->stream, "#0x%x", (int) buffer[0]);
564
30.0k
        format &= ~M6811_OP_IMM8;
565
        /* Set PC destination offset.  */
566
30.0k
        pc_dst_offset = 1;
567
30.0k
      }
568
41.6k
    else if (format & M6811_OP_IX)
569
525
      {
570
        /* Offsets are in range 0..255, print them unsigned.  */
571
525
        (*info->fprintf_func) (info->stream, "0x%x,x", buffer[0] & 0x0FF);
572
525
        format &= ~M6811_OP_IX;
573
525
      }
574
41.0k
    else if (format & M6811_OP_IY)
575
2
      {
576
2
        (*info->fprintf_func) (info->stream, "0x%x,y", buffer[0] & 0x0FF);
577
2
        format &= ~M6811_OP_IY;
578
2
      }
579
41.0k
    else if (format & M6811_OP_DIRECT)
580
41.0k
      {
581
41.0k
        (*info->fprintf_func) (info->stream, "*");
582
41.0k
        if (info->symtab_size > 0) /* Avoid duplicate 0x. */
583
0
    (*info->fprintf_func) (info->stream, "0x");
584
41.0k
        (*info->print_address_func) (buffer[0] & 0x0FF, info);
585
41.0k
        format &= ~M6811_OP_DIRECT;
586
41.0k
      }
587
71.6k
  }
588
589
369k
#define M6812_DST_MOVE  (M6812_OP_IND16_P2 | M6812_OP_IDX_P2)
590
369k
#define M6812_INDEXED_FLAGS (M6812_OP_IDX|M6812_OP_IDX_1|M6812_OP_IDX_2)
591
      /* Analyze the 68HC12 indexed byte.  */
592
369k
      if (format & M6812_INDEXED_FLAGS)
593
54.2k
  {
594
54.2k
    int indirect;
595
54.2k
    bfd_vma endaddr;
596
597
54.2k
    endaddr = memaddr + pos + 1;
598
54.2k
    if (format & M6811_OP_IND16)
599
0
      endaddr += 2;
600
54.2k
    pc_src_offset = -1;
601
54.2k
    pc_dst_offset = 1;
602
54.2k
    status = print_indexed_operand (memaddr + pos, info, &indirect,
603
54.2k
            (format & M6812_DST_MOVE),
604
54.2k
            pc_src_offset, endaddr, arch);
605
54.2k
    if (status < 0)
606
21
      return status;
607
608
54.2k
    pos += status;
609
610
    /* The indirect addressing mode of the call instruction does
611
       not need the page code.  */
612
54.2k
    if ((format & M6812_OP_PAGE) && indirect)
613
31
      format &= ~M6812_OP_PAGE;
614
54.2k
  }
615
616
      /* 68HC12 dbcc/ibcc/tbcc operands.  */
617
369k
      if ((format & M6812_OP_REG) && (format & M6811_OP_JUMP_REL))
618
2.65k
  {
619
2.65k
    status = read_memory (memaddr + pos, &buffer[0], 2, info);
620
2.65k
    if (status != 0)
621
2
      return status;
622
623
2.65k
    (*info->fprintf_func) (info->stream, "%s,",
624
2.65k
         reg_src_table[buffer[0] & 0x07]);
625
2.65k
    val = buffer[1] - ((buffer[0] & 0x10) << 4);
626
627
2.65k
    pos += 2;
628
2.65k
    (*info->fprintf_func) (info->stream, "0x");
629
2.65k
    (*info->print_address_func) (memaddr + pos + val, info);
630
2.65k
    format &= ~(M6812_OP_REG | M6811_OP_JUMP_REL);
631
2.65k
  }
632
366k
      else if (format & (M6812_OP_REG | M6812_OP_REG_2))
633
1.06k
  {
634
1.06k
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
635
1.06k
    if (status != 0)
636
0
      return status;
637
638
1.06k
    pos++;
639
1.06k
    (*info->fprintf_func) (info->stream, "%s,%s",
640
1.06k
         reg_src_table[(buffer[0] >> 4) & 7],
641
1.06k
         reg_dst_table[(buffer[0] & 7)]);
642
1.06k
  }
643
644
369k
      if (format & (M6811_OP_IMM16 | M6811_OP_IND16))
645
72.5k
  {
646
72.5k
    bfd_vma addr;
647
72.5k
    unsigned page = 0;
648
649
72.5k
    status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
650
72.5k
    if (status != 0)
651
27
      return status;
652
653
72.5k
    if (format & M6812_OP_IDX_P2)
654
133
      offset = -2;
655
72.3k
    else
656
72.3k
      offset = 0;
657
72.5k
    pos += 2;
658
659
72.5k
    addr = val = (buffer[0] << 8) | buffer[1];
660
72.5k
    pc_dst_offset = 2;
661
72.5k
    if (format & M6812_OP_PAGE)
662
742
      {
663
742
        status = read_memory (memaddr + pos + offset, buffer, 1, info);
664
742
        if (status != 0)
665
0
    return status;
666
667
742
        page = buffer[0];
668
742
        if (addr >= M68HC12_BANK_BASE && addr < 0x0c000)
669
161
    addr = (val - M68HC12_BANK_BASE + (page << M68HC12_BANK_SHIFT)
670
161
      + M68HC12_BANK_VIRT);
671
742
      }
672
71.7k
    else if ((arch & cpu6812)
673
71.7k
       && addr >= M68HC12_BANK_BASE && addr < 0x0c000)
674
13.2k
      {
675
13.2k
        unsigned cur_page;
676
13.2k
        bfd_vma vaddr;
677
678
13.2k
        if (memaddr >= M68HC12_BANK_VIRT)
679
12.8k
    cur_page = ((memaddr - M68HC12_BANK_VIRT)
680
12.8k
          >> M68HC12_BANK_SHIFT);
681
400
        else
682
400
    cur_page = 0;
683
684
13.2k
        vaddr = (addr - M68HC12_BANK_BASE
685
13.2k
           + (cur_page << M68HC12_BANK_SHIFT)) + M68HC12_BANK_VIRT;
686
13.2k
        if (!info->symbol_at_address_func (addr, info)
687
13.2k
      && info->symbol_at_address_func (vaddr, info))
688
0
    addr = vaddr;
689
13.2k
      }
690
72.5k
    if (format & M6811_OP_IMM16)
691
9.42k
      {
692
9.42k
        format &= ~M6811_OP_IMM16;
693
9.42k
        (*info->fprintf_func) (info->stream, "#");
694
9.42k
      }
695
63.0k
    else
696
63.0k
      {
697
63.0k
        format &= ~M6811_OP_IND16;
698
63.0k
      }
699
700
    /* Avoid duplicate 0x from core binutils.  */
701
72.5k
    if (info->symtab_size > 0)
702
0
      (*info->fprintf_func) (info->stream, "0x");
703
704
72.5k
    (*info->print_address_func) (addr, info);
705
72.5k
    if (format & M6812_OP_PAGE)
706
742
      {
707
742
        (* info->fprintf_func) (info->stream, " {");
708
        /* Avoid duplicate 0x from core binutils.  */
709
742
        if (info->symtab_size > 0)
710
0
    (*info->fprintf_func) (info->stream, "0x");
711
742
        (* info->print_address_func) (val, info);
712
742
        (* info->fprintf_func) (info->stream, ", 0x%x}", page);
713
742
        format &= ~M6812_OP_PAGE;
714
742
        pos += 1;
715
742
      }
716
72.5k
  }
717
718
369k
      if (format & M6812_OP_IDX_P2)
719
137
  {
720
137
    (*info->fprintf_func) (info->stream, ", ");
721
137
    status = print_indexed_operand (memaddr + pos + offset, info,
722
137
            0, 1, pc_dst_offset,
723
137
            memaddr + pos + offset + 1, arch);
724
137
    if (status < 0)
725
0
      return status;
726
137
    pos += status;
727
137
  }
728
729
369k
      if (format & M6812_OP_IND16_P2)
730
109
  {
731
109
    (*info->fprintf_func) (info->stream, ", ");
732
733
109
    status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
734
109
    if (status != 0)
735
1
      return status;
736
737
108
    pos += 2;
738
739
108
    val = (buffer[0] << 8) | buffer[1];
740
    /* Avoid duplicate 0x from core binutils.  */
741
108
    if (info->symtab_size > 0)
742
0
      (*info->fprintf_func) (info->stream, "0x");
743
108
    (*info->print_address_func) (val, info);
744
108
  }
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
369k
      if (format & M6811_OP_BITMASK)
750
15.8k
  {
751
15.8k
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
752
15.8k
    if (status != 0)
753
1
      return status;
754
755
15.8k
    pos++;
756
15.8k
    (*info->fprintf_func) (info->stream, ", #0x%02x%s",
757
15.8k
         buffer[0] & 0x0FF,
758
15.8k
         (format & M6811_OP_JUMP_REL ? ", " : ""));
759
15.8k
    format &= ~M6811_OP_BITMASK;
760
15.8k
  }
761
369k
      if (format & M6811_OP_JUMP_REL)
762
24.9k
  {
763
24.9k
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
764
24.9k
    if (status != 0)
765
7
      return status;
766
767
24.9k
    (*info->fprintf_func) (info->stream, "0x");
768
24.9k
    pos++;
769
24.9k
    val = (buffer[0] ^ 0x80) - 0x80;
770
24.9k
    (*info->print_address_func) (memaddr + pos + val, info);
771
24.9k
    format &= ~M6811_OP_JUMP_REL;
772
24.9k
  }
773
344k
      else if (format & M6812_OP_JUMP_REL16)
774
11
  {
775
11
    status = read_memory (memaddr + pos, &buffer[0], 2, info);
776
11
    if (status != 0)
777
0
      return status;
778
779
11
    pos += 2;
780
11
    val = (((buffer[0] << 8) | buffer[1]) ^ 0x8000) - 0x8000;
781
782
11
    (*info->fprintf_func) (info->stream, "0x");
783
11
    (*info->print_address_func) (memaddr + pos + val, info);
784
11
    format &= ~M6812_OP_JUMP_REL16;
785
11
  }
786
787
369k
      if (format & M6812_OP_PAGE)
788
691
  {
789
691
    status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
790
691
    if (status != 0)
791
1
      return status;
792
793
690
    pos += 1;
794
795
690
    val = buffer[0];
796
690
    (*info->fprintf_func) (info->stream, ", 0x%x", (unsigned) val);
797
690
  }
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
369k
      return pos;
811
369k
    }
812
813
  /* Opcode not recognized.  */
814
1.87k
  if (format == M6811_OP_PAGE2 && arch & cpu6812
815
1.87k
      && ((code >= 0x30 && code <= 0x39) || (code >= 0x40)))
816
611
    (*info->fprintf_func) (info->stream, "trap\t#0x%02x", code & 0x0ff);
817
818
1.26k
  else if (format == M6811_OP_PAGE2)
819
25
    (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
820
25
         M6811_OPCODE_PAGE2, code);
821
1.23k
  else if (format == M6811_OP_PAGE3)
822
2
    (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
823
2
         M6811_OPCODE_PAGE3, code);
824
1.23k
  else if (format == M6811_OP_PAGE4)
825
18
    (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
826
18
         M6811_OPCODE_PAGE4, code);
827
1.21k
  else
828
1.21k
    (*info->fprintf_func) (info->stream, ".byte\t0x%02x", code);
829
830
1.87k
  return pos;
831
371k
}
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
11.2k
{
838
11.2k
  return print_insn (memaddr, info, cpu6811);
839
11.2k
}
840
841
int
842
print_insn_m68hc12 (bfd_vma memaddr, struct disassemble_info* info)
843
359k
{
844
359k
  return print_insn (memaddr, info, cpu6812);
845
359k
}
846
847
int
848
print_insn_m9s12x (bfd_vma memaddr, struct disassemble_info* info)
849
0
{
850
0
  return print_insn (memaddr, info, cpu6812|cpu9s12x);
851
0
}
852
853
int
854
print_insn_m9s12xg (bfd_vma memaddr, struct disassemble_info* info)
855
0
{
856
0
  return print_insn (memaddr, info, cpuxgate);
857
0
}