be_arm.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  1. /*
  2. * Debugger ARM specific functions
  3. *
  4. * Copyright 2010-2013 André Hentschel
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "debugger.h"
  21. #if defined(__arm__) && !defined(__ARMEB__)
  22. /*
  23. * Switch to disassemble Thumb code.
  24. */
  25. static BOOL db_disasm_thumb = FALSE;
  26. /*
  27. * Flag to indicate whether we need to display instruction,
  28. * or whether we just need to know the address of the next
  29. * instruction.
  30. */
  31. static BOOL db_display = FALSE;
  32. #define ARM_INSN_SIZE 4
  33. #define THUMB_INSN_SIZE 2
  34. #define THUMB2_INSN_SIZE 4
  35. #define ROR32(n, r) (((n) >> (r)) | ((n) << (32 - (r))))
  36. #define get_cond(ins) tbl_cond[(ins >> 28) & 0x0f]
  37. #define get_nibble(ins, num) ((ins >> (num * 4)) & 0x0f)
  38. static char const tbl_regs[][4] = {
  39. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
  40. "fp", "ip", "sp", "lr", "pc", "cpsr"
  41. };
  42. static char const tbl_addrmode[][3] = {
  43. "da", "ia", "db", "ib"
  44. };
  45. static char const tbl_cond[][3] = {
  46. "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc", "hi", "ls", "ge", "lt", "gt", "le", "", ""
  47. };
  48. static char const tbl_dataops[][4] = {
  49. "and", "eor", "sub", "rsb", "add", "adc", "sbc", "rsc", "tst", "teq", "cmp", "cmn", "orr",
  50. "mov", "bic", "mvn"
  51. };
  52. static char const tbl_shifts[][4] = {
  53. "lsl", "lsr", "asr", "ror"
  54. };
  55. static char const tbl_hiops_t[][4] = {
  56. "add", "cmp", "mov", "bx"
  57. };
  58. static char const tbl_aluops_t[][4] = {
  59. "and", "eor", "lsl", "lsr", "asr", "adc", "sbc", "ror", "tst", "neg", "cmp", "cmn", "orr",
  60. "mul", "bic", "mvn"
  61. };
  62. static char const tbl_immops_t[][4] = {
  63. "mov", "cmp", "add", "sub"
  64. };
  65. static char const tbl_sregops_t[][5] = {
  66. "strh", "ldsb", "ldrh", "ldsh"
  67. };
  68. static char const tbl_miscops_t2[][6] = {
  69. "rev", "rev16", "rbit", "revsh"
  70. };
  71. static char const tbl_width_t2[][2] = {
  72. "b", "h", "", "?"
  73. };
  74. static char const tbl_special_regs_t2[][12] = {
  75. "apsr", "iapsr", "eapsr", "xpsr", "rsvd", "ipsr", "epsr", "iepsr", "msp", "psp", "rsvd", "rsvd",
  76. "rsvd", "rsvd", "rsvd", "rsvd", "primask", "basepri", "basepri_max", "faultmask", "control"
  77. };
  78. static char const tbl_hints_t2[][6] = {
  79. "nop", "yield", "wfe", "wfi", "sev"
  80. };
  81. static UINT db_get_inst(void* addr, int size)
  82. {
  83. UINT result = 0;
  84. char buffer[4];
  85. if (dbg_read_memory(addr, buffer, size))
  86. {
  87. switch (size)
  88. {
  89. case 4:
  90. result = *(UINT*)buffer;
  91. break;
  92. case 2:
  93. result = *(WORD*)buffer;
  94. break;
  95. }
  96. }
  97. return result;
  98. }
  99. static void db_printsym(unsigned int addr)
  100. {
  101. ADDRESS64 a;
  102. a.Mode = AddrModeFlat;
  103. a.Offset = addr;
  104. print_address(&a, TRUE);
  105. }
  106. static UINT arm_disasm_branch(UINT inst, ADDRESS64 *addr)
  107. {
  108. short link = (inst >> 24) & 0x01;
  109. int offset = (inst << 2) & 0x03ffffff;
  110. if (offset & 0x02000000) offset |= 0xfc000000;
  111. offset += 8;
  112. dbg_printf("\n\tb%s%s\t", link ? "l" : "", get_cond(inst));
  113. db_printsym(addr->Offset + offset);
  114. return 0;
  115. }
  116. static UINT arm_disasm_mul(UINT inst, ADDRESS64 *addr)
  117. {
  118. short accu = (inst >> 21) & 0x01;
  119. short condcodes = (inst >> 20) & 0x01;
  120. if (accu)
  121. dbg_printf("\n\tmla%s%s\t%s, %s, %s, %s", get_cond(inst), condcodes ? "s" : "",
  122. tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)],
  123. tbl_regs[get_nibble(inst, 2)], tbl_regs[get_nibble(inst, 3)]);
  124. else
  125. dbg_printf("\n\tmul%s%s\t%s, %s, %s", get_cond(inst), condcodes ? "s" : "",
  126. tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)],
  127. tbl_regs[get_nibble(inst, 2)]);
  128. return 0;
  129. }
  130. static UINT arm_disasm_longmul(UINT inst, ADDRESS64 *addr)
  131. {
  132. short sign = (inst >> 22) & 0x01;
  133. short accu = (inst >> 21) & 0x01;
  134. short condcodes = (inst >> 20) & 0x01;
  135. dbg_printf("\n\t%s%s%s%s\t%s, %s, %s, %s", sign ? "s" : "u", accu ? "mlal" : "mull",
  136. get_cond(inst), condcodes ? "s" : "",
  137. tbl_regs[get_nibble(inst, 3)], tbl_regs[get_nibble(inst, 4)],
  138. tbl_regs[get_nibble(inst, 0)], tbl_regs[get_nibble(inst, 2)]);
  139. return 0;
  140. }
  141. static UINT arm_disasm_swp(UINT inst, ADDRESS64 *addr)
  142. {
  143. short byte = (inst >> 22) & 0x01;
  144. dbg_printf("\n\tswp%s%s\t%s, %s, [%s]", get_cond(inst), byte ? "b" : "",
  145. tbl_regs[get_nibble(inst, 3)], tbl_regs[get_nibble(inst, 0)],
  146. tbl_regs[get_nibble(inst, 4)]);
  147. return 0;
  148. }
  149. static UINT arm_disasm_halfwordtrans(UINT inst, ADDRESS64 *addr)
  150. {
  151. short halfword = (inst >> 5) & 0x01;
  152. short sign = (inst >> 6) & 0x01;
  153. short load = (inst >> 20) & 0x01;
  154. short writeback = (inst >> 21) & 0x01;
  155. short immediate = (inst >> 22) & 0x01;
  156. short direction = (inst >> 23) & 0x01;
  157. short indexing = (inst >> 24) & 0x01;
  158. short offset = ((inst >> 4) & 0xf0) + (inst & 0x0f);
  159. dbg_printf("\n\t%s%s%s%s%s", load ? "ldr" : "str", sign ? "s" : "",
  160. halfword ? "h" : (sign ? "b" : ""), writeback ? "t" : "", get_cond(inst));
  161. dbg_printf("\t%s, ", tbl_regs[get_nibble(inst, 3)]);
  162. if (indexing)
  163. {
  164. if (immediate)
  165. dbg_printf("[%s, #%s%d]", tbl_regs[get_nibble(inst, 4)], direction ? "" : "-", offset);
  166. else
  167. dbg_printf("[%s, %s]", tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)]);
  168. }
  169. else
  170. {
  171. if (immediate)
  172. dbg_printf("[%s], #%s%d", tbl_regs[get_nibble(inst, 4)], direction ? "" : "-", offset);
  173. else
  174. dbg_printf("[%s], %s", tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)]);
  175. }
  176. return 0;
  177. }
  178. static UINT arm_disasm_branchxchg(UINT inst, ADDRESS64 *addr)
  179. {
  180. dbg_printf("\n\tbx%s\t%s", get_cond(inst), tbl_regs[get_nibble(inst, 0)]);
  181. return 0;
  182. }
  183. static UINT arm_disasm_mrstrans(UINT inst, ADDRESS64 *addr)
  184. {
  185. short src = (inst >> 22) & 0x01;
  186. dbg_printf("\n\tmrs%s\t%s, %s", get_cond(inst), tbl_regs[get_nibble(inst, 3)],
  187. src ? "spsr" : "cpsr");
  188. return 0;
  189. }
  190. static UINT arm_disasm_msrtrans(UINT inst, ADDRESS64 *addr)
  191. {
  192. short immediate = (inst >> 25) & 0x01;
  193. short dst = (inst >> 22) & 0x01;
  194. short simple = (inst >> 16) & 0x01;
  195. if (simple || !immediate)
  196. {
  197. dbg_printf("\n\tmsr%s\t%s, %s", get_cond(inst), dst ? "spsr" : "cpsr",
  198. tbl_regs[get_nibble(inst, 0)]);
  199. return 0;
  200. }
  201. dbg_printf("\n\tmsr%s\t%s, #%u", get_cond(inst), dst ? "spsr" : "cpsr",
  202. ROR32(inst & 0xff, 2 * get_nibble(inst, 2)));
  203. return 0;
  204. }
  205. static UINT arm_disasm_wordmov(UINT inst, ADDRESS64 *addr)
  206. {
  207. short top = (inst >> 22) & 0x01;
  208. dbg_printf("\n\tmov%s%s\t%s, #%u", top ? "t" : "w", get_cond(inst),
  209. tbl_regs[get_nibble(inst, 3)], (get_nibble(inst, 4) << 12) | (inst & 0x0fff));
  210. return 0;
  211. }
  212. static UINT arm_disasm_nop(UINT inst, ADDRESS64 *addr)
  213. {
  214. dbg_printf("\n\tnop%s", get_cond(inst));
  215. return 0;
  216. }
  217. static UINT arm_disasm_dataprocessing(UINT inst, ADDRESS64 *addr)
  218. {
  219. short condcodes = (inst >> 20) & 0x01;
  220. short opcode = (inst >> 21) & 0x0f;
  221. short immediate = (inst >> 25) & 0x01;
  222. short no_op1 = (opcode & 0x0d) == 0x0d;
  223. short no_dst = (opcode & 0x0c) == 0x08;
  224. dbg_printf("\n\t%s%s%s", tbl_dataops[opcode], condcodes ? "s" : "", get_cond(inst));
  225. if (!no_dst) dbg_printf("\t%s, ", tbl_regs[get_nibble(inst, 3)]);
  226. else dbg_printf("\t");
  227. if (no_op1)
  228. {
  229. if (immediate)
  230. dbg_printf("#%u", ROR32(inst & 0xff, 2 * get_nibble(inst, 2)));
  231. else
  232. dbg_printf("%s", tbl_regs[get_nibble(inst, 0)]);
  233. }
  234. else
  235. {
  236. if (immediate)
  237. dbg_printf("%s, #%u", tbl_regs[get_nibble(inst, 4)],
  238. ROR32(inst & 0xff, 2 * get_nibble(inst, 2)));
  239. else if (((inst >> 4) & 0xff) == 0x00) /* no shift */
  240. dbg_printf("%s, %s", tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)]);
  241. else if (((inst >> 4) & 0x09) == 0x01) /* register shift */
  242. dbg_printf("%s, %s, %s %s", tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)],
  243. tbl_shifts[(inst >> 5) & 0x03], tbl_regs[(inst >> 8) & 0x0f]);
  244. else if (((inst >> 4) & 0x01) == 0x00) /* immediate shift */
  245. dbg_printf("%s, %s, %s #%d", tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)],
  246. tbl_shifts[(inst >> 5) & 0x03], (inst >> 7) & 0x1f);
  247. else
  248. return inst;
  249. }
  250. return 0;
  251. }
  252. static UINT arm_disasm_singletrans(UINT inst, ADDRESS64 *addr)
  253. {
  254. short load = (inst >> 20) & 0x01;
  255. short writeback = (inst >> 21) & 0x01;
  256. short byte = (inst >> 22) & 0x01;
  257. short direction = (inst >> 23) & 0x01;
  258. short indexing = (inst >> 24) & 0x01;
  259. short immediate = !((inst >> 25) & 0x01);
  260. short offset = inst & 0x0fff;
  261. dbg_printf("\n\t%s%s%s%s", load ? "ldr" : "str", byte ? "b" : "", writeback ? "t" : "",
  262. get_cond(inst));
  263. dbg_printf("\t%s, ", tbl_regs[get_nibble(inst, 3)]);
  264. if (indexing)
  265. {
  266. if (immediate)
  267. dbg_printf("[%s, #%s%d]", tbl_regs[get_nibble(inst, 4)], direction ? "" : "-", offset);
  268. else if (((inst >> 4) & 0xff) == 0x00) /* no shift */
  269. dbg_printf("[%s, %s]", tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)]);
  270. else if (((inst >> 4) & 0x01) == 0x00) /* immediate shift (there's no register shift) */
  271. dbg_printf("[%s, %s, %s #%d]", tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)],
  272. tbl_shifts[(inst >> 5) & 0x03], (inst >> 7) & 0x1f);
  273. else
  274. return inst;
  275. }
  276. else
  277. {
  278. if (immediate)
  279. dbg_printf("[%s], #%s%d", tbl_regs[get_nibble(inst, 4)], direction ? "" : "-", offset);
  280. else if (((inst >> 4) & 0xff) == 0x00) /* no shift */
  281. dbg_printf("[%s], %s", tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)]);
  282. else if (((inst >> 4) & 0x01) == 0x00) /* immediate shift (there's no register shift) */
  283. dbg_printf("[%s], %s, %s #%d", tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)],
  284. tbl_shifts[(inst >> 5) & 0x03], (inst >> 7) & 0x1f);
  285. else
  286. return inst;
  287. }
  288. return 0;
  289. }
  290. static UINT arm_disasm_blocktrans(UINT inst, ADDRESS64 *addr)
  291. {
  292. short load = (inst >> 20) & 0x01;
  293. short writeback = (inst >> 21) & 0x01;
  294. short psr = (inst >> 22) & 0x01;
  295. short addrmode = (inst >> 23) & 0x03;
  296. short i;
  297. short last=15;
  298. for (i=15;i>=0;i--)
  299. if ((inst>>i) & 1)
  300. {
  301. last = i;
  302. break;
  303. }
  304. dbg_printf("\n\t%s%s%s\t%s%s, {", load ? "ldm" : "stm", tbl_addrmode[addrmode], get_cond(inst),
  305. tbl_regs[get_nibble(inst, 4)], writeback ? "!" : "");
  306. for (i=0;i<=15;i++)
  307. if ((inst>>i) & 1)
  308. {
  309. if (i == last) dbg_printf("%s", tbl_regs[i]);
  310. else dbg_printf("%s, ", tbl_regs[i]);
  311. }
  312. dbg_printf("}%s", psr ? "^" : "");
  313. return 0;
  314. }
  315. static UINT arm_disasm_swi(UINT inst, ADDRESS64 *addr)
  316. {
  317. dbg_printf("\n\tswi%s\t#%d", get_cond(inst), inst & 0x00ffffff);
  318. return 0;
  319. }
  320. static UINT arm_disasm_coproctrans(UINT inst, ADDRESS64 *addr)
  321. {
  322. WORD CRm = inst & 0x0f;
  323. WORD CP = (inst >> 5) & 0x07;
  324. WORD CPnum = (inst >> 8) & 0x0f;
  325. WORD CRn = (inst >> 16) & 0x0f;
  326. WORD load = (inst >> 20) & 0x01;
  327. WORD CP_Opc = (inst >> 21) & 0x07;
  328. dbg_printf("\n\t%s%s\t%u, %u, %s, cr%u, cr%u, {%u}", load ? "mrc" : "mcr", get_cond(inst), CPnum,
  329. CP, tbl_regs[get_nibble(inst, 3)], CRn, CRm, CP_Opc);
  330. return 0;
  331. }
  332. static UINT arm_disasm_coprocdataop(UINT inst, ADDRESS64 *addr)
  333. {
  334. WORD CRm = inst & 0x0f;
  335. WORD CP = (inst >> 5) & 0x07;
  336. WORD CPnum = (inst >> 8) & 0x0f;
  337. WORD CRd = (inst >> 12) & 0x0f;
  338. WORD CRn = (inst >> 16) & 0x0f;
  339. WORD CP_Opc = (inst >> 20) & 0x0f;
  340. dbg_printf("\n\tcdp%s\t%u, %u, cr%u, cr%u, cr%u, {%u}", get_cond(inst),
  341. CPnum, CP, CRd, CRn, CRm, CP_Opc);
  342. return 0;
  343. }
  344. static UINT arm_disasm_coprocdatatrans(UINT inst, ADDRESS64 *addr)
  345. {
  346. WORD CPnum = (inst >> 8) & 0x0f;
  347. WORD CRd = (inst >> 12) & 0x0f;
  348. WORD load = (inst >> 20) & 0x01;
  349. WORD writeback = (inst >> 21) & 0x01;
  350. WORD translen = (inst >> 22) & 0x01;
  351. WORD direction = (inst >> 23) & 0x01;
  352. WORD indexing = (inst >> 24) & 0x01;
  353. short offset = (inst & 0xff) << 2;
  354. dbg_printf("\n\t%s%s%s", load ? "ldc" : "stc", translen ? "l" : "", get_cond(inst));
  355. if (indexing)
  356. dbg_printf("\t%u, cr%u, [%s, #%s%d]%s", CPnum, CRd, tbl_regs[get_nibble(inst, 4)], direction ? "" : "-", offset, writeback?"!":"");
  357. else
  358. dbg_printf("\t%u, cr%u, [%s], #%s%d", CPnum, CRd, tbl_regs[get_nibble(inst, 4)], direction ? "" : "-", offset);
  359. return 0;
  360. }
  361. static WORD thumb_disasm_hireg(WORD inst, ADDRESS64 *addr)
  362. {
  363. short dst = inst & 0x07;
  364. short src = (inst >> 3) & 0x07;
  365. short h2 = (inst >> 6) & 0x01;
  366. short h1 = (inst >> 7) & 0x01;
  367. short op = (inst >> 8) & 0x03;
  368. if (h1) dst += 8;
  369. if (h2) src += 8;
  370. if (op == 2 && dst == src) /* mov rx, rx */
  371. {
  372. dbg_printf("\n\tnop");
  373. return 0;
  374. }
  375. if (op == 3)
  376. dbg_printf("\n\tb%sx\t%s", h1?"l":"", tbl_regs[src]);
  377. else
  378. dbg_printf("\n\t%s\t%s, %s", tbl_hiops_t[op], tbl_regs[dst], tbl_regs[src]);
  379. return 0;
  380. }
  381. static WORD thumb_disasm_aluop(WORD inst, ADDRESS64 *addr)
  382. {
  383. short dst = inst & 0x07;
  384. short src = (inst >> 3) & 0x07;
  385. short op = (inst >> 6) & 0x0f;
  386. dbg_printf("\n\t%s\t%s, %s", tbl_aluops_t[op], tbl_regs[dst], tbl_regs[src]);
  387. return 0;
  388. }
  389. static WORD thumb_disasm_pushpop(WORD inst, ADDRESS64 *addr)
  390. {
  391. short lrpc = (inst >> 8) & 0x01;
  392. short load = (inst >> 11) & 0x01;
  393. short i;
  394. short last;
  395. for (i=7;i>=0;i--)
  396. if ((inst>>i) & 1) break;
  397. last = i;
  398. dbg_printf("\n\t%s\t{", load ? "pop" : "push");
  399. for (i=0;i<=7;i++)
  400. if ((inst>>i) & 1)
  401. {
  402. if (i == last) dbg_printf("%s", tbl_regs[i]);
  403. else dbg_printf("%s, ", tbl_regs[i]);
  404. }
  405. if (lrpc)
  406. dbg_printf("%s%s", last ? ", " : "", load ? "pc" : "lr");
  407. dbg_printf("}");
  408. return 0;
  409. }
  410. static WORD thumb_disasm_blocktrans(WORD inst, ADDRESS64 *addr)
  411. {
  412. short load = (inst >> 11) & 0x01;
  413. short i;
  414. short last;
  415. for (i=7;i>=0;i--)
  416. if ((inst>>i) & 1) break;
  417. last = i;
  418. dbg_printf("\n\t%s\t%s!, {", load ? "ldmia" : "stmia", tbl_regs[(inst >> 8) & 0x07]);
  419. for (i=0;i<=7;i++)
  420. if ((inst>>i) & 1)
  421. {
  422. if (i == last) dbg_printf("%s", tbl_regs[i]);
  423. else dbg_printf("%s, ", tbl_regs[i]);
  424. }
  425. dbg_printf("}");
  426. return 0;
  427. }
  428. static WORD thumb_disasm_swi(WORD inst, ADDRESS64 *addr)
  429. {
  430. dbg_printf("\n\tswi\t#%d", inst & 0x00ff);
  431. return 0;
  432. }
  433. static WORD thumb_disasm_condbranch(WORD inst, ADDRESS64 *addr)
  434. {
  435. WORD offset = inst & 0x00ff;
  436. dbg_printf("\n\tb%s\t", tbl_cond[(inst >> 8) & 0x0f]);
  437. db_printsym(addr->Offset + offset);
  438. return 0;
  439. }
  440. static WORD thumb_disasm_uncondbranch(WORD inst, ADDRESS64 *addr)
  441. {
  442. short offset = (inst & 0x07ff) << 1;
  443. if (offset & 0x0800) offset |= 0xf000;
  444. offset += 4;
  445. dbg_printf("\n\tb\t");
  446. db_printsym(addr->Offset + offset);
  447. return 0;
  448. }
  449. static WORD thumb_disasm_loadadr(WORD inst, ADDRESS64 *addr)
  450. {
  451. WORD src = (inst >> 11) & 0x01;
  452. WORD offset = (inst & 0xff) << 2;
  453. dbg_printf("\n\tadd\t%s, %s, #%d", tbl_regs[(inst >> 8) & 0x07], src ? "sp" : "pc", offset);
  454. return 0;
  455. }
  456. static WORD thumb_disasm_ldrpcrel(WORD inst, ADDRESS64 *addr)
  457. {
  458. WORD offset = (inst & 0xff) << 2;
  459. dbg_printf("\n\tldr\t%s, [pc, #%u]", tbl_regs[(inst >> 8) & 0x07], offset);
  460. return 0;
  461. }
  462. static WORD thumb_disasm_ldrsprel(WORD inst, ADDRESS64 *addr)
  463. {
  464. WORD offset = (inst & 0xff) << 2;
  465. dbg_printf("\n\t%s\t%s, [sp, #%u]", (inst & 0x0800)?"ldr":"str", tbl_regs[(inst >> 8) & 0x07], offset);
  466. return 0;
  467. }
  468. static WORD thumb_disasm_addsprel(WORD inst, ADDRESS64 *addr)
  469. {
  470. WORD offset = (inst & 0x7f) << 2;
  471. if ((inst >> 7) & 0x01)
  472. dbg_printf("\n\tsub\tsp, sp, #%u", offset);
  473. else
  474. dbg_printf("\n\tadd\tsp, sp, #%u", offset);
  475. return 0;
  476. }
  477. static WORD thumb_disasm_ldrimm(WORD inst, ADDRESS64 *addr)
  478. {
  479. WORD offset = (inst & 0x07c0) >> 6;
  480. dbg_printf("\n\t%s%s\t%s, [%s, #%u]", (inst & 0x0800)?"ldr":"str", (inst & 0x1000)?"b":"",
  481. tbl_regs[inst & 0x07], tbl_regs[(inst >> 3) & 0x07], (inst & 0x1000)?offset:(offset << 2));
  482. return 0;
  483. }
  484. static WORD thumb_disasm_ldrhimm(WORD inst, ADDRESS64 *addr)
  485. {
  486. WORD offset = (inst & 0x07c0) >> 5;
  487. dbg_printf("\n\t%s\t%s, [%s, #%u]", (inst & 0x0800)?"ldrh":"strh",
  488. tbl_regs[inst & 0x07], tbl_regs[(inst >> 3) & 0x07], offset);
  489. return 0;
  490. }
  491. static WORD thumb_disasm_ldrreg(WORD inst, ADDRESS64 *addr)
  492. {
  493. dbg_printf("\n\t%s%s\t%s, [%s, %s]", (inst & 0x0800)?"ldr":"str", (inst & 0x0400)?"b":"",
  494. tbl_regs[inst & 0x07], tbl_regs[(inst >> 3) & 0x07], tbl_regs[(inst >> 6) & 0x07]);
  495. return 0;
  496. }
  497. static WORD thumb_disasm_ldrsreg(WORD inst, ADDRESS64 *addr)
  498. {
  499. dbg_printf("\n\t%s\t%s, [%s, %s]", tbl_sregops_t[(inst >> 10) & 0x03],
  500. tbl_regs[inst & 0x07], tbl_regs[(inst >> 3) & 0x07], tbl_regs[(inst >> 6) & 0x07]);
  501. return 0;
  502. }
  503. static WORD thumb_disasm_immop(WORD inst, ADDRESS64 *addr)
  504. {
  505. WORD op = (inst >> 11) & 0x03;
  506. dbg_printf("\n\t%s\t%s, #%u", tbl_immops_t[op], tbl_regs[(inst >> 8) & 0x07], inst & 0xff);
  507. return 0;
  508. }
  509. static WORD thumb_disasm_nop(WORD inst, ADDRESS64 *addr)
  510. {
  511. dbg_printf("\n\tnop");
  512. return 0;
  513. }
  514. static WORD thumb_disasm_addsub(WORD inst, ADDRESS64 *addr)
  515. {
  516. WORD op = (inst >> 9) & 0x01;
  517. WORD immediate = (inst >> 10) & 0x01;
  518. dbg_printf("\n\t%s\t%s, %s, ", op ? "sub" : "add",
  519. tbl_regs[inst & 0x07], tbl_regs[(inst >> 3) & 0x07]);
  520. if (immediate)
  521. dbg_printf("#%d", (inst >> 6) & 0x07);
  522. else
  523. dbg_printf("%s", tbl_regs[(inst >> 6) & 0x07]);
  524. return 0;
  525. }
  526. static WORD thumb_disasm_movshift(WORD inst, ADDRESS64 *addr)
  527. {
  528. WORD op = (inst >> 11) & 0x03;
  529. dbg_printf("\n\t%s\t%s, %s, #%u", tbl_shifts[op],
  530. tbl_regs[inst & 0x07], tbl_regs[(inst >> 3) & 0x07], (inst >> 6) & 0x1f);
  531. return 0;
  532. }
  533. static UINT thumb2_disasm_srtrans(UINT inst, ADDRESS64 *addr)
  534. {
  535. UINT fromsr = (inst >> 21) & 0x03;
  536. UINT sysreg = inst & 0xff;
  537. if (fromsr == 3 && get_nibble(inst,4) == 0x0f && sysreg <= 20)
  538. {
  539. dbg_printf("\n\tmrs\t%s, %s", tbl_regs[get_nibble(inst, 2)], tbl_special_regs_t2[sysreg]);
  540. return 0;
  541. }
  542. if (fromsr == 0 && sysreg <= 20)
  543. {
  544. dbg_printf("\n\tmsr\t%s, %s", tbl_special_regs_t2[sysreg], tbl_regs[get_nibble(inst, 4)]);
  545. return 0;
  546. }
  547. return inst;
  548. }
  549. static UINT thumb2_disasm_hint(UINT inst, ADDRESS64 *addr)
  550. {
  551. WORD op1 = (inst >> 8) & 0x07;
  552. WORD op2 = inst & 0xff;
  553. if (op1) return inst;
  554. if (op2 <= 4)
  555. {
  556. dbg_printf("\n\t%s", tbl_hints_t2[op2]);
  557. return 0;
  558. }
  559. if (op2 & 0xf0)
  560. {
  561. dbg_printf("\n\tdbg\t#%u", get_nibble(inst, 0));
  562. return 0;
  563. }
  564. return inst;
  565. }
  566. static UINT thumb2_disasm_miscctrl(UINT inst, ADDRESS64 *addr)
  567. {
  568. WORD op = (inst >> 4) & 0x0f;
  569. switch (op)
  570. {
  571. case 2:
  572. dbg_printf("\n\tclrex");
  573. break;
  574. case 4:
  575. dbg_printf("\n\tdsb\t#%u", get_nibble(inst, 0));
  576. break;
  577. case 5:
  578. dbg_printf("\n\tdmb\t#%u", get_nibble(inst, 0));
  579. break;
  580. case 6:
  581. dbg_printf("\n\tisb\t#%u", get_nibble(inst, 0));
  582. break;
  583. default:
  584. return inst;
  585. }
  586. return 0;
  587. }
  588. static UINT thumb2_disasm_branch(UINT inst, ADDRESS64 *addr)
  589. {
  590. UINT S = (inst >> 26) & 0x01;
  591. UINT L = (inst >> 14) & 0x01;
  592. UINT I1 = !(((inst >> 13) & 0x01) ^ S);
  593. UINT C = !((inst >> 12) & 0x01);
  594. UINT I2 = !(((inst >> 11) & 0x01) ^ S);
  595. UINT offset = (inst & 0x000007ff) << 1;
  596. if (C)
  597. {
  598. offset |= I1 << 19 | I2 << 18 | (inst & 0x003f0000) >> 4;
  599. if (S) offset |= 0x0fff << 20;
  600. }
  601. else
  602. {
  603. offset |= I1 << 23 | I2 << 22 | (inst & 0x03ff0000) >> 4;
  604. if (S) offset |= 0xff << 24;
  605. }
  606. dbg_printf("\n\tb%s%s\t", L ? "l" : "", C ? tbl_cond[(inst >> 22) & 0x0f] : "");
  607. db_printsym(addr->Offset + offset + 4);
  608. return 0;
  609. }
  610. static UINT thumb2_disasm_misc(UINT inst, ADDRESS64 *addr)
  611. {
  612. WORD op1 = (inst >> 20) & 0x03;
  613. WORD op2 = (inst >> 4) & 0x03;
  614. if (get_nibble(inst, 4) != get_nibble(inst, 0))
  615. return inst;
  616. if (op1 == 3 && op2 == 0)
  617. {
  618. dbg_printf("\n\tclz\t%s, %s", tbl_regs[get_nibble(inst, 2)], tbl_regs[get_nibble(inst, 0)]);
  619. return 0;
  620. }
  621. if (op1 == 1)
  622. {
  623. dbg_printf("\n\t%s\t%s, %s", tbl_miscops_t2[op2], tbl_regs[get_nibble(inst, 2)],
  624. tbl_regs[get_nibble(inst, 0)]);
  625. return 0;
  626. }
  627. return inst;
  628. }
  629. static UINT thumb2_disasm_dataprocessingreg(UINT inst, ADDRESS64 *addr)
  630. {
  631. WORD op1 = (inst >> 20) & 0x07;
  632. WORD op2 = (inst >> 4) & 0x0f;
  633. if (!op2)
  634. {
  635. dbg_printf("\n\t%s%s\t%s, %s, %s", tbl_shifts[op1 >> 1], (op1 & 1)?"s":"",
  636. tbl_regs[get_nibble(inst, 2)], tbl_regs[get_nibble(inst, 4)],
  637. tbl_regs[get_nibble(inst, 0)]);
  638. return 0;
  639. }
  640. if ((op2 & 0x0C) == 0x08 && get_nibble(inst, 4) == 0x0f)
  641. {
  642. dbg_printf("\n\t%sxt%s\t%s, %s", (op1 & 1)?"u":"s", (op1 & 4)?"b":"h",
  643. tbl_regs[get_nibble(inst, 2)], tbl_regs[get_nibble(inst, 0)]);
  644. if (op2 & 0x03)
  645. dbg_printf(", ROR #%u", (op2 & 3) * 8);
  646. return 0;
  647. }
  648. return inst;
  649. }
  650. static UINT thumb2_disasm_mul(UINT inst, ADDRESS64 *addr)
  651. {
  652. WORD op1 = (inst >> 20) & 0x07;
  653. WORD op2 = (inst >> 4) & 0x03;
  654. if (op1)
  655. return inst;
  656. if (op2 == 0 && get_nibble(inst, 3) != 0xf)
  657. {
  658. dbg_printf("\n\tmla\t%s, %s, %s, %s", tbl_regs[get_nibble(inst, 2)],
  659. tbl_regs[get_nibble(inst, 4)],
  660. tbl_regs[get_nibble(inst, 0)],
  661. tbl_regs[get_nibble(inst, 3)]);
  662. return 0;
  663. }
  664. if (op2 == 0 && get_nibble(inst, 3) == 0xf)
  665. {
  666. dbg_printf("\n\tmul\t%s, %s, %s", tbl_regs[get_nibble(inst, 2)],
  667. tbl_regs[get_nibble(inst, 4)],
  668. tbl_regs[get_nibble(inst, 0)]);
  669. return 0;
  670. }
  671. if (op2 == 1)
  672. {
  673. dbg_printf("\n\tmls\t%s, %s, %s, %s", tbl_regs[get_nibble(inst, 2)],
  674. tbl_regs[get_nibble(inst, 4)],
  675. tbl_regs[get_nibble(inst, 0)],
  676. tbl_regs[get_nibble(inst, 3)]);
  677. return 0;
  678. }
  679. return inst;
  680. }
  681. static UINT thumb2_disasm_longmuldiv(UINT inst, ADDRESS64 *addr)
  682. {
  683. WORD op1 = (inst >> 20) & 0x07;
  684. WORD op2 = (inst >> 4) & 0x0f;
  685. if (op2 == 0)
  686. {
  687. switch (op1)
  688. {
  689. case 0:
  690. dbg_printf("\n\tsmull\t");
  691. break;
  692. case 2:
  693. dbg_printf("\n\tumull\t");
  694. break;
  695. case 4:
  696. dbg_printf("\n\tsmlal\t");
  697. break;
  698. case 6:
  699. dbg_printf("\n\tumlal\t");
  700. break;
  701. default:
  702. return inst;
  703. }
  704. dbg_printf("%s, %s, %s, %s", tbl_regs[get_nibble(inst, 3)], tbl_regs[get_nibble(inst, 2)],
  705. tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)]);
  706. return 0;
  707. }
  708. if (op2 == 0xffff)
  709. {
  710. switch (op1)
  711. {
  712. case 1:
  713. dbg_printf("\n\tsdiv\t");
  714. break;
  715. case 3:
  716. dbg_printf("\n\tudiv\t");
  717. break;
  718. default:
  719. return inst;
  720. }
  721. dbg_printf("%s, %s, %s", tbl_regs[get_nibble(inst, 2)], tbl_regs[get_nibble(inst, 4)],
  722. tbl_regs[get_nibble(inst, 0)]);
  723. return 0;
  724. }
  725. return inst;
  726. }
  727. static UINT thumb2_disasm_str(UINT inst, ADDRESS64 *addr)
  728. {
  729. WORD op1 = (inst >> 21) & 0x07;
  730. WORD op2 = (inst >> 6) & 0x3f;
  731. if ((op1 & 0x03) == 3) return inst;
  732. if (!(op1 & 0x04) && inst & 0x0800)
  733. {
  734. int offset;
  735. dbg_printf("\n\tstr%s\t%s, [%s", tbl_width_t2[op1 & 0x03], tbl_regs[get_nibble(inst, 3)],
  736. tbl_regs[get_nibble(inst, 4)]);
  737. offset = inst & 0xff;
  738. if (!(inst & 0x0200)) offset *= -1;
  739. if (!(inst & 0x0400) && (inst & 0x0100)) dbg_printf("], #%i", offset);
  740. else if (inst & 0x0400) dbg_printf(", #%i]%s", offset, (inst & 0x0100)?"!":"");
  741. else return inst;
  742. return 0;
  743. }
  744. if (!(op1 & 0x04) && !op2)
  745. {
  746. dbg_printf("\n\tstr%s\t%s, [%s, %s, LSL #%u]", tbl_width_t2[op1 & 0x03],
  747. tbl_regs[get_nibble(inst, 3)], tbl_regs[get_nibble(inst, 4)],
  748. tbl_regs[get_nibble(inst, 0)], (inst >> 4) & 0x3);
  749. return 0;
  750. }
  751. if (op1 & 0x04)
  752. {
  753. dbg_printf("\n\tstr%s\t%s, [%s, #%u]", tbl_width_t2[op1 & 0x03],
  754. tbl_regs[get_nibble(inst, 3)], tbl_regs[get_nibble(inst, 4)], inst & 0x0fff);
  755. return 0;
  756. }
  757. return inst;
  758. }
  759. static UINT thumb2_disasm_ldrword(UINT inst, ADDRESS64 *addr)
  760. {
  761. WORD op1 = (inst >> 23) & 0x01;
  762. WORD op2 = (inst >> 6) & 0x3f;
  763. int offset;
  764. if (get_nibble(inst, 4) == 0x0f)
  765. {
  766. offset = inst & 0x0fff;
  767. if (!op1) offset *= -1;
  768. offset += 3;
  769. dbg_printf("\n\tldr\t%s, ", tbl_regs[get_nibble(inst, 3)]);
  770. db_printsym(addr->Offset + offset);
  771. return 0;
  772. }
  773. if (!op1 && !op2)
  774. {
  775. dbg_printf("\n\tldr\t%s, [%s, %s, LSL #%u]", tbl_regs[get_nibble(inst, 3)],
  776. tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)], (inst >> 4) & 0x3);
  777. return 0;
  778. }
  779. if (!op1 && (op2 & 0x3c) == 0x38)
  780. {
  781. dbg_printf("\n\tldrt\t%s, [%s, #%u]", tbl_regs[get_nibble(inst, 3)],
  782. tbl_regs[get_nibble(inst, 4)], inst & 0xff);
  783. return 0;
  784. }
  785. dbg_printf("\n\tldr\t%s, [%s", tbl_regs[get_nibble(inst, 3)], tbl_regs[get_nibble(inst, 4)]);
  786. if (op1)
  787. {
  788. dbg_printf(", #%u]", inst & 0x0fff);
  789. return 0;
  790. }
  791. offset = inst & 0xff;
  792. if (!(inst & 0x0200)) offset *= -1;
  793. if (!(inst & 0x0400) && (inst & 0x0100)) dbg_printf("], #%i", offset);
  794. else if (inst & 0x0400) dbg_printf(", #%i]%s", offset, (inst & 0x0100)?"!":"");
  795. else return inst;
  796. return 0;
  797. }
  798. static UINT thumb2_disasm_preload(UINT inst, ADDRESS64 *addr)
  799. {
  800. WORD op1 = (inst >> 23) & 0x03;
  801. if (!(op1 & 0x01) && !((inst >> 6) & 0x3f) && get_nibble(inst, 4) != 15)
  802. {
  803. WORD shift = (inst >> 4) & 0x03;
  804. dbg_printf("\n\t%s\t[%s, %s", op1?"pli":"pld", tbl_regs[get_nibble(inst, 4)],
  805. tbl_regs[get_nibble(inst, 0)]);
  806. if (shift) dbg_printf(", lsl #%u]", shift);
  807. else dbg_printf("]");
  808. return 0;
  809. }
  810. if (get_nibble(inst, 4) != 15)
  811. {
  812. dbg_printf("\n\t%s\t[%s, #%d]", (op1 & 0x02)?"pli":"pld", tbl_regs[get_nibble(inst, 4)],
  813. (op1 & 0x01)?(inst & 0x0fff):(-1 * (inst & 0xff)));
  814. return 0;
  815. }
  816. if (get_nibble(inst, 4) == 15)
  817. {
  818. int offset = inst & 0x0fff;
  819. if (!op1) offset *= -1;
  820. dbg_printf("\n\t%s\t", (op1 & 0x02)?"pli":"pld");
  821. db_printsym(addr->Offset + offset + 4);
  822. return 0;
  823. }
  824. return inst;
  825. }
  826. static UINT thumb2_disasm_ldrnonword(UINT inst, ADDRESS64 *addr)
  827. {
  828. WORD op1 = (inst >> 23) & 0x03;
  829. WORD hw = (inst >> 21) & 0x01;
  830. if (!(op1 & 0x01) && !((inst >> 6) & 0x3f) && get_nibble(inst, 4) != 15)
  831. {
  832. WORD shift = (inst >> 4) & 0x03;
  833. dbg_printf("\n\t%s%s\t%s, [%s, %s", op1?"ldrs":"ldr", hw?"h":"b",
  834. tbl_regs[get_nibble(inst, 3)], tbl_regs[get_nibble(inst, 4)],
  835. tbl_regs[get_nibble(inst, 0)]);
  836. if (shift) dbg_printf(", lsl #%u]", shift);
  837. else dbg_printf("]");
  838. return 0;
  839. }
  840. if (!(op1 & 0x01) && ((inst >> 8) & 0x0f) == 14 && get_nibble(inst, 4) != 15)
  841. {
  842. WORD offset = inst & 0xff;
  843. dbg_printf("\n\t%s%s\t%s, [%s", op1?"ldrs":"ldr", hw?"ht":"bt",
  844. tbl_regs[get_nibble(inst, 3)], tbl_regs[get_nibble(inst, 4)]);
  845. if (offset) dbg_printf(", #%u]", offset);
  846. else dbg_printf("]");
  847. return 0;
  848. }
  849. if (get_nibble(inst, 4) != 15)
  850. {
  851. int offset;
  852. dbg_printf("\n\t%s%s\t%s, [%s", (op1 & 0x02)?"ldrs":"ldr", hw?"h":"b",
  853. tbl_regs[get_nibble(inst, 3)], tbl_regs[get_nibble(inst, 4)]);
  854. if (op1 & 0x01)
  855. {
  856. dbg_printf(", #%u]", inst & 0x0fff);
  857. return 0;
  858. }
  859. offset = inst & 0xff;
  860. if (!(inst & 0x0200)) offset *= -1;
  861. if (!(inst & 0x0400) && (inst & 0x0100)) dbg_printf("], #%i", offset);
  862. else if (inst & 0x0400) dbg_printf(", #%i]%s", offset, (inst & 0x0100)?"!":"");
  863. else return inst;
  864. return 0;
  865. }
  866. if (get_nibble(inst, 4) == 15)
  867. {
  868. int offset = inst & 0x0fff;
  869. if (!op1) offset *= -1;
  870. dbg_printf("\n\t%s%s\t%s, ", (op1 & 0x02)?"ldrs":"ldr", hw?"h":"b",
  871. tbl_regs[get_nibble(inst, 3)]);
  872. db_printsym(addr->Offset + offset + 4);
  873. return 0;
  874. }
  875. return inst;
  876. }
  877. static UINT thumb2_disasm_dataprocessing(UINT inst, ADDRESS64 *addr)
  878. {
  879. WORD op = (inst >> 20) & 0x1f;
  880. WORD imm5 = ((inst >> 10) & 0x1c) + ((inst >> 6) & 0x03);
  881. switch (op)
  882. {
  883. case 0:
  884. {
  885. WORD offset = ((inst >> 15) & 0x0800) + ((inst >> 4) & 0x0700) + (inst & 0xff);
  886. if (get_nibble(inst, 4) == 15)
  887. {
  888. dbg_printf("\n\tadr\t%s, ", tbl_regs[get_nibble(inst, 2)]);
  889. db_printsym(addr->Offset + offset + 4);
  890. }
  891. else
  892. dbg_printf("\n\taddw\t%s, %s, #%u", tbl_regs[get_nibble(inst, 2)],
  893. tbl_regs[get_nibble(inst, 4)], offset);
  894. return 0;
  895. }
  896. case 4:
  897. case 12:
  898. {
  899. WORD offset = ((inst >> 15) & 0x0800) + ((inst >> 4) & 0xf000) +
  900. ((inst >> 4) & 0x0700) + (inst & 0xff);
  901. dbg_printf("\n\t%s\t%s, #%u", op == 12 ? "movt" : "movw", tbl_regs[get_nibble(inst, 2)],
  902. offset);
  903. return 0;
  904. }
  905. case 10:
  906. {
  907. int offset = ((inst >> 15) & 0x0800) + ((inst >> 4) & 0x0700) + (inst & 0xff);
  908. if (get_nibble(inst, 4) == 15)
  909. {
  910. offset *= -1;
  911. dbg_printf("\n\tadr\t%s, ", tbl_regs[get_nibble(inst, 2)]);
  912. db_printsym(addr->Offset + offset + 4);
  913. }
  914. else
  915. dbg_printf("\n\tsubw\t%s, %s, #%u", tbl_regs[get_nibble(inst, 2)],
  916. tbl_regs[get_nibble(inst, 4)], offset);
  917. return 0;
  918. }
  919. case 16:
  920. case 18:
  921. case 24:
  922. case 26:
  923. {
  924. BOOL sign = op < 24;
  925. WORD sh = (inst >> 21) & 0x01;
  926. WORD sat = (inst & 0x1f);
  927. if (sign) sat++;
  928. if (imm5)
  929. dbg_printf("\n\t%s\t%s, #%u, %s, %s #%u", sign ? "ssat" : "usat",
  930. tbl_regs[get_nibble(inst, 2)], sat, tbl_regs[get_nibble(inst, 4)],
  931. sh ? "asr" : "lsl", imm5);
  932. else
  933. dbg_printf("\n\t%s\t%s, #%u, %s", sign ? "ssat" : "usat", tbl_regs[get_nibble(inst, 2)],
  934. sat, tbl_regs[get_nibble(inst, 4)]);
  935. return 0;
  936. }
  937. case 20:
  938. case 28:
  939. {
  940. WORD width = (inst & 0x1f) + 1;
  941. dbg_printf("\n\t%s\t%s, %s, #%u, #%u", op == 28 ? "ubfx" : "sbfx",
  942. tbl_regs[get_nibble(inst, 2)], tbl_regs[get_nibble(inst, 4)], imm5, width);
  943. return 0;
  944. }
  945. case 22:
  946. {
  947. WORD msb = (inst & 0x1f) + 1 - imm5;
  948. if (get_nibble(inst, 4) == 15)
  949. dbg_printf("\n\tbfc\t%s, #%u, #%u", tbl_regs[get_nibble(inst, 2)], imm5, msb);
  950. else
  951. dbg_printf("\n\tbfi\t%s, %s, #%u, #%u", tbl_regs[get_nibble(inst, 2)],
  952. tbl_regs[get_nibble(inst, 4)], imm5, msb);
  953. return 0;
  954. }
  955. default:
  956. return inst;
  957. }
  958. }
  959. static UINT thumb2_disasm_dataprocessingmod(UINT inst, ADDRESS64 *addr)
  960. {
  961. WORD op = (inst >> 21) & 0x0f;
  962. WORD sf = (inst >> 20) & 0x01;
  963. WORD offset = ((inst >> 15) & 0x0800) + ((inst >> 4) & 0x0700) + (inst & 0xff);
  964. /* FIXME: use ThumbExpandImm_C */
  965. switch (op)
  966. {
  967. case 0:
  968. if (get_nibble(inst, 2) == 15)
  969. dbg_printf("\n\ttst\t%s, #%u", tbl_regs[get_nibble(inst, 4)], offset);
  970. else
  971. dbg_printf("\n\tand%s\t%s, %s, #%u", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  972. tbl_regs[get_nibble(inst, 4)], offset);
  973. return 0;
  974. case 1:
  975. dbg_printf("\n\tbic%s\t%s, %s, #%u", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  976. tbl_regs[get_nibble(inst, 4)], offset);
  977. return 0;
  978. case 2:
  979. if (get_nibble(inst, 4) == 15)
  980. dbg_printf("\n\tmov%s\t%s, #%u", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)], offset);
  981. else
  982. dbg_printf("\n\torr%s\t%s, %s, #%u", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  983. tbl_regs[get_nibble(inst, 4)], offset);
  984. return 0;
  985. case 3:
  986. if (get_nibble(inst, 4) == 15)
  987. dbg_printf("\n\tmvn%s\t%s, #%u", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)], offset);
  988. else
  989. dbg_printf("\n\torn%s\t%s, %s, #%u", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  990. tbl_regs[get_nibble(inst, 4)], offset);
  991. return 0;
  992. case 4:
  993. if (get_nibble(inst, 2) == 15)
  994. dbg_printf("\n\tteq\t%s, #%u", tbl_regs[get_nibble(inst, 4)], offset);
  995. else
  996. dbg_printf("\n\teor%s\t%s, %s, #%u", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  997. tbl_regs[get_nibble(inst, 4)], offset);
  998. return 0;
  999. case 8:
  1000. if (get_nibble(inst, 2) == 15)
  1001. dbg_printf("\n\tcmn\t%s, #%u", tbl_regs[get_nibble(inst, 4)], offset);
  1002. else
  1003. dbg_printf("\n\tadd%s\t%s, %s, #%u", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  1004. tbl_regs[get_nibble(inst, 4)], offset);
  1005. return 0;
  1006. case 10:
  1007. dbg_printf("\n\tadc%s\t%s, %s, #%u", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  1008. tbl_regs[get_nibble(inst, 4)], offset);
  1009. return 0;
  1010. case 11:
  1011. dbg_printf("\n\tsbc%s\t%s, %s, #%u", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  1012. tbl_regs[get_nibble(inst, 4)], offset);
  1013. return 0;
  1014. case 13:
  1015. if (get_nibble(inst, 2) == 15)
  1016. dbg_printf("\n\tcmp\t%s, #%u", tbl_regs[get_nibble(inst, 4)], offset);
  1017. else
  1018. dbg_printf("\n\tsub%s\t%s, %s, #%u", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  1019. tbl_regs[get_nibble(inst, 4)], offset);
  1020. return 0;
  1021. case 14:
  1022. dbg_printf("\n\trsb%s\t%s, %s, #%u", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  1023. tbl_regs[get_nibble(inst, 4)], offset);
  1024. return 0;
  1025. default:
  1026. return inst;
  1027. }
  1028. }
  1029. static UINT thumb2_disasm_dataprocessingshift(UINT inst, ADDRESS64 *addr)
  1030. {
  1031. WORD op = (inst >> 21) & 0x0f;
  1032. WORD sf = (inst >> 20) & 0x01;
  1033. WORD imm5 = ((inst >> 10) & 0x1c) + ((inst >> 6) & 0x03);
  1034. WORD type = (inst >> 4) & 0x03;
  1035. if (!imm5 && (type == 1 || type == 2)) imm5 = 32;
  1036. else if (!imm5 && type == 3) type = 4;
  1037. switch (op)
  1038. {
  1039. case 0:
  1040. if (get_nibble(inst, 2) == 15)
  1041. dbg_printf("\n\ttst\t%s, %s", tbl_regs[get_nibble(inst, 4)],
  1042. tbl_regs[get_nibble(inst, 0)]);
  1043. else
  1044. dbg_printf("\n\tand%s\t%s, %s, %s", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  1045. tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)]);
  1046. break;
  1047. case 1:
  1048. dbg_printf("\n\tbic%s\t%s, %s, %s", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  1049. tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)]);
  1050. break;
  1051. case 2:
  1052. if (get_nibble(inst, 4) == 15)
  1053. {
  1054. if (type == 4)
  1055. dbg_printf("\n\trrx%s\t%s, %s", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)], tbl_regs[get_nibble(inst, 0)]);
  1056. else if (!type && !imm5)
  1057. dbg_printf("\n\tmov%s\t%s, %s", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)], tbl_regs[get_nibble(inst, 0)]);
  1058. else
  1059. dbg_printf("\n\t%s%s\t%s, %s, #%u", tbl_shifts[type], sf ? "s" : "", tbl_regs[get_nibble(inst, 2)], tbl_regs[get_nibble(inst, 0)], imm5);
  1060. return 0;
  1061. }
  1062. else
  1063. dbg_printf("\n\torr%s\t%s, %s, %s", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  1064. tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)]);
  1065. break;
  1066. case 3:
  1067. if (get_nibble(inst, 4) == 15)
  1068. dbg_printf("\n\tmvn%s\t%s, %s, %s", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  1069. tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)]);
  1070. else
  1071. dbg_printf("\n\torn%s\t%s, %s, %s", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  1072. tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)]);
  1073. break;
  1074. case 4:
  1075. if (get_nibble(inst, 2) == 15)
  1076. dbg_printf("\n\tteq\t%s, %s", tbl_regs[get_nibble(inst, 4)],
  1077. tbl_regs[get_nibble(inst, 0)]);
  1078. else
  1079. dbg_printf("\n\teor%s\t%s, %s, %s", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  1080. tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)]);
  1081. break;
  1082. case 8:
  1083. if (get_nibble(inst, 2) == 15)
  1084. dbg_printf("\n\tcmn\t%s, %s", tbl_regs[get_nibble(inst, 4)],
  1085. tbl_regs[get_nibble(inst, 0)]);
  1086. else
  1087. dbg_printf("\n\tadd%s\t%s, %s, %s", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  1088. tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)]);
  1089. break;
  1090. case 10:
  1091. dbg_printf("\n\tadc%s\t%s, %s, %s", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  1092. tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)]);
  1093. break;
  1094. case 11:
  1095. dbg_printf("\n\tsbc%s\t%s, %s, %s", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  1096. tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)]);
  1097. break;
  1098. case 13:
  1099. if (get_nibble(inst, 2) == 15)
  1100. dbg_printf("\n\tcmp\t%s, %s", tbl_regs[get_nibble(inst, 4)],
  1101. tbl_regs[get_nibble(inst, 0)]);
  1102. else
  1103. dbg_printf("\n\tsub%s\t%s, %s, %s", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  1104. tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)]);
  1105. break;
  1106. case 14:
  1107. dbg_printf("\n\trsb%s\t%s, %s, %s", sf ? "s" : "", tbl_regs[get_nibble(inst, 2)],
  1108. tbl_regs[get_nibble(inst, 4)], tbl_regs[get_nibble(inst, 0)]);
  1109. break;
  1110. default:
  1111. return inst;
  1112. }
  1113. if (type == 4)
  1114. dbg_printf(", rrx");
  1115. else if (type || imm5)
  1116. dbg_printf(", %s #%u", tbl_shifts[type], imm5);
  1117. return 0;
  1118. }
  1119. static UINT thumb2_disasm_coprocdat(UINT inst, ADDRESS64 *addr)
  1120. {
  1121. WORD opc2 = (inst >> 5) & 0x07;
  1122. dbg_printf("\n\tcdp%s\tp%u, #%u, cr%u, cr%u, cr%u", (inst & 0x10000000)?"2":"",
  1123. get_nibble(inst, 2), get_nibble(inst, 5), get_nibble(inst, 3),
  1124. get_nibble(inst, 4), get_nibble(inst, 0));
  1125. if (opc2) dbg_printf(", #%u", opc2);
  1126. return 0;
  1127. }
  1128. static UINT thumb2_disasm_coprocmov1(UINT inst, ADDRESS64 *addr)
  1129. {
  1130. WORD opc1 = (inst >> 21) & 0x07;
  1131. WORD opc2 = (inst >> 5) & 0x07;
  1132. dbg_printf("\n\t%s%s\tp%u, #%u, %s, cr%u, cr%u", (inst & 0x00100000)?"mrc":"mcr",
  1133. (inst & 0x10000000)?"2":"", get_nibble(inst, 2), opc1,
  1134. tbl_regs[get_nibble(inst, 3)], get_nibble(inst, 4), get_nibble(inst, 0));
  1135. if (opc2) dbg_printf(", #%u", opc2);
  1136. return 0;
  1137. }
  1138. static UINT thumb2_disasm_coprocmov2(UINT inst, ADDRESS64 *addr)
  1139. {
  1140. dbg_printf("\n\t%s%s\tp%u, #%u, %s, %s, cr%u", (inst & 0x00100000)?"mrrc":"mcrr",
  1141. (inst & 0x10000000)?"2":"", get_nibble(inst, 2), get_nibble(inst, 1),
  1142. tbl_regs[get_nibble(inst, 3)], tbl_regs[get_nibble(inst, 4)], get_nibble(inst, 0));
  1143. return 0;
  1144. }
  1145. static UINT thumb2_disasm_coprocdatatrans(UINT inst, ADDRESS64 *addr)
  1146. {
  1147. WORD indexing = (inst >> 24) & 0x01;
  1148. WORD direction = (inst >> 23) & 0x01;
  1149. WORD translen = (inst >> 22) & 0x01;
  1150. WORD writeback = (inst >> 21) & 0x01;
  1151. WORD load = (inst >> 20) & 0x01;
  1152. short offset = (inst & 0xff) << 2;
  1153. if (!direction) offset *= -1;
  1154. dbg_printf("\n\t%s%s%s", load ? "ldc" : "stc", (inst & 0x10000000)?"2":"", translen ? "l" : "");
  1155. if (indexing)
  1156. {
  1157. if (load && get_nibble(inst, 4) == 15)
  1158. {
  1159. dbg_printf("\tp%u, cr%u, ", get_nibble(inst, 2), get_nibble(inst, 3));
  1160. db_printsym(addr->Offset + offset + 4);
  1161. }
  1162. else
  1163. dbg_printf("\tp%u, cr%u, [%s, #%d]%s", get_nibble(inst, 2), get_nibble(inst, 3), tbl_regs[get_nibble(inst, 4)], offset, writeback?"!":"");
  1164. }
  1165. else
  1166. {
  1167. if (writeback)
  1168. dbg_printf("\tp%u, cr%u, [%s], #%d", get_nibble(inst, 2), get_nibble(inst, 3), tbl_regs[get_nibble(inst, 4)], offset);
  1169. else
  1170. dbg_printf("\tp%u, cr%u, [%s], {%u}", get_nibble(inst, 2), get_nibble(inst, 3), tbl_regs[get_nibble(inst, 4)], inst & 0xff);
  1171. }
  1172. return 0;
  1173. }
  1174. static UINT thumb2_disasm_ldrstrmul(UINT inst, ADDRESS64 *addr)
  1175. {
  1176. short load = (inst >> 20) & 0x01;
  1177. short writeback = (inst >> 21) & 0x01;
  1178. short decbefore = (inst >> 24) & 0x01;
  1179. short i;
  1180. short last=15;
  1181. for (i=15;i>=0;i--)
  1182. if ((inst>>i) & 1)
  1183. {
  1184. last = i;
  1185. break;
  1186. }
  1187. if (writeback && get_nibble(inst, 4) == 13)
  1188. dbg_printf("\n\t%s\t{", load ? "pop" : "push");
  1189. else
  1190. dbg_printf("\n\t%s%s\t%s%s, {", load ? "ldm" : "stm", decbefore ? "db" : "ia",
  1191. tbl_regs[get_nibble(inst, 4)], writeback ? "!" : "");
  1192. for (i=0;i<=15;i++)
  1193. if ((inst>>i) & 1)
  1194. {
  1195. if (i == last) dbg_printf("%s", tbl_regs[i]);
  1196. else dbg_printf("%s, ", tbl_regs[i]);
  1197. }
  1198. dbg_printf("}");
  1199. return 0;
  1200. }
  1201. static UINT thumb2_disasm_ldrstrextbr(UINT inst, ADDRESS64 *addr)
  1202. {
  1203. WORD op1 = (inst >> 23) & 0x03;
  1204. WORD op2 = (inst >> 20) & 0x03;
  1205. WORD op3 = (inst >> 4) & 0x0f;
  1206. WORD indexing = (inst >> 24) & 0x01;
  1207. WORD direction = (inst >> 23) & 0x01;
  1208. WORD writeback = (inst >> 21) & 0x01;
  1209. WORD load = (inst >> 20) & 0x01;
  1210. short offset = (inst & 0xff) << 2;
  1211. if (op1 == 1 && op2 == 1 && op3 < 2)
  1212. {
  1213. WORD halfword = (inst >> 4) & 0x01;
  1214. if (halfword)
  1215. dbg_printf("\n\ttbh\t [%s, %s, lsl #1]", tbl_regs[get_nibble(inst, 4)],
  1216. tbl_regs[get_nibble(inst, 0)]);
  1217. else
  1218. dbg_printf("\n\ttbb\t [%s, %s]", tbl_regs[get_nibble(inst, 4)],
  1219. tbl_regs[get_nibble(inst, 0)]);
  1220. return 0;
  1221. }
  1222. if (op1 == 0 && op2 < 2)
  1223. {
  1224. if (get_nibble(inst, 2) == 15)
  1225. dbg_printf("\n\tldrex\t %s, [%s, #%u]", tbl_regs[get_nibble(inst, 3)],
  1226. tbl_regs[get_nibble(inst, 4)], offset);
  1227. else
  1228. dbg_printf("\n\tstrex\t %s, %s, [%s, #%u]", tbl_regs[get_nibble(inst, 2)],
  1229. tbl_regs[get_nibble(inst, 3)], tbl_regs[get_nibble(inst, 4)], offset);
  1230. return 0;
  1231. }
  1232. if (op1 == 1 && op2 < 2)
  1233. {
  1234. WORD halfword = (inst >> 4) & 0x01;
  1235. if (get_nibble(inst, 0) == 15)
  1236. dbg_printf("\n\tldrex%s\t %s, [%s]", halfword ? "h" : "b",
  1237. tbl_regs[get_nibble(inst, 3)], tbl_regs[get_nibble(inst, 4)]);
  1238. else
  1239. dbg_printf("\n\tstrex%s\t %s, %s, [%s]", halfword ? "h" : "b",
  1240. tbl_regs[get_nibble(inst, 0)], tbl_regs[get_nibble(inst, 3)],
  1241. tbl_regs[get_nibble(inst, 4)]);
  1242. return 0;
  1243. }
  1244. if (!direction) offset *= -1;
  1245. dbg_printf("\n\t%s\t", load ? "ldrd" : "strd");
  1246. if (indexing)
  1247. {
  1248. if (load && get_nibble(inst, 4) == 15)
  1249. {
  1250. dbg_printf("%s, %s, ", tbl_regs[get_nibble(inst, 3)], tbl_regs[get_nibble(inst, 2)]);
  1251. db_printsym(addr->Offset + offset + 4);
  1252. }
  1253. else
  1254. dbg_printf("%s, %s, [%s, #%d]%s", tbl_regs[get_nibble(inst, 3)],
  1255. tbl_regs[get_nibble(inst, 2)], tbl_regs[get_nibble(inst, 4)], offset,
  1256. writeback?"!":"");
  1257. }
  1258. else
  1259. dbg_printf("%s, %s, [%s], #%d", tbl_regs[get_nibble(inst, 3)],
  1260. tbl_regs[get_nibble(inst, 2)], tbl_regs[get_nibble(inst, 4)], offset);
  1261. return 0;
  1262. }
  1263. struct inst_arm
  1264. {
  1265. UINT mask;
  1266. UINT pattern;
  1267. UINT (*func)(UINT, ADDRESS64*);
  1268. };
  1269. static const struct inst_arm tbl_arm[] = {
  1270. { 0x0e000000, 0x0a000000, arm_disasm_branch },
  1271. { 0x0fc000f0, 0x00000090, arm_disasm_mul },
  1272. { 0x0f8000f0, 0x00800090, arm_disasm_longmul },
  1273. { 0x0fb00ff0, 0x01000090, arm_disasm_swp },
  1274. { 0x0e000090, 0x00000090, arm_disasm_halfwordtrans },
  1275. { 0x0ffffff0, 0x012fff10, arm_disasm_branchxchg },
  1276. { 0x0fbf0fff, 0x010f0000, arm_disasm_mrstrans },
  1277. { 0x0dbef000, 0x0128f000, arm_disasm_msrtrans },
  1278. { 0x0fb00000, 0x03000000, arm_disasm_wordmov },
  1279. { 0x0fffffff, 0x0320f000, arm_disasm_nop },
  1280. { 0x0c000000, 0x00000000, arm_disasm_dataprocessing },
  1281. { 0x0c000000, 0x04000000, arm_disasm_singletrans },
  1282. { 0x0e000000, 0x08000000, arm_disasm_blocktrans },
  1283. { 0x0f000000, 0x0f000000, arm_disasm_swi },
  1284. { 0x0f000010, 0x0e000010, arm_disasm_coproctrans },
  1285. { 0x0f000010, 0x0e000000, arm_disasm_coprocdataop },
  1286. { 0x0e000000, 0x0c000000, arm_disasm_coprocdatatrans },
  1287. { 0x00000000, 0x00000000, NULL }
  1288. };
  1289. struct inst_thumb16
  1290. {
  1291. WORD mask;
  1292. WORD pattern;
  1293. WORD (*func)(WORD, ADDRESS64*);
  1294. };
  1295. static const struct inst_thumb16 tbl_thumb16[] = {
  1296. { 0xfc00, 0x4400, thumb_disasm_hireg },
  1297. { 0xfc00, 0x4000, thumb_disasm_aluop },
  1298. { 0xf600, 0xb400, thumb_disasm_pushpop },
  1299. { 0xf000, 0xc000, thumb_disasm_blocktrans },
  1300. { 0xff00, 0xdf00, thumb_disasm_swi },
  1301. { 0xf000, 0xd000, thumb_disasm_condbranch },
  1302. { 0xf800, 0xe000, thumb_disasm_uncondbranch },
  1303. { 0xf000, 0xa000, thumb_disasm_loadadr },
  1304. { 0xf800, 0x4800, thumb_disasm_ldrpcrel },
  1305. { 0xf000, 0x9000, thumb_disasm_ldrsprel },
  1306. { 0xff00, 0xb000, thumb_disasm_addsprel },
  1307. { 0xe000, 0x6000, thumb_disasm_ldrimm },
  1308. { 0xf000, 0x8000, thumb_disasm_ldrhimm },
  1309. { 0xf200, 0x5000, thumb_disasm_ldrreg },
  1310. { 0xf200, 0x5200, thumb_disasm_ldrsreg },
  1311. { 0xe000, 0x2000, thumb_disasm_immop },
  1312. { 0xff00, 0xbf00, thumb_disasm_nop },
  1313. { 0xf800, 0x1800, thumb_disasm_addsub },
  1314. { 0xe000, 0x0000, thumb_disasm_movshift },
  1315. { 0x0000, 0x0000, NULL }
  1316. };
  1317. static const struct inst_arm tbl_thumb32[] = {
  1318. { 0xfff0f000, 0xf3e08000, thumb2_disasm_srtrans },
  1319. { 0xfff0f000, 0xf3808000, thumb2_disasm_srtrans },
  1320. { 0xfff0d000, 0xf3a08000, thumb2_disasm_hint },
  1321. { 0xfff0d000, 0xf3b08000, thumb2_disasm_miscctrl },
  1322. { 0xf8008000, 0xf0008000, thumb2_disasm_branch },
  1323. { 0xffc0f0c0, 0xfa80f080, thumb2_disasm_misc },
  1324. { 0xff80f000, 0xfa00f000, thumb2_disasm_dataprocessingreg },
  1325. { 0xff8000c0, 0xfb000000, thumb2_disasm_mul },
  1326. { 0xff8000f0, 0xfb800000, thumb2_disasm_longmuldiv },
  1327. { 0xff8000f0, 0xfb8000f0, thumb2_disasm_longmuldiv },
  1328. { 0xff100000, 0xf8000000, thumb2_disasm_str },
  1329. { 0xff700000, 0xf8500000, thumb2_disasm_ldrword },
  1330. { 0xfe70f000, 0xf810f000, thumb2_disasm_preload },
  1331. { 0xfe500000, 0xf8100000, thumb2_disasm_ldrnonword },
  1332. { 0xfa008000, 0xf2000000, thumb2_disasm_dataprocessing },
  1333. { 0xfa008000, 0xf0000000, thumb2_disasm_dataprocessingmod },
  1334. { 0xfe008000, 0xea000000, thumb2_disasm_dataprocessingshift },
  1335. { 0xef000010, 0xee000000, thumb2_disasm_coprocdat },
  1336. { 0xef000010, 0xee000010, thumb2_disasm_coprocmov1 },
  1337. { 0xefe00000, 0xec400000, thumb2_disasm_coprocmov2 },
  1338. { 0xee000000, 0xec000000, thumb2_disasm_coprocdatatrans },
  1339. { 0xfe402000, 0xe8000000, thumb2_disasm_ldrstrmul },
  1340. { 0xfe400000, 0xe8400000, thumb2_disasm_ldrstrextbr },
  1341. { 0x00000000, 0x00000000, NULL }
  1342. };
  1343. /***********************************************************************
  1344. * disasm_one_insn
  1345. *
  1346. * Disassemble instruction at 'addr'. addr is changed to point to the
  1347. * start of the next instruction.
  1348. */
  1349. void be_arm_disasm_one_insn(ADDRESS64 *addr, int display)
  1350. {
  1351. struct inst_arm *a_ptr = (struct inst_arm *)&tbl_arm;
  1352. struct inst_thumb16 *t_ptr = (struct inst_thumb16 *)&tbl_thumb16;
  1353. struct inst_arm *t2_ptr = (struct inst_arm *)&tbl_thumb32;
  1354. UINT inst;
  1355. WORD tinst;
  1356. int size;
  1357. int matched = 0;
  1358. char tmp[64];
  1359. DWORD_PTR* pval;
  1360. if (!memory_get_register(CV_ARM_CPSR, &pval, tmp, sizeof(tmp)))
  1361. dbg_printf("\n\tmemory_get_register failed: %s", tmp);
  1362. else
  1363. db_disasm_thumb = (*pval & 0x20) != 0;
  1364. db_display = display;
  1365. if (!db_disasm_thumb)
  1366. {
  1367. size = ARM_INSN_SIZE;
  1368. inst = db_get_inst( memory_to_linear_addr(addr), size );
  1369. while (a_ptr->func) {
  1370. if ((inst & a_ptr->mask) == a_ptr->pattern) {
  1371. matched = 1;
  1372. break;
  1373. }
  1374. a_ptr++;
  1375. }
  1376. if (!matched) {
  1377. dbg_printf("\n\tUnknown ARM Instruction: %08x", inst);
  1378. addr->Offset += size;
  1379. }
  1380. else
  1381. {
  1382. if (!a_ptr->func(inst, addr))
  1383. addr->Offset += size;
  1384. }
  1385. return;
  1386. }
  1387. else
  1388. {
  1389. WORD *taddr = memory_to_linear_addr(addr);
  1390. tinst = db_get_inst( taddr, THUMB_INSN_SIZE );
  1391. switch (tinst & 0xf800)
  1392. {
  1393. case 0xe800:
  1394. case 0xf000:
  1395. case 0xf800:
  1396. size = THUMB2_INSN_SIZE;
  1397. taddr++;
  1398. inst = db_get_inst( taddr, THUMB_INSN_SIZE );
  1399. inst |= (tinst << 16);
  1400. while (t2_ptr->func) {
  1401. if ((inst & t2_ptr->mask) == t2_ptr->pattern) {
  1402. matched = 1;
  1403. break;
  1404. }
  1405. t2_ptr++;
  1406. }
  1407. if (!matched) {
  1408. dbg_printf("\n\tUnknown Thumb2 Instruction: %08x", inst);
  1409. addr->Offset += size;
  1410. }
  1411. else
  1412. {
  1413. if (!t2_ptr->func(inst, addr))
  1414. addr->Offset += size;
  1415. }
  1416. return;
  1417. default:
  1418. break;
  1419. }
  1420. size = THUMB_INSN_SIZE;
  1421. while (t_ptr->func) {
  1422. if ((tinst & t_ptr->mask) == t_ptr->pattern) {
  1423. matched = 1;
  1424. break;
  1425. }
  1426. t_ptr++;
  1427. }
  1428. if (!matched) {
  1429. dbg_printf("\n\tUnknown Thumb Instruction: %04x", tinst);
  1430. addr->Offset += size;
  1431. }
  1432. else
  1433. {
  1434. if (!t_ptr->func(tinst, addr))
  1435. addr->Offset += size;
  1436. }
  1437. return;
  1438. }
  1439. }
  1440. static BOOL be_arm_get_addr(HANDLE hThread, const dbg_ctx_t *ctx,
  1441. enum be_cpu_addr bca, ADDRESS64* addr)
  1442. {
  1443. switch (bca)
  1444. {
  1445. case be_cpu_addr_pc:
  1446. return be_cpu_build_addr(hThread, ctx, addr, 0, ctx->ctx.Pc);
  1447. case be_cpu_addr_stack:
  1448. return be_cpu_build_addr(hThread, ctx, addr, 0, ctx->ctx.Sp);
  1449. case be_cpu_addr_frame:
  1450. return be_cpu_build_addr(hThread, ctx, addr, 0, ctx->ctx.R11);
  1451. }
  1452. return FALSE;
  1453. }
  1454. static BOOL be_arm_get_register_info(int regno, enum be_cpu_addr* kind)
  1455. {
  1456. switch (regno)
  1457. {
  1458. case CV_ARM_PC: *kind = be_cpu_addr_pc; return TRUE;
  1459. case CV_ARM_R0 + 11: *kind = be_cpu_addr_frame; return TRUE;
  1460. case CV_ARM_SP: *kind = be_cpu_addr_stack; return TRUE;
  1461. }
  1462. return FALSE;
  1463. }
  1464. static void be_arm_single_step(dbg_ctx_t *ctx, BOOL enable)
  1465. {
  1466. }
  1467. static void be_arm_print_context(HANDLE hThread, const dbg_ctx_t *ctx, int all_regs)
  1468. {
  1469. static const char condflags[] = "NZCV";
  1470. int i;
  1471. char buf[8];
  1472. switch (ctx->ctx.Cpsr & 0x1F)
  1473. {
  1474. case 0: strcpy(buf, "User26"); break;
  1475. case 1: strcpy(buf, "FIQ26"); break;
  1476. case 2: strcpy(buf, "IRQ26"); break;
  1477. case 3: strcpy(buf, "SVC26"); break;
  1478. case 16: strcpy(buf, "User"); break;
  1479. case 17: strcpy(buf, "FIQ"); break;
  1480. case 18: strcpy(buf, "IRQ"); break;
  1481. case 19: strcpy(buf, "SVC"); break;
  1482. case 23: strcpy(buf, "ABT"); break;
  1483. case 27: strcpy(buf, "UND"); break;
  1484. default: strcpy(buf, "UNKNWN"); break;
  1485. }
  1486. dbg_printf("Register dump:\n");
  1487. dbg_printf("%s %s Mode\n", (ctx->ctx.Cpsr & 0x20) ? "Thumb" : "ARM", buf);
  1488. strcpy(buf, condflags);
  1489. for (i = 0; buf[i]; i++)
  1490. if (!((ctx->ctx.Cpsr >> 26) & (1 << (sizeof(condflags) - i))))
  1491. buf[i] = '-';
  1492. dbg_printf(" Pc:%08x Sp:%08x Lr:%08x Cpsr:%08x(%s)\n",
  1493. ctx->ctx.Pc, ctx->ctx.Sp, ctx->ctx.Lr, ctx->ctx.Cpsr, buf);
  1494. dbg_printf(" r0:%08x r1:%08x r2:%08x r3:%08x\n",
  1495. ctx->ctx.R0, ctx->ctx.R1, ctx->ctx.R2, ctx->ctx.R3);
  1496. dbg_printf(" r4:%08x r5:%08x r6:%08x r7:%08x\n",
  1497. ctx->ctx.R4, ctx->ctx.R5, ctx->ctx.R6, ctx->ctx.R7);
  1498. dbg_printf(" r8:%08x r9:%08x r10:%08x r11:%08x r12:%08x\n",
  1499. ctx->ctx.R8, ctx->ctx.R9, ctx->ctx.R10, ctx->ctx.R11, ctx->ctx.R12);
  1500. if (all_regs) dbg_printf( "Floating point ARM dump not implemented\n" );
  1501. }
  1502. static void be_arm_print_segment_info(HANDLE hThread, const dbg_ctx_t *ctx)
  1503. {
  1504. }
  1505. static struct dbg_internal_var be_arm_ctx[] =
  1506. {
  1507. {CV_ARM_R0 + 0, "r0", (void*)FIELD_OFFSET(CONTEXT, R0), dbg_itype_unsigned_int},
  1508. {CV_ARM_R0 + 1, "r1", (void*)FIELD_OFFSET(CONTEXT, R1), dbg_itype_unsigned_int},
  1509. {CV_ARM_R0 + 2, "r2", (void*)FIELD_OFFSET(CONTEXT, R2), dbg_itype_unsigned_int},
  1510. {CV_ARM_R0 + 3, "r3", (void*)FIELD_OFFSET(CONTEXT, R3), dbg_itype_unsigned_int},
  1511. {CV_ARM_R0 + 4, "r4", (void*)FIELD_OFFSET(CONTEXT, R4), dbg_itype_unsigned_int},
  1512. {CV_ARM_R0 + 5, "r5", (void*)FIELD_OFFSET(CONTEXT, R5), dbg_itype_unsigned_int},
  1513. {CV_ARM_R0 + 6, "r6", (void*)FIELD_OFFSET(CONTEXT, R6), dbg_itype_unsigned_int},
  1514. {CV_ARM_R0 + 7, "r7", (void*)FIELD_OFFSET(CONTEXT, R7), dbg_itype_unsigned_int},
  1515. {CV_ARM_R0 + 8, "r8", (void*)FIELD_OFFSET(CONTEXT, R8), dbg_itype_unsigned_int},
  1516. {CV_ARM_R0 + 9, "r9", (void*)FIELD_OFFSET(CONTEXT, R9), dbg_itype_unsigned_int},
  1517. {CV_ARM_R0 + 10, "r10", (void*)FIELD_OFFSET(CONTEXT, R10), dbg_itype_unsigned_int},
  1518. {CV_ARM_R0 + 11, "r11", (void*)FIELD_OFFSET(CONTEXT, R11), dbg_itype_unsigned_int},
  1519. {CV_ARM_R0 + 12, "r12", (void*)FIELD_OFFSET(CONTEXT, R12), dbg_itype_unsigned_int},
  1520. {CV_ARM_SP, "sp", (void*)FIELD_OFFSET(CONTEXT, Sp), dbg_itype_unsigned_int},
  1521. {CV_ARM_LR, "lr", (void*)FIELD_OFFSET(CONTEXT, Lr), dbg_itype_unsigned_int},
  1522. {CV_ARM_PC, "pc", (void*)FIELD_OFFSET(CONTEXT, Pc), dbg_itype_unsigned_int},
  1523. {CV_ARM_CPSR, "cpsr", (void*)FIELD_OFFSET(CONTEXT, Cpsr), dbg_itype_unsigned_int},
  1524. {0, NULL, 0, dbg_itype_none}
  1525. };
  1526. static BOOL be_arm_is_step_over_insn(const void* insn)
  1527. {
  1528. dbg_printf("be_arm_is_step_over_insn: not done\n");
  1529. return FALSE;
  1530. }
  1531. static BOOL be_arm_is_function_return(const void* insn)
  1532. {
  1533. dbg_printf("be_arm_is_function_return: not done\n");
  1534. return FALSE;
  1535. }
  1536. static BOOL be_arm_is_break_insn(const void* insn)
  1537. {
  1538. dbg_printf("be_arm_is_break_insn: not done\n");
  1539. return FALSE;
  1540. }
  1541. static BOOL be_arm_is_func_call(const void* insn, ADDRESS64* callee)
  1542. {
  1543. return FALSE;
  1544. }
  1545. static BOOL be_arm_is_jump(const void* insn, ADDRESS64* jumpee)
  1546. {
  1547. return FALSE;
  1548. }
  1549. static BOOL be_arm_insert_Xpoint(HANDLE hProcess, const struct be_process_io* pio,
  1550. dbg_ctx_t *ctx, enum be_xpoint_type type,
  1551. void* addr, unsigned *val, unsigned size)
  1552. {
  1553. SIZE_T sz;
  1554. switch (type)
  1555. {
  1556. case be_xpoint_break:
  1557. if (!size) return FALSE;
  1558. if (!pio->read(hProcess, addr, val, 4, &sz) || sz != 4) return FALSE;
  1559. default:
  1560. dbg_printf("Unknown/unsupported bp type %c\n", type);
  1561. return FALSE;
  1562. }
  1563. return TRUE;
  1564. }
  1565. static BOOL be_arm_remove_Xpoint(HANDLE hProcess, const struct be_process_io* pio,
  1566. dbg_ctx_t *ctx, enum be_xpoint_type type,
  1567. void* addr, unsigned val, unsigned size)
  1568. {
  1569. SIZE_T sz;
  1570. switch (type)
  1571. {
  1572. case be_xpoint_break:
  1573. if (!size) return FALSE;
  1574. if (!pio->write(hProcess, addr, &val, 4, &sz) || sz == 4) return FALSE;
  1575. break;
  1576. default:
  1577. dbg_printf("Unknown/unsupported bp type %c\n", type);
  1578. return FALSE;
  1579. }
  1580. return TRUE;
  1581. }
  1582. static BOOL be_arm_is_watchpoint_set(const dbg_ctx_t *ctx, unsigned idx)
  1583. {
  1584. dbg_printf("be_arm_is_watchpoint_set: not done\n");
  1585. return FALSE;
  1586. }
  1587. static void be_arm_clear_watchpoint(dbg_ctx_t *ctx, unsigned idx)
  1588. {
  1589. dbg_printf("be_arm_clear_watchpoint: not done\n");
  1590. }
  1591. static int be_arm_adjust_pc_for_break(dbg_ctx_t *ctx, BOOL way)
  1592. {
  1593. INT step = (ctx->ctx.Cpsr & 0x20) ? 2 : 4;
  1594. if (way)
  1595. {
  1596. ctx->ctx.Pc -= step;
  1597. return -step;
  1598. }
  1599. ctx->ctx.Pc += step;
  1600. return step;
  1601. }
  1602. static BOOL be_arm_get_context(HANDLE thread, dbg_ctx_t *ctx)
  1603. {
  1604. ctx->ctx.ContextFlags = CONTEXT_ALL;
  1605. return GetThreadContext(thread, &ctx->ctx);
  1606. }
  1607. static BOOL be_arm_set_context(HANDLE thread, const dbg_ctx_t *ctx)
  1608. {
  1609. return SetThreadContext(thread, &ctx->ctx);
  1610. }
  1611. #define REG(f,n,t,r) {f, n, t, FIELD_OFFSET(CONTEXT, r), sizeof(((CONTEXT*)NULL)->r)}
  1612. static struct gdb_register be_arm_gdb_register_map[] = {
  1613. REG("core", "r0", NULL, R0),
  1614. REG(NULL, "r1", NULL, R1),
  1615. REG(NULL, "r2", NULL, R2),
  1616. REG(NULL, "r3", NULL, R3),
  1617. REG(NULL, "r4", NULL, R4),
  1618. REG(NULL, "r5", NULL, R5),
  1619. REG(NULL, "r6", NULL, R6),
  1620. REG(NULL, "r7", NULL, R7),
  1621. REG(NULL, "r8", NULL, R8),
  1622. REG(NULL, "r9", NULL, R9),
  1623. REG(NULL, "r10", NULL, R10),
  1624. REG(NULL, "r11", NULL, R11),
  1625. REG(NULL, "r12", NULL, R12),
  1626. REG(NULL, "sp", "data_ptr", Sp),
  1627. REG(NULL, "lr", "code_ptr", Lr),
  1628. REG(NULL, "pc", "code_ptr", Pc),
  1629. REG(NULL, "cpsr", NULL, Cpsr),
  1630. };
  1631. struct backend_cpu be_arm =
  1632. {
  1633. IMAGE_FILE_MACHINE_ARMNT,
  1634. 4,
  1635. be_cpu_linearize,
  1636. be_cpu_build_addr,
  1637. be_arm_get_addr,
  1638. be_arm_get_register_info,
  1639. be_arm_single_step,
  1640. be_arm_print_context,
  1641. be_arm_print_segment_info,
  1642. be_arm_ctx,
  1643. be_arm_is_step_over_insn,
  1644. be_arm_is_function_return,
  1645. be_arm_is_break_insn,
  1646. be_arm_is_func_call,
  1647. be_arm_is_jump,
  1648. be_arm_disasm_one_insn,
  1649. be_arm_insert_Xpoint,
  1650. be_arm_remove_Xpoint,
  1651. be_arm_is_watchpoint_set,
  1652. be_arm_clear_watchpoint,
  1653. be_arm_adjust_pc_for_break,
  1654. be_arm_get_context,
  1655. be_arm_set_context,
  1656. be_arm_gdb_register_map,
  1657. ARRAY_SIZE(be_arm_gdb_register_map),
  1658. };
  1659. #endif