Coverage Report

Created: 2023-08-28 06:31

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