File: msp430-dis.c

package info (click to toggle)
gdb 7.12-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 216,152 kB
  • ctags: 304,881
  • sloc: ansic: 2,141,328; asm: 331,662; exp: 133,348; makefile: 57,698; sh: 23,586; yacc: 14,054; cpp: 12,262; perl: 5,300; python: 4,685; ada: 4,343; xml: 3,670; pascal: 3,120; lisp: 1,516; cs: 879; lex: 624; f90: 457; sed: 228; awk: 142; objc: 134; java: 73; fortran: 43
file content (1356 lines) | stat: -rw-r--r-- 29,624 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
/* Disassemble MSP430 instructions.
   Copyright (C) 2002-2016 Free Software Foundation, Inc.

   Contributed by Dmitry Diky <diwil@mail.ru>

   This file is part of the GNU opcodes library.

   This library is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   It is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
   License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
   MA 02110-1301, USA.  */

#include "sysdep.h"
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <errno.h>

#include "dis-asm.h"
#include "opintl.h"
#include "libiberty.h"

#define DASM_SECTION
#include "opcode/msp430.h"
#undef DASM_SECTION


#define PS(x)   (0xffff & (x))

static bfd_boolean
msp430dis_read_two_bytes (bfd_vma            addr,
			  disassemble_info * info,
			  bfd_byte *         buffer,
			  char *             comm)
{
  int status;

  status = info->read_memory_func (addr, buffer, 2, info);
  if (status == 0)
    return TRUE;

  /* PR 20150: A status of EIO means that there were no more bytes left
     to read in the current section.  This can happen when disassembling
     interrupt vectors for example.  Avoid cluttering the output with
     unhelpful error messages in this case.  */
  if (status == EIO)
    {
      if (comm)
	sprintf (comm, _("Warning: disassembly unreliable - not enough bytes available"));
    }
  else
    {
      info->memory_error_func (status, addr, info);
      if (comm)
	sprintf (comm, _("Error: read from memory failed"));
    }

  return FALSE;
}

static bfd_boolean
msp430dis_opcode_unsigned (bfd_vma            addr,
			   disassemble_info * info,
			   unsigned short *   return_val,
			   char *             comm)
{
  bfd_byte buffer[2];

  if (msp430dis_read_two_bytes (addr, info, buffer, comm))
    {
      * return_val = bfd_getl16 (buffer);
      return TRUE;
    }
  else
    {
      * return_val = 0;
      return FALSE;
    }
}

static bfd_boolean
msp430dis_opcode_signed (bfd_vma            addr,
			 disassemble_info * info,
			 signed int *       return_val,
			 char *             comm)
{
  bfd_byte buffer[2];

  if (msp430dis_read_two_bytes (addr, info, buffer, comm))
    {
      int status;

      status = bfd_getl_signed_16 (buffer);
      if (status & 0x8000)
	status |= -1U << 16;
      * return_val = status;
      return TRUE;
    }
  else
    {
      * return_val = 0;
      return FALSE;
    }
}

static int
msp430_nooperands (struct msp430_opcode_s *opcode,
		   bfd_vma addr ATTRIBUTE_UNUSED,
		   unsigned short insn ATTRIBUTE_UNUSED,
		   char *comm,
		   int *cycles)
{
  /* Pop with constant.  */
  if (insn == 0x43b2)
    return 0;
  if (insn == opcode->bin_opcode)
    return 2;

  if (opcode->fmt == 0)
    {
      if ((insn & 0x0f00) != 0x0300 || (insn & 0x0f00) != 0x0200)
	return 0;

      strcpy (comm, "emulated...");
      *cycles = 1;
    }
  else
    {
      strcpy (comm, "return from interupt");
      *cycles = 5;
    }

  return 2;
}

static int
print_as2_reg_name (int regno, char * op1, char * comm1,
		    int c2, int c3, int cd)
{
  switch (regno)
    {
    case 2:
      sprintf (op1, "#4");
      sprintf (comm1, "r2 As==10");
      return c2;

    case 3:
      sprintf (op1, "#2");
      sprintf (comm1, "r3 As==10");
      return c3;

    default:
      /* Indexed register mode @Rn.  */
      sprintf (op1, "@r%d", regno);
      return cd;
    }
}

static int
print_as3_reg_name (int regno, char * op1, char * comm1,
		    int c2, int c3, int cd)
{
  switch (regno)
    {
    case 2:
      sprintf (op1, "#8");
      sprintf (comm1, "r2 As==11");
      return c2;

    case 3:
      sprintf (op1, "#-1");
      sprintf (comm1, "r3 As==11");
      return c3;

    default:
      /* Post incremented @Rn+.  */
      sprintf (op1, "@r%d+", regno);
      return cd;
    }
}

