daily update
[binutils.git] / opcodes / m68hc11-dis.c
blob9860013c2d7893f5c4c403a07b6d33e3917fc2a9
1 /* m68hc11-dis.c -- Motorola 68HC11 & 68HC12 disassembly
2 Copyright 1999, 2000, 2001, 2002, 2003 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
19 #include <stdio.h>
21 #include "ansidecl.h"
22 #include "opcode/m68hc11.h"
23 #include "dis-asm.h"
25 #define PC_REGNUM 3
27 static const char *const reg_name[] = {
28 "X", "Y", "SP", "PC"
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 (bfd_vma, bfd_byte *, int, struct disassemble_info *);
43 static int print_indexed_operand (bfd_vma, struct disassemble_info *,
44 int*, int, int, bfd_vma);
45 static int print_insn (bfd_vma, struct disassemble_info *, int);
47 static int
48 read_memory (bfd_vma memaddr, bfd_byte* buffer, int size,
49 struct disassemble_info* info)
51 int status;
53 /* Get first byte. Only one at a time because we don't know the
54 size of the insn. */
55 status = (*info->read_memory_func) (memaddr, buffer, size, info);
56 if (status != 0)
58 (*info->memory_error_func) (status, memaddr, info);
59 return -1;
61 return 0;
65 /* Read the 68HC12 indexed operand byte and print the corresponding mode.
66 Returns the number of bytes read or -1 if failure. */
67 static int
68 print_indexed_operand (bfd_vma memaddr, struct disassemble_info* info,
69 int* indirect, int mov_insn, int pc_offset,
70 bfd_vma endaddr)
72 bfd_byte buffer[4];
73 int reg;
74 int status;
75 short sval;
76 int pos = 1;
78 if (indirect)
79 *indirect = 0;
81 status = read_memory (memaddr, &buffer[0], 1, info);
82 if (status != 0)
84 return status;
87 /* n,r with 5-bits signed constant. */
88 if ((buffer[0] & 0x20) == 0)
90 reg = (buffer[0] >> 6) & 3;
91 sval = (buffer[0] & 0x1f);
92 if (sval & 0x10)
93 sval |= 0xfff0;
94 /* 68HC12 requires an adjustment for movb/movw pc relative modes. */
95 if (reg == PC_REGNUM && info->mach == bfd_mach_m6812 && mov_insn)
96 sval += pc_offset;
97 (*info->fprintf_func) (info->stream, "%d,%s",
98 (int) sval, reg_name[reg]);
100 if (reg == PC_REGNUM)
102 (* info->fprintf_func) (info->stream, " {");
103 (* info->print_address_func) (endaddr + sval, info);
104 (* info->fprintf_func) (info->stream, "}");
108 /* Auto pre/post increment/decrement. */
109 else if ((buffer[0] & 0xc0) != 0xc0)
111 const char *mode;
113 reg = (buffer[0] >> 6) & 3;
114 sval = (buffer[0] & 0x0f);
115 if (sval & 0x8)
117 sval |= 0xfff0;
118 sval = -sval;
119 mode = "-";
121 else
123 sval = sval + 1;
124 mode = "+";
126 (*info->fprintf_func) (info->stream, "%d,%s%s%s",
127 (int) sval,
128 (buffer[0] & 0x10 ? "" : mode),
129 reg_name[reg], (buffer[0] & 0x10 ? mode : ""));
132 /* [n,r] 16-bits offset indexed indirect. */
133 else if ((buffer[0] & 0x07) == 3)
135 if (mov_insn)
137 (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
138 buffer[0] & 0x0ff);
139 return 0;
141 reg = (buffer[0] >> 3) & 0x03;
142 status = read_memory (memaddr + pos, &buffer[0], 2, info);
143 if (status != 0)
145 return status;
148 pos += 2;
149 sval = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
150 (*info->fprintf_func) (info->stream, "[%u,%s]",
151 sval & 0x0ffff, reg_name[reg]);
152 if (indirect)
153 *indirect = 1;
156 /* n,r with 9 and 16 bit signed constant. */
157 else if ((buffer[0] & 0x4) == 0)
159 if (mov_insn)
161 (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
162 buffer[0] & 0x0ff);
163 return 0;
165 reg = (buffer[0] >> 3) & 0x03;
166 status = read_memory (memaddr + pos,
167 &buffer[1], (buffer[0] & 0x2 ? 2 : 1), info);
168 if (status != 0)
170 return status;
172 if (buffer[0] & 2)
174 sval = ((buffer[1] << 8) | (buffer[2] & 0x0FF));
175 sval &= 0x0FFFF;
176 pos += 2;
177 endaddr += 2;
179 else
181 sval = buffer[1] & 0x00ff;
182 if (buffer[0] & 0x01)
183 sval |= 0xff00;
184 pos++;
185 endaddr++;
187 (*info->fprintf_func) (info->stream, "%d,%s",
188 (int) sval, reg_name[reg]);
189 if (reg == PC_REGNUM)
191 (* info->fprintf_func) (info->stream, " {");
192 (* info->print_address_func) (endaddr + sval, info);
193 (* info->fprintf_func) (info->stream, "}");
196 else
198 reg = (buffer[0] >> 3) & 0x03;
199 switch (buffer[0] & 3)
201 case 0:
202 (*info->fprintf_func) (info->stream, "A,%s", reg_name[reg]);
203 break;
204 case 1:
205 (*info->fprintf_func) (info->stream, "B,%s", reg_name[reg]);
206 break;
207 case 2:
208 (*info->fprintf_func) (info->stream, "D,%s", reg_name[reg]);
209 break;
210 case 3:
211 default:
212 (*info->fprintf_func) (info->stream, "[D,%s]", reg_name[reg]);
213 if (indirect)
214 *indirect = 1;
215 break;
219 return pos;
222 /* Disassemble one instruction at address 'memaddr'. Returns the number
223 of bytes used by that instruction. */
224 static int
225 print_insn (bfd_vma memaddr, struct disassemble_info* info, int arch)
227 int status;
228 bfd_byte buffer[4];
229 unsigned char code;
230 long format, pos, i;
231 short sval;
232 const struct m68hc11_opcode *opcode;
234 /* Get first byte. Only one at a time because we don't know the
235 size of the insn. */
236 status = read_memory (memaddr, buffer, 1, info);
237 if (status != 0)
239 return status;
242 format = 0;
243 code = buffer[0];
244 pos = 0;
246 /* Look for page2,3,4 opcodes. */
247 if (code == M6811_OPCODE_PAGE2)
249 pos++;
250 format = M6811_OP_PAGE2;
252 else if (code == M6811_OPCODE_PAGE3 && arch == cpu6811)
254 pos++;
255 format = M6811_OP_PAGE3;
257 else if (code == M6811_OPCODE_PAGE4 && arch == cpu6811)
259 pos++;
260 format = M6811_OP_PAGE4;
263 /* We are in page2,3,4; get the real opcode. */
264 if (pos == 1)
266 status = read_memory (memaddr + pos, &buffer[1], 1, info);
267 if (status != 0)
269 return status;
271 code = buffer[1];
275 /* Look first for a 68HC12 alias. All of them are 2-bytes long and
276 in page 1. There is no operand to print. We read the second byte
277 only when we have a possible match. */
278 if ((arch & cpu6812) && format == 0)
280 int must_read = 1;
282 /* Walk the alias table to find a code1+code2 match. */
283 for (i = 0; i < m68hc12_num_alias; i++)
285 if (m68hc12_alias[i].code1 == code)
287 if (must_read)
289 status = read_memory (memaddr + pos + 1,
290 &buffer[1], 1, info);
291 if (status != 0)
292 break;
294 must_read = 1;
296 if (m68hc12_alias[i].code2 == (unsigned char) buffer[1])
298 (*info->fprintf_func) (info->stream, "%s",
299 m68hc12_alias[i].name);
300 return 2;
306 pos++;
308 /* Scan the opcode table until we find the opcode
309 with the corresponding page. */
310 opcode = m68hc11_opcodes;
311 for (i = 0; i < m68hc11_num_opcodes; i++, opcode++)
313 int offset;
314 int pc_src_offset;
315 int pc_dst_offset = 0;
317 if ((opcode->arch & arch) == 0)
318 continue;
319 if (opcode->opcode != code)
320 continue;
321 if ((opcode->format & OP_PAGE_MASK) != format)
322 continue;
324 if (opcode->format & M6812_OP_REG)
326 int j;
327 int is_jump;
329 if (opcode->format & M6811_OP_JUMP_REL)
330 is_jump = 1;
331 else
332 is_jump = 0;
334 status = read_memory (memaddr + pos, &buffer[0], 1, info);
335 if (status != 0)
337 return status;
339 for (j = 0; i + j < m68hc11_num_opcodes; j++)
341 if ((opcode[j].arch & arch) == 0)
342 continue;
343 if (opcode[j].opcode != code)
344 continue;
345 if (is_jump)
347 if (!(opcode[j].format & M6811_OP_JUMP_REL))
348 continue;
350 if ((opcode[j].format & M6812_OP_IBCC_MARKER)
351 && (buffer[0] & 0xc0) != 0x80)
352 continue;
353 if ((opcode[j].format & M6812_OP_TBCC_MARKER)
354 && (buffer[0] & 0xc0) != 0x40)
355 continue;
356 if ((opcode[j].format & M6812_OP_DBCC_MARKER)
357 && (buffer[0] & 0xc0) != 0)
358 continue;
359 if ((opcode[j].format & M6812_OP_EQ_MARKER)
360 && (buffer[0] & 0x20) == 0)
361 break;
362 if (!(opcode[j].format & M6812_OP_EQ_MARKER)
363 && (buffer[0] & 0x20) != 0)
364 break;
365 continue;
367 if (opcode[j].format & M6812_OP_EXG_MARKER && buffer[0] & 0x80)
368 break;
369 if ((opcode[j].format & M6812_OP_SEX_MARKER)
370 && (((buffer[0] & 0x07) >= 3 && (buffer[0] & 7) <= 7))
371 && ((buffer[0] & 0x0f0) <= 0x20))
372 break;
373 if (opcode[j].format & M6812_OP_TFR_MARKER
374 && !(buffer[0] & 0x80))
375 break;
377 if (i + j < m68hc11_num_opcodes)
378 opcode = &opcode[j];
381 /* We have found the opcode. Extract the operand and print it. */
382 (*info->fprintf_func) (info->stream, "%s", opcode->name);
384 format = opcode->format;
385 if (format & (M6811_OP_MASK | M6811_OP_BITMASK
386 | M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
388 (*info->fprintf_func) (info->stream, "\t");
391 /* The movb and movw must be handled in a special way...
392 The source constant 'ii' is not always at the same place.
393 This is the same for the destination for the post-indexed byte.
394 The 'offset' is used to do the appropriate correction.
396 offset offset
397 for constant for destination
398 movb 18 OB ii hh ll 0 0
399 18 08 xb ii 1 -1
400 18 0C hh ll hh ll 0 0
401 18 09 xb hh ll 1 -1
402 18 0D xb hh ll 0 0
403 18 0A xb xb 0 0
405 movw 18 03 jj kk hh ll 0 0
406 18 00 xb jj kk 1 -1
407 18 04 hh ll hh ll 0 0
408 18 01 xb hh ll 1 -1
409 18 05 xb hh ll 0 0
410 18 02 xb xb 0 0
412 After the source operand is read, the position 'pos' is incremented
413 this explains the negative offset for destination.
415 movb/movw above are the only instructions with this matching
416 format. */
417 offset = ((format & M6812_OP_IDX_P2)
418 && (format & (M6811_OP_IMM8 | M6811_OP_IMM16 |
419 M6811_OP_IND16)));
421 /* Operand with one more byte: - immediate, offset,
422 direct-low address. */
423 if (format &
424 (M6811_OP_IMM8 | M6811_OP_IX | M6811_OP_IY | M6811_OP_DIRECT))
426 status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
427 if (status != 0)
429 return status;
432 pos++;
434 /* This movb/movw is special (see above). */
435 offset = -offset;
437 pc_dst_offset = 2;
438 if (format & M6811_OP_IMM8)
440 (*info->fprintf_func) (info->stream, "#%d", (int) buffer[0]);
441 format &= ~M6811_OP_IMM8;
442 /* Set PC destination offset. */
443 pc_dst_offset = 1;
445 else if (format & M6811_OP_IX)
447 /* Offsets are in range 0..255, print them unsigned. */
448 (*info->fprintf_func) (info->stream, "%u,x", buffer[0] & 0x0FF);
449 format &= ~M6811_OP_IX;
451 else if (format & M6811_OP_IY)
453 (*info->fprintf_func) (info->stream, "%u,y", buffer[0] & 0x0FF);
454 format &= ~M6811_OP_IY;
456 else if (format & M6811_OP_DIRECT)
458 (*info->fprintf_func) (info->stream, "*");
459 (*info->print_address_func) (buffer[0] & 0x0FF, info);
460 format &= ~M6811_OP_DIRECT;
464 #define M6812_DST_MOVE (M6812_OP_IND16_P2 | M6812_OP_IDX_P2)
465 #define M6812_INDEXED_FLAGS (M6812_OP_IDX|M6812_OP_IDX_1|M6812_OP_IDX_2)
466 /* Analyze the 68HC12 indexed byte. */
467 if (format & M6812_INDEXED_FLAGS)
469 int indirect;
470 bfd_vma endaddr;
472 endaddr = memaddr + pos + 1;
473 if (format & M6811_OP_IND16)
474 endaddr += 2;
475 pc_src_offset = -1;
476 pc_dst_offset = 1;
477 status = print_indexed_operand (memaddr + pos, info, &indirect,
478 (format & M6812_DST_MOVE),
479 pc_src_offset, endaddr);
480 if (status < 0)
482 return status;
484 pos += status;
486 /* The indirect addressing mode of the call instruction does
487 not need the page code. */
488 if ((format & M6812_OP_PAGE) && indirect)
489 format &= ~M6812_OP_PAGE;
492 /* 68HC12 dbcc/ibcc/tbcc operands. */
493 if ((format & M6812_OP_REG) && (format & M6811_OP_JUMP_REL))
495 status = read_memory (memaddr + pos, &buffer[0], 2, info);
496 if (status != 0)
498 return status;
500 (*info->fprintf_func) (info->stream, "%s,",
501 reg_src_table[buffer[0] & 0x07]);
502 sval = buffer[1] & 0x0ff;
503 if (buffer[0] & 0x10)
504 sval |= 0xff00;
506 pos += 2;
507 (*info->print_address_func) (memaddr + pos + sval, info);
508 format &= ~(M6812_OP_REG | M6811_OP_JUMP_REL);
510 else if (format & (M6812_OP_REG | M6812_OP_REG_2))
512 status = read_memory (memaddr + pos, &buffer[0], 1, info);
513 if (status != 0)
515 return status;
518 pos++;
519 (*info->fprintf_func) (info->stream, "%s,%s",
520 reg_src_table[(buffer[0] >> 4) & 7],
521 reg_dst_table[(buffer[0] & 7)]);
524 if (format & (M6811_OP_IMM16 | M6811_OP_IND16))
526 int val;
527 bfd_vma addr;
528 unsigned page = 0;
530 status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
531 if (status != 0)
533 return status;
535 if (format & M6812_OP_IDX_P2)
536 offset = -2;
537 else
538 offset = 0;
539 pos += 2;
541 val = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
542 val &= 0x0FFFF;
543 addr = val;
544 pc_dst_offset = 2;
545 if (format & M6812_OP_PAGE)
547 status = read_memory (memaddr + pos + offset, buffer, 1, info);
548 if (status != 0)
549 return status;
551 page = (unsigned) buffer[0];
552 if (addr >= M68HC12_BANK_BASE && addr < 0x0c000)
553 addr = ((val - M68HC12_BANK_BASE)
554 | (page << M68HC12_BANK_SHIFT))
555 + M68HC12_BANK_VIRT;
557 else if ((arch & cpu6812)
558 && addr >= M68HC12_BANK_BASE && addr < 0x0c000)
560 int cur_page;
561 bfd_vma vaddr;
563 if (memaddr >= M68HC12_BANK_VIRT)
564 cur_page = ((memaddr - M68HC12_BANK_VIRT)
565 >> M68HC12_BANK_SHIFT);
566 else
567 cur_page = 0;
569 vaddr = ((addr - M68HC12_BANK_BASE)
570 + (cur_page << M68HC12_BANK_SHIFT))
571 + M68HC12_BANK_VIRT;
572 if (!info->symbol_at_address_func (addr, info)
573 && info->symbol_at_address_func (vaddr, info))
574 addr = vaddr;
576 if (format & M6811_OP_IMM16)
578 format &= ~M6811_OP_IMM16;
579 (*info->fprintf_func) (info->stream, "#");
581 else
582 format &= ~M6811_OP_IND16;
584 (*info->print_address_func) (addr, info);
585 if (format & M6812_OP_PAGE)
587 (* info->fprintf_func) (info->stream, " {");
588 (* info->print_address_func) (val, info);
589 (* info->fprintf_func) (info->stream, ", %d}", page);
590 format &= ~M6812_OP_PAGE;
591 pos += 1;
595 if (format & M6812_OP_IDX_P2)
597 (*info->fprintf_func) (info->stream, ", ");
598 status = print_indexed_operand (memaddr + pos + offset, info,
599 0, 1, pc_dst_offset,
600 memaddr + pos + offset + 1);
601 if (status < 0)
602 return status;
603 pos += status;
606 if (format & M6812_OP_IND16_P2)
608 int val;
610 (*info->fprintf_func) (info->stream, ", ");
612 status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
613 if (status != 0)
615 return status;
617 pos += 2;
619 val = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
620 val &= 0x0FFFF;
621 (*info->print_address_func) (val, info);
624 /* M6811_OP_BITMASK and M6811_OP_JUMP_REL must be treated separately
625 and in that order. The brset/brclr insn have a bitmask and then
626 a relative branch offset. */
627 if (format & M6811_OP_BITMASK)
629 status = read_memory (memaddr + pos, &buffer[0], 1, info);
630 if (status != 0)
632 return status;
634 pos++;
635 (*info->fprintf_func) (info->stream, " #$%02x%s",
636 buffer[0] & 0x0FF,
637 (format & M6811_OP_JUMP_REL ? " " : ""));
638 format &= ~M6811_OP_BITMASK;
640 if (format & M6811_OP_JUMP_REL)
642 int val;
644 status = read_memory (memaddr + pos, &buffer[0], 1, info);
645 if (status != 0)
647 return status;
650 pos++;
651 val = (buffer[0] & 0x80) ? buffer[0] | 0xFFFFFF00 : buffer[0];
652 (*info->print_address_func) (memaddr + pos + val, info);
653 format &= ~M6811_OP_JUMP_REL;
655 else if (format & M6812_OP_JUMP_REL16)
657 int val;
659 status = read_memory (memaddr + pos, &buffer[0], 2, info);
660 if (status != 0)
662 return status;
665 pos += 2;
666 val = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
667 if (val & 0x8000)
668 val |= 0xffff0000;
670 (*info->print_address_func) (memaddr + pos + val, info);
671 format &= ~M6812_OP_JUMP_REL16;
674 if (format & M6812_OP_PAGE)
676 int val;
678 status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
679 if (status != 0)
681 return status;
683 pos += 1;
685 val = buffer[0] & 0x0ff;
686 (*info->fprintf_func) (info->stream, ", %d", val);
689 #ifdef DEBUG
690 /* Consistency check. 'format' must be 0, so that we have handled
691 all formats; and the computed size of the insn must match the
692 opcode table content. */
693 if (format & ~(M6811_OP_PAGE4 | M6811_OP_PAGE3 | M6811_OP_PAGE2))
695 (*info->fprintf_func) (info->stream, "; Error, format: %x", format);
697 if (pos != opcode->size)
699 (*info->fprintf_func) (info->stream, "; Error, size: %d expect %d",
700 pos, opcode->size);
702 #endif
703 return pos;
706 /* Opcode not recognized. */
707 if (format == M6811_OP_PAGE2 && arch & cpu6812
708 && ((code >= 0x30 && code <= 0x39) || (code >= 0x40)))
709 (*info->fprintf_func) (info->stream, "trap\t#%d", code & 0x0ff);
711 else if (format == M6811_OP_PAGE2)
712 (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
713 M6811_OPCODE_PAGE2, code);
714 else if (format == M6811_OP_PAGE3)
715 (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
716 M6811_OPCODE_PAGE3, code);
717 else if (format == M6811_OP_PAGE4)
718 (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
719 M6811_OPCODE_PAGE4, code);
720 else
721 (*info->fprintf_func) (info->stream, ".byte\t0x%02x", code);
723 return pos;
726 /* Disassemble one instruction at address 'memaddr'. Returns the number
727 of bytes used by that instruction. */
729 print_insn_m68hc11 (bfd_vma memaddr, struct disassemble_info* info)
731 return print_insn (memaddr, info, cpu6811);
735 print_insn_m68hc12 (bfd_vma memaddr, struct disassemble_info* info)
737 return print_insn (memaddr, info, cpu6812);