thumbemu.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632
  1. /* thumbemu.c -- Thumb instruction emulation.
  2. Copyright (C) 1996, Cygnus Software Technologies Ltd.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>. */
  13. /* We can provide simple Thumb simulation by decoding the Thumb
  14. instruction into its corresponding ARM instruction, and using the
  15. existing ARM simulator. */
  16. /* This must come before any other includes. */
  17. #include "defs.h"
  18. #ifndef MODET /* required for the Thumb instruction support */
  19. #if 1
  20. #error "MODET needs to be defined for the Thumb world to work"
  21. #else
  22. #define MODET (1)
  23. #endif
  24. #endif
  25. #include "armdefs.h"
  26. #include "armemu.h"
  27. #include "armos.h"
  28. #define tBIT(n) ( (ARMword)(tinstr >> (n)) & 1)
  29. #define tBITS(m,n) ( (ARMword)(tinstr << (31 - (n))) >> ((31 - (n)) + (m)) )
  30. #define ntBIT(n) ( (ARMword)(next_instr >> (n)) & 1)
  31. #define ntBITS(m,n) ( (ARMword)(next_instr << (31 - (n))) >> ((31 - (n)) + (m)) )
  32. static int
  33. test_cond (int cond, ARMul_State * state)
  34. {
  35. switch (cond)
  36. {
  37. case EQ: return ZFLAG;
  38. case NE: return !ZFLAG;
  39. case VS: return VFLAG;
  40. case VC: return !VFLAG;
  41. case MI: return NFLAG;
  42. case PL: return !NFLAG;
  43. case CS: return CFLAG;
  44. case CC: return !CFLAG;
  45. case HI: return (CFLAG && !ZFLAG);
  46. case LS: return (!CFLAG || ZFLAG);
  47. case GE: return ((!NFLAG && !VFLAG) || (NFLAG && VFLAG));
  48. case LT: return ((NFLAG && !VFLAG) || (!NFLAG && VFLAG));
  49. case GT: return ((!NFLAG && !VFLAG && !ZFLAG)
  50. || (NFLAG && VFLAG && !ZFLAG));
  51. case LE: return ((NFLAG && !VFLAG) || (!NFLAG && VFLAG)) || ZFLAG;
  52. case AL: return TRUE;
  53. case NV:
  54. default: return FALSE;
  55. }
  56. }
  57. static ARMword skipping_32bit_thumb = 0;
  58. static int IT_block_cond = AL;
  59. static ARMword IT_block_mask = 0;
  60. static int IT_block_first = FALSE;
  61. static void
  62. handle_IT_block (ARMul_State * state,
  63. ARMword tinstr,
  64. tdstate * pvalid)
  65. {
  66. * pvalid = t_branch;
  67. IT_block_mask = tBITS (0, 3);
  68. if (IT_block_mask == 0)
  69. // NOP or a HINT.
  70. return;
  71. IT_block_cond = tBITS (4, 7);
  72. IT_block_first = TRUE;
  73. }
  74. static int
  75. in_IT_block (void)
  76. {
  77. return IT_block_mask != 0;
  78. }
  79. static int
  80. IT_block_allow (ARMul_State * state)
  81. {
  82. int cond;
  83. if (IT_block_mask == 0)
  84. return TRUE;
  85. cond = IT_block_cond;
  86. if (IT_block_first)
  87. IT_block_first = FALSE;
  88. else
  89. {
  90. if ((IT_block_mask & 8) == 0)
  91. cond &= 0xe;
  92. else
  93. cond |= 1;
  94. IT_block_mask <<= 1;
  95. IT_block_mask &= 0xF;
  96. }
  97. if (IT_block_mask == 0x8)
  98. IT_block_mask = 0;
  99. return test_cond (cond, state);
  100. }
  101. static ARMword
  102. ThumbExpandImm (ARMword tinstr)
  103. {
  104. ARMword val;
  105. if (tBITS (10, 11) == 0)
  106. {
  107. switch (tBITS (8, 9))
  108. {
  109. case 0: val = tBITS (0, 7); break;
  110. case 1: val = tBITS (0, 7) << 8; break;
  111. case 2: val = (tBITS (0, 7) << 8) | (tBITS (0, 7) << 24); break;
  112. case 3: val = tBITS (0, 7) * 0x01010101; break;
  113. default: val = 0;
  114. }
  115. }
  116. else
  117. {
  118. int ror = tBITS (7, 11);
  119. val = (1 << 7) | tBITS (0, 6);
  120. val = (val >> ror) | (val << (32 - ror));
  121. }
  122. return val;
  123. }
  124. #define tASSERT(truth) \
  125. do \
  126. { \
  127. if (! (truth)) \
  128. { \
  129. fprintf (stderr, "unhandled T2 insn %04x|%04x detected at thumbemu.c:%d\n", \
  130. tinstr, next_instr, __LINE__); \
  131. return ; \
  132. } \
  133. } \
  134. while (0)
  135. /* Attempt to emulate a 32-bit ARMv7 Thumb instruction.
  136. Stores t_branch into PVALUE upon success or t_undefined otherwise. */
  137. static void
  138. handle_T2_insn (ARMul_State * state,
  139. ARMword tinstr,
  140. ARMword next_instr,
  141. ARMword pc,
  142. ARMword * ainstr,
  143. tdstate * pvalid)
  144. {
  145. * pvalid = t_undefined;
  146. if (! state->is_v6)
  147. return;
  148. if (trace)
  149. fprintf (stderr, "|%04x ", next_instr);
  150. if (tBITS (11, 15) == 0x1E && ntBIT (15) == 1)
  151. {
  152. ARMsword simm32 = 0;
  153. int S = tBIT (10);
  154. * pvalid = t_branch;
  155. switch ((ntBIT (14) << 1) | ntBIT (12))
  156. {
  157. case 0: /* B<c>.W */
  158. {
  159. ARMword cond = tBITS (6, 9);
  160. ARMword imm6;
  161. ARMword imm11;
  162. ARMword J1;
  163. ARMword J2;
  164. tASSERT (cond != AL && cond != NV);
  165. if (! test_cond (cond, state))
  166. return;
  167. imm6 = tBITS (0, 5);
  168. imm11 = ntBITS (0, 10);
  169. J1 = ntBIT (13);
  170. J2 = ntBIT (11);
  171. simm32 = (J1 << 19) | (J2 << 18) | (imm6 << 12) | (imm11 << 1);
  172. if (S)
  173. simm32 |= -(1 << 20);
  174. break;
  175. }
  176. case 1: /* B.W */
  177. {
  178. ARMword imm10 = tBITS (0, 9);
  179. ARMword imm11 = ntBITS (0, 10);
  180. ARMword I1 = (ntBIT (13) ^ S) ? 0 : 1;
  181. ARMword I2 = (ntBIT (11) ^ S) ? 0 : 1;
  182. simm32 = (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
  183. if (S)
  184. simm32 |= -(1 << 24);
  185. break;
  186. }
  187. case 2: /* BLX <label> */
  188. {
  189. ARMword imm10h = tBITS (0, 9);
  190. ARMword imm10l = ntBITS (1, 10);
  191. ARMword I1 = (ntBIT (13) ^ S) ? 0 : 1;
  192. ARMword I2 = (ntBIT (11) ^ S) ? 0 : 1;
  193. simm32 = (I1 << 23) | (I2 << 22) | (imm10h << 12) | (imm10l << 2);
  194. if (S)
  195. simm32 |= -(1 << 24);
  196. CLEART;
  197. state->Reg[14] = (pc + 4) | 1;
  198. break;
  199. }
  200. case 3: /* BL <label> */
  201. {
  202. ARMword imm10 = tBITS (0, 9);
  203. ARMword imm11 = ntBITS (0, 10);
  204. ARMword I1 = (ntBIT (13) ^ S) ? 0 : 1;
  205. ARMword I2 = (ntBIT (11) ^ S) ? 0 : 1;
  206. simm32 = (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1);
  207. if (S)
  208. simm32 |= -(1 << 24);
  209. state->Reg[14] = (pc + 4) | 1;
  210. break;
  211. }
  212. }
  213. state->Reg[15] = (pc + 4 + simm32);
  214. FLUSHPIPE;
  215. if (trace_funcs)
  216. fprintf (stderr, " pc changed to %x\n", state->Reg[15]);
  217. return;
  218. }
  219. switch (tBITS (5,12))
  220. {
  221. case 0x29: // TST<c>.W <Rn>,<Rm>{,<shift>}
  222. {
  223. ARMword Rn = tBITS (0, 3);
  224. ARMword Rm = ntBITS (0, 3);
  225. ARMword type = ntBITS (4, 5);
  226. ARMword imm5 = (ntBITS (12, 14) << 2) | ntBITS (6, 7);
  227. tASSERT (ntBITS (8, 11) == 0xF);
  228. * ainstr = 0xE1100000;
  229. * ainstr |= (Rn << 16);
  230. * ainstr |= (Rm);
  231. * ainstr |= (type << 5);
  232. * ainstr |= (imm5 << 7);
  233. * pvalid = t_decoded;
  234. break;
  235. }
  236. case 0x46:
  237. if (tBIT (4) && ntBITS (5, 15) == 0x780)
  238. {
  239. // Table Branch
  240. ARMword Rn = tBITS (0, 3);
  241. ARMword Rm = ntBITS (0, 3);
  242. ARMword address, dest;
  243. if (ntBIT (4))
  244. {
  245. // TBH
  246. address = state->Reg[Rn] + state->Reg[Rm] * 2;
  247. dest = ARMul_LoadHalfWord (state, address);
  248. }
  249. else
  250. {
  251. // TBB
  252. address = state->Reg[Rn] + state->Reg[Rm];
  253. dest = ARMul_LoadByte (state, address);
  254. }
  255. state->Reg[15] = (pc + 4 + dest * 2);
  256. FLUSHPIPE;
  257. * pvalid = t_branch;
  258. break;
  259. }
  260. /* Fall through. */
  261. case 0x42:
  262. case 0x43:
  263. case 0x47:
  264. case 0x4A:
  265. case 0x4B:
  266. case 0x4E: // STRD
  267. case 0x4F: // LDRD
  268. {
  269. ARMword Rn = tBITS (0, 3);
  270. ARMword Rt = ntBITS (12, 15);
  271. ARMword Rt2 = ntBITS (8, 11);
  272. ARMword imm8 = ntBITS (0, 7);
  273. ARMword P = tBIT (8);
  274. ARMword U = tBIT (7);
  275. ARMword W = tBIT (5);
  276. tASSERT (Rt2 == Rt + 1);
  277. imm8 <<= 2;
  278. tASSERT (imm8 <= 255);
  279. tASSERT (P != 0 || W != 0);
  280. // Convert into an ARM A1 encoding.
  281. if (Rn == 15)
  282. {
  283. tASSERT (tBIT (4) == 1);
  284. // LDRD (literal)
  285. // Ignore W even if 1.
  286. * ainstr = 0xE14F00D0;
  287. }
  288. else
  289. {
  290. if (tBIT (4) == 1)
  291. // LDRD (immediate)
  292. * ainstr = 0xE04000D0;
  293. else
  294. {
  295. // STRD<c> <Rt>,<Rt2>,[<Rn>{,#+/-<imm8>}]
  296. // STRD<c> <Rt>,<Rt2>,[<Rn>],#+/-<imm8>
  297. // STRD<c> <Rt>,<Rt2>,[<Rn>,#+/-<imm8>]!
  298. * ainstr = 0xE04000F0;
  299. }
  300. * ainstr |= (Rn << 16);
  301. * ainstr |= (P << 24);
  302. * ainstr |= (W << 21);
  303. }
  304. * ainstr |= (U << 23);
  305. * ainstr |= (Rt << 12);
  306. * ainstr |= ((imm8 << 4) & 0xF00);
  307. * ainstr |= (imm8 & 0xF);
  308. * pvalid = t_decoded;
  309. break;
  310. }
  311. case 0x44:
  312. case 0x45: // LDMIA
  313. {
  314. ARMword Rn = tBITS (0, 3);
  315. int W = tBIT (5);
  316. ARMword list = (ntBIT (15) << 15) | (ntBIT (14) << 14) | ntBITS (0, 12);
  317. if (Rn == 13)
  318. * ainstr = 0xE8BD0000;
  319. else
  320. {
  321. * ainstr = 0xE8900000;
  322. * ainstr |= (W << 21);
  323. * ainstr |= (Rn << 16);
  324. }
  325. * ainstr |= list;
  326. * pvalid = t_decoded;
  327. break;
  328. }
  329. case 0x48:
  330. case 0x49: // STMDB
  331. {
  332. ARMword Rn = tBITS (0, 3);
  333. int W = tBIT (5);
  334. ARMword list = (ntBIT (14) << 14) | ntBITS (0, 12);
  335. if (Rn == 13 && W)
  336. * ainstr = 0xE92D0000;
  337. else
  338. {
  339. * ainstr = 0xE9000000;
  340. * ainstr |= (W << 21);
  341. * ainstr |= (Rn << 16);
  342. }
  343. * ainstr |= list;
  344. * pvalid = t_decoded;
  345. break;
  346. }
  347. case 0x50:
  348. {
  349. ARMword Rd = ntBITS (8, 11);
  350. ARMword Rn = tBITS (0, 3);
  351. ARMword Rm = ntBITS (0, 3);
  352. ARMword imm5 = (ntBITS (12, 14) << 2) | ntBITS (6, 7);
  353. ARMword type = ntBITS (4, 5);
  354. tASSERT (ntBIT (15) == 0);
  355. if (Rd == 15)
  356. {
  357. tASSERT (tBIT (4) == 1);
  358. // TST<c>.W <Rn>,<Rm>{,<shift>}
  359. * ainstr = 0xE1100000;
  360. }
  361. else
  362. {
  363. // AND{S}<c>.W <Rd>,<Rn>,<Rm>{,<shift>}
  364. int S = tBIT (4);
  365. * ainstr = 0xE0000000;
  366. if (in_IT_block ())
  367. S = 0;
  368. * ainstr |= (S << 20);
  369. }
  370. * ainstr |= (Rn << 16);
  371. * ainstr |= (imm5 << 7);
  372. * ainstr |= (type << 5);
  373. * ainstr |= (Rm << 0);
  374. * pvalid = t_decoded;
  375. break;
  376. }
  377. case 0x51: // BIC{S}<c>.W <Rd>,<Rn>,<Rm>{,<shift>}
  378. {
  379. ARMword Rn = tBITS (0, 3);
  380. ARMword S = tBIT(4);
  381. ARMword Rm = ntBITS (0, 3);
  382. ARMword Rd = ntBITS (8, 11);
  383. ARMword imm5 = (ntBITS (12, 14) << 2) | ntBITS (6, 7);
  384. ARMword type = ntBITS (4, 5);
  385. tASSERT (ntBIT (15) == 0);
  386. * ainstr = 0xE1C00000;
  387. * ainstr |= (S << 20);
  388. * ainstr |= (Rn << 16);
  389. * ainstr |= (Rd << 12);
  390. * ainstr |= (imm5 << 7);
  391. * ainstr |= (type << 5);
  392. * ainstr |= (Rm << 0);
  393. * pvalid = t_decoded;
  394. break;
  395. }
  396. case 0x52:
  397. {
  398. ARMword Rn = tBITS (0, 3);
  399. ARMword Rd = ntBITS (8, 11);
  400. ARMword Rm = ntBITS (0, 3);
  401. int S = tBIT (4);
  402. ARMword imm5 = (ntBITS (12, 14) << 2) | ntBITS (6, 7);
  403. ARMword type = ntBITS (4, 5);
  404. tASSERT (Rd != 15);
  405. if (in_IT_block ())
  406. S = 0;
  407. if (Rn == 15)
  408. {
  409. tASSERT (ntBIT (15) == 0);
  410. switch (ntBITS (4, 5))
  411. {
  412. case 0:
  413. // LSL{S}<c>.W <Rd>,<Rm>,#<imm5>
  414. * ainstr = 0xE1A00000;
  415. break;
  416. case 1:
  417. // LSR{S}<c>.W <Rd>,<Rm>,#<imm>
  418. * ainstr = 0xE1A00020;
  419. break;
  420. case 2:
  421. // ASR{S}<c>.W <Rd>,<Rm>,#<imm>
  422. * ainstr = 0xE1A00040;
  423. break;
  424. case 3:
  425. // ROR{S}<c> <Rd>,<Rm>,#<imm>
  426. * ainstr = 0xE1A00060;
  427. break;
  428. default:
  429. tASSERT (0);
  430. * ainstr = 0;
  431. }
  432. }
  433. else
  434. {
  435. // ORR{S}<c>.W <Rd>,<Rn>,<Rm>{,<shift>}
  436. * ainstr = 0xE1800000;
  437. * ainstr |= (Rn << 16);
  438. * ainstr |= (type << 5);
  439. }
  440. * ainstr |= (Rd << 12);
  441. * ainstr |= (S << 20);
  442. * ainstr |= (imm5 << 7);
  443. * ainstr |= (Rm << 0);
  444. * pvalid = t_decoded;
  445. break;
  446. }
  447. case 0x53: // MVN{S}<c>.W <Rd>,<Rm>{,<shift>}
  448. {
  449. ARMword Rd = ntBITS (8, 11);
  450. ARMword Rm = ntBITS (0, 3);
  451. int S = tBIT (4);
  452. ARMword imm5 = (ntBITS (12, 14) << 2) | ntBITS (6, 7);
  453. ARMword type = ntBITS (4, 5);
  454. tASSERT (ntBIT (15) == 0);
  455. if (in_IT_block ())
  456. S = 0;
  457. * ainstr = 0xE1E00000;
  458. * ainstr |= (S << 20);
  459. * ainstr |= (Rd << 12);
  460. * ainstr |= (imm5 << 7);
  461. * ainstr |= (type << 5);
  462. * ainstr |= (Rm << 0);
  463. * pvalid = t_decoded;
  464. break;
  465. }
  466. case 0x54:
  467. {
  468. ARMword Rn = tBITS (0, 3);
  469. ARMword Rd = ntBITS (8, 11);
  470. ARMword Rm = ntBITS (0, 3);
  471. int S = tBIT (4);
  472. ARMword imm5 = (ntBITS (12, 14) << 2) | ntBITS (6, 7);
  473. ARMword type = ntBITS (4, 5);
  474. if (Rd == 15 && S)
  475. {
  476. // TEQ<c> <Rn>,<Rm>{,<shift>}
  477. tASSERT (ntBIT (15) == 0);
  478. * ainstr = 0xE1300000;
  479. }
  480. else
  481. {
  482. // EOR{S}<c>.W <Rd>,<Rn>,<Rm>{,<shift>}
  483. if (in_IT_block ())
  484. S = 0;
  485. * ainstr = 0xE0200000;
  486. * ainstr |= (S << 20);
  487. * ainstr |= (Rd << 8);
  488. }
  489. * ainstr |= (Rn << 16);
  490. * ainstr |= (imm5 << 7);
  491. * ainstr |= (type << 5);
  492. * ainstr |= (Rm << 0);
  493. * pvalid = t_decoded;
  494. break;
  495. }
  496. case 0x58: // ADD{S}<c>.W <Rd>,<Rn>,<Rm>{,<shift>}
  497. {
  498. ARMword Rn = tBITS (0, 3);
  499. ARMword Rd = ntBITS (8, 11);
  500. ARMword Rm = ntBITS (0, 3);
  501. int S = tBIT (4);
  502. ARMword imm5 = (ntBITS (12, 14) << 2) | ntBITS (6, 7);
  503. ARMword type = ntBITS (4, 5);
  504. tASSERT (! (Rd == 15 && S));
  505. if (in_IT_block ())
  506. S = 0;
  507. * ainstr = 0xE0800000;
  508. * ainstr |= (S << 20);
  509. * ainstr |= (Rn << 16);
  510. * ainstr |= (Rd << 12);
  511. * ainstr |= (imm5 << 7);
  512. * ainstr |= (type << 5);
  513. * ainstr |= Rm;
  514. * pvalid = t_decoded;
  515. break;
  516. }
  517. case 0x5A: // ADC{S}<c>.W <Rd>,<Rn>,<Rm>{,<shift>}
  518. tASSERT (ntBIT (15) == 0);
  519. * ainstr = 0xE0A00000;
  520. if (! in_IT_block ())
  521. * ainstr |= (tBIT (4) << 20); // S
  522. * ainstr |= (tBITS (0, 3) << 16); // Rn
  523. * ainstr |= (ntBITS (8, 11) << 12); // Rd
  524. * ainstr |= ((ntBITS (12, 14) << 2) | ntBITS (6, 7)) << 7; // imm5
  525. * ainstr |= (ntBITS (4, 5) << 5); // type
  526. * ainstr |= ntBITS (0, 3); // Rm
  527. * pvalid = t_decoded;
  528. break;
  529. case 0x5B: // SBC{S}<c>.W <Rd>,<Rn>,<Rm>{,<shift>}
  530. {
  531. ARMword Rn = tBITS (0, 3);
  532. ARMword Rd = ntBITS (8, 11);
  533. ARMword Rm = ntBITS (0, 3);
  534. int S = tBIT (4);
  535. ARMword imm5 = (ntBITS (12, 14) << 2) | ntBITS (6, 7);
  536. ARMword type = ntBITS (4, 5);
  537. tASSERT (ntBIT (15) == 0);
  538. if (in_IT_block ())
  539. S = 0;
  540. * ainstr = 0xE0C00000;
  541. * ainstr |= (S << 20);
  542. * ainstr |= (Rn << 16);
  543. * ainstr |= (Rd << 12);
  544. * ainstr |= (imm5 << 7);
  545. * ainstr |= (type << 5);
  546. * ainstr |= Rm;
  547. * pvalid = t_decoded;
  548. break;
  549. }
  550. case 0x5E: // RSB{S}<c> <Rd>,<Rn>,<Rm>{,<shift>}
  551. case 0x5D: // SUB{S}<c>.W <Rd>,<Rn>,<Rm>{,<shift>}
  552. {
  553. ARMword Rn = tBITS (0, 3);
  554. ARMword Rd = ntBITS (8, 11);
  555. ARMword Rm = ntBITS (0, 3);
  556. ARMword S = tBIT (4);
  557. ARMword type = ntBITS (4, 5);
  558. ARMword imm5 = (ntBITS (12, 14) << 2) | ntBITS (6, 7);
  559. tASSERT (ntBIT(15) == 0);
  560. if (Rd == 15)
  561. {
  562. // CMP<c>.W <Rn>, <Rm> {,<shift>}
  563. * ainstr = 0xE1500000;
  564. Rd = 0;
  565. }
  566. else if (tBIT (5))
  567. * ainstr = 0xE0400000;
  568. else
  569. * ainstr = 0xE0600000;
  570. * ainstr |= (S << 20);
  571. * ainstr |= (Rn << 16);
  572. * ainstr |= (Rd << 12);
  573. * ainstr |= (imm5 << 7);
  574. * ainstr |= (type << 5);
  575. * ainstr |= (Rm << 0);
  576. * pvalid = t_decoded;
  577. break;
  578. }
  579. case 0x9D: // NOP.W
  580. tASSERT (tBITS (0, 15) == 0xF3AF);
  581. tASSERT (ntBITS (0, 15) == 0x8000);
  582. * pvalid = t_branch;
  583. break;
  584. case 0x80: // AND
  585. case 0xA0: // TST
  586. {
  587. ARMword Rn = tBITS (0, 3);
  588. ARMword imm12 = (tBIT(10) << 11) | (ntBITS (12, 14) << 8) | ntBITS (0, 7);
  589. ARMword Rd = ntBITS (8, 11);
  590. ARMword val;
  591. int S = tBIT (4);
  592. imm12 = ThumbExpandImm (imm12);
  593. val = state->Reg[Rn] & imm12;
  594. if (Rd == 15)
  595. {
  596. // TST<c> <Rn>,#<const>
  597. tASSERT (S == 1);
  598. }
  599. else
  600. {
  601. // AND{S}<c> <Rd>,<Rn>,#<const>
  602. if (in_IT_block ())
  603. S = 0;
  604. state->Reg[Rd] = val;
  605. }
  606. if (S)
  607. ARMul_NegZero (state, val);
  608. * pvalid = t_branch;
  609. break;
  610. }
  611. case 0xA1:
  612. case 0x81: // BIC.W
  613. {
  614. ARMword Rn = tBITS (0, 3);
  615. ARMword Rd = ntBITS (8, 11);
  616. ARMword S = tBIT (4);
  617. ARMword imm8 = (ntBITS (12, 14) << 8) | ntBITS (0, 7);
  618. tASSERT (ntBIT (15) == 0);
  619. imm8 = ThumbExpandImm (imm8);
  620. state->Reg[Rd] = state->Reg[Rn] & ~ imm8;
  621. if (S && ! in_IT_block ())
  622. ARMul_NegZero (state, state->Reg[Rd]);
  623. * pvalid = t_resolved;
  624. break;
  625. }
  626. case 0xA2:
  627. case 0x82: // MOV{S}<c>.W <Rd>,#<const>
  628. {
  629. ARMword val = (tBIT(10) << 11) | (ntBITS (12, 14) << 8) | ntBITS (0, 7);
  630. ARMword Rd = ntBITS (8, 11);
  631. val = ThumbExpandImm (val);
  632. state->Reg[Rd] = val;
  633. if (tBIT (4) && ! in_IT_block ())
  634. ARMul_NegZero (state, val);
  635. /* Indicate that the instruction has been processed. */
  636. * pvalid = t_branch;
  637. break;
  638. }
  639. case 0xA3:
  640. case 0x83: // MVN{S}<c> <Rd>,#<const>
  641. {
  642. ARMword val = (tBIT(10) << 11) | (ntBITS (12, 14) << 8) | ntBITS (0, 7);
  643. ARMword Rd = ntBITS (8, 11);
  644. val = ThumbExpandImm (val);
  645. val = ~ val;
  646. state->Reg[Rd] = val;
  647. if (tBIT (4) && ! in_IT_block ())
  648. ARMul_NegZero (state, val);
  649. * pvalid = t_resolved;
  650. break;
  651. }
  652. case 0xA4: // EOR
  653. case 0x84: // TEQ
  654. {
  655. ARMword Rn = tBITS (0, 3);
  656. ARMword Rd = ntBITS (8, 11);
  657. ARMword S = tBIT (4);
  658. ARMword imm12 = ((tBIT (10) << 11) | (ntBITS (12, 14) << 8) | ntBITS (0, 7));
  659. ARMword result;
  660. imm12 = ThumbExpandImm (imm12);
  661. result = state->Reg[Rn] ^ imm12;
  662. if (Rd == 15 && S)
  663. // TEQ<c> <Rn>,#<const>
  664. ;
  665. else
  666. {
  667. // EOR{S}<c> <Rd>,<Rn>,#<const>
  668. state->Reg[Rd] = result;
  669. if (in_IT_block ())
  670. S = 0;
  671. }
  672. if (S)
  673. ARMul_NegZero (state, result);
  674. * pvalid = t_resolved;
  675. break;
  676. }
  677. case 0xA8: // CMN
  678. case 0x88: // ADD
  679. {
  680. ARMword Rd = ntBITS (8, 11);
  681. int S = tBIT (4);
  682. ARMword Rn = tBITS (0, 3);
  683. ARMword lhs = state->Reg[Rn];
  684. ARMword imm12 = (tBIT (10) << 11) | (ntBITS (12, 14) << 8) | ntBITS (0, 7);
  685. ARMword rhs = ThumbExpandImm (imm12);
  686. ARMword res = lhs + rhs;
  687. if (Rd == 15 && S)
  688. {
  689. // CMN<c> <Rn>,#<const>
  690. res = lhs - rhs;
  691. }
  692. else
  693. {
  694. // ADD{S}<c>.W <Rd>,<Rn>,#<const>
  695. res = lhs + rhs;
  696. if (in_IT_block ())
  697. S = 0;
  698. state->Reg[Rd] = res;
  699. }
  700. if (S)
  701. {
  702. ARMul_NegZero (state, res);
  703. if ((lhs | rhs) >> 30)
  704. {
  705. /* Possible C,V,N to set. */
  706. ARMul_AddCarry (state, lhs, rhs, res);
  707. ARMul_AddOverflow (state, lhs, rhs, res);
  708. }
  709. else
  710. {
  711. CLEARC;
  712. CLEARV;
  713. }
  714. }
  715. * pvalid = t_branch;
  716. break;
  717. }
  718. case 0xAA:
  719. case 0x8A: // ADC{S}<c> <Rd>,<Rn>,#<const>
  720. {
  721. ARMword Rn = tBITS (0, 3);
  722. ARMword Rd = ntBITS (8, 11);
  723. int S = tBIT (4);
  724. ARMword imm12 = (tBIT (10) << 11) | (ntBITS (12, 14) << 8) | ntBITS (0, 7);
  725. ARMword lhs = state->Reg[Rn];
  726. ARMword rhs = ThumbExpandImm (imm12);
  727. ARMword res;
  728. tASSERT (ntBIT (15) == 0);
  729. if (CFLAG)
  730. rhs += 1;
  731. res = lhs + rhs;
  732. state->Reg[Rd] = res;
  733. if (in_IT_block ())
  734. S = 0;
  735. if (S)
  736. {
  737. ARMul_NegZero (state, res);
  738. if ((lhs >= rhs) || ((rhs | lhs) >> 31))
  739. {
  740. ARMul_AddCarry (state, lhs, rhs, res);
  741. ARMul_AddOverflow (state, lhs, rhs, res);
  742. }
  743. else
  744. {
  745. CLEARC;
  746. CLEARV;
  747. }
  748. }
  749. * pvalid = t_branch;
  750. break;
  751. }
  752. case 0xAB:
  753. case 0x8B: // SBC{S}<c> <Rd>,<Rn>,#<const>
  754. {
  755. ARMword Rn = tBITS (0, 3);
  756. ARMword Rd = ntBITS (8, 11);
  757. int S = tBIT (4);
  758. ARMword imm12 = (tBIT (10) << 11) | (ntBITS (12, 14) << 8) | ntBITS (0, 7);
  759. ARMword lhs = state->Reg[Rn];
  760. ARMword rhs = ThumbExpandImm (imm12);
  761. ARMword res;
  762. tASSERT (ntBIT (15) == 0);
  763. if (! CFLAG)
  764. rhs += 1;
  765. res = lhs - rhs;
  766. state->Reg[Rd] = res;
  767. if (in_IT_block ())
  768. S = 0;
  769. if (S)
  770. {
  771. ARMul_NegZero (state, res);
  772. if ((lhs >= rhs) || ((rhs | lhs) >> 31))
  773. {
  774. ARMul_SubCarry (state, lhs, rhs, res);
  775. ARMul_SubOverflow (state, lhs, rhs, res);
  776. }
  777. else
  778. {
  779. CLEARC;
  780. CLEARV;
  781. }
  782. }
  783. * pvalid = t_branch;
  784. break;
  785. }
  786. case 0xAD:
  787. case 0x8D: // SUB
  788. {
  789. ARMword Rn = tBITS (0, 3);
  790. ARMword Rd = ntBITS (8, 11);
  791. int S = tBIT (4);
  792. ARMword imm12 = (tBIT (10) << 11) | (ntBITS (12, 14) << 8) | ntBITS (0, 7);
  793. ARMword lhs = state->Reg[Rn];
  794. ARMword rhs = ThumbExpandImm (imm12);
  795. ARMword res = lhs - rhs;
  796. if (Rd == 15 && S)
  797. {
  798. // CMP<c>.W <Rn>,#<const>
  799. tASSERT (S);
  800. }
  801. else
  802. {
  803. // SUB{S}<c>.W <Rd>,<Rn>,#<const>
  804. if (in_IT_block ())
  805. S = 0;
  806. state->Reg[Rd] = res;
  807. }
  808. if (S)
  809. {
  810. ARMul_NegZero (state, res);
  811. if ((lhs >= rhs) || ((rhs | lhs) >> 31))
  812. {
  813. ARMul_SubCarry (state, lhs, rhs, res);
  814. ARMul_SubOverflow (state, lhs, rhs, res);
  815. }
  816. else
  817. {
  818. CLEARC;
  819. CLEARV;
  820. }
  821. }
  822. * pvalid = t_branch;
  823. break;
  824. }
  825. case 0xAE:
  826. case 0x8E: // RSB{S}<c>.W <Rd>,<Rn>,#<const>
  827. {
  828. ARMword Rn = tBITS (0, 3);
  829. ARMword Rd = ntBITS (8, 11);
  830. ARMword imm12 = (tBIT (10) << 11) | (ntBITS (12, 14) << 8) | ntBITS (0, 7);
  831. int S = tBIT (4);
  832. ARMword lhs = imm12;
  833. ARMword rhs = state->Reg[Rn];
  834. ARMword res = lhs - rhs;
  835. tASSERT (ntBIT (15) == 0);
  836. state->Reg[Rd] = res;
  837. if (S)
  838. {
  839. ARMul_NegZero (state, res);
  840. if ((lhs >= rhs) || ((rhs | lhs) >> 31))
  841. {
  842. ARMul_SubCarry (state, lhs, rhs, res);
  843. ARMul_SubOverflow (state, lhs, rhs, res);
  844. }
  845. else
  846. {
  847. CLEARC;
  848. CLEARV;
  849. }
  850. }
  851. * pvalid = t_branch;
  852. break;
  853. }
  854. case 0xB0:
  855. case 0x90: // ADDW<c> <Rd>,<Rn>,#<imm12>
  856. {
  857. ARMword Rn = tBITS (0, 3);
  858. ARMword Rd = ntBITS (8, 11);
  859. ARMword imm12 = (tBIT (10) << 11) | (ntBITS (12, 14) << 8) | ntBITS (0, 7);
  860. tASSERT (tBIT (4) == 0);
  861. tASSERT (ntBIT (15) == 0);
  862. state->Reg[Rd] = state->Reg[Rn] + imm12;
  863. * pvalid = t_branch;
  864. break;
  865. }
  866. case 0xB2:
  867. case 0x92: // MOVW<c> <Rd>,#<imm16>
  868. {
  869. ARMword Rd = ntBITS (8, 11);
  870. ARMword imm = (tBITS (0, 3) << 12) | (tBIT (10) << 11) | (ntBITS (12, 14) << 8) | ntBITS (0, 7);
  871. state->Reg[Rd] = imm;
  872. /* Indicate that the instruction has been processed. */
  873. * pvalid = t_branch;
  874. break;
  875. }
  876. case 0xb5:
  877. case 0x95:// SUBW<c> <Rd>,<Rn>,#<imm12>
  878. {
  879. ARMword Rd = ntBITS (8, 11);
  880. ARMword Rn = tBITS (0, 3);
  881. ARMword imm12 = (tBIT (10) << 11) | (ntBITS (12, 14) << 8) | ntBITS (0, 7);
  882. tASSERT (tBIT (4) == 0);
  883. tASSERT (ntBIT (15) == 0);
  884. /* Note the ARM ARM indicates special cases for Rn == 15 (ADR)
  885. and Rn == 13 (SUB SP minus immediate), but these are implemented
  886. in exactly the same way as the normal SUBW insn. */
  887. state->Reg[Rd] = state->Reg[Rn] - imm12;
  888. * pvalid = t_resolved;
  889. break;
  890. }
  891. case 0xB6:
  892. case 0x96: // MOVT<c> <Rd>,#<imm16>
  893. {
  894. ARMword Rd = ntBITS (8, 11);
  895. ARMword imm = (tBITS (0, 3) << 12) | (tBIT (10) << 11) | (ntBITS (12, 14) << 8) | ntBITS (0, 7);
  896. state->Reg[Rd] &= 0xFFFF;
  897. state->Reg[Rd] |= (imm << 16);
  898. * pvalid = t_resolved;
  899. break;
  900. }
  901. case 0x9A: // SBFXc> <Rd>,<Rn>,#<lsb>,#<width>
  902. tASSERT (tBIT (4) == 0);
  903. tASSERT (ntBIT (15) == 0);
  904. tASSERT (ntBIT (5) == 0);
  905. * ainstr = 0xE7A00050;
  906. * ainstr |= (ntBITS (0, 4) << 16); // widthm1
  907. * ainstr |= (ntBITS (8, 11) << 12); // Rd
  908. * ainstr |= (((ntBITS (12, 14) << 2) | ntBITS (6, 7)) << 7); // lsb
  909. * ainstr |= tBITS (0, 3); // Rn
  910. * pvalid = t_decoded;
  911. break;
  912. case 0x9B:
  913. {
  914. ARMword Rd = ntBITS (8, 11);
  915. ARMword Rn = tBITS (0, 3);
  916. ARMword msbit = ntBITS (0, 5);
  917. ARMword lsbit = (ntBITS (12, 14) << 2) | ntBITS (6, 7);
  918. ARMword mask = -(1 << lsbit);
  919. tASSERT (tBIT (4) == 0);
  920. tASSERT (ntBIT (15) == 0);
  921. tASSERT (ntBIT (5) == 0);
  922. mask &= ((1 << (msbit + 1)) - 1);
  923. if (lsbit > msbit)
  924. ; // UNPREDICTABLE
  925. else if (Rn == 15)
  926. {
  927. // BFC<c> <Rd>,#<lsb>,#<width>
  928. state->Reg[Rd] &= ~ mask;
  929. }
  930. else
  931. {
  932. // BFI<c> <Rd>,<Rn>,#<lsb>,#<width>
  933. ARMword val = state->Reg[Rn] & (mask >> lsbit);
  934. val <<= lsbit;
  935. state->Reg[Rd] &= ~ mask;
  936. state->Reg[Rd] |= val;
  937. }
  938. * pvalid = t_resolved;
  939. break;
  940. }
  941. case 0x9E: // UBFXc> <Rd>,<Rn>,#<lsb>,#<width>
  942. tASSERT (tBIT (4) == 0);
  943. tASSERT (ntBIT (15) == 0);
  944. tASSERT (ntBIT (5) == 0);
  945. * ainstr = 0xE7E00050;
  946. * ainstr |= (ntBITS (0, 4) << 16); // widthm1
  947. * ainstr |= (ntBITS (8, 11) << 12); // Rd
  948. * ainstr |= (((ntBITS (12, 14) << 2) | ntBITS (6, 7)) << 7); // lsb
  949. * ainstr |= tBITS (0, 3); // Rn
  950. * pvalid = t_decoded;
  951. break;
  952. case 0xC0: // STRB
  953. case 0xC4: // LDRB
  954. {
  955. ARMword Rn = tBITS (0, 3);
  956. ARMword Rt = ntBITS (12, 15);
  957. if (tBIT (4))
  958. {
  959. if (Rn == 15)
  960. {
  961. tASSERT (Rt != 15);
  962. /* LDRB<c> <Rt>,<label> => 1111 1000 U001 1111 */
  963. * ainstr = 0xE55F0000;
  964. * ainstr |= (tBIT (7) << 23);
  965. * ainstr |= ntBITS (0, 11);
  966. }
  967. else if (tBIT (7))
  968. {
  969. /* LDRB<c>.W <Rt>,[<Rn>{,#<imm12>}] => 1111 1000 1001 rrrr */
  970. * ainstr = 0xE5D00000;
  971. * ainstr |= ntBITS (0, 11);
  972. }
  973. else if (ntBIT (11) == 0)
  974. {
  975. /* LDRB<c>.W <Rt>,[<Rn>,<Rm>{,LSL #<imm2>}] => 1111 1000 0001 rrrr */
  976. * ainstr = 0xE7D00000;
  977. * ainstr |= (ntBITS (4, 5) << 7);
  978. * ainstr |= ntBITS (0, 3);
  979. }
  980. else
  981. {
  982. int P = ntBIT (10);
  983. int U = ntBIT (9);
  984. int W = ntBIT (8);
  985. tASSERT (! (Rt == 15 && P && !U && !W));
  986. tASSERT (! (P && U && !W));
  987. /* LDRB<c> <Rt>,[<Rn>,#-<imm8>] => 1111 1000 0001 rrrr
  988. LDRB<c> <Rt>,[<Rn>],#+/-<imm8> => 1111 1000 0001 rrrr
  989. LDRB<c> <Rt>,[<Rn>,#+/-<imm8>]! => 1111 1000 0001 rrrr */
  990. * ainstr = 0xE4500000;
  991. * ainstr |= (P << 24);
  992. * ainstr |= (U << 23);
  993. * ainstr |= (W << 21);
  994. * ainstr |= ntBITS (0, 7);
  995. }
  996. }
  997. else
  998. {
  999. if (tBIT (7) == 1)
  1000. {
  1001. // STRB<c>.W <Rt>,[<Rn>,#<imm12>]
  1002. ARMword imm12 = ntBITS (0, 11);
  1003. ARMul_StoreByte (state, state->Reg[Rn] + imm12, state->Reg [Rt]);
  1004. * pvalid = t_branch;
  1005. break;
  1006. }
  1007. else if (ntBIT (11))
  1008. {
  1009. // STRB<c> <Rt>,[<Rn>,#-<imm8>]
  1010. // STRB<c> <Rt>,[<Rn>],#+/-<imm8>
  1011. // STRB<c> <Rt>,[<Rn>,#+/-<imm8>]!
  1012. int P = ntBIT (10);
  1013. int U = ntBIT (9);
  1014. int W = ntBIT (8);
  1015. ARMword imm8 = ntBITS (0, 7);
  1016. tASSERT (! (P && U && !W));
  1017. tASSERT (! (Rn == 13 && P && !U && W && imm8 == 4));
  1018. * ainstr = 0xE4000000;
  1019. * ainstr |= (P << 24);
  1020. * ainstr |= (U << 23);
  1021. * ainstr |= (W << 21);
  1022. * ainstr |= imm8;
  1023. }
  1024. else
  1025. {
  1026. // STRB<c>.W <Rt>,[<Rn>,<Rm>{,LSL #<imm2>}]
  1027. tASSERT (ntBITS (6, 11) == 0);
  1028. * ainstr = 0xE7C00000;
  1029. * ainstr |= (ntBITS (4, 5) << 7);
  1030. * ainstr |= ntBITS (0, 3);
  1031. }
  1032. }
  1033. * ainstr |= (Rn << 16);
  1034. * ainstr |= (Rt << 12);
  1035. * pvalid = t_decoded;
  1036. break;
  1037. }
  1038. case 0xC2: // LDR, STR
  1039. {
  1040. ARMword Rn = tBITS (0, 3);
  1041. ARMword Rt = ntBITS (12, 15);
  1042. ARMword imm8 = ntBITS (0, 7);
  1043. ARMword P = ntBIT (10);
  1044. ARMword U = ntBIT (9);
  1045. ARMword W = ntBIT (8);
  1046. tASSERT (Rn != 15);
  1047. if (tBIT (4))
  1048. {
  1049. if (Rn == 15)
  1050. {
  1051. // LDR<c>.W <Rt>,<label>
  1052. * ainstr = 0xE51F0000;
  1053. * ainstr |= ntBITS (0, 11);
  1054. }
  1055. else if (ntBIT (11))
  1056. {
  1057. tASSERT (! (P && U && ! W));
  1058. tASSERT (! (!P && U && W && Rn == 13 && imm8 == 4 && ntBIT (11) == 0));
  1059. tASSERT (! (P && !U && W && Rn == 13 && imm8 == 4 && ntBIT (11)));
  1060. // LDR<c> <Rt>,[<Rn>,#-<imm8>]
  1061. // LDR<c> <Rt>,[<Rn>],#+/-<imm8>
  1062. // LDR<c> <Rt>,[<Rn>,#+/-<imm8>]!
  1063. if (!P && W)
  1064. W = 0;
  1065. * ainstr = 0xE4100000;
  1066. * ainstr |= (P << 24);
  1067. * ainstr |= (U << 23);
  1068. * ainstr |= (W << 21);
  1069. * ainstr |= imm8;
  1070. }
  1071. else
  1072. {
  1073. // LDR<c>.W <Rt>,[<Rn>,<Rm>{,LSL #<imm2>}]
  1074. tASSERT (ntBITS (6, 11) == 0);
  1075. * ainstr = 0xE7900000;
  1076. * ainstr |= ntBITS (4, 5) << 7;
  1077. * ainstr |= ntBITS (0, 3);
  1078. }
  1079. }
  1080. else
  1081. {
  1082. if (ntBIT (11))
  1083. {
  1084. tASSERT (! (P && U && ! W));
  1085. if (Rn == 13 && P && !U && W && imm8 == 4)
  1086. {
  1087. // PUSH<c>.W <register>
  1088. tASSERT (ntBITS (0, 11) == 0xD04);
  1089. tASSERT (tBITS (0, 4) == 0x0D);
  1090. * ainstr = 0xE92D0000;
  1091. * ainstr |= (1 << Rt);
  1092. Rt = Rn = 0;
  1093. }
  1094. else
  1095. {
  1096. tASSERT (! (P && U && !W));
  1097. if (!P && W)
  1098. W = 0;
  1099. // STR<c> <Rt>,[<Rn>,#-<imm8>]
  1100. // STR<c> <Rt>,[<Rn>],#+/-<imm8>
  1101. // STR<c> <Rt>,[<Rn>,#+/-<imm8>]!
  1102. * ainstr = 0xE4000000;
  1103. * ainstr |= (P << 24);
  1104. * ainstr |= (U << 23);
  1105. * ainstr |= (W << 21);
  1106. * ainstr |= imm8;
  1107. }
  1108. }
  1109. else
  1110. {
  1111. // STR<c>.W <Rt>,[<Rn>,<Rm>{,LSL #<imm2>}]
  1112. tASSERT (ntBITS (6, 11) == 0);
  1113. * ainstr = 0xE7800000;
  1114. * ainstr |= ntBITS (4, 5) << 7;
  1115. * ainstr |= ntBITS (0, 3);
  1116. }
  1117. }
  1118. * ainstr |= (Rn << 16);
  1119. * ainstr |= (Rt << 12);
  1120. * pvalid = t_decoded;
  1121. break;
  1122. }
  1123. case 0xC1: // STRH
  1124. case 0xC5: // LDRH
  1125. {
  1126. ARMword Rn = tBITS (0, 3);
  1127. ARMword Rt = ntBITS (12, 15);
  1128. ARMword address;
  1129. tASSERT (Rn != 15);
  1130. if (tBIT (4) == 1)
  1131. {
  1132. if (tBIT (7))
  1133. {
  1134. // LDRH<c>.W <Rt>,[<Rn>{,#<imm12>}]
  1135. ARMword imm12 = ntBITS (0, 11);
  1136. address = state->Reg[Rn] + imm12;
  1137. }
  1138. else if (ntBIT (11))
  1139. {
  1140. // LDRH<c> <Rt>,[<Rn>,#-<imm8>]
  1141. // LDRH<c> <Rt>,[<Rn>],#+/-<imm8>
  1142. // LDRH<c> <Rt>,[<Rn>,#+/-<imm8>]!
  1143. ARMword P = ntBIT (10);
  1144. ARMword U = ntBIT (9);
  1145. ARMword W = ntBIT (8);
  1146. ARMword imm8 = ntBITS (0, 7);
  1147. tASSERT (Rn != 15);
  1148. tASSERT (! (P && U && !W));
  1149. * ainstr = 0xE05000B0;
  1150. * ainstr |= (P << 24);
  1151. * ainstr |= (U << 23);
  1152. * ainstr |= (W << 21);
  1153. * ainstr |= (Rn << 16);
  1154. * ainstr |= (Rt << 12);
  1155. * ainstr |= ((imm8 & 0xF0) << 4);
  1156. * ainstr |= (imm8 & 0xF);
  1157. * pvalid = t_decoded;
  1158. break;
  1159. }
  1160. else
  1161. {
  1162. // LDRH<c>.W <Rt>,[<Rn>,<Rm>{,LSL #<imm2>}]
  1163. ARMword Rm = ntBITS (0, 3);
  1164. ARMword imm2 = ntBITS (4, 5);
  1165. tASSERT (ntBITS (6, 10) == 0);
  1166. address = state->Reg[Rn] + (state->Reg[Rm] << imm2);
  1167. }
  1168. state->Reg[Rt] = ARMul_LoadHalfWord (state, address);
  1169. }
  1170. else
  1171. {
  1172. if (tBIT (7))
  1173. {
  1174. // STRH<c>.W <Rt>,[<Rn>{,#<imm12>}]
  1175. ARMword imm12 = ntBITS (0, 11);
  1176. address = state->Reg[Rn] + imm12;
  1177. }
  1178. else if (ntBIT (11))
  1179. {
  1180. // STRH<c> <Rt>,[<Rn>,#-<imm8>]
  1181. // STRH<c> <Rt>,[<Rn>],#+/-<imm8>
  1182. // STRH<c> <Rt>,[<Rn>,#+/-<imm8>]!
  1183. ARMword P = ntBIT (10);
  1184. ARMword U = ntBIT (9);
  1185. ARMword W = ntBIT (8);
  1186. ARMword imm8 = ntBITS (0, 7);
  1187. tASSERT (! (P && U && !W));
  1188. * ainstr = 0xE04000B0;
  1189. * ainstr |= (P << 24);
  1190. * ainstr |= (U << 23);
  1191. * ainstr |= (W << 21);
  1192. * ainstr |= (Rn << 16);
  1193. * ainstr |= (Rt << 12);
  1194. * ainstr |= ((imm8 & 0xF0) << 4);
  1195. * ainstr |= (imm8 & 0xF);
  1196. * pvalid = t_decoded;
  1197. break;
  1198. }
  1199. else
  1200. {
  1201. // STRH<c>.W <Rt>,[<Rn>,<Rm>{,LSL #<imm2>}]
  1202. ARMword Rm = ntBITS (0, 3);
  1203. ARMword imm2 = ntBITS (4, 5);
  1204. tASSERT (ntBITS (6, 10) == 0);
  1205. address = state->Reg[Rn] + (state->Reg[Rm] << imm2);
  1206. }
  1207. ARMul_StoreHalfWord (state, address, state->Reg [Rt]);
  1208. }
  1209. * pvalid = t_branch;
  1210. break;
  1211. }
  1212. case 0xC6: // LDR.W/STR.W
  1213. {
  1214. ARMword Rn = tBITS (0, 3);
  1215. ARMword Rt = ntBITS (12, 15);
  1216. ARMword imm12 = ntBITS (0, 11);
  1217. ARMword address = state->Reg[Rn];
  1218. if (Rn == 15)
  1219. {
  1220. // LDR<c>.W <Rt>,<label>
  1221. tASSERT (tBIT (4) == 1);
  1222. // tASSERT (tBIT (7) == 1)
  1223. }
  1224. address += imm12;
  1225. if (tBIT (4) == 1)
  1226. state->Reg[Rt] = ARMul_LoadWordN (state, address);
  1227. else
  1228. ARMul_StoreWordN (state, address, state->Reg [Rt]);
  1229. * pvalid = t_resolved;
  1230. break;
  1231. }
  1232. case 0xC8:
  1233. case 0xCC: // LDRSB
  1234. {
  1235. ARMword Rt = ntBITS (12, 15);
  1236. ARMword Rn = tBITS (0, 3);
  1237. ARMword U = tBIT (7);
  1238. ARMword address = state->Reg[Rn];
  1239. tASSERT (tBIT (4) == 1);
  1240. tASSERT (Rt != 15); // PLI
  1241. if (Rn == 15)
  1242. {
  1243. // LDRSB<c> <Rt>,<label>
  1244. ARMword imm12 = ntBITS (0, 11);
  1245. address += (U ? imm12 : - imm12);
  1246. }
  1247. else if (U)
  1248. {
  1249. // LDRSB<c> <Rt>,[<Rn>,#<imm12>]
  1250. ARMword imm12 = ntBITS (0, 11);
  1251. address += imm12;
  1252. }
  1253. else if (ntBIT (11))
  1254. {
  1255. // LDRSB<c> <Rt>,[<Rn>,#-<imm8>]
  1256. // LDRSB<c> <Rt>,[<Rn>],#+/-<imm8>
  1257. // LDRSB<c> <Rt>,[<Rn>,#+/-<imm8>]!
  1258. * ainstr = 0xE05000D0;
  1259. * ainstr |= ntBIT (10) << 24; // P
  1260. * ainstr |= ntBIT (9) << 23; // U
  1261. * ainstr |= ntBIT (8) << 21; // W
  1262. * ainstr |= Rn << 16;
  1263. * ainstr |= Rt << 12;
  1264. * ainstr |= ntBITS (4, 7) << 8;
  1265. * ainstr |= ntBITS (0, 3);
  1266. * pvalid = t_decoded;
  1267. break;
  1268. }
  1269. else
  1270. {
  1271. // LDRSB<c>.W <Rt>,[<Rn>,<Rm>{,LSL #<imm2>}]
  1272. ARMword Rm = ntBITS (0, 3);
  1273. ARMword imm2 = ntBITS (4,5);
  1274. tASSERT (ntBITS (6, 11) == 0);
  1275. address += (state->Reg[Rm] << imm2);
  1276. }
  1277. state->Reg[Rt] = ARMul_LoadByte (state, address);
  1278. if (state->Reg[Rt] & 0x80)
  1279. state->Reg[Rt] |= -(1 << 8);
  1280. * pvalid = t_resolved;
  1281. break;
  1282. }
  1283. case 0xC9:
  1284. case 0xCD:// LDRSH
  1285. {
  1286. ARMword Rt = ntBITS (12, 15);
  1287. ARMword Rn = tBITS (0, 3);
  1288. ARMword U = tBIT (7);
  1289. ARMword address = state->Reg[Rn];
  1290. tASSERT (tBIT (4) == 1);
  1291. if (Rn == 15 || U == 1)
  1292. {
  1293. // Rn==15 => LDRSH<c> <Rt>,<label>
  1294. // Rn!=15 => LDRSH<c> <Rt>,[<Rn>,#<imm12>]
  1295. ARMword imm12 = ntBITS (0, 11);
  1296. address += (U ? imm12 : - imm12);
  1297. }
  1298. else if (ntBIT (11))
  1299. {
  1300. // LDRSH<c> <Rt>,[<Rn>,#-<imm8>]
  1301. // LDRSH<c> <Rt>,[<Rn>],#+/-<imm8>
  1302. // LDRSH<c> <Rt>,[<Rn>,#+/-<imm8>]!
  1303. * ainstr = 0xE05000F0;
  1304. * ainstr |= ntBIT (10) << 24; // P
  1305. * ainstr |= ntBIT (9) << 23; // U
  1306. * ainstr |= ntBIT (8) << 21; // W
  1307. * ainstr |= Rn << 16;
  1308. * ainstr |= Rt << 12;
  1309. * ainstr |= ntBITS (4, 7) << 8;
  1310. * ainstr |= ntBITS (0, 3);
  1311. * pvalid = t_decoded;
  1312. break;
  1313. }
  1314. else /* U == 0 */
  1315. {
  1316. // LDRSH<c>.W <Rt>,[<Rn>,<Rm>{,LSL #<imm2>}]
  1317. ARMword Rm = ntBITS (0, 3);
  1318. ARMword imm2 = ntBITS (4,5);
  1319. tASSERT (ntBITS (6, 11) == 0);
  1320. address += (state->Reg[Rm] << imm2);
  1321. }
  1322. state->Reg[Rt] = ARMul_LoadHalfWord (state, address);
  1323. if (state->Reg[Rt] & 0x8000)
  1324. state->Reg[Rt] |= -(1 << 16);
  1325. * pvalid = t_branch;
  1326. break;
  1327. }
  1328. case 0x0D0:
  1329. {
  1330. ARMword Rm = ntBITS (0, 3);
  1331. ARMword Rd = ntBITS (8, 11);
  1332. tASSERT (ntBITS (12, 15) == 15);
  1333. if (ntBIT (7) == 1)
  1334. {
  1335. // SXTH<c>.W <Rd>,<Rm>{,<rotation>}
  1336. ARMword ror = ntBITS (4, 5) << 3;
  1337. ARMword val;
  1338. val = state->Reg[Rm];
  1339. val = (val >> ror) | (val << (32 - ror));
  1340. if (val & 0x8000)
  1341. val |= -(1 << 16);
  1342. state->Reg[Rd] = val;
  1343. }
  1344. else
  1345. {
  1346. // LSL{S}<c>.W <Rd>,<Rn>,<Rm>
  1347. ARMword Rn = tBITS (0, 3);
  1348. tASSERT (ntBITS (4, 6) == 0);
  1349. state->Reg[Rd] = state->Reg[Rn] << (state->Reg[Rm] & 0xFF);
  1350. if (tBIT (4))
  1351. ARMul_NegZero (state, state->Reg[Rd]);
  1352. }
  1353. * pvalid = t_branch;
  1354. break;
  1355. }
  1356. case 0x0D1: // LSR{S}<c>.W <Rd>,<Rn>,<Rm>
  1357. {
  1358. ARMword Rd = ntBITS (8, 11);
  1359. ARMword Rn = tBITS (0, 3);
  1360. ARMword Rm = ntBITS (0, 3);
  1361. tASSERT (ntBITS (12, 15) == 15);
  1362. tASSERT (ntBITS (4, 7) == 0);
  1363. state->Reg[Rd] = state->Reg[Rn] >> (state->Reg[Rm] & 0xFF);
  1364. if (tBIT (4))
  1365. ARMul_NegZero (state, state->Reg[Rd]);
  1366. * pvalid = t_resolved;
  1367. break;
  1368. }
  1369. case 0xD2:
  1370. tASSERT (ntBITS (12, 15) == 15);
  1371. if (ntBIT (7))
  1372. {
  1373. tASSERT (ntBIT (6) == 0);
  1374. // UXTB<c>.W <Rd>,<Rm>{,<rotation>}
  1375. * ainstr = 0xE6EF0070;
  1376. * ainstr |= (ntBITS (4, 5) << 10); // rotate
  1377. * ainstr |= ntBITS (0, 3); // Rm
  1378. }
  1379. else
  1380. {
  1381. // ASR{S}<c>.W <Rd>,<Rn>,<Rm>
  1382. tASSERT (ntBITS (4, 7) == 0);
  1383. * ainstr = 0xE1A00050;
  1384. if (! in_IT_block ())
  1385. * ainstr |= (tBIT (4) << 20);
  1386. * ainstr |= (ntBITS (0, 3) << 8); // Rm
  1387. * ainstr |= tBITS (0, 3); // Rn
  1388. }
  1389. * ainstr |= (ntBITS (8, 11) << 12); // Rd
  1390. * pvalid = t_decoded;
  1391. break;
  1392. case 0xD3: // ROR{S}<c>.W <Rd>,<Rn>,<Rm>
  1393. tASSERT (ntBITS (12, 15) == 15);
  1394. tASSERT (ntBITS (4, 7) == 0);
  1395. * ainstr = 0xE1A00070;
  1396. if (! in_IT_block ())
  1397. * ainstr |= (tBIT (4) << 20);
  1398. * ainstr |= (ntBITS (8, 11) << 12); // Rd
  1399. * ainstr |= (ntBITS (0, 3) << 8); // Rm
  1400. * ainstr |= (tBITS (0, 3) << 0); // Rn
  1401. * pvalid = t_decoded;
  1402. break;
  1403. case 0xD4:
  1404. {
  1405. ARMword Rn = tBITS (0, 3);
  1406. ARMword Rd = ntBITS (8, 11);
  1407. ARMword Rm = ntBITS (0, 3);
  1408. tASSERT (ntBITS (12, 15) == 15);
  1409. if (ntBITS (4, 7) == 8)
  1410. {
  1411. // REV<c>.W <Rd>,<Rm>
  1412. ARMword val = state->Reg[Rm];
  1413. tASSERT (Rm == Rn);
  1414. state->Reg [Rd] =
  1415. (val >> 24)
  1416. | ((val >> 8) & 0xFF00)
  1417. | ((val << 8) & 0xFF0000)
  1418. | (val << 24);
  1419. * pvalid = t_resolved;
  1420. }
  1421. else
  1422. {
  1423. tASSERT (ntBITS (4, 7) == 4);
  1424. if (tBIT (4) == 1)
  1425. // UADD8<c> <Rd>,<Rn>,<Rm>
  1426. * ainstr = 0xE6500F10;
  1427. else
  1428. // UADD16<c> <Rd>,<Rn>,<Rm>
  1429. * ainstr = 0xE6500F90;
  1430. * ainstr |= (Rn << 16);
  1431. * ainstr |= (Rd << 12);
  1432. * ainstr |= (Rm << 0);
  1433. * pvalid = t_decoded;
  1434. }
  1435. break;
  1436. }
  1437. case 0xD5:
  1438. {
  1439. ARMword Rn = tBITS (0, 3);
  1440. ARMword Rd = ntBITS (8, 11);
  1441. ARMword Rm = ntBITS (0, 3);
  1442. tASSERT (ntBITS (12, 15) == 15);
  1443. tASSERT (ntBITS (4, 7) == 8);
  1444. if (tBIT (4))
  1445. {
  1446. // CLZ<c> <Rd>,<Rm>
  1447. tASSERT (Rm == Rn);
  1448. * ainstr = 0xE16F0F10;
  1449. }
  1450. else
  1451. {
  1452. // SEL<c> <Rd>,<Rn>,<Rm>
  1453. * ainstr = 0xE6800FB0;
  1454. * ainstr |= (Rn << 16);
  1455. }
  1456. * ainstr |= (Rd << 12);
  1457. * ainstr |= (Rm << 0);
  1458. * pvalid = t_decoded;
  1459. break;
  1460. }
  1461. case 0xD8: // MUL
  1462. {
  1463. ARMword Rn = tBITS (0, 3);
  1464. ARMword Rm = ntBITS (0, 3);
  1465. ARMword Rd = ntBITS (8, 11);
  1466. ARMword Ra = ntBITS (12, 15);
  1467. if (tBIT (4))
  1468. {
  1469. // SMLA<x><y><c> <Rd>,<Rn>,<Rm>,<Ra>
  1470. ARMword nval = state->Reg[Rn];
  1471. ARMword mval = state->Reg[Rm];
  1472. ARMword res;
  1473. tASSERT (ntBITS (6, 7) == 0);
  1474. tASSERT (Ra != 15);
  1475. if (ntBIT (5))
  1476. nval >>= 16;
  1477. else
  1478. nval &= 0xFFFF;
  1479. if (ntBIT (4))
  1480. mval >>= 16;
  1481. else
  1482. mval &= 0xFFFF;
  1483. res = nval * mval;
  1484. res += state->Reg[Ra];
  1485. // FIXME: Test and clear/set the Q bit.
  1486. state->Reg[Rd] = res;
  1487. }
  1488. else
  1489. {
  1490. if (ntBITS (4, 7) == 1)
  1491. {
  1492. // MLS<c> <Rd>,<Rn>,<Rm>,<Ra>
  1493. state->Reg[Rd] = state->Reg[Ra] - (state->Reg[Rn] * state->Reg[Rm]);
  1494. }
  1495. else
  1496. {
  1497. tASSERT (ntBITS (4, 7) == 0);
  1498. if (Ra == 15)
  1499. // MUL<c> <Rd>,<Rn>,<Rm>
  1500. state->Reg[Rd] = state->Reg[Rn] * state->Reg[Rm];
  1501. else
  1502. // MLA<c> <Rd>,<Rn>,<Rm>,<Ra>
  1503. state->Reg[Rd] = state->Reg[Rn] * state->Reg[Rm] + state->Reg[Ra];
  1504. }
  1505. }
  1506. * pvalid = t_resolved;
  1507. break;
  1508. }
  1509. case 0xDC:
  1510. if (tBIT (4) == 0 && ntBITS (4, 7) == 0)
  1511. {
  1512. // SMULL
  1513. * ainstr = 0xE0C00090;
  1514. * ainstr |= (ntBITS (8, 11) << 16); // RdHi
  1515. * ainstr |= (ntBITS (12, 15) << 12); // RdLo
  1516. * ainstr |= (ntBITS (0, 3) << 8); // Rm
  1517. * ainstr |= tBITS (0, 3); // Rn
  1518. * pvalid = t_decoded;
  1519. }
  1520. else if (tBIT (4) == 1 && ntBITS (4, 7) == 0xF)
  1521. {
  1522. // SDIV
  1523. * ainstr = 0xE710F010;
  1524. * ainstr |= (ntBITS (8, 11) << 16); // Rd
  1525. * ainstr |= (ntBITS (0, 3) << 8); // Rm
  1526. * ainstr |= tBITS (0, 3); // Rn
  1527. * pvalid = t_decoded;
  1528. }
  1529. else
  1530. {
  1531. fprintf (stderr, "(op = %x) ", tBITS (5,12));
  1532. tASSERT (0);
  1533. return;
  1534. }
  1535. break;
  1536. case 0xDD:
  1537. if (tBIT (4) == 0 && ntBITS (4, 7) == 0)
  1538. {
  1539. // UMULL
  1540. * ainstr = 0xE0800090;
  1541. * ainstr |= (ntBITS (8, 11) << 16); // RdHi
  1542. * ainstr |= (ntBITS (12, 15) << 12); // RdLo
  1543. * ainstr |= (ntBITS (0, 3) << 8); // Rm
  1544. * ainstr |= tBITS (0, 3); // Rn
  1545. * pvalid = t_decoded;
  1546. }
  1547. else if (tBIT (4) == 1 && ntBITS (4, 7) == 0xF)
  1548. {
  1549. // UDIV
  1550. * ainstr = 0xE730F010;
  1551. * ainstr |= (ntBITS (8, 11) << 16); // Rd
  1552. * ainstr |= (ntBITS (0, 3) << 8); // Rm
  1553. * ainstr |= tBITS (0, 3); // Rn
  1554. * pvalid = t_decoded;
  1555. }
  1556. else
  1557. {
  1558. fprintf (stderr, "(op = %x) ", tBITS (5,12));
  1559. tASSERT (0);
  1560. return;
  1561. }
  1562. break;
  1563. case 0xDF: // UMLAL
  1564. tASSERT (tBIT (4) == 0);
  1565. tASSERT (ntBITS (4, 7) == 0);
  1566. * ainstr = 0xE0A00090;
  1567. * ainstr |= (ntBITS (8, 11) << 16); // RdHi
  1568. * ainstr |= (ntBITS (12, 15) << 12); // RdLo
  1569. * ainstr |= (ntBITS (0, 3) << 8); // Rm
  1570. * ainstr |= tBITS (0, 3); // Rn
  1571. * pvalid = t_decoded;
  1572. break;
  1573. default:
  1574. fprintf (stderr, "(op = %x) ", tBITS (5,12));
  1575. tASSERT (0);
  1576. return;
  1577. }
  1578. /* Tell the Thumb decoder to skip the next 16-bit insn - it was
  1579. part of this insn - unless this insn has changed the PC. */
  1580. skipping_32bit_thumb = pc + 2;
  1581. }
  1582. /* Attempt to emulate an ARMv6 instruction.
  1583. Stores t_branch into PVALUE upon success or t_undefined otherwise. */
  1584. static void
  1585. handle_v6_thumb_insn (ARMul_State * state,
  1586. ARMword tinstr,
  1587. ARMword next_instr,
  1588. ARMword pc,
  1589. ARMword * ainstr,
  1590. tdstate * pvalid)
  1591. {
  1592. if (! state->is_v6)
  1593. {
  1594. * pvalid = t_undefined;
  1595. return;
  1596. }
  1597. if (tBITS (12, 15) == 0xB
  1598. && tBIT (10) == 0
  1599. && tBIT (8) == 1)
  1600. {
  1601. // Conditional branch forwards.
  1602. ARMword Rn = tBITS (0, 2);
  1603. ARMword imm5 = tBIT (9) << 5 | tBITS (3, 7);
  1604. if (tBIT (11))
  1605. {
  1606. if (state->Reg[Rn] != 0)
  1607. {
  1608. state->Reg[15] = (pc + 4 + imm5 * 2);
  1609. FLUSHPIPE;
  1610. }
  1611. }
  1612. else
  1613. {
  1614. if (state->Reg[Rn] == 0)
  1615. {
  1616. state->Reg[15] = (pc + 4 + imm5 * 2);
  1617. FLUSHPIPE;
  1618. }
  1619. }
  1620. * pvalid = t_branch;
  1621. return;
  1622. }
  1623. switch (tinstr & 0xFFC0)
  1624. {
  1625. case 0x4400:
  1626. case 0x4480:
  1627. case 0x4440:
  1628. case 0x44C0: // ADD
  1629. {
  1630. ARMword Rd = (tBIT (7) << 3) | tBITS (0, 2);
  1631. ARMword Rm = tBITS (3, 6);
  1632. state->Reg[Rd] += state->Reg[Rm];
  1633. break;
  1634. }
  1635. case 0x4600: // MOV<c> <Rd>,<Rm>
  1636. {
  1637. // instr [15, 8] = 0100 0110
  1638. // instr [7] = Rd<high>
  1639. // instr [6,3] = Rm
  1640. // instr [2,0] = Rd<low>
  1641. ARMword Rd = (tBIT(7) << 3) | tBITS (0, 2);
  1642. // FIXME: Check for Rd == 15 and ITblock.
  1643. state->Reg[Rd] = state->Reg[tBITS (3, 6)];
  1644. break;
  1645. }
  1646. case 0xBF00:
  1647. case 0xBF40:
  1648. case 0xBF80:
  1649. case 0xBFC0:
  1650. handle_IT_block (state, tinstr, pvalid);
  1651. return;
  1652. case 0xE840:
  1653. case 0xE880: // LDMIA
  1654. case 0xE8C0:
  1655. case 0xE900: // STM
  1656. case 0xE940:
  1657. case 0xE980:
  1658. case 0xE9C0: // LDRD
  1659. case 0xEA00: // BIC
  1660. case 0xEA40: // ORR
  1661. case 0xEA80: // EOR
  1662. case 0xEAC0:
  1663. case 0xEB00: // ADD
  1664. case 0xEB40: // SBC
  1665. case 0xEB80: // SUB
  1666. case 0xEBC0: // RSB
  1667. case 0xFA80: // UADD, SEL
  1668. case 0xFBC0: // UMULL, SMULL, SDIV, UDIV
  1669. handle_T2_insn (state, tinstr, next_instr, pc, ainstr, pvalid);
  1670. return;
  1671. case 0xba00: /* rev */
  1672. {
  1673. ARMword val = state->Reg[tBITS (3, 5)];
  1674. state->Reg [tBITS (0, 2)] =
  1675. (val >> 24)
  1676. | ((val >> 8) & 0xFF00)
  1677. | ((val << 8) & 0xFF0000)
  1678. | (val << 24);
  1679. break;
  1680. }
  1681. case 0xba40: /* rev16 */
  1682. {
  1683. ARMword val = state->Reg[tBITS (3, 5)];
  1684. state->Reg [tBITS (0, 2)] = (val >> 16) | (val << 16);
  1685. break;
  1686. }
  1687. case 0xb660: /* cpsie */
  1688. case 0xb670: /* cpsid */
  1689. case 0xbac0: /* revsh */
  1690. case 0xb650: /* setend */
  1691. default:
  1692. printf ("Unhandled v6 thumb insn: %04x\n", tinstr);
  1693. * pvalid = t_undefined;
  1694. return;
  1695. case 0xb200: /* sxth */
  1696. {
  1697. ARMword Rm = state->Reg [(tinstr & 0x38) >> 3];
  1698. if (Rm & 0x8000)
  1699. state->Reg [(tinstr & 0x7)] = (Rm & 0xffff) | 0xffff0000;
  1700. else
  1701. state->Reg [(tinstr & 0x7)] = Rm & 0xffff;
  1702. break;
  1703. }
  1704. case 0xb240: /* sxtb */
  1705. {
  1706. ARMword Rm = state->Reg [(tinstr & 0x38) >> 3];
  1707. if (Rm & 0x80)
  1708. state->Reg [(tinstr & 0x7)] = (Rm & 0xff) | 0xffffff00;
  1709. else
  1710. state->Reg [(tinstr & 0x7)] = Rm & 0xff;
  1711. break;
  1712. }
  1713. case 0xb280: /* uxth */
  1714. {
  1715. ARMword Rm = state->Reg [(tinstr & 0x38) >> 3];
  1716. state->Reg [(tinstr & 0x7)] = Rm & 0xffff;
  1717. break;
  1718. }
  1719. case 0xb2c0: /* uxtb */
  1720. {
  1721. ARMword Rm = state->Reg [(tinstr & 0x38) >> 3];
  1722. state->Reg [(tinstr & 0x7)] = Rm & 0xff;
  1723. break;
  1724. }
  1725. }
  1726. /* Indicate that the instruction has been processed. */
  1727. * pvalid = t_branch;
  1728. }
  1729. /* Decode a 16bit Thumb instruction. The instruction is in the low
  1730. 16-bits of the tinstr field, with the following Thumb instruction
  1731. held in the high 16-bits. Passing in two Thumb instructions allows
  1732. easier simulation of the special dual BL instruction. */
  1733. tdstate
  1734. ARMul_ThumbDecode (ARMul_State * state,
  1735. ARMword pc,
  1736. ARMword tinstr,
  1737. ARMword * ainstr)
  1738. {
  1739. tdstate valid = t_decoded; /* default assumes a valid instruction */
  1740. ARMword next_instr;
  1741. ARMword old_tinstr = tinstr;
  1742. if (skipping_32bit_thumb == pc)
  1743. {
  1744. skipping_32bit_thumb = 0;
  1745. return t_branch;
  1746. }
  1747. skipping_32bit_thumb = 0;
  1748. if (state->bigendSig)
  1749. {
  1750. next_instr = tinstr & 0xFFFF;
  1751. tinstr >>= 16;
  1752. }
  1753. else
  1754. {
  1755. next_instr = tinstr >> 16;
  1756. tinstr &= 0xFFFF;
  1757. }
  1758. if (! IT_block_allow (state))
  1759. {
  1760. if ( tBITS (11, 15) == 0x1F
  1761. || tBITS (11, 15) == 0x1E
  1762. || tBITS (11, 15) == 0x1D)
  1763. {
  1764. if (trace)
  1765. fprintf (stderr, "pc: %x, SKIP instr: %04x|%04x\n",
  1766. pc & ~1, tinstr, next_instr);
  1767. skipping_32bit_thumb = pc + 2;
  1768. }
  1769. else if (trace)
  1770. fprintf (stderr, "pc: %x, SKIP instr: %04x\n", pc & ~1, tinstr);
  1771. return t_branch;
  1772. }
  1773. old_tinstr = tinstr;
  1774. if (trace)
  1775. fprintf (stderr, "pc: %x, Thumb instr: %x", pc & ~1, tinstr);
  1776. #if 1 /* debugging to catch non updates */
  1777. *ainstr = 0xDEADC0DE;
  1778. #endif
  1779. switch ((tinstr & 0xF800) >> 11)
  1780. {
  1781. case 0: /* LSL */
  1782. case 1: /* LSR */
  1783. case 2: /* ASR */
  1784. /* Format 1 */
  1785. *ainstr = 0xE1B00000 /* base opcode */
  1786. | ((tinstr & 0x1800) >> (11 - 5)) /* shift type */
  1787. | ((tinstr & 0x07C0) << (7 - 6)) /* imm5 */
  1788. | ((tinstr & 0x0038) >> 3) /* Rs */
  1789. | ((tinstr & 0x0007) << 12); /* Rd */
  1790. break;
  1791. case 3: /* ADD/SUB */
  1792. /* Format 2 */
  1793. {
  1794. ARMword subset[4] =
  1795. {
  1796. 0xE0900000, /* ADDS Rd,Rs,Rn */
  1797. 0xE0500000, /* SUBS Rd,Rs,Rn */
  1798. 0xE2900000, /* ADDS Rd,Rs,#imm3 */
  1799. 0xE2500000 /* SUBS Rd,Rs,#imm3 */
  1800. };
  1801. /* It is quicker indexing into a table, than performing switch
  1802. or conditionals: */
  1803. *ainstr = subset[(tinstr & 0x0600) >> 9] /* base opcode */
  1804. | ((tinstr & 0x01C0) >> 6) /* Rn or imm3 */
  1805. | ((tinstr & 0x0038) << (16 - 3)) /* Rs */
  1806. | ((tinstr & 0x0007) << (12 - 0)); /* Rd */
  1807. if (in_IT_block ())
  1808. *ainstr &= ~ (1 << 20);
  1809. }
  1810. break;
  1811. case 4:
  1812. * ainstr = 0xE3A00000; /* MOV Rd,#imm8 */
  1813. if (! in_IT_block ())
  1814. * ainstr |= (1 << 20);
  1815. * ainstr |= tBITS (8, 10) << 12;
  1816. * ainstr |= tBITS (0, 7);
  1817. break;
  1818. case 5:
  1819. * ainstr = 0xE3500000; /* CMP Rd,#imm8 */
  1820. * ainstr |= tBITS (8, 10) << 16;
  1821. * ainstr |= tBITS (0, 7);
  1822. break;
  1823. case 6:
  1824. case 7:
  1825. * ainstr = tBIT (11)
  1826. ? 0xE2400000 /* SUB Rd,Rd,#imm8 */
  1827. : 0xE2800000; /* ADD Rd,Rd,#imm8 */
  1828. if (! in_IT_block ())
  1829. * ainstr |= (1 << 20);
  1830. * ainstr |= tBITS (8, 10) << 12;
  1831. * ainstr |= tBITS (8, 10) << 16;
  1832. * ainstr |= tBITS (0, 7);
  1833. break;
  1834. case 8: /* Arithmetic and high register transfers */
  1835. /* TODO: Since the subsets for both Format 4 and Format 5
  1836. instructions are made up of different ARM encodings, we could
  1837. save the following conditional, and just have one large
  1838. subset. */
  1839. if ((tinstr & (1 << 10)) == 0)
  1840. {
  1841. /* Format 4 */
  1842. struct
  1843. {
  1844. ARMword opcode;
  1845. enum
  1846. { t_norm, t_shift, t_neg, t_mul }
  1847. otype;
  1848. }
  1849. subset[16] =
  1850. {
  1851. { 0xE0100000, t_norm}, /* ANDS Rd,Rd,Rs */
  1852. { 0xE0300000, t_norm}, /* EORS Rd,Rd,Rs */
  1853. { 0xE1B00010, t_shift}, /* MOVS Rd,Rd,LSL Rs */
  1854. { 0xE1B00030, t_shift}, /* MOVS Rd,Rd,LSR Rs */
  1855. { 0xE1B00050, t_shift}, /* MOVS Rd,Rd,ASR Rs */
  1856. { 0xE0B00000, t_norm}, /* ADCS Rd,Rd,Rs */
  1857. { 0xE0D00000, t_norm}, /* SBCS Rd,Rd,Rs */
  1858. { 0xE1B00070, t_shift}, /* MOVS Rd,Rd,ROR Rs */
  1859. { 0xE1100000, t_norm}, /* TST Rd,Rs */
  1860. { 0xE2700000, t_neg}, /* RSBS Rd,Rs,#0 */
  1861. { 0xE1500000, t_norm}, /* CMP Rd,Rs */
  1862. { 0xE1700000, t_norm}, /* CMN Rd,Rs */
  1863. { 0xE1900000, t_norm}, /* ORRS Rd,Rd,Rs */
  1864. { 0xE0100090, t_mul} , /* MULS Rd,Rd,Rs */
  1865. { 0xE1D00000, t_norm}, /* BICS Rd,Rd,Rs */
  1866. { 0xE1F00000, t_norm} /* MVNS Rd,Rs */
  1867. };
  1868. *ainstr = subset[(tinstr & 0x03C0) >> 6].opcode; /* base */
  1869. if (in_IT_block ())
  1870. {
  1871. struct
  1872. {
  1873. ARMword opcode;
  1874. enum
  1875. { t_norm, t_shift, t_neg, t_mul }
  1876. otype;
  1877. }
  1878. subset[16] =
  1879. {
  1880. { 0xE0000000, t_norm}, /* AND Rd,Rd,Rs */
  1881. { 0xE0200000, t_norm}, /* EOR Rd,Rd,Rs */
  1882. { 0xE1A00010, t_shift}, /* MOV Rd,Rd,LSL Rs */
  1883. { 0xE1A00030, t_shift}, /* MOV Rd,Rd,LSR Rs */
  1884. { 0xE1A00050, t_shift}, /* MOV Rd,Rd,ASR Rs */
  1885. { 0xE0A00000, t_norm}, /* ADC Rd,Rd,Rs */
  1886. { 0xE0C00000, t_norm}, /* SBC Rd,Rd,Rs */
  1887. { 0xE1A00070, t_shift}, /* MOV Rd,Rd,ROR Rs */
  1888. { 0xE1100000, t_norm}, /* TST Rd,Rs */
  1889. { 0xE2600000, t_neg}, /* RSB Rd,Rs,#0 */
  1890. { 0xE1500000, t_norm}, /* CMP Rd,Rs */
  1891. { 0xE1700000, t_norm}, /* CMN Rd,Rs */
  1892. { 0xE1800000, t_norm}, /* ORR Rd,Rd,Rs */
  1893. { 0xE0000090, t_mul} , /* MUL Rd,Rd,Rs */
  1894. { 0xE1C00000, t_norm}, /* BIC Rd,Rd,Rs */
  1895. { 0xE1E00000, t_norm} /* MVN Rd,Rs */
  1896. };
  1897. *ainstr = subset[(tinstr & 0x03C0) >> 6].opcode; /* base */
  1898. }
  1899. switch (subset[(tinstr & 0x03C0) >> 6].otype)
  1900. {
  1901. case t_norm:
  1902. *ainstr |= ((tinstr & 0x0007) << 16) /* Rn */
  1903. | ((tinstr & 0x0007) << 12) /* Rd */
  1904. | ((tinstr & 0x0038) >> 3); /* Rs */
  1905. break;
  1906. case t_shift:
  1907. *ainstr |= ((tinstr & 0x0007) << 12) /* Rd */
  1908. | ((tinstr & 0x0007) >> 0) /* Rm */
  1909. | ((tinstr & 0x0038) << (8 - 3)); /* Rs */
  1910. break;
  1911. case t_neg:
  1912. *ainstr |= ((tinstr & 0x0007) << 12) /* Rd */
  1913. | ((tinstr & 0x0038) << (16 - 3)); /* Rn */
  1914. break;
  1915. case t_mul:
  1916. *ainstr |= ((tinstr & 0x0007) << 16) /* Rd */
  1917. | ((tinstr & 0x0007) << 8) /* Rs */
  1918. | ((tinstr & 0x0038) >> 3); /* Rm */
  1919. break;
  1920. }
  1921. }
  1922. else
  1923. {
  1924. /* Format 5 */
  1925. ARMword Rd = ((tinstr & 0x0007) >> 0);
  1926. ARMword Rs = ((tinstr & 0x0038) >> 3);
  1927. if (tinstr & (1 << 7))
  1928. Rd += 8;
  1929. if (tinstr & (1 << 6))
  1930. Rs += 8;
  1931. switch ((tinstr & 0x03C0) >> 6)
  1932. {
  1933. case 0x1: /* ADD Rd,Rd,Hs */
  1934. case 0x2: /* ADD Hd,Hd,Rs */
  1935. case 0x3: /* ADD Hd,Hd,Hs */
  1936. *ainstr = 0xE0800000 /* base */
  1937. | (Rd << 16) /* Rn */
  1938. | (Rd << 12) /* Rd */
  1939. | (Rs << 0); /* Rm */
  1940. break;
  1941. case 0x5: /* CMP Rd,Hs */
  1942. case 0x6: /* CMP Hd,Rs */
  1943. case 0x7: /* CMP Hd,Hs */
  1944. *ainstr = 0xE1500000 /* base */
  1945. | (Rd << 16) /* Rn */
  1946. | (Rd << 12) /* Rd */
  1947. | (Rs << 0); /* Rm */
  1948. break;
  1949. case 0x9: /* MOV Rd,Hs */
  1950. case 0xA: /* MOV Hd,Rs */
  1951. case 0xB: /* MOV Hd,Hs */
  1952. *ainstr = 0xE1A00000 /* base */
  1953. | (Rd << 12) /* Rd */
  1954. | (Rs << 0); /* Rm */
  1955. break;
  1956. case 0xC: /* BX Rs */
  1957. case 0xD: /* BX Hs */
  1958. *ainstr = 0xE12FFF10 /* base */
  1959. | ((tinstr & 0x0078) >> 3); /* Rd */
  1960. break;
  1961. case 0xE: /* UNDEFINED */
  1962. case 0xF: /* UNDEFINED */
  1963. if (state->is_v5)
  1964. {
  1965. /* BLX Rs; BLX Hs */
  1966. *ainstr = 0xE12FFF30 /* base */
  1967. | ((tinstr & 0x0078) >> 3); /* Rd */
  1968. break;
  1969. }
  1970. /* Drop through. */
  1971. default:
  1972. case 0x0: /* UNDEFINED */
  1973. case 0x4: /* UNDEFINED */
  1974. case 0x8: /* UNDEFINED */
  1975. handle_v6_thumb_insn (state, tinstr, next_instr, pc, ainstr, & valid);
  1976. break;
  1977. }
  1978. }
  1979. break;
  1980. case 9: /* LDR Rd,[PC,#imm8] */
  1981. /* Format 6 */
  1982. *ainstr = 0xE59F0000 /* base */
  1983. | ((tinstr & 0x0700) << (12 - 8)) /* Rd */
  1984. | ((tinstr & 0x00FF) << (2 - 0)); /* off8 */
  1985. break;
  1986. case 10:
  1987. case 11:
  1988. /* TODO: Format 7 and Format 8 perform the same ARM encoding, so
  1989. the following could be merged into a single subset, saving on
  1990. the following boolean: */
  1991. if ((tinstr & (1 << 9)) == 0)
  1992. {
  1993. /* Format 7 */
  1994. ARMword subset[4] = {
  1995. 0xE7800000, /* STR Rd,[Rb,Ro] */
  1996. 0xE7C00000, /* STRB Rd,[Rb,Ro] */
  1997. 0xE7900000, /* LDR Rd,[Rb,Ro] */
  1998. 0xE7D00000 /* LDRB Rd,[Rb,Ro] */
  1999. };
  2000. *ainstr = subset[(tinstr & 0x0C00) >> 10] /* base */
  2001. | ((tinstr & 0x0007) << (12 - 0)) /* Rd */
  2002. | ((tinstr & 0x0038) << (16 - 3)) /* Rb */
  2003. | ((tinstr & 0x01C0) >> 6); /* Ro */
  2004. }
  2005. else
  2006. {
  2007. /* Format 8 */
  2008. ARMword subset[4] = {
  2009. 0xE18000B0, /* STRH Rd,[Rb,Ro] */
  2010. 0xE19000D0, /* LDRSB Rd,[Rb,Ro] */
  2011. 0xE19000B0, /* LDRH Rd,[Rb,Ro] */
  2012. 0xE19000F0 /* LDRSH Rd,[Rb,Ro] */
  2013. };
  2014. *ainstr = subset[(tinstr & 0x0C00) >> 10] /* base */
  2015. | ((tinstr & 0x0007) << (12 - 0)) /* Rd */
  2016. | ((tinstr & 0x0038) << (16 - 3)) /* Rb */
  2017. | ((tinstr & 0x01C0) >> 6); /* Ro */
  2018. }
  2019. break;
  2020. case 12: /* STR Rd,[Rb,#imm5] */
  2021. case 13: /* LDR Rd,[Rb,#imm5] */
  2022. case 14: /* STRB Rd,[Rb,#imm5] */
  2023. case 15: /* LDRB Rd,[Rb,#imm5] */
  2024. /* Format 9 */
  2025. {
  2026. ARMword subset[4] = {
  2027. 0xE5800000, /* STR Rd,[Rb,#imm5] */
  2028. 0xE5900000, /* LDR Rd,[Rb,#imm5] */
  2029. 0xE5C00000, /* STRB Rd,[Rb,#imm5] */
  2030. 0xE5D00000 /* LDRB Rd,[Rb,#imm5] */
  2031. };
  2032. /* The offset range defends on whether we are transferring a
  2033. byte or word value: */
  2034. *ainstr = subset[(tinstr & 0x1800) >> 11] /* base */
  2035. | ((tinstr & 0x0007) << (12 - 0)) /* Rd */
  2036. | ((tinstr & 0x0038) << (16 - 3)) /* Rb */
  2037. | ((tinstr & 0x07C0) >> (6 - ((tinstr & (1 << 12)) ? 0 : 2))); /* off5 */
  2038. }
  2039. break;
  2040. case 16: /* STRH Rd,[Rb,#imm5] */
  2041. case 17: /* LDRH Rd,[Rb,#imm5] */
  2042. /* Format 10 */
  2043. *ainstr = ((tinstr & (1 << 11)) /* base */
  2044. ? 0xE1D000B0 /* LDRH */
  2045. : 0xE1C000B0) /* STRH */
  2046. | ((tinstr & 0x0007) << (12 - 0)) /* Rd */
  2047. | ((tinstr & 0x0038) << (16 - 3)) /* Rb */
  2048. | ((tinstr & 0x01C0) >> (6 - 1)) /* off5, low nibble */
  2049. | ((tinstr & 0x0600) >> (9 - 8)); /* off5, high nibble */
  2050. break;
  2051. case 18: /* STR Rd,[SP,#imm8] */
  2052. case 19: /* LDR Rd,[SP,#imm8] */
  2053. /* Format 11 */
  2054. *ainstr = ((tinstr & (1 << 11)) /* base */
  2055. ? 0xE59D0000 /* LDR */
  2056. : 0xE58D0000) /* STR */
  2057. | ((tinstr & 0x0700) << (12 - 8)) /* Rd */
  2058. | ((tinstr & 0x00FF) << 2); /* off8 */
  2059. break;
  2060. case 20: /* ADD Rd,PC,#imm8 */
  2061. case 21: /* ADD Rd,SP,#imm8 */
  2062. /* Format 12 */
  2063. if ((tinstr & (1 << 11)) == 0)
  2064. {
  2065. /* NOTE: The PC value used here should by word aligned */
  2066. /* We encode shift-left-by-2 in the rotate immediate field,
  2067. so no shift of off8 is needed. */
  2068. *ainstr = 0xE28F0F00 /* base */
  2069. | ((tinstr & 0x0700) << (12 - 8)) /* Rd */
  2070. | (tinstr & 0x00FF); /* off8 */
  2071. }
  2072. else
  2073. {
  2074. /* We encode shift-left-by-2 in the rotate immediate field,
  2075. so no shift of off8 is needed. */
  2076. *ainstr = 0xE28D0F00 /* base */
  2077. | ((tinstr & 0x0700) << (12 - 8)) /* Rd */
  2078. | (tinstr & 0x00FF); /* off8 */
  2079. }
  2080. break;
  2081. case 22:
  2082. case 23:
  2083. switch (tinstr & 0x0F00)
  2084. {
  2085. case 0x0000:
  2086. /* Format 13 */
  2087. /* NOTE: The instruction contains a shift left of 2
  2088. equivalent (implemented as ROR #30): */
  2089. *ainstr = ((tinstr & (1 << 7)) /* base */
  2090. ? 0xE24DDF00 /* SUB */
  2091. : 0xE28DDF00) /* ADD */
  2092. | (tinstr & 0x007F); /* off7 */
  2093. break;
  2094. case 0x0400:
  2095. /* Format 14 - Push */
  2096. * ainstr = 0xE92D0000 | (tinstr & 0x00FF);
  2097. break;
  2098. case 0x0500:
  2099. /* Format 14 - Push + LR */
  2100. * ainstr = 0xE92D4000 | (tinstr & 0x00FF);
  2101. break;
  2102. case 0x0c00:
  2103. /* Format 14 - Pop */
  2104. * ainstr = 0xE8BD0000 | (tinstr & 0x00FF);
  2105. break;
  2106. case 0x0d00:
  2107. /* Format 14 - Pop + PC */
  2108. * ainstr = 0xE8BD8000 | (tinstr & 0x00FF);
  2109. break;
  2110. case 0x0e00:
  2111. if (state->is_v5)
  2112. {
  2113. /* This is normally an undefined instruction. The v5t architecture
  2114. defines this particular pattern as a BKPT instruction, for
  2115. hardware assisted debugging. We map onto the arm BKPT
  2116. instruction. */
  2117. if (state->is_v6)
  2118. // Map to the SVC instruction instead of the BKPT instruction.
  2119. * ainstr = 0xEF000000 | tBITS (0, 7);
  2120. else
  2121. * ainstr = 0xE1200070 | ((tinstr & 0xf0) << 4) | (tinstr & 0xf);
  2122. break;
  2123. }
  2124. /* Drop through. */
  2125. default:
  2126. /* Everything else is an undefined instruction. */
  2127. handle_v6_thumb_insn (state, tinstr, next_instr, pc, ainstr, & valid);
  2128. break;
  2129. }
  2130. break;
  2131. case 24: /* STMIA */
  2132. case 25: /* LDMIA */
  2133. /* Format 15 */
  2134. *ainstr = ((tinstr & (1 << 11)) /* base */
  2135. ? 0xE8B00000 /* LDMIA */
  2136. : 0xE8A00000) /* STMIA */
  2137. | ((tinstr & 0x0700) << (16 - 8)) /* Rb */
  2138. | (tinstr & 0x00FF); /* mask8 */
  2139. break;
  2140. case 26: /* Bcc */
  2141. case 27: /* Bcc/SWI */
  2142. if ((tinstr & 0x0F00) == 0x0F00)
  2143. {
  2144. /* Format 17 : SWI */
  2145. *ainstr = 0xEF000000;
  2146. /* Breakpoint must be handled specially. */
  2147. if ((tinstr & 0x00FF) == 0x18)
  2148. *ainstr |= ((tinstr & 0x00FF) << 16);
  2149. /* New breakpoint value. See gdb/arm-tdep.c */
  2150. else if ((tinstr & 0x00FF) == 0xFE)
  2151. *ainstr |= SWI_Breakpoint;
  2152. else
  2153. *ainstr |= (tinstr & 0x00FF);
  2154. }
  2155. else if ((tinstr & 0x0F00) != 0x0E00)
  2156. {
  2157. /* Format 16 */
  2158. int doit = FALSE;
  2159. /* TODO: Since we are doing a switch here, we could just add
  2160. the SWI and undefined instruction checks into this
  2161. switch to same on a couple of conditionals: */
  2162. switch ((tinstr & 0x0F00) >> 8)
  2163. {
  2164. case EQ:
  2165. doit = ZFLAG;
  2166. break;
  2167. case NE:
  2168. doit = !ZFLAG;
  2169. break;
  2170. case VS:
  2171. doit = VFLAG;
  2172. break;
  2173. case VC:
  2174. doit = !VFLAG;
  2175. break;
  2176. case MI:
  2177. doit = NFLAG;
  2178. break;
  2179. case PL:
  2180. doit = !NFLAG;
  2181. break;
  2182. case CS:
  2183. doit = CFLAG;
  2184. break;
  2185. case CC:
  2186. doit = !CFLAG;
  2187. break;
  2188. case HI:
  2189. doit = (CFLAG && !ZFLAG);
  2190. break;
  2191. case LS:
  2192. doit = (!CFLAG || ZFLAG);
  2193. break;
  2194. case GE:
  2195. doit = ((!NFLAG && !VFLAG) || (NFLAG && VFLAG));
  2196. break;
  2197. case LT:
  2198. doit = ((NFLAG && !VFLAG) || (!NFLAG && VFLAG));
  2199. break;
  2200. case GT:
  2201. doit = ((!NFLAG && !VFLAG && !ZFLAG)
  2202. || (NFLAG && VFLAG && !ZFLAG));
  2203. break;
  2204. case LE:
  2205. doit = ((NFLAG && !VFLAG) || (!NFLAG && VFLAG)) || ZFLAG;
  2206. break;
  2207. }
  2208. if (doit)
  2209. {
  2210. state->Reg[15] = (pc + 4
  2211. + (((tinstr & 0x7F) << 1)
  2212. | ((tinstr & (1 << 7)) ? 0xFFFFFF00 : 0)));
  2213. FLUSHPIPE;
  2214. }
  2215. valid = t_branch;
  2216. }
  2217. else
  2218. /* UNDEFINED : cc=1110(AL) uses different format. */
  2219. handle_v6_thumb_insn (state, tinstr, next_instr, pc, ainstr, & valid);
  2220. break;
  2221. case 28: /* B */
  2222. /* Format 18 */
  2223. state->Reg[15] = (pc + 4
  2224. + (((tinstr & 0x3FF) << 1)
  2225. | ((tinstr & (1 << 10)) ? 0xFFFFF800 : 0)));
  2226. FLUSHPIPE;
  2227. valid = t_branch;
  2228. break;
  2229. case 29: /* UNDEFINED */
  2230. if (state->is_v6)
  2231. {
  2232. handle_v6_thumb_insn (state, tinstr, next_instr, pc, ainstr, & valid);
  2233. break;
  2234. }
  2235. if (state->is_v5)
  2236. {
  2237. if (tinstr & 1)
  2238. {
  2239. handle_v6_thumb_insn (state, tinstr, next_instr, pc, ainstr, & valid);
  2240. break;
  2241. }
  2242. /* Drop through. */
  2243. /* Format 19 */
  2244. /* There is no single ARM instruction equivalent for this
  2245. instruction. Also, it should only ever be matched with the
  2246. fmt19 "BL/BLX instruction 1" instruction. However, we do
  2247. allow the simulation of it on its own, with undefined results
  2248. if r14 is not suitably initialised. */
  2249. {
  2250. ARMword tmp = (pc + 2);
  2251. state->Reg[15] = ((state->Reg[14] + ((tinstr & 0x07FF) << 1))
  2252. & 0xFFFFFFFC);
  2253. CLEART;
  2254. state->Reg[14] = (tmp | 1);
  2255. valid = t_branch;
  2256. FLUSHPIPE;
  2257. if (trace_funcs)
  2258. fprintf (stderr, " pc changed to %x\n", state->Reg[15]);
  2259. break;
  2260. }
  2261. }
  2262. handle_v6_thumb_insn (state, tinstr, next_instr, pc, ainstr, & valid);
  2263. break;
  2264. case 30: /* BL instruction 1 */
  2265. if (state->is_v6)
  2266. {
  2267. handle_T2_insn (state, tinstr, next_instr, pc, ainstr, & valid);
  2268. break;
  2269. }
  2270. /* Format 19 */
  2271. /* There is no single ARM instruction equivalent for this Thumb
  2272. instruction. To keep the simulation simple (from the user
  2273. perspective) we check if the following instruction is the
  2274. second half of this BL, and if it is we simulate it
  2275. immediately. */
  2276. state->Reg[14] = state->Reg[15] \
  2277. + (((tinstr & 0x07FF) << 12) \
  2278. | ((tinstr & (1 << 10)) ? 0xFF800000 : 0));
  2279. valid = t_branch; /* in-case we don't have the 2nd half */
  2280. tinstr = next_instr; /* move the instruction down */
  2281. pc += 2; /* point the pc at the 2nd half */
  2282. if (((tinstr & 0xF800) >> 11) != 31)
  2283. {
  2284. if (((tinstr & 0xF800) >> 11) == 29)
  2285. {
  2286. ARMword tmp = (pc + 2);
  2287. state->Reg[15] = ((state->Reg[14]
  2288. + ((tinstr & 0x07FE) << 1))
  2289. & 0xFFFFFFFC);
  2290. CLEART;
  2291. state->Reg[14] = (tmp | 1);
  2292. valid = t_branch;
  2293. FLUSHPIPE;
  2294. }
  2295. else
  2296. /* Exit, since not correct instruction. */
  2297. pc -= 2;
  2298. break;
  2299. }
  2300. /* else we fall through to process the second half of the BL */
  2301. pc += 2; /* point the pc at the 2nd half */
  2302. case 31: /* BL instruction 2 */
  2303. if (state->is_v6)
  2304. {
  2305. handle_T2_insn (state, old_tinstr, next_instr, pc, ainstr, & valid);
  2306. break;
  2307. }
  2308. /* Format 19 */
  2309. /* There is no single ARM instruction equivalent for this
  2310. instruction. Also, it should only ever be matched with the
  2311. fmt19 "BL instruction 1" instruction. However, we do allow
  2312. the simulation of it on its own, with undefined results if
  2313. r14 is not suitably initialised. */
  2314. {
  2315. ARMword tmp = pc;
  2316. state->Reg[15] = (state->Reg[14] + ((tinstr & 0x07FF) << 1));
  2317. state->Reg[14] = (tmp | 1);
  2318. valid = t_branch;
  2319. FLUSHPIPE;
  2320. }
  2321. break;
  2322. }
  2323. if (trace && valid != t_decoded)
  2324. fprintf (stderr, "\n");
  2325. return valid;
  2326. }