1 /* m68hc11-dis.c -- Motorola 68HC11 & 68HC12 disassembly
2 Copyright 1999, 2000, 2001, 2002, 2003, 2006
3 Free Software Foundation, Inc.
4 Written by Stephane Carrez (stcarrez@nerim.fr)
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include "opcode/m68hc11.h"
28 static const char *const reg_name[] = {
32 static const char *const reg_src_table[] = {
33 "A", "B", "CCR", "TMP3", "D", "X", "Y", "SP"
36 static const char *const reg_dst_table[] = {
37 "A", "B", "CCR", "TMP2", "D", "X", "Y", "SP"
40 #define OP_PAGE_MASK (M6811_OP_PAGE2|M6811_OP_PAGE3|M6811_OP_PAGE4)
42 /* Prototypes for local functions. */
43 static int read_memory (bfd_vma, bfd_byte *, int, struct disassemble_info *);
44 static int print_indexed_operand (bfd_vma, struct disassemble_info *,
45 int*, int, int, bfd_vma);
46 static int print_insn (bfd_vma, struct disassemble_info *, int);
49 read_memory (bfd_vma memaddr, bfd_byte* buffer, int size,
50 struct disassemble_info* info)
54 /* Get first byte. Only one at a time because we don't know the
56 status = (*info->read_memory_func) (memaddr, buffer, size, info);
59 (*info->memory_error_func) (status, memaddr, info);
66 /* Read the 68HC12 indexed operand byte and print the corresponding mode.
67 Returns the number of bytes read or -1 if failure. */
69 print_indexed_operand (bfd_vma memaddr, struct disassemble_info* info,
70 int* indirect, int mov_insn, int pc_offset,
82 status = read_memory (memaddr, &buffer[0], 1, info);
88 /* n,r with 5-bits signed constant. */
89 if ((buffer[0] & 0x20) == 0)
91 reg = (buffer[0] >> 6) & 3;
92 sval = (buffer[0] & 0x1f);
95 /* 68HC12 requires an adjustment for movb/movw pc relative modes. */
96 if (reg == PC_REGNUM && info->mach == bfd_mach_m6812 && mov_insn)
98 (*info->fprintf_func) (info->stream, "%d,%s",
99 (int) sval, reg_name[reg]);
101 if (reg == PC_REGNUM)
103 (* info->fprintf_func) (info->stream, " {");
104 (* info->print_address_func) (endaddr + sval, info);
105 (* info->fprintf_func) (info->stream, "}");
109 /* Auto pre/post increment/decrement. */
110 else if ((buffer[0] & 0xc0) != 0xc0)
114 reg = (buffer[0] >> 6) & 3;
115 sval = (buffer[0] & 0x0f);
127 (*info->fprintf_func) (info->stream, "%d,%s%s%s",
129 (buffer[0] & 0x10 ? "" : mode),
130 reg_name[reg], (buffer[0] & 0x10 ? mode : ""));
133 /* [n,r] 16-bits offset indexed indirect. */
134 else if ((buffer[0] & 0x07) == 3)
138 (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
142 reg = (buffer[0] >> 3) & 0x03;
143 status = read_memory (memaddr + pos, &buffer[0], 2, info);
150 sval = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
151 (*info->fprintf_func) (info->stream, "[%u,%s]",
152 sval & 0x0ffff, reg_name[reg]);
157 /* n,r with 9 and 16 bit signed constant. */
158 else if ((buffer[0] & 0x4) == 0)
162 (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
166 reg = (buffer[0] >> 3) & 0x03;
167 status = read_memory (memaddr + pos,
168 &buffer[1], (buffer[0] & 0x2 ? 2 : 1), info);
175 sval = ((buffer[1] << 8) | (buffer[2] & 0x0FF));
182 sval = buffer[1] & 0x00ff;
183 if (buffer[0] & 0x01)
188 (*info->fprintf_func) (info->stream, "%d,%s",
189 (int) sval, reg_name[reg]);
190 if (reg == PC_REGNUM)
192 (* info->fprintf_func) (info->stream, " {");
193 (* info->print_address_func) (endaddr + sval, info);
194 (* info->fprintf_func) (info->stream, "}");
199 reg = (buffer[0] >> 3) & 0x03;
200 switch (buffer[0] & 3)
203 (*info->fprintf_func) (info->stream, "A,%s", reg_name[reg]);
206 (*info->fprintf_func) (info->stream, "B,%s", reg_name[reg]);
209 (*info->fprintf_func) (info->stream, "D,%s", reg_name[reg]);
213 (*info->fprintf_func) (info->stream, "[D,%s]", reg_name[reg]);
223 /* Disassemble one instruction at address 'memaddr'. Returns the number
224 of bytes used by that instruction. */
226 print_insn (bfd_vma memaddr, struct disassemble_info* info, int arch)
233 const struct m68hc11_opcode *opcode;
235 /* Get first byte. Only one at a time because we don't know the
237 status = read_memory (memaddr, buffer, 1, info);
247 /* Look for page2,3,4 opcodes. */
248 if (code == M6811_OPCODE_PAGE2)
251 format = M6811_OP_PAGE2;
253 else if (code == M6811_OPCODE_PAGE3 && arch == cpu6811)
256 format = M6811_OP_PAGE3;
258 else if (code == M6811_OPCODE_PAGE4 && arch == cpu6811)
261 format = M6811_OP_PAGE4;
264 /* We are in page2,3,4; get the real opcode. */
267 status = read_memory (memaddr + pos, &buffer[1], 1, info);
276 /* Look first for a 68HC12 alias. All of them are 2-bytes long and
277 in page 1. There is no operand to print. We read the second byte
278 only when we have a possible match. */
279 if ((arch & cpu6812) && format == 0)
283 /* Walk the alias table to find a code1+code2 match. */
284 for (i = 0; i < m68hc12_num_alias; i++)
286 if (m68hc12_alias[i].code1 == code)
290 status = read_memory (memaddr + pos + 1,
291 &buffer[1], 1, info);
297 if (m68hc12_alias[i].code2 == (unsigned char) buffer[1])
299 (*info->fprintf_func) (info->stream, "%s",
300 m68hc12_alias[i].name);
309 /* Scan the opcode table until we find the opcode
310 with the corresponding page. */
311 opcode = m68hc11_opcodes;
312 for (i = 0; i < m68hc11_num_opcodes; i++, opcode++)
316 int pc_dst_offset = 0;
318 if ((opcode->arch & arch) == 0)
320 if (opcode->opcode != code)
322 if ((opcode->format & OP_PAGE_MASK) != format)
325 if (opcode->format & M6812_OP_REG)
330 if (opcode->format & M6811_OP_JUMP_REL)
335 status = read_memory (memaddr + pos, &buffer[0], 1, info);
340 for (j = 0; i + j < m68hc11_num_opcodes; j++)
342 if ((opcode[j].arch & arch) == 0)
344 if (opcode[j].opcode != code)
348 if (!(opcode[j].format & M6811_OP_JUMP_REL))
351 if ((opcode[j].format & M6812_OP_IBCC_MARKER)
352 && (buffer[0] & 0xc0) != 0x80)
354 if ((opcode[j].format & M6812_OP_TBCC_MARKER)
355 && (buffer[0] & 0xc0) != 0x40)
357 if ((opcode[j].format & M6812_OP_DBCC_MARKER)
358 && (buffer[0] & 0xc0) != 0)
360 if ((opcode[j].format & M6812_OP_EQ_MARKER)
361 && (buffer[0] & 0x20) == 0)
363 if (!(opcode[j].format & M6812_OP_EQ_MARKER)
364 && (buffer[0] & 0x20) != 0)
368 if (opcode[j].format & M6812_OP_EXG_MARKER && buffer[0] & 0x80)
370 if ((opcode[j].format & M6812_OP_SEX_MARKER)
371 && (((buffer[0] & 0x07) >= 3 && (buffer[0] & 7) <= 7))
372 && ((buffer[0] & 0x0f0) <= 0x20))
374 if (opcode[j].format & M6812_OP_TFR_MARKER
375 && !(buffer[0] & 0x80))
378 if (i + j < m68hc11_num_opcodes)
382 /* We have found the opcode. Extract the operand and print it. */
383 (*info->fprintf_func) (info->stream, "%s", opcode->name);
385 format = opcode->format;
386 if (format & (M6811_OP_MASK | M6811_OP_BITMASK
387 | M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
389 (*info->fprintf_func) (info->stream, "\t");
392 /* The movb and movw must be handled in a special way...
393 The source constant 'ii' is not always at the same place.
394 This is the same for the destination for the post-indexed byte.
395 The 'offset' is used to do the appropriate correction.
398 for constant for destination
399 movb 18 OB ii hh ll 0 0
401 18 0C hh ll hh ll 0 0
406 movw 18 03 jj kk hh ll 0 0
408 18 04 hh ll hh ll 0 0
413 After the source operand is read, the position 'pos' is incremented
414 this explains the negative offset for destination.
416 movb/movw above are the only instructions with this matching
418 offset = ((format & M6812_OP_IDX_P2)
419 && (format & (M6811_OP_IMM8 | M6811_OP_IMM16 |
422 /* Operand with one more byte: - immediate, offset,
423 direct-low address. */
425 (M6811_OP_IMM8 | M6811_OP_IX | M6811_OP_IY | M6811_OP_DIRECT))
427 status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
435 /* This movb/movw is special (see above). */
439 if (format & M6811_OP_IMM8)
441 (*info->fprintf_func) (info->stream, "#%d", (int) buffer[0]);
442 format &= ~M6811_OP_IMM8;
443 /* Set PC destination offset. */
446 else if (format & M6811_OP_IX)
448 /* Offsets are in range 0..255, print them unsigned. */
449 (*info->fprintf_func) (info->stream, "%u,x", buffer[0] & 0x0FF);
450 format &= ~M6811_OP_IX;
452 else if (format & M6811_OP_IY)
454 (*info->fprintf_func) (info->stream, "%u,y", buffer[0] & 0x0FF);
455 format &= ~M6811_OP_IY;
457 else if (format & M6811_OP_DIRECT)
459 (*info->fprintf_func) (info->stream, "*");
460 (*info->print_address_func) (buffer[0] & 0x0FF, info);
461 format &= ~M6811_OP_DIRECT;
465 #define M6812_DST_MOVE (M6812_OP_IND16_P2 | M6812_OP_IDX_P2)
466 #define M6812_INDEXED_FLAGS (M6812_OP_IDX|M6812_OP_IDX_1|M6812_OP_IDX_2)
467 /* Analyze the 68HC12 indexed byte. */
468 if (format & M6812_INDEXED_FLAGS)
473 endaddr = memaddr + pos + 1;
474 if (format & M6811_OP_IND16)
478 status = print_indexed_operand (memaddr + pos, info, &indirect,
479 (format & M6812_DST_MOVE),
480 pc_src_offset, endaddr);
487 /* The indirect addressing mode of the call instruction does
488 not need the page code. */
489 if ((format & M6812_OP_PAGE) && indirect)
490 format &= ~M6812_OP_PAGE;
493 /* 68HC12 dbcc/ibcc/tbcc operands. */
494 if ((format & M6812_OP_REG) && (format & M6811_OP_JUMP_REL))
496 status = read_memory (memaddr + pos, &buffer[0], 2, info);
501 (*info->fprintf_func) (info->stream, "%s,",
502 reg_src_table[buffer[0] & 0x07]);
503 sval = buffer[1] & 0x0ff;
504 if (buffer[0] & 0x10)
508 (*info->print_address_func) (memaddr + pos + sval, info);
509 format &= ~(M6812_OP_REG | M6811_OP_JUMP_REL);
511 else if (format & (M6812_OP_REG | M6812_OP_REG_2))
513 status = read_memory (memaddr + pos, &buffer[0], 1, info);
520 (*info->fprintf_func) (info->stream, "%s,%s",
521 reg_src_table[(buffer[0] >> 4) & 7],
522 reg_dst_table[(buffer[0] & 7)]);
525 if (format & (M6811_OP_IMM16 | M6811_OP_IND16))
531 status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
536 if (format & M6812_OP_IDX_P2)
542 val = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
546 if (format & M6812_OP_PAGE)
548 status = read_memory (memaddr + pos + offset, buffer, 1, info);
552 page = (unsigned) buffer[0];
553 if (addr >= M68HC12_BANK_BASE && addr < 0x0c000)
554 addr = ((val - M68HC12_BANK_BASE)
555 | (page << M68HC12_BANK_SHIFT))
558 else if ((arch & cpu6812)
559 && addr >= M68HC12_BANK_BASE && addr < 0x0c000)
564 if (memaddr >= M68HC12_BANK_VIRT)
565 cur_page = ((memaddr - M68HC12_BANK_VIRT)
566 >> M68HC12_BANK_SHIFT);
570 vaddr = ((addr - M68HC12_BANK_BASE)
571 + (cur_page << M68HC12_BANK_SHIFT))
573 if (!info->symbol_at_address_func (addr, info)
574 && info->symbol_at_address_func (vaddr, info))
577 if (format & M6811_OP_IMM16)
579 format &= ~M6811_OP_IMM16;
580 (*info->fprintf_func) (info->stream, "#");
583 format &= ~M6811_OP_IND16;
585 (*info->print_address_func) (addr, info);
586 if (format & M6812_OP_PAGE)
588 (* info->fprintf_func) (info->stream, " {");
589 (* info->print_address_func) (val, info);
590 (* info->fprintf_func) (info->stream, ", %d}", page);
591 format &= ~M6812_OP_PAGE;
596 if (format & M6812_OP_IDX_P2)
598 (*info->fprintf_func) (info->stream, ", ");
599 status = print_indexed_operand (memaddr + pos + offset, info,
601 memaddr + pos + offset + 1);
607 if (format & M6812_OP_IND16_P2)
611 (*info->fprintf_func) (info->stream, ", ");
613 status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
620 val = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
622 (*info->print_address_func) (val, info);
625 /* M6811_OP_BITMASK and M6811_OP_JUMP_REL must be treated separately
626 and in that order. The brset/brclr insn have a bitmask and then
627 a relative branch offset. */
628 if (format & M6811_OP_BITMASK)
630 status = read_memory (memaddr + pos, &buffer[0], 1, info);
636 (*info->fprintf_func) (info->stream, " #$%02x%s",
638 (format & M6811_OP_JUMP_REL ? " " : ""));
639 format &= ~M6811_OP_BITMASK;
641 if (format & M6811_OP_JUMP_REL)
645 status = read_memory (memaddr + pos, &buffer[0], 1, info);
652 val = (buffer[0] & 0x80) ? buffer[0] | 0xFFFFFF00 : buffer[0];
653 (*info->print_address_func) (memaddr + pos + val, info);
654 format &= ~M6811_OP_JUMP_REL;
656 else if (format & M6812_OP_JUMP_REL16)
660 status = read_memory (memaddr + pos, &buffer[0], 2, info);
667 val = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
671 (*info->print_address_func) (memaddr + pos + val, info);
672 format &= ~M6812_OP_JUMP_REL16;
675 if (format & M6812_OP_PAGE)
679 status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
686 val = buffer[0] & 0x0ff;
687 (*info->fprintf_func) (info->stream, ", %d", val);
691 /* Consistency check. 'format' must be 0, so that we have handled
692 all formats; and the computed size of the insn must match the
693 opcode table content. */
694 if (format & ~(M6811_OP_PAGE4 | M6811_OP_PAGE3 | M6811_OP_PAGE2))
696 (*info->fprintf_func) (info->stream, "; Error, format: %lx", format);
698 if (pos != opcode->size)
700 (*info->fprintf_func) (info->stream, "; Error, size: %ld expect %d",
707 /* Opcode not recognized. */
708 if (format == M6811_OP_PAGE2 && arch & cpu6812
709 && ((code >= 0x30 && code <= 0x39) || (code >= 0x40)))
710 (*info->fprintf_func) (info->stream, "trap\t#%d", code & 0x0ff);
712 else if (format == M6811_OP_PAGE2)
713 (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
714 M6811_OPCODE_PAGE2, code);
715 else if (format == M6811_OP_PAGE3)
716 (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
717 M6811_OPCODE_PAGE3, code);
718 else if (format == M6811_OP_PAGE4)
719 (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
720 M6811_OPCODE_PAGE4, code);
722 (*info->fprintf_func) (info->stream, ".byte\t0x%02x", code);
727 /* Disassemble one instruction at address 'memaddr'. Returns the number
728 of bytes used by that instruction. */
730 print_insn_m68hc11 (bfd_vma memaddr, struct disassemble_info* info)
732 return print_insn (memaddr, info, cpu6811);
736 print_insn_m68hc12 (bfd_vma memaddr, struct disassemble_info* info)
738 return print_insn (memaddr, info, cpu6812);