Coverage Report

Created: 2026-03-10 08:46

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