static int
msp430_singleoperand (disassemble_info *info,
		      struct msp430_opcode_s *opcode,
		      bfd_vma addr,
		      unsigned short insn,
		      char *op,
		      char *comm,
		      unsigned short extension_word,
		      int *cycles)
{
  int regs = 0, regd = 0;
  int ad = 0, as = 0;
  int where = 0;
  int cmd_len = 2;
  int dst = 0;
  int fmt;
  int extended_dst = extension_word & 0xf;

  regd = insn & 0x0f;
  regs = (insn & 0x0f00) >> 8;
  as = (insn & 0x0030) >> 4;
  ad = (insn & 0x0080) >> 7;

  if (opcode->fmt < 0)
    fmt = (- opcode->fmt) - 1;
  else
    fmt = opcode->fmt;

  switch (fmt)
    {
    case 0:			/* Emulated work with dst register.  */
      if (regs != 2 && regs != 3 && regs != 1)
	return 0;

      /* Check if not clr insn.  */
      if (opcode->bin_opcode == 0x4300 && (ad || as))
	return 0;

      /* Check if really inc, incd insns.  */
      if ((opcode->bin_opcode & 0xff00) == 0x5300 && as == 3)
	return 0;

      if (ad == 0)
	{
	  *cycles = 1;

	  /* Register.  */
	  if (regd == 0)
	    {
	      *cycles += 1;
	      sprintf (op, "r0");
	    }
	  else if (regd == 1)
	    sprintf (op, "r1");

	  else if (regd == 2)
	    sprintf (op, "r2");

	  else
	    sprintf (op, "r%d", regd);
	}
      else	/* ad == 1 msp430dis_opcode.  */
	{
	  if (regd == 0)
	    {
	      /* PC relative.  */
	      if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
		{
		  cmd_len += 2;
		  *cycles = 4;
		  sprintf (op, "0x%04x", dst);
		  sprintf (comm, "PC rel. abs addr 0x%04x",
			   PS ((short) (addr + 2) + dst));
		  if (extended_dst)
		    {
		      dst |= extended_dst << 16;
		      sprintf (op, "0x%05x", dst);
		      sprintf (comm, "PC rel. abs addr 0x%05lx",
			       (long)((addr + 2 + dst) & 0xfffff));
		    }
		}
	    }
	  else if (regd == 2)
	    {
	      /* Absolute.  */
	      if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
		{
		  cmd_len += 2;
		  *cycles = 4;
		  sprintf (op, "&0x%04x", PS (dst));
		  if (extended_dst)
		    {
		      dst |= extended_dst << 16;
		      sprintf (op, "&0x%05x", dst & 0xfffff);
		    }
		}
	    }
	  else
	    {
	      if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
		{
		  cmd_len += 2;
		  *cycles = 4;
		  if (extended_dst)
		    {
		      dst |= extended_dst << 16;
		      if (dst & 0x80000)
			dst |= -1U << 20;
		    }
		  sprintf (op, "%d(r%d)", dst, regd);
		}
	    }
	}
      break;

    case 2:	/* rrc, push, call, swpb, rra, sxt, push, call, reti etc...  */
      if (as == 0)
	{
	  if (regd == 3)
	    {
	      /* Constsnts.  */
	      sprintf (op, "#0");
	      sprintf (comm, "r3 As==00");
	    }
	  else
	    {
	      /* Register.  */
	      sprintf (op, "r%d", regd);
	    }
	  *cycles = 1;
	}
      else if (as == 2)
	{
	  * cycles = print_as2_reg_name (regd, op, comm, 1, 1, 3);
	}
      else if (as == 3)
	{
	  if (regd == 0)
	    {
	      *cycles = 3;
	      /* absolute. @pc+ */
	      if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
		{
		  cmd_len += 2;
		  sprintf (op, "#%d", dst);
		  if (dst > 9 || dst < 0)
		    sprintf (comm, "#0x%04x", PS (dst));
		  if (extended_dst)
		    {
		      dst |= extended_dst << 16;
		      if (dst & 0x80000)
			dst |= -1U << 20;
		      sprintf (op, "#%d", dst);
		      if (dst > 9 || dst < 0)
			sprintf (comm, "#0x%05x", dst);
		    }
		}
	    }
	  else
	    * cycles = print_as3_reg_name (regd, op, comm, 1, 1, 3);
	}
      else if (as == 1)
	{
	  *cycles = 4;
	  if (regd == 0)
	    {
	      /* PC relative.  */
	      if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
		{
		  cmd_len += 2;
		  sprintf (op, "0x%04x", PS (dst));
		  sprintf (comm, "PC rel. 0x%04x",
			   PS ((short) addr + 2 + dst));
		  if (extended_dst)
		    {
		      dst |= extended_dst << 16;
		      sprintf (op, "0x%05x", dst & 0xffff);
		      sprintf (comm, "PC rel. 0x%05lx",
			       (long)((addr + 2 + dst) & 0xfffff));
		    }
		}
	    }
	  else if (regd == 2)
	    {
	      /* Absolute.  */
	      if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
		{
		  cmd_len += 2;
		  sprintf (op, "&0x%04x", PS (dst));
		  if (extended_dst)
		    {
		      dst |= extended_dst << 16;
		      sprintf (op, "&0x%05x", dst & 0xfffff);
		    }
		}
	    }
	  else if (regd == 3)
	    {
	      *cycles = 1;
	      sprintf (op, "#1");
	      sprintf (comm, "r3 As==01");
	    }
	  else
	    {
	      /* Indexed.  */
	      if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
		{
		  cmd_len += 2;
		  if (extended_dst)
		    {
		      dst |= extended_dst << 16;
		      if (dst & 0x80000)
			dst |= -1U << 20;
		    }
		  sprintf (op, "%d(r%d)", dst, regd);
		  if (dst > 9 || dst < 0)
		    sprintf (comm, "%05x", dst);
		}
	    }
	}
      break;

    case 3:			/* Jumps.  */
      where = insn & 0x03ff;
      if (where & 0x200)
	where |= ~0x03ff;
      if (where > 512 || where < -511)
	return 0;

      where *= 2;
      sprintf (op, "$%+-8d", where + 2);
      sprintf (comm, "abs 0x%lx", (long) (addr + 2 + where));
      *cycles = 2;
      return 2;
      break;

    default:
      cmd_len = 0;
    }

  return cmd_len;
}

