msp430-dis.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. /* Disassemble MSP430 instructions.
  2. Copyright (C) 2002-2022 Free Software Foundation, Inc.
  3. Contributed by Dmitry Diky <diwil@mail.ru>
  4. This file is part of the GNU opcodes library.
  5. This library 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 3, or (at your option)
  8. any later version.
  9. It is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  12. License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include <sys/types.h>
  21. #include <errno.h>
  22. #include "disassemble.h"
  23. #include "opintl.h"
  24. #include "libiberty.h"
  25. #define DASM_SECTION
  26. #include "opcode/msp430.h"
  27. #undef DASM_SECTION
  28. #define PS(x) (0xffff & (x))
  29. static bool
  30. msp430dis_read_two_bytes (bfd_vma addr,
  31. disassemble_info * info,
  32. bfd_byte * buffer,
  33. char * comm)
  34. {
  35. int status;
  36. status = info->read_memory_func (addr, buffer, 2, info);
  37. if (status == 0)
  38. return true;
  39. /* PR 20150: A status of EIO means that there were no more bytes left
  40. to read in the current section. This can happen when disassembling
  41. interrupt vectors for example. Avoid cluttering the output with
  42. unhelpful error messages in this case. */
  43. if (status == EIO)
  44. {
  45. if (comm)
  46. sprintf (comm, _("Warning: disassembly unreliable - not enough bytes available"));
  47. }
  48. else
  49. {
  50. info->memory_error_func (status, addr, info);
  51. if (comm)
  52. sprintf (comm, _("Error: read from memory failed"));
  53. }
  54. return false;
  55. }
  56. static bool
  57. msp430dis_opcode_unsigned (bfd_vma addr,
  58. disassemble_info * info,
  59. unsigned short * return_val,
  60. char * comm)
  61. {
  62. bfd_byte buffer[2];
  63. if (msp430dis_read_two_bytes (addr, info, buffer, comm))
  64. {
  65. * return_val = bfd_getl16 (buffer);
  66. return true;
  67. }
  68. else
  69. {
  70. * return_val = 0;
  71. return false;
  72. }
  73. }
  74. static bool
  75. msp430dis_opcode_signed (bfd_vma addr,
  76. disassemble_info * info,
  77. signed int * return_val,
  78. char * comm)
  79. {
  80. bfd_byte buffer[2];
  81. if (msp430dis_read_two_bytes (addr, info, buffer, comm))
  82. {
  83. int status;
  84. status = bfd_getl_signed_16 (buffer);
  85. if (status & 0x8000)
  86. status |= -1U << 16;
  87. * return_val = status;
  88. return true;
  89. }
  90. else
  91. {
  92. * return_val = 0;
  93. return false;
  94. }
  95. }
  96. static int
  97. msp430_nooperands (struct msp430_opcode_s *opcode,
  98. bfd_vma addr ATTRIBUTE_UNUSED,
  99. unsigned short insn ATTRIBUTE_UNUSED,
  100. char *comm,
  101. int *cycles)
  102. {
  103. /* Pop with constant. */
  104. if (insn == 0x43b2)
  105. return 0;
  106. if (insn == opcode->bin_opcode)
  107. return 2;
  108. if (opcode->fmt == 0)
  109. {
  110. if ((insn & 0x0f00) != 0x0300 || (insn & 0x0f00) != 0x0200)
  111. return 0;
  112. strcpy (comm, "emulated...");
  113. *cycles = 1;
  114. }
  115. else
  116. {
  117. strcpy (comm, "return from interupt");
  118. *cycles = 5;
  119. }
  120. return 2;
  121. }
  122. static int
  123. print_as2_reg_name (int regno, char * op1, char * comm1,
  124. int c2, int c3, int cd)
  125. {
  126. switch (regno)
  127. {
  128. case 2:
  129. sprintf (op1, "#4");
  130. sprintf (comm1, "r2 As==10");
  131. return c2;
  132. case 3:
  133. sprintf (op1, "#2");
  134. sprintf (comm1, "r3 As==10");
  135. return c3;
  136. default:
  137. /* Indexed register mode @Rn. */
  138. sprintf (op1, "@r%d", regno);
  139. return cd;
  140. }
  141. }
  142. static int
  143. print_as3_reg_name (int regno, char * op1, char * comm1,
  144. int c2, int c3, int cd)
  145. {
  146. switch (regno)
  147. {
  148. case 2:
  149. sprintf (op1, "#8");
  150. sprintf (comm1, "r2 As==11");
  151. return c2;
  152. case 3:
  153. sprintf (op1, "#-1");
  154. sprintf (comm1, "r3 As==11");
  155. return c3;
  156. default:
  157. /* Post incremented @Rn+. */
  158. sprintf (op1, "@r%d+", regno);
  159. return cd;
  160. }
  161. }
  162. static int
  163. msp430_singleoperand (disassemble_info *info,
  164. struct msp430_opcode_s *opcode,
  165. bfd_vma addr,
  166. unsigned short insn,
  167. char *op,
  168. char *comm,
  169. unsigned short extension_word,
  170. int *cycles)
  171. {
  172. int regs = 0, regd = 0;
  173. int ad = 0, as = 0;
  174. int where = 0;
  175. int cmd_len = 2;
  176. int dst = 0;
  177. int fmt;
  178. int extended_dst = extension_word & 0xf;
  179. regd = insn & 0x0f;
  180. regs = (insn & 0x0f00) >> 8;
  181. as = (insn & 0x0030) >> 4;
  182. ad = (insn & 0x0080) >> 7;
  183. if (opcode->fmt < 0)
  184. fmt = (- opcode->fmt) - 1;
  185. else
  186. fmt = opcode->fmt;
  187. switch (fmt)
  188. {
  189. case 0: /* Emulated work with dst register. */
  190. if (regs != 2 && regs != 3 && regs != 1)
  191. return 0;
  192. /* Check if not clr insn. */
  193. if (opcode->bin_opcode == 0x4300 && (ad || as))
  194. return 0;
  195. /* Check if really inc, incd insns. */
  196. if ((opcode->bin_opcode & 0xff00) == 0x5300 && as == 3)
  197. return 0;
  198. if (ad == 0)
  199. {
  200. *cycles = 1;
  201. /* Register. */
  202. if (regd == 0)
  203. {
  204. *cycles += 1;
  205. sprintf (op, "r0");
  206. }
  207. else if (regd == 1)
  208. sprintf (op, "r1");
  209. else if (regd == 2)
  210. sprintf (op, "r2");
  211. else
  212. sprintf (op, "r%d", regd);
  213. }
  214. else /* ad == 1 msp430dis_opcode. */
  215. {
  216. if (regd == 0)
  217. {
  218. /* PC relative. */
  219. if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
  220. {
  221. cmd_len += 2;
  222. *cycles = 4;
  223. sprintf (op, "0x%04x", dst);
  224. sprintf (comm, "PC rel. abs addr 0x%04x",
  225. PS ((short) (addr + 2) + dst));
  226. if (extended_dst)
  227. {
  228. dst |= extended_dst << 16;
  229. sprintf (op, "0x%05x", dst);
  230. sprintf (comm, "PC rel. abs addr 0x%05lx",
  231. (long)((addr + 2 + dst) & 0xfffff));
  232. }
  233. }
  234. else
  235. return -1;
  236. }
  237. else if (regd == 2)
  238. {
  239. /* Absolute. */
  240. if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
  241. {
  242. cmd_len += 2;
  243. *cycles = 4;
  244. sprintf (op, "&0x%04x", PS (dst));
  245. if (extended_dst)
  246. {
  247. dst |= extended_dst << 16;
  248. sprintf (op, "&0x%05x", dst & 0xfffff);
  249. }
  250. }
  251. else
  252. return -1;
  253. }
  254. else
  255. {
  256. if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
  257. {
  258. cmd_len += 2;
  259. *cycles = 4;
  260. if (extended_dst)
  261. {
  262. dst |= extended_dst << 16;
  263. if (dst & 0x80000)
  264. dst |= -1U << 20;
  265. }
  266. sprintf (op, "%d(r%d)", dst, regd);
  267. }
  268. else
  269. return -1;
  270. }
  271. }
  272. break;
  273. case 2: /* rrc, push, call, swpb, rra, sxt, push, call, reti etc... */
  274. if (as == 0)
  275. {
  276. if (regd == 3)
  277. {
  278. /* Constsnts. */
  279. sprintf (op, "#0");
  280. sprintf (comm, "r3 As==00");
  281. }
  282. else
  283. {
  284. /* Register. */
  285. sprintf (op, "r%d", regd);
  286. }
  287. *cycles = 1;
  288. }
  289. else if (as == 2)
  290. {
  291. * cycles = print_as2_reg_name (regd, op, comm, 1, 1, 3);
  292. }
  293. else if (as == 3)
  294. {
  295. if (regd == 0)
  296. {
  297. *cycles = 3;
  298. /* absolute. @pc+ */
  299. if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
  300. {
  301. cmd_len += 2;
  302. sprintf (op, "#%d", dst);
  303. if (dst > 9 || dst < 0)
  304. sprintf (comm, "#0x%04x", PS (dst));
  305. if (extended_dst)
  306. {
  307. dst |= extended_dst << 16;
  308. if (dst & 0x80000)
  309. dst |= -1U << 20;
  310. sprintf (op, "#%d", dst);
  311. if (dst > 9 || dst < 0)
  312. sprintf (comm, "#0x%05x", dst);
  313. }
  314. }
  315. else
  316. return -1;
  317. }
  318. else
  319. * cycles = print_as3_reg_name (regd, op, comm, 1, 1, 3);
  320. }
  321. else if (as == 1)
  322. {
  323. *cycles = 4;
  324. if (regd == 0)
  325. {
  326. /* PC relative. */
  327. if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
  328. {
  329. cmd_len += 2;
  330. sprintf (op, "0x%04x", PS (dst));
  331. sprintf (comm, "PC rel. 0x%04x",
  332. PS ((short) addr + 2 + dst));
  333. if (extended_dst)
  334. {
  335. dst |= extended_dst << 16;
  336. sprintf (op, "0x%05x", dst & 0xffff);
  337. sprintf (comm, "PC rel. 0x%05lx",
  338. (long)((addr + 2 + dst) & 0xfffff));
  339. }
  340. }
  341. else
  342. return -1;
  343. }
  344. else if (regd == 2)
  345. {
  346. /* Absolute. */
  347. if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
  348. {
  349. cmd_len += 2;
  350. sprintf (op, "&0x%04x", PS (dst));
  351. if (extended_dst)
  352. {
  353. dst |= extended_dst << 16;
  354. sprintf (op, "&0x%05x", dst & 0xfffff);
  355. }
  356. }
  357. else
  358. return -1;
  359. }
  360. else if (regd == 3)
  361. {
  362. *cycles = 1;
  363. sprintf (op, "#1");
  364. sprintf (comm, "r3 As==01");
  365. }
  366. else
  367. {
  368. /* Indexed. */
  369. if (msp430dis_opcode_signed (addr + 2, info, &dst, comm))
  370. {
  371. cmd_len += 2;
  372. if (extended_dst)
  373. {
  374. dst |= extended_dst << 16;
  375. if (dst & 0x80000)
  376. dst |= -1U << 20;
  377. }
  378. sprintf (op, "%d(r%d)", dst, regd);
  379. if (dst > 9 || dst < 0)
  380. sprintf (comm, "%05x", dst);
  381. }
  382. else
  383. return -1;
  384. }
  385. }
  386. break;
  387. case 3: /* Jumps. */
  388. where = insn & 0x03ff;
  389. if (where & 0x200)
  390. where |= ~0x03ff;
  391. if (where > 512 || where < -511)
  392. return 0;
  393. where *= 2;
  394. sprintf (op, "$%+-8d", where + 2);
  395. sprintf (comm, "abs 0x%lx", (long) (addr + 2 + where));
  396. *cycles = 2;
  397. return 2;
  398. break;
  399. default:
  400. cmd_len = 0;
  401. }
  402. return cmd_len;
  403. }
  404. static int
  405. msp430_doubleoperand (disassemble_info *info,
  406. struct msp430_opcode_s *opcode,
  407. bfd_vma addr,
  408. unsigned short insn,
  409. char *op1,
  410. char *op2,
  411. char *comm1,
  412. char *comm2,
  413. unsigned short extension_word,
  414. int *cycles)
  415. {
  416. int regs = 0, regd = 0;
  417. int ad = 0, as = 0;
  418. int cmd_len = 2;
  419. int dst = 0;
  420. int fmt;
  421. int extended_dst = extension_word & 0xf;
  422. int extended_src = (extension_word >> 7) & 0xf;
  423. regd = insn & 0x0f;
  424. regs = (insn & 0x0f00) >> 8;
  425. as = (insn & 0x0030) >> 4;
  426. ad = (insn & 0x0080) >> 7;
  427. if (opcode->fmt < 0)
  428. fmt = (- opcode->fmt) - 1;
  429. else
  430. fmt = opcode->fmt;
  431. if (fmt == 0)
  432. {
  433. /* Special case: rla and rlc are the only 2 emulated instructions that
  434. fall into two operand instructions. */
  435. /* With dst, there are only:
  436. Rm Register,
  437. x(Rm) Indexed,
  438. 0xXXXX Relative,
  439. &0xXXXX Absolute
  440. emulated_ins dst
  441. basic_ins dst, dst. */
  442. if (regd != regs || as != ad)
  443. return 0; /* May be 'data' section. */
  444. if (ad == 0)
  445. {
  446. /* Register mode. */
  447. if (regd == 3)
  448. {
  449. strcpy (comm1, _("Warning: illegal as emulation instr"));
  450. return -1;
  451. }
  452. sprintf (op1, "r%d", regd);
  453. *cycles = 1;
  454. }
  455. else /* ad == 1 */
  456. {
  457. if (regd == 0)
  458. {
  459. /* PC relative, Symbolic. */
  460. if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
  461. {
  462. cmd_len += 4;
  463. *cycles = 6;
  464. sprintf (op1, "0x%04x", PS (dst));
  465. sprintf (comm1, "PC rel. 0x%04x",
  466. PS ((short) addr + 2 + dst));
  467. if (extension_word)
  468. {
  469. dst |= extended_dst << 16;
  470. if (dst & 0x80000)
  471. dst |= -1U << 20;
  472. sprintf (op1, "0x%05x", dst & 0xfffff);
  473. sprintf (comm1, "PC rel. 0x%05lx",
  474. (long)((addr + 2 + dst) & 0xfffff));
  475. }
  476. }
  477. else
  478. return -1;
  479. }
  480. else if (regd == 2)
  481. {
  482. /* Absolute. */
  483. if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
  484. {
  485. int src;
  486. /* If the 'src' field is not the same as the dst
  487. then this is not an rla instruction. */
  488. if (msp430dis_opcode_signed (addr + 4, info, &src, comm2))
  489. {
  490. if (src != dst)
  491. return 0;
  492. }
  493. else
  494. return -1;
  495. cmd_len += 4;
  496. *cycles = 6;
  497. sprintf (op1, "&0x%04x", PS (dst));
  498. if (extension_word)
  499. {
  500. dst |= extended_dst << 16;
  501. sprintf (op1, "&0x%05x", dst & 0xfffff);
  502. }
  503. }
  504. else
  505. return -1;
  506. }
  507. else
  508. {
  509. /* Indexed. */
  510. if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
  511. {
  512. if (extension_word)
  513. {
  514. dst |= extended_dst << 16;
  515. if (dst & 0x80000)
  516. dst |= -1U << 20;
  517. }
  518. cmd_len += 4;
  519. *cycles = 6;
  520. sprintf (op1, "%d(r%d)", dst, regd);
  521. if (dst > 9 || dst < -9)
  522. sprintf (comm1, "#0x%05x", dst);
  523. }
  524. else
  525. return -1;
  526. }
  527. }
  528. *op2 = 0;
  529. *comm2 = 0;
  530. return cmd_len;
  531. }
  532. /* Two operands exactly. */
  533. if (ad == 0 && regd == 3)
  534. {
  535. /* R2/R3 are illegal as dest: may be data section. */
  536. strcpy (comm1, _("Warning: illegal as 2-op instr"));
  537. return -1;
  538. }
  539. /* Source. */
  540. if (as == 0)
  541. {
  542. *cycles = 1;
  543. if (regs == 3)
  544. {
  545. /* Constants. */
  546. sprintf (op1, "#0");
  547. sprintf (comm1, "r3 As==00");
  548. }
  549. else
  550. {
  551. /* Register. */
  552. sprintf (op1, "r%d", regs);
  553. }
  554. }
  555. else if (as == 2)
  556. {
  557. * cycles = print_as2_reg_name (regs, op1, comm1, 1, 1, regs == 0 ? 3 : 2);
  558. }
  559. else if (as == 3)
  560. {
  561. if (regs == 0)
  562. {
  563. *cycles = 3;
  564. /* Absolute. @pc+. */
  565. if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
  566. {
  567. cmd_len += 2;
  568. sprintf (op1, "#%d", dst);
  569. if (dst > 9 || dst < 0)
  570. sprintf (comm1, "#0x%04x", PS (dst));
  571. if (extension_word)
  572. {
  573. dst &= 0xffff;
  574. dst |= extended_src << 16;
  575. if (dst & 0x80000)
  576. dst |= -1U << 20;
  577. sprintf (op1, "#%d", dst);
  578. if (dst > 9 || dst < 0)
  579. sprintf (comm1, "0x%05x", dst & 0xfffff);
  580. }
  581. }
  582. else
  583. return -1;
  584. }
  585. else
  586. * cycles = print_as3_reg_name (regs, op1, comm1, 1, 1, 2);
  587. }
  588. else if (as == 1)
  589. {
  590. if (regs == 0)
  591. {
  592. *cycles = 4;
  593. /* PC relative. */
  594. if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
  595. {
  596. cmd_len += 2;
  597. sprintf (op1, "0x%04x", PS (dst));
  598. sprintf (comm1, "PC rel. 0x%04x",
  599. PS ((short) addr + 2 + dst));
  600. if (extension_word)
  601. {
  602. dst &= 0xffff;
  603. dst |= extended_src << 16;
  604. if (dst & 0x80000)
  605. dst |= -1U << 20;
  606. sprintf (op1, "0x%05x", dst & 0xfffff);
  607. sprintf (comm1, "PC rel. 0x%05lx",
  608. (long) ((addr + 2 + dst) & 0xfffff));
  609. }
  610. }
  611. else
  612. return -1;
  613. }
  614. else if (regs == 2)
  615. {
  616. *cycles = 2;
  617. /* Absolute. */
  618. if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
  619. {
  620. cmd_len += 2;
  621. sprintf (op1, "&0x%04x", PS (dst));
  622. sprintf (comm1, "0x%04x", PS (dst));
  623. if (extension_word)
  624. {
  625. dst &= 0xffff;
  626. dst |= extended_src << 16;
  627. sprintf (op1, "&0x%05x", dst & 0xfffff);
  628. * comm1 = 0;
  629. }
  630. }
  631. else
  632. return -1;
  633. }
  634. else if (regs == 3)
  635. {
  636. *cycles = 1;
  637. sprintf (op1, "#1");
  638. sprintf (comm1, "r3 As==01");
  639. }
  640. else
  641. {
  642. *cycles = 3;
  643. /* Indexed. */
  644. if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
  645. {
  646. cmd_len += 2;
  647. if (extension_word)
  648. {
  649. dst &= 0xffff;
  650. dst |= extended_src << 16;
  651. if (dst & 0x80000)
  652. dst |= -1U << 20;
  653. }
  654. sprintf (op1, "%d(r%d)", dst, regs);
  655. if (dst > 9 || dst < -9)
  656. sprintf (comm1, "0x%05x", dst);
  657. }
  658. else
  659. return -1;
  660. }
  661. }
  662. /* Destination. Special care needed on addr + XXXX. */
  663. if (ad == 0)
  664. {
  665. /* Register. */
  666. if (regd == 0)
  667. {
  668. *cycles += 1;
  669. sprintf (op2, "r0");
  670. }
  671. else if (regd == 1)
  672. sprintf (op2, "r1");
  673. else if (regd == 2)
  674. sprintf (op2, "r2");
  675. else
  676. sprintf (op2, "r%d", regd);
  677. }
  678. else /* ad == 1. */
  679. {
  680. * cycles += 3;
  681. if (regd == 0)
  682. {
  683. /* PC relative. */
  684. *cycles += 1;
  685. if (msp430dis_opcode_signed (addr + cmd_len, info, &dst, comm2))
  686. {
  687. sprintf (op2, "0x%04x", PS (dst));
  688. sprintf (comm2, "PC rel. 0x%04x",
  689. PS ((short) addr + cmd_len + dst));
  690. if (extension_word)
  691. {
  692. dst |= extended_dst << 16;
  693. if (dst & 0x80000)
  694. dst |= -1U << 20;
  695. sprintf (op2, "0x%05x", dst & 0xfffff);
  696. sprintf (comm2, "PC rel. 0x%05lx",
  697. (long)((addr + cmd_len + dst) & 0xfffff));
  698. }
  699. }
  700. else
  701. return -1;
  702. cmd_len += 2;
  703. }
  704. else if (regd == 2)
  705. {
  706. /* Absolute. */
  707. if (msp430dis_opcode_signed (addr + cmd_len, info, &dst, comm2))
  708. {
  709. cmd_len += 2;
  710. sprintf (op2, "&0x%04x", PS (dst));
  711. if (extension_word)
  712. {
  713. dst |= extended_dst << 16;
  714. sprintf (op2, "&0x%05x", dst & 0xfffff);
  715. }
  716. }
  717. else
  718. return -1;
  719. }
  720. else
  721. {
  722. if (msp430dis_opcode_signed (addr + cmd_len, info, &dst, comm2))
  723. {
  724. cmd_len += 2;
  725. if (dst > 9 || dst < 0)
  726. sprintf (comm2, "0x%04x", PS (dst));
  727. if (extension_word)
  728. {
  729. dst |= extended_dst << 16;
  730. if (dst & 0x80000)
  731. dst |= -1U << 20;
  732. if (dst > 9 || dst < 0)
  733. sprintf (comm2, "0x%05x", dst & 0xfffff);
  734. }
  735. sprintf (op2, "%d(r%d)", dst, regd);
  736. }
  737. else
  738. return -1;
  739. }
  740. }
  741. return cmd_len;
  742. }
  743. static int
  744. msp430_branchinstr (disassemble_info *info,
  745. struct msp430_opcode_s *opcode ATTRIBUTE_UNUSED,
  746. bfd_vma addr ATTRIBUTE_UNUSED,
  747. unsigned short insn,
  748. char *op1,
  749. char *comm1,
  750. int *cycles)
  751. {
  752. int regs = 0, regd = 0;
  753. int as = 0;
  754. int cmd_len = 2;
  755. int dst = 0;
  756. unsigned short udst = 0;
  757. regd = insn & 0x0f;
  758. regs = (insn & 0x0f00) >> 8;
  759. as = (insn & 0x0030) >> 4;
  760. if (regd != 0) /* Destination register is not a PC. */
  761. return 0;
  762. /* dst is a source register. */
  763. if (as == 0)
  764. {
  765. /* Constants. */
  766. if (regs == 3)
  767. {
  768. *cycles = 1;
  769. sprintf (op1, "#0");
  770. sprintf (comm1, "r3 As==00");
  771. }
  772. else
  773. {
  774. /* Register. */
  775. *cycles = 1;
  776. sprintf (op1, "r%d", regs);
  777. }
  778. }
  779. else if (as == 2)
  780. {
  781. * cycles = print_as2_reg_name (regs, op1, comm1, 2, 1, 2);
  782. }
  783. else if (as == 3)
  784. {
  785. if (regs == 0)
  786. {
  787. /* Absolute. @pc+ */
  788. *cycles = 3;
  789. if (msp430dis_opcode_unsigned (addr + 2, info, &udst, comm1))
  790. {
  791. cmd_len += 2;
  792. sprintf (op1, "#0x%04x", PS (udst));
  793. }
  794. else
  795. return -1;
  796. }
  797. else
  798. * cycles = print_as3_reg_name (regs, op1, comm1, 1, 1, 2);
  799. }
  800. else if (as == 1)
  801. {
  802. * cycles = 3;
  803. if (regs == 0)
  804. {
  805. /* PC relative. */
  806. if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
  807. {
  808. cmd_len += 2;
  809. (*cycles)++;
  810. sprintf (op1, "0x%04x", PS (dst));
  811. sprintf (comm1, "PC rel. 0x%04x",
  812. PS ((short) addr + 2 + dst));
  813. }
  814. else
  815. return -1;
  816. }
  817. else if (regs == 2)
  818. {
  819. /* Absolute. */
  820. if (msp430dis_opcode_unsigned (addr + 2, info, &udst, comm1))
  821. {
  822. cmd_len += 2;
  823. sprintf (op1, "&0x%04x", PS (udst));
  824. }
  825. else
  826. return -1;
  827. }
  828. else if (regs == 3)
  829. {
  830. (*cycles)--;
  831. sprintf (op1, "#1");
  832. sprintf (comm1, "r3 As==01");
  833. }
  834. else
  835. {
  836. /* Indexed. */
  837. if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
  838. {
  839. cmd_len += 2;
  840. sprintf (op1, "%d(r%d)", dst, regs);
  841. }
  842. else
  843. return -1;
  844. }
  845. }
  846. return cmd_len;
  847. }
  848. static int
  849. msp430x_calla_instr (disassemble_info * info,
  850. bfd_vma addr,
  851. unsigned short insn,
  852. char * op1,
  853. char * comm1,
  854. int * cycles)
  855. {
  856. unsigned int ureg = insn & 0xf;
  857. int reg = insn & 0xf;
  858. int am = (insn & 0xf0) >> 4;
  859. int cmd_len = 2;
  860. unsigned short udst = 0;
  861. int dst = 0;
  862. switch (am)
  863. {
  864. case 4: /* CALLA Rdst */
  865. *cycles = 1;
  866. sprintf (op1, "r%d", reg);
  867. break;
  868. case 5: /* CALLA x(Rdst) */
  869. *cycles = 3;
  870. if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
  871. {
  872. cmd_len += 2;
  873. sprintf (op1, "%d(r%d)", dst, reg);
  874. if (reg == 0)
  875. sprintf (comm1, "PC rel. 0x%05lx", (long) (addr + 2 + dst));
  876. else
  877. sprintf (comm1, "0x%05x", dst);
  878. }
  879. else
  880. return -1;
  881. break;
  882. case 6: /* CALLA @Rdst */
  883. *cycles = 2;
  884. sprintf (op1, "@r%d", reg);
  885. break;
  886. case 7: /* CALLA @Rdst+ */
  887. *cycles = 2;
  888. sprintf (op1, "@r%d+", reg);
  889. break;
  890. case 8: /* CALLA &abs20 */
  891. if (msp430dis_opcode_unsigned (addr + 2, info, &udst, comm1))
  892. {
  893. cmd_len += 2;
  894. *cycles = 4;
  895. sprintf (op1, "&%d", (ureg << 16) + udst);
  896. sprintf (comm1, "0x%05x", (ureg << 16) + udst);
  897. }
  898. else
  899. return -1;
  900. break;
  901. case 9: /* CALLA pcrel-sym */
  902. if (msp430dis_opcode_signed (addr + 2, info, &dst, comm1))
  903. {
  904. cmd_len += 2;
  905. *cycles = 4;
  906. sprintf (op1, "%d(PC)", (reg << 16) + dst);
  907. sprintf (comm1, "PC rel. 0x%05lx",
  908. (long) (addr + 2 + dst + (reg << 16)));
  909. }
  910. else
  911. return -1;
  912. break;
  913. case 11: /* CALLA #imm20 */
  914. if (msp430dis_opcode_unsigned (addr + 2, info, &udst, comm1))
  915. {
  916. cmd_len += 2;
  917. *cycles = 4;
  918. sprintf (op1, "#%d", (ureg << 16) + udst);
  919. sprintf (comm1, "0x%05x", (ureg << 16) + udst);
  920. }
  921. else
  922. return -1;
  923. break;
  924. default:
  925. strcpy (comm1, _("Warning: unrecognised CALLA addressing mode"));
  926. return -1;
  927. }
  928. return cmd_len;
  929. }
  930. int
  931. print_insn_msp430 (bfd_vma addr, disassemble_info *info)
  932. {
  933. void *stream = info->stream;
  934. fprintf_ftype prin = info->fprintf_func;
  935. struct msp430_opcode_s *opcode;
  936. char op1[32], op2[32], comm1[64], comm2[64];
  937. int cmd_len = 0;
  938. unsigned short insn;
  939. int cycles = 0;
  940. char *bc = "";
  941. unsigned short extension_word = 0;
  942. unsigned short bits;
  943. if (! msp430dis_opcode_unsigned (addr, info, &insn, NULL))
  944. return -1;
  945. if (((int) addr & 0xffff) > 0xffdf)
  946. {
  947. (*prin) (stream, "interrupt service routine at 0x%04x", 0xffff & insn);
  948. return 2;
  949. }
  950. *comm1 = 0;
  951. *comm2 = 0;
  952. /* Check for an extension word. */
  953. if ((insn & 0xf800) == 0x1800)
  954. {
  955. extension_word = insn;
  956. addr += 2;
  957. if (! msp430dis_opcode_unsigned (addr, info, &insn, NULL))
  958. return -1;
  959. }
  960. for (opcode = msp430_opcodes; opcode->name; opcode++)
  961. {
  962. if ((insn & opcode->bin_mask) == opcode->bin_opcode
  963. && opcode->bin_opcode != 0x9300)
  964. {
  965. *op1 = 0;
  966. *op2 = 0;
  967. *comm1 = 0;
  968. *comm2 = 0;
  969. /* r0 as destination. Ad should be zero. */
  970. if (opcode->insn_opnumb == 3
  971. && (insn & 0x000f) == 0
  972. && (insn & 0x0080) == 0)
  973. {
  974. int ret =
  975. msp430_branchinstr (info, opcode, addr, insn, op1, comm1,
  976. &cycles);
  977. if (ret == -1)
  978. return -1;
  979. cmd_len += ret;
  980. if (cmd_len)
  981. break;
  982. }
  983. switch (opcode->insn_opnumb)
  984. {
  985. int n;
  986. int reg;
  987. int ret;
  988. case 4:
  989. ret = msp430x_calla_instr (info, addr, insn,
  990. op1, comm1, & cycles);
  991. if (ret == -1)
  992. return -1;
  993. cmd_len += ret;
  994. break;
  995. case 5: /* PUSHM/POPM */
  996. n = (insn & 0xf0) >> 4;
  997. reg = (insn & 0xf);
  998. sprintf (op1, "#%d", n + 1);
  999. if (opcode->bin_opcode == 0x1400)
  1000. /* PUSHM */
  1001. sprintf (op2, "r%d", reg);
  1002. else
  1003. /* POPM */
  1004. sprintf (op2, "r%d", reg + n);
  1005. if (insn & 0x100)
  1006. sprintf (comm1, "16-bit words");
  1007. else
  1008. {
  1009. sprintf (comm1, "20-bit words");
  1010. bc =".a";
  1011. }
  1012. cycles = 2; /*FIXME*/
  1013. cmd_len = 2;
  1014. break;
  1015. case 6: /* RRAM, RRCM, RRUM, RLAM. */
  1016. n = ((insn >> 10) & 0x3) + 1;
  1017. reg = (insn & 0xf);
  1018. if ((insn & 0x10) == 0)
  1019. bc =".a";
  1020. sprintf (op1, "#%d", n);
  1021. sprintf (op2, "r%d", reg);
  1022. cycles = 2; /*FIXME*/
  1023. cmd_len = 2;
  1024. break;
  1025. case 8: /* ADDA, CMPA, SUBA. */
  1026. reg = (insn & 0xf);
  1027. n = (insn >> 8) & 0xf;
  1028. if (insn & 0x40)
  1029. {
  1030. sprintf (op1, "r%d", n);
  1031. cmd_len = 2;
  1032. }
  1033. else
  1034. {
  1035. n <<= 16;
  1036. if (msp430dis_opcode_unsigned (addr + 2, info, &bits, comm1))
  1037. {
  1038. n |= bits;
  1039. sprintf (op1, "#%d", n);
  1040. if (n > 9 || n < 0)
  1041. sprintf (comm1, "0x%05x", n);
  1042. }
  1043. else
  1044. return -1;
  1045. cmd_len = 4;
  1046. }
  1047. sprintf (op2, "r%d", reg);
  1048. cycles = 2; /*FIXME*/
  1049. break;
  1050. case 9: /* MOVA */
  1051. reg = (insn & 0xf);
  1052. n = (insn >> 8) & 0xf;
  1053. switch ((insn >> 4) & 0xf)
  1054. {
  1055. case 0: /* MOVA @Rsrc, Rdst */
  1056. cmd_len = 2;
  1057. sprintf (op1, "@r%d", n);
  1058. if (strcmp (opcode->name, "bra") != 0)
  1059. sprintf (op2, "r%d", reg);
  1060. break;
  1061. case 1: /* MOVA @Rsrc+, Rdst */
  1062. cmd_len = 2;
  1063. if (strcmp (opcode->name, "reta") != 0)
  1064. {
  1065. sprintf (op1, "@r%d+", n);
  1066. if (strcmp (opcode->name, "bra") != 0)
  1067. sprintf (op2, "r%d", reg);
  1068. }
  1069. break;
  1070. case 2: /* MOVA &abs20, Rdst */
  1071. cmd_len = 4;
  1072. n <<= 16;
  1073. if (msp430dis_opcode_unsigned (addr + 2, info, &bits, comm1))
  1074. {
  1075. n |= bits;
  1076. sprintf (op1, "&%d", n);
  1077. if (n > 9 || n < 0)
  1078. sprintf (comm1, "0x%05x", n);
  1079. if (strcmp (opcode->name, "bra") != 0)
  1080. sprintf (op2, "r%d", reg);
  1081. }
  1082. else
  1083. return -1;
  1084. break;
  1085. case 3: /* MOVA x(Rsrc), Rdst */
  1086. cmd_len = 4;
  1087. if (strcmp (opcode->name, "bra") != 0)
  1088. sprintf (op2, "r%d", reg);
  1089. reg = n;
  1090. if (msp430dis_opcode_signed (addr + 2, info, &n, comm1))
  1091. {
  1092. sprintf (op1, "%d(r%d)", n, reg);
  1093. if (n > 9 || n < 0)
  1094. {
  1095. if (reg == 0)
  1096. sprintf (comm1, "PC rel. 0x%05lx",
  1097. (long) (addr + 2 + n));
  1098. else
  1099. sprintf (comm1, "0x%05x", n);
  1100. }
  1101. }
  1102. else
  1103. return -1;
  1104. break;
  1105. case 6: /* MOVA Rsrc, &abs20 */
  1106. cmd_len = 4;
  1107. reg <<= 16;
  1108. if (msp430dis_opcode_unsigned (addr + 2, info, &bits, comm2))
  1109. {
  1110. reg |= bits;
  1111. sprintf (op1, "r%d", n);
  1112. sprintf (op2, "&%d", reg);
  1113. if (reg > 9 || reg < 0)
  1114. sprintf (comm2, "0x%05x", reg);
  1115. }
  1116. else
  1117. return -1;
  1118. break;
  1119. case 7: /* MOVA Rsrc, x(Rdst) */
  1120. cmd_len = 4;
  1121. sprintf (op1, "r%d", n);
  1122. if (msp430dis_opcode_signed (addr + 2, info, &n, comm2))
  1123. {
  1124. sprintf (op2, "%d(r%d)", n, reg);
  1125. if (n > 9 || n < 0)
  1126. {
  1127. if (reg == 0)
  1128. sprintf (comm2, "PC rel. 0x%05lx",
  1129. (long) (addr + 2 + n));
  1130. else
  1131. sprintf (comm2, "0x%05x", n);
  1132. }
  1133. }
  1134. else
  1135. return -1;
  1136. break;
  1137. case 8: /* MOVA #imm20, Rdst */
  1138. cmd_len = 4;
  1139. n <<= 16;
  1140. if (msp430dis_opcode_unsigned (addr + 2, info, &bits, comm1))
  1141. {
  1142. n |= bits;
  1143. if (n & 0x80000)
  1144. n |= -1U << 20;
  1145. sprintf (op1, "#%d", n);
  1146. if (n > 9 || n < 0)
  1147. sprintf (comm1, "0x%05x", n);
  1148. if (strcmp (opcode->name, "bra") != 0)
  1149. sprintf (op2, "r%d", reg);
  1150. }
  1151. else
  1152. return -1;
  1153. break;
  1154. case 12: /* MOVA Rsrc, Rdst */
  1155. cmd_len = 2;
  1156. sprintf (op1, "r%d", n);
  1157. if (strcmp (opcode->name, "bra") != 0)
  1158. sprintf (op2, "r%d", reg);
  1159. break;
  1160. default:
  1161. break;
  1162. }
  1163. cycles = 2; /* FIXME */
  1164. break;
  1165. }
  1166. if (cmd_len)
  1167. break;
  1168. switch (opcode->insn_opnumb)
  1169. {
  1170. int ret;
  1171. case 0:
  1172. cmd_len += msp430_nooperands (opcode, addr, insn, comm1, &cycles);
  1173. break;
  1174. case 2:
  1175. ret =
  1176. msp430_doubleoperand (info, opcode, addr, insn, op1, op2,
  1177. comm1, comm2,
  1178. extension_word,
  1179. &cycles);
  1180. if (ret == -1)
  1181. return -1;
  1182. cmd_len += ret;
  1183. if (insn & BYTE_OPERATION)
  1184. {
  1185. if (extension_word != 0 && ((extension_word & BYTE_OPERATION) == 0))
  1186. bc = ".a";
  1187. else
  1188. bc = ".b";
  1189. }
  1190. else if (extension_word)
  1191. {
  1192. if (extension_word & BYTE_OPERATION)
  1193. bc = ".w";
  1194. else
  1195. {
  1196. bc = ".?";
  1197. sprintf (comm2, _("Warning: reserved use of A/L and B/W bits detected"));
  1198. }
  1199. }
  1200. break;
  1201. case 1:
  1202. ret =
  1203. msp430_singleoperand (info, opcode, addr, insn, op1, comm1,
  1204. extension_word,
  1205. &cycles);
  1206. if (ret == -1)
  1207. return -1;
  1208. cmd_len += ret;
  1209. if (extension_word
  1210. && (strcmp (opcode->name, "swpb") == 0
  1211. || strcmp (opcode->name, "sxt") == 0))
  1212. {
  1213. if (insn & BYTE_OPERATION)
  1214. {
  1215. bc = ".?";
  1216. sprintf (comm2, _("Warning: reserved use of A/L and B/W bits detected"));
  1217. }
  1218. else if (extension_word & BYTE_OPERATION)
  1219. bc = ".w";
  1220. else
  1221. bc = ".a";
  1222. }
  1223. else if (insn & BYTE_OPERATION && opcode->fmt != 3)
  1224. {
  1225. if (extension_word != 0 && ((extension_word & BYTE_OPERATION) == 0))
  1226. bc = ".a";
  1227. else
  1228. bc = ".b";
  1229. }
  1230. else if (extension_word)
  1231. {
  1232. if (extension_word & (1 << 6))
  1233. bc = ".w";
  1234. else
  1235. {
  1236. bc = ".?";
  1237. sprintf (comm2, _("Warning: reserved use of A/L and B/W bits detected"));
  1238. }
  1239. }
  1240. break;
  1241. default:
  1242. break;
  1243. }
  1244. }
  1245. if (cmd_len)
  1246. break;
  1247. }
  1248. if (cmd_len < 1)
  1249. {
  1250. /* Unknown opcode, or invalid combination of operands. */
  1251. if (extension_word)
  1252. {
  1253. prin (stream, ".word 0x%04x, 0x%04x; ????", extension_word, PS (insn));
  1254. if (*comm1)
  1255. prin (stream, "\t %s", comm1);
  1256. return 4;
  1257. }
  1258. (*prin) (stream, ".word 0x%04x; ????", PS (insn));
  1259. return 2;
  1260. }
  1261. /* Display the repeat count (if set) for extended register mode. */
  1262. if (cmd_len == 2 && ((extension_word & 0xf) != 0))
  1263. {
  1264. if (extension_word & (1 << 7))
  1265. prin (stream, "rpt r%d { ", extension_word & 0xf);
  1266. else
  1267. prin (stream, "rpt #%d { ", (extension_word & 0xf) + 1);
  1268. }
  1269. /* Special case: RRC with an extension word and the ZC bit set is actually RRU. */
  1270. if (extension_word
  1271. && (extension_word & IGNORE_CARRY_BIT)
  1272. && strcmp (opcode->name, "rrc") == 0)
  1273. (*prin) (stream, "rrux%s", bc);
  1274. else if (extension_word && opcode->name[strlen (opcode->name) - 1] != 'x')
  1275. (*prin) (stream, "%sx%s", opcode->name, bc);
  1276. else
  1277. (*prin) (stream, "%s%s", opcode->name, bc);
  1278. if (*op1)
  1279. (*prin) (stream, "\t%s", op1);
  1280. if (*op2)
  1281. (*prin) (stream, ",");
  1282. if (strlen (op1) < 7)
  1283. (*prin) (stream, "\t");
  1284. if (!strlen (op1))
  1285. (*prin) (stream, "\t");
  1286. if (*op2)
  1287. (*prin) (stream, "%s", op2);
  1288. if (strlen (op2) < 8)
  1289. (*prin) (stream, "\t");
  1290. if (*comm1 || *comm2)
  1291. (*prin) (stream, ";");
  1292. else if (cycles)
  1293. {
  1294. if (*op2)
  1295. (*prin) (stream, ";");
  1296. else
  1297. {
  1298. if (strlen (op1) < 7)
  1299. (*prin) (stream, ";");
  1300. else
  1301. (*prin) (stream, "\t;");
  1302. }
  1303. }
  1304. if (*comm1)
  1305. (*prin) (stream, "%s", comm1);
  1306. if (*comm1 && *comm2)
  1307. (*prin) (stream, ",");
  1308. if (*comm2)
  1309. (*prin) (stream, " %s", comm2);
  1310. if (extension_word)
  1311. cmd_len += 2;
  1312. return cmd_len;
  1313. }