fpa11_cpdt.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. NetWinder Floating Point Emulator
  3. (c) Rebel.com, 1998-1999
  4. (c) Philip Blundell, 1998
  5. Direct questions, comments to Scott Bambrough <scottb@netwinder.org>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include "fpa11.h"
  19. #include "softfloat.h"
  20. #include "fpopcode.h"
  21. #include "fpmodule.h"
  22. #include "fpmodule.inl"
  23. #include <asm/uaccess.h>
  24. static inline
  25. void loadSingle(const unsigned int Fn,const unsigned int *pMem)
  26. {
  27. FPA11 *fpa11 = GET_FPA11();
  28. fpa11->fType[Fn] = typeSingle;
  29. get_user(fpa11->fpreg[Fn].fSingle, pMem);
  30. }
  31. static inline
  32. void loadDouble(const unsigned int Fn,const unsigned int *pMem)
  33. {
  34. FPA11 *fpa11 = GET_FPA11();
  35. unsigned int *p;
  36. p = (unsigned int*)&fpa11->fpreg[Fn].fDouble;
  37. fpa11->fType[Fn] = typeDouble;
  38. get_user(p[0], &pMem[1]);
  39. get_user(p[1], &pMem[0]); /* sign & exponent */
  40. }
  41. static inline
  42. void loadExtended(const unsigned int Fn,const unsigned int *pMem)
  43. {
  44. FPA11 *fpa11 = GET_FPA11();
  45. unsigned int *p;
  46. p = (unsigned int*)&fpa11->fpreg[Fn].fExtended;
  47. fpa11->fType[Fn] = typeExtended;
  48. get_user(p[0], &pMem[0]); /* sign & exponent */
  49. get_user(p[1], &pMem[2]); /* ls bits */
  50. get_user(p[2], &pMem[1]); /* ms bits */
  51. }
  52. static inline
  53. void loadMultiple(const unsigned int Fn,const unsigned int *pMem)
  54. {
  55. FPA11 *fpa11 = GET_FPA11();
  56. register unsigned int *p;
  57. unsigned long x;
  58. p = (unsigned int*)&(fpa11->fpreg[Fn]);
  59. get_user(x, &pMem[0]);
  60. fpa11->fType[Fn] = (x >> 14) & 0x00000003;
  61. switch (fpa11->fType[Fn])
  62. {
  63. case typeSingle:
  64. case typeDouble:
  65. {
  66. get_user(p[0], &pMem[2]); /* Single */
  67. get_user(p[1], &pMem[1]); /* double msw */
  68. p[2] = 0; /* empty */
  69. }
  70. break;
  71. case typeExtended:
  72. {
  73. get_user(p[1], &pMem[2]);
  74. get_user(p[2], &pMem[1]); /* msw */
  75. p[0] = (x & 0x80003fff);
  76. }
  77. break;
  78. }
  79. }
  80. static inline
  81. void storeSingle(const unsigned int Fn,unsigned int *pMem)
  82. {
  83. FPA11 *fpa11 = GET_FPA11();
  84. union
  85. {
  86. float32 f;
  87. unsigned int i[1];
  88. } val;
  89. switch (fpa11->fType[Fn])
  90. {
  91. case typeDouble:
  92. val.f = float64_to_float32(fpa11->fpreg[Fn].fDouble);
  93. break;
  94. case typeExtended:
  95. val.f = floatx80_to_float32(fpa11->fpreg[Fn].fExtended);
  96. break;
  97. default: val.f = fpa11->fpreg[Fn].fSingle;
  98. }
  99. put_user(val.i[0], pMem);
  100. }
  101. static inline
  102. void storeDouble(const unsigned int Fn,unsigned int *pMem)
  103. {
  104. FPA11 *fpa11 = GET_FPA11();
  105. union
  106. {
  107. float64 f;
  108. unsigned int i[2];
  109. } val;
  110. switch (fpa11->fType[Fn])
  111. {
  112. case typeSingle:
  113. val.f = float32_to_float64(fpa11->fpreg[Fn].fSingle);
  114. break;
  115. case typeExtended:
  116. val.f = floatx80_to_float64(fpa11->fpreg[Fn].fExtended);
  117. break;
  118. default: val.f = fpa11->fpreg[Fn].fDouble;
  119. }
  120. put_user(val.i[1], &pMem[0]); /* msw */
  121. put_user(val.i[0], &pMem[1]); /* lsw */
  122. }
  123. static inline
  124. void storeExtended(const unsigned int Fn,unsigned int *pMem)
  125. {
  126. FPA11 *fpa11 = GET_FPA11();
  127. union
  128. {
  129. floatx80 f;
  130. unsigned int i[3];
  131. } val;
  132. switch (fpa11->fType[Fn])
  133. {
  134. case typeSingle:
  135. val.f = float32_to_floatx80(fpa11->fpreg[Fn].fSingle);
  136. break;
  137. case typeDouble:
  138. val.f = float64_to_floatx80(fpa11->fpreg[Fn].fDouble);
  139. break;
  140. default: val.f = fpa11->fpreg[Fn].fExtended;
  141. }
  142. put_user(val.i[0], &pMem[0]); /* sign & exp */
  143. put_user(val.i[1], &pMem[2]);
  144. put_user(val.i[2], &pMem[1]); /* msw */
  145. }
  146. static inline
  147. void storeMultiple(const unsigned int Fn,unsigned int *pMem)
  148. {
  149. FPA11 *fpa11 = GET_FPA11();
  150. register unsigned int nType, *p;
  151. p = (unsigned int*)&(fpa11->fpreg[Fn]);
  152. nType = fpa11->fType[Fn];
  153. switch (nType)
  154. {
  155. case typeSingle:
  156. case typeDouble:
  157. {
  158. put_user(p[0], &pMem[2]); /* single */
  159. put_user(p[1], &pMem[1]); /* double msw */
  160. put_user(nType << 14, &pMem[0]);
  161. }
  162. break;
  163. case typeExtended:
  164. {
  165. put_user(p[2], &pMem[1]); /* msw */
  166. put_user(p[1], &pMem[2]);
  167. put_user((p[0] & 0x80003fff) | (nType << 14), &pMem[0]);
  168. }
  169. break;
  170. }
  171. }
  172. unsigned int PerformLDF(const unsigned int opcode)
  173. {
  174. unsigned int *pBase, *pAddress, *pFinal, nRc = 1,
  175. write_back = WRITE_BACK(opcode);
  176. //printk("PerformLDF(0x%08x), Fd = 0x%08x\n",opcode,getFd(opcode));
  177. pBase = (unsigned int*)readRegister(getRn(opcode));
  178. if (REG_PC == getRn(opcode))
  179. {
  180. pBase += 2;
  181. write_back = 0;
  182. }
  183. pFinal = pBase;
  184. if (BIT_UP_SET(opcode))
  185. pFinal += getOffset(opcode);
  186. else
  187. pFinal -= getOffset(opcode);
  188. if (PREINDEXED(opcode)) pAddress = pFinal; else pAddress = pBase;
  189. switch (opcode & MASK_TRANSFER_LENGTH)
  190. {
  191. case TRANSFER_SINGLE : loadSingle(getFd(opcode),pAddress); break;
  192. case TRANSFER_DOUBLE : loadDouble(getFd(opcode),pAddress); break;
  193. case TRANSFER_EXTENDED: loadExtended(getFd(opcode),pAddress); break;
  194. default: nRc = 0;
  195. }
  196. if (write_back) writeRegister(getRn(opcode),(unsigned int)pFinal);
  197. return nRc;
  198. }
  199. unsigned int PerformSTF(const unsigned int opcode)
  200. {
  201. unsigned int *pBase, *pAddress, *pFinal, nRc = 1,
  202. write_back = WRITE_BACK(opcode);
  203. //printk("PerformSTF(0x%08x), Fd = 0x%08x\n",opcode,getFd(opcode));
  204. SetRoundingMode(ROUND_TO_NEAREST);
  205. pBase = (unsigned int*)readRegister(getRn(opcode));
  206. if (REG_PC == getRn(opcode))
  207. {
  208. pBase += 2;
  209. write_back = 0;
  210. }
  211. pFinal = pBase;
  212. if (BIT_UP_SET(opcode))
  213. pFinal += getOffset(opcode);
  214. else
  215. pFinal -= getOffset(opcode);
  216. if (PREINDEXED(opcode)) pAddress = pFinal; else pAddress = pBase;
  217. switch (opcode & MASK_TRANSFER_LENGTH)
  218. {
  219. case TRANSFER_SINGLE : storeSingle(getFd(opcode),pAddress); break;
  220. case TRANSFER_DOUBLE : storeDouble(getFd(opcode),pAddress); break;
  221. case TRANSFER_EXTENDED: storeExtended(getFd(opcode),pAddress); break;
  222. default: nRc = 0;
  223. }
  224. if (write_back) writeRegister(getRn(opcode),(unsigned int)pFinal);
  225. return nRc;
  226. }
  227. unsigned int PerformLFM(const unsigned int opcode)
  228. {
  229. unsigned int i, Fd, *pBase, *pAddress, *pFinal,
  230. write_back = WRITE_BACK(opcode);
  231. pBase = (unsigned int*)readRegister(getRn(opcode));
  232. if (REG_PC == getRn(opcode))
  233. {
  234. pBase += 2;
  235. write_back = 0;
  236. }
  237. pFinal = pBase;
  238. if (BIT_UP_SET(opcode))
  239. pFinal += getOffset(opcode);
  240. else
  241. pFinal -= getOffset(opcode);
  242. if (PREINDEXED(opcode)) pAddress = pFinal; else pAddress = pBase;
  243. Fd = getFd(opcode);
  244. for (i=getRegisterCount(opcode);i>0;i--)
  245. {
  246. loadMultiple(Fd,pAddress);
  247. pAddress += 3; Fd++;
  248. if (Fd == 8) Fd = 0;
  249. }
  250. if (write_back) writeRegister(getRn(opcode),(unsigned int)pFinal);
  251. return 1;
  252. }
  253. unsigned int PerformSFM(const unsigned int opcode)
  254. {
  255. unsigned int i, Fd, *pBase, *pAddress, *pFinal,
  256. write_back = WRITE_BACK(opcode);
  257. pBase = (unsigned int*)readRegister(getRn(opcode));
  258. if (REG_PC == getRn(opcode))
  259. {
  260. pBase += 2;
  261. write_back = 0;
  262. }
  263. pFinal = pBase;
  264. if (BIT_UP_SET(opcode))
  265. pFinal += getOffset(opcode);
  266. else
  267. pFinal -= getOffset(opcode);
  268. if (PREINDEXED(opcode)) pAddress = pFinal; else pAddress = pBase;
  269. Fd = getFd(opcode);
  270. for (i=getRegisterCount(opcode);i>0;i--)
  271. {
  272. storeMultiple(Fd,pAddress);
  273. pAddress += 3; Fd++;
  274. if (Fd == 8) Fd = 0;
  275. }
  276. if (write_back) writeRegister(getRn(opcode),(unsigned int)pFinal);
  277. return 1;
  278. }
  279. #if 1
  280. unsigned int EmulateCPDT(const unsigned int opcode)
  281. {
  282. unsigned int nRc = 0;
  283. //printk("EmulateCPDT(0x%08x)\n",opcode);
  284. if (LDF_OP(opcode))
  285. {
  286. nRc = PerformLDF(opcode);
  287. }
  288. else if (LFM_OP(opcode))
  289. {
  290. nRc = PerformLFM(opcode);
  291. }
  292. else if (STF_OP(opcode))
  293. {
  294. nRc = PerformSTF(opcode);
  295. }
  296. else if (SFM_OP(opcode))
  297. {
  298. nRc = PerformSFM(opcode);
  299. }
  300. else
  301. {
  302. nRc = 0;
  303. }
  304. return nRc;
  305. }
  306. #endif