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 <string.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
19#include "CompressedTileStorage_getData.h"
20
21static const bool sc_verbose = false;
22
23CellSpursJobContext2* g_pSpursJobContext;
24
25
26
27
28class CCompressedTileStorage_getData
29{
30public:
31 unsigned char indicesAndData[32768+4096];
32 int allocatedSize;
33 int newAllocatedSize;
34 uint32_t newIndicesPPU;
35private:
36
37 static const int INDEX_OFFSET_MASK = 0x7ffe;
38 static const int INDEX_OFFSET_SHIFT = 1;
39 static const int INDEX_TILE_MASK = 0x00ff;
40 static const int INDEX_TILE_SHIFT = 8;
41 static const int INDEX_TYPE_MASK = 0x0003;
42 static const int INDEX_TYPE_1_BIT = 0x0000;
43 static const int INDEX_TYPE_2_BIT = 0x0001;
44 static const int INDEX_TYPE_4_BIT = 0x0002;
45 static const int INDEX_TYPE_0_OR_8_BIT = 0x0003;
46 static const int INDEX_TYPE_0_BIT_FLAG = 0x0004;
47
48public:
49 CCompressedTileStorage_getData(unsigned char* idxAndData, int dataSize)
50 {
51 allocatedSize = dataSize;
52 spu_assert(allocatedSize < (int)sizeof(indicesAndData));
53 DmaData_SPU::getAndWait(indicesAndData, (uintptr_t)idxAndData, DmaData_SPU::roundUpDMASize(allocatedSize));
54 }
55 void getData(uint8_t* retArray, unsigned int retOffset);
56
57 // Get an index into the normal ordering of tiles for the java game, given a block index (0 to 511) and a tile index (0 to 63)
58 int getIndex(int block, int tile)
59 {
60 // bits for index into data is: xxxxzzzzyyyyyyy
61 // we want block(b) & tile(t) spread out as:
62 // from: ______bbbbbbbbb
63 // to: bb__bb__bbbbb__
64 //
65 // from: _________tttttt
66 // to: __tt__tt_____tt
67
68 int index = ( ( block & 0x180) << 6 ) | ( ( block & 0x060 ) << 4 ) | ( ( block & 0x01f ) << 2 );
69 index |= ( ( tile & 0x30 ) << 7) | ( ( tile & 0x0c ) << 5 ) | ( tile & 0x03 );
70
71 return index;
72 }
73
74};
75
76void CCompressedTileStorage_getData::getData(uint8_t* retArray, unsigned int retOffset)
77{
78 unsigned short *blockIndices = (unsigned short *)indicesAndData;
79 unsigned char *data = indicesAndData + 1024;
80
81 for( int i = 0; i < 512; i++ )
82 {
83 int indexType = blockIndices[i] & INDEX_TYPE_MASK;
84 if( indexType == INDEX_TYPE_0_OR_8_BIT )
85 {
86 if( blockIndices[i] & INDEX_TYPE_0_BIT_FLAG )
87 {
88 for( int j = 0; j < 64; j++ )
89 {
90 retArray[getIndex(i,j) + retOffset] = ( blockIndices[i] >> INDEX_TILE_SHIFT ) & INDEX_TILE_MASK;
91 }
92 }
93 else
94 {
95 // 8-bit reads are just directly read from the 64 long array of values stored for the block
96 unsigned char *packed = data + ( ( blockIndices[i] >> INDEX_OFFSET_SHIFT ) & INDEX_OFFSET_MASK );
97
98 for( int j = 0; j < 64; j++ )
99 {
100 retArray[getIndex(i,j) + retOffset] = packed[j];
101 }
102 }
103 }
104 else
105 {
106 // 1, 2, or 4 bits per block packed format
107
108 int bitspertile = 1 << indexType; // will be 1, 2 or 4 (from index values of 0, 1, 2)
109 int tiletypecount = 1 << bitspertile; // will be 2, 4 or 16 (from index values of 0, 1, 2)
110 int tiletypemask = tiletypecount - 1; // will be 1, 3 or 15 (from index values of 0, 1, 2)
111 int indexshift = 3 - indexType; // will be 3, 2 or 1 (from index values of 0, 1, 2)
112 int indexmask_bits = 7 >> indexType; // will be 7, 3 or 1 (from index values of 0, 1, 2)
113 int indexmask_bytes = 62 >> indexshift; // will be 7, 15 or 31 (from index values of 0, 1, 2)
114
115 unsigned char *tile_types = data + ( ( blockIndices[i] >> INDEX_OFFSET_SHIFT ) & INDEX_OFFSET_MASK );
116 unsigned char *packed = tile_types + tiletypecount;
117
118 for( int j = 0; j < 64; j++ )
119 {
120 int idx = ( j >> indexshift ) & indexmask_bytes;
121 int bit = ( j & indexmask_bits ) * bitspertile;
122 retArray[getIndex(i,j) + retOffset] = tile_types[( packed[idx] >> bit ) & tiletypemask];
123 }
124 }
125 }
126}
127
128
129
130void cellSpursJobQueueMain(CellSpursJobContext2 *pContext, CellSpursJob256 *pJob)
131{
132// CellSpursTaskId idTask = cellSpursGetTaskId();
133 unsigned int idSpu = cellSpursGetCurrentSpuId();
134
135 if(sc_verbose)
136 spu_printf("CompressedTileStorage_getData [SPU#%u] start\n", idSpu);
137
138 g_pSpursJobContext = pContext;
139
140 unsigned char* pIdxAndData = (unsigned char*)pJob->workArea.userData[0];
141 int dataSize = (int)pJob->workArea.userData[1];
142 unsigned char* pDst = (unsigned char*)pJob->workArea.userData[2];
143 unsigned int retOffset = (unsigned int)pJob->workArea.userData[3];
144
145 CCompressedTileStorage_getData c(pIdxAndData, dataSize);
146
147 unsigned char retArray[32768];
148
149 c.getData(retArray, retOffset);
150
151 DmaData_SPU::putAndWait(retArray, (uintptr_t)pDst, 32768);
152
153 if(sc_verbose)
154 spu_printf("CompressedTileStorage_getData [SPU#%u] exit\n", idSpu);
155}
156