2021-03-02 02:13:17 +00:00
|
|
|
// Project64 - A Nintendo 64 emulator
|
2021-05-18 11:51:36 +00:00
|
|
|
// https://www.pj64-emu.com/
|
2021-03-02 02:13:17 +00:00
|
|
|
// Copyright(C) 2001-2021 Project64
|
|
|
|
// Copyright(C) 2015 Bobby Smiles
|
|
|
|
// GNU/GPLv2 licensed: https://gnu.org/licenses/gpl-2.0.html
|
2021-05-18 11:51:36 +00:00
|
|
|
|
2016-01-29 11:17:59 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "GBCart.h"
|
|
|
|
|
2016-02-03 13:33:03 +00:00
|
|
|
#include <time.h>
|
2016-02-04 19:09:56 +00:00
|
|
|
#include <memory>
|
2016-02-03 13:33:03 +00:00
|
|
|
|
2016-01-29 11:17:59 +00:00
|
|
|
static void read_gb_cart_normal(struct gb_cart* gb_cart, uint16_t address, uint8_t* data)
|
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
uint16_t offset;
|
|
|
|
|
|
|
|
if ((address >= 0x0000) && (address <= 0x7FFF))
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Read Game Boy cart
|
2016-10-09 09:44:31 +00:00
|
|
|
if (address >= gb_cart->rom_size)
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// If address is larger then our ROM size, bail out
|
2016-10-09 09:44:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy(data, &gb_cart->rom[address], 0x20);
|
|
|
|
}
|
|
|
|
else if ((address >= 0xA000) && (address <= 0xBFFF))
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Read from RAM
|
2021-04-12 11:35:39 +00:00
|
|
|
if (gb_cart->ram == nullptr)
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// No RAM to write to
|
2016-10-09 09:44:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = address - 0xA000;
|
|
|
|
if (offset >= gb_cart->ram_size)
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Offset is larger then our RAM size
|
2016-10-09 09:44:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&gb_cart->ram[offset], data, 0x20);
|
|
|
|
memcpy(data, &gb_cart->ram[offset], 0x20);
|
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void write_gb_cart_normal(struct gb_cart* gb_cart, uint16_t address, const uint8_t* data)
|
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
uint16_t offset;
|
|
|
|
if ((address >= 0xA000) && (address <= 0xBFFF))
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Write to RAM
|
2021-04-12 11:35:39 +00:00
|
|
|
if (gb_cart->ram == nullptr)
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// No RAM to write to
|
2016-10-09 09:44:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
|
2016-10-09 09:44:31 +00:00
|
|
|
offset = address - 0xa000;
|
|
|
|
if (offset >= gb_cart->ram_size)
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Offset is larger then our RAM size
|
2016-10-09 09:44:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&gb_cart->ram[offset], data, 0x20);
|
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void read_gb_cart_mbc1(struct gb_cart* gb_cart, uint16_t address, uint8_t* data)
|
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
size_t offset;
|
|
|
|
|
2021-05-18 11:51:36 +00:00
|
|
|
if ((address >= 0x0000) && (address <= 0x3FFF)) // No unbanked memory
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
|
|
|
memcpy(data, &gb_cart->rom[address], 0x20);
|
|
|
|
}
|
2021-05-18 11:51:36 +00:00
|
|
|
else if ((address >= 0x4000) && (address <= 0x7FFF)) // Read from ROM
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
|
|
|
offset = (address - 0x4000) + (gb_cart->rom_bank * 0x4000);
|
|
|
|
if (offset < gb_cart->rom_size)
|
|
|
|
{
|
|
|
|
memcpy(data, &gb_cart->rom[offset], 0x20);
|
|
|
|
}
|
|
|
|
}
|
2021-05-18 11:51:36 +00:00
|
|
|
else if ((address >= 0xA000) && (address <= 0xBFFF)) // Read from RAM
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (gb_cart->ram != nullptr)
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
|
|
|
offset = (address - 0xA000) + (gb_cart->ram_bank * 0x2000);
|
|
|
|
if (offset < gb_cart->ram_size)
|
|
|
|
{
|
|
|
|
memcpy(data, &gb_cart->ram[offset], 0x20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void write_gb_cart_mbc1(struct gb_cart* gb_cart, uint16_t address, const uint8_t* data)
|
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
size_t offset;
|
|
|
|
|
|
|
|
if ((address >= 0x0000) && (address <= 0x1FFF)) // RAM enable
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Enable/disable RAM
|
2016-10-09 09:44:31 +00:00
|
|
|
gb_cart->ram_enabled = (data[0] & 0x0F) == 0x0A;
|
|
|
|
}
|
|
|
|
else if ((address >= 0x2000) && (address <= 0x3FFF)) // ROM bank select
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
gb_cart->rom_bank &= 0x60; // Keep MSB
|
2016-10-09 09:44:31 +00:00
|
|
|
gb_cart->rom_bank |= data[0] & 0x1F;
|
|
|
|
|
2021-05-18 11:51:36 +00:00
|
|
|
// Emulate quirk: 0x00 -> 0x01, 0x20 -> 0x21, 0x40->0x41, 0x60 -> 0x61
|
2016-10-09 09:44:31 +00:00
|
|
|
if ((gb_cart->rom_bank & 0x1F) == 0)
|
|
|
|
{
|
|
|
|
gb_cart->rom_bank |= 0x01;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((address >= 0x4000) && (address <= 0x5FFF)) // RAM bank select
|
|
|
|
{
|
|
|
|
if (gb_cart->ram_bank_mode)
|
|
|
|
{
|
|
|
|
gb_cart->ram_bank = data[0] & 0x03;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gb_cart->rom_bank &= 0x1F;
|
2021-05-18 11:51:36 +00:00
|
|
|
gb_cart->rom_bank |= ((data[0] & 0x03) << 5); // Set bits 5 and 6 of ROM bank
|
2016-10-09 09:44:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((address >= 0x6000) && (address <= 0x7FFF)) // MBC1 mode select
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// This is overly complicated, but it keeps us from having to do bitwise math later
|
|
|
|
// Basically we shuffle the 2 "magic bits" between rom_bank and ram_bank as necessary
|
2016-10-09 09:44:31 +00:00
|
|
|
if (gb_cart->ram_bank_mode != (data[0] & 0x01))
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// We should only alter the ROM and RAM bank numbers if we have changed modes
|
2016-10-09 09:44:31 +00:00
|
|
|
gb_cart->ram_bank_mode = data[0] & 0x01;
|
|
|
|
if (gb_cart->ram_bank_mode)
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
gb_cart->ram_bank = gb_cart->rom_bank >> 5; // Set the RAM bank to the "magic bits"
|
|
|
|
gb_cart->rom_bank &= 0x1F; // Zero out bits 5 and 6 to keep consistency
|
2016-10-09 09:44:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gb_cart->rom_bank &= 0x1F;
|
|
|
|
gb_cart->rom_bank |= (gb_cart->ram_bank << 5);
|
2021-05-18 11:51:36 +00:00
|
|
|
gb_cart->ram_bank = 0x00; // We can only reach RAM page 0
|
2016-10-09 09:44:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((address >= 0xA000) && (address <= 0xBFFF)) // Write to RAM
|
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (gb_cart->ram != nullptr)
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
|
|
|
offset = (address - 0xA000) + (gb_cart->ram_bank * 0x2000);
|
|
|
|
if (offset < gb_cart->ram_size)
|
|
|
|
{
|
|
|
|
memcpy(&gb_cart->ram[offset], data, 0x20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void read_gb_cart_mbc2(struct gb_cart* gb_cart, uint16_t address, uint8_t* data)
|
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
size_t offset;
|
|
|
|
|
2021-05-18 11:51:36 +00:00
|
|
|
if ((address < 0x4000)) // ROM bank 0
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
|
|
|
memcpy(data, &gb_cart->rom[address], 0x20);
|
|
|
|
}
|
2021-05-18 11:51:36 +00:00
|
|
|
else if ((address >= 0x4000) && (address < 0x8000)) // Switchable ROM bank
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
|
|
|
offset = (address - 0x4000) + (gb_cart->rom_bank * 0x4000);
|
|
|
|
if (offset < gb_cart->rom_size)
|
|
|
|
{
|
|
|
|
memcpy(data, &gb_cart->rom[offset], 0x20);
|
|
|
|
}
|
|
|
|
}
|
2021-05-18 11:51:36 +00:00
|
|
|
else if ((address >= 0xA000) && (address <= 0xC000)) // Upper bounds of memory map
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (gb_cart->ram != nullptr)
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
|
|
|
offset = (address - 0xA000) + (gb_cart->ram_bank * 0x2000);
|
|
|
|
if (offset < gb_cart->ram_size)
|
|
|
|
{
|
|
|
|
memcpy(data, &gb_cart->ram[offset], 0x20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void write_gb_cart_mbc2(struct gb_cart* gb_cart, uint16_t address, const uint8_t* data)
|
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
size_t offset;
|
|
|
|
|
|
|
|
if ((address >= 0x0000) && (address <= 0x1FFF)) // We shouldn't be able to read/write to RAM unless this is toggled on
|
|
|
|
{
|
|
|
|
gb_cart->ram_enabled = (data[0] & 0x0F) == 0x0A;
|
|
|
|
}
|
|
|
|
else if ((address >= 0x2000) && (address <= 0x3FFF)) // ROM bank select
|
|
|
|
{
|
|
|
|
gb_cart->rom_bank = data[0] & 0x0F;
|
|
|
|
if (gb_cart->rom_bank == 0)
|
|
|
|
{
|
|
|
|
gb_cart->rom_bank = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((address >= 0x4000) && (address <= 0x5FFF)) // RAM bank select
|
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (gb_cart->ram != nullptr)
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
|
|
|
gb_cart->ram_bank = data[0] & 0x07;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((address >= 0xA000) && (address <= 0xBFFF)) // Write to RAM
|
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (gb_cart->ram != nullptr)
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
|
|
|
offset = (address - 0xA000) + (gb_cart->ram_bank * 0x2000);
|
|
|
|
if (offset < gb_cart->ram_size)
|
|
|
|
{
|
|
|
|
memcpy(&gb_cart->ram[offset], data, 0x20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 13:33:03 +00:00
|
|
|
void memoryUpdateMBC3Clock(struct gb_cart* gb_cart)
|
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
time_t now = time(nullptr);
|
2016-02-03 13:33:03 +00:00
|
|
|
time_t diff = now - gb_cart->rtc_last_time;
|
|
|
|
if (diff > 0) {
|
2021-05-18 11:51:36 +00:00
|
|
|
// Update the clock according to the last update time
|
2016-02-03 13:33:03 +00:00
|
|
|
gb_cart->rtc_data[0] += (int)(diff % 60);
|
|
|
|
if (gb_cart->rtc_data[0] > 59) {
|
|
|
|
gb_cart->rtc_data[0] -= 60;
|
|
|
|
gb_cart->rtc_data[1]++;
|
|
|
|
}
|
|
|
|
diff /= 60;
|
|
|
|
|
|
|
|
gb_cart->rtc_data[1] += (int)(diff % 60);
|
|
|
|
if (gb_cart->rtc_data[1] > 59) {
|
|
|
|
gb_cart->rtc_data[1] -= 60;
|
|
|
|
gb_cart->rtc_data[2]++;
|
|
|
|
}
|
|
|
|
diff /= 60;
|
|
|
|
|
|
|
|
gb_cart->rtc_data[2] += (int)(diff % 24);
|
|
|
|
if (gb_cart->rtc_data[2] > 23) {
|
|
|
|
gb_cart->rtc_data[2] -= 24;
|
|
|
|
gb_cart->rtc_data[3]++;
|
|
|
|
}
|
|
|
|
diff /= 24;
|
2016-02-04 01:57:09 +00:00
|
|
|
|
|
|
|
gb_cart->rtc_data[3] += (int)(diff & 0xFFFFFFFF);
|
2016-02-03 13:33:03 +00:00
|
|
|
if (gb_cart->rtc_data[3] > 255) {
|
|
|
|
if (gb_cart->rtc_data[3] > 511) {
|
|
|
|
gb_cart->rtc_data[3] %= 512;
|
|
|
|
gb_cart->rtc_data[3] |= 0x80;
|
|
|
|
}
|
2016-10-09 09:44:31 +00:00
|
|
|
gb_cart->rtc_data[4] = (gb_cart->rtc_data[4] & 0xFE) | (gb_cart->rtc_data[3] > 255 ? 1 : 0);
|
2016-02-03 13:33:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
gb_cart->rtc_last_time = now;
|
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
|
|
|
|
static void read_gb_cart_mbc3(struct gb_cart* gb_cart, uint16_t address, uint8_t* data)
|
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
size_t offset;
|
|
|
|
|
2021-05-18 11:51:36 +00:00
|
|
|
if ((address < 0x4000)) // ROM bank 0
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
|
|
|
memcpy(data, &gb_cart->rom[address], 0x20);
|
|
|
|
}
|
2021-05-18 11:51:36 +00:00
|
|
|
else if ((address >= 0x4000) && (address < 0x8000)) // Switchable ROM bank
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
|
|
|
offset = (address - 0x4000) + (gb_cart->rom_bank * 0x4000);
|
|
|
|
if (offset < gb_cart->rom_size)
|
|
|
|
{
|
|
|
|
memcpy(data, &gb_cart->rom[offset], 0x20);
|
|
|
|
}
|
|
|
|
}
|
2021-05-18 11:51:36 +00:00
|
|
|
else if ((address >= 0xA000) && (address <= 0xC000)) // Upper bounds of memory map
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (gb_cart->ram != nullptr)
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
2016-11-27 20:34:02 +00:00
|
|
|
if (gb_cart->ram_bank <= 0x03)
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
2016-02-02 08:44:18 +00:00
|
|
|
offset = (address - 0xA000) + (gb_cart->ram_bank * 0x2000);
|
|
|
|
if (offset < gb_cart->ram_size)
|
|
|
|
{
|
|
|
|
memcpy(data, &gb_cart->ram[offset], 0x20);
|
|
|
|
}
|
2016-10-09 09:44:31 +00:00
|
|
|
}
|
|
|
|
else if (gb_cart->has_rtc)
|
|
|
|
{
|
2016-02-03 13:33:03 +00:00
|
|
|
if (gb_cart->rtc_latch)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 32; i++)
|
|
|
|
{
|
2021-01-19 05:58:59 +00:00
|
|
|
data[i] = (uint8_t)(gb_cart->rtc_latch_data[gb_cart->ram_bank - 0x08]);
|
2016-02-03 13:33:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2016-01-30 14:52:26 +00:00
|
|
|
{
|
2016-02-03 13:33:03 +00:00
|
|
|
memoryUpdateMBC3Clock(gb_cart);
|
|
|
|
for (int i = 0; i < 32; i++)
|
|
|
|
{
|
2021-01-19 05:58:59 +00:00
|
|
|
data[i] = (uint8_t)(gb_cart->rtc_data[gb_cart->ram_bank - 0x08]);
|
2016-02-03 13:33:03 +00:00
|
|
|
}
|
2016-01-30 14:52:26 +00:00
|
|
|
}
|
2016-10-09 09:44:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void write_gb_cart_mbc3(struct gb_cart* gb_cart, uint16_t address, const uint8_t* data)
|
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
uint8_t bank;
|
|
|
|
size_t offset;
|
|
|
|
|
|
|
|
if ((address >= 0x0000) && (address <= 0x1FFF)) // We shouldn't be able to read/write to RAM unless this is toggled on
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Enable / Disable RAM -- NOT WORKING --
|
2016-10-09 09:44:31 +00:00
|
|
|
gb_cart->ram_enabled = (data[0] & 0x0F) == 0x0A;
|
|
|
|
}
|
|
|
|
else if ((address >= 0x2000) && (address <= 0x3FFF)) // ROM bank select
|
|
|
|
{
|
2016-01-30 14:52:26 +00:00
|
|
|
bank = data[0] & 0x7F;
|
2016-10-09 09:44:31 +00:00
|
|
|
gb_cart->rom_bank = (bank == 0) ? 1 : bank;
|
|
|
|
}
|
2021-05-18 11:51:36 +00:00
|
|
|
else if ((address >= 0x4000) && (address <= 0x5FFF)) // RAM/clock bank select
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (gb_cart->ram != nullptr)
|
2016-01-29 11:17:59 +00:00
|
|
|
{
|
|
|
|
bank = data[0];
|
|
|
|
if (gb_cart->has_rtc && (bank >= 0x8 && bank <= 0xc))
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Set the bank for the timer
|
2016-01-29 11:17:59 +00:00
|
|
|
gb_cart->ram_bank = bank;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gb_cart->ram_bank = bank & 0x03;
|
|
|
|
}
|
|
|
|
}
|
2016-10-09 09:44:31 +00:00
|
|
|
}
|
|
|
|
else if ((address >= 0x6000) && (address <= 0x7FFF)) // Latch timer data
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Implement RTC timer / latch
|
2016-01-30 14:52:26 +00:00
|
|
|
if (gb_cart->rtc_latch == 0 && data[0] == 1)
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Update time
|
2016-02-03 13:33:03 +00:00
|
|
|
memoryUpdateMBC3Clock(gb_cart);
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
gb_cart->rtc_latch_data[i] = gb_cart->rtc_data[i];
|
|
|
|
}
|
2016-01-30 14:52:26 +00:00
|
|
|
}
|
|
|
|
gb_cart->rtc_latch = data[0];
|
2016-10-09 09:44:31 +00:00
|
|
|
}
|
|
|
|
else if ((address >= 0xA000) && (address <= 0xBFFF)) // Write to RAM
|
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (gb_cart->ram != nullptr)
|
2016-01-29 11:17:59 +00:00
|
|
|
{
|
2016-11-27 20:34:02 +00:00
|
|
|
if (gb_cart->ram_bank <= 0x03)
|
2016-02-02 08:44:18 +00:00
|
|
|
{
|
|
|
|
offset = (address - 0xA000) + (gb_cart->ram_bank * 0x2000);
|
|
|
|
if (offset < gb_cart->ram_size)
|
|
|
|
{
|
|
|
|
memcpy(&gb_cart->ram[offset], data, 0x20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (gb_cart->has_rtc)
|
2016-01-29 11:17:59 +00:00
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// RTC write
|
2016-02-03 13:33:03 +00:00
|
|
|
gb_cart->rtc_data[gb_cart->ram_bank - 0x08] = data[0];
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-09 09:44:31 +00:00
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 05:58:59 +00:00
|
|
|
static void read_gb_cart_mbc4(struct gb_cart * /*gb_cart*/, uint16_t /*address*/, uint8_t * /*data*/)
|
2016-01-29 11:17:59 +00:00
|
|
|
{
|
|
|
|
g_Notify->BreakPoint(__FILE__, __LINE__);
|
|
|
|
}
|
|
|
|
|
2021-01-19 05:58:59 +00:00
|
|
|
static void write_gb_cart_mbc4(struct gb_cart * /*gb_cart*/, uint16_t /*address*/, const uint8_t * /*data*/)
|
2016-01-29 11:17:59 +00:00
|
|
|
{
|
|
|
|
g_Notify->BreakPoint(__FILE__, __LINE__);
|
|
|
|
}
|
|
|
|
|
2021-01-19 05:58:59 +00:00
|
|
|
static void read_gb_cart_mbc5(struct gb_cart * gb_cart, uint16_t address, uint8_t * data)
|
2016-01-29 11:17:59 +00:00
|
|
|
{
|
2016-02-01 08:58:37 +00:00
|
|
|
size_t offset;
|
|
|
|
|
2021-05-18 11:51:36 +00:00
|
|
|
if ((address < 0x4000)) // ROM bank 0
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
|
|
|
memcpy(data, &gb_cart->rom[address], 0x20);
|
|
|
|
}
|
2021-05-18 11:51:36 +00:00
|
|
|
else if ((address >= 0x4000) && (address < 0x8000)) // Switchable ROM bank
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
|
|
|
offset = (address - 0x4000) + (gb_cart->rom_bank * 0x4000);
|
|
|
|
if (offset < gb_cart->rom_size)
|
|
|
|
{
|
|
|
|
memcpy(data, &gb_cart->rom[offset], 0x20);
|
|
|
|
}
|
|
|
|
}
|
2021-05-18 11:51:36 +00:00
|
|
|
else if ((address >= 0xA000) && (address <= 0xC000)) // Upper bounds of memory map
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (gb_cart->ram != nullptr)
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
|
|
|
offset = (address - 0xA000) + (gb_cart->ram_bank * 0x2000);
|
|
|
|
if (offset < gb_cart->ram_size)
|
|
|
|
{
|
|
|
|
memcpy(data, &gb_cart->ram[offset], 0x20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void write_gb_cart_mbc5(struct gb_cart* gb_cart, uint16_t address, const uint8_t* data)
|
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
size_t offset;
|
2016-01-29 11:17:59 +00:00
|
|
|
|
2016-10-09 09:44:31 +00:00
|
|
|
if ((address >= 0x0000) && (address <= 0x1FFF)) // We shouldn't be able to read/write to RAM unless this is toggled on
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Enable / Disable RAM -- NOT WORKING --
|
2016-10-09 09:44:31 +00:00
|
|
|
gb_cart->ram_enabled = (data[0] & 0x0F) == 0x0A;
|
|
|
|
}
|
|
|
|
else if ((address >= 0x2000) && (address <= 0x2FFF)) // ROM bank select, low bits
|
|
|
|
{
|
|
|
|
gb_cart->rom_bank &= 0xff00;
|
|
|
|
gb_cart->rom_bank |= data[0];
|
|
|
|
}
|
|
|
|
else if ((address >= 0x3000) && (address <= 0x3FFF)) // ROM bank select, high bit
|
|
|
|
{
|
|
|
|
gb_cart->rom_bank &= 0x00ff;
|
|
|
|
gb_cart->rom_bank |= (data[0] & 0x01) << 8;
|
|
|
|
}
|
|
|
|
else if ((address >= 0x4000) && (address <= 0x5FFF)) // RAM bank select
|
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (gb_cart->ram != nullptr)
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
|
|
|
gb_cart->ram_bank = data[0] & 0x0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((address >= 0xA000) && (address <= 0xBFFF)) // Write to RAM
|
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (gb_cart->ram != nullptr)
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
|
|
|
offset = (address - 0xA000) + (gb_cart->ram_bank * 0x2000);
|
|
|
|
if (offset < gb_cart->ram_size)
|
|
|
|
{
|
|
|
|
memcpy(&gb_cart->ram[offset], data, 0x20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 05:58:59 +00:00
|
|
|
static void read_gb_cart_mmm01(struct gb_cart * /*gb_cart*/, uint16_t /*address*/, uint8_t * /*data*/)
|
2016-01-29 11:17:59 +00:00
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
g_Notify->BreakPoint(__FILE__, __LINE__);
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 05:58:59 +00:00
|
|
|
static void write_gb_cart_mmm01(struct gb_cart * /*gb_cart*/, uint16_t /*address*/, const uint8_t * /*data*/)
|
2016-01-29 11:17:59 +00:00
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
g_Notify->BreakPoint(__FILE__, __LINE__);
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 05:58:59 +00:00
|
|
|
static void read_gb_cart_pocket_cam(struct gb_cart * gb_cart, uint16_t address, uint8_t * data)
|
2016-01-29 11:17:59 +00:00
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
size_t offset;
|
|
|
|
|
2021-05-18 11:51:36 +00:00
|
|
|
if ((address < 0x4000)) // ROM bank 0
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
|
|
|
memcpy(data, &gb_cart->rom[address], 0x20);
|
|
|
|
}
|
2021-05-18 11:51:36 +00:00
|
|
|
else if ((address >= 0x4000) && (address < 0x8000)) // Switchable ROM bank
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
|
|
|
offset = (address - 0x4000) + (gb_cart->rom_bank * 0x4000);
|
|
|
|
if (offset < gb_cart->rom_size)
|
|
|
|
{
|
|
|
|
memcpy(data, &gb_cart->rom[offset], 0x20);
|
|
|
|
}
|
|
|
|
}
|
2021-05-18 11:51:36 +00:00
|
|
|
else if ((address >= 0xA000) && (address <= 0xC000)) // Upper bounds of memory map
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Check to see if where currently in register mode
|
2021-04-12 11:35:39 +00:00
|
|
|
if (gb_cart->ram != nullptr)
|
2016-01-29 11:17:59 +00:00
|
|
|
{
|
|
|
|
if (gb_cart->ram_bank & 0x10)
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// When in register mode, based off of N-Rage code we just fill the memory with zeroes
|
|
|
|
// Seems to be incorrect behavior, but need to find more documentation
|
2016-01-29 11:17:59 +00:00
|
|
|
memset(data, 0x00, 0x20);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Read RAM normally
|
2016-01-29 11:17:59 +00:00
|
|
|
offset = (address - 0xA000) + (gb_cart->ram_bank * 0x2000);
|
|
|
|
if (offset < gb_cart->ram_size)
|
|
|
|
{
|
|
|
|
memcpy(data, &gb_cart->ram[offset], 0x20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-09 09:44:31 +00:00
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void write_gb_cart_pocket_cam(struct gb_cart* gb_cart, uint16_t address, const uint8_t* data)
|
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
size_t offset;
|
|
|
|
|
|
|
|
if ((address >= 0x0000) && (address <= 0x1FFF)) // We shouldn't be able to read/write to RAM unless this is toggled on
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Enable / Disable RAM
|
2016-10-09 09:44:31 +00:00
|
|
|
gb_cart->ram_enabled = (data[0] & 0x0F) == 0x0A;
|
|
|
|
}
|
|
|
|
else if ((address >= 0x2000) && (address <= 0x2FFF)) // ROM bank select, low bits
|
|
|
|
{
|
|
|
|
gb_cart->rom_bank &= 0xFF00;
|
|
|
|
gb_cart->rom_bank |= data[0];
|
|
|
|
}
|
2021-05-18 11:51:36 +00:00
|
|
|
else if ((address >= 0x4000) && (address <= 0x4FFF)) // Camera register and RAM bank select
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (gb_cart->ram != nullptr)
|
2016-01-29 11:17:59 +00:00
|
|
|
{
|
|
|
|
if (data[0] & 0x10)
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Register mode
|
2016-01-29 11:17:59 +00:00
|
|
|
gb_cart->ram_bank = data[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// RAM mode
|
2016-01-29 11:17:59 +00:00
|
|
|
gb_cart->ram_bank = data[0] & 0x0F;
|
|
|
|
}
|
|
|
|
}
|
2016-10-09 09:44:31 +00:00
|
|
|
}
|
|
|
|
else if ((address >= 0xA000) && (address <= 0xBFFF)) // Write to RAM
|
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (gb_cart->ram != nullptr)
|
2016-01-29 11:17:59 +00:00
|
|
|
{
|
|
|
|
if (gb_cart->ram_bank & 0x10)
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Register mode (do nothing)
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// RAM mode
|
2016-01-29 11:17:59 +00:00
|
|
|
offset = (address - 0xA000) + (gb_cart->ram_bank * 0x2000);
|
|
|
|
if (offset < gb_cart->ram_size)
|
|
|
|
{
|
|
|
|
memcpy(&gb_cart->ram[offset], data, 0x20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-09 09:44:31 +00:00
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 05:58:59 +00:00
|
|
|
static void read_gb_cart_bandai_tama5(struct gb_cart * /*gb_cart*/, uint16_t /*address*/, uint8_t * /*data*/)
|
2016-01-29 11:17:59 +00:00
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
g_Notify->BreakPoint(__FILE__, __LINE__);
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 05:58:59 +00:00
|
|
|
static void write_gb_cart_bandai_tama5(struct gb_cart * /*gb_cart*/, uint16_t /*address*/, const uint8_t * /*data*/)
|
2016-01-29 11:17:59 +00:00
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
g_Notify->BreakPoint(__FILE__, __LINE__);
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 05:58:59 +00:00
|
|
|
static void read_gb_cart_huc1(struct gb_cart * /*gb_cart*/, uint16_t /*address*/, uint8_t * /*data*/)
|
2016-01-29 11:17:59 +00:00
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
g_Notify->BreakPoint(__FILE__, __LINE__);
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 05:58:59 +00:00
|
|
|
static void write_gb_cart_huc1(struct gb_cart * /*gb_cart*/, uint16_t /*address*/, const uint8_t * /*data*/)
|
2016-01-29 11:17:59 +00:00
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
g_Notify->BreakPoint(__FILE__, __LINE__);
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 05:58:59 +00:00
|
|
|
static void read_gb_cart_huc3(struct gb_cart * /*gb_cart*/, uint16_t /*address*/, uint8_t * /*data*/)
|
2016-01-29 11:17:59 +00:00
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
g_Notify->BreakPoint(__FILE__, __LINE__);
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 05:58:59 +00:00
|
|
|
static void write_gb_cart_huc3(struct gb_cart * /*gb_cart*/, uint16_t /*address*/, const uint8_t * /*data*/)
|
2016-01-29 11:17:59 +00:00
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
g_Notify->BreakPoint(__FILE__, __LINE__);
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum gbcart_extra_devices
|
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
GED_NONE = 0x00,
|
|
|
|
GED_RAM = 0x01,
|
|
|
|
GED_BATTERY = 0x02,
|
|
|
|
GED_RTC = 0x04,
|
|
|
|
GED_RUMBLE = 0x08
|
2016-01-29 11:17:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct parsed_cart_type
|
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
void(*read_gb_cart)(struct gb_cart*, uint16_t, uint8_t*);
|
|
|
|
void(*write_gb_cart)(struct gb_cart*, uint16_t, const uint8_t*);
|
|
|
|
unsigned int extra_devices;
|
2016-01-29 11:17:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct parsed_cart_type* parse_cart_type(uint8_t cart_type)
|
|
|
|
{
|
|
|
|
#define MBC(x) read_gb_cart_ ## x, write_gb_cart_ ## x
|
2016-10-09 09:44:31 +00:00
|
|
|
static const struct parsed_cart_type GB_CART_TYPES[] =
|
|
|
|
{
|
|
|
|
{ MBC(normal), GED_NONE },
|
|
|
|
{ MBC(mbc1), GED_NONE },
|
|
|
|
{ MBC(mbc1), GED_RAM },
|
|
|
|
{ MBC(mbc1), GED_RAM | GED_BATTERY },
|
|
|
|
{ MBC(mbc2), GED_NONE },
|
|
|
|
{ MBC(mbc2), GED_BATTERY },
|
|
|
|
{ MBC(normal), GED_RAM },
|
|
|
|
{ MBC(normal), GED_RAM | GED_BATTERY },
|
|
|
|
{ MBC(mmm01), GED_NONE },
|
|
|
|
{ MBC(mmm01), GED_RAM },
|
|
|
|
{ MBC(mmm01), GED_RAM | GED_BATTERY },
|
|
|
|
{ MBC(mbc3), GED_BATTERY | GED_RTC },
|
|
|
|
{ MBC(mbc3), GED_RAM | GED_BATTERY | GED_RTC },
|
|
|
|
{ MBC(mbc3), GED_NONE },
|
|
|
|
{ MBC(mbc3), GED_RAM },
|
|
|
|
{ MBC(mbc3), GED_RAM | GED_BATTERY },
|
|
|
|
{ MBC(mbc4), GED_NONE },
|
|
|
|
{ MBC(mbc4), GED_RAM },
|
|
|
|
{ MBC(mbc4), GED_RAM | GED_BATTERY },
|
|
|
|
{ MBC(mbc5), GED_NONE },
|
|
|
|
{ MBC(mbc5), GED_RAM },
|
|
|
|
{ MBC(mbc5), GED_RAM | GED_BATTERY },
|
|
|
|
{ MBC(mbc5), GED_RUMBLE },
|
|
|
|
{ MBC(mbc5), GED_RAM | GED_RUMBLE },
|
|
|
|
{ MBC(mbc5), GED_RAM | GED_BATTERY | GED_RUMBLE },
|
|
|
|
{ MBC(pocket_cam), GED_NONE },
|
|
|
|
{ MBC(bandai_tama5), GED_NONE },
|
|
|
|
{ MBC(huc3), GED_NONE },
|
|
|
|
{ MBC(huc1), GED_RAM | GED_BATTERY }
|
|
|
|
};
|
2016-01-29 11:17:59 +00:00
|
|
|
#undef MBC
|
|
|
|
|
2016-10-09 09:44:31 +00:00
|
|
|
switch (cart_type)
|
|
|
|
{
|
|
|
|
case 0x00: return &GB_CART_TYPES[0];
|
|
|
|
case 0x01: return &GB_CART_TYPES[1];
|
|
|
|
case 0x02: return &GB_CART_TYPES[2];
|
|
|
|
case 0x03: return &GB_CART_TYPES[3];
|
|
|
|
case 0x05: return &GB_CART_TYPES[4];
|
|
|
|
case 0x06: return &GB_CART_TYPES[5];
|
|
|
|
case 0x08: return &GB_CART_TYPES[6];
|
|
|
|
case 0x09: return &GB_CART_TYPES[7];
|
|
|
|
case 0x0B: return &GB_CART_TYPES[8];
|
|
|
|
case 0x0C: return &GB_CART_TYPES[9];
|
|
|
|
case 0x0D: return &GB_CART_TYPES[10];
|
|
|
|
case 0x0F: return &GB_CART_TYPES[11];
|
|
|
|
case 0x10: return &GB_CART_TYPES[12];
|
|
|
|
case 0x11: return &GB_CART_TYPES[13];
|
|
|
|
case 0x12: return &GB_CART_TYPES[14];
|
|
|
|
case 0x13: return &GB_CART_TYPES[15];
|
|
|
|
case 0x15: return &GB_CART_TYPES[16];
|
|
|
|
case 0x16: return &GB_CART_TYPES[17];
|
|
|
|
case 0x17: return &GB_CART_TYPES[18];
|
|
|
|
case 0x19: return &GB_CART_TYPES[19];
|
|
|
|
case 0x1A: return &GB_CART_TYPES[20];
|
|
|
|
case 0x1B: return &GB_CART_TYPES[21];
|
|
|
|
case 0x1C: return &GB_CART_TYPES[22];
|
|
|
|
case 0x1D: return &GB_CART_TYPES[23];
|
|
|
|
case 0x1E: return &GB_CART_TYPES[24];
|
|
|
|
case 0xFC: return &GB_CART_TYPES[25];
|
|
|
|
case 0xFD: return &GB_CART_TYPES[26];
|
|
|
|
case 0xFE: return &GB_CART_TYPES[27];
|
|
|
|
case 0xFF: return &GB_CART_TYPES[28];
|
2021-04-12 11:35:39 +00:00
|
|
|
default: return nullptr;
|
2016-10-09 09:44:31 +00:00
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
2016-02-01 08:58:37 +00:00
|
|
|
bool GBCart::init_gb_cart(struct gb_cart* gb_cart, const char* gb_file)
|
2016-01-29 11:17:59 +00:00
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
const struct parsed_cart_type* type;
|
2021-04-12 06:04:59 +00:00
|
|
|
std::unique_ptr<uint8_t> rom;
|
2016-02-03 02:34:52 +00:00
|
|
|
size_t rom_size = 0;
|
2021-04-12 06:04:59 +00:00
|
|
|
std::unique_ptr<uint8_t> ram;
|
2016-02-03 02:34:52 +00:00
|
|
|
size_t ram_size = 0;
|
|
|
|
CFile tempFile;
|
|
|
|
|
2021-05-18 11:51:36 +00:00
|
|
|
// Load Game Boy cart ROM
|
2016-02-03 02:34:52 +00:00
|
|
|
if (!tempFile.Open(gb_file, CFileBase::modeRead))
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
g_Notify->DisplayError("Failed to open Transfer Pak ROM");
|
2016-02-03 02:34:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
|
2016-02-03 02:34:52 +00:00
|
|
|
rom_size = tempFile.GetLength();
|
|
|
|
rom.reset(new uint8_t[rom_size]);
|
|
|
|
|
|
|
|
tempFile.Read(rom.get(), rom_size);
|
|
|
|
tempFile.Close();
|
2016-01-29 11:17:59 +00:00
|
|
|
|
2016-10-09 09:44:31 +00:00
|
|
|
if (rom_size < 0x8000)
|
|
|
|
{
|
2016-02-01 08:58:37 +00:00
|
|
|
return false;
|
2016-10-09 09:44:31 +00:00
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
|
2021-05-18 11:51:36 +00:00
|
|
|
// Get and parse cart type
|
2016-10-09 09:44:31 +00:00
|
|
|
uint8_t cart_type = rom.get()[0x147];
|
|
|
|
type = parse_cart_type(cart_type);
|
2021-04-12 11:35:39 +00:00
|
|
|
if (type == nullptr)
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
2016-02-01 08:58:37 +00:00
|
|
|
return false;
|
2016-10-09 09:44:31 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 11:51:36 +00:00
|
|
|
// Load RAM (if present)
|
2016-10-09 09:44:31 +00:00
|
|
|
if (type->extra_devices & GED_RAM)
|
|
|
|
{
|
|
|
|
ram_size = 0;
|
|
|
|
switch (rom.get()[0x149])
|
|
|
|
{
|
|
|
|
case 0x01: ram_size = 1 * 0x800; break;
|
|
|
|
case 0x02: ram_size = 4 * 0x800; break;
|
|
|
|
case 0x03: ram_size = 16 * 0x800; break;
|
|
|
|
case 0x04: ram_size = 64 * 0x800; break;
|
|
|
|
case 0x05: ram_size = 32 * 0x800; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ram_size != 0)
|
|
|
|
{
|
2016-02-03 02:34:52 +00:00
|
|
|
ram.reset(new uint8_t[ram_size]);
|
2021-04-12 11:35:39 +00:00
|
|
|
if (ram.get() == nullptr)
|
2016-10-09 09:44:31 +00:00
|
|
|
{
|
2016-02-01 08:58:37 +00:00
|
|
|
return false;
|
2016-10-09 09:44:31 +00:00
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
|
2016-02-03 02:34:52 +00:00
|
|
|
if (!tempFile.Open(g_Settings->LoadStringVal(Game_Transferpak_Sav).c_str(), CFileBase::modeRead))
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
g_Notify->DisplayError("Failed to open Transfer Pak SAV file");
|
2016-02-03 02:34:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
tempFile.Read(ram.get(), ram_size);
|
2016-10-09 09:44:31 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 11:51:36 +00:00
|
|
|
// If we have RTC we need to load in the data, we assume the save will use the VBA-M format
|
2016-01-30 14:52:26 +00:00
|
|
|
if (type->extra_devices & GED_RTC)
|
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
tempFile.Read(&gb_cart->rtc_data[0], 4);
|
|
|
|
tempFile.Read(&gb_cart->rtc_data[1], 4);
|
|
|
|
tempFile.Read(&gb_cart->rtc_data[2], 4);
|
|
|
|
tempFile.Read(&gb_cart->rtc_data[3], 4);
|
|
|
|
tempFile.Read(&gb_cart->rtc_data[4], 4);
|
2016-02-03 13:33:03 +00:00
|
|
|
tempFile.Read(&gb_cart->rtc_latch_data[0], 4);
|
|
|
|
tempFile.Read(&gb_cart->rtc_latch_data[1], 4);
|
|
|
|
tempFile.Read(&gb_cart->rtc_latch_data[2], 4);
|
|
|
|
tempFile.Read(&gb_cart->rtc_latch_data[3], 4);
|
|
|
|
tempFile.Read(&gb_cart->rtc_latch_data[4], 4);
|
2016-10-09 09:44:31 +00:00
|
|
|
tempFile.Read(&gb_cart->rtc_last_time, 8);
|
2016-02-03 13:33:03 +00:00
|
|
|
memoryUpdateMBC3Clock(gb_cart);
|
2016-01-30 14:52:26 +00:00
|
|
|
}
|
2016-02-03 13:33:03 +00:00
|
|
|
tempFile.Close();
|
2016-10-09 09:44:31 +00:00
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
|
2021-05-18 11:51:36 +00:00
|
|
|
// Update gb_cart
|
2016-02-03 02:34:52 +00:00
|
|
|
gb_cart->rom = rom.release();
|
2016-02-03 13:33:03 +00:00
|
|
|
gb_cart->ram = ram.release();
|
2016-10-09 09:44:31 +00:00
|
|
|
gb_cart->rom_size = rom_size;
|
|
|
|
gb_cart->ram_size = ram_size;
|
|
|
|
gb_cart->rom_bank = 1;
|
|
|
|
gb_cart->ram_bank = 0;
|
|
|
|
gb_cart->has_rtc = (type->extra_devices & GED_RTC) ? 1 : 0;
|
|
|
|
gb_cart->read_gb_cart = type->read_gb_cart;
|
|
|
|
gb_cart->write_gb_cart = type->write_gb_cart;
|
|
|
|
return true;
|
2016-02-01 08:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBCart::save_gb_cart(struct gb_cart* gb_cart)
|
|
|
|
{
|
2016-02-03 02:34:52 +00:00
|
|
|
CFile ramFile;
|
|
|
|
ramFile.Open(g_Settings->LoadStringVal(Game_Transferpak_Sav).c_str(), CFileBase::modeWrite | CFileBase::modeCreate);
|
2016-02-03 13:33:03 +00:00
|
|
|
ramFile.Write(gb_cart->ram, gb_cart->ram_size);
|
|
|
|
|
2016-02-01 08:58:37 +00:00
|
|
|
if (gb_cart->has_rtc)
|
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
ramFile.Write(&gb_cart->rtc_data[0], 4);
|
|
|
|
ramFile.Write(&gb_cart->rtc_data[1], 4);
|
|
|
|
ramFile.Write(&gb_cart->rtc_data[2], 4);
|
|
|
|
ramFile.Write(&gb_cart->rtc_data[3], 4);
|
|
|
|
ramFile.Write(&gb_cart->rtc_data[4], 4);
|
2016-02-03 13:33:03 +00:00
|
|
|
ramFile.Write(&gb_cart->rtc_latch_data[0], 4);
|
|
|
|
ramFile.Write(&gb_cart->rtc_latch_data[1], 4);
|
|
|
|
ramFile.Write(&gb_cart->rtc_latch_data[2], 4);
|
|
|
|
ramFile.Write(&gb_cart->rtc_latch_data[3], 4);
|
|
|
|
ramFile.Write(&gb_cart->rtc_latch_data[4], 4);
|
2016-10-09 09:44:31 +00:00
|
|
|
ramFile.Write(&gb_cart->rtc_last_time, 8);
|
2016-02-01 08:58:37 +00:00
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
|
2016-02-03 02:34:52 +00:00
|
|
|
ramFile.Close();
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBCart::release_gb_cart(struct gb_cart* gb_cart)
|
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (gb_cart->rom != nullptr)
|
2016-10-09 09:44:31 +00:00
|
|
|
delete gb_cart->rom;
|
2016-01-29 11:17:59 +00:00
|
|
|
|
2021-04-12 11:35:39 +00:00
|
|
|
if (gb_cart->ram != nullptr)
|
2016-02-03 02:42:06 +00:00
|
|
|
delete gb_cart->ram;
|
2016-01-29 11:17:59 +00:00
|
|
|
|
2016-10-09 09:44:31 +00:00
|
|
|
memset(gb_cart, 0, sizeof(*gb_cart));
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBCart::read_gb_cart(struct gb_cart* gb_cart, uint16_t address, uint8_t* data)
|
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
gb_cart->read_gb_cart(gb_cart, address, data);
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GBCart::write_gb_cart(struct gb_cart* gb_cart, uint16_t address, const uint8_t* data)
|
|
|
|
{
|
2016-10-09 09:44:31 +00:00
|
|
|
gb_cart->write_gb_cart(gb_cart, address, data);
|
|
|
|
}
|