-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.c
executable file
·539 lines (451 loc) · 11.3 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#ifndef GNUBOY_NO_MINIZIP
/*
** use http://www.winimage.com/zLibDll/minizip.html v1.1
** which needs zlib
*/
#include <unzip/unzip.h>
#endif /* GNUBOY_USE_MINIZIP */
#include "gnuboy.h"
#include "defs.h"
#include "regs.h"
#include "mem.h"
#include "hw.h"
#include "lcd.h"
#include "rtc.h"
#include "rc.h"
#include "sound.h"
#ifndef GNUBOY_NO_MINIZIP
static int check_zip(char *filename);
static byte *loadzipfile(char *archive, int *filesize);
#endif /* GNUBOY_USE_MINIZIP */
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 *romfile=NULL;
static char *sramfile=NULL;
static char *rtcfile=NULL;
static char *saveprefix=NULL;
static char *savename=NULL;
static char *savedir=NULL;
static int saveslot=0;
static int forcebatt=0, nobatt=0;
static int forcedmg=0, gbamode=0;
static int memfill = 0, 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 l = 0, c = 0;
byte *d = NULL;
#ifdef GNUBOY_ENABLE_ORIGINAL_SLOW_INCREMENTAL_LOADER
int p = 0;
byte buf[512];
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;
}
#else /* fast and no space check */
/* alloc and read once - NOTE no sanity check on filesize */
fseek(f, 0, SEEK_END);
l = ftell(f);
fseek(f, 0, SEEK_SET);
d = (byte*) malloc(l);
if (d != NULL)
{
c = fread((void *) d, (size_t) l, 1, f);
if (c != 1)
{
l = 0;
/* NOTE if this fails caller doesn't catch it (ditto the slow and "safe" version) */
}
}
#endif /* GNUBOY_ENABLE_ORIGINAL_SLOW_INCREMENTAL_LOADER */
*len = l;
return d;
}
static byte *inf_buf;
static int inf_pos, inf_len;
static void inflate_callback(byte b)
{
if (inf_pos >= inf_len)
{
inf_len += 512;
inf_buf = realloc(inf_buf, inf_len);
if (!inf_buf) die("out of memory inflating file @ %d bytes\n", inf_pos);
}
inf_buf[inf_pos++] = b;
}
static byte *decompress(byte *data, int *len)
{
long pos = 0;
if (data[0] != 0x1f || data[1] != 0x8b)
return data;
inf_buf = 0;
inf_pos = inf_len = 0;
if (unzip(data, &pos, inflate_callback) < 0)
return data;
*len = inf_pos;
return inf_buf;
}
int rom_load()
{
FILE *f=NULL;
byte c, *data, *header;
int len = 0, rlen;
#ifndef GNUBOY_NO_MINIZIP
if(!check_zip(romfile)){
#endif /* GNUBOY_USE_MINIZIP */
if (strcmp(romfile, "-")) f = fopen(romfile, "rb");
else f = stdin;
if (!f) die("cannot open rom file: %s\n", romfile);
data = loadfile(f, &len);
#ifndef GNUBOY_NO_MINIZIP
}
else {
data = loadzipfile(romfile, &len);
if(!data) die("cannot open (zip) rom file: %s\n", romfile);
}
#endif /* GNUBOY_USE_MINIZIP */
header = data = decompress(data, &len);
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) die("unknown ROM size %02X\n", header[0x0148]);
if (!mbc.ramsize) die("unknown SRAM size %02X\n", header[0x0149]);
rlen = 16384 * mbc.romsize;
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;
c = header[0x0143];
hw.cgb = ((c == 0x80) || (c == 0xc0)) && !forcedmg;
hw.gba = (hw.cgb && gbamode);
if (strcmp(romfile, "-")) if (f) fclose(f);
return 0;
}
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);
}
void loader_unload()
{
sram_save();
if (romfile) free(romfile);
if (sramfile) free(sramfile);
if (saveprefix) free(saveprefix);
if (rom.bank) free(rom.bank);
if (ram.sbank) free(ram.sbank);
romfile = sramfile = saveprefix = 0;
rom.bank = 0;
ram.sbank = 0;
mbc.type = mbc.romsize = mbc.ramsize = mbc.batt = 0;
}
/* basename/dirname like function */
static char *base(char *s)
{
char *p;
p = (char *) strrchr(s, DIRSEP_CHAR);
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..? */
}
void loader_init(char *s)
{
char *name, *p;
sys_checkdir(savedir, 1); /* needs to be writable */
romfile = s;
rom_load();
vid_settitle(rom.name);
/* savename(s) for sram and rtc are based on the filename, not the romname (or name in compressed file/archive) */
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 = (char *) strchr(name, '.');
if (p) *p = 0;
}
else name = ldup(rom.name);
saveprefix = malloc(strlen(savedir) + strlen(name) + 2);
sprintf(saveprefix, "%s%s%s", savedir, DIRSEP, 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);
}
rcvar_t loader_exports[] =
{
RCV_STRING("savedir", &savedir),
RCV_STRING("savename", &savename),
RCV_INT("saveslot", &saveslot),
RCV_BOOL("forcebatt", &forcebatt),
RCV_BOOL("nobatt", &nobatt),
RCV_BOOL("forcedmg", &forcedmg),
RCV_BOOL("gbamode", &gbamode),
RCV_INT("memfill", &memfill),
RCV_INT("memrand", &memrand),
RCV_END
};
#ifndef GNUBOY_NO_MINIZIP
/*
** Simplistic zip support, only loads the first file in a zip file
** with no check as to the type (or filename/extension).
*/
/*
** returns 1 if a filename is a zip file
*/
static int check_zip(char *filename)
{
char buf[2];
FILE *fd = NULL;
fd = fopen(filename, "rb");
if(!fd) return (0);
fread(buf, 2, 1, fd);
fclose(fd);
if(memcmp(buf, "PK", 2) == 0) return (1);
return (0);
}
static byte *loadzipfile(char *archive, int *filesize)
{
char name[256];
unsigned char *buffer=NULL;
int zerror = UNZ_OK;
unzFile zhandle;
unz_file_info zinfo;
char tmp_header[0x200];
unsigned char rom_found=0;
zhandle = unzOpen(archive);
if(!zhandle) return (NULL);
#ifdef IS_LITTLE_ENDIAN
#define GAMEBOY_HEADER_MAGIC 0x6666EDCE
#else /* BIG ENDIAN */
#define GAMEBOY_HEADER_MAGIC 0xCEED6666
#endif /* IS_LITTLE_ENDIAN */
/* Find first gameboy rom, do not use file extension, look for magic bytes/fingerprint */
/* Seek to first file in archive */
zerror = unzGoToFirstFile(zhandle);
if(zerror != UNZ_OK)
{
unzClose(zhandle);
return (NULL);
}
do
{
unzOpenCurrentFile(zhandle);
unzReadCurrentFile(zhandle, tmp_header, sizeof(tmp_header));
unzCloseCurrentFile(zhandle);
if ((*((unsigned long *)(tmp_header + 0x104))) == GAMEBOY_HEADER_MAGIC)
{
/* Gameboy Rom found! */
rom_found++;
break;
}
} while (unzGoToNextFile(zhandle) != UNZ_END_OF_LIST_OF_FILE);
if (rom_found == 0)
return (NULL);
/* Get information about the file */
unzGetCurrentFileInfo(zhandle, &zinfo, &name[0], 0xff, NULL, 0, NULL, 0);
*filesize = zinfo.uncompressed_size;
/* Error: file size is zero */
if(*filesize <= 0)
{
unzClose(zhandle);
return (NULL);
}
/* Open current file */
zerror = unzOpenCurrentFile(zhandle);
if(zerror != UNZ_OK)
{
unzClose(zhandle);
return (NULL);
}
/* Allocate buffer and read in file */
buffer = malloc(*filesize);
if(!buffer) return (NULL);
zerror = unzReadCurrentFile(zhandle, buffer, *filesize);
/* Internal error: free buffer and close file */
if(zerror < 0 || zerror != *filesize)
{
free(buffer);
buffer = NULL;
unzCloseCurrentFile(zhandle);
unzClose(zhandle);
return (NULL);
}
/* Close current file and archive file */
unzCloseCurrentFile(zhandle);
unzClose(zhandle);
return (buffer);
}
#endif /* GNUBOY_USE_MINIZIP */