1 /* m68hc11-dis.c -- Motorola 68HC11 & 68HC12 disassembly
2 Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 Written by Stephane Carrez (stcarrez@nerim.fr)
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "opcode/m68hc11.h"
27 static const char *const reg_name[] = {
31 static const char *const reg_src_table[] = {
32 "A", "B", "CCR", "TMP3", "D", "X", "Y", "SP"
35 static const char *const reg_dst_table[] = {
36 "A", "B", "CCR", "TMP2", "D", "X", "Y", "SP"
39 #define OP_PAGE_MASK (M6811_OP_PAGE2|M6811_OP_PAGE3|M6811_OP_PAGE4)
41 /* Prototypes for local functions. */
42 static int read_memory
43 PARAMS ((bfd_vma, bfd_byte *, int, struct disassemble_info *));
44 static int print_indexed_operand
45 PARAMS ((bfd_vma, struct disassemble_info *, int*, int, int, bfd_vma));
47 PARAMS ((bfd_vma, struct disassemble_info *, int));
50 read_memory (memaddr, buffer, size, info)
54 struct disassemble_info *info;
58 /* Get first byte. Only one at a time because we don't know the
60 status = (*info->read_memory_func) (memaddr, buffer, size, info);
63 (*info->memory_error_func) (status, memaddr, info);
70 /* Read the 68HC12 indexed operand byte and print the corresponding mode.
71 Returns the number of bytes read or -1 if failure. */
73 print_indexed_operand (memaddr, info, indirect, mov_insn, pc_offset, endaddr)
75 struct disassemble_info *info;
90 status = read_memory (memaddr, &buffer[0], 1, info);
96 /* n,r with 5-bits signed constant. */
97 if ((buffer[0] & 0x20) == 0)
99 reg = (buffer[0] >> 6) & 3;
100 sval = (buffer[0] & 0x1f);
103 /* 68HC12 requires an adjustment for movb/movw pc relative modes. */
104 if (reg == PC_REGNUM && info->mach == bfd_mach_m6812 && mov_insn)
106 (*info->fprintf_func) (info->stream, "%d,%s",
107 (int) sval, reg_name[reg]);
109 if (reg == PC_REGNUM)
111 (* info->fprintf_func) (info->stream, " {");
112 (* info->print_address_func) (endaddr + sval, info);
113 (* info->fprintf_func) (info->stream, "}");
117 /* Auto pre/post increment/decrement. */
118 else if ((buffer[0] & 0xc0) != 0xc0)
122 reg = (buffer[0] >> 6) & 3;
123 sval = (buffer[0] & 0x0f);
135 (*info->fprintf_func) (info->stream, "%d,%s%s%s",
137 (buffer[0] & 0x10 ? "" : mode),
138 reg_name[reg], (buffer[0] & 0x10 ? mode : ""));
141 /* [n,r] 16-bits offset indexed indirect. */
142 else if ((buffer[0] & 0x07) == 3)
146 (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
150 reg = (buffer[0] >> 3) & 0x03;
151 status = read_memory (memaddr + pos, &buffer[0], 2, info);
158 sval = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
159 (*info->fprintf_func) (info->stream, "[%u,%s]",
160 sval & 0x0ffff, reg_name[reg]);
165 /* n,r with 9 and 16 bit signed constant. */
166 else if ((buffer[0] & 0x4) == 0)
170 (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
174 reg = (buffer[0] >> 3) & 0x03;
175 status = read_memory (memaddr + pos,
176 &buffer[1], (buffer[0] & 0x2 ? 2 : 1), info);
183 sval = ((buffer[1] << 8) | (buffer[2] & 0x0FF));
190 sval = buffer[1] & 0x00ff;
191 if (buffer[0] & 0x01)
196 (*info->fprintf_func) (info->stream, "%d,%s",
197 (int) sval, reg_name[reg]);
198 if (reg == PC_REGNUM)
200 (* info->fprintf_func) (info->stream, " {");
201 (* info->print_address_func) (endaddr + sval, info);
202 (* info->fprintf_func) (info->stream, "}");
207 reg = (buffer[0] >> 3) & 0x03;
208 switch (buffer[0] & 3)
211 (*info->fprintf_func) (info->stream, "A,%s", reg_name[reg]);
214 (*info->fprintf_func) (info->stream, "B,%s", reg_name[reg]);
217 (*info->fprintf_func) (info->stream, "D,%s", reg_name[reg]);
221 (*info->fprintf_func) (info->stream, "[D,%s]", reg_name[reg]);
231 /* Disassemble one instruction at address 'memaddr'. Returns the number
232 of bytes used by that instruction. */
234 print_insn (memaddr, info, arch)
236 struct disassemble_info *info;
244 const struct m68hc11_opcode *opcode;
246 /* Get first byte. Only one at a time because we don't know the
248 status = read_memory (memaddr, buffer, 1, info);
258 /* Look for page2,3,4 opcodes. */
259 if (code == M6811_OPCODE_PAGE2)
262 format = M6811_OP_PAGE2;
264 else if (code == M6811_OPCODE_PAGE3 && arch == cpu6811)
267 format = M6811_OP_PAGE3;
269 else if (code == M6811_OPCODE_PAGE4 && arch == cpu6811)
272 format = M6811_OP_PAGE4;
275 /* We are in page2,3,4; get the real opcode. */
278 status = read_memory (memaddr + pos, &buffer[1], 1, info);
287 /* Look first for a 68HC12 alias. All of them are 2-bytes long and
288 in page 1. There is no operand to print. We read the second byte
289 only when we have a possible match. */
290 if ((arch & cpu6812) && format == 0)
294 /* Walk the alias table to find a code1+code2 match. */
295 for (i = 0; i < m68hc12_num_alias; i++)
297 if (m68hc12_alias[i].code1 == code)
301 status = read_memory (memaddr + pos + 1,
302 &buffer[1], 1, info);
308 if (m68hc12_alias[i].code2 == (unsigned char) buffer[1])
310 (*info->fprintf_func) (info->stream, "%s",
311 m68hc12_alias[i].name);
320 /* Scan the opcode table until we find the opcode
321 with the corresponding page. */
322 opcode = m68hc11_opcodes;
323 for (i = 0; i < m68hc11_num_opcodes; i++, opcode++)
329 if ((opcode->arch & arch) == 0)
331 if (opcode->opcode != code)
333 if ((opcode->format & OP_PAGE_MASK) != format)
336 if (opcode->format & M6812_OP_REG)
341 if (opcode->format & M6811_OP_JUMP_REL)
346 status = read_memory (memaddr + pos, &buffer[0], 1, info);
351 for (j = 0; i + j < m68hc11_num_opcodes; j++)
353 if ((opcode[j].arch & arch) == 0)
355 if (opcode[j].opcode != code)
359 if (!(opcode[j].format & M6811_OP_JUMP_REL))
362 if ((opcode[j].format & M6812_OP_IBCC_MARKER)
363 && (buffer[0] & 0xc0) != 0x80)
365 if ((opcode[j].format & M6812_OP_TBCC_MARKER)
366 && (buffer[0] & 0xc0) != 0x40)
368 if ((opcode[j].format & M6812_OP_DBCC_MARKER)
369 && (buffer[0] & 0xc0) != 0)
371 if ((opcode[j].format & M6812_OP_EQ_MARKER)
372 && (buffer[0] & 0x20) == 0)
374 if (!(opcode[j].format & M6812_OP_EQ_MARKER)
375 && (buffer[0] & 0x20) != 0)
379 if (opcode[j].format & M6812_OP_EXG_MARKER && buffer[0] & 0x80)
381 if ((opcode[j].format & M6812_OP_SEX_MARKER)
382 && (((buffer[0] & 0x07) >= 3 && (buffer[0] & 7) <= 7))
383 && ((buffer[0] & 0x0f0) <= 0x20))
385 if (opcode[j].format & M6812_OP_TFR_MARKER
386 && !(buffer[0] & 0x80))
389 if (i + j < m68hc11_num_opcodes)
393 /* We have found the opcode. Extract the operand and print it. */
394 (*info->fprintf_func) (info->stream, "%s", opcode->name);
396 format = opcode->format;
397 if (format & (M6811_OP_MASK | M6811_OP_BITMASK
398 | M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
400 (*info->fprintf_func) (info->stream, "\t");
403 /* The movb and movw must be handled in a special way...
404 The source constant 'ii' is not always at the same place.
405 This is the same for the destination for the post-indexed byte.
406 The 'offset' is used to do the appropriate correction.
409 for constant for destination
410 movb 18 OB ii hh ll 0 0
412 18 0C hh ll hh ll 0 0
417 movw 18 03 jj kk hh ll 0 0
419 18 04 hh ll hh ll 0 0
424 After the source operand is read, the position 'pos' is incremented
425 this explains the negative offset for destination.
427 movb/movw above are the only instructions with this matching
429 offset = ((format & M6812_OP_IDX_P2)
430 && (format & (M6811_OP_IMM8 | M6811_OP_IMM16 |
433 /* Operand with one more byte: - immediate, offset,
434 direct-low address. */
436 (M6811_OP_IMM8 | M6811_OP_IX | M6811_OP_IY | M6811_OP_DIRECT))
438 status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
446 /* This movb/movw is special (see above). */
450 if (format & M6811_OP_IMM8)
452 (*info->fprintf_func) (info->stream, "#%d", (int) buffer[0]);
453 format &= ~M6811_OP_IMM8;
454 /* Set PC destination offset. */
457 else if (format & M6811_OP_IX)
459 /* Offsets are in range 0..255, print them unsigned. */
460 (*info->fprintf_func) (info->stream, "%u,x", buffer[0] & 0x0FF);
461 format &= ~M6811_OP_IX;
463 else if (format & M6811_OP_IY)
465 (*info->fprintf_func) (info->stream, "%u,y", buffer[0] & 0x0FF);
466 format &= ~M6811_OP_IY;
468 else if (format & M6811_OP_DIRECT)
470 (*info->fprintf_func) (info->stream, "*");
471 (*info->print_address_func) (buffer[0] & 0x0FF, info);
472 format &= ~M6811_OP_DIRECT;
476 #define M6812_DST_MOVE (M6812_OP_IND16_P2 | M6812_OP_IDX_P2)
477 #define M6812_INDEXED_FLAGS (M6812_OP_IDX|M6812_OP_IDX_1|M6812_OP_IDX_2)
478 /* Analyze the 68HC12 indexed byte. */
479 if (format & M6812_INDEXED_FLAGS)
484 endaddr = memaddr + pos + 1;
485 if (format & M6811_OP_IND16)
489 status = print_indexed_operand (memaddr + pos, info, &indirect,
490 (format & M6812_DST_MOVE),
491 pc_src_offset, endaddr);
498 /* The indirect addressing mode of the call instruction does
499 not need the page code. */
500 if ((format & M6812_OP_PAGE) && indirect)
501 format &= ~M6812_OP_PAGE;
504 /* 68HC12 dbcc/ibcc/tbcc operands. */
505 if ((format & M6812_OP_REG) && (format & M6811_OP_JUMP_REL))
507 status = read_memory (memaddr + pos, &buffer[0], 2, info);
512 (*info->fprintf_func) (info->stream, "%s,",
513 reg_src_table[buffer[0] & 0x07]);
514 sval = buffer[1] & 0x0ff;
515 if (buffer[0] & 0x10)
519 (*info->print_address_func) (memaddr + pos + sval, info);
520 format &= ~(M6812_OP_REG | M6811_OP_JUMP_REL);
522 else if (format & (M6812_OP_REG | M6812_OP_REG_2))
524 status = read_memory (memaddr + pos, &buffer[0], 1, info);
531 (*info->fprintf_func) (info->stream, "%s,%s",
532 reg_src_table[(buffer[0] >> 4) & 7],
533 reg_dst_table[(buffer[0] & 7)]);
536 if (format & (M6811_OP_IMM16 | M6811_OP_IND16))
542 status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
547 if (format & M6812_OP_IDX_P2)
553 val = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
557 if (format & M6812_OP_PAGE)
559 status = read_memory (memaddr + pos + offset, buffer, 1, info);
563 page = (unsigned) buffer[0];
564 if (addr >= M68HC12_BANK_BASE && addr < 0x0c000)
565 addr = ((val - M68HC12_BANK_BASE)
566 | (page << M68HC12_BANK_SHIFT))
569 else if ((arch & cpu6812)
570 && addr >= M68HC12_BANK_BASE && addr < 0x0c000)
575 if (memaddr >= M68HC12_BANK_VIRT)
576 cur_page = ((memaddr - M68HC12_BANK_VIRT)
577 >> M68HC12_BANK_SHIFT);
581 vaddr = ((addr - M68HC12_BANK_BASE)
582 + (cur_page << M68HC12_BANK_SHIFT))
584 if (!info->symbol_at_address_func (addr, info)
585 && info->symbol_at_address_func (vaddr, info))
588 if (format & M6811_OP_IMM16)
590 format &= ~M6811_OP_IMM16;
591 (*info->fprintf_func) (info->stream, "#");
594 format &= ~M6811_OP_IND16;
596 (*info->print_address_func) (addr, info);
597 if (format & M6812_OP_PAGE)
599 (* info->fprintf_func) (info->stream, " {");
600 (* info->print_address_func) (val, info);
601 (* info->fprintf_func) (info->stream, ", %d}", page);
602 format &= ~M6812_OP_PAGE;
607 if (format & M6812_OP_IDX_P2)
609 (*info->fprintf_func) (info->stream, ", ");
610 status = print_indexed_operand (memaddr + pos + offset, info,
612 memaddr + pos + offset + 1);
618 if (format & M6812_OP_IND16_P2)
622 (*info->fprintf_func) (info->stream, ", ");
624 status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
631 val = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
633 (*info->print_address_func) (val, info);
636 /* M6811_OP_BITMASK and M6811_OP_JUMP_REL must be treated separately
637 and in that order. The brset/brclr insn have a bitmask and then
638 a relative branch offset. */
639 if (format & M6811_OP_BITMASK)
641 status = read_memory (memaddr + pos, &buffer[0], 1, info);
647 (*info->fprintf_func) (info->stream, " #$%02x%s",
649 (format & M6811_OP_JUMP_REL ? " " : ""));
650 format &= ~M6811_OP_BITMASK;
652 if (format & M6811_OP_JUMP_REL)
656 status = read_memory (memaddr + pos, &buffer[0], 1, info);
663 val = (buffer[0] & 0x80) ? buffer[0] | 0xFFFFFF00 : buffer[0];
664 (*info->print_address_func) (memaddr + pos + val, info);
665 format &= ~M6811_OP_JUMP_REL;
667 else if (format & M6812_OP_JUMP_REL16)
671 status = read_memory (memaddr + pos, &buffer[0], 2, info);
678 val = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
682 (*info->print_address_func) (memaddr + pos + val, info);
683 format &= ~M6812_OP_JUMP_REL16;
686 if (format & M6812_OP_PAGE)
690 status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
697 val = buffer[0] & 0x0ff;
698 (*info->fprintf_func) (info->stream, ", %d", val);
702 /* Consistency check. 'format' must be 0, so that we have handled
703 all formats; and the computed size of the insn must match the
704 opcode table content. */
705 if (format & ~(M6811_OP_PAGE4 | M6811_OP_PAGE3 | M6811_OP_PAGE2))
707 (*info->fprintf_func) (info->stream, "; Error, format: %x", format);
709 if (pos != opcode->size)
711 (*info->fprintf_func) (info->stream, "; Error, size: %d expect %d",
718 /* Opcode not recognized. */
719 if (format == M6811_OP_PAGE2 && arch & cpu6812
720 && ((code >= 0x30 && code <= 0x39) || (code >= 0x40 && code <= 0xff)))
721 (*info->fprintf_func) (info->stream, "trap\t#%d", code & 0x0ff);
723 else if (format == M6811_OP_PAGE2)
724 (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
725 M6811_OPCODE_PAGE2, code);
726 else if (format == M6811_OP_PAGE3)
727 (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
728 M6811_OPCODE_PAGE3, code);
729 else if (format == M6811_OP_PAGE4)
730 (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
731 M6811_OPCODE_PAGE4, code);
733 (*info->fprintf_func) (info->stream, ".byte\t0x%02x", code);
738 /* Disassemble one instruction at address 'memaddr'. Returns the number
739 of bytes used by that instruction. */
741 print_insn_m68hc11 (memaddr, info)
743 struct disassemble_info *info;
745 return print_insn (memaddr, info, cpu6811);
749 print_insn_m68hc12 (memaddr, info)
751 struct disassemble_info *info;
753 return print_insn (memaddr, info, cpu6812);