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
|
#include "rockmacros.h"
#include "defs.h"
#include "regs.h"
#include "mem.h"
#include "hw.h"
#include "lcd-gb.h"
#include "rtc-gb.h"
#include "save.h"
#include "sound.h"
/* From http://www.semis.demon.co.uk/Gameboy/Gbspec.txt (4/17/2007)
* Cartridge type:
* 0 - ROM ONLY 12 - ROM+MBC3+RAM
* 1 - ROM+MBC1 13 - ROM+MBC3+RAM+BATT
* 2 - ROM+MBC1+RAM 19 - ROM+MBC5
* 3 - ROM+MBC1+RAM+BATT 1A - ROM+MBC5+RAM
* 5 - ROM+MBC2 1B - ROM+MBC5+RAM+BAT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 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 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 unsigned short romsize_table[56] =
{
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, 128, 128, 128, 0
/* 0, 0, 0, 0, 72, 80, 96 -- actual values but bad to use these! */
};
/* Ram size should be no larger then 16 banks 1Mbit */
static unsigned char ramsize_table[5] =
{
0, 1, 1, 4, 16
};
static char *romfile;
static char sramfile[500];
static char rtcfile[500];
static char saveprefix[500];
static int saveslot;
static int forcebatt, nobatt;
static int forcedmg;
static int memfill = -1, memrand = -1;
static void initmem(void *mem, int size)
{
char *p = mem;
if (memrand >= 0)
{
srand(memrand ? memrand : -6 ); /* time(0)); */
while(size--) *(p++) = rand();
}
else if (memfill >= 0)
memset(p, memfill, size);
}
static byte *loadfile(int fd, int *len)
{
int c;
byte *d;
*len=lseek(fd,0,SEEK_END);
d=malloc((*len)*sizeof(char)+64);
if(d==0)
{
die("Not enough memory");
return 0;
}
lseek(fd,0,SEEK_SET);
c = read(fd, d, *len);
return d;
}
int rom_load(void)
{
int fd;
byte c, *data, *header;
int len = 0, rlen;
fd = open(romfile, O_RDONLY);
if (fd<0)
{
die("cannot open rom file %s", romfile);
return 1;
}
data = loadfile(fd, &len);
close(fd);
if(data==0)
{
die("Not Enough Memory");
return 1;
}
header = data; /* no zip. = 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_MBC1|MBC_MBC2|MBC_MBC3|MBC_MBC5|MBC_RUMBLE|MBC_HUC1|MBC_HUC3);
mbc.batt = ((mbc_table[c]&MBC_BAT) && !nobatt) || forcebatt;
rtc.batt = mbc_table[c]&MBC_RTC;
if(header[0x0148]<10 || (header[0x0148]>51 && header[0x0148]<55))
mbc.romsize = romsize_table[header[0x0148]];
else
{
die("unknown ROM size %02X\n", header[0x0148]);
return 1;
}
if(header[0x0149]<5)
mbc.ramsize = ramsize_table[header[0x0149]];
else
{
die("unknown SRAM size %02X\n", header[0x0149]);
return 1;
}
rlen = 16384 * mbc.romsize;
rom.bank = (void *) data; /* realloc(data, rlen); */
if (rlen > len) memset(rom.bank[0]+len, 0xff, rlen - len);
/* This is the size of the ram on the cartridge
* See http://www.semis.demon.co.uk/Gameboy/Gbspec.txt
* for a full description. (8192*number of banks)
*/
ram.sbank = malloc(8192 * mbc.ramsize);
if(ram.sbank==0 && mbc.ramsize!=0)
{
die("Not enough Memory");
return 1;
}
/* ram.ibank = malloc(4096*8); */
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;
return 0;
}
int sram_load(void)
{
int fd;
char meow[500];
if (!mbc.batt || !sramfile || !*sramfile) return -1;
/* Consider sram loaded at this point, even if file doesn't exist */
ram.loaded = 1;
fd = open(sramfile, O_RDONLY);
snprintf(meow,499,"Opening %s %d",sramfile,fd);
rb->splash(HZ*2, meow);
if (fd<0) return -1;
snprintf(meow,499,"Loading savedata from %s",sramfile);
rb->splash(HZ*2, meow);
read(fd,ram.sbank, 8192*mbc.ramsize);
close(fd);
return 0;
}
int sram_save(void)
{
int fd;
char meow[500];
/* If we crash before we ever loaded sram, DO NOT SAVE! */
if (!mbc.batt || !sramfile || !ram.loaded || !mbc.ramsize)
return -1;
fd = open(sramfile, O_WRONLY|O_CREAT|O_TRUNC);
if (fd<0) return -1;
snprintf(meow,499,"Saving savedata to %s",sramfile);
rb->splash(HZ*2, meow);
write(fd,ram.sbank, 8192*mbc.ramsize);
close(fd);
return 0;
}
void state_save(int n)
{
int fd;
char name[500];
if (n < 0) n = saveslot;
if (n < 0) n = 0;
snprintf(name, 499,"%s.%03d", saveprefix, n);
if ((fd = open(name, O_WRONLY|O_CREAT|O_TRUNC)>=0))
{
savestate(fd);
close(fd);
}
}
void state_load(int n)
{
int fd;
char name[500];
if (n < 0) n = saveslot;
if (n < 0) n = 0;
snprintf(name, 499, "%s.%03d", saveprefix, n);
if ((fd = open(name, O_RDONLY)>=0))
{
loadstate(fd);
close(fd);
vram_dirty();
pal_dirty();
sound_dirty();
mem_updatemap();
}
}
void rtc_save(void)
{
int fd;
if (!rtc.batt) return;
if ((fd = open(rtcfile, O_WRONLY|O_CREAT|O_TRUNC))<0) return;
rtc_save_internal(fd);
close(fd);
}
void rtc_load(void)
{
int fd;
if (!rtc.batt) return;
if ((fd = open(rtcfile, O_RDONLY))<0) return;
rtc_load_internal(fd);
close(fd);
}
void loader_unload(void)
{
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 = 0;
rom.bank = 0;
ram.sbank = 0;
mbc.type = mbc.romsize = mbc.ramsize = mbc.batt = 0;
}
void cleanup(void)
{
sram_save();
rtc_save();
/* IDEA - if error, write emergency savestate..? */
}
void loader_init(char *s)
{
romfile = s;
if(rom_load())
return;
rb->splash(HZ/2, rom.name);
snprintf(saveprefix, 499, "%s/%s", savedir, rom.name);
strcpy(sramfile, saveprefix);
strcat(sramfile, ".sav");
strcpy(rtcfile, saveprefix);
strcat(rtcfile, ".rtc");
sram_load();
rtc_load();
}
|