-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathblast.c
611 lines (582 loc) · 17.2 KB
/
blast.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#include <stdlib.h>
#include "utils.h"
// 802A5E10 (061650)
// just a memcpy from a0 to a3
int decode_block0(unsigned char *in, int length, unsigned char *out)
{
int ret_len = length;
length >>= 3; // a1 - gets number of dwords
while (length != 0) {
int i;
for (i = 0; i < 8; i++) {
out[i] = in[i];
}
in += 8;
out += 8;
length--;
}
return ret_len;
}
// 802A5AE0 (061320)
int decode_block1(unsigned char *in, int length, unsigned char *out)
{
unsigned short t0, t1, t3;
unsigned char *t2;
int len = 0;
while (length != 0) {
t0 = read_u16_be(in); // a0
in += 2; // a0
if ((t0 & 0x8000) == 0) {
t1 = (t0 & 0xFFC0) << 1;
t0 &= 0x3F;
t0 = t0 | t1;
write_u16_be(out, t0);
out += 2; // a3
len += 2;
length -= 2; // a1
} else {
t1 = t0 & 0x1F; // lookback length
t0 = (t0 & 0x7FFF) >> 5; // lookback offset
length -= 2; // a1
t2 = out - t0; // t2 - lookback pointer from current out
while (t1 != 0) {
t3 = read_u16_be(t2);
t2 += 2;
out += 2; // a3
len += 2;
t1 -= 1;
write_u16_be(out-2, t3);
}
}
}
return len;
}
// 802A5B90 (0613D0)
int decode_block2(unsigned char *in, int length, unsigned char *out)
{
unsigned char *look;
unsigned short t0;
unsigned int t1, t2, t3;
int len = 0;
while (length != 0) {
t0 = read_u16_be(in);
in += 2;
if ((t0 & 0x8000) == 0) { // t0 >= 0
t1 = t0 & 0x7800;
t2 = t0 & 0x0780;
t1 <<= 17; // 0x11
t2 <<= 13; // 0xD;
t1 |= t2;
t2 = t0 & 0x78;
t2 <<= 9;
t1 |= t2;
t2 = t0 & 7;
t2 <<= 5;
t1 |= t2;
write_u32_be(out, t1);
out += 4;
length -= 2;
len += 4;
} else {
t1 = t0 & 0x1f;
t0 &= 0x7FE0;
t0 >>= 4;
length -= 2;
look = out - t0; // t2
while (t1 != 0) {
t3 = read_u32_be(look); // lw t2
write_u32_be(out, t3);
look += 4; // t2
t1 -= 1;
out += 4;
len += 4;
}
}
}
return len;
}
// 802A5C5C (06149C)
int decode_block4(unsigned char *in, int length, unsigned char *out, unsigned char *lut)
{
unsigned char *look;
unsigned int t3;
unsigned short t0, t1, t2;
int len = 0;
while (length != 0) {
t0 = read_u16_be(in);
in += 2;
if ((t0 & 0x8000) == 0) {
t1 = t0 >> 8;
t2 = t1 & 0xFE;
look = lut + t2; // t2 += t4; // t4 set in proc_802A57DC: lw $t4, 0xc($a0)
t2 = read_u16_be(look);
t1 &= 1;
t2 <<= 1;
t1 |= t2;
write_u16_be(out, t1);
out += 2;
t1 = t0 & 0xFE;
look = lut + t1;
t1 = read_u16_be(look);
t0 &= 1;
length -= 2;
t1 <<= 1;
t1 |= t0;
write_u16_be(out, t1);
out += 2;
len += 4;
} else {
t1 = t0 & 0x1F;
t0 &= 0x7FE0;
t0 >>= 4;
length -= 2;
look = out - t0;
while (t1 != 0) {
t3 = read_u32_be(look);
look += 4;
t1 -= 1;
write_u32_be(out, t3);
out += 4;
len += 4;
}
}
}
return len;
}
// 802A5D34 (061574)
int decode_block5(unsigned char *in, int length, unsigned char *out, unsigned char *lut)
{
unsigned char *tmp;
unsigned short t0, t1;
unsigned int t2, t3;
int len = 0;
while (length != 0) {
t0 = read_u16_be(in);
in += 2;
if ((t0 & 0x8000) == 0) { // bltz
t1 = t0 >> 4;
t1 = t1 << 1;
tmp = t1 + lut; // t1 += t4
t1 = read_u16_be(tmp);
t0 &= 0xF;
t0 <<= 4;
t2 = t1 & 0x7C00;
t3 = t1 & 0x03E0;
t2 <<= 17; // 0x11
t3 <<= 14; // 0xe
t2 |= t3;
t3 = t1 & 0x1F;
t3 <<= 11; // 0xb
t2 |= t3;
t2 |= t0;
write_u32_be(out, t2);
out += 4;
length -= 2;
len += 4;
} else {
t1 = t0 & 0x1F;
t0 &= 0x7FE0;
t0 >>= 4;
length -= 2;
tmp = out - t0; // t2
while (t1 != 0) {
t3 = read_u32_be(tmp); //t2
tmp += 4; // t2
t1 -= 1;
write_u32_be(out, t3);
out += 4;
len += 4;
}
}
}
return len;
}
// 802A5A2C (06126C)
int decode_block3(unsigned char *in, int length, unsigned char *out)
{
unsigned short t0, t1, t3;
unsigned char *t2;
int len = 0;
while (length != 0) {
t0 = read_u16_be(in);
in += 2;
if ((0x8000 & t0) == 0) {
t1 = t0 >> 8;
t1 <<= 1;
*out = (unsigned char)t1; // sb
t1 = t0 & 0xFF;
t1 <<= 1;
*(out+1) = (unsigned char)t1; // sb
out += 2;
length -= 2;
len += 2;
} else {
t1 = t0 & 0x1F;
t0 &= 0x7FFF;
t0 >>= 5;
length -= 2;
t2 = out - t0;
while (t1 != 0) {
t3 = read_u16_be(t2);
t2 += 2;
t1 -= 1;
write_u16_be(out, t3);
out += 2;
len += 2;
}
}
}
return len;
}
// 802A5958 (061198)
int decode_block6(unsigned char *in, int length, unsigned char *out)
{
unsigned short t0, t1, t3;
int len = 0;
// .Lproc_802A5958_20: # 802A5978
while (length != 0) {
t0 = read_u16_be(in);
in += 2;
if ((0x8000 & t0) == 0) {
unsigned short t2;
t1 = t0 >> 8;
t2 = t1 & 0x38;
t1 = t1 & 0x07;
t2 <<= 2;
t1 <<= 1;
t1 |= t2;
*out = t1; // sb
t1 = t0 & 0xFF;
t2 = t1 & 0x38;
t1 = t1 & 0x07;
t2 <<= 2;
t1 <<= 1;
t1 |= t2;
*(out+1) = t1;
out += 2;
length -= 2;
len += 2;
} else {
unsigned char *t2;
t1 = t0 & 0x1F;
t0 = t0 & 0x7FFF;
t0 >>= 5;
length -= 2;
t2 = out - t0;
while (t1 != 0) {
t3 = read_u16_be(t2);
t2 += 2;
t1 -= 1;
write_u16_be(out, t3);
out += 2;
len += 2;
}
}
}
return len;
}
int blast_decode_file(char *in_filename, int type, char *out_filename, unsigned char *lut)
{
unsigned char *in_buf = NULL;
unsigned char *out_buf = NULL;
int in_len;
int write_len;
int out_len = 0;
int ret_val = 0;
in_len = read_file(in_filename, &in_buf);
if (in_len <= 0) {
return 1;
}
// estimate worst case size
out_buf = malloc(100*in_len);
if (out_buf == NULL) {
ret_val = 2;
goto free_all;
}
switch (type) {
// a0 - input buffer
// a1 - input length
// a2 - type (always unused)
// a3 - output buffer
// t4 - blocks 4 & 5 reference t4 which is set to FP
case 0: out_len = decode_block0(in_buf, in_len, out_buf); break;
case 1: out_len = decode_block1(in_buf, in_len, out_buf); break;
case 2: out_len = decode_block2(in_buf, in_len, out_buf); break;
// TODO: need to figure out where last param is set for decoders 4 and 5
case 4: out_len = decode_block4(in_buf, in_len, out_buf, lut); break;
case 5: out_len = decode_block5(in_buf, in_len, out_buf, lut); break;
case 3: out_len = decode_block3(in_buf, in_len, out_buf); break;
case 6: out_len = decode_block6(in_buf, in_len, out_buf); break;
default: ERROR("Unknown Blast type %d\n", type); break;
}
write_len = write_file(out_filename, out_buf, out_len);
if (write_len != out_len) {
ret_val = 2;
}
free_all:
if (out_buf) {
free(out_buf);
}
if (in_buf) {
free(in_buf);
}
return ret_val;
}
#ifdef BLAST_STANDALONE
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>
#include "n64graphics.h"
typedef struct
{
unsigned char *w0; // source ptr
unsigned int w4; // length
unsigned int w8; // type
unsigned int wC;
} block_t;
// 802A57DC (06101C)
// a0 is only real parameters in ROM
int proc_802A57DC(block_t *a0, unsigned char **copy, unsigned char *rom)
{
unsigned char *src;
unsigned int len;
unsigned int type;
int v0 = -1;
len = a0->w4;
*copy = malloc(100*len);
src = a0->w0;
type = a0->w8;
switch (type) {
// a0 - input buffer
// a1 - input length
// a2 - type (always unused)
// a3 - output buffer
// t4 - blocks 4 & 5 reference t4 which is set to FP
case 0: v0 = decode_block0(src, len, *copy); break;
case 1: v0 = decode_block1(src, len, *copy); break;
case 2: v0 = decode_block2(src, len, *copy); break;
// TODO: need to figure out where last param is set for decoders 4 and 5
case 4: v0 = decode_block4(src, len, *copy, &rom[0x047480]); break;
//case 5: v0 = decode_block5(src, len, *copy, &rom[0x0998E0]); break;
case 5: v0 = decode_block5(src, len, *copy, &rom[0x152970]); break;
//case 5: v0 = decode_block5(src, len, *copy, &rom[0x1E2C00]); break;
case 3: v0 = decode_block3(src, len, *copy); break;
case 6: v0 = decode_block6(src, len, *copy); break;
default: printf("Need type %d\n", type); break;
}
return v0;
}
static void convert_to_png(char *fname, unsigned short len, unsigned short type)
{
char pngname[512];
int height, width, depth;
rgba *rimg;
ia *img;
generate_filename(fname, pngname, "png");
switch (type) {
case 0:
// TODO: memcpy, no info
break;
case 1:
// guess at dims
switch (len) {
case 16: width = 4; height = 2; break;
case 512: width = 16; height = 16; break;
case 1*KB: width = 16; height = 32; break;
case 2*KB: width = 32; height = 32; break;
case 4*KB: width = 64; height = 32; break;
case 8*KB: width = 64; height = 64; break;
default: width = 32; height = len/width/2; break;
}
// RGBA16
rimg = file2rgba(fname, 0, width, height, 16);
if (rimg) rgba2png(rimg, width, height, pngname);
break;
case 2:
// guess at dims
switch (len) {
case 1*KB: width = 16; height = 16; break;
case 2*KB: width = 16; height = 32; break;
case 4*KB: width = 32; height = 32; break;
case 8*KB: width = 64; height = 32; break;
default: width = 32; height = len/width/4; break;
}
// RGBA32
rimg = file2rgba(fname, 0, width, height, 32);
if (rimg) rgba2png(rimg, width, height, pngname);
break;
case 3:
// guess at dims
switch (len) {
case 1*KB: width = 32; height = 32; break;
case 2*KB: width = 32; height = 64; break;
case 4*KB: width = 64; height = 64; break;
default: width = 32; height = len/width; break;
}
// IA8
img = file2ia(fname, 0, width, height, 8);
if (img) ia2png(img, width, height, pngname);
break;
case 4:
// guess at dims
switch (len) {
case 1*KB: width = 32; height = 16; break;
case 2*KB: width = 32; height = 32; break;
case 4*KB: width = 32; height = 64; break;
case 8*KB: width = 64; height = 64; break;
default: width = 32; height = len/width/2; break;
}
// IA16
img = file2ia(fname, 0, width, height, 16);
if (img) ia2png(img, width, height, pngname);
break;
case 5:
// guess at dims
switch (len) {
case 1*KB: width = 16; height = 16; break;
case 2*KB: width = 32; height = 16; break;
case 4*KB: width = 32; height = 32; break;
case 8*KB: width = 64; height = 32; break;
default: width = 32; height = len/width/2; break;
}
// RGBA32
rimg = file2rgba(fname, 0, width, height, 32);
if (rimg) rgba2png(rimg, width, height, pngname);
break;
case 6:
// guess at dims
depth = 8;
width = 16;
height = (len*8/depth)/width;
// IA8
img = file2ia(fname, 0, width, height, depth);
if (img) ia2png(img, width, height, pngname);
break;
}
}
int main(int argc, char *argv[])
{
#define ROM_OFFSET 0x4CE0
#define END_OFFSET 0xCCE0
block_t block;
char out_fname[512];
unsigned char *data;
long size;
int out_size;
unsigned int off;
unsigned char *out;
int width, height, depth;
char *format;
if (argc < 2) return 1;
// read in Blast Corps ROM
size = read_file(argv[1], &data);
// loop through from 0x4CE0 to 0xCCE0
for (off = ROM_OFFSET; off < END_OFFSET; off += 8) {
unsigned int start = read_u32_be(&data[off]);
unsigned short len = read_u16_be(&data[off+4]);
unsigned short type = read_u16_be(&data[off+6]);
assert(size >= start);
// TODO: there are large sections of len=0, possibly LUTs for 4 & 5?
if (len > 0) {
block.w0 = &data[start+ROM_OFFSET];
block.w4 = len;
block.w8 = type;
//printf("%X (%X) %X %d\n", start, start+ROM_OFFSET, len, type);
out_size = proc_802A57DC(&block, &out, data);
sprintf(out_fname, "%s.%06X.%d.bin",
argv[1], start, type);
//printf("writing %s: %04X -> %04X\n", out_fname, len, out_size);
depth = 0;
switch (type) {
case 0:
// TODO: memcpy, no info
break;
case 1:
// guess at dims
switch (out_size) {
case 16: width = 4; height = 2; break;
case 512: width = 16; height = 16; break;
case 1*KB: width = 16; height = 32; break;
case 2*KB: width = 32; height = 32; break;
case 4*KB: width = 64; height = 32; break;
case 8*KB: width = 64; height = 64; break;
case 3200: width = 40; height = 40; break;
default: width = 32; height = out_size/width/2; break;
}
format = "\"rgba\"";
depth = 16;
break;
case 2:
// guess at dims
switch (out_size) {
case 256: width = 8; height = 8; break;
case 512: width = 8; height = 16; break;
case 1*KB: width = 16; height = 16; break;
case 2*KB: width = 16; height = 32; break;
case 4*KB: width = 32; height = 32; break;
case 8*KB: width = 64; height = 32; break;
default: width = 32; height = out_size/width/4; break;
}
format = "\"rgba\"";
depth = 32;
break;
case 3:
// guess at dims
switch (out_size) {
case 1*KB: width = 32; height = 32; break;
case 2*KB: width = 32; height = 64; break;
case 4*KB: width = 64; height = 64; break;
default: width = 32; height = out_size/width; break;
}
format = "\"ia\"";
depth = 8;
break;
case 4:
// guess at dims
switch (out_size) {
case 1*KB: width = 16; height = 32; break;
case 2*KB: width = 32; height = 32; break;
case 4*KB: width = 32; height = 64; break;
case 8*KB: width = 64; height = 64; break;
default: width = 32; height = out_size/width/2; break;
}
format = "\"ia\"";
depth = 16;
break;
case 5:
// guess at dims
switch (out_size) {
case 1*KB: width = 16; height = 16; break;
case 2*KB: width = 32; height = 16; break;
case 4*KB: width = 32; height = 32; break;
case 8*KB: width = 64; height = 32; break;
default: width = 32; height = out_size/width/4; break;
}
format = "\"rgba\"";
depth = 32;
break;
case 6:
// guess at dims
depth = 8;
width = 16;
height = (out_size*8/depth)/width;
format = "\"ia\"";
break;
}
if (depth) {
if (width == 0 || height == 0) {
ERROR("Error: %d x %d for %X at %X type %d\n", width, height, out_size, start+ROM_OFFSET, type);
}
printf(" (0x%06X, 0x%06X, \"blast\", %d, ((0x0, %6s, %2d, %2d, %2d))),\n",
start+ROM_OFFSET, start+ROM_OFFSET+len, type, format, depth, width, height);
}
write_file(out_fname, out, out_size);
// attempt to convert to PNG
convert_to_png(out_fname, out_size, type);
}
}
free(data);
return 0;
}
#endif // BLAST_STANDALONE