2016-07-09 14:34:55 +00:00
|
|
|
#include <stdbool.h>
|
2016-07-09 16:25:13 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2016-07-09 14:34:55 +00:00
|
|
|
#include "gb.h"
|
|
|
|
|
|
|
|
const GB_cartridge_t GB_cart_defs[256] = {
|
|
|
|
// From http://gbdev.gg8.se/wiki/articles/The_Cartridge_Header#0147_-_Cartridge_Type
|
2017-02-22 22:27:08 +00:00
|
|
|
/* MBC SUBTYPE RAM BAT. RTC RUMB. */
|
2016-10-17 15:51:43 +00:00
|
|
|
{ GB_NO_MBC, GB_STANDARD_MBC, false, false, false, false}, // 00h ROM ONLY
|
2016-10-01 23:15:03 +00:00
|
|
|
{ GB_MBC1 , GB_STANDARD_MBC, false, false, false, false}, // 01h MBC1
|
|
|
|
{ GB_MBC1 , GB_STANDARD_MBC, true , false, false, false}, // 02h MBC1+RAM
|
|
|
|
{ GB_MBC1 , GB_STANDARD_MBC, true , true , false, false}, // 03h MBC1+RAM+BATTERY
|
2016-07-09 14:34:55 +00:00
|
|
|
[5] =
|
2016-10-01 23:15:03 +00:00
|
|
|
{ GB_MBC2 , GB_STANDARD_MBC, true , false, false, false}, // 05h MBC2
|
|
|
|
{ GB_MBC2 , GB_STANDARD_MBC, true , true , false, false}, // 06h MBC2+BATTERY
|
2016-07-09 14:34:55 +00:00
|
|
|
[8] =
|
2016-10-01 23:15:03 +00:00
|
|
|
{ GB_NO_MBC, GB_STANDARD_MBC, true , false, false, false}, // 08h ROM+RAM
|
|
|
|
{ GB_NO_MBC, GB_STANDARD_MBC, true , true , false, false}, // 09h ROM+RAM+BATTERY
|
2016-07-09 14:34:55 +00:00
|
|
|
[0xB] =
|
2016-10-17 15:51:43 +00:00
|
|
|
/* Todo: Not supported yet */
|
2016-10-01 23:15:03 +00:00
|
|
|
{ GB_NO_MBC, GB_STANDARD_MBC, false, false, false, false}, // 0Bh MMM01
|
|
|
|
{ GB_NO_MBC, GB_STANDARD_MBC, false, false, false, false}, // 0Ch MMM01+RAM
|
|
|
|
{ GB_NO_MBC, GB_STANDARD_MBC, false, false, false, false}, // 0Dh MMM01+RAM+BATTERY
|
2016-07-09 14:34:55 +00:00
|
|
|
[0xF] =
|
2016-10-01 23:15:03 +00:00
|
|
|
{ GB_MBC3 , GB_STANDARD_MBC, false, true, true , false}, // 0Fh MBC3+TIMER+BATTERY
|
|
|
|
{ GB_MBC3 , GB_STANDARD_MBC, true , true, true , false}, // 10h MBC3+TIMER+RAM+BATTERY
|
|
|
|
{ GB_MBC3 , GB_STANDARD_MBC, false, false, false, false}, // 11h MBC3
|
|
|
|
{ GB_MBC3 , GB_STANDARD_MBC, true , false, false, false}, // 12h MBC3+RAM
|
|
|
|
{ GB_MBC3 , GB_STANDARD_MBC, true , true , false, false}, // 13h MBC3+RAM+BATTERY
|
2016-07-09 14:34:55 +00:00
|
|
|
[0x19] =
|
2016-10-01 23:15:03 +00:00
|
|
|
{ GB_MBC5 , GB_STANDARD_MBC, false, false, false, false}, // 19h MBC5
|
|
|
|
{ GB_MBC5 , GB_STANDARD_MBC, true , false, false, false}, // 1Ah MBC5+RAM
|
|
|
|
{ GB_MBC5 , GB_STANDARD_MBC, true , true , false, false}, // 1Bh MBC5+RAM+BATTERY
|
|
|
|
{ GB_MBC5 , GB_STANDARD_MBC, false, false, false, true }, // 1Ch MBC5+RUMBLE
|
|
|
|
{ GB_MBC5 , GB_STANDARD_MBC, true , false, false, true }, // 1Dh MBC5+RUMBLE+RAM
|
|
|
|
{ GB_MBC5 , GB_STANDARD_MBC, true , true , false, true }, // 1Eh MBC5+RUMBLE+RAM+BATTERY
|
2016-07-09 14:34:55 +00:00
|
|
|
[0xFC] =
|
2016-10-17 15:51:43 +00:00
|
|
|
{ GB_MBC5 , GB_CAMERA , true , true , false, false}, // FCh POCKET CAMERA
|
|
|
|
{ GB_NO_MBC, GB_STANDARD_MBC, false, false, false, false}, // FDh BANDAI TAMA5 (Todo: Not supported)
|
|
|
|
{ GB_HUC3 , GB_STANDARD_MBC, true , true , false, false}, // FEh HuC3 (Todo: Mapper support only)
|
2017-02-16 19:07:35 +00:00
|
|
|
{ GB_HUC1 , GB_STANDARD_MBC, true , true , false, false}, // FFh HuC1+RAM+BATTERY (Todo: No IR bindings)
|
2016-07-09 14:34:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void GB_update_mbc_mappings(GB_gameboy_t *gb)
|
|
|
|
{
|
|
|
|
switch (gb->cartridge_type->mbc_type) {
|
2016-10-17 15:51:43 +00:00
|
|
|
case GB_NO_MBC: return;
|
2016-07-09 14:34:55 +00:00
|
|
|
case GB_MBC1:
|
2017-06-20 22:07:11 +00:00
|
|
|
switch (gb->mbc1_wiring) {
|
|
|
|
case GB_STANDARD_MBC1_WIRING:
|
|
|
|
gb->mbc_rom_bank = gb->mbc1.bank_low | (gb->mbc1.bank_high << 5);
|
|
|
|
if (gb->mbc1.mode == 0) {
|
2016-07-09 16:25:13 +00:00
|
|
|
gb->mbc_ram_bank = 0;
|
2017-06-19 16:46:09 +00:00
|
|
|
gb->mbc_rom0_bank = 0;
|
2017-06-20 22:07:11 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-07-09 16:25:13 +00:00
|
|
|
gb->mbc_ram_bank = gb->mbc1.bank_high;
|
2017-06-19 16:46:09 +00:00
|
|
|
gb->mbc_rom0_bank = gb->mbc1.bank_high << 5;
|
2017-06-20 22:07:11 +00:00
|
|
|
}
|
|
|
|
if ((gb->mbc_rom_bank & 0x1F) == 0) {
|
|
|
|
gb->mbc_rom_bank++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GB_MBC1M_WIRING:
|
|
|
|
gb->mbc_rom_bank = (gb->mbc1.bank_low & 0xF) | (gb->mbc1.bank_high << 4);
|
|
|
|
if (gb->mbc1.mode == 0) {
|
|
|
|
gb->mbc_ram_bank = 0;
|
|
|
|
gb->mbc_rom0_bank = 0;
|
|
|
|
}
|
|
|
|
else {
|
2016-07-09 16:25:13 +00:00
|
|
|
gb->mbc_rom0_bank = gb->mbc1.bank_high << 4;
|
|
|
|
gb->mbc_ram_bank = 0;
|
2017-06-20 22:07:11 +00:00
|
|
|
}
|
|
|
|
if ((gb->mbc1.bank_low & 0x1F) == 0) {
|
|
|
|
gb->mbc_rom_bank++;
|
|
|
|
}
|
|
|
|
break;
|
2016-07-09 14:34:55 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GB_MBC2:
|
|
|
|
gb->mbc_rom_bank = gb->mbc2.rom_bank;
|
2017-06-20 22:07:11 +00:00
|
|
|
if ((gb->mbc_rom_bank & 0xF) == 0) {
|
|
|
|
gb->mbc_rom_bank = 1;
|
|
|
|
}
|
2016-07-09 14:34:55 +00:00
|
|
|
break;
|
|
|
|
case GB_MBC3:
|
|
|
|
gb->mbc_rom_bank = gb->mbc3.rom_bank;
|
|
|
|
gb->mbc_ram_bank = gb->mbc3.ram_bank;
|
2020-04-08 16:07:29 +00:00
|
|
|
if (!gb->is_mbc30) {
|
|
|
|
gb->mbc_rom_bank &= 0x7F;
|
|
|
|
gb->mbc_ram_bank &= 0x3;
|
|
|
|
}
|
2017-06-20 22:07:11 +00:00
|
|
|
if (gb->mbc_rom_bank == 0) {
|
|
|
|
gb->mbc_rom_bank = 1;
|
|
|
|
}
|
2016-07-09 14:34:55 +00:00
|
|
|
break;
|
|
|
|
case GB_MBC5:
|
|
|
|
gb->mbc_rom_bank = gb->mbc5.rom_bank_low | (gb->mbc5.rom_bank_high << 8);
|
|
|
|
gb->mbc_ram_bank = gb->mbc5.ram_bank;
|
|
|
|
break;
|
2017-02-16 19:07:35 +00:00
|
|
|
case GB_HUC1:
|
|
|
|
if (gb->huc1.mode == 0) {
|
|
|
|
gb->mbc_rom_bank = gb->huc1.bank_low | (gb->mbc1.bank_high << 6);
|
|
|
|
gb->mbc_ram_bank = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
gb->mbc_rom_bank = gb->huc1.bank_low;
|
|
|
|
gb->mbc_ram_bank = gb->huc1.bank_high;
|
|
|
|
}
|
|
|
|
break;
|
2016-10-17 15:51:43 +00:00
|
|
|
case GB_HUC3:
|
|
|
|
gb->mbc_rom_bank = gb->huc3.rom_bank;
|
|
|
|
gb->mbc_ram_bank = gb->huc3.ram_bank;
|
|
|
|
break;
|
2016-07-09 14:34:55 +00:00
|
|
|
}
|
2016-07-09 16:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GB_configure_cart(GB_gameboy_t *gb)
|
|
|
|
{
|
|
|
|
gb->cartridge_type = &GB_cart_defs[gb->rom[0x147]];
|
2017-02-24 13:14:47 +00:00
|
|
|
|
|
|
|
if (gb->rom[0x147] == 0 && gb->rom_size > 0x8000) {
|
|
|
|
GB_log(gb, "ROM header reports no MBC, but file size is over 32Kb. Assuming cartridge uses MBC3.\n");
|
|
|
|
gb->cartridge_type = &GB_cart_defs[0x11];
|
|
|
|
}
|
|
|
|
else if (gb->rom[0x147] != 0 && memcmp(gb->cartridge_type, &GB_cart_defs[0], sizeof(GB_cart_defs[0])) == 0) {
|
2016-09-03 01:00:37 +00:00
|
|
|
GB_log(gb, "Cartridge type %02x is not yet supported.\n", gb->rom[0x147]);
|
|
|
|
}
|
|
|
|
|
2016-07-09 16:25:13 +00:00
|
|
|
if (gb->cartridge_type->has_ram) {
|
2016-09-30 20:34:06 +00:00
|
|
|
if (gb->cartridge_type->mbc_type == GB_MBC2) {
|
|
|
|
gb->mbc_ram_size = 0x200;
|
|
|
|
}
|
|
|
|
else {
|
2020-04-09 11:32:52 +00:00
|
|
|
static const unsigned ram_sizes[256] = {0, 0x800, 0x2000, 0x8000, 0x20000, 0x10000};
|
2016-09-30 20:34:06 +00:00
|
|
|
gb->mbc_ram_size = ram_sizes[gb->rom[0x149]];
|
|
|
|
}
|
2016-07-09 16:25:13 +00:00
|
|
|
gb->mbc_ram = malloc(gb->mbc_ram_size);
|
2016-09-30 20:34:06 +00:00
|
|
|
|
2017-05-27 14:30:12 +00:00
|
|
|
/* Todo: Some games assume unintialized MBC RAM is 0xFF. It this true for all cartridges types? */
|
2016-09-10 10:25:18 +00:00
|
|
|
memset(gb->mbc_ram, 0xFF, gb->mbc_ram_size);
|
2016-07-09 16:25:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* MBC1 has at least 3 types of wiring (We currently support two (Standard and 4bit-MBC1M) of these).
|
|
|
|
See http://forums.nesdev.com/viewtopic.php?f=20&t=14099 */
|
|
|
|
|
|
|
|
/* Attempt to "guess" wiring */
|
|
|
|
if (gb->cartridge_type->mbc_type == GB_MBC1) {
|
|
|
|
if (gb->rom_size >= 0x44000 && memcmp(gb->rom + 0x104, gb->rom + 0x40104, 0x30) == 0) {
|
|
|
|
gb->mbc1_wiring = GB_MBC1M_WIRING;
|
|
|
|
}
|
|
|
|
}
|
2017-02-16 21:13:55 +00:00
|
|
|
|
2020-04-08 16:07:29 +00:00
|
|
|
/* Detect MBC30 */
|
|
|
|
if (gb->cartridge_type->mbc_type == GB_MBC3) {
|
|
|
|
if (gb->rom_size > 0x200000 || gb->mbc_ram_size > 0x8000) {
|
|
|
|
gb->is_mbc30 = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 21:13:55 +00:00
|
|
|
/* Set MBC5's bank to 1 correctly */
|
|
|
|
if (gb->cartridge_type->mbc_type == GB_MBC5) {
|
|
|
|
gb->mbc5.rom_bank_low = 1;
|
|
|
|
}
|
2017-02-16 19:07:35 +00:00
|
|
|
}
|