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"
|
|
|
|
#include "Transferpak.h"
|
|
|
|
|
|
|
|
static transferpak tpak;
|
|
|
|
|
|
|
|
uint16_t gb_cart_address(unsigned int bank, uint16_t address)
|
|
|
|
{
|
|
|
|
return (address & 0x3FFF) | ((bank & 0x3) * 0x4000);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Transferpak::Init()
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Quick check to ensure we don't have a ROM already
|
2021-04-12 11:35:39 +00:00
|
|
|
if (tpak.gb_cart.rom == nullptr)
|
2016-02-09 07:57:30 +00:00
|
|
|
{
|
|
|
|
memset(&tpak, 0, sizeof(tpak));
|
|
|
|
tpak.access_mode = (!GBCart::init_gb_cart(&tpak.gb_cart, g_Settings->LoadStringVal(Game_Transferpak_ROM).c_str())) ? CART_NOT_INSERTED : CART_ACCESS_MODE_0;
|
|
|
|
tpak.access_mode_changed = 0x44;
|
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Transferpak::Release()
|
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
if (tpak.gb_cart.rom != nullptr)
|
2016-05-16 10:20:00 +00:00
|
|
|
{
|
|
|
|
GBCart::release_gb_cart(&tpak.gb_cart);
|
|
|
|
}
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Transferpak::ReadFrom(uint16_t address, uint8_t * data)
|
|
|
|
{
|
|
|
|
if ((address >= 0x8000) && (address <= 0x8FFF))
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Ensure we actually have a ROM loaded in first
|
2021-04-12 11:35:39 +00:00
|
|
|
if (tpak.gb_cart.rom == nullptr)
|
2016-05-16 10:20:00 +00:00
|
|
|
{
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
2021-05-18 11:51:36 +00:00
|
|
|
// Get whether the Game Boy cart is enabled or disabled
|
2016-01-29 11:17:59 +00:00
|
|
|
uint8_t value = (tpak.enabled) ? 0x84 : 0x00;
|
|
|
|
|
|
|
|
memset(data, value, 0x20);
|
|
|
|
}
|
|
|
|
else if ((address >= 0xB000) && (address <= 0xBFFF))
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Get the Game Boy cart access mode
|
2016-01-29 11:17:59 +00:00
|
|
|
if (tpak.enabled)
|
|
|
|
{
|
|
|
|
memset(data, tpak.access_mode, 0x20);
|
|
|
|
if (tpak.access_mode != CART_NOT_INSERTED)
|
|
|
|
{
|
|
|
|
data[0] |= tpak.access_mode_changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
tpak.access_mode_changed = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (address >= 0xC000)
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Read the Game Boy cart
|
2016-01-29 11:17:59 +00:00
|
|
|
if (tpak.enabled)
|
|
|
|
{
|
|
|
|
GBCart::read_gb_cart(&tpak.gb_cart, gb_cart_address(tpak.bank, address), data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Transferpak::WriteTo(uint16_t address, uint8_t * data)
|
|
|
|
{
|
2016-05-16 10:20:00 +00:00
|
|
|
|
2016-01-29 11:17:59 +00:00
|
|
|
if ((address >= 0x8000) && (address <= 0x8FFF))
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Ensure we actually have a ROM loaded in first
|
2021-04-12 11:35:39 +00:00
|
|
|
if (tpak.gb_cart.rom == nullptr)
|
2016-05-16 10:20:00 +00:00
|
|
|
{
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
2021-05-18 11:51:36 +00:00
|
|
|
// Set whether the Game Boy cart is enabled or disabled
|
2016-01-29 11:17:59 +00:00
|
|
|
switch (*data)
|
|
|
|
{
|
|
|
|
case 0xFE:
|
|
|
|
tpak.enabled = false;
|
|
|
|
break;
|
|
|
|
case 0x84:
|
|
|
|
tpak.enabled = true;
|
|
|
|
break;
|
|
|
|
default:
|
2021-05-18 11:51:36 +00:00
|
|
|
// Do nothing
|
2016-01-29 11:17:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((address >= 0xA000) && (address <= 0xAFFF))
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Set the bank for the Game Boy cart
|
2016-01-29 11:17:59 +00:00
|
|
|
if (tpak.enabled)
|
|
|
|
{
|
|
|
|
tpak.bank = *data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((address >= 0xB000) && (address <= 0xBFFF))
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Get the Game Boy cart access mode
|
2016-01-29 11:17:59 +00:00
|
|
|
if (tpak.enabled)
|
|
|
|
{
|
|
|
|
tpak.access_mode_changed = 0x04;
|
|
|
|
|
|
|
|
tpak.access_mode = ((*data & 1) == 0) ? CART_ACCESS_MODE_0 : CART_ACCESS_MODE_1;
|
|
|
|
|
|
|
|
if ((*data & 0xFE) != 0)
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Unknown Transfer Pak write
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (address >= 0xC000)
|
|
|
|
{
|
2021-05-18 11:51:36 +00:00
|
|
|
// Write to the Game Boy cart
|
2016-01-29 11:17:59 +00:00
|
|
|
if (tpak.enabled)
|
|
|
|
{
|
|
|
|
GBCart::write_gb_cart(&tpak.gb_cart, gb_cart_address(tpak.bank, address), data);
|
2016-02-01 08:58:37 +00:00
|
|
|
GBCart::save_gb_cart(&tpak.gb_cart);
|
2016-01-29 11:17:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|