2005-03-02 23:49:38 +00:00
|
|
|
#include "rockmacros.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "regs.h"
|
|
|
|
#include "mem.h"
|
|
|
|
#include "hw.h"
|
2005-07-03 14:05:12 +00:00
|
|
|
#include "lcd-gb.h"
|
|
|
|
#include "rtc-gb.h"
|
2005-03-02 23:49:38 +00:00
|
|
|
#include "save.h"
|
|
|
|
#include "sound.h"
|
2011-12-28 11:32:13 +00:00
|
|
|
#include "loader.h"
|
2005-03-02 23:49:38 +00:00
|
|
|
|
2007-04-18 07:41:31 +00:00
|
|
|
/* 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+BATT
|
|
|
|
* 6 - ROM+MBC2+BATTERY 1C - ROM+MBC5+RUMBLE
|
|
|
|
* 8 - ROM+RAM 1D - ROM+MBC5+RUMBLE+SRAM
|
|
|
|
* 9 - ROM+RAM+BATTERY 1E - ROM+MBC5+RUMBLE+SRAM+BATT
|
|
|
|
* B - ROM+MMM01 1F - Pocket Camera
|
|
|
|
* C - ROM+MMM01+SRAM FD - Bandai TAMA5
|
|
|
|
* D - ROM+MMM01+SRAM+BATT FE - Hudson HuC-3
|
|
|
|
* F - ROM+MBC3+TIMER+BATT FF - Hudson HuC-1
|
|
|
|
* 10 - ROM+MBC3+TIMER+RAM+BATT
|
|
|
|
* 11 - ROM+MBC3
|
|
|
|
*/
|
2005-03-02 23:49:38 +00:00
|
|
|
|
2007-10-16 18:16:22 +00:00
|
|
|
static const int mbc_table[256] =
|
2005-03-02 23:49:38 +00:00
|
|
|
{
|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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, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
2007-04-18 07:41:31 +00:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
MBC_HUC3,
|
|
|
|
MBC_HUC1
|
2005-03-02 23:49:38 +00:00
|
|
|
};
|
|
|
|
|
2007-10-16 18:16:22 +00:00
|
|
|
static const unsigned short romsize_table[56] =
|
2005-03-02 23:49:38 +00:00
|
|
|
{
|
2007-04-18 07:41:31 +00:00
|
|
|
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! */
|
2005-03-02 23:49:38 +00:00
|
|
|
};
|
|
|
|
|
2007-04-18 07:41:31 +00:00
|
|
|
/* Ram size should be no larger then 16 banks 1Mbit */
|
2007-10-16 18:16:22 +00:00
|
|
|
static const unsigned char ramsize_table[5] =
|
2005-03-02 23:49:38 +00:00
|
|
|
{
|
2007-04-18 07:41:31 +00:00
|
|
|
0, 1, 1, 4, 16
|
2005-03-02 23:49:38 +00:00
|
|
|
};
|
|
|
|
|
2008-05-13 09:57:56 +00:00
|
|
|
static const char *romfile;
|
2005-03-02 23:49:38 +00:00
|
|
|
static char sramfile[500];
|
|
|
|
static char rtcfile[500];
|
2010-11-25 12:45:13 +00:00
|
|
|
static char snfile[500];
|
2005-03-02 23:49:38 +00:00
|
|
|
static char saveprefix[500];
|
|
|
|
|
|
|
|
static int forcebatt, nobatt;
|
2007-02-06 21:41:08 +00:00
|
|
|
static int forcedmg;
|
2005-03-02 23:49:38 +00:00
|
|
|
|
|
|
|
static int memfill = -1, memrand = -1;
|
|
|
|
|
|
|
|
static void initmem(void *mem, int size)
|
|
|
|
{
|
2007-02-06 21:41:08 +00:00
|
|
|
char *p = mem;
|
|
|
|
if (memrand >= 0)
|
|
|
|
{
|
|
|
|
srand(memrand ? memrand : -6 ); /* time(0)); */
|
|
|
|
while(size--) *(p++) = rand();
|
|
|
|
}
|
|
|
|
else if (memfill >= 0)
|
|
|
|
memset(p, memfill, size);
|
2005-03-02 23:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static byte *loadfile(int fd, int *len)
|
|
|
|
{
|
2007-04-18 07:41:31 +00:00
|
|
|
byte *d;
|
|
|
|
|
|
|
|
*len=lseek(fd,0,SEEK_END);
|
|
|
|
d=malloc((*len)*sizeof(char)+64);
|
|
|
|
if(d==0)
|
2007-02-06 21:41:08 +00:00
|
|
|
{
|
2007-04-18 07:41:31 +00:00
|
|
|
die("Not enough memory");
|
|
|
|
return 0;
|
2007-02-06 21:41:08 +00:00
|
|
|
}
|
2007-04-18 07:41:31 +00:00
|
|
|
lseek(fd,0,SEEK_SET);
|
|
|
|
|
2011-05-03 20:28:56 +00:00
|
|
|
read(fd, d, *len);
|
2007-04-18 07:41:31 +00:00
|
|
|
|
2007-02-06 21:41:08 +00:00
|
|
|
return d;
|
2005-03-02 23:49:38 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 18:16:22 +00:00
|
|
|
static int rom_load(void)
|
2005-03-02 23:49:38 +00:00
|
|
|
{
|
2007-02-06 21:41:08 +00:00
|
|
|
int fd;
|
|
|
|
byte c, *data, *header;
|
|
|
|
int len = 0, rlen;
|
|
|
|
|
|
|
|
fd = open(romfile, O_RDONLY);
|
|
|
|
|
2007-04-18 07:41:31 +00:00
|
|
|
if (fd<0)
|
|
|
|
{
|
|
|
|
die("cannot open rom file %s", romfile);
|
2007-02-06 21:41:08 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = loadfile(fd, &len);
|
2007-04-18 07:41:31 +00:00
|
|
|
close(fd);
|
|
|
|
if(data==0)
|
|
|
|
{
|
|
|
|
die("Not Enough Memory");
|
|
|
|
return 1;
|
|
|
|
}
|
2007-02-06 21:41:08 +00:00
|
|
|
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];
|
2007-04-18 07:41:31 +00:00
|
|
|
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;
|
2007-02-06 21:41:08 +00:00
|
|
|
|
2007-04-18 07:41:31 +00:00
|
|
|
if(header[0x0148]<10 || (header[0x0148]>51 && header[0x0148]<55))
|
|
|
|
mbc.romsize = romsize_table[header[0x0148]];
|
|
|
|
else
|
|
|
|
{
|
2007-02-06 21:41:08 +00:00
|
|
|
die("unknown ROM size %02X\n", header[0x0148]);
|
|
|
|
return 1;
|
|
|
|
}
|
2007-04-18 07:41:31 +00:00
|
|
|
|
|
|
|
if(header[0x0149]<5)
|
|
|
|
mbc.ramsize = ramsize_table[header[0x0149]];
|
|
|
|
else
|
|
|
|
{
|
2007-02-06 21:41:08 +00:00
|
|
|
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);
|
|
|
|
|
2007-04-18 07:41:31 +00:00
|
|
|
/* 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)
|
|
|
|
*/
|
2007-02-06 21:41:08 +00:00
|
|
|
ram.sbank = malloc(8192 * mbc.ramsize);
|
2007-04-18 07:41:31 +00:00
|
|
|
if(ram.sbank==0 && mbc.ramsize!=0)
|
|
|
|
{
|
|
|
|
die("Not enough Memory");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-02-06 21:41:08 +00:00
|
|
|
/* 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];
|
2005-03-02 23:49:38 +00:00
|
|
|
hw.cgb = ((c == 0x80) || (c == 0xc0)) && !forcedmg;
|
|
|
|
|
2007-02-06 21:41:08 +00:00
|
|
|
return 0;
|
2005-03-02 23:49:38 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 18:16:22 +00:00
|
|
|
static int sram_load(void)
|
2005-03-02 23:49:38 +00:00
|
|
|
{
|
2007-02-06 21:41:08 +00:00
|
|
|
int fd;
|
2005-03-02 23:49:38 +00:00
|
|
|
|
2007-09-09 02:49:24 +00:00
|
|
|
if (!mbc.batt || !*sramfile) return -1;
|
2005-03-02 23:49:38 +00:00
|
|
|
|
2007-02-06 21:41:08 +00:00
|
|
|
/* Consider sram loaded at this point, even if file doesn't exist */
|
|
|
|
ram.loaded = 1;
|
2005-03-02 23:49:38 +00:00
|
|
|
|
2007-02-06 21:41:08 +00:00
|
|
|
fd = open(sramfile, O_RDONLY);
|
|
|
|
if (fd<0) return -1;
|
|
|
|
read(fd,ram.sbank, 8192*mbc.ramsize);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 0;
|
2005-03-02 23:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-16 18:16:22 +00:00
|
|
|
static int sram_save(void)
|
2005-03-02 23:49:38 +00:00
|
|
|
{
|
2007-02-06 21:41:08 +00:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* If we crash before we ever loaded sram, DO NOT SAVE! */
|
2007-09-09 02:49:24 +00:00
|
|
|
if (!mbc.batt || !ram.loaded || !mbc.ramsize)
|
2007-02-06 21:41:08 +00:00
|
|
|
return -1;
|
2010-05-06 17:35:13 +00:00
|
|
|
fd = open(sramfile, O_WRONLY|O_CREAT|O_TRUNC, 0666);
|
2007-02-06 21:41:08 +00:00
|
|
|
if (fd<0) return -1;
|
|
|
|
write(fd,ram.sbank, 8192*mbc.ramsize);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 0;
|
2005-03-02 23:49:38 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 18:16:22 +00:00
|
|
|
static void rtc_save(void)
|
2005-03-02 23:49:38 +00:00
|
|
|
{
|
2007-02-06 21:41:08 +00:00
|
|
|
int fd;
|
|
|
|
if (!rtc.batt) return;
|
2010-05-06 17:35:13 +00:00
|
|
|
if ((fd = open(rtcfile, O_WRONLY|O_CREAT|O_TRUNC, 0666))<0) return;
|
2007-02-06 21:41:08 +00:00
|
|
|
rtc_save_internal(fd);
|
|
|
|
close(fd);
|
2005-03-02 23:49:38 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 18:16:22 +00:00
|
|
|
static void rtc_load(void)
|
2005-03-02 23:49:38 +00:00
|
|
|
{
|
2007-02-06 21:41:08 +00:00
|
|
|
int fd;
|
|
|
|
if (!rtc.batt) return;
|
|
|
|
if ((fd = open(rtcfile, O_RDONLY))<0) return;
|
|
|
|
rtc_load_internal(fd);
|
|
|
|
close(fd);
|
2005-03-02 23:49:38 +00:00
|
|
|
}
|
|
|
|
|
2010-11-25 12:45:13 +00:00
|
|
|
void sn_save(void)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
if ((fd = open(snfile, O_WRONLY | O_CREAT, 0666)) < 0)
|
|
|
|
return;
|
|
|
|
savestate(fd);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sn_load(void)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
if ((fd = open(snfile, O_RDONLY, 0666)) < 0)
|
|
|
|
return;
|
|
|
|
loadstate(fd);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2005-03-02 23:49:38 +00:00
|
|
|
void cleanup(void)
|
|
|
|
{
|
2007-02-06 21:41:08 +00:00
|
|
|
sram_save();
|
|
|
|
rtc_save();
|
|
|
|
/* IDEA - if error, write emergency savestate..? */
|
2005-03-02 23:49:38 +00:00
|
|
|
}
|
|
|
|
|
2008-05-13 09:57:56 +00:00
|
|
|
void loader_init(const char *s)
|
2005-03-02 23:49:38 +00:00
|
|
|
{
|
2007-02-06 21:41:08 +00:00
|
|
|
romfile = s;
|
|
|
|
if(rom_load())
|
|
|
|
return;
|
|
|
|
|
|
|
|
snprintf(saveprefix, 499, "%s/%s", savedir, rom.name);
|
|
|
|
|
|
|
|
strcpy(sramfile, saveprefix);
|
|
|
|
strcat(sramfile, ".sav");
|
|
|
|
|
|
|
|
strcpy(rtcfile, saveprefix);
|
|
|
|
strcat(rtcfile, ".rtc");
|
2010-11-25 12:45:13 +00:00
|
|
|
strcpy(snfile, saveprefix);
|
|
|
|
strcat(snfile, ".sn");
|
2007-02-06 21:41:08 +00:00
|
|
|
|
|
|
|
sram_load();
|
|
|
|
rtc_load();
|
2005-03-02 23:49:38 +00:00
|
|
|
}
|