vfp.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. armvfp.c - ARM VFPv3 emulation unit
  3. Copyright (C) 2003 Skyeye Develop Group
  4. for help please send mail to <skyeye-developer@lists.gro.clinux.org>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. /* Note: this file handles interface with arm core and vfp registers */
  18. /* Opens debug for classic interpreter only */
  19. //#define DEBUG
  20. #include "common/common.h"
  21. #include "core/arm/skyeye_common/armdefs.h"
  22. #include "core/arm/skyeye_common/vfp/vfp.h"
  23. #define DEBUG DBG
  24. //ARMul_State* persistent_state; /* function calls from SoftFloat lib don't have an access to ARMul_state. */
  25. unsigned
  26. VFPInit (ARMul_State *state)
  27. {
  28. state->VFP[VFP_OFFSET(VFP_FPSID)] = VFP_FPSID_IMPLMEN<<24 | VFP_FPSID_SW<<23 | VFP_FPSID_SUBARCH<<16 |
  29. VFP_FPSID_PARTNUM<<8 | VFP_FPSID_VARIANT<<4 | VFP_FPSID_REVISION;
  30. state->VFP[VFP_OFFSET(VFP_FPEXC)] = 0;
  31. state->VFP[VFP_OFFSET(VFP_FPSCR)] = 0;
  32. //persistent_state = state;
  33. /* Reset only specify VFP_FPEXC_EN = '0' */
  34. return 0;
  35. }
  36. unsigned
  37. VFPMRC (ARMul_State * state, unsigned type, u32 instr, u32 * value)
  38. {
  39. /* MRC<c> <coproc>,<opc1>,<Rt>,<CRn>,<CRm>{,<opc2>} */
  40. int CoProc = BITS (8, 11); /* 10 or 11 */
  41. int OPC_1 = BITS (21, 23);
  42. int Rt = BITS (12, 15);
  43. int CRn = BITS (16, 19);
  44. int CRm = BITS (0, 3);
  45. int OPC_2 = BITS (5, 7);
  46. /* TODO check access permission */
  47. /* CRn/opc1 CRm/opc2 */
  48. if (CoProc == 10 || CoProc == 11) {
  49. #define VFP_MRC_TRANS
  50. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  51. #undef VFP_MRC_TRANS
  52. }
  53. DEBUG("Can't identify %x, CoProc %x, OPC_1 %x, Rt %x, CRn %x, CRm %x, OPC_2 %x\n",
  54. instr, CoProc, OPC_1, Rt, CRn, CRm, OPC_2);
  55. return ARMul_CANT;
  56. }
  57. unsigned
  58. VFPMCR (ARMul_State * state, unsigned type, u32 instr, u32 value)
  59. {
  60. /* MCR<c> <coproc>,<opc1>,<Rt>,<CRn>,<CRm>{,<opc2>} */
  61. int CoProc = BITS (8, 11); /* 10 or 11 */
  62. int OPC_1 = BITS (21, 23);
  63. int Rt = BITS (12, 15);
  64. int CRn = BITS (16, 19);
  65. int CRm = BITS (0, 3);
  66. int OPC_2 = BITS (5, 7);
  67. /* TODO check access permission */
  68. /* CRn/opc1 CRm/opc2 */
  69. if (CoProc == 10 || CoProc == 11) {
  70. #define VFP_MCR_TRANS
  71. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  72. #undef VFP_MCR_TRANS
  73. }
  74. DEBUG("Can't identify %x, CoProc %x, OPC_1 %x, Rt %x, CRn %x, CRm %x, OPC_2 %x\n",
  75. instr, CoProc, OPC_1, Rt, CRn, CRm, OPC_2);
  76. return ARMul_CANT;
  77. }
  78. unsigned
  79. VFPMRRC (ARMul_State * state, unsigned type, u32 instr, u32 * value1, u32 * value2)
  80. {
  81. /* MCRR<c> <coproc>,<opc1>,<Rt>,<Rt2>,<CRm> */
  82. int CoProc = BITS (8, 11); /* 10 or 11 */
  83. int OPC_1 = BITS (4, 7);
  84. int Rt = BITS (12, 15);
  85. int Rt2 = BITS (16, 19);
  86. int CRm = BITS (0, 3);
  87. if (CoProc == 10 || CoProc == 11) {
  88. #define VFP_MRRC_TRANS
  89. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  90. #undef VFP_MRRC_TRANS
  91. }
  92. DEBUG("Can't identify %x, CoProc %x, OPC_1 %x, Rt %x, Rt2 %x, CRm %x\n",
  93. instr, CoProc, OPC_1, Rt, Rt2, CRm);
  94. return ARMul_CANT;
  95. }
  96. unsigned
  97. VFPMCRR (ARMul_State * state, unsigned type, u32 instr, u32 value1, u32 value2)
  98. {
  99. /* MCRR<c> <coproc>,<opc1>,<Rt>,<Rt2>,<CRm> */
  100. int CoProc = BITS (8, 11); /* 10 or 11 */
  101. int OPC_1 = BITS (4, 7);
  102. int Rt = BITS (12, 15);
  103. int Rt2 = BITS (16, 19);
  104. int CRm = BITS (0, 3);
  105. /* TODO check access permission */
  106. /* CRn/opc1 CRm/opc2 */
  107. if (CoProc == 11 || CoProc == 10) {
  108. #define VFP_MCRR_TRANS
  109. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  110. #undef VFP_MCRR_TRANS
  111. }
  112. DEBUG("Can't identify %x, CoProc %x, OPC_1 %x, Rt %x, Rt2 %x, CRm %x\n",
  113. instr, CoProc, OPC_1, Rt, Rt2, CRm);
  114. return ARMul_CANT;
  115. }
  116. unsigned
  117. VFPSTC (ARMul_State * state, unsigned type, u32 instr, u32 * value)
  118. {
  119. /* STC{L}<c> <coproc>,<CRd>,[<Rn>],<option> */
  120. int CoProc = BITS (8, 11); /* 10 or 11 */
  121. int CRd = BITS (12, 15);
  122. int Rn = BITS (16, 19);
  123. int imm8 = BITS (0, 7);
  124. int P = BIT(24);
  125. int U = BIT(23);
  126. int D = BIT(22);
  127. int W = BIT(21);
  128. /* TODO check access permission */
  129. /* VSTM */
  130. if ( (P|U|D|W) == 0 ) {
  131. DEBUG("In %s, UNDEFINED\n", __FUNCTION__);
  132. exit(-1);
  133. }
  134. if (CoProc == 10 || CoProc == 11) {
  135. #if 1
  136. if (P == 0 && U == 0 && W == 0) {
  137. DEBUG("VSTM Related encodings\n");
  138. exit(-1);
  139. }
  140. if (P == U && W == 1) {
  141. DEBUG("UNDEFINED\n");
  142. exit(-1);
  143. }
  144. #endif
  145. #define VFP_STC_TRANS
  146. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  147. #undef VFP_STC_TRANS
  148. }
  149. DEBUG("Can't identify %x, CoProc %x, CRd %x, Rn %x, imm8 %x, P %x, U %x, D %x, W %x\n",
  150. instr, CoProc, CRd, Rn, imm8, P, U, D, W);
  151. return ARMul_CANT;
  152. }
  153. unsigned
  154. VFPLDC (ARMul_State * state, unsigned type, u32 instr, u32 value)
  155. {
  156. /* LDC{L}<c> <coproc>,<CRd>,[<Rn>] */
  157. int CoProc = BITS (8, 11); /* 10 or 11 */
  158. int CRd = BITS (12, 15);
  159. int Rn = BITS (16, 19);
  160. int imm8 = BITS (0, 7);
  161. int P = BIT(24);
  162. int U = BIT(23);
  163. int D = BIT(22);
  164. int W = BIT(21);
  165. /* TODO check access permission */
  166. if ( (P|U|D|W) == 0 ) {
  167. DEBUG("In %s, UNDEFINED\n", __FUNCTION__);
  168. exit(-1);
  169. }
  170. if (CoProc == 10 || CoProc == 11) {
  171. #define VFP_LDC_TRANS
  172. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  173. #undef VFP_LDC_TRANS
  174. }
  175. DEBUG("Can't identify %x, CoProc %x, CRd %x, Rn %x, imm8 %x, P %x, U %x, D %x, W %x\n",
  176. instr, CoProc, CRd, Rn, imm8, P, U, D, W);
  177. return ARMul_CANT;
  178. }
  179. unsigned
  180. VFPCDP (ARMul_State * state, unsigned type, u32 instr)
  181. {
  182. /* CDP<c> <coproc>,<opc1>,<CRd>,<CRn>,<CRm>,<opc2> */
  183. int CoProc = BITS (8, 11); /* 10 or 11 */
  184. int OPC_1 = BITS (20, 23);
  185. int CRd = BITS (12, 15);
  186. int CRn = BITS (16, 19);
  187. int CRm = BITS (0, 3);
  188. int OPC_2 = BITS (5, 7);
  189. //ichfly
  190. /*if ((instr & 0x0FBF0FD0) == 0x0EB70AC0) //vcvt.f64.f32 d8, s16 (s is bit 0-3 and LSB bit 22) (d is bit 12 - 15 MSB is Bit 6)
  191. {
  192. struct vfp_double vdd;
  193. struct vfp_single vsd;
  194. int dn = BITS(12, 15) + (BIT(22) << 4);
  195. int sd = (BITS(0, 3) << 1) + BIT(5);
  196. s32 n = vfp_get_float(state, sd);
  197. vfp_single_unpack(&vsd, n);
  198. if (vsd.exponent & 0x80)
  199. {
  200. vdd.exponent = (vsd.exponent&~0x80) | 0x400;
  201. }
  202. else
  203. {
  204. vdd.exponent = vsd.exponent | 0x380;
  205. }
  206. vdd.sign = vsd.sign;
  207. vdd.significand = (u64)(vsd.significand & ~0xC0000000) << 32; // I have no idea why but the 2 uppern bits are not from the significand
  208. vfp_put_double(state, vfp_double_pack(&vdd), dn);
  209. return ARMul_DONE;
  210. }
  211. if ((instr & 0x0FBF0FD0) == 0x0EB70BC0) //vcvt.f32.f64 s15, d6
  212. {
  213. struct vfp_double vdd;
  214. struct vfp_single vsd;
  215. int sd = BITS(0, 3) + (BIT(5) << 4);
  216. int dn = (BITS(12, 15) << 1) + BIT(22);
  217. vfp_double_unpack(&vdd, vfp_get_double(state, sd));
  218. if (vdd.exponent & 0x400) //todo if the exponent is to low or to high for this convert
  219. {
  220. vsd.exponent = (vdd.exponent) | 0x80;
  221. }
  222. else
  223. {
  224. vsd.exponent = vdd.exponent & ~0x80;
  225. }
  226. vsd.exponent &= 0xFF;
  227. // vsd.exponent = vdd.exponent >> 3;
  228. vsd.sign = vdd.sign;
  229. vsd.significand = ((u64)(vdd.significand ) >> 32)& ~0xC0000000;
  230. vfp_put_float(state, vfp_single_pack(&vsd), dn);
  231. return ARMul_DONE;
  232. }*/
  233. /* TODO check access permission */
  234. /* CRn/opc1 CRm/opc2 */
  235. if (CoProc == 10 || CoProc == 11) {
  236. #define VFP_CDP_TRANS
  237. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  238. #undef VFP_CDP_TRANS
  239. int exceptions = 0;
  240. if (CoProc == 10)
  241. exceptions = vfp_single_cpdo(state, instr, state->VFP[VFP_OFFSET(VFP_FPSCR)]);
  242. else
  243. exceptions = vfp_double_cpdo(state, instr, state->VFP[VFP_OFFSET(VFP_FPSCR)]);
  244. vfp_raise_exceptions(state, exceptions, instr, state->VFP[VFP_OFFSET(VFP_FPSCR)]);
  245. return ARMul_DONE;
  246. }
  247. DEBUG("Can't identify %x\n", instr);
  248. return ARMul_CANT;
  249. }
  250. /* ----------- MRC ------------ */
  251. #define VFP_MRC_IMPL
  252. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  253. #undef VFP_MRC_IMPL
  254. #define VFP_MRRC_IMPL
  255. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  256. #undef VFP_MRRC_IMPL
  257. /* ----------- MCR ------------ */
  258. #define VFP_MCR_IMPL
  259. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  260. #undef VFP_MCR_IMPL
  261. #define VFP_MCRR_IMPL
  262. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  263. #undef VFP_MCRR_IMPL
  264. /* Memory operation are not inlined, as old Interpreter and Fast interpreter
  265. don't have the same memory operation interface.
  266. Old interpreter framework does one access to coprocessor per data, and
  267. handles already data write, as well as address computation,
  268. which is not the case for Fast interpreter. Therefore, implementation
  269. of vfp instructions in old interpreter and fast interpreter are separate. */
  270. /* ----------- STC ------------ */
  271. #define VFP_STC_IMPL
  272. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  273. #undef VFP_STC_IMPL
  274. /* ----------- LDC ------------ */
  275. #define VFP_LDC_IMPL
  276. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  277. #undef VFP_LDC_IMPL
  278. /* ----------- CDP ------------ */
  279. #define VFP_CDP_IMPL
  280. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  281. #undef VFP_CDP_IMPL
  282. /* Miscellaneous functions */
  283. int32_t vfp_get_float(arm_core_t* state, unsigned int reg)
  284. {
  285. DEBUG("VFP get float: s%d=[%08x]\n", reg, state->ExtReg[reg]);
  286. return state->ExtReg[reg];
  287. }
  288. void vfp_put_float(arm_core_t* state, int32_t val, unsigned int reg)
  289. {
  290. DEBUG("VFP put float: s%d <= [%08x]\n", reg, val);
  291. state->ExtReg[reg] = val;
  292. }
  293. uint64_t vfp_get_double(arm_core_t* state, unsigned int reg)
  294. {
  295. uint64_t result;
  296. result = ((uint64_t) state->ExtReg[reg*2+1])<<32 | state->ExtReg[reg*2];
  297. DEBUG("VFP get double: s[%d-%d]=[%016llx]\n", reg*2+1, reg*2, result);
  298. return result;
  299. }
  300. void vfp_put_double(arm_core_t* state, uint64_t val, unsigned int reg)
  301. {
  302. DEBUG("VFP put double: s[%d-%d] <= [%08x-%08x]\n", reg*2+1, reg*2, (uint32_t) (val>>32), (uint32_t) (val & 0xffffffff));
  303. state->ExtReg[reg*2] = (uint32_t) (val & 0xffffffff);
  304. state->ExtReg[reg*2+1] = (uint32_t) (val>>32);
  305. }
  306. /*
  307. * Process bitmask of exception conditions. (from vfpmodule.c)
  308. */
  309. void vfp_raise_exceptions(ARMul_State* state, u32 exceptions, u32 inst, u32 fpscr)
  310. {
  311. int si_code = 0;
  312. vfpdebug("VFP: raising exceptions %08x\n", exceptions);
  313. if (exceptions == VFP_EXCEPTION_ERROR) {
  314. DEBUG("unhandled bounce %x\n", inst);
  315. exit(-1);
  316. return;
  317. }
  318. /*
  319. * If any of the status flags are set, update the FPSCR.
  320. * Comparison instructions always return at least one of
  321. * these flags set.
  322. */
  323. if (exceptions & (FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V))
  324. fpscr &= ~(FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V);
  325. fpscr |= exceptions;
  326. state->VFP[VFP_OFFSET(VFP_FPSCR)] = fpscr;
  327. }