static int
msp430_doubleoperand (disassemble_info *info,
		      struct msp430_opcode_s *opcode,
		      bfd_vma addr,
		      unsigned short insn,
		      char *op1,
		      char *op2,
		      char *comm1,
		      char *comm2,
		      unsigned short extension_word,
		      int *cycles)
{
  int regs = 0, regd = 0;
  int ad = 0, as = 0;
  int cmd_len = 2;
  int dst = 0;
  int fmt;
  int extended_dst = extension_word & 0xf;
  int extended_src = (extension_word >> 7) & 0xf;

  regd = insn & 0x0f;
  regs = (insn & 0x0f00) >> 8;
  as = (insn & 0x0030) >> 4;
  ad = (insn & 0x0080) >> 7;

  if (opcode->fmt < 0)
    fmt = (- opcode->fmt) - 1;
  else
    fmt = opcode->fmt;

  if (fmt == 0)
    {
      /* Special case: rla and rlc are the only 2 emulated instructions that
	 fall into two operand instructions.  */
      /* With dst, there are only:
	 Rm       	Register,
         x(Rm)     	Indexed,
         0xXXXX    	Relative,
         &0xXXXX    	Absolute
         emulated_ins   dst
         basic_ins      dst, dst.  */

      if (regd != regs || as != ad)
	return 0;		/* May be 'data' section.  */

      if (ad == 0)
	{
	  /* Register mode.  */
	  if (regd == 3)
	    {
	      strcpy (comm1, _("Warning: illegal as emulation instr"));
	      return -1;
	    }

	  sprintf (op1, "r%d", regd);
	  *cycles = 1;
	}
      else			/* ad == 1 */
	{
	  if (regd == 0)
	    {
	      /* PC relative, Symbolic.  */
	      if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
		{
		  cmd_len += 4;
		  *cycles = 6;
		  sprintf (op1, "0x%04x", PS (dst));
		  sprintf (comm1, "PC rel. 0x%04x",
			   PS ((short) addr + 2 + dst));
		  if (extension_word)
		    {
		      dst |= extended_dst << 16;
		      if (dst & 0x80000)
			dst |= -1U << 20;
		      sprintf (op1, "0x%05x", dst & 0xfffff);
		      sprintf (comm1, "PC rel. 0x%05lx",
			       (long)((addr + 2 + dst) & 0xfffff));
		    }
		}
	    }
	  else if (regd == 2)
	    {
	      /* Absolute.  */
	      if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
		{
		  int src;

		  /* If the 'src' field is not the same as the dst
		     then this is not an rla instruction.  */
		  if (msp430dis_opcode_signed (addr + 4, info, &src, comm2))
		    {
		      if (src != dst)
			return 0;
		    }
		  cmd_len += 4;
		  *cycles = 6;
		  sprintf (op1, "&0x%04x", PS (dst));
		  if (extension_word)
		    {
		      dst |= extended_dst << 16;
		      sprintf (op1, "&0x%05x", dst & 0xfffff);
		    }
		}
	    }
	  else
	    {
	      /* Indexed.  */
	      if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
		{
		  if (extension_word)
		    {
		      dst |= extended_dst << 16;
		      if (dst & 0x80000)
			dst |= -1U << 20;
		    }
		  cmd_len += 4;
		  *cycles = 6;
		  sprintf (op1, "%d(r%d)", dst, regd);
		  if (dst > 9 || dst < -9)
		    sprintf (comm1, "#0x%05x", dst);
		}
	    }
	}

      *op2 = 0;
      *comm2 = 0;

      return cmd_len;
    }

  /* Two operands exactly.  */
  if (ad == 0 && regd == 3)
    {
      /* R2/R3 are illegal as dest: may be data section.  */
      strcpy (comm1, _("Warning: illegal as 2-op instr"));
      return -1;
    }

  /* Source.  */
  if (as == 0)
    {
      *cycles = 1;
      if (regs == 3)
	{
	  /* Constants.  */
	  sprintf (op1, "#0");
	  sprintf (comm1, "r3 As==00");
	}
      else
	{
	  /* Register.  */
	  sprintf (op1, "r%d", regs);
	}
    }
  else if (as == 2)
    {
      * cycles = print_as2_reg_name (regs, op1, comm1, 1, 1, regs == 0 ? 3 : 2);
    }
  else if (as == 3)
    {
      if (regs == 0)
	{
	  *cycles = 3;
	  /* Absolute. @pc+.  */
	  if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
	    {
	      cmd_len += 2;
	      sprintf (op1, "#%d", dst);
	      if (dst > 9 || dst < 0)
		sprintf (comm1, "#0x%04x", PS (dst));
	      if (extension_word)
		{
		  dst &= 0xffff;
		  dst |= extended_src << 16;
		  if (dst & 0x80000)
		    dst |= -1U << 20;
		  sprintf (op1, "#%d", dst);
		  if (dst > 9 || dst < 0)
		    sprintf (comm1, "0x%05x", dst & 0xfffff);
		}
	    }
	}
      else
	* cycles = print_as3_reg_name (regs, op1, comm1, 1, 1, 2);
    }
  else if (as == 1)
    {
      if (regs == 0)
	{
	  *cycles = 4;
	  /* PC relative.  */
	  if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
	    {
	      cmd_len += 2;
	      sprintf (op1, "0x%04x", PS (dst));
	      sprintf (comm1, "PC rel. 0x%04x",
		       PS ((short) addr + 2 + dst));
	      if (extension_word)
		{
		  dst &= 0xffff;
		  dst |= extended_src << 16;
		  if (dst & 0x80000)
		    dst |= -1U << 20;
		  sprintf (op1, "0x%05x", dst & 0xfffff);
		  sprintf (comm1, "PC rel. 0x%05lx",
			   (long) ((addr + 2 + dst) & 0xfffff));
		}
	    }
	}
      else if (regs == 2)
	{
	  *cycles = 2;
	  /* Absolute.  */
	  if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
	    {
	      cmd_len += 2;
	      sprintf (op1, "&0x%04x", PS (dst));
	      sprintf (comm1, "0x%04x", PS (dst));
	      if (extension_word)
		{
		  dst &= 0xffff;
		  dst |= extended_src << 16;
		  sprintf (op1, "&0x%05x", dst & 0xfffff);
		  * comm1 = 0;
		}
	    }
	}
      else if (regs == 3)
	{
	  *cycles = 1;
	  sprintf (op1, "#1");
	  sprintf (comm1, "r3 As==01");
	}
      else
	{
	  *cycles = 3;
	  /* Indexed.  */
	  if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
	    {
	      cmd_len += 2;
	      if (extension_word)
		{
		  dst &= 0xffff;
		  dst |= extended_src << 16;
		  if (dst & 0x80000)
		    dst |= -1U << 20;
		}
	      sprintf (op1, "%d(r%d)", dst, regs);
	      if (dst > 9 || dst < -9)
		sprintf (comm1, "0x%05x", dst);
	    }
	}
    }

  /* Destination. Special care needed on addr + XXXX.  */

  if (ad == 0)
    {
      /* Register.  */
      if (regd == 0)
	{
	  *cycles += 1;
	  sprintf (op2, "r0");
	}
      else if (regd == 1)
	sprintf (op2, "r1");

      else if (regd == 2)
	sprintf (op2, "r2");

      else
	sprintf (op2, "r%d", regd);
    }
  else	/* ad == 1.  */
    {
      * cycles += 3;

      if (regd == 0)
	{
	  /* PC relative.  */
	  *cycles += 1;
	  if (msp430dis_opcode_signed (addr + cmd_len, info, &dst, comm2))
	    {
	      sprintf (op2, "0x%04x", PS (dst));
	      sprintf (comm2, "PC rel. 0x%04x",
		       PS ((short) addr + cmd_len + dst));
	      if (extension_word)
		{
		  dst |= extended_dst << 16;
		  if (dst & 0x80000)
		    dst |= -1U << 20;
		  sprintf (op2, "0x%05x", dst & 0xfffff);
		  sprintf (comm2, "PC rel. 0x%05lx",
			   (long)((addr + cmd_len + dst) & 0xfffff));
		}
	    }
	  cmd_len += 2;
	}
      else if (regd == 2)
	{
	  /* Absolute.  */
	  if (msp430dis_opcode_signed (addr + cmd_len, info, &dst, comm2))
	    {
	      cmd_len += 2;
	      sprintf (op2, "&0x%04x", PS (dst));
	      if (extension_word)
		{
		  dst |= extended_dst << 16;
		  sprintf (op2, "&0x%05x", dst & 0xfffff);
		}
	    }
	}
      else
	{
	  if (msp430dis_opcode_signed (addr + cmd_len, info, &dst, comm2))
	    {
	      cmd_len += 2;
	      if (dst > 9 || dst < 0)
		sprintf (comm2, "0x%04x", PS (dst));
	      if (extension_word)
		{
		  dst |= extended_dst << 16;
		  if (dst & 0x80000)
		    dst |= -1U << 20;
		  if (dst > 9 || dst < 0)
		    sprintf (comm2, "0x%05x", dst & 0xfffff);
		}
	      sprintf (op2, "%d(r%d)", dst, regd);
	    }
	}
    }

  return cmd_len;
}

