Skip to content

Commit 5d9aa2d

Browse files
authored
Merge pull request #89 from yogi313/master
Fix TIM compatibility and improve TIM validation.
2 parents 787db19 + deb1b48 commit 5d9aa2d

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

libpsn00b/psxgpu/image.c

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,27 @@ static int _next_saved_rect = 0;
2929

3030
static void _dma_transfer(const RECT *rect, uint32_t *data, int write) {
3131
size_t length = rect->w * rect->h;
32+
size_t dma_chunk_length = DMA_CHUNK_LENGTH;
3233
if (length % 2)
3334
_sdk_log("can't transfer an odd number of pixels\n");
3435

3536
length /= 2;
36-
if ((length >= DMA_CHUNK_LENGTH) && (length % DMA_CHUNK_LENGTH)) {
37-
_sdk_log("transfer data length (%d) is not a multiple of %d, rounding\n", length, DMA_CHUNK_LENGTH);
38-
length += DMA_CHUNK_LENGTH - 1;
37+
if (length >= dma_chunk_length) {
38+
uint32_t tail = length % dma_chunk_length;
39+
if(tail) {
40+
// index length w h
41+
// 0 80 5 16
42+
// 1 34 2 17
43+
// 2 36 6 6
44+
// 3 38 2 19
45+
// 4 40 5 8
46+
// 5 42 6 7
47+
// 6 44 11 4
48+
// 7 46 23 2
49+
const uint8_t dma_chunk_length_lookup[8] = {8, 1, 2, 1, 4, 1, 2, 1};
50+
dma_chunk_length = dma_chunk_length_lookup[tail % 8];
51+
_sdk_log("transfer data length / 2 (%d) is not a multiple of %d, changing dma_chunk_length %d\n", length, DMA_CHUNK_LENGTH, dma_chunk_length);
52+
}
3953
}
4054

4155
while (!(GPU_GP1 & (1 << 26)))
@@ -66,11 +80,11 @@ static void _dma_transfer(const RECT *rect, uint32_t *data, int write) {
6680
__asm__ volatile("");
6781

6882
DMA_MADR(DMA_GPU) = (uint32_t) data;
69-
if (length < DMA_CHUNK_LENGTH)
83+
if (length < dma_chunk_length)
7084
DMA_BCR(DMA_GPU) = 0x00010000 | length;
7185
else
72-
DMA_BCR(DMA_GPU) = DMA_CHUNK_LENGTH |
73-
((length / DMA_CHUNK_LENGTH) << 16);
86+
DMA_BCR(DMA_GPU) = dma_chunk_length |
87+
((length / dma_chunk_length) << 16);
7488

7589
DMA_CHCR(DMA_GPU) = 0x01000200 | write;
7690
}
@@ -169,6 +183,7 @@ int GsGetTimInfo(const uint32_t *tim, GsIMAGE *info) {
169183
if ((*(tim++) & 0xffff) != 0x0010)
170184
return 1;
171185

186+
*info = (GsIMAGE){0};
172187
info->pmode = *(tim++);
173188
if (info->pmode & 8) {
174189
const uint32_t *palette_end = tim;
@@ -179,15 +194,15 @@ int GsGetTimInfo(const uint32_t *tim, GsIMAGE *info) {
179194
info->clut = (uint32_t *) tim;
180195

181196
tim = palette_end;
182-
} else {
183-
info->clut = 0;
184197
}
185198

186-
tim++;
187-
*((uint32_t *) &(info->px)) = *(tim++);
188-
*((uint32_t *) &(info->pw)) = *(tim++);
189-
info->pixel = (uint32_t *) tim;
190-
199+
uint32_t plength = *(tim++);
200+
// bnum(4 bytes) + pos(4 bytes) + size(4 bytes)
201+
if (plength > 12) {
202+
*((uint32_t *) &(info->px)) = *(tim++);
203+
*((uint32_t *) &(info->pw)) = *(tim++);
204+
info->pixel = (uint32_t *) tim;
205+
}
191206
return 0;
192207
}
193208

@@ -197,6 +212,7 @@ int GetTimInfo(const uint32_t *tim, TIM_IMAGE *info) {
197212
if ((*(tim++) & 0xffff) != 0x0010)
198213
return 1;
199214

215+
*info = (TIM_IMAGE){0};
200216
info->mode = *(tim++);
201217
if (info->mode & 8) {
202218
const uint32_t *palette_end = tim;
@@ -206,13 +222,13 @@ int GetTimInfo(const uint32_t *tim, TIM_IMAGE *info) {
206222
info->caddr = (uint32_t *) &tim[2];
207223

208224
tim = palette_end;
209-
} else {
210-
info->caddr = 0;
211225
}
212226

213-
tim++;
214-
info->prect = (RECT *) tim;
215-
info->paddr = (uint32_t *) &tim[2];
216-
227+
uint32_t plength = *(tim++);
228+
// bnum(4 bytes) + pos(4 bytes) + size(4 bytes)
229+
if (plength > 12) {
230+
info->prect = (RECT *) tim;
231+
info->paddr = (uint32_t *) &tim[2];
232+
}
217233
return 0;
218234
}

0 commit comments

Comments
 (0)