-
Notifications
You must be signed in to change notification settings - Fork 3
/
mvsmakerom.c
519 lines (426 loc) · 29.7 KB
/
mvsmakerom.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
#include <stdio.h>
#include <memory.h>
#include <ctype.h>
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/stat.h> // mkdir
#include "miniz.h"
#define MIN(a,b) ((a)<(b)?(a):(b))
#define panic(s, ...) ({ fprintf(stderr, s, ##__VA_ARGS__); exit(1); })
#define strstartwith(s, prefix) !strncmp(s, prefix, strlen(prefix))
#define BIT(x,n) (((x)>>(n))&1)
#define BITSWAP8(val,B7,B6,B5,B4,B3,B2,B1,B0) \
((BIT(val,B7) << 7) | (BIT(val,B6) << 6) | (BIT(val,B5) << 5) | (BIT(val,B4) << 4) | (BIT(val,B3) << 3) | (BIT(val,B2) << 2) | (BIT(val,B1) << 1) | (BIT(val,B0) << 0))
#define BITSWAP16(val,B15,B14,B13,B12,B11,B10,B9,B8,B7,B6,B5,B4,B3,B2,B1,B0) \
((BIT(val,B15) << 15) | (BIT(val,B14) << 14) | (BIT(val,B13) << 13) | (BIT(val,B12) << 12) | (BIT(val,B11) << 11) | (BIT(val,B10) << 10) | (BIT(val, B9) << 9) | (BIT(val, B8) << 8) | (BIT(val, B7) << 7) | (BIT(val, B6) << 6) | (BIT(val, B5) << 5) | (BIT(val, B4) << 4) | (BIT(val, B3) << 3) | (BIT(val, B2) << 2) | (BIT(val, B1) << 1) | (BIT(val, B0) << 0))
#define BITSWAP24(val,B23,B22,B21,B20,B19,B18,B17,B16,B15,B14,B13,B12,B11,B10,B9,B8,B7,B6,B5,B4,B3,B2,B1,B0) \
((BIT(val,B23) << 23) | (BIT(val,B22) << 22) | (BIT(val,B21) << 21) | (BIT(val,B20) << 20) | (BIT(val,B19) << 19) | (BIT(val,B18) << 18) | (BIT(val,B17) << 17) | (BIT(val,B16) << 16) | (BIT(val,B15) << 15) | (BIT(val,B14) << 14) | (BIT(val,B13) << 13) | (BIT(val,B12) << 12) | (BIT(val,B11) << 11) | (BIT(val,B10) << 10) | (BIT(val, B9) << 9) | (BIT(val, B8) << 8) | (BIT(val, B7) << 7) | (BIT(val, B6) << 6) | (BIT(val, B5) << 5) | (BIT(val, B4) << 4) | (BIT(val, B3) << 3) | (BIT(val, B2) << 2) | (BIT(val, B1) << 1) | (BIT(val, B0) << 0))
#define BITSWAP32(val,B31,B30,B29,B28,B27,B26,B25,B24,B23,B22,B21,B20,B19,B18,B17,B16,B15,B14,B13,B12,B11,B10,B9,B8,B7,B6,B5,B4,B3,B2,B1,B0) \
((BIT(val,B31) << 31) | (BIT(val,B30) << 30) | (BIT(val,B29) << 29) | (BIT(val,B28) << 28) | (BIT(val,B27) << 27) | (BIT(val,B26) << 26) | (BIT(val,B25) << 25) | (BIT(val,B24) << 24) | (BIT(val,B23) << 23) | (BIT(val,B22) << 22) | (BIT(val,B21) << 21) | (BIT(val,B20) << 20) | (BIT(val,B19) << 19) | (BIT(val,B18) << 18) | (BIT(val,B17) << 17) | (BIT(val,B16) << 16) | (BIT(val,B15) << 15) | (BIT(val,B14) << 14) | (BIT(val,B13) << 13) | (BIT(val,B12) << 12) | (BIT(val,B11) << 11) | (BIT(val,B10) << 10) | (BIT(val, B9) << 9) | (BIT(val, B8) << 8) | (BIT(val, B7) << 7) | (BIT(val, B6) << 6) | (BIT(val, B5) << 5) | (BIT(val, B4) << 4) | (BIT(val, B3) << 3) | (BIT(val, B2) << 2) | (BIT(val, B1) << 1) | (BIT(val, B0) << 0))
enum GameId {
GAME_MSLUG = 0x0201, GAME_SAMSHO = 0x0045, GAME_SENGOKU3 = 0x0261,
GAME_S1945P = 0x0254, GAME_AOF = 0x0044, GAME_AOF3 = 0x0096,
GAME_PBOBBLEN = 0x0083
};
extern const char* game_ini[65536];
typedef struct {
char *fn[32];
int size[32];
int total_size;
} Romset;
typedef struct {
uint16_t code;
uint8_t *PROM; int prom_size;
uint8_t *CROM; int crom_size;
uint8_t *SROM; int srom_size;
} Game;
typedef struct {
uint8_t *PROM; int prom_size;
uint8_t *SROM; int srom_size;
} Bios;
void fixrom_preprocess(uint8_t *rom, int sz) {
#define NIBBLE_SWAP(v_) ({ uint8_t v = (v_); (((v)>>4) | ((v)<<4)); })
uint8_t buf[4*8];
for (int i=0; i<sz; i+=4*8) {
uint8_t *c0 = &rom[16], *c1 = &rom[24], *c2 = &rom[0], *c3 = &rom[8];
uint8_t *d = buf;
for (int j=0;j<8;j++) {
*d++ = NIBBLE_SWAP(*c0++);
*d++ = NIBBLE_SWAP(*c1++);
*d++ = NIBBLE_SWAP(*c2++);
*d++ = NIBBLE_SWAP(*c3++);
}
memcpy(rom, buf, 4*8);
rom += 4*8;
}
}
void crom_preprocess(uint8_t *rom0, int sz) {
uint8_t *buf0 = calloc(1, sz), *buf = buf0, *rom = rom0;
uint8_t *c1 = rom;
for (int i=0;i<sz;i+=8*16) {
for (int b=0;b<4;b++) {
uint8_t *dst = buf + (b&1)*64 + ((b^2)&2)*2;
for (int y=0;y<8;y++) {
for (int x=0;x<8;x+=2) {
uint8_t px4 = (c1[0] >> (x)) & 1;
uint8_t px5 = (c1[2] >> (x)) & 1;
uint8_t px6 = (c1[1] >> (x)) & 1;
uint8_t px7 = (c1[3] >> (x)) & 1;
uint8_t px0 = (c1[0] >> (x+1)) & 1;
uint8_t px1 = (c1[2] >> (x+1)) & 1;
uint8_t px2 = (c1[1] >> (x+1)) & 1;
uint8_t px3 = (c1[3] >> (x+1)) & 1;
dst[x/2] = (px7<<7)|(px6<<6)|(px5<<5)|(px4<<4)|(px3<<3)|(px2<<2)|(px1<<1)|(px0<<0);
}
c1 += 4;
dst += 8;
}
}
buf += 8*16;
}
memcpy(rom0, buf0, sz);
free(buf0);
}
typedef struct {
uint8_t type0_t03[256], type0_t12[256], type1_t03[256], type1_t12[256];
uint8_t address_8_15_xor1[256], address_8_15_xor2[256];
uint8_t address_16_23_xor1[256], address_16_23_xor2[256];
uint8_t address_0_7_xor[256];
} CMCTables;
const CMCTables cmc42_tables = {
.type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xc0, 0xd0, 0xc5, 0x79, 0x78, 0x41, 0x59, 0x21, 0x2e, 0xff, 0xc2, 0x4b, 0x38, 0x83, 0x32, 0xe6, 0xe2, 0x7f, 0x1e, 0x17, 0x58, 0x1d, 0x1a, 0xfa, 0x85, 0x82, 0x94, 0xc8, 0x72, 0x7e, 0xb7, 0xac, 0x0e, 0xfc, 0xfd, 0x16, 0x27, 0x75, 0x8d, 0xcb, 0x08, 0xfe, 0x0a, 0x02, 0x0d, 0x36, 0x11, 0x22, 0x84, 0x40, 0x34, 0x3e, 0x2d, 0x68, 0x5a, 0xa7, 0x67, 0xae, 0x87, 0x07, 0x10, 0x60, 0x14, 0x73, 0x3c, 0x51, 0x19, 0xa3, 0xb5, 0xcf, 0x13, 0xf0, 0x15, 0x4e },
.address_16_23_xor1 = { 0x00, 0x5f, 0x03, 0x52, 0xce, 0xe3, 0x7d, 0x8f, 0x6b, 0xf8, 0x20, 0xde, 0x7b, 0x7e, 0x39, 0xbe, 0xf5, 0x94, 0x18, 0x78, 0x80, 0xc9, 0x7f, 0x7a, 0x3e, 0x63, 0xf2, 0xe0, 0x4e, 0xf7, 0x87, 0x27, 0x69, 0x6c, 0xa4, 0x1d, 0x85, 0x5b, 0xe6, 0x44, 0x25, 0x0c, 0x98, 0xc7, 0x01, 0x02, 0xa3, 0x26, 0x09, 0x38, 0xdb, 0xc3, 0x1e, 0xcf, 0x23, 0x45, 0x68, 0x76, 0xd6, 0x22, 0x5d, 0x5a, 0xae, 0x16, 0x9f, 0xa2, 0xb5, 0xcd, 0x81, 0xea, 0x5e, 0xb8, 0xb9, 0x9d, 0x9c, 0x1a, 0x0f, 0xff, 0xe1, 0xe7, 0x74, 0xaa, 0xd4, 0xaf, 0xfc, 0xc6, 0x33, 0x29, 0x5c, 0xab, 0x95, 0xf0, 0x19, 0x47, 0x59, 0x67, 0xf3, 0x96, 0x60, 0x1f, 0x62, 0x92, 0xbd, 0x89, 0xee, 0x28, 0x13, 0x06, 0xfe, 0xfa,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, 0xa3, 0x03, 0x0e, 0x72, 0x26, 0x79, 0xe2, 0xb8, 0xfc, 0xaa, 0xfe, 0xb4, 0x86, 0xc8, 0xd1, 0xbc, 0x12, 0x08, 0x77, 0xeb, 0x40, 0x8d, 0x04, 0x25, 0x4d, 0x5a, 0x6a, 0x7a, 0x2e, 0x41, 0x65, 0x1c, 0x13, 0x94, 0xb2, 0x63, 0x28, 0x59, 0x5e, 0x9a, 0x30, 0x07, 0xc6, 0xbf, 0x17, 0xf5, 0x0f, 0x89, 0xf3, 0x1f, 0xea, 0x6d, 0xb3, 0xc0, 0x70, 0x47, 0xf9, 0x53, 0xf6, 0xd6, 0x54, 0xed, 0x6b, 0x4c, 0xe4, 0x8b, 0x83, 0x24, 0x90, 0xb1, 0x7c, 0xbb, 0x73, 0xab, 0xd5, 0x2f, 0x5f, 0xec, 0x9c, 0x2c, 0xa8, 0x34, 0x46, 0x37, 0x27, 0xa1, 0x0a, 0x06, 0x80, 0x68, 0x82, 0x32, 0x84, 0xff, 0x48, 0xac, 0x7f, 0x3f, 0x95, 0xdc, 0x98, 0x9b, 0xbe, 0x23, 0x57, 0x3e, 0x5b, 0xd0, 0x3d, 0xa6 },
};
void cmc_decrypt_round(uint8_t *r0, uint8_t *r1, uint8_t c0, uint8_t c1, const CMCTables *t,
const uint8_t *table0hi, const uint8_t *table0lo, const uint8_t *table1,
int base, int invert)
{
uint8_t tmp, xor0, xor1;
tmp = table1[(base & 0xff) ^ t->address_0_7_xor[(base >> 8) & 0xff]];
xor0 = (table0hi[(base >> 8) & 0xff] & 0xfe) | (tmp & 0x01);
xor1 = (tmp & 0xfe) | (table0lo[(base >> 8) & 0xff] & 0x01);
if (invert) { *r0 = c1 ^ xor0; *r1 = c0 ^ xor1; }
else { *r0 = c0 ^ xor0; *r1 = c1 ^ xor1; }
}
void cmc_decrypt_crom(uint8_t* rom, uint32_t rom_size, const CMCTables *t, int extra_xor)
{
int rpos;
uint8_t *buf = calloc(rom_size, 1);
// Data xor
for (rpos = 0; rpos < rom_size/4; rpos++)
{
cmc_decrypt_round(&buf[4*rpos+0], &buf[4*rpos+3], rom[4*rpos+0], rom[4*rpos+3], t, t->type0_t03, t->type0_t12, t->type1_t03, rpos, (rpos>>8) & 1);
cmc_decrypt_round(&buf[4*rpos+1], &buf[4*rpos+2], rom[4*rpos+1], rom[4*rpos+2], t, t->type0_t12, t->type0_t03, t->type1_t12, rpos, ((rpos>>16) ^ t->address_16_23_xor2[(rpos>>8) & 0xff]) & 1);
}
// Address xor
for (rpos = 0; rpos < rom_size/4; rpos++)
{
int baser;
baser = rpos;
baser ^= extra_xor;
baser ^= t->address_8_15_xor1[(baser >> 16) & 0xff] << 8;
baser ^= t->address_8_15_xor2[baser & 0xff] << 8;
baser ^= t->address_16_23_xor1[baser & 0xff] << 16;
baser ^= t->address_16_23_xor2[(baser >> 8) & 0xff] << 16;
baser ^= t->address_0_7_xor[(baser >> 8) & 0xff];
if (rom_size == 0x3000000) /* special handling for preisle2 */
{
if (rpos < 0x2000000/4)
baser &= (0x2000000/4)-1;
else
baser = 0x2000000/4 + (baser & ((0x1000000/4)-1));
}
else if (rom_size == 0x6000000) /* special handling for kf2k3pcb */
{
if (rpos < 0x4000000/4)
baser &= (0x4000000/4)-1;
else
baser = 0x4000000/4 + (baser & ((0x1000000/4)-1));
}
else /* Clamp to the real rom size */
baser &= (rom_size/4)-1;
rom[4*rpos+0] = buf[4*baser+0];
rom[4*rpos+1] = buf[4*baser+1];
rom[4*rpos+2] = buf[4*baser+2];
rom[4*rpos+3] = buf[4*baser+3];
}
free(buf);
}
void cmc_decrypt_srom(uint8_t* crom, uint32_t crom_size, uint8_t* srom, uint32_t srom_size) {
int tx_size = srom_size;
uint8_t *src = crom + crom_size - tx_size;
uint8_t *dst = srom;
for (int i = 0; i < tx_size; i++)
dst[i] = src[(i & ~0x1f) + ((i & 7) << 2) + ((~i & 8) >> 2) + ((i & 0x10) >> 4)];
}
void cmc_decrypt(Game *g) {
static const uint8_t extra_xor[65536] = { [GAME_SENGOKU3]=0xFE, [GAME_S1945P]=0x05 };
if (extra_xor[g->code] == 0) panic("unsupported CMC encrypted ROM (code: %04x)\n", g->code);
cmc_decrypt_crom(g->CROM, g->crom_size, &cmc42_tables, extra_xor[g->code]);
g->srom_size = 128*1024;
g->SROM = malloc(g->srom_size);
cmc_decrypt_srom(g->CROM, g->crom_size, g->SROM, g->srom_size);
g->crom_size -= g->srom_size;
}
void kof99_decrypt_prom(uint8_t *prom)
{
uint16_t *rom = (uint16_t*)(prom + 1024*1024);
for (int i = 0; i < 0x800000/2; i++)
rom[i] = BITSWAP16(rom[i],13,7,3,0,9,4,5,6,1,12,8,14,10,11,2,15);
for (int i = 0; i < 0x600000/2; i+=0x800/2) {
uint16_t buffer[0x800/2];
memcpy(buffer, &rom[i], 0x800);
for (int j = 0; j < 0x800/2; j++)
rom[i+j] = buffer[BITSWAP24(j,23,22,21,20,19,18,17,16,15,14,13,12,11,10,6,2,4,9,8,3,1,7,0,5)];
}
rom = (uint16_t*)prom;
for (int i = 0; i < 0x0c0000/2; i++)
rom[i] = rom[0x700000/2 + BITSWAP24(i,23,22,21,20,19,18,11,6,14,17,16,5,8,10,12,0,4,3,2,7,9,15,13,1)];
}
void chfn(char *path, char* fn) {
int i = strlen(path);
while (--i >= 0) if (path[i] == '/' || path[i] == '\\') break;
strcpy(path+i+1, fn);
}
void chext(char *fn, char* ext) {
int i = strlen(fn);
while (--i >= 0) if (fn[i] == '.') break;
assert(i >= 0);
strcpy(fn+i, ext);
}
bool stranyprefix(const char *line, const char **prefixes) {
while (*prefixes) {
if (strstartwith(line, *prefixes))
return true;
prefixes++;
}
return false;
}
void byteswap(uint8_t *p0, int s0, uint8_t *p1, int s1, int sz) {
for (int i=0;i<sz;i++) {
uint8_t v = *p0;
*p0 = *p1;
*p1 = v;
p0+=s0; p1+=s1;
}
}
uint8_t* readall(const char *fn, int *sz) {
FILE *f = fopen(fn, "rb"); if (!f) panic("error: cannot open: %s\n", fn);
fseek(f, 0, SEEK_END);
*sz = ftell(f);
fseek(f, 0, SEEK_SET);
uint8_t *ROM = malloc(*sz);
fread(ROM, 1, *sz, f);
fclose(f);
return ROM;
}
void saveto(const uint8_t *data, int size, const char *fn) {
FILE *f = fopen(fn, "wb"); if (!f) panic("error: cannot create: %s", fn);
fwrite(data, 1, size, f);
fclose(f);
}
bool is_bios(char *fn) {
static const char *bios[] = { "sp-", "sp1.", "uni-bios", "asia-", "japan-", "sfix.", "sm1.", NULL };
return stranyprefix(fn, bios);
}
int romtype(char *fn, char ch) {
char *pt = fn-1;
while ((pt = strchr(pt+1, ch))) {
if (pt[1] >= '1' && pt[1] <= '9')
return pt[1]-'0';
}
return 0;
}
void romset_add(Romset *r, int idx, mz_zip_archive_file_stat *stat) {
if (idx >= sizeof(r->fn)/sizeof(r->fn[0])) panic("invalid ROM index: %d (%s)\n", idx, stat->m_filename);
if (r->fn[idx-1]) panic("duplicated rom at %d: %s %s\n", idx, r->fn[idx-1], stat->m_filename);
r->fn[idx-1] = strdup(stat->m_filename);
r->size[idx-1] = stat->m_uncomp_size;
r->total_size += stat->m_uncomp_size;
}
int romset_count(Romset *r) {
int count = 0;
while (r->fn[count]) count++;
return count;
}
#define ROMSET_LOAD_INTERLEAVE 1
#define ROMSET_OFFSET_1MIB 2
uint8_t* romset_load(Romset *r, mz_zip_archive *zip, int flags) {
int cnt = romset_count(r);
int base = flags & ROMSET_OFFSET_1MIB ? 1024*1024 : 0;
uint8_t *ROM = malloc(r->total_size + base);
memset(ROM, 0, base);
int offset = 0;
for (int i=0;i<cnt;i++) {
uint8_t *buf = malloc(r->size[i]);
if (!mz_zip_reader_extract_file_to_mem(zip, r->fn[i], buf, r->size[i], 0))
panic("%s\n", mz_zip_get_error_string(mz_zip_get_last_error(zip)));
if (flags & ROMSET_LOAD_INTERLEAVE) {
byteswap(ROM+base+offset+(i&1), 2, buf, 1, r->size[i]);
if (i&1) offset += r->size[i]*2;
} else {
memcpy(ROM+base+offset, buf, r->size[i]);
offset += r->size[i];
}
free(buf);
}
assert(offset == r->total_size);
return ROM;
}
void load_bios(const char *fn, Bios *bios) {
bios->PROM = readall(fn, &bios->prom_size);
byteswap(bios->PROM, 2, bios->PROM+1, 2, bios->prom_size/2);
char *sfix = strdup(fn);
chfn(sfix, "sfix.sfix");
bios->SROM = readall(sfix, &bios->srom_size);
fixrom_preprocess(bios->SROM, bios->srom_size);
}
void load_game(const char *fn, Game *game) {
mz_zip_archive zip;
mz_zip_zero_struct(&zip);
memset(game, 0, sizeof(*game));
if (!mz_zip_reader_init_file(&zip, fn, 0)) panic("%s\n", mz_zip_get_error_string(mz_zip_get_last_error(&zip)));
Romset P, C, S, X;
memset(&P, 0, sizeof(Romset));
memset(&C, 0, sizeof(Romset));
memset(&S, 0, sizeof(Romset));
memset(&X, 0, sizeof(Romset));
for (int index = 0;;index++) {
mz_zip_archive_file_stat stat;
if (!mz_zip_reader_file_stat(&zip, index, &stat)) break;
if (stat.m_is_directory) continue;
char *fn = stat.m_filename;
for (int i=0;stat.m_filename[i];i++) fn[i] = tolower(fn[i]);
int idx;
if (is_bios(fn)) continue;
if ((idx = romtype(fn, 'p'))) romset_add(&P, idx, &stat);
if ((idx = romtype(fn, 'g'))) romset_add(&P, idx, &stat); // some PROMs are called pg1/pg2
if ((idx = romtype(fn, 's'))) romset_add(&S, idx, &stat);
if ((idx = romtype(fn, 'c'))) romset_add(&C, idx, &stat);
if (strstr(fn, "sma")) romset_add(&X, 1, &stat);
}
int num_croms = romset_count(&C);
if (!num_croms) panic("error: no CROM files found\n");
if (num_croms & 1) panic("error: odd number of CROM files found: %d\n", num_croms);
int num_sroms = romset_count(&S);
if (num_sroms > 1) panic("error: invalid number of SROM files found: %d\n", num_sroms);
int num_proms = romset_count(&P);
if (!num_proms) panic("error: no PROM files found\n");
int num_smas = romset_count(&X);
if (num_smas > 1) panic("error: invalid number of SMA files found: %d\n", num_smas);
// Load PROMs
if (num_smas == 0) {
game->PROM = romset_load(&P, &zip, 0);
byteswap(game->PROM, 2, game->PROM+1, 2, P.total_size/2);
game->prom_size = P.total_size;
// In some cases, the single PROM has the two halves inverted.
if (num_proms == 1) {
if (memcmp(game->PROM+P.total_size/2+0x100, "NEO-GEO", 7)==0)
byteswap(game->PROM, 1, game->PROM+P.total_size/2, 1, P.total_size/2);
}
if (memcmp(game->PROM+0x100, "NEO-GEO", 7)) panic("error: cannot detect PROM layout\n");
} else {
game->PROM = romset_load(&P, &zip, ROMSET_OFFSET_1MIB);
// Load SMA rom
if (X.size[0] != 0x40000) panic("error: invalid SMA size: %d\n", X.size[0]);
if (!mz_zip_reader_extract_file_to_mem(&zip, X.fn[0], game->PROM + 0x0C0000, X.size[0], 0))
panic("%s\n", mz_zip_get_error_string(mz_zip_get_last_error(&zip)));
game->prom_size = P.total_size + 1024*1024;
byteswap(game->PROM, 2, game->PROM+1, 2, game->prom_size/2);
// Detect SMA rom
if (!memcmp(game->PROM+0x0C0FA7, "KOF'99", 6) || 1) {
kof99_decrypt_prom(game->PROM);
FILE *f = fopen("kof99.prom", "wb");
fwrite(game->PROM, 1, game->prom_size, f);
fclose(f);
} else {
panic("error: cannot detect SMA rom\n");
}
// if (memcmp(game->PROM+0x100, "NEO-GEO", 7)) panic("error: SMA PROM decryption failed\n");
}
game->code = ((int)game->PROM[0x108] << 8) | game->PROM[0x109];
// Load and preprocess CROM roms
game->CROM = romset_load(&C, &zip, ROMSET_LOAD_INTERLEAVE);
game->crom_size = C.total_size;
// Load and preprocess SROM roms
if (num_sroms != 0) {
game->SROM = romset_load(&S, &zip, 0);
game->srom_size = S.total_size;
} else {
cmc_decrypt(game);
}
// Compact CROM
while (memcmp(game->CROM+game->crom_size-256, game->CROM+game->crom_size-128, 128) == 0)
game->crom_size -= 128;
// Compact SROM
while (memcmp(game->SROM+game->srom_size-64, game->SROM+game->srom_size-32, 32) == 0)
game->srom_size -= 32;
// Preprocess graphics ROMs to convert it into N64 4bpp format
crom_preprocess(game->CROM, game->crom_size);
fixrom_preprocess(game->SROM, game->srom_size);
}
void patch_game(Game *game) {
switch (game->code) {
case 0x44: // aof
byteswap(game->CROM+2*1024*1024, 1, game->CROM+4*1024*1024, 1, 2*1024*1024);
break;
}
}
int main(int argc, char *argv[]) {
if (argc < 3) {
fprintf(stderr, "MVS64 ROM conversion tool\n\n");
fprintf(stderr, "Usage:\n");
fprintf(stderr, " mvsmakerom <bios> <game.zip>\n");
fprintf(stderr, "\n");
fprintf(stderr, "Notes:\n");
fprintf(stderr, " * <bios> must be a valid NeoGeo BIOS (original or homebrew)\n");
fprintf(stderr, " * Make sure sfix.sfix is in the same directory of the BIOS\n");
exit(2);
}
Bios bios;
load_bios(argv[1], &bios);
Game game;
load_game(argv[2], &game);
patch_game(&game);
char outfn[strlen(argv[2])+16];
strcpy(outfn, argv[2]);
chext(outfn, ".n64");
mkdir(outfn, 0777);
strcat(outfn, "/?.rom");
int off = strlen(outfn)-5;
outfn[off] = 'p';
saveto(game.PROM, MIN(game.prom_size, 1024*1024), outfn);
if (game.prom_size > 1024*1024) {
outfn[off] = 'b';
saveto(game.PROM+1024*1024, game.prom_size-1024*1024, outfn);
}
outfn[off] = 'c';
saveto(game.CROM, game.crom_size, outfn);
outfn[off] = 's';
saveto(game.SROM, game.srom_size, outfn);
strcpy(outfn+off, "?.bios");
outfn[off] = 'p';
saveto(bios.PROM, bios.prom_size, outfn);
outfn[off] = 's';
saveto(bios.SROM, bios.srom_size, outfn);
const char *ini = game_ini[game.code];
if (ini) {
strcpy(outfn+off, "game.ini");
saveto(ini, strlen(ini), outfn);
}
}
const char* game_ini[65536] = {
[GAME_MSLUG] = "idle_skip=0x1FE2\n",
[GAME_SAMSHO] = "idle_skip=0xF7E\n",
[GAME_AOF] = "idle_skip=0x6790\n",
[GAME_AOF3] = "idle_skip=0x15D2\n",
[GAME_PBOBBLEN] = "idle_skip=0xE76\n",
};