vfp.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. //ARMul_State* persistent_state; /* function calls from SoftFloat lib don't have an access to ARMul_state. */
  24. unsigned
  25. VFPInit (ARMul_State *state)
  26. {
  27. state->VFP[VFP_OFFSET(VFP_FPSID)] = VFP_FPSID_IMPLMEN<<24 | VFP_FPSID_SW<<23 | VFP_FPSID_SUBARCH<<16 |
  28. VFP_FPSID_PARTNUM<<8 | VFP_FPSID_VARIANT<<4 | VFP_FPSID_REVISION;
  29. state->VFP[VFP_OFFSET(VFP_FPEXC)] = 0;
  30. state->VFP[VFP_OFFSET(VFP_FPSCR)] = 0;
  31. //persistent_state = state;
  32. /* Reset only specify VFP_FPEXC_EN = '0' */
  33. return No_exp;
  34. }
  35. unsigned
  36. VFPMRC (ARMul_State * state, unsigned type, ARMword instr, ARMword * value)
  37. {
  38. /* MRC<c> <coproc>,<opc1>,<Rt>,<CRn>,<CRm>{,<opc2>} */
  39. int CoProc = BITS (8, 11); /* 10 or 11 */
  40. int OPC_1 = BITS (21, 23);
  41. int Rt = BITS (12, 15);
  42. int CRn = BITS (16, 19);
  43. int CRm = BITS (0, 3);
  44. int OPC_2 = BITS (5, 7);
  45. /* TODO check access permission */
  46. /* CRn/opc1 CRm/opc2 */
  47. if (CoProc == 10 || CoProc == 11)
  48. {
  49. #define VFP_MRC_TRANS
  50. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  51. #undef VFP_MRC_TRANS
  52. }
  53. DEBUG_LOG(ARM11, "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, ARMword instr, ARMword 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. {
  71. #define VFP_MCR_TRANS
  72. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  73. #undef VFP_MCR_TRANS
  74. }
  75. DEBUG_LOG(ARM11, "Can't identify %x, CoProc %x, OPC_1 %x, Rt %x, CRn %x, CRm %x, OPC_2 %x\n",
  76. instr, CoProc, OPC_1, Rt, CRn, CRm, OPC_2);
  77. return ARMul_CANT;
  78. }
  79. unsigned
  80. VFPMRRC (ARMul_State * state, unsigned type, ARMword instr, ARMword * value1, ARMword * value2)
  81. {
  82. /* MCRR<c> <coproc>,<opc1>,<Rt>,<Rt2>,<CRm> */
  83. int CoProc = BITS (8, 11); /* 10 or 11 */
  84. int OPC_1 = BITS (4, 7);
  85. int Rt = BITS (12, 15);
  86. int Rt2 = BITS (16, 19);
  87. int CRm = BITS (0, 3);
  88. if (CoProc == 10 || CoProc == 11)
  89. {
  90. #define VFP_MRRC_TRANS
  91. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  92. #undef VFP_MRRC_TRANS
  93. }
  94. DEBUG_LOG(ARM11, "Can't identify %x, CoProc %x, OPC_1 %x, Rt %x, Rt2 %x, CRm %x\n",
  95. instr, CoProc, OPC_1, Rt, Rt2, CRm);
  96. return ARMul_CANT;
  97. }
  98. unsigned
  99. VFPMCRR (ARMul_State * state, unsigned type, ARMword instr, ARMword value1, ARMword value2)
  100. {
  101. /* MCRR<c> <coproc>,<opc1>,<Rt>,<Rt2>,<CRm> */
  102. int CoProc = BITS (8, 11); /* 10 or 11 */
  103. int OPC_1 = BITS (4, 7);
  104. int Rt = BITS (12, 15);
  105. int Rt2 = BITS (16, 19);
  106. int CRm = BITS (0, 3);
  107. /* TODO check access permission */
  108. /* CRn/opc1 CRm/opc2 */
  109. if (CoProc == 11 || CoProc == 10)
  110. {
  111. #define VFP_MCRR_TRANS
  112. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  113. #undef VFP_MCRR_TRANS
  114. }
  115. DEBUG_LOG(ARM11, "Can't identify %x, CoProc %x, OPC_1 %x, Rt %x, Rt2 %x, CRm %x\n",
  116. instr, CoProc, OPC_1, Rt, Rt2, CRm);
  117. return ARMul_CANT;
  118. }
  119. unsigned
  120. VFPSTC (ARMul_State * state, unsigned type, ARMword instr, ARMword * value)
  121. {
  122. /* STC{L}<c> <coproc>,<CRd>,[<Rn>],<option> */
  123. int CoProc = BITS (8, 11); /* 10 or 11 */
  124. int CRd = BITS (12, 15);
  125. int Rn = BITS (16, 19);
  126. int imm8 = BITS (0, 7);
  127. int P = BIT(24);
  128. int U = BIT(23);
  129. int D = BIT(22);
  130. int W = BIT(21);
  131. /* TODO check access permission */
  132. /* VSTM */
  133. if ( (P|U|D|W) == 0 )
  134. {
  135. DEBUG_LOG(ARM11, "In %s, UNDEFINED\n", __FUNCTION__); exit(-1);
  136. }
  137. if (CoProc == 10 || CoProc == 11)
  138. {
  139. #if 1
  140. if (P == 0 && U == 0 && W == 0)
  141. {
  142. DEBUG_LOG(ARM11, "VSTM Related encodings\n"); exit(-1);
  143. }
  144. if (P == U && W == 1)
  145. {
  146. DEBUG_LOG(ARM11, "UNDEFINED\n"); exit(-1);
  147. }
  148. #endif
  149. #define VFP_STC_TRANS
  150. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  151. #undef VFP_STC_TRANS
  152. }
  153. DEBUG_LOG(ARM11, "Can't identify %x, CoProc %x, CRd %x, Rn %x, imm8 %x, P %x, U %x, D %x, W %x\n",
  154. instr, CoProc, CRd, Rn, imm8, P, U, D, W);
  155. return ARMul_CANT;
  156. }
  157. unsigned
  158. VFPLDC (ARMul_State * state, unsigned type, ARMword instr, ARMword value)
  159. {
  160. /* LDC{L}<c> <coproc>,<CRd>,[<Rn>] */
  161. int CoProc = BITS (8, 11); /* 10 or 11 */
  162. int CRd = BITS (12, 15);
  163. int Rn = BITS (16, 19);
  164. int imm8 = BITS (0, 7);
  165. int P = BIT(24);
  166. int U = BIT(23);
  167. int D = BIT(22);
  168. int W = BIT(21);
  169. /* TODO check access permission */
  170. if ( (P|U|D|W) == 0 )
  171. {
  172. DEBUG_LOG(ARM11, "In %s, UNDEFINED\n", __FUNCTION__); exit(-1);
  173. }
  174. if (CoProc == 10 || CoProc == 11)
  175. {
  176. #define VFP_LDC_TRANS
  177. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  178. #undef VFP_LDC_TRANS
  179. }
  180. DEBUG_LOG(ARM11, "Can't identify %x, CoProc %x, CRd %x, Rn %x, imm8 %x, P %x, U %x, D %x, W %x\n",
  181. instr, CoProc, CRd, Rn, imm8, P, U, D, W);
  182. return ARMul_CANT;
  183. }
  184. unsigned
  185. VFPCDP (ARMul_State * state, unsigned type, ARMword instr)
  186. {
  187. /* CDP<c> <coproc>,<opc1>,<CRd>,<CRn>,<CRm>,<opc2> */
  188. int CoProc = BITS (8, 11); /* 10 or 11 */
  189. int OPC_1 = BITS (20, 23);
  190. int CRd = BITS (12, 15);
  191. int CRn = BITS (16, 19);
  192. int CRm = BITS (0, 3);
  193. int OPC_2 = BITS (5, 7);
  194. /* TODO check access permission */
  195. /* CRn/opc1 CRm/opc2 */
  196. if (CoProc == 10 || CoProc == 11)
  197. {
  198. #define VFP_CDP_TRANS
  199. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  200. #undef VFP_CDP_TRANS
  201. int exceptions = 0;
  202. if (CoProc == 10)
  203. exceptions = vfp_single_cpdo(state, instr, state->VFP[VFP_OFFSET(VFP_FPSCR)]);
  204. else
  205. exceptions = vfp_double_cpdo(state, instr, state->VFP[VFP_OFFSET(VFP_FPSCR)]);
  206. vfp_raise_exceptions(state, exceptions, instr, state->VFP[VFP_OFFSET(VFP_FPSCR)]);
  207. return ARMul_DONE;
  208. }
  209. DEBUG_LOG(ARM11, "Can't identify %x\n", instr);
  210. return ARMul_CANT;
  211. }
  212. /* ----------- MRC ------------ */
  213. #define VFP_MRC_IMPL
  214. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  215. #undef VFP_MRC_IMPL
  216. #define VFP_MRRC_IMPL
  217. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  218. #undef VFP_MRRC_IMPL
  219. /* ----------- MCR ------------ */
  220. #define VFP_MCR_IMPL
  221. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  222. #undef VFP_MCR_IMPL
  223. #define VFP_MCRR_IMPL
  224. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  225. #undef VFP_MCRR_IMPL
  226. /* Memory operation are not inlined, as old Interpreter and Fast interpreter
  227. don't have the same memory operation interface.
  228. Old interpreter framework does one access to coprocessor per data, and
  229. handles already data write, as well as address computation,
  230. which is not the case for Fast interpreter. Therefore, implementation
  231. of vfp instructions in old interpreter and fast interpreter are separate. */
  232. /* ----------- STC ------------ */
  233. #define VFP_STC_IMPL
  234. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  235. #undef VFP_STC_IMPL
  236. /* ----------- LDC ------------ */
  237. #define VFP_LDC_IMPL
  238. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  239. #undef VFP_LDC_IMPL
  240. /* ----------- CDP ------------ */
  241. #define VFP_CDP_IMPL
  242. #include "core/arm/skyeye_common/vfp/vfpinstr.cpp"
  243. #undef VFP_CDP_IMPL
  244. /* Miscellaneous functions */
  245. int32_t vfp_get_float(arm_core_t* state, unsigned int reg)
  246. {
  247. DBG("VFP get float: s%d=[%08x]\n", reg, state->ExtReg[reg]);
  248. return state->ExtReg[reg];
  249. }
  250. void vfp_put_float(arm_core_t* state, int32_t val, unsigned int reg)
  251. {
  252. DBG("VFP put float: s%d <= [%08x]\n", reg, val);
  253. state->ExtReg[reg] = val;
  254. }
  255. uint64_t vfp_get_double(arm_core_t* state, unsigned int reg)
  256. {
  257. uint64_t result;
  258. result = ((uint64_t) state->ExtReg[reg*2+1])<<32 | state->ExtReg[reg*2];
  259. DBG("VFP get double: s[%d-%d]=[%016llx]\n", reg*2+1, reg*2, result);
  260. return result;
  261. }
  262. void vfp_put_double(arm_core_t* state, uint64_t val, unsigned int reg)
  263. {
  264. DBG("VFP put double: s[%d-%d] <= [%08x-%08x]\n", reg*2+1, reg*2, (uint32_t) (val>>32), (uint32_t) (val & 0xffffffff));
  265. state->ExtReg[reg*2] = (uint32_t) (val & 0xffffffff);
  266. state->ExtReg[reg*2+1] = (uint32_t) (val>>32);
  267. }
  268. /*
  269. * Process bitmask of exception conditions. (from vfpmodule.c)
  270. */
  271. void vfp_raise_exceptions(ARMul_State* state, u32 exceptions, u32 inst, u32 fpscr)
  272. {
  273. int si_code = 0;
  274. vfpdebug("VFP: raising exceptions %08x\n", exceptions);
  275. if (exceptions == VFP_EXCEPTION_ERROR) {
  276. DEBUG_LOG(ARM11, "unhandled bounce %x\n", inst);
  277. exit(-1);
  278. return;
  279. }
  280. /*
  281. * If any of the status flags are set, update the FPSCR.
  282. * Comparison instructions always return at least one of
  283. * these flags set.
  284. */
  285. if (exceptions & (FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V))
  286. fpscr &= ~(FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V);
  287. fpscr |= exceptions;
  288. state->VFP[VFP_OFFSET(VFP_FPSCR)] = fpscr;
  289. }