z8k-dis.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /* Disassemble z8000 code.
  2. Copyright (C) 1992-2015 Free Software Foundation, Inc.
  3. This file is part of the GNU opcodes library.
  4. This library is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. It is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this file; see the file COPYING. If not, write to the
  14. Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "dis-asm.h"
  18. #define DEFINE_TABLE
  19. #include "z8k-opc.h"
  20. #include <setjmp.h>
  21. typedef struct
  22. {
  23. /* These are all indexed by nibble number (i.e only every other entry
  24. of bytes is used, and every 4th entry of words). */
  25. unsigned char nibbles[24];
  26. unsigned char bytes[24];
  27. unsigned short words[24];
  28. /* Nibble number of first word not yet fetched. */
  29. int max_fetched;
  30. bfd_vma insn_start;
  31. OPCODES_SIGJMP_BUF bailout;
  32. int tabl_index;
  33. char instr_asmsrc[80];
  34. unsigned long arg_reg[0x0f];
  35. unsigned long immediate;
  36. unsigned long displacement;
  37. unsigned long address;
  38. unsigned long cond_code;
  39. unsigned long ctrl_code;
  40. unsigned long flags;
  41. unsigned long interrupts;
  42. }
  43. instr_data_s;
  44. /* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
  45. to ADDR (exclusive) are valid. Returns 1 for success, longjmps
  46. on error. */
  47. #define FETCH_DATA(info, nibble) \
  48. ((nibble) < ((instr_data_s *) (info->private_data))->max_fetched \
  49. ? 1 : fetch_data ((info), (nibble)))
  50. static int
  51. fetch_data (struct disassemble_info *info, int nibble)
  52. {
  53. unsigned char mybuf[20];
  54. int status;
  55. instr_data_s *priv = (instr_data_s *) info->private_data;
  56. if ((nibble % 4) != 0)
  57. abort ();
  58. status = (*info->read_memory_func) (priv->insn_start,
  59. (bfd_byte *) mybuf,
  60. nibble / 2,
  61. info);
  62. if (status != 0)
  63. {
  64. (*info->memory_error_func) (status, priv->insn_start, info);
  65. OPCODES_SIGLONGJMP (priv->bailout, 1);
  66. }
  67. {
  68. int i;
  69. unsigned char *p = mybuf;
  70. for (i = 0; i < nibble;)
  71. {
  72. priv->words[i] = (p[0] << 8) | p[1];
  73. priv->bytes[i] = *p;
  74. priv->nibbles[i++] = *p >> 4;
  75. priv->nibbles[i++] = *p & 0xf;
  76. ++p;
  77. priv->bytes[i] = *p;
  78. priv->nibbles[i++] = *p >> 4;
  79. priv->nibbles[i++] = *p & 0xf;
  80. ++p;
  81. }
  82. }
  83. priv->max_fetched = nibble;
  84. return 1;
  85. }
  86. static char *codes[16] =
  87. {
  88. "f",
  89. "lt",
  90. "le",
  91. "ule",
  92. "ov/pe",
  93. "mi",
  94. "eq",
  95. "c/ult",
  96. "t",
  97. "ge",
  98. "gt",
  99. "ugt",
  100. "nov/po",
  101. "pl",
  102. "ne",
  103. "nc/uge"
  104. };
  105. static char *ctrl_names[8] =
  106. {
  107. "<invld>",
  108. "flags",
  109. "fcw",
  110. "refresh",
  111. "psapseg",
  112. "psapoff",
  113. "nspseg",
  114. "nspoff"
  115. };
  116. static int seg_length;
  117. int z8k_lookup_instr (unsigned char *, disassemble_info *);
  118. static void output_instr (instr_data_s *, unsigned long, disassemble_info *);
  119. static void unpack_instr (instr_data_s *, int, disassemble_info *);
  120. static void unparse_instr (instr_data_s *, int);
  121. static int
  122. print_insn_z8k (bfd_vma addr, disassemble_info *info, int is_segmented)
  123. {
  124. instr_data_s instr_data;
  125. info->private_data = (PTR) &instr_data;
  126. instr_data.max_fetched = 0;
  127. instr_data.insn_start = addr;
  128. if (OPCODES_SIGSETJMP (instr_data.bailout) != 0)
  129. /* Error return. */
  130. return -1;
  131. info->bytes_per_chunk = 2;
  132. info->bytes_per_line = 6;
  133. info->display_endian = BFD_ENDIAN_BIG;
  134. instr_data.tabl_index = z8k_lookup_instr (instr_data.nibbles, info);
  135. if (instr_data.tabl_index >= 0)
  136. {
  137. unpack_instr (&instr_data, is_segmented, info);
  138. unparse_instr (&instr_data, is_segmented);
  139. output_instr (&instr_data, addr, info);
  140. return z8k_table[instr_data.tabl_index].length + seg_length;
  141. }
  142. else
  143. {
  144. FETCH_DATA (info, 4);
  145. (*info->fprintf_func) (info->stream, ".word %02x%02x",
  146. instr_data.bytes[0], instr_data.bytes[2]);
  147. return 2;
  148. }
  149. }
  150. int
  151. print_insn_z8001 (bfd_vma addr, disassemble_info *info)
  152. {
  153. return print_insn_z8k (addr, info, 1);
  154. }
  155. int
  156. print_insn_z8002 (bfd_vma addr, disassemble_info *info)
  157. {
  158. return print_insn_z8k (addr, info, 0);
  159. }
  160. int
  161. z8k_lookup_instr (unsigned char *nibbles, disassemble_info *info)
  162. {
  163. int nibl_index, tabl_index;
  164. int nibl_matched;
  165. int need_fetch = 0;
  166. unsigned short instr_nibl;
  167. unsigned short tabl_datum, datum_class, datum_value;
  168. nibl_matched = 0;
  169. tabl_index = 0;
  170. FETCH_DATA (info, 4);
  171. while (!nibl_matched && z8k_table[tabl_index].name)
  172. {
  173. nibl_matched = 1;
  174. for (nibl_index = 0;
  175. nibl_index < z8k_table[tabl_index].length * 2 && nibl_matched;
  176. nibl_index++)
  177. {
  178. if ((nibl_index % 4) == 0)
  179. {
  180. /* Fetch data only if it isn't already there. */
  181. if (nibl_index >= 4 || (nibl_index < 4 && need_fetch))
  182. FETCH_DATA (info, nibl_index + 4); /* Fetch one word at a time. */
  183. if (nibl_index < 4)
  184. need_fetch = 0;
  185. else
  186. need_fetch = 1;
  187. }
  188. instr_nibl = nibbles[nibl_index];
  189. tabl_datum = z8k_table[tabl_index].byte_info[nibl_index];
  190. datum_class = tabl_datum & CLASS_MASK;
  191. datum_value = ~CLASS_MASK & tabl_datum;
  192. switch (datum_class)
  193. {
  194. case CLASS_BIT:
  195. if (datum_value != instr_nibl)
  196. nibl_matched = 0;
  197. break;
  198. case CLASS_IGNORE:
  199. break;
  200. case CLASS_00II:
  201. if (!((~instr_nibl) & 0x4))
  202. nibl_matched = 0;
  203. break;
  204. case CLASS_01II:
  205. if (!(instr_nibl & 0x4))
  206. nibl_matched = 0;
  207. break;
  208. case CLASS_0CCC:
  209. if (!((~instr_nibl) & 0x8))
  210. nibl_matched = 0;
  211. break;
  212. case CLASS_1CCC:
  213. if (!(instr_nibl & 0x8))
  214. nibl_matched = 0;
  215. break;
  216. case CLASS_0DISP7:
  217. if (!((~instr_nibl) & 0x8))
  218. nibl_matched = 0;
  219. nibl_index += 1;
  220. break;
  221. case CLASS_1DISP7:
  222. if (!(instr_nibl & 0x8))
  223. nibl_matched = 0;
  224. nibl_index += 1;
  225. break;
  226. case CLASS_REGN0:
  227. if (instr_nibl == 0)
  228. nibl_matched = 0;
  229. break;
  230. case CLASS_BIT_1OR2:
  231. if ((instr_nibl | 0x2) != (datum_value | 0x2))
  232. nibl_matched = 0;
  233. break;
  234. default:
  235. break;
  236. }
  237. }
  238. if (nibl_matched)
  239. return tabl_index;
  240. tabl_index++;
  241. }
  242. return -1;
  243. }
  244. static void
  245. output_instr (instr_data_s *instr_data,
  246. unsigned long addr ATTRIBUTE_UNUSED,
  247. disassemble_info *info)
  248. {
  249. int num_bytes;
  250. char out_str[100];
  251. out_str[0] = 0;
  252. num_bytes = (z8k_table[instr_data->tabl_index].length + seg_length) * 2;
  253. FETCH_DATA (info, num_bytes);
  254. strcat (out_str, instr_data->instr_asmsrc);
  255. (*info->fprintf_func) (info->stream, "%s", out_str);
  256. }
  257. static void
  258. unpack_instr (instr_data_s *instr_data, int is_segmented, disassemble_info *info)
  259. {
  260. int nibl_count, loop;
  261. unsigned short instr_nibl, instr_byte, instr_word;
  262. long instr_long;
  263. unsigned int tabl_datum, datum_class;
  264. unsigned short datum_value;
  265. nibl_count = 0;
  266. loop = 0;
  267. seg_length = 0;
  268. while (z8k_table[instr_data->tabl_index].byte_info[loop] != 0)
  269. {
  270. FETCH_DATA (info, nibl_count + 4 - (nibl_count % 4));
  271. instr_nibl = instr_data->nibbles[nibl_count];
  272. instr_byte = instr_data->bytes[nibl_count & ~1];
  273. instr_word = instr_data->words[nibl_count & ~3];
  274. tabl_datum = z8k_table[instr_data->tabl_index].byte_info[loop];
  275. datum_class = tabl_datum & CLASS_MASK;
  276. datum_value = tabl_datum & ~CLASS_MASK;
  277. switch (datum_class)
  278. {
  279. case CLASS_DISP:
  280. switch (datum_value)
  281. {
  282. case ARG_DISP16:
  283. instr_data->displacement = instr_data->insn_start + 4
  284. + (signed short) (instr_word & 0xffff);
  285. nibl_count += 3;
  286. break;
  287. case ARG_DISP12:
  288. if (instr_word & 0x800)
  289. /* Negative 12 bit displacement. */
  290. instr_data->displacement = instr_data->insn_start + 2
  291. - (signed short) ((instr_word & 0xfff) | 0xf000) * 2;
  292. else
  293. instr_data->displacement = instr_data->insn_start + 2
  294. - (instr_word & 0x0fff) * 2;
  295. nibl_count += 2;
  296. break;
  297. default:
  298. break;
  299. }
  300. break;
  301. case CLASS_IMM:
  302. switch (datum_value)
  303. {
  304. case ARG_IMM4:
  305. instr_data->immediate = instr_nibl;
  306. break;
  307. case ARG_NIM4:
  308. instr_data->immediate = (- instr_nibl) & 0xf;
  309. break;
  310. case ARG_NIM8:
  311. instr_data->immediate = (- instr_byte) & 0xff;
  312. nibl_count += 1;
  313. break;
  314. case ARG_IMM8:
  315. instr_data->immediate = instr_byte;
  316. nibl_count += 1;
  317. break;
  318. case ARG_IMM16:
  319. instr_data->immediate = instr_word;
  320. nibl_count += 3;
  321. break;
  322. case ARG_IMM32:
  323. FETCH_DATA (info, nibl_count + 8);
  324. instr_long = (instr_data->words[nibl_count] << 16)
  325. | (instr_data->words[nibl_count + 4]);
  326. instr_data->immediate = instr_long;
  327. nibl_count += 7;
  328. break;
  329. case ARG_IMMN:
  330. instr_data->immediate = instr_nibl - 1;
  331. break;
  332. case ARG_IMM4M1:
  333. instr_data->immediate = instr_nibl + 1;
  334. break;
  335. case ARG_IMM_1:
  336. instr_data->immediate = 1;
  337. break;
  338. case ARG_IMM_2:
  339. instr_data->immediate = 2;
  340. break;
  341. case ARG_IMM2:
  342. instr_data->immediate = instr_nibl & 0x3;
  343. break;
  344. default:
  345. break;
  346. }
  347. break;
  348. case CLASS_CC:
  349. instr_data->cond_code = instr_nibl;
  350. break;
  351. case CLASS_ADDRESS:
  352. if (is_segmented)
  353. {
  354. if (instr_nibl & 0x8)
  355. {
  356. FETCH_DATA (info, nibl_count + 8);
  357. instr_long = (instr_data->words[nibl_count] << 16)
  358. | (instr_data->words[nibl_count + 4]);
  359. instr_data->address = ((instr_word & 0x7f00) << 16)
  360. + (instr_long & 0xffff);
  361. nibl_count += 7;
  362. seg_length = 2;
  363. }
  364. else
  365. {
  366. instr_data->address = ((instr_word & 0x7f00) << 16)
  367. + (instr_word & 0x00ff);
  368. nibl_count += 3;
  369. }
  370. }
  371. else
  372. {
  373. instr_data->address = instr_word;
  374. nibl_count += 3;
  375. }
  376. break;
  377. case CLASS_0CCC:
  378. case CLASS_1CCC:
  379. instr_data->ctrl_code = instr_nibl & 0x7;
  380. break;
  381. case CLASS_0DISP7:
  382. instr_data->displacement =
  383. instr_data->insn_start + 2 - (instr_byte & 0x7f) * 2;
  384. nibl_count += 1;
  385. break;
  386. case CLASS_1DISP7:
  387. instr_data->displacement =
  388. instr_data->insn_start + 2 - (instr_byte & 0x7f) * 2;
  389. nibl_count += 1;
  390. break;
  391. case CLASS_01II:
  392. instr_data->interrupts = instr_nibl & 0x3;
  393. break;
  394. case CLASS_00II:
  395. instr_data->interrupts = instr_nibl & 0x3;
  396. break;
  397. case CLASS_IGNORE:
  398. case CLASS_BIT:
  399. instr_data->ctrl_code = instr_nibl & 0x7;
  400. break;
  401. case CLASS_FLAGS:
  402. instr_data->flags = instr_nibl;
  403. break;
  404. case CLASS_REG:
  405. instr_data->arg_reg[datum_value] = instr_nibl;
  406. break;
  407. case CLASS_REGN0:
  408. instr_data->arg_reg[datum_value] = instr_nibl;
  409. break;
  410. case CLASS_DISP8:
  411. instr_data->displacement =
  412. instr_data->insn_start + 2 + (signed char) instr_byte * 2;
  413. nibl_count += 1;
  414. break;
  415. case CLASS_BIT_1OR2:
  416. instr_data->immediate = ((instr_nibl >> 1) & 0x1) + 1;
  417. nibl_count += 1;
  418. break;
  419. default:
  420. abort ();
  421. break;
  422. }
  423. loop += 1;
  424. nibl_count += 1;
  425. }
  426. }
  427. static void
  428. print_intr(char *tmp_str, unsigned long interrupts)
  429. {
  430. int comma = 0;
  431. *tmp_str = 0;
  432. if (! (interrupts & 2))
  433. {
  434. strcat (tmp_str, "vi");
  435. comma = 1;
  436. }
  437. if (! (interrupts & 1))
  438. {
  439. if (comma) strcat (tmp_str, ",");
  440. strcat (tmp_str, "nvi");
  441. }
  442. }
  443. static void
  444. print_flags(char *tmp_str, unsigned long flags)
  445. {
  446. int comma = 0;
  447. *tmp_str = 0;
  448. if (flags & 8)
  449. {
  450. strcat (tmp_str, "c");
  451. comma = 1;
  452. }
  453. if (flags & 4)
  454. {
  455. if (comma) strcat (tmp_str, ",");
  456. strcat (tmp_str, "z");
  457. comma = 1;
  458. }
  459. if (flags & 2)
  460. {
  461. if (comma) strcat (tmp_str, ",");
  462. strcat (tmp_str, "s");
  463. comma = 1;
  464. }
  465. if (flags & 1)
  466. {
  467. if (comma) strcat (tmp_str, ",");
  468. strcat (tmp_str, "p");
  469. }
  470. }
  471. static void
  472. unparse_instr (instr_data_s *instr_data, int is_segmented)
  473. {
  474. unsigned short datum_value;
  475. unsigned int tabl_datum, datum_class;
  476. int loop, loop_limit;
  477. char out_str[80], tmp_str[25];
  478. sprintf (out_str, "%s\t", z8k_table[instr_data->tabl_index].name);
  479. loop_limit = z8k_table[instr_data->tabl_index].noperands;
  480. for (loop = 0; loop < loop_limit; loop++)
  481. {
  482. if (loop)
  483. strcat (out_str, ",");
  484. tabl_datum = z8k_table[instr_data->tabl_index].arg_info[loop];
  485. datum_class = tabl_datum & CLASS_MASK;
  486. datum_value = tabl_datum & ~CLASS_MASK;
  487. switch (datum_class)
  488. {
  489. case CLASS_X:
  490. sprintf (tmp_str, "0x%0lx(r%ld)", instr_data->address,
  491. instr_data->arg_reg[datum_value]);
  492. strcat (out_str, tmp_str);
  493. break;
  494. case CLASS_BA:
  495. if (is_segmented)
  496. sprintf (tmp_str, "rr%ld(#0x%lx)", instr_data->arg_reg[datum_value],
  497. instr_data->immediate);
  498. else
  499. sprintf (tmp_str, "r%ld(#0x%lx)", instr_data->arg_reg[datum_value],
  500. instr_data->immediate);
  501. strcat (out_str, tmp_str);
  502. break;
  503. case CLASS_BX:
  504. if (is_segmented)
  505. sprintf (tmp_str, "rr%ld(r%ld)", instr_data->arg_reg[datum_value],
  506. instr_data->arg_reg[ARG_RX]);
  507. else
  508. sprintf (tmp_str, "r%ld(r%ld)", instr_data->arg_reg[datum_value],
  509. instr_data->arg_reg[ARG_RX]);
  510. strcat (out_str, tmp_str);
  511. break;
  512. case CLASS_DISP:
  513. sprintf (tmp_str, "0x%0lx", instr_data->displacement);
  514. strcat (out_str, tmp_str);
  515. break;
  516. case CLASS_IMM:
  517. if (datum_value == ARG_IMM2) /* True with EI/DI instructions only. */
  518. {
  519. print_intr (tmp_str, instr_data->interrupts);
  520. strcat (out_str, tmp_str);
  521. break;
  522. }
  523. sprintf (tmp_str, "#0x%0lx", instr_data->immediate);
  524. strcat (out_str, tmp_str);
  525. break;
  526. case CLASS_CC:
  527. sprintf (tmp_str, "%s", codes[instr_data->cond_code]);
  528. strcat (out_str, tmp_str);
  529. break;
  530. case CLASS_CTRL:
  531. sprintf (tmp_str, "%s", ctrl_names[instr_data->ctrl_code]);
  532. strcat (out_str, tmp_str);
  533. break;
  534. case CLASS_DA:
  535. case CLASS_ADDRESS:
  536. sprintf (tmp_str, "0x%0lx", instr_data->address);
  537. strcat (out_str, tmp_str);
  538. break;
  539. case CLASS_IR:
  540. if (is_segmented)
  541. sprintf (tmp_str, "@rr%ld", instr_data->arg_reg[datum_value]);
  542. else
  543. sprintf (tmp_str, "@r%ld", instr_data->arg_reg[datum_value]);
  544. strcat (out_str, tmp_str);
  545. break;
  546. case CLASS_IRO:
  547. sprintf (tmp_str, "@r%ld", instr_data->arg_reg[datum_value]);
  548. strcat (out_str, tmp_str);
  549. break;
  550. case CLASS_FLAGS:
  551. print_flags(tmp_str, instr_data->flags);
  552. strcat (out_str, tmp_str);
  553. break;
  554. case CLASS_REG_BYTE:
  555. if (instr_data->arg_reg[datum_value] >= 0x8)
  556. sprintf (tmp_str, "rl%ld",
  557. instr_data->arg_reg[datum_value] - 0x8);
  558. else
  559. sprintf (tmp_str, "rh%ld", instr_data->arg_reg[datum_value]);
  560. strcat (out_str, tmp_str);
  561. break;
  562. case CLASS_REG_WORD:
  563. sprintf (tmp_str, "r%ld", instr_data->arg_reg[datum_value]);
  564. strcat (out_str, tmp_str);
  565. break;
  566. case CLASS_REG_QUAD:
  567. sprintf (tmp_str, "rq%ld", instr_data->arg_reg[datum_value]);
  568. strcat (out_str, tmp_str);
  569. break;
  570. case CLASS_REG_LONG:
  571. sprintf (tmp_str, "rr%ld", instr_data->arg_reg[datum_value]);
  572. strcat (out_str, tmp_str);
  573. break;
  574. case CLASS_PR:
  575. if (is_segmented)
  576. sprintf (tmp_str, "rr%ld", instr_data->arg_reg[datum_value]);
  577. else
  578. sprintf (tmp_str, "r%ld", instr_data->arg_reg[datum_value]);
  579. strcat (out_str, tmp_str);
  580. break;
  581. default:
  582. abort ();
  583. break;
  584. }
  585. }
  586. strcpy (instr_data->instr_asmsrc, out_str);
  587. }