Automatic date update in version.in
[binutils-gdb.git] / opcodes / msp430-dis.c
blob39d1646cdca7695c944a6561b13995fcd4e74eeb
1 /* Disassemble MSP430 instructions.
2 Copyright (C) 2002-2024 Free Software Foundation, Inc.
4 Contributed by Dmitry Diky <diwil@mail.ru>
6 This file is part of the GNU opcodes library.
8 This library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 It is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #include "sysdep.h"
24 #include <stdio.h>
25 #include <ctype.h>
26 #include <sys/types.h>
27 #include <errno.h>
29 #include "disassemble.h"
30 #include "opintl.h"
31 #include "libiberty.h"
33 #define DASM_SECTION
34 #include "opcode/msp430.h"
35 #undef DASM_SECTION
38 #define PS(x) (0xffff & (x))
40 static bool
41 msp430dis_read_two_bytes (bfd_vma addr,
42 disassemble_info * info,
43 bfd_byte * buffer,
44 char * comm)
46 int status;
48 status = info->read_memory_func (addr, buffer, 2, info);
49 if (status == 0)
50 return true;
52 /* PR 20150: A status of EIO means that there were no more bytes left
53 to read in the current section. This can happen when disassembling
54 interrupt vectors for example. Avoid cluttering the output with
55 unhelpful error messages in this case. */
56 if (status == EIO)
58 if (comm)
59 sprintf (comm, _("Warning: disassembly unreliable - not enough bytes available"));
61 else
63 info->memory_error_func (status, addr, info);
64 if (comm)
65 sprintf (comm, _("Error: read from memory failed"));
68 return false;
71 static bool
72 msp430dis_opcode_unsigned (bfd_vma addr,
73 disassemble_info * info,
74 unsigned short * return_val,
75 char * comm)
77 bfd_byte buffer[2];
79 if (msp430dis_read_two_bytes (addr, info, buffer, comm))
81 * return_val = bfd_getl16 (buffer);
82 return true;
84 else
86 * return_val = 0;
87 return false;
91 static bool
92 msp430dis_opcode_signed (bfd_vma addr,
93 disassemble_info * info,
94 signed int * return_val,
95 char * comm)
97 bfd_byte buffer[2];
99 if (msp430dis_read_two_bytes (addr, info, buffer, comm))
101 int status;
103 status = bfd_getl_signed_16 (buffer);
104 if (status & 0x8000)
105 status |= -1U << 16;
106 * return_val = status;
107 return true;
109 else
111 * return_val = 0;
112 return false;
116 static int
117 msp430_nooperands (struct msp430_opcode_s *opcode,
118 bfd_vma addr ATTRIBUTE_UNUSED,
119 unsigned short insn ATTRIBUTE_UNUSED,
120 char *comm,
121 int *cycles)
123 /* Pop with constant. */
124 if (insn == 0x43b2)
125 return 0;
126 if (insn == opcode->bin_opcode)
127 return 2;
129 if (opcode->fmt == 0)
131 if ((insn & 0x0f00) != 0x0300 || (insn & 0x0f00) != 0x0200)
132 return 0;
134 strcpy (comm, "emulated...");
135 *cycles = 1;
137 else
139 strcpy (comm, "return from interupt");
140 *cycles = 5;
143 return 2;
146 static int
147 print_as2_reg_name (int regno, char * op1, char * comm1,
148 int c2, int c3, int cd)
150 switch (regno)
152 case 2:
153 sprintf (op1, "#4");
154 sprintf (comm1, "r2 As==10");
155 return c2;
157 case 3:
158 sprintf (op1, "#2");
159 sprintf (comm1, "r3 As==10");
160 return c3;
162 default:
163 /* Indexed register mode @Rn. */
164 sprintf (op1, "@r%d", regno);
165 return cd;
169 static int
170 print_as3_reg_name (int regno, char * op1, char * comm1,
171 int c2, int c3, int cd)
173 switch (regno)
175 case 2:
176 sprintf (op1, "#8");
177 sprintf (comm1, "r2 As==11");
178 return c2;
180 case 3:
181 sprintf (op1, "#-1");
182 sprintf (comm1, "r3 As==11");
183 return c3;
185 default:
186 /* Post incremented @Rn+. */
187 sprintf (op1, "@r%d+", regno);
188 return cd;
192 static int
193 msp430_singleoperand (disassemble_info *info,
194 struct msp430_opcode_s *opcode,
195 bfd_vma addr,
196 unsigned short insn,
197 char *op,
198 char *comm,
199 unsigned short extension_word,
200 int *cycles)
202 int regs = 0, regd = 0;
203 int ad = 0, as = 0;
204 int where = 0;
205 int cmd_len = 2;
206 int dst = 0;
207 int fmt;
208 int extended_dst = extension_word & 0xf;
210 regd = insn & 0x0f;
211 regs = (insn & 0x0f00) >> 8;
212 as = (insn & 0x0030) >> 4;
213 ad = (insn & 0x0080) >> 7;
215 if (opcode->fmt < 0)
216 fmt = (- opcode->fmt) - 1;
217 else
218 fmt = opcode->fmt;
220 switch (fmt)
222 case 0: /* Emulated work with dst register. */
223 if (regs != 2 && regs != 3 && regs != 1)
224 return 0;
226 /* Check if not clr insn. */
227 if (opcode->bin_opcode == 0x4300 && (ad || as))
228 return 0;
230 /* Check if really inc, incd insns. */
231 if ((opcode->bin_opcode & 0xff00) == 0x5300 && as == 3)
232 return 0;
234 if (ad == 0)
236 *cycles = 1;
238 /* Register. */
239 if (regd == 0)
241 *cycles += 1;
242 sprintf (op, "r0");
244 else if (regd == 1)
245 sprintf (op, "r1");
247 else if (regd == 2)
248 sprintf (op, "r2");
250 else
251 sprintf (op, "r%d", regd);
253 else /* ad == 1 msp430dis_opcode. */
255 if (regd == 0)
257 /* PC relative. */
258 if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
260 cmd_len += 2;
261 *cycles = 4;
262 sprintf (op, "0x%04x", dst);
263 sprintf (comm, "PC rel. abs addr 0x%04x",
264 PS ((short) (addr + 2) + dst));
265 if (extended_dst)
267 dst |= extended_dst << 16;
268 sprintf (op, "0x%05x", dst);
269 sprintf (comm, "PC rel. abs addr 0x%05lx",
270 (long)((addr + 2 + dst) & 0xfffff));
273 else
274 return -1;
276 else if (regd == 2)
278 /* Absolute. */
279 if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
281 cmd_len += 2;
282 *cycles = 4;
283 sprintf (op, "&0x%04x", PS (dst));
284 if (extended_dst)
286 dst |= extended_dst << 16;
287 sprintf (op, "&0x%05x", dst & 0xfffff);
290 else
291 return -1;
293 else
295 if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
297 cmd_len += 2;
298 *cycles = 4;
299 if (extended_dst)
301 dst |= extended_dst << 16;
302 if (dst & 0x80000)
303 dst |= -1U << 20;
305 sprintf (op, "%d(r%d)", dst, regd);
307 else
308 return -1;
311 break;
313 case 2: /* rrc, push, call, swpb, rra, sxt, push, call, reti etc... */
314 if (as == 0)
316 if (regd == 3)
318 /* Constsnts. */
319 sprintf (op, "#0");
320 sprintf (comm, "r3 As==00");
322 else
324 /* Register. */
325 sprintf (op, "r%d", regd);
327 *cycles = 1;
329 else if (as == 2)
331 * cycles = print_as2_reg_name (regd, op, comm, 1, 1, 3);
333 else if (as == 3)
335 if (regd == 0)
337 *cycles = 3;
338 /* absolute. @pc+ */
339 if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
341 cmd_len += 2;
342 sprintf (op, "#%d", dst);
343 if (dst > 9 || dst < 0)
344 sprintf (comm, "#0x%04x", PS (dst));
345 if (extended_dst)
347 dst |= extended_dst << 16;
348 if (dst & 0x80000)
349 dst |= -1U << 20;
350 sprintf (op, "#%d", dst);
351 if (dst > 9 || dst < 0)
352 sprintf (comm, "#0x%05x", dst);
355 else
356 return -1;
358 else
359 * cycles = print_as3_reg_name (regd, op, comm, 1, 1, 3);
361 else if (as == 1)
363 *cycles = 4;
364 if (regd == 0)
366 /* PC relative. */
367 if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
369 cmd_len += 2;
370 sprintf (op, "0x%04x", PS (dst));
371 sprintf (comm, "PC rel. 0x%04x",
372 PS ((short) addr + 2 + dst));
373 if (extended_dst)
375 dst |= extended_dst << 16;
376 sprintf (op, "0x%05x", dst & 0xffff);
377 sprintf (comm, "PC rel. 0x%05lx",
378 (long)((addr + 2 + dst) & 0xfffff));
381 else
382 return -1;
384 else if (regd == 2)
386 /* Absolute. */
387 if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
389 cmd_len += 2;
390 sprintf (op, "&0x%04x", PS (dst));
391 if (extended_dst)
393 dst |= extended_dst << 16;
394 sprintf (op, "&0x%05x", dst & 0xfffff);
397 else
398 return -1;
400 else if (regd == 3)
402 *cycles = 1;
403 sprintf (op, "#1");
404 sprintf (comm, "r3 As==01");
406 else
408 /* Indexed. */
409 if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
411 cmd_len += 2;
412 if (extended_dst)
414 dst |= extended_dst << 16;
415 if (dst & 0x80000)
416 dst |= -1U << 20;
418 sprintf (op, "%d(r%d)", dst, regd);
419 if (dst > 9 || dst < 0)
420 sprintf (comm, "%05x", dst);
422 else
423 return -1;
426 break;
428 case 3: /* Jumps. */
429 where = insn & 0x03ff;
430 if (where & 0x200)
431 where |= ~0x03ff;
432 if (where > 512 || where < -511)
433 return 0;
435 where *= 2;
436 sprintf (op, "$%+-8d", where + 2);
437 sprintf (comm, "abs 0x%lx", (long) (addr + 2 + where));
438 *cycles = 2;
439 return 2;
440 break;
442 default:
443 cmd_len = 0;
446 return cmd_len;
449 static int
450 msp430_doubleoperand (disassemble_info *info,
451 struct msp430_opcode_s *opcode,
452 bfd_vma addr,
453 unsigned short insn,
454 char *op1,
455 char *op2,
456 char *comm1,
457 char *comm2,
458 unsigned short extension_word,
459 int *cycles)
461 int regs = 0, regd = 0;
462 int ad = 0, as = 0;
463 int cmd_len = 2;
464 int dst = 0;
465 int fmt;
466 int extended_dst = extension_word & 0xf;
467 int extended_src = (extension_word >> 7) & 0xf;
469 regd = insn & 0x0f;
470 regs = (insn & 0x0f00) >> 8;
471 as = (insn & 0x0030) >> 4;
472 ad = (insn & 0x0080) >> 7;
474 if (opcode->fmt < 0)
475 fmt = (- opcode->fmt) - 1;
476 else
477 fmt = opcode->fmt;
479 if (fmt == 0)
481 /* Special case: rla and rlc are the only 2 emulated instructions that
482 fall into two operand instructions. */
483 /* With dst, there are only:
484 Rm Register,
485 x(Rm) Indexed,
486 0xXXXX Relative,
487 &0xXXXX Absolute
488 emulated_ins dst
489 basic_ins dst, dst. */
491 if (regd != regs || as != ad)
492 return 0; /* May be 'data' section. */
494 if (ad == 0)
496 /* Register mode. */
497 if (regd == 3)
499 strcpy (comm1, _("Warning: illegal as emulation instr"));
500 return -1;
503 sprintf (op1, "r%d", regd);
504 *cycles = 1;
506 else /* ad == 1 */
508 if (regd == 0)
510 /* PC relative, Symbolic. */
511 if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
513 cmd_len += 4;
514 *cycles = 6;
515 sprintf (op1, "0x%04x", PS (dst));
516 sprintf (comm1, "PC rel. 0x%04x",
517 PS ((short) addr + 2 + dst));
518 if (extension_word)
520 dst |= extended_dst << 16;
521 if (dst & 0x80000)
522 dst |= -1U << 20;
523 sprintf (op1, "0x%05x", dst & 0xfffff);
524 sprintf (comm1, "PC rel. 0x%05lx",
525 (long)((addr + 2 + dst) & 0xfffff));
528 else
529 return -1;
531 else if (regd == 2)
533 /* Absolute. */
534 if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
536 int src;
538 /* If the 'src' field is not the same as the dst
539 then this is not an rla instruction. */
540 if (msp430dis_opcode_signed (addr + 4, info, &src, comm2))
542 if (src != dst)
543 return 0;
545 else
546 return -1;
547 cmd_len += 4;
548 *cycles = 6;
549 sprintf (op1, "&0x%04x", PS (dst));
550 if (extension_word)
552 dst |= extended_dst << 16;
553 sprintf (op1, "&0x%05x", dst & 0xfffff);
556 else
557 return -1;
559 else
561 /* Indexed. */
562 if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
564 if (extension_word)
566 dst |= extended_dst << 16;
567 if (dst & 0x80000)
568 dst |= -1U << 20;
570 cmd_len += 4;
571 *cycles = 6;
572 sprintf (op1, "%d(r%d)", dst, regd);
573 if (dst > 9 || dst < -9)
574 sprintf (comm1, "#0x%05x", dst);
576 else
577 return -1;
581 *op2 = 0;
582 *comm2 = 0;
584 return cmd_len;
587 /* Two operands exactly. */
588 if (ad == 0 && regd == 3)
590 /* R2/R3 are illegal as dest: may be data section. */
591 strcpy (comm1, _("Warning: illegal as 2-op instr"));
592 return -1;
595 /* Source. */
596 if (as == 0)
598 *cycles = 1;
599 if (regs == 3)
601 /* Constants. */
602 sprintf (op1, "#0");
603 sprintf (comm1, "r3 As==00");
605 else
607 /* Register. */
608 sprintf (op1, "r%d", regs);
611 else if (as == 2)
613 * cycles = print_as2_reg_name (regs, op1, comm1, 1, 1, regs == 0 ? 3 : 2);
615 else if (as == 3)
617 if (regs == 0)
619 *cycles = 3;
620 /* Absolute. @pc+. */
621 if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
623 cmd_len += 2;
624 sprintf (op1, "#%d", dst);
625 if (dst > 9 || dst < 0)
626 sprintf (comm1, "#0x%04x", PS (dst));
627 if (extension_word)
629 dst &= 0xffff;
630 dst |= extended_src << 16;
631 if (dst & 0x80000)
632 dst |= -1U << 20;
633 sprintf (op1, "#%d", dst);
634 if (dst > 9 || dst < 0)
635 sprintf (comm1, "0x%05x", dst & 0xfffff);
638 else
639 return -1;
641 else
642 * cycles = print_as3_reg_name (regs, op1, comm1, 1, 1, 2);
644 else if (as == 1)
646 if (regs == 0)
648 *cycles = 4;
649 /* PC relative. */
650 if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
652 cmd_len += 2;
653 sprintf (op1, "0x%04x", PS (dst));
654 sprintf (comm1, "PC rel. 0x%04x",
655 PS ((short) addr + 2 + dst));
656 if (extension_word)
658 dst &= 0xffff;
659 dst |= extended_src << 16;
660 if (dst & 0x80000)
661 dst |= -1U << 20;
662 sprintf (op1, "0x%05x", dst & 0xfffff);
663 sprintf (comm1, "PC rel. 0x%05lx",
664 (long) ((addr + 2 + dst) & 0xfffff));
667 else
668 return -1;
670 else if (regs == 2)
672 *cycles = 2;
673 /* Absolute. */
674 if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
676 cmd_len += 2;
677 sprintf (op1, "&0x%04x", PS (dst));
678 sprintf (comm1, "0x%04x", PS (dst));
679 if (extension_word)
681 dst &= 0xffff;
682 dst |= extended_src << 16;
683 sprintf (op1, "&0x%05x", dst & 0xfffff);
684 * comm1 = 0;
687 else
688 return -1;
690 else if (regs == 3)
692 *cycles = 1;
693 sprintf (op1, "#1");
694 sprintf (comm1, "r3 As==01");
696 else
698 *cycles = 3;
699 /* Indexed. */
700 if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
702 cmd_len += 2;
703 if (extension_word)
705 dst &= 0xffff;
706 dst |= extended_src << 16;
707 if (dst & 0x80000)
708 dst |= -1U << 20;
710 sprintf (op1, "%d(r%d)", dst, regs);
711 if (dst > 9 || dst < -9)
712 sprintf (comm1, "0x%05x", dst);
714 else
715 return -1;
719 /* Destination. Special care needed on addr + XXXX. */
721 if (ad == 0)
723 /* Register. */
724 if (regd == 0)
726 *cycles += 1;
727 sprintf (op2, "r0");
729 else if (regd == 1)
730 sprintf (op2, "r1");
732 else if (regd == 2)
733 sprintf (op2, "r2");
735 else
736 sprintf (op2, "r%d", regd);
738 else /* ad == 1. */
740 * cycles += 3;
742 if (regd == 0)
744 /* PC relative. */
745 *cycles += 1;
746 if (msp430dis_opcode_signed (addr + cmd_len, info, &dst, comm2))
748 sprintf (op2, "0x%04x", PS (dst));
749 sprintf (comm2, "PC rel. 0x%04x",
750 PS ((short) addr + cmd_len + dst));
751 if (extension_word)
753 dst |= extended_dst << 16;
754 if (dst & 0x80000)
755 dst |= -1U << 20;
756 sprintf (op2, "0x%05x", dst & 0xfffff);
757 sprintf (comm2, "PC rel. 0x%05lx",
758 (long)((addr + cmd_len + dst) & 0xfffff));
761 else
762 return -1;
763 cmd_len += 2;
765 else if (regd == 2)
767 /* Absolute. */
768 if (msp430dis_opcode_signed (addr + cmd_len, info, &dst, comm2))
770 cmd_len += 2;
771 sprintf (op2, "&0x%04x", PS (dst));
772 if (extension_word)
774 dst |= extended_dst << 16;
775 sprintf (op2, "&0x%05x", dst & 0xfffff);
778 else
779 return -1;
781 else
783 if (msp430dis_opcode_signed (addr + cmd_len, info, &dst, comm2))
785 cmd_len += 2;
786 if (dst > 9 || dst < 0)
787 sprintf (comm2, "0x%04x", PS (dst));
788 if (extension_word)
790 dst |= extended_dst << 16;
791 if (dst & 0x80000)
792 dst |= -1U << 20;
793 if (dst > 9 || dst < 0)
794 sprintf (comm2, "0x%05x", dst & 0xfffff);
796 sprintf (op2, "%d(r%d)", dst, regd);
798 else
799 return -1;
803 return cmd_len;
806 static int
807 msp430_branchinstr (disassemble_info *info,
808 struct msp430_opcode_s *opcode ATTRIBUTE_UNUSED,
809 bfd_vma addr ATTRIBUTE_UNUSED,
810 unsigned short insn,
811 char *op1,
812 char *comm1,
813 int *cycles)
815 int regs = 0, regd = 0;
816 int as = 0;
817 int cmd_len = 2;
818 int dst = 0;
819 unsigned short udst = 0;
821 regd = insn & 0x0f;
822 regs = (insn & 0x0f00) >> 8;
823 as = (insn & 0x0030) >> 4;
825 if (regd != 0) /* Destination register is not a PC. */
826 return 0;
828 /* dst is a source register. */
829 if (as == 0)
831 /* Constants. */
832 if (regs == 3)
834 *cycles = 1;
835 sprintf (op1, "#0");
836 sprintf (comm1, "r3 As==00");
838 else
840 /* Register. */
841 *cycles = 1;
842 sprintf (op1, "r%d", regs);
845 else if (as == 2)
847 * cycles = print_as2_reg_name (regs, op1, comm1, 2, 1, 2);
849 else if (as == 3)
851 if (regs == 0)
853 /* Absolute. @pc+ */
854 *cycles = 3;
855 if (msp430dis_opcode_unsigned (addr + 2, info, &udst, comm1))
857 cmd_len += 2;
858 sprintf (op1, "#0x%04x", PS (udst));
860 else
861 return -1;
863 else
864 * cycles = print_as3_reg_name (regs, op1, comm1, 1, 1, 2);
866 else if (as == 1)
868 * cycles = 3;
870 if (regs == 0)
872 /* PC relative. */
873 if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
875 cmd_len += 2;
876 (*cycles)++;
877 sprintf (op1, "0x%04x", PS (dst));
878 sprintf (comm1, "PC rel. 0x%04x",
879 PS ((short) addr + 2 + dst));
881 else
882 return -1;
884 else if (regs == 2)
886 /* Absolute. */
887 if (msp430dis_opcode_unsigned (addr + 2, info, &udst, comm1))
889 cmd_len += 2;
890 sprintf (op1, "&0x%04x", PS (udst));
892 else
893 return -1;
895 else if (regs == 3)
897 (*cycles)--;
898 sprintf (op1, "#1");
899 sprintf (comm1, "r3 As==01");
901 else
903 /* Indexed. */
904 if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
906 cmd_len += 2;
907 sprintf (op1, "%d(r%d)", dst, regs);
909 else
910 return -1;
914 return cmd_len;
917 static int
918 msp430x_calla_instr (disassemble_info * info,
919 bfd_vma addr,
920 unsigned short insn,
921 char * op1,
922 char * comm1,
923 int * cycles)
925 unsigned int ureg = insn & 0xf;
926 int reg = insn & 0xf;
927 int am = (insn & 0xf0) >> 4;
928 int cmd_len = 2;
929 unsigned short udst = 0;
930 int dst = 0;
932 switch (am)
934 case 4: /* CALLA Rdst */
935 *cycles = 1;
936 sprintf (op1, "r%d", reg);
937 break;
939 case 5: /* CALLA x(Rdst) */
940 *cycles = 3;
941 if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
943 cmd_len += 2;
944 sprintf (op1, "%d(r%d)", dst, reg);
945 if (reg == 0)
946 sprintf (comm1, "PC rel. 0x%05lx", (long) (addr + 2 + dst));
947 else
948 sprintf (comm1, "0x%05x", dst);
950 else
951 return -1;
952 break;
954 case 6: /* CALLA @Rdst */
955 *cycles = 2;
956 sprintf (op1, "@r%d", reg);
957 break;
959 case 7: /* CALLA @Rdst+ */
960 *cycles = 2;
961 sprintf (op1, "@r%d+", reg);
962 break;
964 case 8: /* CALLA &abs20 */
965 if (msp430dis_opcode_unsigned (addr + 2, info, &udst, comm1))
967 cmd_len += 2;
968 *cycles = 4;
969 sprintf (op1, "&%d", (ureg << 16) + udst);
970 sprintf (comm1, "0x%05x", (ureg << 16) + udst);
972 else
973 return -1;
974 break;
976 case 9: /* CALLA pcrel-sym */
977 if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
979 cmd_len += 2;
980 *cycles = 4;
981 sprintf (op1, "%d(PC)", (reg << 16) + dst);
982 sprintf (comm1, "PC rel. 0x%05lx",
983 (long) (addr + 2 + dst + (reg << 16)));
985 else
986 return -1;
987 break;
989 case 11: /* CALLA #imm20 */
990 if (msp430dis_opcode_unsigned (addr + 2, info, &udst, comm1))
992 cmd_len += 2;
993 *cycles = 4;
994 sprintf (op1, "#%d", (ureg << 16) + udst);
995 sprintf (comm1, "0x%05x", (ureg << 16) + udst);
997 else
998 return -1;
999 break;
1001 default:
1002 strcpy (comm1, _("Warning: unrecognised CALLA addressing mode"));
1003 return -1;
1006 return cmd_len;
1010 print_insn_msp430 (bfd_vma addr, disassemble_info *info)
1012 void *stream = info->stream;
1013 fprintf_ftype prin = info->fprintf_func;
1014 struct msp430_opcode_s *opcode;
1015 char op1[32], op2[32], comm1[64], comm2[64];
1016 int cmd_len = 0;
1017 unsigned short insn;
1018 int cycles = 0;
1019 char *bc = "";
1020 unsigned short extension_word = 0;
1021 unsigned short bits;
1023 if (! msp430dis_opcode_unsigned (addr, info, &insn, NULL))
1024 return -1;
1026 if (((int) addr & 0xffff) > 0xffdf)
1028 (*prin) (stream, "interrupt service routine at 0x%04x", 0xffff & insn);
1029 return 2;
1032 *comm1 = 0;
1033 *comm2 = 0;
1035 /* Check for an extension word. */
1036 if ((insn & 0xf800) == 0x1800)
1038 extension_word = insn;
1039 addr += 2;
1040 if (! msp430dis_opcode_unsigned (addr, info, &insn, NULL))
1041 return -1;
1044 for (opcode = msp430_opcodes; opcode->name; opcode++)
1046 if ((insn & opcode->bin_mask) == opcode->bin_opcode
1047 && opcode->bin_opcode != 0x9300)
1049 *op1 = 0;
1050 *op2 = 0;
1051 *comm1 = 0;
1052 *comm2 = 0;
1054 /* r0 as destination. Ad should be zero. */
1055 if (opcode->insn_opnumb == 3
1056 && (insn & 0x000f) == 0
1057 && (insn & 0x0080) == 0)
1059 int ret =
1060 msp430_branchinstr (info, opcode, addr, insn, op1, comm1,
1061 &cycles);
1063 if (ret == -1)
1064 return -1;
1065 cmd_len += ret;
1066 if (cmd_len)
1067 break;
1070 switch (opcode->insn_opnumb)
1072 int n;
1073 int reg;
1074 int ret;
1076 case 4:
1077 ret = msp430x_calla_instr (info, addr, insn,
1078 op1, comm1, & cycles);
1079 if (ret == -1)
1080 return -1;
1081 cmd_len += ret;
1082 break;
1084 case 5: /* PUSHM/POPM */
1085 n = (insn & 0xf0) >> 4;
1086 reg = (insn & 0xf);
1088 sprintf (op1, "#%d", n + 1);
1089 if (opcode->bin_opcode == 0x1400)
1090 /* PUSHM */
1091 sprintf (op2, "r%d", reg);
1092 else
1093 /* POPM */
1094 sprintf (op2, "r%d", reg + n);
1095 if (insn & 0x100)
1096 sprintf (comm1, "16-bit words");
1097 else
1099 sprintf (comm1, "20-bit words");
1100 bc =".a";
1103 cycles = 2; /*FIXME*/
1104 cmd_len = 2;
1105 break;
1107 case 6: /* RRAM, RRCM, RRUM, RLAM. */
1108 n = ((insn >> 10) & 0x3) + 1;
1109 reg = (insn & 0xf);
1110 if ((insn & 0x10) == 0)
1111 bc =".a";
1112 sprintf (op1, "#%d", n);
1113 sprintf (op2, "r%d", reg);
1114 cycles = 2; /*FIXME*/
1115 cmd_len = 2;
1116 break;
1118 case 8: /* ADDA, CMPA, SUBA. */
1119 reg = (insn & 0xf);
1120 n = (insn >> 8) & 0xf;
1121 if (insn & 0x40)
1123 sprintf (op1, "r%d", n);
1124 cmd_len = 2;
1126 else
1128 n <<= 16;
1129 if (msp430dis_opcode_unsigned (addr + 2, info, &bits, comm1))
1131 n |= bits;
1132 sprintf (op1, "#%d", n);
1133 if (n > 9 || n < 0)
1134 sprintf (comm1, "0x%05x", n);
1136 else
1137 return -1;
1138 cmd_len = 4;
1140 sprintf (op2, "r%d", reg);
1141 cycles = 2; /*FIXME*/
1142 break;
1144 case 9: /* MOVA */
1145 reg = (insn & 0xf);
1146 n = (insn >> 8) & 0xf;
1147 switch ((insn >> 4) & 0xf)
1149 case 0: /* MOVA @Rsrc, Rdst */
1150 cmd_len = 2;
1151 sprintf (op1, "@r%d", n);
1152 if (strcmp (opcode->name, "bra") != 0)
1153 sprintf (op2, "r%d", reg);
1154 break;
1156 case 1: /* MOVA @Rsrc+, Rdst */
1157 cmd_len = 2;
1158 if (strcmp (opcode->name, "reta") != 0)
1160 sprintf (op1, "@r%d+", n);
1161 if (strcmp (opcode->name, "bra") != 0)
1162 sprintf (op2, "r%d", reg);
1164 break;
1166 case 2: /* MOVA &abs20, Rdst */
1167 cmd_len = 4;
1168 n <<= 16;
1169 if (msp430dis_opcode_unsigned (addr + 2, info, &bits, comm1))
1171 n |= bits;
1172 sprintf (op1, "&%d", n);
1173 if (n > 9 || n < 0)
1174 sprintf (comm1, "0x%05x", n);
1175 if (strcmp (opcode->name, "bra") != 0)
1176 sprintf (op2, "r%d", reg);
1178 else
1179 return -1;
1180 break;
1182 case 3: /* MOVA x(Rsrc), Rdst */
1183 cmd_len = 4;
1184 if (strcmp (opcode->name, "bra") != 0)
1185 sprintf (op2, "r%d", reg);
1186 reg = n;
1187 if (msp430dis_opcode_signed (addr + 2, info, &n, comm1))
1189 sprintf (op1, "%d(r%d)", n, reg);
1190 if (n > 9 || n < 0)
1192 if (reg == 0)
1193 sprintf (comm1, "PC rel. 0x%05lx",
1194 (long) (addr + 2 + n));
1195 else
1196 sprintf (comm1, "0x%05x", n);
1199 else
1200 return -1;
1201 break;
1203 case 6: /* MOVA Rsrc, &abs20 */
1204 cmd_len = 4;
1205 reg <<= 16;
1206 if (msp430dis_opcode_unsigned (addr + 2, info, &bits, comm2))
1208 reg |= bits;
1209 sprintf (op1, "r%d", n);
1210 sprintf (op2, "&%d", reg);
1211 if (reg > 9 || reg < 0)
1212 sprintf (comm2, "0x%05x", reg);
1214 else
1215 return -1;
1216 break;
1218 case 7: /* MOVA Rsrc, x(Rdst) */
1219 cmd_len = 4;
1220 sprintf (op1, "r%d", n);
1221 if (msp430dis_opcode_signed (addr + 2, info, &n, comm2))
1223 sprintf (op2, "%d(r%d)", n, reg);
1224 if (n > 9 || n < 0)
1226 if (reg == 0)
1227 sprintf (comm2, "PC rel. 0x%05lx",
1228 (long) (addr + 2 + n));
1229 else
1230 sprintf (comm2, "0x%05x", n);
1233 else
1234 return -1;
1235 break;
1237 case 8: /* MOVA #imm20, Rdst */
1238 cmd_len = 4;
1239 n <<= 16;
1240 if (msp430dis_opcode_unsigned (addr + 2, info, &bits, comm1))
1242 n |= bits;
1243 if (n & 0x80000)
1244 n |= -1U << 20;
1245 sprintf (op1, "#%d", n);
1246 if (n > 9 || n < 0)
1247 sprintf (comm1, "0x%05x", n);
1248 if (strcmp (opcode->name, "bra") != 0)
1249 sprintf (op2, "r%d", reg);
1251 else
1252 return -1;
1253 break;
1255 case 12: /* MOVA Rsrc, Rdst */
1256 cmd_len = 2;
1257 sprintf (op1, "r%d", n);
1258 if (strcmp (opcode->name, "bra") != 0)
1259 sprintf (op2, "r%d", reg);
1260 break;
1262 default:
1263 break;
1265 cycles = 2; /* FIXME */
1266 break;
1269 if (cmd_len)
1270 break;
1272 switch (opcode->insn_opnumb)
1274 int ret;
1276 case 0:
1277 cmd_len += msp430_nooperands (opcode, addr, insn, comm1, &cycles);
1278 break;
1279 case 2:
1280 ret =
1281 msp430_doubleoperand (info, opcode, addr, insn, op1, op2,
1282 comm1, comm2,
1283 extension_word,
1284 &cycles);
1286 if (ret == -1)
1287 return -1;
1288 cmd_len += ret;
1289 if (insn & BYTE_OPERATION)
1291 if (extension_word != 0 && ((extension_word & BYTE_OPERATION) == 0))
1292 bc = ".a";
1293 else
1294 bc = ".b";
1296 else if (extension_word)
1298 if (extension_word & BYTE_OPERATION)
1299 bc = ".w";
1300 else
1302 bc = ".?";
1303 sprintf (comm2, _("Warning: reserved use of A/L and B/W bits detected"));
1307 break;
1308 case 1:
1309 ret =
1310 msp430_singleoperand (info, opcode, addr, insn, op1, comm1,
1311 extension_word,
1312 &cycles);
1314 if (ret == -1)
1315 return -1;
1316 cmd_len += ret;
1317 if (extension_word
1318 && (strcmp (opcode->name, "swpb") == 0
1319 || strcmp (opcode->name, "sxt") == 0))
1321 if (insn & BYTE_OPERATION)
1323 bc = ".?";
1324 sprintf (comm2, _("Warning: reserved use of A/L and B/W bits detected"));
1326 else if (extension_word & BYTE_OPERATION)
1327 bc = ".w";
1328 else
1329 bc = ".a";
1331 else if (insn & BYTE_OPERATION && opcode->fmt != 3)
1333 if (extension_word != 0 && ((extension_word & BYTE_OPERATION) == 0))
1334 bc = ".a";
1335 else
1336 bc = ".b";
1338 else if (extension_word)
1340 if (extension_word & (1 << 6))
1341 bc = ".w";
1342 else
1344 bc = ".?";
1345 sprintf (comm2, _("Warning: reserved use of A/L and B/W bits detected"));
1348 break;
1349 default:
1350 break;
1354 if (cmd_len)
1355 break;
1358 if (cmd_len < 1)
1360 /* Unknown opcode, or invalid combination of operands. */
1361 if (extension_word)
1363 prin (stream, ".word 0x%04x, 0x%04x; ????", extension_word, PS (insn));
1364 if (*comm1)
1365 prin (stream, "\t %s", comm1);
1366 return 4;
1368 (*prin) (stream, ".word 0x%04x; ????", PS (insn));
1369 return 2;
1372 /* Display the repeat count (if set) for extended register mode. */
1373 if (cmd_len == 2 && ((extension_word & 0xf) != 0))
1375 if (extension_word & (1 << 7))
1376 prin (stream, "rpt r%d { ", extension_word & 0xf);
1377 else
1378 prin (stream, "rpt #%d { ", (extension_word & 0xf) + 1);
1381 /* Special case: RRC with an extension word and the ZC bit set is actually RRU. */
1382 if (extension_word
1383 && (extension_word & IGNORE_CARRY_BIT)
1384 && strcmp (opcode->name, "rrc") == 0)
1385 (*prin) (stream, "rrux%s", bc);
1386 else if (extension_word && opcode->name[strlen (opcode->name) - 1] != 'x')
1387 (*prin) (stream, "%sx%s", opcode->name, bc);
1388 else
1389 (*prin) (stream, "%s%s", opcode->name, bc);
1391 if (*op1)
1392 (*prin) (stream, "\t%s", op1);
1393 if (*op2)
1394 (*prin) (stream, ",");
1396 if (strlen (op1) < 7)
1397 (*prin) (stream, "\t");
1398 if (!strlen (op1))
1399 (*prin) (stream, "\t");
1401 if (*op2)
1402 (*prin) (stream, "%s", op2);
1403 if (strlen (op2) < 8)
1404 (*prin) (stream, "\t");
1406 if (*comm1 || *comm2)
1407 (*prin) (stream, ";");
1408 else if (cycles)
1410 if (*op2)
1411 (*prin) (stream, ";");
1412 else
1414 if (strlen (op1) < 7)
1415 (*prin) (stream, ";");
1416 else
1417 (*prin) (stream, "\t;");
1420 if (*comm1)
1421 (*prin) (stream, "%s", comm1);
1422 if (*comm1 && *comm2)
1423 (*prin) (stream, ",");
1424 if (*comm2)
1425 (*prin) (stream, " %s", comm2);
1427 if (extension_word)
1428 cmd_len += 2;
1430 return cmd_len;