static int
msp430_branchinstr (disassemble_info *info,
		    struct msp430_opcode_s *opcode ATTRIBUTE_UNUSED,
		    bfd_vma addr ATTRIBUTE_UNUSED,
		    unsigned short insn,
		    char *op1,
		    char *comm1,
		    int *cycles)
{
  int regs = 0, regd = 0;
  int as = 0;
  int cmd_len = 2;
  int dst = 0;
  unsigned short udst = 0;

  regd = insn & 0x0f;
  regs = (insn & 0x0f00) >> 8;
  as = (insn & 0x0030) >> 4;

  if (regd != 0)	/* Destination register is not a PC.  */
    return 0;

  /* dst is a source register.  */
  if (as == 0)
    {
      /* Constants.  */
      if (regs == 3)
	{
	  *cycles = 1;
	  sprintf (op1, "#0");
	  sprintf (comm1, "r3 As==00");
	}
      else
	{
	  /* Register.  */
	  *cycles = 1;
	  sprintf (op1, "r%d", regs);
	}
    }
  else if (as == 2)
    {
      * cycles = print_as2_reg_name (regs, op1, comm1, 2, 1, 2);
    }
  else if (as == 3)
    {
      if (regs == 0)
	{
	  /* Absolute. @pc+  */
	  *cycles = 3;
	  if (msp430dis_opcode_unsigned (addr + 2, info, &udst, comm1))
	    {
	      cmd_len += 2;
	      sprintf (op1, "#0x%04x", PS (udst));
	    }
	}
      else
	* cycles = print_as3_reg_name (regs, op1, comm1, 1, 1, 2);
    }
  else if (as == 1)
    {
      * cycles = 3;

      if (regs == 0)
	{
	  /* PC relative.  */
	  if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
	    {
	      cmd_len += 2;
	      (*cycles)++;
	      sprintf (op1, "0x%04x", PS (dst));
	      sprintf (comm1, "PC rel. 0x%04x",
		       PS ((short) addr + 2 + dst));
	    }
	}
      else if (regs == 2)
	{
	  /* Absolute.  */
	  if (msp430dis_opcode_unsigned (addr + 2, info, &udst, comm1))
	    {
	      cmd_len += 2;
	      sprintf (op1, "&0x%04x", PS (udst));
	    }
	}
      else if (regs == 3)
	{
	  (*cycles)--;
	  sprintf (op1, "#1");
	  sprintf (comm1, "r3 As==01");
	}
      else
	{
	  /* Indexed.  */
	  if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
	    {
	      cmd_len += 2;
	      sprintf (op1, "%d(r%d)", dst, regs);
	    }
	}
    }

  return cmd_len;
}

