Coverage Report

Created: 2025-06-24 06:45

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