-
Notifications
You must be signed in to change notification settings - Fork 32
/
loader.c
608 lines (510 loc) · 12.5 KB
/
loader.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
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#undef _GNU_SOURCE
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <time.h>
#include "defs.h"
#include "loader.h"
#include "regs.h"
#include "mem.h"
#include "hw.h"
#include "rtc.h"
#include "rc.h"
#include "lcd.h"
#include "inflate.h"
#include "miniz.h"
#define XZ_USE_CRC64
#include "xz/xz.h"
#include "save.h"
#include "sound.h"
#include "sys.h"
static int mbc_table[256] =
{
0, 1, 1, 1, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 3,
3, 3, 3, 3, 0, 0, 0, 0, 0, 5, 5, 5, MBC_RUMBLE, MBC_RUMBLE, MBC_RUMBLE, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, MBC_HUC3, MBC_HUC1
};
static int rtc_table[256] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0
};
static int batt_table[256] =
{
0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0,
1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0,
0
};
static int romsize_table[256] =
{
2, 4, 8, 16, 32, 64, 128, 256, 512,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 128, 128, 128
/* 0, 0, 72, 80, 96 -- actual values but bad to use these! */
};
static int ramsize_table[256] =
{
1, 1, 1, 4, 16,
4 /* FIXME - what value should this be?! */
};
static char *bootroms[2];
static char *romfile;
static char *sramfile;
static char *rtcfile;
static char *saveprefix;
static char *savename;
static char *savedir;
static int saveslot;
static int forcebatt, nobatt;
static int forcedmg, gbamode;
static int memfill = -1, memrand = -1;
static void initmem(void *mem, int size)
{
char *p = mem;
if (memrand >= 0)
{
srand(memrand ? memrand : time(0));
while(size--) *(p++) = rand();
}
else if (memfill >= 0)
memset(p, memfill, size);
}
static byte *loadfile(FILE *f, int *len)
{
int c, l = 0, p = 0;
byte *d = 0, buf[4096];
for(;;)
{
c = fread(buf, 1, sizeof buf, f);
if (c <= 0) break;
l += c;
d = realloc(d, l);
if (!d) return 0;
memcpy(d+p, buf, c);
p += c;
}
*len = l;
return d;
}
static byte *inf_buf;
static int inf_pos, inf_len;
static char* loader_error;
char* loader_get_error(void)
{
return loader_error;
}
void loader_set_error(char *fmt, ...)
{
char buf[1024];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof buf, fmt, ap);
va_end(ap);
loader_error = strdup(buf);
}
static int inflate_callback(byte b)
{
if (inf_pos >= inf_len)
{
inf_len += 512;
inf_buf = realloc(inf_buf, inf_len);
if (!inf_buf) {
loader_set_error("out of memory inflating file @ %d bytes\n", inf_pos);
return -1;
}
}
inf_buf[inf_pos++] = b;
return 0;
}
typedef int (*unzip_or_inflate_func) (const unsigned char *data, long *p, int (* callback) (unsigned char d));
static byte *gunzip_or_inflate(byte *data, int *len, unsigned offset,
unzip_or_inflate_func func)
{
long pos = 0;
inf_buf = 0;
inf_pos = inf_len = 0;
if (func(data+offset, &pos, inflate_callback) < 0)
return data;
free(data);
*len = inf_pos;
return inf_buf;
}
static byte *gunzip(byte *data, int *len) {
return gunzip_or_inflate(data, len, 0, unzip);
}
/* primitive pkzip decompressor. it can only decompress the first
file in a zip archive. */
static byte *pkunzip(byte *data, int *len) {
unsigned short fnl, el, comp;
unsigned int st;
void *new;
size_t newlen;
int oldlen = *len;
if (*len < 128) return data;
memcpy(&comp, data+8, 2);
comp = LIL(comp);
if(comp != 0 && comp != 8) return data;
memcpy(&fnl, data+26, 2);
memcpy(&el, data+28, 2);
fnl = LIL(fnl);
el = LIL(el);
st = 30 + fnl + el;
if(*len < st) return data;
if(comp == 0) {
inf_buf = realloc(NULL, *len - st);
memcpy(inf_buf, data+st, *len - st);
free(data);
inf_len = *len = *len - st;
return inf_buf;
}
*len -= st;
newlen = 0;
new = tinfl_decompress_mem_to_heap(data+st, *len, &newlen, 0);
if(new) {
*len = newlen;
free(data);
return new;
}
*len = oldlen;
return data;
}
static int write_dec(byte *data, int len) {
int i;
for(i=0; i < len; i++)
if(inflate_callback(data[i])) return -1;
return 0;
}
static int unxz(byte *data, int len) {
struct xz_buf b;
struct xz_dec *s;
enum xz_ret ret;
unsigned char out[4096];
/*
* Support up to 64 MiB dictionary. The actually needed memory
* is allocated once the headers have been parsed.
*/
s = xz_dec_init(XZ_DYNALLOC, 1 << 26);
if(!s) goto err;
b.in = data;
b.in_pos = 0;
b.in_size = len;
b.out = out;
b.out_pos = 0;
b.out_size = sizeof(out);
while (1) {
ret = xz_dec_run(s, &b);
if(b.out_pos == sizeof(out)) {
if(write_dec(out, sizeof(out))) goto err;
b.out_pos = 0;
}
if(ret == XZ_OK) continue;
if(write_dec(out, b.out_pos)) goto err;
if(ret == XZ_STREAM_END) {
xz_dec_end(s);
return 0;
}
goto err;
}
err:
xz_dec_end(s);
return -1;
}
static byte *do_unxz(byte *data, int *len) {
xz_crc32_init();
xz_crc64_init();
inf_buf = 0;
inf_pos = inf_len = 0;
if (unxz(data, *len) < 0)
return data;
free(data);
*len = inf_pos;
return inf_buf;
}
static byte *decompress(byte *data, int *len)
{
if (data[0] == 0x1f && data[1] == 0x8b)
return gunzip(data, len);
if (data[0] == 0xFD && !memcmp(data+1, "7zXZ", 4))
return do_unxz(data, len);
if (data[0] == 'P' && !memcmp(data+1, "K\03\04", 3))
return pkunzip(data, len);
return data;
}
static FILE* rom_loadfile(char *fn, byte** data, int *len) {
FILE *f;
if (strcmp(fn, "-")) f = fopen(fn, "rb");
else f = stdin;
if (!f) {
err:
loader_set_error("cannot open rom file: %s\n", fn);
return f;
}
*data = loadfile(f, len);
if (!*data) {
fclose(f);
f = 0;
goto err;
}
*data = decompress(*data, len);
return f;
}
int bootrom_load() {
byte *data;
int len;
FILE *f;
REG(RI_BOOT) = 0xff;
if (!bootroms[hw.cgb] || !bootroms[hw.cgb][0]) return 0;
f = rom_loadfile(bootroms[hw.cgb], &data, &len);
if(!f) return -1;
bootrom.bank = realloc(data, 16384);
memset(bootrom.bank[0]+len, 0xff, 16384-len);
memcpy(bootrom.bank[0]+0x100, rom.bank[0]+0x100, 0x100);
fclose(f);
REG(RI_BOOT) = 0xfe;
return 0;
}
/* memory allocation breakdown:
loadfile returns local buffer retrieved via realloc()
it's called only by rom_loadfile.
rom_loadfile is called by bootrom_load and rom_load.
bootrom_load is called once per romfile load via loader_init from
load_rom_and_rc, and mem ends up in bootrom.bank, loader_unload() frees it.
rom_load is called by rom_load_simple and loader_init().
rom_load_simple is only called by rominfo in main.c, which we can ignore.
the mem allocated by loadfile thru loader_init/rom_load ends up in
rom.bank, which is freed in loader_unload(), just like the malloc'd
rom.sbank.
where it gets complicated is when rom_loadfile uncompresses data.
the allocation returned by loadfile is passed to decompress().
if it fails, it returns the original loadfile allocation, on success
it returns a pointer to inf_buf which contains the uncompressed data.
*/
int rom_load()
{
FILE *f;
byte c, *data, *header;
int len = 0, rlen;
f = rom_loadfile(romfile, &data, &len);
if(!f) return -1;
header = data;
memcpy(rom.name, header+0x0134, 16);
if (rom.name[14] & 0x80) rom.name[14] = 0;
if (rom.name[15] & 0x80) rom.name[15] = 0;
rom.name[16] = 0;
c = header[0x0147];
mbc.type = mbc_table[c];
mbc.batt = (batt_table[c] && !nobatt) || forcebatt;
rtc.batt = rtc_table[c];
mbc.romsize = romsize_table[header[0x0148]];
mbc.ramsize = ramsize_table[header[0x0149]];
if (!mbc.romsize) {
loader_set_error("unknown ROM size %02X\n", header[0x0148]);
return -1;
}
if (!mbc.ramsize) {
loader_set_error("unknown SRAM size %02X\n", header[0x0149]);
return -1;
}
rlen = 16384 * mbc.romsize;
c = header[0x0143];
/* from this point on, we may no longer access data and header */
rom.bank = realloc(data, rlen);
if (rlen > len) memset(rom.bank[0]+len, 0xff, rlen - len);
ram.sbank = malloc(8192 * mbc.ramsize);
initmem(ram.sbank, 8192 * mbc.ramsize);
initmem(ram.ibank, 4096 * 8);
mbc.rombank = 1;
mbc.rambank = 0;
hw.cgb = ((c == 0x80) || (c == 0xc0)) && !forcedmg;
hw.gba = (hw.cgb && gbamode);
if (strcmp(romfile, "-")) fclose(f);
return 0;
}
int rom_load_simple(char *fn) {
romfile = fn;
return rom_load();
}
int sram_load()
{
FILE *f;
if (!mbc.batt || !sramfile || !*sramfile) return -1;
/* Consider sram loaded at this point, even if file doesn't exist */
ram.loaded = 1;
f = fopen(sramfile, "rb");
if (!f) return -1;
fread(ram.sbank, 8192, mbc.ramsize, f);
fclose(f);
return 0;
}
int sram_save()
{
FILE *f;
/* If we crash before we ever loaded sram, DO NOT SAVE! */
if (!mbc.batt || !sramfile || !ram.loaded || !mbc.ramsize)
return -1;
f = fopen(sramfile, "wb");
if (!f) return -1;
fwrite(ram.sbank, 8192, mbc.ramsize, f);
fclose(f);
return 0;
}
void state_save(int n)
{
FILE *f;
char *name;
if (n < 0) n = saveslot;
if (n < 0) n = 0;
name = malloc(strlen(saveprefix) + 5);
sprintf(name, "%s.%03d", saveprefix, n);
if ((f = fopen(name, "wb")))
{
savestate(f);
fclose(f);
}
free(name);
}
void state_load(int n)
{
FILE *f;
char *name;
if (n < 0) n = saveslot;
if (n < 0) n = 0;
name = malloc(strlen(saveprefix) + 5);
sprintf(name, "%s.%03d", saveprefix, n);
if ((f = fopen(name, "rb")))
{
loadstate(f);
fclose(f);
vram_dirty();
pal_dirty();
sound_dirty();
mem_updatemap();
}
free(name);
}
void rtc_save()
{
FILE *f;
if (!rtc.batt) return;
if (!(f = fopen(rtcfile, "wb"))) return;
rtc_save_internal(f);
fclose(f);
}
void rtc_load()
{
FILE *f;
if (!rtc.batt) return;
if (!(f = fopen(rtcfile, "r"))) return;
rtc_load_internal(f);
fclose(f);
}
#define FREENULL(X) do { free(X); X = 0; } while(0)
void loader_unload()
{
sram_save();
if (romfile) FREENULL(romfile);
if (sramfile) FREENULL(sramfile);
if (saveprefix) FREENULL(saveprefix);
if (rom.bank) FREENULL(rom.bank);
if (ram.sbank) FREENULL(ram.sbank);
if (bootrom.bank) FREENULL(bootrom.bank);
mbc.type = mbc.romsize = mbc.ramsize = mbc.batt = 0;
}
static char *base(char *s)
{
char *p;
p = strrchr(s, '/');
if (p) return p+1;
return s;
}
static char *ldup(char *s)
{
int i;
char *n, *p;
p = n = malloc(strlen(s));
for (i = 0; s[i]; i++) if (isalnum(s[i])) *(p++) = tolower(s[i]);
*p = 0;
return n;
}
static void cleanup()
{
sram_save();
rtc_save();
/* IDEA - if error, write emergency savestate..? */
}
int loader_init(char *s)
{
char *name, *p;
sys_checkdir(savedir, 1); /* needs to be writable */
romfile = s;
if(rom_load()) return -1;
bootrom_load();
vid_settitle(rom.name);
if (savename && *savename)
{
if (savename[0] == '-' && savename[1] == 0)
name = ldup(rom.name);
else name = strdup(savename);
}
else if (romfile && *base(romfile) && strcmp(romfile, "-"))
{
name = strdup(base(romfile));
p = strchr(name, '.');
if (p) *p = 0;
}
else name = ldup(rom.name);
saveprefix = malloc(strlen(savedir) + strlen(name) + 2);
sprintf(saveprefix, "%s/%s", savedir, name);
sramfile = malloc(strlen(saveprefix) + 5);
strcpy(sramfile, saveprefix);
strcat(sramfile, ".sav");
rtcfile = malloc(strlen(saveprefix) + 5);
strcpy(rtcfile, saveprefix);
strcat(rtcfile, ".rtc");
sram_load();
rtc_load();
atexit(cleanup);
return 0;
}
rcvar_t loader_exports[] =
{
RCV_STRING("bootrom_dmg", &bootroms[0], "bootrom for DMG games"),
RCV_STRING("bootrom_cgb", &bootroms[1], "bootrom for CGB games"),
RCV_STRING("savedir", &savedir, "save directory"),
RCV_STRING("savename", &savename, "base filename for saves"),
RCV_INT("saveslot", &saveslot, "which savestate slot to use"),
RCV_BOOL("forcebatt", &forcebatt, "save SRAM even on carts w/o battery"),
RCV_BOOL("nobatt", &nobatt, "never save SRAM"),
RCV_BOOL("forcedmg", &forcedmg, "force DMG mode for CGB carts"),
RCV_BOOL("gbamode", &gbamode, "simulate cart being used on a GBA"),
RCV_INT("memfill", &memfill, ""),
RCV_INT("memrand", &memrand, ""),
RCV_END
};