static int
msp430x_calla_instr (disassemble_info * info,
		     bfd_vma            addr,
		     unsigned short     insn,
		     char *             op1,
		     char *             comm1,
		     int *              cycles)
{
  unsigned int   ureg = insn & 0xf;
  int            reg = insn & 0xf;
  int            am = (insn & 0xf0) >> 4;
  int            cmd_len = 2;
  unsigned short udst = 0;
  int            dst = 0;

  switch (am)
    {
    case 4: /* CALLA Rdst */
      *cycles = 1;
      sprintf (op1, "r%d", reg);
      break;

    case 5: /* CALLA x(Rdst) */
      *cycles = 3;
      if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
	{
	  cmd_len += 2;
	  sprintf (op1, "%d(r%d)", dst, reg);
	  if (reg == 0)
	    sprintf (comm1, "PC rel. 0x%05lx", (long) (addr + 2 + dst));
	  else
	    sprintf (comm1, "0x%05x", dst);
	}
      break;

    case 6: /* CALLA @Rdst */
      *cycles = 2;
      sprintf (op1, "@r%d", reg);
      break;

    case 7: /* CALLA @Rdst+ */
      *cycles = 2;
      sprintf (op1, "@r%d+", reg);
      break;

    case 8: /* CALLA &abs20 */
      if (msp430dis_opcode_unsigned (addr + 2, info, &udst, comm1))
	{
	  cmd_len += 2;
	  *cycles = 4;
	  sprintf (op1, "&%d", (ureg << 16) + udst);
	  sprintf (comm1, "0x%05x", (ureg << 16) + udst);
	}
      break;

    case 9: /* CALLA pcrel-sym */
      if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
	{
	  cmd_len += 2;
	  *cycles = 4;
	  sprintf (op1, "%d(PC)", (reg << 16) + dst);
	  sprintf (comm1, "PC rel. 0x%05lx",
		   (long) (addr + 2 + dst + (reg << 16)));
	}
      break;

    case 11: /* CALLA #imm20 */
      if (msp430dis_opcode_unsigned (addr + 2, info, &udst, comm1))
	{
	  cmd_len += 2;
	  *cycles = 4;
	  sprintf (op1, "#%d", (ureg << 16) + udst);
	  sprintf (comm1, "0x%05x", (ureg << 16) + udst);
	}
      break;

    default:
      strcpy (comm1, _("Warning: unrecognised CALLA addressing mode"));
      return -1;
    }

  return cmd_len;
}

