vfp.cpp 23 KB

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