m68hc11-dis.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /* m68hc11-dis.c -- Motorola 68HC11 & 68HC12 disassembly
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. Written by Stephane Carrez (stcarrez@nerim.fr)
  4. XGATE and S12X added by James Murray (jsm@jsm-net.demon.co.uk)
  5. This file is part of the GNU opcodes library.
  6. This library is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. It is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  13. License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  17. MA 02110-1301, USA. */
  18. #include "sysdep.h"
  19. #include <stdio.h>
  20. #include "opcode/m68hc11.h"
  21. #include "disassemble.h"
  22. #define PC_REGNUM 3
  23. static const char *const reg_name[] =
  24. {
  25. "X", "Y", "SP", "PC"
  26. };
  27. static const char *const reg_src_table[] =
  28. {
  29. "A", "B", "CCR", "TMP3", "D", "X", "Y", "SP"
  30. };
  31. static const char *const reg_dst_table[] =
  32. {
  33. "A", "B", "CCR", "TMP2", "D", "X", "Y", "SP"
  34. };
  35. #define OP_PAGE_MASK (M6811_OP_PAGE2|M6811_OP_PAGE3|M6811_OP_PAGE4)
  36. static int
  37. read_memory (bfd_vma memaddr, bfd_byte* buffer, int size,
  38. struct disassemble_info* info)
  39. {
  40. int status;
  41. /* Get first byte. Only one at a time because we don't know the
  42. size of the insn. */
  43. status = (*info->read_memory_func) (memaddr, buffer, size, info);
  44. if (status != 0)
  45. {
  46. (*info->memory_error_func) (status, memaddr, info);
  47. return -1;
  48. }
  49. return 0;
  50. }
  51. /* Read the 68HC12 indexed operand byte and print the corresponding mode.
  52. Returns the number of bytes read or -1 if failure. */
  53. static int
  54. print_indexed_operand (bfd_vma memaddr, struct disassemble_info* info,
  55. int* indirect, int mov_insn, int pc_offset,
  56. bfd_vma endaddr, int arch)
  57. {
  58. bfd_byte buffer[4];
  59. int reg;
  60. int status;
  61. bfd_vma val;
  62. int pos = 1;
  63. if (indirect)
  64. *indirect = 0;
  65. status = read_memory (memaddr, &buffer[0], 1, info);
  66. if (status != 0)
  67. {
  68. return status;
  69. }
  70. /* n,r with 5-bits signed constant. */
  71. if ((buffer[0] & 0x20) == 0)
  72. {
  73. reg = (buffer[0] >> 6) & 3;
  74. val = ((buffer[0] & 0x1f) ^ 0x10) - 0x10;
  75. /* 68HC12 requires an adjustment for movb/movw pc relative modes. */
  76. if (reg == PC_REGNUM && info->mach == bfd_mach_m6812 && mov_insn)
  77. val += pc_offset;
  78. (*info->fprintf_func) (info->stream, "0x%x,%s",
  79. (unsigned) val & 0xffff, reg_name[reg]);
  80. if (reg == PC_REGNUM)
  81. {
  82. (* info->fprintf_func) (info->stream, " {");
  83. /* Avoid duplicate 0x from core binutils. */
  84. if (info->symtab_size > 0)
  85. (*info->fprintf_func) (info->stream, "0x");
  86. (* info->print_address_func) (endaddr + val, info);
  87. (* info->fprintf_func) (info->stream, "}");
  88. }
  89. }
  90. /* Auto pre/post increment/decrement. */
  91. else if ((buffer[0] & 0xc0) != 0xc0)
  92. {
  93. const char *mode;
  94. reg = (buffer[0] >> 6) & 3;
  95. val = buffer[0] & 7;
  96. if (buffer[0] & 8)
  97. {
  98. val = 8 - val;
  99. mode = "-";
  100. }
  101. else
  102. {
  103. val = val + 1;
  104. mode = "+";
  105. }
  106. (*info->fprintf_func) (info->stream, "%d,%s%s%s",
  107. (unsigned) val,
  108. buffer[0] & 0x10 ? "" : mode,
  109. reg_name[reg], buffer[0] & 0x10 ? mode : "");
  110. }
  111. /* [n,r] 16-bits offset indexed indirect. */
  112. else if ((buffer[0] & 0x07) == 3)
  113. {
  114. if ((mov_insn) && (!(arch & cpu9s12x)))
  115. {
  116. (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
  117. buffer[0] & 0x0ff);
  118. return 0;
  119. }
  120. reg = (buffer[0] >> 3) & 0x03;
  121. status = read_memory (memaddr + pos, &buffer[0], 2, info);
  122. if (status != 0)
  123. return status;
  124. pos += 2;
  125. val = (buffer[0] << 8) | buffer[1];
  126. (*info->fprintf_func) (info->stream, "[0x%x,%s]",
  127. (unsigned) val & 0xffff, reg_name[reg]);
  128. if (indirect)
  129. *indirect = 1;
  130. }
  131. /* n,r with 9 and 16 bit signed constant. */
  132. else if ((buffer[0] & 0x4) == 0)
  133. {
  134. if ((mov_insn) && (!(arch & cpu9s12x)))
  135. {
  136. (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
  137. buffer[0] & 0x0ff);
  138. return 0;
  139. }
  140. reg = (buffer[0] >> 3) & 0x03;
  141. status = read_memory (memaddr + pos,
  142. &buffer[1], (buffer[0] & 0x2 ? 2 : 1), info);
  143. if (status != 0)
  144. return status;
  145. if (buffer[0] & 2)
  146. {
  147. val = (((buffer[1] << 8) | buffer[2]) ^ 0x8000) - 0x8000;
  148. pos += 2;
  149. endaddr += 2;
  150. }
  151. else
  152. {
  153. val = buffer[1] - ((buffer[0] & 1) << 8);
  154. pos++;
  155. endaddr++;
  156. }
  157. (*info->fprintf_func) (info->stream, "0x%x,%s",
  158. (unsigned) val & 0xffff, reg_name[reg]);
  159. if (reg == PC_REGNUM)
  160. {
  161. (* info->fprintf_func) (info->stream, " {0x");
  162. (* info->print_address_func) (endaddr + val, info);
  163. (* info->fprintf_func) (info->stream, "}");
  164. }
  165. }
  166. else
  167. {
  168. reg = (buffer[0] >> 3) & 0x03;
  169. switch (buffer[0] & 3)
  170. {
  171. case 0:
  172. (*info->fprintf_func) (info->stream, "A,%s", reg_name[reg]);
  173. break;
  174. case 1:
  175. (*info->fprintf_func) (info->stream, "B,%s", reg_name[reg]);
  176. break;
  177. case 2:
  178. (*info->fprintf_func) (info->stream, "D,%s", reg_name[reg]);
  179. break;
  180. case 3:
  181. default:
  182. (*info->fprintf_func) (info->stream, "[D,%s]", reg_name[reg]);
  183. if (indirect)
  184. *indirect = 1;
  185. break;
  186. }
  187. }
  188. return pos;
  189. }
  190. /* Disassemble one instruction at address 'memaddr'. Returns the number
  191. of bytes used by that instruction. */
  192. static int
  193. print_insn (bfd_vma memaddr, struct disassemble_info* info, int arch)
  194. {
  195. int status;
  196. bfd_byte buffer[4];
  197. unsigned int code;
  198. long format, pos, i;
  199. bfd_vma val;
  200. const struct m68hc11_opcode *opcode;
  201. if (arch & cpuxgate)
  202. {
  203. /* Get two bytes as all XGATE instructions are 16bit. */
  204. status = read_memory (memaddr, buffer, 2, info);
  205. if (status != 0)
  206. return status;
  207. format = 0;
  208. code = (buffer[0] << 8) + buffer[1];
  209. /* Scan the opcode table until we find the opcode
  210. with the corresponding page. */
  211. opcode = m68hc11_opcodes;
  212. for (i = 0; i < m68hc11_num_opcodes; i++, opcode++)
  213. {
  214. if ((opcode->opcode != (code & opcode->xg_mask)) || (opcode->arch != cpuxgate))
  215. continue;
  216. /* We have found the opcode. Extract the operand and print it. */
  217. (*info->fprintf_func) (info->stream, "%s", opcode->name);
  218. format = opcode->format;
  219. if (format & (M68XG_OP_NONE))
  220. {
  221. /* Nothing to print. */
  222. }
  223. else if (format & M68XG_OP_IMM3)
  224. (*info->fprintf_func) (info->stream, " #0x%x", (code >> 8) & 0x7);
  225. else if (format & M68XG_OP_R_R)
  226. (*info->fprintf_func) (info->stream, " R%x, R%x",
  227. (code >> 8) & 0x7, (code >> 5) & 0x7);
  228. else if (format & M68XG_OP_R_R_R)
  229. (*info->fprintf_func) (info->stream, " R%x, R%x, R%x",
  230. (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
  231. else if (format & M68XG_OP_RD_RB_RI)
  232. (*info->fprintf_func) (info->stream, " R%x, (R%x, R%x)",
  233. (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
  234. else if (format & M68XG_OP_RD_RB_RIp)
  235. (*info->fprintf_func) (info->stream, " R%x, (R%x, R%x+)",
  236. (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
  237. else if (format & M68XG_OP_RD_RB_mRI)
  238. (*info->fprintf_func) (info->stream, " R%x, (R%x, -R%x)",
  239. (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
  240. else if (format & M68XG_OP_R_R_OFFS5)
  241. (*info->fprintf_func) (info->stream, " R%x, (R%x, #0x%x)",
  242. (code >> 8) & 0x7, (code >> 5) & 0x7, code & 0x1f);
  243. else if (format & M68XG_OP_R_IMM8)
  244. (*info->fprintf_func) (info->stream, " R%x, #0x%02x",
  245. (code >> 8) & 0x7, code & 0xff);
  246. else if (format & M68XG_OP_R_IMM4)
  247. (*info->fprintf_func) (info->stream, " R%x, #0x%x",
  248. (code >> 8) & 0x7, (code & 0xf0) >> 4);
  249. else if (format & M68XG_OP_REL9)
  250. {
  251. (*info->fprintf_func) (info->stream, " 0x");
  252. val = buffer[1] - ((buffer[0] & 1) << 8);
  253. (*info->print_address_func) (memaddr + (val << 1) + 2, info);
  254. }
  255. else if (format & M68XG_OP_REL10)
  256. {
  257. (*info->fprintf_func) (info->stream, " 0x");
  258. val = (buffer[0] << 8) | buffer[1];
  259. val = ((val & 0x3ff) ^ 0x200) - 0x200;
  260. (*info->print_address_func) (memaddr + (val << 1) + 2, info);
  261. }
  262. else if ((code & 0x00ff) == 0x00f8)
  263. (*info->fprintf_func) (info->stream, " R%x, CCR", (code >> 8) & 0x7);
  264. else if ((code & 0x00ff) == 0x00f9)
  265. (*info->fprintf_func) (info->stream, " CCR, R%x", (code >> 8) & 0x7);
  266. else if ((code & 0x00ff) == 0x0)
  267. (*info->fprintf_func) (info->stream, " R%x, PC", (code >> 8) & 0x7);
  268. else if (format & M68XG_OP_R)
  269. {
  270. /* Special cases for TFR. */
  271. if ((code & 0xf8ff) == 0x00f8)
  272. (*info->fprintf_func) (info->stream, " R%x, CCR", (code >> 8) & 0x7);
  273. else if ((code & 0xf8ff) == 0x00f9)
  274. (*info->fprintf_func) (info->stream, " CCR, R%x", (code >> 8) & 0x7);
  275. else if ((code & 0xf8ff) == 0x00fa)
  276. (*info->fprintf_func) (info->stream, " R%x, PC", (code >> 8) & 0x7);
  277. else
  278. (*info->fprintf_func) (info->stream, " R%x", (code >> 8) & 0x7);
  279. }
  280. else
  281. /* Opcode not recognized. */
  282. (*info->fprintf_func) (info->stream, "Not yet handled TEST .byte\t0x%04x", code);
  283. return 2;
  284. }
  285. /* Opcode not recognized. */
  286. (*info->fprintf_func) (info->stream, ".byte\t0x%04x", code);
  287. return 2; /* Everything is two bytes. */
  288. }
  289. /* HC11 and HC12. */
  290. /* Get first byte. Only one at a time because we don't know the
  291. size of the insn. */
  292. status = read_memory (memaddr, buffer, 1, info);
  293. if (status != 0)
  294. return status;
  295. format = 0;
  296. code = buffer[0];
  297. pos = 0;
  298. /* Look for page2,3,4 opcodes. */
  299. if (code == M6811_OPCODE_PAGE2)
  300. {
  301. pos++;
  302. format = M6811_OP_PAGE2;
  303. }
  304. else if (code == M6811_OPCODE_PAGE3 && arch == cpu6811)
  305. {
  306. pos++;
  307. format = M6811_OP_PAGE3;
  308. }
  309. else if (code == M6811_OPCODE_PAGE4 && arch == cpu6811)
  310. {
  311. pos++;
  312. format = M6811_OP_PAGE4;
  313. }
  314. /* We are in page2,3,4; get the real opcode. */
  315. if (pos == 1)
  316. {
  317. status = read_memory (memaddr + pos, &buffer[1], 1, info);
  318. if (status != 0)
  319. return status;
  320. code = buffer[1];
  321. }
  322. /* Look first for a 68HC12 alias. All of them are 2-bytes long and
  323. in page 1. There is no operand to print. We read the second byte
  324. only when we have a possible match. */
  325. if ((arch & cpu6812) && format == 0)
  326. {
  327. int must_read = 1;
  328. /* Walk the alias table to find a code1+code2 match. */
  329. for (i = 0; i < m68hc12_num_alias; i++)
  330. {
  331. if (m68hc12_alias[i].code1 == code)
  332. {
  333. if (must_read)
  334. {
  335. status = read_memory (memaddr + pos + 1,
  336. &buffer[1], 1, info);
  337. if (status != 0)
  338. break;
  339. must_read = 1;
  340. }
  341. if (m68hc12_alias[i].code2 == (unsigned char) buffer[1])
  342. {
  343. (*info->fprintf_func) (info->stream, "%s",
  344. m68hc12_alias[i].name);
  345. return 2;
  346. }
  347. }
  348. }
  349. }
  350. pos++;
  351. /* Scan the opcode table until we find the opcode
  352. with the corresponding page. */
  353. opcode = m68hc11_opcodes;
  354. for (i = 0; i < m68hc11_num_opcodes; i++, opcode++)
  355. {
  356. int offset;
  357. int pc_src_offset;
  358. int pc_dst_offset = 0;
  359. if ((opcode->arch & arch) == 0)
  360. continue;
  361. if (opcode->opcode != code)
  362. continue;
  363. if ((opcode->format & OP_PAGE_MASK) != format)
  364. continue;
  365. if (opcode->format & M6812_OP_REG)
  366. {
  367. int j;
  368. int is_jump;
  369. if (opcode->format & M6811_OP_JUMP_REL)
  370. is_jump = 1;
  371. else
  372. is_jump = 0;
  373. status = read_memory (memaddr + pos, &buffer[0], 1, info);
  374. if (status != 0)
  375. {
  376. return status;
  377. }
  378. for (j = 0; i + j < m68hc11_num_opcodes; j++)
  379. {
  380. if ((opcode[j].arch & arch) == 0)
  381. continue;
  382. if (opcode[j].opcode != code)
  383. continue;
  384. if (is_jump)
  385. {
  386. if (!(opcode[j].format & M6811_OP_JUMP_REL))
  387. continue;
  388. if ((opcode[j].format & M6812_OP_IBCC_MARKER)
  389. && (buffer[0] & 0xc0) != 0x80)
  390. continue;
  391. if ((opcode[j].format & M6812_OP_TBCC_MARKER)
  392. && (buffer[0] & 0xc0) != 0x40)
  393. continue;
  394. if ((opcode[j].format & M6812_OP_DBCC_MARKER)
  395. && (buffer[0] & 0xc0) != 0)
  396. continue;
  397. if ((opcode[j].format & M6812_OP_EQ_MARKER)
  398. && (buffer[0] & 0x20) == 0)
  399. break;
  400. if (!(opcode[j].format & M6812_OP_EQ_MARKER)
  401. && (buffer[0] & 0x20) != 0)
  402. break;
  403. continue;
  404. }
  405. if (opcode[j].format & M6812_OP_EXG_MARKER && buffer[0] & 0x80)
  406. break;
  407. if ((opcode[j].format & M6812_OP_SEX_MARKER)
  408. && (((buffer[0] & 0x07) >= 3 && (buffer[0] & 7) <= 7))
  409. && ((buffer[0] & 0x0f0) <= 0x20))
  410. break;
  411. if ((opcode[j].format & M6812_OP_SEX_MARKER)
  412. && (arch & cpu9s12x)
  413. && ((buffer[0] == 0x4d) || (buffer[0] == 0x4e)))
  414. break;
  415. if (opcode[j].format & M6812_OP_TFR_MARKER
  416. && !(buffer[0] & 0x80))
  417. break;
  418. }
  419. if (i + j < m68hc11_num_opcodes)
  420. opcode = &opcode[j];
  421. }
  422. /* We have found the opcode. Extract the operand and print it. */
  423. (*info->fprintf_func) (info->stream, "%s", opcode->name);
  424. format = opcode->format;
  425. if (format & (M6811_OP_MASK | M6811_OP_BITMASK
  426. | M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
  427. {
  428. (*info->fprintf_func) (info->stream, "\t");
  429. }
  430. /* The movb and movw must be handled in a special way...
  431. The source constant 'ii' is not always at the same place.
  432. This is the same for the destination for the post-indexed byte.
  433. The 'offset' is used to do the appropriate correction.
  434. offset offset
  435. for constant for destination
  436. movb 18 OB ii hh ll 0 0
  437. 18 08 xb ii 1 -1
  438. 18 08 xb ff ii 2 1 9 bit
  439. 18 08 xb ee ff ii 3 1 16 bit
  440. 18 0C hh ll hh ll 0 0
  441. 18 09 xb hh ll 1 -1
  442. 18 0D xb hh ll 0 0
  443. 18 0A xb xb 0 0
  444. movw 18 03 jj kk hh ll 0 0
  445. 18 00 xb jj kk 1 -1
  446. 18 04 hh ll hh ll 0 0
  447. 18 01 xb hh ll 1 -1
  448. 18 05 xb hh ll 0 0
  449. 18 02 xb xb 0 0
  450. After the source operand is read, the position 'pos' is incremented
  451. this explains the negative offset for destination.
  452. movb/movw above are the only instructions with this matching
  453. format. */
  454. offset = ((format & M6812_OP_IDX_P2)
  455. && (format & (M6811_OP_IMM8 | M6811_OP_IMM16 |
  456. M6811_OP_IND16)));
  457. if (offset)
  458. {
  459. /* Check xb to see position of data. */
  460. status = read_memory (memaddr + pos, &buffer[0], 1, info);
  461. if (status != 0)
  462. {
  463. return status;
  464. }
  465. if (((buffer[0] & 0xe0) == 0xe0) && ((buffer[0] & 0x04) == 0))
  466. {
  467. /* 9 or 16 bit. */
  468. if ((buffer[0] & 0x02) == 0)
  469. {
  470. /* 9 bit. */
  471. offset = 2;
  472. }
  473. else
  474. {
  475. /* 16 bit. */
  476. offset = 3;
  477. }
  478. }
  479. }
  480. /* Operand with one more byte: - immediate, offset,
  481. direct-low address. */
  482. if (format &
  483. (M6811_OP_IMM8 | M6811_OP_IX | M6811_OP_IY | M6811_OP_DIRECT))
  484. {
  485. status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
  486. if (status != 0)
  487. return status;
  488. /* This movb/movw is special (see above). */
  489. if (offset < 2)
  490. {
  491. offset = -offset;
  492. pc_dst_offset = 2;
  493. }
  494. else
  495. {
  496. offset = -1;
  497. pc_dst_offset = 5;
  498. }
  499. pos++;
  500. if (format & M6811_OP_IMM8)
  501. {
  502. (*info->fprintf_func) (info->stream, "#0x%x", (int) buffer[0]);
  503. format &= ~M6811_OP_IMM8;
  504. /* Set PC destination offset. */
  505. pc_dst_offset = 1;
  506. }
  507. else if (format & M6811_OP_IX)
  508. {
  509. /* Offsets are in range 0..255, print them unsigned. */
  510. (*info->fprintf_func) (info->stream, "0x%x,x", buffer[0] & 0x0FF);
  511. format &= ~M6811_OP_IX;
  512. }
  513. else if (format & M6811_OP_IY)
  514. {
  515. (*info->fprintf_func) (info->stream, "0x%x,y", buffer[0] & 0x0FF);
  516. format &= ~M6811_OP_IY;
  517. }
  518. else if (format & M6811_OP_DIRECT)
  519. {
  520. (*info->fprintf_func) (info->stream, "*");
  521. if (info->symtab_size > 0) /* Avoid duplicate 0x. */
  522. (*info->fprintf_func) (info->stream, "0x");
  523. (*info->print_address_func) (buffer[0] & 0x0FF, info);
  524. format &= ~M6811_OP_DIRECT;
  525. }
  526. }
  527. #define M6812_DST_MOVE (M6812_OP_IND16_P2 | M6812_OP_IDX_P2)
  528. #define M6812_INDEXED_FLAGS (M6812_OP_IDX|M6812_OP_IDX_1|M6812_OP_IDX_2)
  529. /* Analyze the 68HC12 indexed byte. */
  530. if (format & M6812_INDEXED_FLAGS)
  531. {
  532. int indirect;
  533. bfd_vma endaddr;
  534. endaddr = memaddr + pos + 1;
  535. if (format & M6811_OP_IND16)
  536. endaddr += 2;
  537. pc_src_offset = -1;
  538. pc_dst_offset = 1;
  539. status = print_indexed_operand (memaddr + pos, info, &indirect,
  540. (format & M6812_DST_MOVE),
  541. pc_src_offset, endaddr, arch);
  542. if (status < 0)
  543. return status;
  544. pos += status;
  545. /* The indirect addressing mode of the call instruction does
  546. not need the page code. */
  547. if ((format & M6812_OP_PAGE) && indirect)
  548. format &= ~M6812_OP_PAGE;
  549. }
  550. /* 68HC12 dbcc/ibcc/tbcc operands. */
  551. if ((format & M6812_OP_REG) && (format & M6811_OP_JUMP_REL))
  552. {
  553. status = read_memory (memaddr + pos, &buffer[0], 2, info);
  554. if (status != 0)
  555. return status;
  556. (*info->fprintf_func) (info->stream, "%s,",
  557. reg_src_table[buffer[0] & 0x07]);
  558. val = buffer[1] - ((buffer[0] & 0x10) << 4);
  559. pos += 2;
  560. (*info->fprintf_func) (info->stream, "0x");
  561. (*info->print_address_func) (memaddr + pos + val, info);
  562. format &= ~(M6812_OP_REG | M6811_OP_JUMP_REL);
  563. }
  564. else if (format & (M6812_OP_REG | M6812_OP_REG_2))
  565. {
  566. status = read_memory (memaddr + pos, &buffer[0], 1, info);
  567. if (status != 0)
  568. return status;
  569. pos++;
  570. (*info->fprintf_func) (info->stream, "%s,%s",
  571. reg_src_table[(buffer[0] >> 4) & 7],
  572. reg_dst_table[(buffer[0] & 7)]);
  573. }
  574. if (format & (M6811_OP_IMM16 | M6811_OP_IND16))
  575. {
  576. bfd_vma addr;
  577. unsigned page = 0;
  578. status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
  579. if (status != 0)
  580. return status;
  581. if (format & M6812_OP_IDX_P2)
  582. offset = -2;
  583. else
  584. offset = 0;
  585. pos += 2;
  586. addr = val = (buffer[0] << 8) | buffer[1];
  587. pc_dst_offset = 2;
  588. if (format & M6812_OP_PAGE)
  589. {
  590. status = read_memory (memaddr + pos + offset, buffer, 1, info);
  591. if (status != 0)
  592. return status;
  593. page = buffer[0];
  594. if (addr >= M68HC12_BANK_BASE && addr < 0x0c000)
  595. addr = (val - M68HC12_BANK_BASE + (page << M68HC12_BANK_SHIFT)
  596. + M68HC12_BANK_VIRT);
  597. }
  598. else if ((arch & cpu6812)
  599. && addr >= M68HC12_BANK_BASE && addr < 0x0c000)
  600. {
  601. unsigned cur_page;
  602. bfd_vma vaddr;
  603. if (memaddr >= M68HC12_BANK_VIRT)
  604. cur_page = ((memaddr - M68HC12_BANK_VIRT)
  605. >> M68HC12_BANK_SHIFT);
  606. else
  607. cur_page = 0;
  608. vaddr = (addr - M68HC12_BANK_BASE
  609. + (cur_page << M68HC12_BANK_SHIFT)) + M68HC12_BANK_VIRT;
  610. if (!info->symbol_at_address_func (addr, info)
  611. && info->symbol_at_address_func (vaddr, info))
  612. addr = vaddr;
  613. }
  614. if (format & M6811_OP_IMM16)
  615. {
  616. format &= ~M6811_OP_IMM16;
  617. (*info->fprintf_func) (info->stream, "#");
  618. }
  619. else
  620. {
  621. format &= ~M6811_OP_IND16;
  622. }
  623. /* Avoid duplicate 0x from core binutils. */
  624. if (info->symtab_size > 0)
  625. (*info->fprintf_func) (info->stream, "0x");
  626. (*info->print_address_func) (addr, info);
  627. if (format & M6812_OP_PAGE)
  628. {
  629. (* info->fprintf_func) (info->stream, " {");
  630. /* Avoid duplicate 0x from core binutils. */
  631. if (info->symtab_size > 0)
  632. (*info->fprintf_func) (info->stream, "0x");
  633. (* info->print_address_func) (val, info);
  634. (* info->fprintf_func) (info->stream, ", 0x%x}", page);
  635. format &= ~M6812_OP_PAGE;
  636. pos += 1;
  637. }
  638. }
  639. if (format & M6812_OP_IDX_P2)
  640. {
  641. (*info->fprintf_func) (info->stream, ", ");
  642. status = print_indexed_operand (memaddr + pos + offset, info,
  643. 0, 1, pc_dst_offset,
  644. memaddr + pos + offset + 1, arch);
  645. if (status < 0)
  646. return status;
  647. pos += status;
  648. }
  649. if (format & M6812_OP_IND16_P2)
  650. {
  651. (*info->fprintf_func) (info->stream, ", ");
  652. status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
  653. if (status != 0)
  654. return status;
  655. pos += 2;
  656. val = (buffer[0] << 8) | buffer[1];
  657. /* Avoid duplicate 0x from core binutils. */
  658. if (info->symtab_size > 0)
  659. (*info->fprintf_func) (info->stream, "0x");
  660. (*info->print_address_func) (val, info);
  661. }
  662. /* M6811_OP_BITMASK and M6811_OP_JUMP_REL must be treated separately
  663. and in that order. The brset/brclr insn have a bitmask and then
  664. a relative branch offset. */
  665. if (format & M6811_OP_BITMASK)
  666. {
  667. status = read_memory (memaddr + pos, &buffer[0], 1, info);
  668. if (status != 0)
  669. return status;
  670. pos++;
  671. (*info->fprintf_func) (info->stream, ", #0x%02x%s",
  672. buffer[0] & 0x0FF,
  673. (format & M6811_OP_JUMP_REL ? ", " : ""));
  674. format &= ~M6811_OP_BITMASK;
  675. }
  676. if (format & M6811_OP_JUMP_REL)
  677. {
  678. status = read_memory (memaddr + pos, &buffer[0], 1, info);
  679. if (status != 0)
  680. return status;
  681. (*info->fprintf_func) (info->stream, "0x");
  682. pos++;
  683. val = (buffer[0] ^ 0x80) - 0x80;
  684. (*info->print_address_func) (memaddr + pos + val, info);
  685. format &= ~M6811_OP_JUMP_REL;
  686. }
  687. else if (format & M6812_OP_JUMP_REL16)
  688. {
  689. status = read_memory (memaddr + pos, &buffer[0], 2, info);
  690. if (status != 0)
  691. return status;
  692. pos += 2;
  693. val = (((buffer[0] << 8) | buffer[1]) ^ 0x8000) - 0x8000;
  694. (*info->fprintf_func) (info->stream, "0x");
  695. (*info->print_address_func) (memaddr + pos + val, info);
  696. format &= ~M6812_OP_JUMP_REL16;
  697. }
  698. if (format & M6812_OP_PAGE)
  699. {
  700. status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
  701. if (status != 0)
  702. return status;
  703. pos += 1;
  704. val = buffer[0];
  705. (*info->fprintf_func) (info->stream, ", 0x%x", (unsigned) val);
  706. }
  707. #ifdef DEBUG
  708. /* Consistency check. 'format' must be 0, so that we have handled
  709. all formats; and the computed size of the insn must match the
  710. opcode table content. */
  711. if (format & ~(M6811_OP_PAGE4 | M6811_OP_PAGE3 | M6811_OP_PAGE2))
  712. (*info->fprintf_func) (info->stream, "; Error, format: %lx", format);
  713. if (pos != opcode->size)
  714. (*info->fprintf_func) (info->stream, "; Error, size: %ld expect %d",
  715. pos, opcode->size);
  716. #endif
  717. return pos;
  718. }
  719. /* Opcode not recognized. */
  720. if (format == M6811_OP_PAGE2 && arch & cpu6812
  721. && ((code >= 0x30 && code <= 0x39) || (code >= 0x40)))
  722. (*info->fprintf_func) (info->stream, "trap\t#0x%02x", code & 0x0ff);
  723. else if (format == M6811_OP_PAGE2)
  724. (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
  725. M6811_OPCODE_PAGE2, code);
  726. else if (format == M6811_OP_PAGE3)
  727. (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
  728. M6811_OPCODE_PAGE3, code);
  729. else if (format == M6811_OP_PAGE4)
  730. (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
  731. M6811_OPCODE_PAGE4, code);
  732. else
  733. (*info->fprintf_func) (info->stream, ".byte\t0x%02x", code);
  734. return pos;
  735. }
  736. /* Disassemble one instruction at address 'memaddr'. Returns the number
  737. of bytes used by that instruction. */
  738. int
  739. print_insn_m68hc11 (bfd_vma memaddr, struct disassemble_info* info)
  740. {
  741. return print_insn (memaddr, info, cpu6811);
  742. }
  743. int
  744. print_insn_m68hc12 (bfd_vma memaddr, struct disassemble_info* info)
  745. {
  746. return print_insn (memaddr, info, cpu6812);
  747. }
  748. int
  749. print_insn_m9s12x (bfd_vma memaddr, struct disassemble_info* info)
  750. {
  751. return print_insn (memaddr, info, cpu6812|cpu9s12x);
  752. }
  753. int
  754. print_insn_m9s12xg (bfd_vma memaddr, struct disassemble_info* info)
  755. {
  756. return print_insn (memaddr, info, cpuxgate);
  757. }