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