vfp.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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. #include "common/common.h"
  19. #include "core/arm/skyeye_common/armdefs.h"
  20. #include "core/arm/skyeye_common/vfp/asm_vfp.h"
  21. #include "core/arm/skyeye_common/vfp/vfp.h"
  22. //ARMul_State* persistent_state; /* function calls from SoftFloat lib don't have an access to ARMul_state. */
  23. unsigned VFPInit(ARMul_State* state)
  24. {
  25. state->VFP[VFP_OFFSET(VFP_FPSID)] = VFP_FPSID_IMPLMEN<<24 | VFP_FPSID_SW<<23 | VFP_FPSID_SUBARCH<<16 |
  26. VFP_FPSID_PARTNUM<<8 | VFP_FPSID_VARIANT<<4 | VFP_FPSID_REVISION;
  27. state->VFP[VFP_OFFSET(VFP_FPEXC)] = 0;
  28. state->VFP[VFP_OFFSET(VFP_FPSCR)] = 0;
  29. //persistent_state = state;
  30. /* Reset only specify VFP_FPEXC_EN = '0' */
  31. return 0;
  32. }
  33. unsigned VFPMRC(ARMul_State* state, unsigned type, u32 instr, u32* value)
  34. {
  35. /* MRC<c> <coproc>,<opc1>,<Rt>,<CRn>,<CRm>{,<opc2>} */
  36. int CoProc = BITS(instr, 8, 11); /* 10 or 11 */
  37. int OPC_1 = BITS(instr, 21, 23);
  38. int Rt = BITS(instr, 12, 15);
  39. int CRn = BITS(instr, 16, 19);
  40. int CRm = BITS(instr, 0, 3);
  41. int OPC_2 = BITS(instr, 5, 7);
  42. /* TODO check access permission */
  43. /* CRn/opc1 CRm/opc2 */
  44. if (CoProc == 10 || CoProc == 11)
  45. {
  46. if (OPC_1 == 0x0 && CRm == 0 && (OPC_2 & 0x3) == 0)
  47. {
  48. /* VMOV r to s */
  49. /* Transfering Rt is not mandatory, as the value of interest is pointed by value */
  50. VMOVBRS(state, BIT(instr, 20), Rt, BIT(instr, 7)|CRn<<1, value);
  51. return ARMul_DONE;
  52. }
  53. if (OPC_1 == 0x7 && CRm == 0 && OPC_2 == 0)
  54. {
  55. VMRS(state, CRn, Rt, value);
  56. return ARMul_DONE;
  57. }
  58. }
  59. LOG_WARNING(Core_ARM11, "Can't identify %x, CoProc %x, OPC_1 %x, Rt %x, CRn %x, CRm %x, OPC_2 %x\n",
  60. instr, CoProc, OPC_1, Rt, CRn, CRm, OPC_2);
  61. return ARMul_CANT;
  62. }
  63. unsigned VFPMCR(ARMul_State* state, unsigned type, u32 instr, u32 value)
  64. {
  65. /* MCR<c> <coproc>,<opc1>,<Rt>,<CRn>,<CRm>{,<opc2>} */
  66. int CoProc = BITS(instr, 8, 11); /* 10 or 11 */
  67. int OPC_1 = BITS(instr, 21, 23);
  68. int Rt = BITS(instr, 12, 15);
  69. int CRn = BITS(instr, 16, 19);
  70. int CRm = BITS(instr, 0, 3);
  71. int OPC_2 = BITS(instr, 5, 7);
  72. /* TODO check access permission */
  73. /* CRn/opc1 CRm/opc2 */
  74. if (CoProc == 10 || CoProc == 11)
  75. {
  76. if (OPC_1 == 0x0 && CRm == 0 && (OPC_2 & 0x3) == 0)
  77. {
  78. /* VMOV s to r */
  79. /* Transfering Rt is not mandatory, as the value of interest is pointed by value */
  80. VMOVBRS(state, BIT(instr, 20), Rt, BIT(instr, 7)|CRn<<1, &value);
  81. return ARMul_DONE;
  82. }
  83. if (OPC_1 == 0x7 && CRm == 0 && OPC_2 == 0)
  84. {
  85. VMSR(state, CRn, Rt);
  86. return ARMul_DONE;
  87. }
  88. if ((OPC_1 & 0x4) == 0 && CoProc == 11 && CRm == 0)
  89. {
  90. VFP_DEBUG_UNIMPLEMENTED(VMOVBRC);
  91. return ARMul_DONE;
  92. }
  93. if (CoProc == 11 && CRm == 0)
  94. {
  95. VFP_DEBUG_UNIMPLEMENTED(VMOVBCR);
  96. return ARMul_DONE;
  97. }
  98. }
  99. LOG_WARNING(Core_ARM11, "Can't identify %x, CoProc %x, OPC_1 %x, Rt %x, CRn %x, CRm %x, OPC_2 %x\n",
  100. instr, CoProc, OPC_1, Rt, CRn, CRm, OPC_2);
  101. return ARMul_CANT;
  102. }
  103. unsigned VFPMRRC(ARMul_State* state, unsigned type, u32 instr, u32* value1, u32* value2)
  104. {
  105. /* MCRR<c> <coproc>,<opc1>,<Rt>,<Rt2>,<CRm> */
  106. int CoProc = BITS(instr, 8, 11); /* 10 or 11 */
  107. int OPC_1 = BITS(instr, 4, 7);
  108. int Rt = BITS(instr, 12, 15);
  109. int Rt2 = BITS(instr, 16, 19);
  110. int CRm = BITS(instr, 0, 3);
  111. if (CoProc == 10 || CoProc == 11)
  112. {
  113. if (CoProc == 10 && (OPC_1 & 0xD) == 1)
  114. {
  115. VMOVBRRSS(state, BIT(instr, 20), Rt, Rt2, BIT(instr, 5)<<4|CRm, value1, value2);
  116. return ARMul_DONE;
  117. }
  118. if (CoProc == 11 && (OPC_1 & 0xD) == 1)
  119. {
  120. /* Transfering Rt and Rt2 is not mandatory, as the value of interest is pointed by value1 and value2 */
  121. VMOVBRRD(state, BIT(instr, 20), Rt, Rt2, BIT(instr, 5)<<4|CRm, value1, value2);
  122. return ARMul_DONE;
  123. }
  124. }
  125. LOG_WARNING(Core_ARM11, "Can't identify %x, CoProc %x, OPC_1 %x, Rt %x, Rt2 %x, CRm %x\n",
  126. instr, CoProc, OPC_1, Rt, Rt2, CRm);
  127. return ARMul_CANT;
  128. }
  129. unsigned VFPMCRR(ARMul_State* state, unsigned type, u32 instr, u32 value1, u32 value2)
  130. {
  131. /* MCRR<c> <coproc>,<opc1>,<Rt>,<Rt2>,<CRm> */
  132. int CoProc = BITS(instr, 8, 11); /* 10 or 11 */
  133. int OPC_1 = BITS(instr, 4, 7);
  134. int Rt = BITS(instr, 12, 15);
  135. int Rt2 = BITS(instr, 16, 19);
  136. int CRm = BITS(instr, 0, 3);
  137. /* TODO check access permission */
  138. /* CRn/opc1 CRm/opc2 */
  139. if (CoProc == 11 || CoProc == 10)
  140. {
  141. if (CoProc == 10 && (OPC_1 & 0xD) == 1)
  142. {
  143. VMOVBRRSS(state, BIT(instr, 20), Rt, Rt2, BIT(instr, 5)<<4|CRm, &value1, &value2);
  144. return ARMul_DONE;
  145. }
  146. if (CoProc == 11 && (OPC_1 & 0xD) == 1)
  147. {
  148. /* Transfering Rt and Rt2 is not mandatory, as the value of interest is pointed by value1 and value2 */
  149. VMOVBRRD(state, BIT(instr, 20), Rt, Rt2, BIT(instr, 5)<<4|CRm, &value1, &value2);
  150. return ARMul_DONE;
  151. }
  152. }
  153. LOG_WARNING(Core_ARM11, "Can't identify %x, CoProc %x, OPC_1 %x, Rt %x, Rt2 %x, CRm %x\n",
  154. instr, CoProc, OPC_1, Rt, Rt2, CRm);
  155. return ARMul_CANT;
  156. }
  157. unsigned VFPSTC(ARMul_State* state, unsigned type, u32 instr, u32 * value)
  158. {
  159. /* STC{L}<c> <coproc>,<CRd>,[<Rn>],<option> */
  160. int CoProc = BITS(instr, 8, 11); /* 10 or 11 */
  161. int CRd = BITS(instr, 12, 15);
  162. int Rn = BITS(instr, 16, 19);
  163. int imm8 = BITS(instr, 0, 7);
  164. int P = BIT(instr, 24);
  165. int U = BIT(instr, 23);
  166. int D = BIT(instr, 22);
  167. int W = BIT(instr, 21);
  168. /* TODO check access permission */
  169. /* VSTM */
  170. if ( (P|U|D|W) == 0 ) {
  171. LOG_ERROR(Core_ARM11, "In %s, UNDEFINED\n", __FUNCTION__);
  172. exit(-1);
  173. }
  174. if (CoProc == 10 || CoProc == 11) {
  175. #if 1
  176. if (P == 0 && U == 0 && W == 0) {
  177. LOG_ERROR(Core_ARM11, "VSTM Related encodings\n");
  178. exit(-1);
  179. }
  180. if (P == U && W == 1) {
  181. LOG_ERROR(Core_ARM11, "UNDEFINED\n");
  182. exit(-1);
  183. }
  184. #endif
  185. if (P == 1 && W == 0)
  186. {
  187. return VSTR(state, type, instr, value);
  188. }
  189. if (P == 1 && U == 0 && W == 1 && Rn == 0xD)
  190. {
  191. return VPUSH(state, type, instr, value);
  192. }
  193. return VSTM(state, type, instr, value);
  194. }
  195. LOG_WARNING(Core_ARM11, "Can't identify %x, CoProc %x, CRd %x, Rn %x, imm8 %x, P %x, U %x, D %x, W %x\n",
  196. instr, CoProc, CRd, Rn, imm8, P, U, D, W);
  197. return ARMul_CANT;
  198. }
  199. unsigned VFPLDC(ARMul_State* state, unsigned type, u32 instr, u32 value)
  200. {
  201. /* LDC{L}<c> <coproc>,<CRd>,[<Rn>] */
  202. int CoProc = BITS(instr, 8, 11); /* 10 or 11 */
  203. int CRd = BITS(instr, 12, 15);
  204. int Rn = BITS(instr, 16, 19);
  205. int imm8 = BITS(instr, 0, 7);
  206. int P = BIT(instr, 24);
  207. int U = BIT(instr, 23);
  208. int D = BIT(instr, 22);
  209. int W = BIT(instr, 21);
  210. /* TODO check access permission */
  211. if ( (P|U|D|W) == 0 ) {
  212. LOG_ERROR(Core_ARM11, "In %s, UNDEFINED\n", __FUNCTION__);
  213. exit(-1);
  214. }
  215. if (CoProc == 10 || CoProc == 11)
  216. {
  217. if (P == 1 && W == 0)
  218. {
  219. return VLDR(state, type, instr, value);
  220. }
  221. if (P == 0 && U == 1 && W == 1 && Rn == 0xD)
  222. {
  223. return VPOP(state, type, instr, value);
  224. }
  225. return VLDM(state, type, instr, value);
  226. }
  227. LOG_WARNING(Core_ARM11, "Can't identify %x, CoProc %x, CRd %x, Rn %x, imm8 %x, P %x, U %x, D %x, W %x\n",
  228. instr, CoProc, CRd, Rn, imm8, P, U, D, W);
  229. return ARMul_CANT;
  230. }
  231. unsigned VFPCDP(ARMul_State* state, unsigned type, u32 instr)
  232. {
  233. /* CDP<c> <coproc>,<opc1>,<CRd>,<CRn>,<CRm>,<opc2> */
  234. int CoProc = BITS(instr, 8, 11); /* 10 or 11 */
  235. int OPC_1 = BITS(instr, 20, 23);
  236. int CRd = BITS(instr, 12, 15);
  237. int CRn = BITS(instr, 16, 19);
  238. int CRm = BITS(instr, 0, 3);
  239. int OPC_2 = BITS(instr, 5, 7);
  240. /* TODO check access permission */
  241. /* CRn/opc1 CRm/opc2 */
  242. if (CoProc == 10 || CoProc == 11)
  243. {
  244. if ((OPC_1 & 0xB) == 0xB && BITS(instr, 4, 7) == 0)
  245. {
  246. unsigned int single = BIT(instr, 8) == 0;
  247. unsigned int d = (single ? BITS(instr, 12,15)<<1 | BIT(instr, 22) : BITS(instr, 12,15) | BIT(instr, 22)<<4);
  248. unsigned int imm;
  249. instr = BITS(instr, 16, 19) << 4 | BITS(instr, 0, 3); // FIXME dirty workaround to get a correct imm
  250. if (single)
  251. imm = BIT(instr, 7)<<31 | (BIT(instr, 6)==0)<<30 | (BIT(instr, 6) ? 0x1f : 0)<<25 | BITS(instr, 0, 5)<<19;
  252. else
  253. imm = BIT(instr, 7)<<31 | (BIT(instr, 6)==0)<<30 | (BIT(instr, 6) ? 0xff : 0)<<22 | BITS(instr, 0, 5)<<16;
  254. VMOVI(state, single, d, imm);
  255. return ARMul_DONE;
  256. }
  257. if ((OPC_1 & 0xB) == 0xB && CRn == 0 && (OPC_2 & 0x6) == 0x2)
  258. {
  259. unsigned int single = BIT(instr, 8) == 0;
  260. unsigned int d = (single ? BITS(instr, 12,15)<<1 | BIT(instr, 22) : BITS(instr, 12,15) | BIT(instr, 22)<<4);
  261. unsigned int m = (single ? BITS(instr, 0, 3)<<1 | BIT(instr, 5) : BITS(instr, 0, 3) | BIT(instr, 5)<<4);
  262. VMOVR(state, single, d, m);
  263. return ARMul_DONE;
  264. }
  265. int exceptions = 0;
  266. if (CoProc == 10)
  267. exceptions = vfp_single_cpdo(state, instr, state->VFP[VFP_OFFSET(VFP_FPSCR)]);
  268. else
  269. exceptions = vfp_double_cpdo(state, instr, state->VFP[VFP_OFFSET(VFP_FPSCR)]);
  270. vfp_raise_exceptions(state, exceptions, instr, state->VFP[VFP_OFFSET(VFP_FPSCR)]);
  271. return ARMul_DONE;
  272. }
  273. LOG_WARNING(Core_ARM11, "Can't identify %x\n", instr);
  274. return ARMul_CANT;
  275. }
  276. /* ----------- MRC ------------ */
  277. void VMOVBRS(ARMul_State* state, ARMword to_arm, ARMword t, ARMword n, ARMword* value)
  278. {
  279. if (to_arm)
  280. {
  281. *value = state->ExtReg[n];
  282. }
  283. else
  284. {
  285. state->ExtReg[n] = *value;
  286. }
  287. }
  288. void VMRS(ARMul_State* state, ARMword reg, ARMword Rt, ARMword* value)
  289. {
  290. if (reg == 1)
  291. {
  292. if (Rt != 15)
  293. {
  294. *value = state->VFP[VFP_OFFSET(VFP_FPSCR)];
  295. }
  296. else
  297. {
  298. *value = state->VFP[VFP_OFFSET(VFP_FPSCR)] ;
  299. }
  300. }
  301. else
  302. {
  303. switch (reg)
  304. {
  305. case 0:
  306. *value = state->VFP[VFP_OFFSET(VFP_FPSID)];
  307. break;
  308. case 6:
  309. /* MVFR1, VFPv3 only ? */
  310. LOG_TRACE(Core_ARM11, "\tr%d <= MVFR1 unimplemented\n", Rt);
  311. break;
  312. case 7:
  313. /* MVFR0, VFPv3 only? */
  314. LOG_TRACE(Core_ARM11, "\tr%d <= MVFR0 unimplemented\n", Rt);
  315. break;
  316. case 8:
  317. *value = state->VFP[VFP_OFFSET(VFP_FPEXC)];
  318. break;
  319. default:
  320. LOG_TRACE(Core_ARM11, "\tSUBARCHITECTURE DEFINED\n");
  321. break;
  322. }
  323. }
  324. }
  325. void VMOVBRRD(ARMul_State* state, ARMword to_arm, ARMword t, ARMword t2, ARMword n, ARMword* value1, ARMword* value2)
  326. {
  327. if (to_arm)
  328. {
  329. *value2 = state->ExtReg[n*2+1];
  330. *value1 = state->ExtReg[n*2];
  331. }
  332. else
  333. {
  334. state->ExtReg[n*2+1] = *value2;
  335. state->ExtReg[n*2] = *value1;
  336. }
  337. }
  338. void VMOVBRRSS(ARMul_State* state, ARMword to_arm, ARMword t, ARMword t2, ARMword n, ARMword* value1, ARMword* value2)
  339. {
  340. if (to_arm)
  341. {
  342. *value1 = state->ExtReg[n+0];
  343. *value2 = state->ExtReg[n+1];
  344. }
  345. else
  346. {
  347. state->ExtReg[n+0] = *value1;
  348. state->ExtReg[n+1] = *value2;
  349. }
  350. }
  351. /* ----------- MCR ------------ */
  352. void VMSR(ARMul_State* state, ARMword reg, ARMword Rt)
  353. {
  354. if (reg == 1)
  355. {
  356. state->VFP[VFP_OFFSET(VFP_FPSCR)] = state->Reg[Rt];
  357. }
  358. else if (reg == 8)
  359. {
  360. state->VFP[VFP_OFFSET(VFP_FPEXC)] = state->Reg[Rt];
  361. }
  362. }
  363. /* Memory operation are not inlined, as old Interpreter and Fast interpreter
  364. don't have the same memory operation interface.
  365. Old interpreter framework does one access to coprocessor per data, and
  366. handles already data write, as well as address computation,
  367. which is not the case for Fast interpreter. Therefore, implementation
  368. of vfp instructions in old interpreter and fast interpreter are separate. */
  369. /* ----------- STC ------------ */
  370. int VSTR(ARMul_State* state, int type, ARMword instr, ARMword* value)
  371. {
  372. static int i = 0;
  373. static int single_reg, add, d, n, imm32, regs;
  374. if (type == ARMul_FIRST)
  375. {
  376. single_reg = BIT(instr, 8) == 0; // Double precision
  377. add = BIT(instr, 23);
  378. imm32 = BITS(instr, 0,7)<<2; // may not be used
  379. d = single_reg ? BITS(instr, 12, 15)<<1|BIT(instr, 22) : BIT(instr, 22)<<4|BITS(instr, 12, 15); /* Base register */
  380. n = BITS(instr, 16, 19); // destination register
  381. i = 0;
  382. regs = 1;
  383. return ARMul_DONE;
  384. }
  385. else if (type == ARMul_DATA)
  386. {
  387. if (single_reg)
  388. {
  389. *value = state->ExtReg[d+i];
  390. i++;
  391. if (i < regs)
  392. return ARMul_INC;
  393. else
  394. return ARMul_DONE;
  395. }
  396. else
  397. {
  398. /* FIXME Careful of endianness, may need to rework this */
  399. *value = state->ExtReg[d*2+i];
  400. i++;
  401. if (i < regs*2)
  402. return ARMul_INC;
  403. else
  404. return ARMul_DONE;
  405. }
  406. }
  407. return -1;
  408. }
  409. int VPUSH(ARMul_State* state, int type, ARMword instr, ARMword* value)
  410. {
  411. static int i = 0;
  412. static int single_regs, d, imm32, regs;
  413. if (type == ARMul_FIRST)
  414. {
  415. single_regs = BIT(instr, 8) == 0; // Single precision
  416. d = single_regs ? BITS(instr, 12, 15)<<1|BIT(instr, 22) : BIT(instr, 22)<<4|BITS(instr, 12, 15); // Base register
  417. imm32 = BITS(instr, 0,7)<<2; // may not be used
  418. regs = single_regs ? BITS(instr, 0, 7) : BITS(instr, 1, 7); // FSTMX if regs is odd
  419. state->Reg[R13] = state->Reg[R13] - imm32;
  420. i = 0;
  421. return ARMul_DONE;
  422. }
  423. else if (type == ARMul_DATA)
  424. {
  425. if (single_regs)
  426. {
  427. *value = state->ExtReg[d + i];
  428. i++;
  429. if (i < regs)
  430. return ARMul_INC;
  431. else
  432. return ARMul_DONE;
  433. }
  434. else
  435. {
  436. /* FIXME Careful of endianness, may need to rework this */
  437. *value = state->ExtReg[d*2 + i];
  438. i++;
  439. if (i < regs*2)
  440. return ARMul_INC;
  441. else
  442. return ARMul_DONE;
  443. }
  444. }
  445. return -1;
  446. }
  447. int VSTM(ARMul_State* state, int type, ARMword instr, ARMword* value)
  448. {
  449. static int i = 0;
  450. static int single_regs, add, wback, d, n, imm32, regs;
  451. if (type == ARMul_FIRST)
  452. {
  453. single_regs = BIT(instr, 8) == 0; // Single precision
  454. add = BIT(instr, 23);
  455. wback = BIT(instr, 21); // write-back
  456. d = single_regs ? BITS(instr, 12, 15)<<1|BIT(instr, 22) : BIT(instr, 22)<<4|BITS(instr, 12, 15); // Base register
  457. n = BITS(instr, 16, 19); // destination register
  458. imm32 = BITS(instr, 0,7) * 4; // may not be used
  459. regs = single_regs ? BITS(instr, 0, 7) : BITS(instr, 0, 7)>>1; // FSTMX if regs is odd
  460. if (wback) {
  461. state->Reg[n] = (add ? state->Reg[n] + imm32 : state->Reg[n] - imm32);
  462. }
  463. i = 0;
  464. return ARMul_DONE;
  465. }
  466. else if (type == ARMul_DATA)
  467. {
  468. if (single_regs)
  469. {
  470. *value = state->ExtReg[d + i];
  471. i++;
  472. if (i < regs)
  473. return ARMul_INC;
  474. else
  475. return ARMul_DONE;
  476. }
  477. else
  478. {
  479. /* FIXME Careful of endianness, may need to rework this */
  480. *value = state->ExtReg[d*2 + i];
  481. i++;
  482. if (i < regs*2)
  483. return ARMul_INC;
  484. else
  485. return ARMul_DONE;
  486. }
  487. }
  488. return -1;
  489. }
  490. /* ----------- LDC ------------ */
  491. int VPOP(ARMul_State* state, int type, ARMword instr, ARMword value)
  492. {
  493. static int i = 0;
  494. static int single_regs, d, imm32, regs;
  495. if (type == ARMul_FIRST)
  496. {
  497. single_regs = BIT(instr, 8) == 0; // Single precision
  498. d = single_regs ? BITS(instr, 12, 15)<<1|BIT(instr, 22) : BIT(instr, 22)<<4|BITS(instr, 12, 15); // Base register
  499. imm32 = BITS(instr, 0, 7)<<2; // may not be used
  500. regs = single_regs ? BITS(instr, 0, 7) : BITS(instr, 1, 7); // FLDMX if regs is odd
  501. state->Reg[R13] = state->Reg[R13] + imm32;
  502. i = 0;
  503. return ARMul_DONE;
  504. }
  505. else if (type == ARMul_TRANSFER)
  506. {
  507. return ARMul_DONE;
  508. }
  509. else if (type == ARMul_DATA)
  510. {
  511. if (single_regs)
  512. {
  513. state->ExtReg[d + i] = value;
  514. i++;
  515. if (i < regs)
  516. return ARMul_INC;
  517. else
  518. return ARMul_DONE;
  519. }
  520. else
  521. {
  522. /* FIXME Careful of endianness, may need to rework this */
  523. state->ExtReg[d*2 + i] = value;
  524. i++;
  525. if (i < regs*2)
  526. return ARMul_INC;
  527. else
  528. return ARMul_DONE;
  529. }
  530. }
  531. return -1;
  532. }
  533. int VLDR(ARMul_State* state, int type, ARMword instr, ARMword value)
  534. {
  535. static int i = 0;
  536. static int single_reg, add, d, n, imm32, regs;
  537. if (type == ARMul_FIRST)
  538. {
  539. single_reg = BIT(instr, 8) == 0; // Double precision
  540. add = BIT(instr, 23);
  541. imm32 = BITS(instr, 0, 7)<<2; // may not be used
  542. d = single_reg ? BITS(instr, 12, 15)<<1|BIT(instr, 22) : BIT(instr, 22)<<4|BITS(instr, 12, 15); // Base register
  543. n = BITS(instr, 16, 19); // destination register
  544. i = 0;
  545. regs = 1;
  546. return ARMul_DONE;
  547. }
  548. else if (type == ARMul_TRANSFER)
  549. {
  550. return ARMul_DONE;
  551. }
  552. else if (type == ARMul_DATA)
  553. {
  554. if (single_reg)
  555. {
  556. state->ExtReg[d+i] = value;
  557. i++;
  558. if (i < regs)
  559. return ARMul_INC;
  560. else
  561. return ARMul_DONE;
  562. }
  563. else
  564. {
  565. /* FIXME Careful of endianness, may need to rework this */
  566. state->ExtReg[d*2+i] = value;
  567. i++;
  568. if (i < regs*2)
  569. return ARMul_INC;
  570. else
  571. return ARMul_DONE;
  572. }
  573. }
  574. return -1;
  575. }
  576. int VLDM(ARMul_State* state, int type, ARMword instr, ARMword value)
  577. {
  578. static int i = 0;
  579. static int single_regs, add, wback, d, n, imm32, regs;
  580. if (type == ARMul_FIRST)
  581. {
  582. single_regs = BIT(instr, 8) == 0; // Single precision
  583. add = BIT(instr, 23);
  584. wback = BIT(instr, 21); // write-back
  585. d = single_regs ? BITS(instr, 12, 15)<<1|BIT(instr, 22) : BIT(instr, 22)<<4|BITS(instr, 12, 15); // Base register
  586. n = BITS(instr, 16, 19); // destination register
  587. imm32 = BITS(instr, 0, 7) * 4; // may not be used
  588. regs = single_regs ? BITS(instr, 0, 7) : BITS(instr, 0, 7)>>1; // FLDMX if regs is odd
  589. if (wback) {
  590. state->Reg[n] = (add ? state->Reg[n] + imm32 : state->Reg[n] - imm32);
  591. }
  592. i = 0;
  593. return ARMul_DONE;
  594. }
  595. else if (type == ARMul_DATA)
  596. {
  597. if (single_regs)
  598. {
  599. state->ExtReg[d + i] = value;
  600. i++;
  601. if (i < regs)
  602. return ARMul_INC;
  603. else
  604. return ARMul_DONE;
  605. }
  606. else
  607. {
  608. /* FIXME Careful of endianness, may need to rework this */
  609. state->ExtReg[d*2 + i] = value;
  610. i++;
  611. if (i < regs*2)
  612. return ARMul_INC;
  613. else
  614. return ARMul_DONE;
  615. }
  616. }
  617. return -1;
  618. }
  619. /* ----------- CDP ------------ */
  620. void VMOVI(ARMul_State* state, ARMword single, ARMword d, ARMword imm)
  621. {
  622. if (single)
  623. {
  624. state->ExtReg[d] = imm;
  625. }
  626. else
  627. {
  628. /* Check endian please */
  629. state->ExtReg[d*2+1] = imm;
  630. state->ExtReg[d*2] = 0;
  631. }
  632. }
  633. void VMOVR(ARMul_State* state, ARMword single, ARMword d, ARMword m)
  634. {
  635. if (single)
  636. {
  637. state->ExtReg[d] = state->ExtReg[m];
  638. }
  639. else
  640. {
  641. /* Check endian please */
  642. state->ExtReg[d*2+1] = state->ExtReg[m*2+1];
  643. state->ExtReg[d*2] = state->ExtReg[m*2];
  644. }
  645. }
  646. /* Miscellaneous functions */
  647. int32_t vfp_get_float(arm_core_t* state, unsigned int reg)
  648. {
  649. LOG_TRACE(Core_ARM11, "VFP get float: s%d=[%08x]\n", reg, state->ExtReg[reg]);
  650. return state->ExtReg[reg];
  651. }
  652. void vfp_put_float(arm_core_t* state, int32_t val, unsigned int reg)
  653. {
  654. LOG_TRACE(Core_ARM11, "VFP put float: s%d <= [%08x]\n", reg, val);
  655. state->ExtReg[reg] = val;
  656. }
  657. uint64_t vfp_get_double(arm_core_t* state, unsigned int reg)
  658. {
  659. uint64_t result = ((uint64_t) state->ExtReg[reg*2+1])<<32 | state->ExtReg[reg*2];
  660. LOG_TRACE(Core_ARM11, "VFP get double: s[%d-%d]=[%016llx]\n", reg * 2 + 1, reg * 2, result);
  661. return result;
  662. }
  663. void vfp_put_double(arm_core_t* state, uint64_t val, unsigned int reg)
  664. {
  665. LOG_TRACE(Core_ARM11, "VFP put double: s[%d-%d] <= [%08x-%08x]\n", reg * 2 + 1, reg * 2, (uint32_t)(val >> 32), (uint32_t)(val & 0xffffffff));
  666. state->ExtReg[reg*2] = (uint32_t) (val & 0xffffffff);
  667. state->ExtReg[reg*2+1] = (uint32_t) (val>>32);
  668. }
  669. /*
  670. * Process bitmask of exception conditions. (from vfpmodule.c)
  671. */
  672. void vfp_raise_exceptions(ARMul_State* state, u32 exceptions, u32 inst, u32 fpscr)
  673. {
  674. LOG_TRACE(Core_ARM11, "VFP: raising exceptions %08x\n", exceptions);
  675. if (exceptions == VFP_EXCEPTION_ERROR) {
  676. LOG_TRACE(Core_ARM11, "unhandled bounce %x\n", inst);
  677. exit(-1);
  678. return;
  679. }
  680. /*
  681. * If any of the status flags are set, update the FPSCR.
  682. * Comparison instructions always return at least one of
  683. * these flags set.
  684. */
  685. if (exceptions & (FPSCR_NFLAG|FPSCR_ZFLAG|FPSCR_CFLAG|FPSCR_VFLAG))
  686. fpscr &= ~(FPSCR_NFLAG|FPSCR_ZFLAG|FPSCR_CFLAG|FPSCR_VFLAG);
  687. fpscr |= exceptions;
  688. state->VFP[VFP_OFFSET(VFP_FPSCR)] = fpscr;
  689. }