vfp.cpp 23 KB

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