SpuFakeDma.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #include "SpuFakeDma.h"
  14. #include <bullet/LinearMath/btScalar.h> //for btAssert
  15. //Disabling memcpy sometimes helps debugging DMA
  16. #define USE_MEMCPY 1
  17. #ifdef USE_MEMCPY
  18. #endif
  19. void* cellDmaLargeGetReadOnly(void *ls, uint64_t ea, uint32_t size, uint32_t tag, uint32_t tid, uint32_t rid)
  20. {
  21. #if defined (__SPU__) || defined (USE_LIBSPE2)
  22. cellDmaLargeGet(ls,ea,size,tag,tid,rid);
  23. return ls;
  24. #else
  25. return (void*)(ppu_address_t)ea;
  26. #endif
  27. }
  28. void* cellDmaSmallGetReadOnly(void *ls, uint64_t ea, uint32_t size, uint32_t tag, uint32_t tid, uint32_t rid)
  29. {
  30. #if defined (__SPU__) || defined (USE_LIBSPE2)
  31. mfc_get(ls,ea,size,tag,0,0);
  32. return ls;
  33. #else
  34. return (void*)(ppu_address_t)ea;
  35. #endif
  36. }
  37. void* cellDmaGetReadOnly(void *ls, uint64_t ea, uint32_t size, uint32_t tag, uint32_t tid, uint32_t rid)
  38. {
  39. #if defined (__SPU__) || defined (USE_LIBSPE2)
  40. cellDmaGet(ls,ea,size,tag,tid,rid);
  41. return ls;
  42. #else
  43. return (void*)(ppu_address_t)ea;
  44. #endif
  45. }
  46. ///this unalignedDma should not be frequently used, only for small data. It handles alignment and performs check on size (<16 bytes)
  47. int stallingUnalignedDmaSmallGet(void *ls, uint64_t ea, uint32_t size)
  48. {
  49. btAssert(size<32);
  50. ATTRIBUTE_ALIGNED16(char tmpBuffer[32]);
  51. char* localStore = (char*)ls;
  52. uint32_t i;
  53. ///make sure last 4 bits are the same, for cellDmaSmallGet
  54. uint32_t last4BitsOffset = ea & 0x0f;
  55. char* tmpTarget = tmpBuffer + last4BitsOffset;
  56. #if defined (__SPU__) || defined (USE_LIBSPE2)
  57. int remainingSize = size;
  58. //#define FORCE_cellDmaUnalignedGet 1
  59. #ifdef FORCE_cellDmaUnalignedGet
  60. cellDmaUnalignedGet(tmpTarget,ea,size,DMA_TAG(1),0,0);
  61. #else
  62. char* remainingTmpTarget = tmpTarget;
  63. uint64_t remainingEa = ea;
  64. while (remainingSize)
  65. {
  66. switch (remainingSize)
  67. {
  68. case 1:
  69. case 2:
  70. case 4:
  71. case 8:
  72. case 16:
  73. {
  74. mfc_get(remainingTmpTarget,remainingEa,remainingSize,DMA_TAG(1),0,0);
  75. remainingSize=0;
  76. break;
  77. }
  78. default:
  79. {
  80. //spu_printf("unaligned DMA with non-natural size:%d\n",remainingSize);
  81. int actualSize = 0;
  82. if (remainingSize > 16)
  83. actualSize = 16;
  84. else
  85. if (remainingSize >8)
  86. actualSize=8;
  87. else
  88. if (remainingSize >4)
  89. actualSize=4;
  90. else
  91. if (remainingSize >2)
  92. actualSize=2;
  93. mfc_get(remainingTmpTarget,remainingEa,actualSize,DMA_TAG(1),0,0);
  94. remainingSize-=actualSize;
  95. remainingTmpTarget+=actualSize;
  96. remainingEa += actualSize;
  97. }
  98. }
  99. }
  100. #endif//FORCE_cellDmaUnalignedGet
  101. #else
  102. char* mainMem = (char*)ea;
  103. //copy into final destination
  104. #ifdef USE_MEMCPY
  105. memcpy(tmpTarget,mainMem,size);
  106. #else
  107. for ( i=0;i<size;i++)
  108. {
  109. tmpTarget[i] = mainMem[i];
  110. }
  111. #endif //USE_MEMCPY
  112. #endif
  113. cellDmaWaitTagStatusAll(DMA_MASK(1));
  114. //this is slowish, perhaps memcpy on SPU is smarter?
  115. for (i=0; btLikely( i<size );i++)
  116. {
  117. localStore[i] = tmpTarget[i];
  118. }
  119. return 0;
  120. }
  121. #if defined (__SPU__) || defined (USE_LIBSPE2)
  122. #else
  123. int cellDmaLargeGet(void *ls, uint64_t ea, uint32_t size, uint32_t tag, uint32_t tid, uint32_t rid)
  124. {
  125. char* mainMem = (char*)ea;
  126. char* localStore = (char*)ls;
  127. #ifdef USE_MEMCPY
  128. memcpy(localStore,mainMem,size);
  129. #else
  130. for (uint32_t i=0;i<size;i++)
  131. {
  132. localStore[i] = mainMem[i];
  133. }
  134. #endif
  135. return 0;
  136. }
  137. int cellDmaGet(void *ls, uint64_t ea, uint32_t size, uint32_t tag, uint32_t tid, uint32_t rid)
  138. {
  139. char* mainMem = (char*)ea;
  140. char* localStore = (char*)ls;
  141. // printf("mainMem=%x, localStore=%x",mainMem,localStore);
  142. #ifdef USE_MEMCPY
  143. memcpy(localStore,mainMem,size);
  144. #else
  145. for (uint32_t i=0;i<size;i++)
  146. {
  147. localStore[i] = mainMem[i];
  148. }
  149. #endif //#ifdef USE_MEMCPY
  150. // printf(" finished\n");
  151. return 0;
  152. }
  153. int cellDmaLargePut(const void *ls, uint64_t ea, uint32_t size, uint32_t tag, uint32_t tid, uint32_t rid)
  154. {
  155. char* mainMem = (char*)ea;
  156. const char* localStore = (const char*)ls;
  157. #ifdef USE_MEMCPY
  158. memcpy(mainMem,localStore,size);
  159. #else
  160. for (uint32_t i=0;i<size;i++)
  161. {
  162. mainMem[i] = localStore[i];
  163. }
  164. #endif //#ifdef USE_MEMCPY
  165. return 0;
  166. }
  167. void cellDmaWaitTagStatusAll(int ignore)
  168. {
  169. }
  170. #endif