arm_dyncom_thumb.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // Copyright 2012 Michael Kang, 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. // We can provide simple Thumb simulation by decoding the Thumb instruction into its corresponding
  5. // ARM instruction, and using the existing ARM simulator.
  6. #include "core/arm/dyncom/arm_dyncom_thumb.h"
  7. // Decode a 16bit Thumb instruction. The instruction is in the low 16-bits of the tinstr field,
  8. // with the following Thumb instruction held in the high 16-bits. Passing in two Thumb instructions
  9. // allows easier simulation of the special dual BL instruction.
  10. tdstate thumb_translate(u32 addr, u32 instr, u32* ainstr, u32* inst_size) {
  11. tdstate valid = t_uninitialized;
  12. ARMword tinstr = instr;
  13. // The endian should be judge here
  14. if((addr & 0x3) != 0)
  15. tinstr = instr >> 16;
  16. else
  17. tinstr &= 0xFFFF;
  18. *ainstr = 0xDEADC0DE; // Debugging to catch non updates
  19. switch ((tinstr & 0xF800) >> 11) {
  20. case 0: // LSL
  21. case 1: // LSR
  22. case 2: // ASR
  23. *ainstr = 0xE1B00000 // base opcode
  24. | ((tinstr & 0x1800) >> (11 - 5)) // shift type
  25. |((tinstr & 0x07C0) << (7 - 6)) // imm5
  26. |((tinstr & 0x0038) >> 3) // Rs
  27. |((tinstr & 0x0007) << 12); // Rd
  28. break;
  29. case 3: // ADD/SUB
  30. {
  31. static const ARMword subset[4] = {
  32. 0xE0900000, // ADDS Rd,Rs,Rn
  33. 0xE0500000, // SUBS Rd,Rs,Rn
  34. 0xE2900000, // ADDS Rd,Rs,#imm3
  35. 0xE2500000 // SUBS Rd,Rs,#imm3
  36. };
  37. // It is quicker indexing into a table, than performing switch or conditionals:
  38. *ainstr = subset[(tinstr & 0x0600) >> 9] // base opcode
  39. |((tinstr & 0x01C0) >> 6) // Rn or imm3
  40. |((tinstr & 0x0038) << (16 - 3)) // Rs
  41. |((tinstr & 0x0007) << (12 - 0)); // Rd
  42. }
  43. break;
  44. case 4: // MOV
  45. case 5: // CMP
  46. case 6: // ADD
  47. case 7: // SUB
  48. {
  49. static const ARMword subset[4] = {
  50. 0xE3B00000, // MOVS Rd,#imm8
  51. 0xE3500000, // CMP Rd,#imm8
  52. 0xE2900000, // ADDS Rd,Rd,#imm8
  53. 0xE2500000, // SUBS Rd,Rd,#imm8
  54. };
  55. *ainstr = subset[(tinstr & 0x1800) >> 11] // base opcode
  56. |((tinstr & 0x00FF) >> 0) // imm8
  57. |((tinstr & 0x0700) << (16 - 8)) // Rn
  58. |((tinstr & 0x0700) << (12 - 8)); // Rd
  59. }
  60. break;
  61. case 8: // Arithmetic and high register transfers
  62. // TODO: Since the subsets for both Format 4 and Format 5 instructions are made up of
  63. // different ARM encodings, we could save the following conditional, and just have one
  64. // large subset
  65. if ((tinstr & (1 << 10)) == 0) {
  66. enum otype {
  67. t_norm,
  68. t_shift,
  69. t_neg,
  70. t_mul
  71. };
  72. static const struct {
  73. ARMword opcode;
  74. otype type;
  75. } subset[16] = {
  76. { 0xE0100000, t_norm }, // ANDS Rd,Rd,Rs
  77. { 0xE0300000, t_norm }, // EORS Rd,Rd,Rs
  78. { 0xE1B00010, t_shift }, // MOVS Rd,Rd,LSL Rs
  79. { 0xE1B00030, t_shift }, // MOVS Rd,Rd,LSR Rs
  80. { 0xE1B00050, t_shift }, // MOVS Rd,Rd,ASR Rs
  81. { 0xE0B00000, t_norm }, // ADCS Rd,Rd,Rs
  82. { 0xE0D00000, t_norm }, // SBCS Rd,Rd,Rs
  83. { 0xE1B00070, t_shift }, // MOVS Rd,Rd,ROR Rs
  84. { 0xE1100000, t_norm }, // TST Rd,Rs
  85. { 0xE2700000, t_neg }, // RSBS Rd,Rs,#0
  86. { 0xE1500000, t_norm }, // CMP Rd,Rs
  87. { 0xE1700000, t_norm }, // CMN Rd,Rs
  88. { 0xE1900000, t_norm }, // ORRS Rd,Rd,Rs
  89. { 0xE0100090, t_mul }, // MULS Rd,Rd,Rs
  90. { 0xE1D00000, t_norm }, // BICS Rd,Rd,Rs
  91. { 0xE1F00000, t_norm } // MVNS Rd,Rs
  92. };
  93. *ainstr = subset[(tinstr & 0x03C0) >> 6].opcode; // base
  94. switch (subset[(tinstr & 0x03C0) >> 6].type) {
  95. case t_norm:
  96. *ainstr |= ((tinstr & 0x0007) << 16) // Rn
  97. |((tinstr & 0x0007) << 12) // Rd
  98. |((tinstr & 0x0038) >> 3); // Rs
  99. break;
  100. case t_shift:
  101. *ainstr |= ((tinstr & 0x0007) << 12) // Rd
  102. |((tinstr & 0x0007) >> 0) // Rm
  103. |((tinstr & 0x0038) << (8 - 3)); // Rs
  104. break;
  105. case t_neg:
  106. *ainstr |= ((tinstr & 0x0007) << 12) // Rd
  107. |((tinstr & 0x0038) << (16 - 3)); // Rn
  108. break;
  109. case t_mul:
  110. *ainstr |= ((tinstr & 0x0007) << 16) // Rd
  111. |((tinstr & 0x0007) << 8) // Rs
  112. |((tinstr & 0x0038) >> 3); // Rm
  113. break;
  114. }
  115. } else {
  116. ARMword Rd = ((tinstr & 0x0007) >> 0);
  117. ARMword Rs = ((tinstr & 0x0038) >> 3);
  118. if (tinstr & (1 << 7))
  119. Rd += 8;
  120. if (tinstr & (1 << 6))
  121. Rs += 8;
  122. switch ((tinstr & 0x03C0) >> 6) {
  123. case 0x1: // ADD Rd,Rd,Hs
  124. case 0x2: // ADD Hd,Hd,Rs
  125. case 0x3: // ADD Hd,Hd,Hs
  126. *ainstr = 0xE0800000 // base
  127. | (Rd << 16) // Rn
  128. |(Rd << 12) // Rd
  129. |(Rs << 0); // Rm
  130. break;
  131. case 0x5: // CMP Rd,Hs
  132. case 0x6: // CMP Hd,Rs
  133. case 0x7: // CMP Hd,Hs
  134. *ainstr = 0xE1500000 // base
  135. | (Rd << 16) // Rn
  136. |(Rd << 12) // Rd
  137. |(Rs << 0); // Rm
  138. break;
  139. case 0x9: // MOV Rd,Hs
  140. case 0xA: // MOV Hd,Rs
  141. case 0xB: // MOV Hd,Hs
  142. *ainstr = 0xE1A00000 // base
  143. | (Rd << 16) // Rn
  144. |(Rd << 12) // Rd
  145. |(Rs << 0); // Rm
  146. break;
  147. case 0xC: // BX Rs
  148. case 0xD: // BX Hs
  149. *ainstr = 0xE12FFF10 // base
  150. | ((tinstr & 0x0078) >> 3); // Rd
  151. break;
  152. case 0x0: // UNDEFINED
  153. case 0x4: // UNDEFINED
  154. case 0x8: // UNDEFINED
  155. valid = t_undefined;
  156. break;
  157. case 0xE: // BLX
  158. case 0xF: // BLX
  159. *ainstr = 0xE1200030 // base
  160. | (Rs << 0); // Rm
  161. break;
  162. }
  163. }
  164. break;
  165. case 9: // LDR Rd,[PC,#imm8]
  166. *ainstr = 0xE59F0000 // base
  167. | ((tinstr & 0x0700) << (12 - 8)) // Rd
  168. |((tinstr & 0x00FF) << (2 - 0)); // off8
  169. break;
  170. case 10:
  171. case 11:
  172. {
  173. static const ARMword subset[8] = {
  174. 0xE7800000, // STR Rd,[Rb,Ro]
  175. 0xE18000B0, // STRH Rd,[Rb,Ro]
  176. 0xE7C00000, // STRB Rd,[Rb,Ro]
  177. 0xE19000D0, // LDRSB Rd,[Rb,Ro]
  178. 0xE7900000, // LDR Rd,[Rb,Ro]
  179. 0xE19000B0, // LDRH Rd,[Rb,Ro]
  180. 0xE7D00000, // LDRB Rd,[Rb,Ro]
  181. 0xE19000F0 // LDRSH Rd,[Rb,Ro]
  182. };
  183. *ainstr = subset[(tinstr & 0xE00) >> 9] // base
  184. |((tinstr & 0x0007) << (12 - 0)) // Rd
  185. |((tinstr & 0x0038) << (16 - 3)) // Rb
  186. |((tinstr & 0x01C0) >> 6); // Ro
  187. }
  188. break;
  189. case 12: // STR Rd,[Rb,#imm5]
  190. case 13: // LDR Rd,[Rb,#imm5]
  191. case 14: // STRB Rd,[Rb,#imm5]
  192. case 15: // LDRB Rd,[Rb,#imm5]
  193. {
  194. static const ARMword subset[4] = {
  195. 0xE5800000, // STR Rd,[Rb,#imm5]
  196. 0xE5900000, // LDR Rd,[Rb,#imm5]
  197. 0xE5C00000, // STRB Rd,[Rb,#imm5]
  198. 0xE5D00000 // LDRB Rd,[Rb,#imm5]
  199. };
  200. // The offset range defends on whether we are transferring a byte or word value:
  201. *ainstr = subset[(tinstr & 0x1800) >> 11] // base
  202. |((tinstr & 0x0007) << (12 - 0)) // Rd
  203. |((tinstr & 0x0038) << (16 - 3)) // Rb
  204. |((tinstr & 0x07C0) >> (6 - ((tinstr & (1 << 12)) ? 0 : 2))); // off5
  205. }
  206. break;
  207. case 16: // STRH Rd,[Rb,#imm5]
  208. case 17: // LDRH Rd,[Rb,#imm5]
  209. *ainstr = ((tinstr & (1 << 11)) // base
  210. ? 0xE1D000B0 // LDRH
  211. : 0xE1C000B0) // STRH
  212. |((tinstr & 0x0007) << (12 - 0)) // Rd
  213. |((tinstr & 0x0038) << (16 - 3)) // Rb
  214. |((tinstr & 0x01C0) >> (6 - 1)) // off5, low nibble
  215. |((tinstr & 0x0600) >> (9 - 8)); // off5, high nibble
  216. break;
  217. case 18: // STR Rd,[SP,#imm8]
  218. case 19: // LDR Rd,[SP,#imm8]
  219. *ainstr = ((tinstr & (1 << 11)) // base
  220. ? 0xE59D0000 // LDR
  221. : 0xE58D0000) // STR
  222. |((tinstr & 0x0700) << (12 - 8)) // Rd
  223. |((tinstr & 0x00FF) << 2); // off8
  224. break;
  225. case 20: // ADD Rd,PC,#imm8
  226. case 21: // ADD Rd,SP,#imm8
  227. if ((tinstr & (1 << 11)) == 0) {
  228. // NOTE: The PC value used here should by word aligned. We encode shift-left-by-2 in the
  229. // rotate immediate field, so no shift of off8 is needed.
  230. *ainstr = 0xE28F0F00 // base
  231. | ((tinstr & 0x0700) << (12 - 8)) // Rd
  232. |(tinstr & 0x00FF); // off8
  233. } else {
  234. // We encode shift-left-by-2 in the rotate immediate field, so no shift of off8 is needed.
  235. *ainstr = 0xE28D0F00 // base
  236. | ((tinstr & 0x0700) << (12 - 8)) // Rd
  237. |(tinstr & 0x00FF); // off8
  238. }
  239. break;
  240. case 22:
  241. case 23:
  242. if ((tinstr & 0x0F00) == 0x0000) {
  243. // NOTE: The instruction contains a shift left of 2 equivalent (implemented as ROR #30):
  244. *ainstr = ((tinstr & (1 << 7)) // base
  245. ? 0xE24DDF00 // SUB
  246. : 0xE28DDF00) // ADD
  247. |(tinstr & 0x007F); // off7
  248. } else if ((tinstr & 0x0F00) == 0x0e00) {
  249. *ainstr = 0xEF000000 | 0x180000; // base | BKPT mask
  250. } else if ((tinstr & 0x0F00) == 0x0200) {
  251. static const ARMword subset[4] = {
  252. 0xE6BF0070, // SXTH
  253. 0xE6AF0070, // SXTB
  254. 0xE6FF0070, // UXTH
  255. 0xE6EF0070, // UXTB
  256. };
  257. *ainstr = subset[BITS(tinstr, 6, 7)] // base
  258. | (BITS(tinstr, 0, 2) << 12) // Rd
  259. | BITS(tinstr, 3, 5); // Rm
  260. } else if ((tinstr & 0x0F00) == 0x0a00) {
  261. static const ARMword subset[3] = {
  262. 0xE6BF0F30, // REV
  263. 0xE6BF0FB0, // REV16
  264. 0xE6FF0FB0, // REVSH
  265. };
  266. *ainstr = subset[BITS(tinstr, 6, 7)] // base
  267. | (BITS(tinstr, 0, 2) << 12) // Rd
  268. | BITS(tinstr, 3, 5); // Rm
  269. } else {
  270. static const ARMword subset[4] = {
  271. 0xE92D0000, // STMDB sp!,{rlist}
  272. 0xE92D4000, // STMDB sp!,{rlist,lr}
  273. 0xE8BD0000, // LDMIA sp!,{rlist}
  274. 0xE8BD8000 // LDMIA sp!,{rlist,pc}
  275. };
  276. *ainstr = subset[((tinstr & (1 << 11)) >> 10) | ((tinstr & (1 << 8)) >> 8)] // base
  277. |(tinstr & 0x00FF); // mask8
  278. }
  279. break;
  280. case 24: // STMIA
  281. case 25: // LDMIA
  282. *ainstr = ((tinstr & (1 << 11)) // base
  283. ? 0xE8B00000 // LDMIA
  284. : 0xE8A00000) // STMIA
  285. |((tinstr & 0x0700) << (16 - 8)) // Rb
  286. |(tinstr & 0x00FF); // mask8
  287. break;
  288. case 26: // Bcc
  289. case 27: // Bcc/SWI
  290. if ((tinstr & 0x0F00) == 0x0F00) {
  291. // Format 17 : SWI
  292. *ainstr = 0xEF000000;
  293. // Breakpoint must be handled specially.
  294. if ((tinstr & 0x00FF) == 0x18)
  295. *ainstr |= ((tinstr & 0x00FF) << 16);
  296. // New breakpoint value. See gdb/arm-tdep.c
  297. else if ((tinstr & 0x00FF) == 0xFE)
  298. *ainstr |= 0x180000; // base |= BKPT mask
  299. else
  300. *ainstr |= (tinstr & 0x00FF);
  301. } else if ((tinstr & 0x0F00) != 0x0E00)
  302. valid = t_branch;
  303. else // UNDEFINED : cc=1110(AL) uses different format
  304. valid = t_undefined;
  305. break;
  306. case 28: // B
  307. valid = t_branch;
  308. break;
  309. case 29:
  310. if(tinstr & 0x1)
  311. valid = t_undefined;
  312. else
  313. valid = t_branch;
  314. break;
  315. case 30: // BL instruction 1
  316. // There is no single ARM instruction equivalent for this Thumb instruction. To keep the
  317. // simulation simple (from the user perspective) we check if the following instruction is
  318. // the second half of this BL, and if it is we simulate it immediately
  319. valid = t_branch;
  320. break;
  321. case 31: // BL instruction 2
  322. // There is no single ARM instruction equivalent for this instruction. Also, it should only
  323. // ever be matched with the fmt19 "BL instruction 1" instruction. However, we do allow the
  324. // simulation of it on its own, with undefined results if r14 is not suitably initialised.
  325. valid = t_branch;
  326. break;
  327. }
  328. *inst_size = 2;
  329. return valid;
  330. }