Add Brazillian Portugese translation.
[binutils.git] / opcodes / m68hc11-dis.c
blob33d1f0e9f47414e99a776b36144ecbbef63a126e
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. */
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
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));
46 static int print_insn
47 PARAMS ((bfd_vma, struct disassemble_info *, int));
49 static int
50 read_memory (memaddr, buffer, size, info)
51 bfd_vma memaddr;
52 bfd_byte *buffer;
53 int size;
54 struct disassemble_info *info;
56 int status;
58 /* Get first byte. Only one at a time because we don't know the
59 size of the insn. */
60 status = (*info->read_memory_func) (memaddr, buffer, size, info);
61 if (status != 0)
63 (*info->memory_error_func) (status, memaddr, info);
64 return -1;
66 return 0;
70 /* Read the 68HC12 indexed operand byte and print the corresponding mode.
71 Returns the number of bytes read or -1 if failure. */
72 static int
73 print_indexed_operand (memaddr, info, indirect, mov_insn, pc_offset, endaddr)
74 bfd_vma memaddr;
75 struct disassemble_info *info;
76 int *indirect;
77 int mov_insn;
78 int pc_offset;
79 bfd_vma endaddr;
81 bfd_byte buffer[4];
82 int reg;
83 int status;
84 short sval;
85 int pos = 1;
87 if (indirect)
88 *indirect = 0;
90 status = read_memory (memaddr, &buffer[0], 1, info);
91 if (status != 0)
93 return status;
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);
101 if (sval & 0x10)
102 sval |= 0xfff0;
103 /* 68HC12 requires an adjustment for movb/movw pc relative modes. */
104 if (reg == PC_REGNUM && info->mach == bfd_mach_m6812 && mov_insn)
105 sval += pc_offset;
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)
120 const char *mode;
122 reg = (buffer[0] >> 6) & 3;
123 sval = (buffer[0] & 0x0f);
124 if (sval & 0x8)
126 sval |= 0xfff0;
127 sval = -sval;
128 mode = "-";
130 else
132 sval = sval + 1;
133 mode = "+";
135 (*info->fprintf_func) (info->stream, "%d,%s%s%s",
136 (int) sval,
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)
144 if (mov_insn)
146 (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
147 buffer[0] & 0x0ff);
148 return 0;
150 reg = (buffer[0] >> 3) & 0x03;
151 status = read_memory (memaddr + pos, &buffer[0], 2, info);
152 if (status != 0)
154 return status;
157 pos += 2;
158 sval = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
159 (*info->fprintf_func) (info->stream, "[%u,%s]",
160 sval & 0x0ffff, reg_name[reg]);
161 if (indirect)
162 *indirect = 1;
165 /* n,r with 9 and 16 bit signed constant. */
166 else if ((buffer[0] & 0x4) == 0)
168 if (mov_insn)
170 (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
171 buffer[0] & 0x0ff);
172 return 0;
174 reg = (buffer[0] >> 3) & 0x03;
175 status = read_memory (memaddr + pos,
176 &buffer[1], (buffer[0] & 0x2 ? 2 : 1), info);
177 if (status != 0)
179 return status;
181 if (buffer[0] & 2)
183 sval = ((buffer[1] << 8) | (buffer[2] & 0x0FF));
184 sval &= 0x0FFFF;
185 pos += 2;
187 else
189 sval = buffer[1] & 0x00ff;
190 if (buffer[0] & 0x01)
191 sval |= 0xff00;
192 pos++;
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, "}");
203 else
205 reg = (buffer[0] >> 3) & 0x03;
206 switch (buffer[0] & 3)
208 case 0:
209 (*info->fprintf_func) (info->stream, "A,%s", reg_name[reg]);
210 break;
211 case 1:
212 (*info->fprintf_func) (info->stream, "B,%s", reg_name[reg]);
213 break;
214 case 2:
215 (*info->fprintf_func) (info->stream, "D,%s", reg_name[reg]);
216 break;
217 case 3:
218 default:
219 (*info->fprintf_func) (info->stream, "[D,%s]", reg_name[reg]);
220 if (indirect)
221 *indirect = 1;
222 break;
226 return pos;
229 /* Disassemble one instruction at address 'memaddr'. Returns the number
230 of bytes used by that instruction. */
231 static int
232 print_insn (memaddr, info, arch)
233 bfd_vma memaddr;
234 struct disassemble_info *info;
235 int arch;
237 int status;
238 bfd_byte buffer[4];
239 unsigned char code;
240 long format, pos, i;
241 short sval;
242 const struct m68hc11_opcode *opcode;
244 /* Get first byte. Only one at a time because we don't know the
245 size of the insn. */
246 status = read_memory (memaddr, buffer, 1, info);
247 if (status != 0)
249 return status;
252 format = 0;
253 code = buffer[0];
254 pos = 0;
256 /* Look for page2,3,4 opcodes. */
257 if (code == M6811_OPCODE_PAGE2)
259 pos++;
260 format = M6811_OP_PAGE2;
262 else if (code == M6811_OPCODE_PAGE3 && arch == cpu6811)
264 pos++;
265 format = M6811_OP_PAGE3;
267 else if (code == M6811_OPCODE_PAGE4 && arch == cpu6811)
269 pos++;
270 format = M6811_OP_PAGE4;
273 /* We are in page2,3,4; get the real opcode. */
274 if (pos == 1)
276 status = read_memory (memaddr + pos, &buffer[1], 1, info);
277 if (status != 0)
279 return status;
281 code = buffer[1];
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)
290 int must_read = 1;
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)
297 if (must_read)
299 status = read_memory (memaddr + pos + 1,
300 &buffer[1], 1, info);
301 if (status != 0)
302 break;
304 must_read = 1;
306 if (m68hc12_alias[i].code2 == (unsigned char) buffer[1])
308 (*info->fprintf_func) (info->stream, "%s",
309 m68hc12_alias[i].name);
310 return 2;
316 pos++;
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++)
323 int offset;
324 int pc_src_offset;
325 int pc_dst_offset;
327 if ((opcode->arch & arch) == 0)
328 continue;
329 if (opcode->opcode != code)
330 continue;
331 if ((opcode->format & OP_PAGE_MASK) != format)
332 continue;
334 if (opcode->format & M6812_OP_REG)
336 int j;
337 int is_jump;
339 if (opcode->format & M6811_OP_JUMP_REL)
340 is_jump = 1;
341 else
342 is_jump = 0;
344 status = read_memory (memaddr + pos, &buffer[0], 1, info);
345 if (status != 0)
347 return status;
349 for (j = 0; i + j < m68hc11_num_opcodes; j++)
351 if ((opcode[j].arch & arch) == 0)
352 continue;
353 if (opcode[j].opcode != code)
354 continue;
355 if (is_jump)
357 if (!(opcode[j].format & M6811_OP_JUMP_REL))
358 continue;
360 if ((opcode[j].format & M6812_OP_IBCC_MARKER)
361 && (buffer[0] & 0xc0) != 0x80)
362 continue;
363 if ((opcode[j].format & M6812_OP_TBCC_MARKER)
364 && (buffer[0] & 0xc0) != 0x40)
365 continue;
366 if ((opcode[j].format & M6812_OP_DBCC_MARKER)
367 && (buffer[0] & 0xc0) != 0)
368 continue;
369 if ((opcode[j].format & M6812_OP_EQ_MARKER)
370 && (buffer[0] & 0x20) == 0)
371 break;
372 if (!(opcode[j].format & M6812_OP_EQ_MARKER)
373 && (buffer[0] & 0x20) != 0)
374 break;
375 continue;
377 if (opcode[j].format & M6812_OP_EXG_MARKER && buffer[0] & 0x80)
378 break;
379 if ((opcode[j].format & M6812_OP_SEX_MARKER)
380 && (((buffer[0] & 0x07) >= 3 && (buffer[0] & 7) <= 7))
381 && ((buffer[0] & 0x0f0) <= 0x20))
382 break;
383 if (opcode[j].format & M6812_OP_TFR_MARKER
384 && !(buffer[0] & 0x80))
385 break;
387 if (i + j < m68hc11_num_opcodes)
388 opcode = &opcode[j];
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.
406 offset offset
407 for constant for destination
408 movb 18 OB ii hh ll 0 0
409 18 08 xb ii 1 -1
410 18 0C hh ll hh ll 0 0
411 18 09 xb hh ll 1 -1
412 18 0D xb hh ll 0 0
413 18 0A xb xb 0 0
415 movw 18 03 jj kk hh ll 0 0
416 18 00 xb jj kk 1 -1
417 18 04 hh ll hh ll 0 0
418 18 01 xb hh ll 1 -1
419 18 05 xb hh ll 0 0
420 18 02 xb xb 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
426 format. */
427 offset = ((format & M6812_OP_IDX_P2)
428 && (format & (M6811_OP_IMM8 | M6811_OP_IMM16 |
429 M6811_OP_IND16)));
431 /* Operand with one more byte: - immediate, offset,
432 direct-low address. */
433 if (format &
434 (M6811_OP_IMM8 | M6811_OP_IX | M6811_OP_IY | M6811_OP_DIRECT))
436 status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
437 if (status != 0)
439 return status;
442 pos++;
444 /* This movb/movw is special (see above). */
445 offset = -offset;
447 pc_dst_offset = 2;
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. */
453 pc_dst_offset = 1;
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)
479 int indirect;
480 bfd_vma endaddr;
482 endaddr = memaddr + pos + 1;
483 if (format & M6811_OP_IND16)
484 endaddr += 2;
485 pc_src_offset = -1;
486 pc_dst_offset = 1;
487 status = print_indexed_operand (memaddr + pos, info, &indirect,
488 (format & M6812_DST_MOVE),
489 pc_src_offset, endaddr);
490 if (status < 0)
492 return status;
494 pos += status;
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);
506 if (status != 0)
508 return status;
510 (*info->fprintf_func) (info->stream, "%s,",
511 reg_src_table[buffer[0] & 0x07]);
512 sval = buffer[1] & 0x0ff;
513 if (buffer[0] & 0x10)
514 sval |= 0xff00;
516 pos += 2;
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);
523 if (status != 0)
525 return status;
528 pos++;
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))
536 int val;
537 bfd_vma addr;
538 unsigned page = 0;
540 status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
541 if (status != 0)
543 return status;
545 if (format & M6812_OP_IDX_P2)
546 offset = -2;
547 else
548 offset = 0;
549 pos += 2;
551 val = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
552 val &= 0x0FFFF;
553 addr = val;
554 pc_dst_offset = 2;
555 if (format & M6812_OP_PAGE)
557 status = read_memory (memaddr + pos + offset, buffer, 1, info);
558 if (status != 0)
559 return status;
561 page = (unsigned) buffer[0];
562 if (addr >= M68HC12_BANK_BASE && addr < 0x0c000)
563 addr = ((val - M68HC12_BANK_BASE)
564 | (page << M68HC12_BANK_SHIFT))
565 + M68HC12_BANK_VIRT;
567 else if ((arch & cpu6812)
568 && addr >= M68HC12_BANK_BASE && addr < 0x0c000)
570 int cur_page;
571 bfd_vma vaddr;
573 if (memaddr >= M68HC12_BANK_VIRT)
574 cur_page = ((memaddr - M68HC12_BANK_VIRT)
575 >> M68HC12_BANK_SHIFT);
576 else
577 cur_page = 0;
579 vaddr = ((addr - M68HC12_BANK_BASE)
580 + (cur_page << M68HC12_BANK_SHIFT))
581 + M68HC12_BANK_VIRT;
582 if (!info->symbol_at_address_func (addr, info)
583 && info->symbol_at_address_func (vaddr, info))
584 addr = vaddr;
586 if (format & M6811_OP_IMM16)
588 format &= ~M6811_OP_IMM16;
589 (*info->fprintf_func) (info->stream, "#");
591 else
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;
601 pos += 1;
605 if (format & M6812_OP_IDX_P2)
607 (*info->fprintf_func) (info->stream, ", ");
608 status = print_indexed_operand (memaddr + pos + offset, info,
609 0, 1, pc_dst_offset,
610 memaddr + pos + offset + 1);
611 if (status < 0)
612 return status;
613 pos += status;
616 if (format & M6812_OP_IND16_P2)
618 int val;
620 (*info->fprintf_func) (info->stream, ", ");
622 status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
623 if (status != 0)
625 return status;
627 pos += 2;
629 val = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
630 val &= 0x0FFFF;
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);
640 if (status != 0)
642 return status;
644 pos++;
645 (*info->fprintf_func) (info->stream, " #$%02x%s",
646 buffer[0] & 0x0FF,
647 (format & M6811_OP_JUMP_REL ? " " : ""));
648 format &= ~M6811_OP_BITMASK;
650 if (format & M6811_OP_JUMP_REL)
652 int val;
654 status = read_memory (memaddr + pos, &buffer[0], 1, info);
655 if (status != 0)
657 return status;
660 pos++;
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)
667 int val;
669 status = read_memory (memaddr + pos, &buffer[0], 2, info);
670 if (status != 0)
672 return status;
675 pos += 2;
676 val = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
677 if (val & 0x8000)
678 val |= 0xffff0000;
680 (*info->print_address_func) (memaddr + pos + val, info);
681 format &= ~M6812_OP_JUMP_REL16;
684 if (format & M6812_OP_PAGE)
686 int val;
688 status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
689 if (status != 0)
691 return status;
693 pos += 1;
695 val = buffer[0] & 0x0ff;
696 (*info->fprintf_func) (info->stream, ", %d", val);
699 #ifdef DEBUG
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",
710 pos, opcode->size);
712 #endif
713 return pos;
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);
730 else
731 (*info->fprintf_func) (info->stream, ".byte\t0x%02x", code);
733 return pos;
736 /* Disassemble one instruction at address 'memaddr'. Returns the number
737 of bytes used by that instruction. */
739 print_insn_m68hc11 (memaddr, info)
740 bfd_vma memaddr;
741 struct disassemble_info *info;
743 return print_insn (memaddr, info, cpu6811);
747 print_insn_m68hc12 (memaddr, info)
748 bfd_vma memaddr;
749 struct disassemble_info *info;
751 return print_insn (memaddr, info, cpu6812);