be_arm.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895
  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