the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1/* SCE CONFIDENTIAL
2PlayStation(R)3 Programmer Tool Runtime Library 430.001
3* Copyright (C) 2007 Sony Computer Entertainment Inc.
4* All Rights Reserved.
5*/
6
7/* common headers */
8#include <stdint.h>
9#include <stdlib.h>
10#include <alloca.h>
11#include <spu_intrinsics.h>
12#include <cell/spurs.h>
13#include <spu_printf.h>
14#include <cell/dma.h>
15#include <cell/spurs/job_queue.h>
16
17#include "..\Common\DmaData.h"
18#include <vectormath/c/vectormath_aos_v.h>
19
20
21static const bool sc_verbose = false;
22CellSpursJobContext2* g_pSpursJobContext;
23
24
25class PPULoadArray
26{
27 static const int sc_cacheSize = 16384;
28 unsigned char m_localCache[sc_cacheSize];
29 unsigned char* m_pDataPPU;
30 int m_cachePos;
31 int m_cacheFilled;
32 int m_ppuPos;
33 int m_dataSize;
34
35public:
36 PPULoadArray(uintptr_t pDataPPU, int dataSize)
37 {
38 m_pDataPPU = (unsigned char*)pDataPPU;
39 m_cachePos = 0;
40 m_ppuPos = 0;
41 m_dataSize = dataSize;
42 fillCache();
43 }
44
45 unsigned char getCurrent()
46 {
47 unsigned char val = m_localCache[m_cachePos];
48 return val;
49 }
50 unsigned char getNext()
51 {
52 m_cachePos++;
53 if(m_cachePos >= sc_cacheSize)
54 loadMore();
55 unsigned char val = m_localCache[m_cachePos];
56 return val;
57 }
58
59 int getPos() { return m_ppuPos + m_cachePos; }
60
61 void loadMore()
62 {
63 m_ppuPos += sc_cacheSize;
64 fillCache();
65 }
66 void fillCache()
67 {
68 // dma data from PPU
69 // spu_printf("DMAing %d bytes from 0x%08x(SPU) to 0x%08x(PPU)\n",(int)( m_cachePos*sizeof(int)), (int)m_localCache, (int)&m_pDataPPU[m_ppuPos]);
70 int dmaSize = (m_dataSize - m_ppuPos);
71 if(dmaSize > sc_cacheSize)
72 dmaSize = sc_cacheSize;
73 dmaSize = DmaData_SPU::roundUpDMASize(dmaSize);
74 DmaData_SPU::getAndWait(m_localCache, (uintptr_t)&m_pDataPPU[m_ppuPos], dmaSize);
75 m_cachePos = 0;
76 }
77};
78
79class PPUStoreArray
80{
81 static const int sc_cacheSize = 16384;
82 unsigned char m_localCache[sc_cacheSize];
83 unsigned char* m_pDataPPU;
84 int m_cachePos;
85 int m_ppuPos;
86
87public:
88 PPUStoreArray(uintptr_t pDataPPU) { m_pDataPPU = (unsigned char*)pDataPPU; m_cachePos = 0; m_ppuPos = 0;}
89
90 void store(unsigned char val)
91 {
92 m_localCache[m_cachePos] = val;
93 m_cachePos++;
94 if(m_cachePos >= sc_cacheSize)
95 flush();
96 }
97
98 void flush()
99 {
100 if(m_cachePos > 0)
101 {
102 // dma the local cache back to PPU and start again
103 // spu_printf("DMAing %d bytes from 0x%08x(SPU) to 0x%08x(PPU)\n",(int)( m_cachePos*sizeof(int)), (int)m_localCache, (int)&m_pDataPPU[m_ppuPos]);
104 DmaData_SPU::putAndWait(m_localCache, (uintptr_t)&m_pDataPPU[m_ppuPos], DmaData_SPU::roundUpDMASize(m_cachePos));
105// if(m_ppuPos == 0)
106// spu_printf("first 4 - %d %d %d %d\n", m_localCache[0], m_localCache[1], m_localCache[2], m_localCache[3]);
107 m_ppuPos += m_cachePos;
108 m_cachePos = 0;
109 }
110 }
111 int getSize() { return m_ppuPos; }
112};
113
114
115void RLECompress(void *pPPUSrc, int srcSize, void* pPPUDst, int* pDstSize)
116{
117
118 PPULoadArray srcBuffer((uintptr_t)pPPUSrc, srcSize);
119// PPUStoreArray dstBuffer((uintptr_t)pPPUDst);
120
121 unsigned char dstBuffer[1024*100];
122 int dstPos = 0;
123 int endPos = srcSize-1;
124
125 // Compress with RLE first:
126 // 0 - 254 - encodes a single byte
127 // 255 followed by 0, 1, 2 - encodes a 1, 2, or 3 255s
128 // 255 followed by 3-255, followed by a byte - encodes a run of n + 1 bytes
129 do
130 {
131 unsigned char thisOne = srcBuffer.getCurrent();
132
133 unsigned int count = 1;
134 while( ( srcBuffer.getPos() != endPos ) && ( srcBuffer.getNext() == thisOne ) && ( count < 256 ) )
135 {
136 count++;
137 }
138
139 if( count <= 3 )
140 {
141 if( thisOne == 255 )
142 {
143 dstBuffer[dstPos++] = 255;
144 dstBuffer[dstPos++] = count - 1;
145 }
146 else
147 {
148 for( unsigned int i = 0; i < count ; i++ )
149 {
150 dstBuffer[dstPos++] = thisOne;
151 }
152 }
153 }
154 else
155 {
156 dstBuffer[dstPos++] = 255;
157 dstBuffer[dstPos++] = count - 1;
158 dstBuffer[dstPos++] = thisOne;
159 }
160 } while (srcBuffer.getPos() != endPos);
161 DmaData_SPU::wait();
162
163 DmaData_SPU::putValue32(dstPos, (uintptr_t)pDstSize);
164 int dstDmaSize = DmaData_SPU::roundUpDMASize(dstPos);
165 DmaData_SPU::putAndWait(dstBuffer, (uintptr_t)pPPUDst, dstDmaSize);
166
167}
168
169
170
171
172void cellSpursJobQueueMain(CellSpursJobContext2 *pContext, CellSpursJob256 *pJob)
173{
174 // CellSpursTaskId idTask = cellSpursGetTaskId();
175 unsigned int idSpu = cellSpursGetCurrentSpuId();
176
177 if(sc_verbose)
178 spu_printf("PerlinNoiseJob [SPU#%u] start\n", idSpu);
179
180 g_pSpursJobContext = pContext;
181 void* pPPUSrc = (void*)pJob->workArea.userData[0];
182 int srcSize = (int)pJob->workArea.userData[1];
183 void* pPPUDst = (void*)pJob->workArea.userData[2];
184 int* pDstSize = (int*)pJob->workArea.userData[3];
185//
186// spu_printf("pPPUSrc : 0x%08x\n", pPPUSrc);
187// spu_printf("srcSize : %d\n", srcSize);
188// spu_printf("pPPUDst : 0x%08x\n", pPPUDst);
189// spu_printf("pDstSize : 0x%08x\n", pDstSize);
190
191 RLECompress(pPPUSrc, srcSize, pPPUDst, pDstSize);
192
193
194 if(sc_verbose)
195 spu_printf("PerlinNoiseJob [SPU#%u] exit\n", idSpu);
196}
197