arm_dyncom_thumb.cpp 14 KB

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