int
print_insn_msp430 (bfd_vma addr, disassemble_info *info)
{
  void *stream = info->stream;
  fprintf_ftype prin = info->fprintf_func;
  struct msp430_opcode_s *opcode;
  char op1[32], op2[32], comm1[64], comm2[64];
  int cmd_len = 0;
  unsigned short insn;
  int cycles = 0;
  char *bc = "";
  unsigned short extension_word = 0;
  unsigned short bits;

  if (! msp430dis_opcode_unsigned (addr, info, &insn, NULL))
    {
      prin (stream, ".word	0xffff;	????");
      return 2;
    }

  if (((int) addr & 0xffff) > 0xffdf)
    {
      (*prin) (stream, "interrupt service routine at 0x%04x", 0xffff & insn);
      return 2;
    }

  *comm1 = 0;
  *comm2 = 0;

  /* Check for an extension word.  */
  if ((insn & 0xf800) == 0x1800)
    {
      extension_word = insn;
      addr += 2;
      if (! msp430dis_opcode_unsigned (addr, info, &insn, NULL))
	{
	  prin (stream, ".word	0x%04x, 0xffff;	????",
		extension_word);
	  return 4;
	}
   }

  for (opcode = msp430_opcodes; opcode->name; opcode++)
    {
      if ((insn & opcode->bin_mask) == opcode->bin_opcode
	  && opcode->bin_opcode != 0x9300)
	{
	  *op1 = 0;
	  *op2 = 0;
	  *comm1 = 0;
	  *comm2 = 0;

	  /* r0 as destination. Ad should be zero.  */
	  if (opcode->insn_opnumb == 3
	      && (insn & 0x000f) == 0
	      && (insn & 0x0080) == 0)
	    {
	      cmd_len +=
		msp430_branchinstr (info, opcode, addr, insn, op1, comm1,
				    &cycles);
	      if (cmd_len)
		break;
	    }

	  switch (opcode->insn_opnumb)
	    {
	      int n;
	      int reg;

	    case 4:
	      cmd_len += msp430x_calla_instr (info, addr, insn,
					      op1, comm1, & cycles);
	      break;

	    case 5: /* PUSHM/POPM */
	      n = (insn & 0xf0) >> 4;
	      reg = (insn & 0xf);

	      sprintf (op1, "#%d", n + 1);
	      if (opcode->bin_opcode == 0x1400)
		/* PUSHM */
		sprintf (op2, "r%d", reg);
	      else
		/* POPM */
		sprintf (op2, "r%d", reg + n);
	      if (insn & 0x100)
		sprintf (comm1, "16-bit words");
	      else
		{
		  sprintf (comm1, "20-bit words");
		  bc =".a";
		}

	      cycles = 2; /*FIXME*/
	      cmd_len = 2;
	      break;

	    case 6: /* RRAM, RRCM, RRUM, RLAM.  */
	      n = ((insn >> 10) & 0x3) + 1;
	      reg = (insn & 0xf);
	      if ((insn & 0x10) == 0)
		bc =".a";
	      sprintf (op1, "#%d", n);
	      sprintf (op2, "r%d", reg);
	      cycles = 2; /*FIXME*/
	      cmd_len = 2;
	      break;

	    case 8: /* ADDA, CMPA, SUBA.  */
	      reg = (insn & 0xf);
	      n = (insn >> 8) & 0xf;
	      if (insn & 0x40)
		{
		  sprintf (op1, "r%d", n);
		  cmd_len = 2;
		}
	      else
		{
		  n <<= 16;
		  if (msp430dis_opcode_unsigned (addr + 2, info, &bits, comm1))
		    {
		      n |= bits;
		      sprintf (op1, "#%d", n);
		      if (n > 9 || n < 0)
			sprintf (comm1, "0x%05x", n);
		    }
		  cmd_len = 4;
		}
	      sprintf (op2, "r%d", reg);
	      cycles = 2; /*FIXME*/
	      break;

	    case 9: /* MOVA */
	      reg = (insn & 0xf);
	      n = (insn >> 8) & 0xf;
	      switch ((insn >> 4) & 0xf)
		{
		case 0: /* MOVA @Rsrc, Rdst */
		  cmd_len = 2;
		  sprintf (op1, "@r%d", n);
		  if (strcmp (opcode->name, "bra") != 0)
		    sprintf (op2, "r%d", reg);
		  break;

		case 1: /* MOVA @Rsrc+, Rdst */
		  cmd_len = 2;
		  if (strcmp (opcode->name, "reta") != 0)
		    {
		      sprintf (op1, "@r%d+", n);
		      if (strcmp (opcode->name, "bra") != 0)
			sprintf (op2, "r%d", reg);
		    }
		  break;

		case 2: /* MOVA &abs20, Rdst */
		  cmd_len = 4;
		  n <<= 16;
		  if (msp430dis_opcode_unsigned (addr + 2, info, &bits, comm1))
		    {
		      n |= bits;
		      sprintf (op1, "&%d", n);
		      if (n > 9 || n < 0)
			sprintf (comm1, "0x%05x", n);
		      if (strcmp (opcode->name, "bra") != 0)
			sprintf (op2, "r%d", reg);
		    }
		  break;

		case 3: /* MOVA x(Rsrc), Rdst */
		  cmd_len = 4;
		  if (strcmp (opcode->name, "bra") != 0)
		    sprintf (op2, "r%d", reg);
		  reg = n;
		  if (msp430dis_opcode_signed (addr + 2, info, &n, comm1))
		    {
		      sprintf (op1, "%d(r%d)", n, reg);
		      if (n > 9 || n < 0)
			{
			  if (reg == 0)
			    sprintf (comm1, "PC rel. 0x%05lx",
				     (long) (addr + 2 + n));
			  else
			    sprintf (comm1, "0x%05x", n);
			}
		    }
		  break;

		case 6: /* MOVA Rsrc, &abs20 */
		  cmd_len = 4;
		  reg <<= 16;
		  if (msp430dis_opcode_unsigned (addr + 2, info, &bits, comm2))
		    {
		      reg |= bits;
		      sprintf (op1, "r%d", n);
		      sprintf (op2, "&%d", reg);
		      if (reg > 9 || reg < 0)
			sprintf (comm2, "0x%05x", reg);
		    }
		  break;

		case 7: /* MOVA Rsrc, x(Rdst) */
		  cmd_len = 4;
		  sprintf (op1, "r%d", n);
		  if (msp430dis_opcode_signed (addr + 2, info, &n, comm2))
		    {
		      sprintf (op2, "%d(r%d)", n, reg);
		      if (n > 9 || n < 0)
			{
			  if (reg == 0)
			    sprintf (comm2, "PC rel. 0x%05lx",
				     (long) (addr + 2 + n));
			  else
			    sprintf (comm2, "0x%05x", n);
			}
		    }
		  break;

		case 8: /* MOVA #imm20, Rdst */
		  cmd_len = 4;
		  n <<= 16;
		  if (msp430dis_opcode_unsigned (addr + 2, info, &bits, comm1))
		    {
		      n |= bits;
		      if (n & 0x80000)
			n |= -1U << 20;
		      sprintf (op1, "#%d", n);
		      if (n > 9 || n < 0)
			sprintf (comm1, "0x%05x", n);
		      if (strcmp (opcode->name, "bra") != 0)
			sprintf (op2, "r%d", reg);
		    }
		  break;

		case 12: /* MOVA Rsrc, Rdst */
		  cmd_len = 2;
		  sprintf (op1, "r%d", n);
		  if (strcmp (opcode->name, "bra") != 0)
		    sprintf (op2, "r%d", reg);
		  break;

		default:
		  break;
		}
	      cycles = 2; /* FIXME */
	      break;
	    }

	  if (cmd_len)
	    break;

	  switch (opcode->insn_opnumb)
	    {
	    case 0:
	      cmd_len += msp430_nooperands (opcode, addr, insn, comm1, &cycles);
	      break;
	    case 2:
	      cmd_len +=
		msp430_doubleoperand (info, opcode, addr, insn, op1, op2,
				      comm1, comm2,
				      extension_word,
				      &cycles);
	      if (insn & BYTE_OPERATION)
		{
		  if (extension_word != 0 && ((extension_word & BYTE_OPERATION) == 0))
		    bc = ".a";
		  else
		    bc = ".b";
		}
	      else if (extension_word)
		{
		  if (extension_word & BYTE_OPERATION)
		    bc = ".w";
		  else
		    {
		      bc = ".?";
		      sprintf (comm2, _("Warning: reserved use of A/L and B/W bits detected"));
		    }
		}

	      break;
	    case 1:
	      cmd_len +=
		msp430_singleoperand (info, opcode, addr, insn, op1, comm1,
				      extension_word,
				      &cycles);
	      if (extension_word
		  && (strcmp (opcode->name, "swpb") == 0
		      || strcmp (opcode->name, "sxt") == 0))
		{
		  if (insn & BYTE_OPERATION)
		    {
		      bc = ".?";
		      sprintf (comm2, _("Warning: reserved use of A/L and B/W bits detected"));
		    }
		  else if (extension_word & BYTE_OPERATION)
		    bc = ".w";
		  else
		    bc = ".a";
		}
	      else if (insn & BYTE_OPERATION && opcode->fmt != 3)
		{
		  if (extension_word != 0 && ((extension_word & BYTE_OPERATION) == 0))
		    bc = ".a";
		  else
		    bc = ".b";
		}
	      else if (extension_word)
		{
		  if (extension_word & (1 << 6))
		    bc = ".w";
		  else
		    {
		      bc = ".?";
		      sprintf (comm2, _("Warning: reserved use of A/L and B/W bits detected"));
		    }
		}
	      break;
	    default:
	      break;
	    }
	}

      if (cmd_len)
	break;
    }

  if (cmd_len < 1)
    {
      /* Unknown opcode, or invalid combination of operands.  */
      if (extension_word)
	{
	  prin (stream, ".word	0x%04x, 0x%04x;	????", extension_word, PS (insn));
	  if (*comm1)
	    prin (stream, "\t %s", comm1);
	  return 4;
	}
      (*prin) (stream, ".word	0x%04x;	????", PS (insn));
      return 2;
    }

  /* Display the repeat count (if set) for extended register mode.  */
  if (cmd_len == 2 && ((extension_word & 0xf) != 0))
    {
      if (extension_word & (1 << 7))
	prin (stream, "rpt r%d { ", extension_word & 0xf);
      else
	prin (stream, "rpt #%d { ", (extension_word & 0xf) + 1);
    }

  /* Special case:  RRC with an extension word and the ZC bit set is actually RRU.  */
  if (extension_word
      && (extension_word & IGNORE_CARRY_BIT)
      && strcmp (opcode->name, "rrc") == 0)
    (*prin) (stream, "rrux%s", bc);
  else if (extension_word && opcode->name[strlen (opcode->name) - 1] != 'x')
    (*prin) (stream, "%sx%s", opcode->name, bc);
  else
    (*prin) (stream, "%s%s", opcode->name, bc);

  if (*op1)
    (*prin) (stream, "\t%s", op1);
  if (*op2)
    (*prin) (stream, ",");

  if (strlen (op1) < 7)
    (*prin) (stream, "\t");
  if (!strlen (op1))
    (*prin) (stream, "\t");

  if (*op2)
    (*prin) (stream, "%s", op2);
  if (strlen (op2) < 8)
    (*prin) (stream, "\t");

  if (*comm1 || *comm2)
    (*prin) (stream, ";");
  else if (cycles)
    {
      if (*op2)
	(*prin) (stream, ";");
      else
	{
	  if (strlen (op1) < 7)
	    (*prin) (stream, ";");
	  else
	    (*prin) (stream, "\t;");
	}
    }
  if (*comm1)
    (*prin) (stream, "%s", comm1);
  if (*comm1 && *comm2)
    (*prin) (stream, ",");
  if (*comm2)
    (*prin) (stream, " %s", comm2);

  if (extension_word)
    cmd_len += 2;

  return cmd_len;
}