33d1f0e9f47414e99a776b36144ecbbef63a126e
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));
189 sval
= buffer
[1] & 0x00ff;
190 if (buffer
[0] & 0x01)
194 (*info
->fprintf_func
) (info
->stream
, "%d,%s",
195 (int) sval
, reg_name
[reg
]);
196 if (reg
== PC_REGNUM
)
198 (* info
->fprintf_func
) (info
->stream
, " {");
199 (* info
->print_address_func
) (endaddr
+ sval
, info
);
200 (* info
->fprintf_func
) (info
->stream
, "}");
205 reg
= (buffer
[0] >> 3) & 0x03;
206 switch (buffer
[0] & 3)
209 (*info
->fprintf_func
) (info
->stream
, "A,%s", reg_name
[reg
]);
212 (*info
->fprintf_func
) (info
->stream
, "B,%s", reg_name
[reg
]);
215 (*info
->fprintf_func
) (info
->stream
, "D,%s", reg_name
[reg
]);
219 (*info
->fprintf_func
) (info
->stream
, "[D,%s]", reg_name
[reg
]);
229 /* Disassemble one instruction at address 'memaddr'. Returns the number
230 of bytes used by that instruction. */
232 print_insn (memaddr
, info
, arch
)
234 struct disassemble_info
*info
;
242 const struct m68hc11_opcode
*opcode
;
244 /* Get first byte. Only one at a time because we don't know the
246 status
= read_memory (memaddr
, buffer
, 1, info
);
256 /* Look for page2,3,4 opcodes. */
257 if (code
== M6811_OPCODE_PAGE2
)
260 format
= M6811_OP_PAGE2
;
262 else if (code
== M6811_OPCODE_PAGE3
&& arch
== cpu6811
)
265 format
= M6811_OP_PAGE3
;
267 else if (code
== M6811_OPCODE_PAGE4
&& arch
== cpu6811
)
270 format
= M6811_OP_PAGE4
;
273 /* We are in page2,3,4; get the real opcode. */
276 status
= read_memory (memaddr
+ pos
, &buffer
[1], 1, info
);
285 /* Look first for a 68HC12 alias. All of them are 2-bytes long and
286 in page 1. There is no operand to print. We read the second byte
287 only when we have a possible match. */
288 if ((arch
& cpu6812
) && format
== 0)
292 /* Walk the alias table to find a code1+code2 match. */
293 for (i
= 0; i
< m68hc12_num_alias
; i
++)
295 if (m68hc12_alias
[i
].code1
== code
)
299 status
= read_memory (memaddr
+ pos
+ 1,
300 &buffer
[1], 1, info
);
306 if (m68hc12_alias
[i
].code2
== (unsigned char) buffer
[1])
308 (*info
->fprintf_func
) (info
->stream
, "%s",
309 m68hc12_alias
[i
].name
);
318 /* Scan the opcode table until we find the opcode
319 with the corresponding page. */
320 opcode
= m68hc11_opcodes
;
321 for (i
= 0; i
< m68hc11_num_opcodes
; i
++, opcode
++)
327 if ((opcode
->arch
& arch
) == 0)
329 if (opcode
->opcode
!= code
)
331 if ((opcode
->format
& OP_PAGE_MASK
) != format
)
334 if (opcode
->format
& M6812_OP_REG
)
339 if (opcode
->format
& M6811_OP_JUMP_REL
)
344 status
= read_memory (memaddr
+ pos
, &buffer
[0], 1, info
);
349 for (j
= 0; i
+ j
< m68hc11_num_opcodes
; j
++)
351 if ((opcode
[j
].arch
& arch
) == 0)
353 if (opcode
[j
].opcode
!= code
)
357 if (!(opcode
[j
].format
& M6811_OP_JUMP_REL
))
360 if ((opcode
[j
].format
& M6812_OP_IBCC_MARKER
)
361 && (buffer
[0] & 0xc0) != 0x80)
363 if ((opcode
[j
].format
& M6812_OP_TBCC_MARKER
)
364 && (buffer
[0] & 0xc0) != 0x40)
366 if ((opcode
[j
].format
& M6812_OP_DBCC_MARKER
)
367 && (buffer
[0] & 0xc0) != 0)
369 if ((opcode
[j
].format
& M6812_OP_EQ_MARKER
)
370 && (buffer
[0] & 0x20) == 0)
372 if (!(opcode
[j
].format
& M6812_OP_EQ_MARKER
)
373 && (buffer
[0] & 0x20) != 0)
377 if (opcode
[j
].format
& M6812_OP_EXG_MARKER
&& buffer
[0] & 0x80)
379 if ((opcode
[j
].format
& M6812_OP_SEX_MARKER
)
380 && (((buffer
[0] & 0x07) >= 3 && (buffer
[0] & 7) <= 7))
381 && ((buffer
[0] & 0x0f0) <= 0x20))
383 if (opcode
[j
].format
& M6812_OP_TFR_MARKER
384 && !(buffer
[0] & 0x80))
387 if (i
+ j
< m68hc11_num_opcodes
)
391 /* We have found the opcode. Extract the operand and print it. */
392 (*info
->fprintf_func
) (info
->stream
, "%s", opcode
->name
);
394 format
= opcode
->format
;
395 if (format
& (M6811_OP_MASK
| M6811_OP_BITMASK
396 | M6811_OP_JUMP_REL
| M6812_OP_JUMP_REL16
))
398 (*info
->fprintf_func
) (info
->stream
, "\t");
401 /* The movb and movw must be handled in a special way...
402 The source constant 'ii' is not always at the same place.
403 This is the same for the destination for the post-indexed byte.
404 The 'offset' is used to do the appropriate correction.
407 for constant for destination
408 movb 18 OB ii hh ll 0 0
410 18 0C hh ll hh ll 0 0
415 movw 18 03 jj kk hh ll 0 0
417 18 04 hh ll hh ll 0 0
422 After the source operand is read, the position 'pos' is incremented
423 this explains the negative offset for destination.
425 movb/movw above are the only instructions with this matching
427 offset
= ((format
& M6812_OP_IDX_P2
)
428 && (format
& (M6811_OP_IMM8
| M6811_OP_IMM16
|
431 /* Operand with one more byte: - immediate, offset,
432 direct-low address. */
434 (M6811_OP_IMM8
| M6811_OP_IX
| M6811_OP_IY
| M6811_OP_DIRECT
))
436 status
= read_memory (memaddr
+ pos
+ offset
, &buffer
[0], 1, info
);
444 /* This movb/movw is special (see above). */
448 if (format
& M6811_OP_IMM8
)
450 (*info
->fprintf_func
) (info
->stream
, "#%d", (int) buffer
[0]);
451 format
&= ~M6811_OP_IMM8
;
452 /* Set PC destination offset. */
455 else if (format
& M6811_OP_IX
)
457 /* Offsets are in range 0..255, print them unsigned. */
458 (*info
->fprintf_func
) (info
->stream
, "%u,x", buffer
[0] & 0x0FF);
459 format
&= ~M6811_OP_IX
;
461 else if (format
& M6811_OP_IY
)
463 (*info
->fprintf_func
) (info
->stream
, "%u,y", buffer
[0] & 0x0FF);
464 format
&= ~M6811_OP_IY
;
466 else if (format
& M6811_OP_DIRECT
)
468 (*info
->fprintf_func
) (info
->stream
, "*");
469 (*info
->print_address_func
) (buffer
[0] & 0x0FF, info
);
470 format
&= ~M6811_OP_DIRECT
;
474 #define M6812_DST_MOVE (M6812_OP_IND16_P2 | M6812_OP_IDX_P2)
475 #define M6812_INDEXED_FLAGS (M6812_OP_IDX|M6812_OP_IDX_1|M6812_OP_IDX_2)
476 /* Analyze the 68HC12 indexed byte. */
477 if (format
& M6812_INDEXED_FLAGS
)
482 endaddr
= memaddr
+ pos
+ 1;
483 if (format
& M6811_OP_IND16
)
487 status
= print_indexed_operand (memaddr
+ pos
, info
, &indirect
,
488 (format
& M6812_DST_MOVE
),
489 pc_src_offset
, endaddr
);
496 /* The indirect addressing mode of the call instruction does
497 not need the page code. */
498 if ((format
& M6812_OP_PAGE
) && indirect
)
499 format
&= ~M6812_OP_PAGE
;
502 /* 68HC12 dbcc/ibcc/tbcc operands. */
503 if ((format
& M6812_OP_REG
) && (format
& M6811_OP_JUMP_REL
))
505 status
= read_memory (memaddr
+ pos
, &buffer
[0], 2, info
);
510 (*info
->fprintf_func
) (info
->stream
, "%s,",
511 reg_src_table
[buffer
[0] & 0x07]);
512 sval
= buffer
[1] & 0x0ff;
513 if (buffer
[0] & 0x10)
517 (*info
->print_address_func
) (memaddr
+ pos
+ sval
, info
);
518 format
&= ~(M6812_OP_REG
| M6811_OP_JUMP_REL
);
520 else if (format
& (M6812_OP_REG
| M6812_OP_REG_2
))
522 status
= read_memory (memaddr
+ pos
, &buffer
[0], 1, info
);
529 (*info
->fprintf_func
) (info
->stream
, "%s,%s",
530 reg_src_table
[(buffer
[0] >> 4) & 7],
531 reg_dst_table
[(buffer
[0] & 7)]);
534 if (format
& (M6811_OP_IMM16
| M6811_OP_IND16
))
540 status
= read_memory (memaddr
+ pos
+ offset
, &buffer
[0], 2, info
);
545 if (format
& M6812_OP_IDX_P2
)
551 val
= ((buffer
[0] << 8) | (buffer
[1] & 0x0FF));
555 if (format
& M6812_OP_PAGE
)
557 status
= read_memory (memaddr
+ pos
+ offset
, buffer
, 1, info
);
561 page
= (unsigned) buffer
[0];
562 if (addr
>= M68HC12_BANK_BASE
&& addr
< 0x0c000)
563 addr
= ((val
- M68HC12_BANK_BASE
)
564 | (page
<< M68HC12_BANK_SHIFT
))
567 else if ((arch
& cpu6812
)
568 && addr
>= M68HC12_BANK_BASE
&& addr
< 0x0c000)
573 if (memaddr
>= M68HC12_BANK_VIRT
)
574 cur_page
= ((memaddr
- M68HC12_BANK_VIRT
)
575 >> M68HC12_BANK_SHIFT
);
579 vaddr
= ((addr
- M68HC12_BANK_BASE
)
580 + (cur_page
<< M68HC12_BANK_SHIFT
))
582 if (!info
->symbol_at_address_func (addr
, info
)
583 && info
->symbol_at_address_func (vaddr
, info
))
586 if (format
& M6811_OP_IMM16
)
588 format
&= ~M6811_OP_IMM16
;
589 (*info
->fprintf_func
) (info
->stream
, "#");
592 format
&= ~M6811_OP_IND16
;
594 (*info
->print_address_func
) (addr
, info
);
595 if (format
& M6812_OP_PAGE
)
597 (* info
->fprintf_func
) (info
->stream
, " {");
598 (* info
->print_address_func
) (val
, info
);
599 (* info
->fprintf_func
) (info
->stream
, ", %d}", page
);
600 format
&= ~M6812_OP_PAGE
;
605 if (format
& M6812_OP_IDX_P2
)
607 (*info
->fprintf_func
) (info
->stream
, ", ");
608 status
= print_indexed_operand (memaddr
+ pos
+ offset
, info
,
610 memaddr
+ pos
+ offset
+ 1);
616 if (format
& M6812_OP_IND16_P2
)
620 (*info
->fprintf_func
) (info
->stream
, ", ");
622 status
= read_memory (memaddr
+ pos
+ offset
, &buffer
[0], 2, info
);
629 val
= ((buffer
[0] << 8) | (buffer
[1] & 0x0FF));
631 (*info
->print_address_func
) (val
, info
);
634 /* M6811_OP_BITMASK and M6811_OP_JUMP_REL must be treated separately
635 and in that order. The brset/brclr insn have a bitmask and then
636 a relative branch offset. */
637 if (format
& M6811_OP_BITMASK
)
639 status
= read_memory (memaddr
+ pos
, &buffer
[0], 1, info
);
645 (*info
->fprintf_func
) (info
->stream
, " #$%02x%s",
647 (format
& M6811_OP_JUMP_REL
? " " : ""));
648 format
&= ~M6811_OP_BITMASK
;
650 if (format
& M6811_OP_JUMP_REL
)
654 status
= read_memory (memaddr
+ pos
, &buffer
[0], 1, info
);
661 val
= (buffer
[0] & 0x80) ? buffer
[0] | 0xFFFFFF00 : buffer
[0];
662 (*info
->print_address_func
) (memaddr
+ pos
+ val
, info
);
663 format
&= ~M6811_OP_JUMP_REL
;
665 else if (format
& M6812_OP_JUMP_REL16
)
669 status
= read_memory (memaddr
+ pos
, &buffer
[0], 2, info
);
676 val
= ((buffer
[0] << 8) | (buffer
[1] & 0x0FF));
680 (*info
->print_address_func
) (memaddr
+ pos
+ val
, info
);
681 format
&= ~M6812_OP_JUMP_REL16
;
684 if (format
& M6812_OP_PAGE
)
688 status
= read_memory (memaddr
+ pos
+ offset
, &buffer
[0], 1, info
);
695 val
= buffer
[0] & 0x0ff;
696 (*info
->fprintf_func
) (info
->stream
, ", %d", val
);
700 /* Consistency check. 'format' must be 0, so that we have handled
701 all formats; and the computed size of the insn must match the
702 opcode table content. */
703 if (format
& ~(M6811_OP_PAGE4
| M6811_OP_PAGE3
| M6811_OP_PAGE2
))
705 (*info
->fprintf_func
) (info
->stream
, "; Error, format: %x", format
);
707 if (pos
!= opcode
->size
)
709 (*info
->fprintf_func
) (info
->stream
, "; Error, size: %d expect %d",
716 /* Opcode not recognized. */
717 if (format
== M6811_OP_PAGE2
&& arch
& cpu6812
718 && ((code
>= 0x30 && code
<= 0x39) || (code
>= 0x40 && code
<= 0xff)))
719 (*info
->fprintf_func
) (info
->stream
, "trap\t#%d", code
& 0x0ff);
721 else if (format
== M6811_OP_PAGE2
)
722 (*info
->fprintf_func
) (info
->stream
, ".byte\t0x%02x, 0x%02x",
723 M6811_OPCODE_PAGE2
, code
);
724 else if (format
== M6811_OP_PAGE3
)
725 (*info
->fprintf_func
) (info
->stream
, ".byte\t0x%02x, 0x%02x",
726 M6811_OPCODE_PAGE3
, code
);
727 else if (format
== M6811_OP_PAGE4
)
728 (*info
->fprintf_func
) (info
->stream
, ".byte\t0x%02x, 0x%02x",
729 M6811_OPCODE_PAGE4
, code
);
731 (*info
->fprintf_func
) (info
->stream
, ".byte\t0x%02x", code
);
736 /* Disassemble one instruction at address 'memaddr'. Returns the number
737 of bytes used by that instruction. */
739 print_insn_m68hc11 (memaddr
, info
)
741 struct disassemble_info
*info
;
743 return print_insn (memaddr
, info
, cpu6811
);
747 print_insn_m68hc12 (memaddr
, info
)
749 struct disassemble_info
*info
;
751 return print_insn (memaddr
, info
, cpu6812
);
This page took 0.045662 seconds and 4 git commands to generate.