2018-11-16 00:31:39 +01:00
|
|
|
/*****************************************************************************\
|
|
|
|
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
|
|
|
|
This file is licensed under the Snes9x License.
|
|
|
|
For further information, consult the LICENSE file in the root directory.
|
|
|
|
\*****************************************************************************/
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
#include "bml.h"
|
|
|
|
#include "cheats.h"
|
2010-09-25 17:46:12 +02:00
|
|
|
#include "snes9x.h"
|
|
|
|
#include "memmap.h"
|
2018-12-29 19:47:15 +01:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
static inline uint8 S9xGetByteFree(uint32 Address)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
int block = (Address & 0xffffff) >> MEMMAP_SHIFT;
|
2018-04-27 22:56:26 +02:00
|
|
|
uint8 *GetAddress = Memory.Map[block];
|
|
|
|
uint8 byte;
|
2018-04-25 00:54:05 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
if (GetAddress >= (uint8 *)CMemory::MAP_LAST)
|
2018-04-25 00:54:05 +02:00
|
|
|
{
|
|
|
|
byte = *(GetAddress + (Address & 0xffff));
|
|
|
|
return (byte);
|
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
switch ((pint)GetAddress)
|
2018-04-25 00:54:05 +02:00
|
|
|
{
|
|
|
|
case CMemory::MAP_CPU:
|
|
|
|
byte = S9xGetCPU(Address & 0xffff);
|
|
|
|
return (byte);
|
|
|
|
|
|
|
|
case CMemory::MAP_PPU:
|
|
|
|
if (CPU.InDMAorHDMA && (Address & 0xff00) == 0x2100)
|
|
|
|
return (OpenBus);
|
|
|
|
|
|
|
|
byte = S9xGetPPU(Address & 0xffff);
|
|
|
|
return (byte);
|
|
|
|
|
|
|
|
case CMemory::MAP_LOROM_SRAM:
|
|
|
|
case CMemory::MAP_SA1RAM:
|
|
|
|
// Address & 0x7fff : offset into bank
|
|
|
|
// Address & 0xff0000 : bank
|
|
|
|
// bank >> 1 | offset : SRAM address, unbound
|
|
|
|
// unbound & SRAMMask : SRAM offset
|
|
|
|
byte = *(Memory.SRAM + ((((Address & 0xff0000) >> 1) | (Address & 0x7fff)) & Memory.SRAMMask));
|
|
|
|
return (byte);
|
|
|
|
|
|
|
|
case CMemory::MAP_LOROM_SRAM_B:
|
|
|
|
byte = *(Multi.sramB + ((((Address & 0xff0000) >> 1) | (Address & 0x7fff)) & Multi.sramMaskB));
|
|
|
|
return (byte);
|
|
|
|
|
|
|
|
case CMemory::MAP_HIROM_SRAM:
|
|
|
|
case CMemory::MAP_RONLY_SRAM:
|
2021-08-25 19:12:11 +02:00
|
|
|
byte = *(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0x1f0000) >> 3)) & Memory.SRAMMask));
|
2018-04-25 00:54:05 +02:00
|
|
|
return (byte);
|
|
|
|
|
|
|
|
case CMemory::MAP_BWRAM:
|
|
|
|
byte = *(Memory.BWRAM + ((Address & 0x7fff) - 0x6000));
|
|
|
|
return (byte);
|
|
|
|
|
|
|
|
case CMemory::MAP_DSP:
|
|
|
|
byte = S9xGetDSP(Address & 0xffff);
|
|
|
|
return (byte);
|
|
|
|
|
|
|
|
case CMemory::MAP_SPC7110_ROM:
|
|
|
|
byte = S9xGetSPC7110Byte(Address);
|
|
|
|
return (byte);
|
|
|
|
|
|
|
|
case CMemory::MAP_SPC7110_DRAM:
|
|
|
|
byte = S9xGetSPC7110(0x4800);
|
|
|
|
return (byte);
|
|
|
|
|
|
|
|
case CMemory::MAP_C4:
|
|
|
|
byte = S9xGetC4(Address & 0xffff);
|
|
|
|
return (byte);
|
|
|
|
|
|
|
|
case CMemory::MAP_OBC_RAM:
|
|
|
|
byte = S9xGetOBC1(Address & 0xffff);
|
|
|
|
return (byte);
|
|
|
|
|
|
|
|
case CMemory::MAP_SETA_DSP:
|
|
|
|
byte = S9xGetSetaDSP(Address);
|
|
|
|
return (byte);
|
|
|
|
|
|
|
|
case CMemory::MAP_SETA_RISC:
|
|
|
|
byte = S9xGetST018(Address);
|
|
|
|
return (byte);
|
|
|
|
|
|
|
|
case CMemory::MAP_BSX:
|
|
|
|
byte = S9xGetBSX(Address);
|
|
|
|
return (byte);
|
|
|
|
|
|
|
|
case CMemory::MAP_NONE:
|
|
|
|
default:
|
|
|
|
byte = OpenBus;
|
|
|
|
return (byte);
|
|
|
|
}
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
static inline void S9xSetByteFree(uint8 Byte, uint32 Address)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2018-04-27 22:56:26 +02:00
|
|
|
int block = (Address & 0xffffff) >> MEMMAP_SHIFT;
|
|
|
|
uint8 *SetAddress = Memory.Map[block];
|
2018-04-25 00:54:05 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
if (SetAddress >= (uint8 *)CMemory::MAP_LAST)
|
2018-04-25 00:54:05 +02:00
|
|
|
{
|
|
|
|
*(SetAddress + (Address & 0xffff)) = Byte;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
switch ((pint)SetAddress)
|
2018-04-25 00:54:05 +02:00
|
|
|
{
|
|
|
|
case CMemory::MAP_CPU:
|
|
|
|
S9xSetCPU(Byte, Address & 0xffff);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CMemory::MAP_PPU:
|
|
|
|
if (CPU.InDMAorHDMA && (Address & 0xff00) == 0x2100)
|
|
|
|
return;
|
|
|
|
|
|
|
|
S9xSetPPU(Byte, Address & 0xffff);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CMemory::MAP_LOROM_SRAM:
|
|
|
|
if (Memory.SRAMMask)
|
|
|
|
{
|
|
|
|
*(Memory.SRAM + ((((Address & 0xff0000) >> 1) | (Address & 0x7fff)) & Memory.SRAMMask)) = Byte;
|
|
|
|
CPU.SRAMModified = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CMemory::MAP_LOROM_SRAM_B:
|
|
|
|
if (Multi.sramMaskB)
|
|
|
|
{
|
|
|
|
*(Multi.sramB + ((((Address & 0xff0000) >> 1) | (Address & 0x7fff)) & Multi.sramMaskB)) = Byte;
|
|
|
|
CPU.SRAMModified = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CMemory::MAP_HIROM_SRAM:
|
|
|
|
if (Memory.SRAMMask)
|
|
|
|
{
|
2021-08-25 19:12:11 +02:00
|
|
|
*(Memory.SRAM + (((Address & 0x7fff) - 0x6000 + ((Address & 0x1f0000) >> 3)) & Memory.SRAMMask)) = Byte;
|
2018-04-25 00:54:05 +02:00
|
|
|
CPU.SRAMModified = TRUE;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CMemory::MAP_BWRAM:
|
|
|
|
*(Memory.BWRAM + ((Address & 0x7fff) - 0x6000)) = Byte;
|
|
|
|
CPU.SRAMModified = TRUE;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CMemory::MAP_SA1RAM:
|
|
|
|
*(Memory.SRAM + (Address & 0xffff)) = Byte;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CMemory::MAP_DSP:
|
|
|
|
S9xSetDSP(Byte, Address & 0xffff);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CMemory::MAP_C4:
|
|
|
|
S9xSetC4(Byte, Address & 0xffff);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CMemory::MAP_OBC_RAM:
|
|
|
|
S9xSetOBC1(Byte, Address & 0xffff);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CMemory::MAP_SETA_DSP:
|
|
|
|
S9xSetSetaDSP(Byte, Address);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CMemory::MAP_SETA_RISC:
|
|
|
|
S9xSetST018(Byte, Address);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CMemory::MAP_BSX:
|
|
|
|
S9xSetBSX(Byte, Address);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CMemory::MAP_NONE:
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
void S9xInitWatchedAddress(void)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2018-04-26 02:29:26 +02:00
|
|
|
for (unsigned int i = 0; i < sizeof(watches) / sizeof(watches[0]); i++)
|
|
|
|
watches[i].on = false;
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
void S9xInitCheatData(void)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2018-04-26 02:29:26 +02:00
|
|
|
Cheat.RAM = Memory.RAM;
|
|
|
|
Cheat.SRAM = Memory.SRAM;
|
|
|
|
Cheat.FillRAM = Memory.FillRAM;
|
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
static inline std::string trim(const std::string &&string)
|
|
|
|
{
|
|
|
|
auto start = string.find_first_not_of(" \t\n\r");
|
|
|
|
auto end = string.find_last_not_of(" \t\n\r");
|
|
|
|
if (start != std::string::npos && end != std::string::npos)
|
|
|
|
return string.substr(start, end - start + 1);
|
|
|
|
return "";
|
|
|
|
}
|
2018-04-26 02:29:26 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
void S9xUpdateCheatInMemory(SCheat &c)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
|
|
|
uint8 byte;
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
if (!c.enabled)
|
2018-04-26 02:29:26 +02:00
|
|
|
return;
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
byte = S9xGetByteFree(c.address);
|
2018-04-26 02:29:26 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
if (byte != c.byte)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
|
|
|
/* The game wrote a different byte to the address, update saved_byte */
|
2022-05-11 03:47:12 +02:00
|
|
|
c.saved_byte = byte;
|
2018-04-26 02:29:26 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
if (c.conditional)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
if (c.saved_byte != c.cond_byte && c.cond_true)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
|
|
|
/* Condition is now false, let the byte stand */
|
2022-05-11 03:47:12 +02:00
|
|
|
c.cond_true = false;
|
2018-04-26 02:29:26 +02:00
|
|
|
}
|
2022-05-11 03:47:12 +02:00
|
|
|
else if (c.saved_byte == c.cond_byte && !c.cond_true)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
c.cond_true = true;
|
|
|
|
S9xSetByteFree(c.byte, c.address);
|
2018-04-26 02:29:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2022-05-11 03:47:12 +02:00
|
|
|
S9xSetByteFree(c.byte, c.address);
|
2018-04-26 02:29:26 +02:00
|
|
|
}
|
2022-05-11 03:47:12 +02:00
|
|
|
else if (c.conditional)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
if (byte == c.cond_byte)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
c.cond_true = true;
|
|
|
|
c.saved_byte = byte;
|
|
|
|
S9xSetByteFree(c.byte, c.address);
|
2018-04-26 02:29:26 +02:00
|
|
|
}
|
|
|
|
}
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
void S9xDisableCheat(SCheat &c)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
if (!c.enabled)
|
2018-04-26 02:29:26 +02:00
|
|
|
return;
|
|
|
|
|
2018-04-26 18:15:20 +02:00
|
|
|
if (!Cheat.enabled)
|
2018-04-26 20:03:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
c.enabled = false;
|
2018-04-26 18:15:20 +02:00
|
|
|
return;
|
2018-04-26 20:03:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure we restore the up-to-date written byte */
|
2022-05-11 03:47:12 +02:00
|
|
|
S9xUpdateCheatInMemory(c);
|
|
|
|
c.enabled = false;
|
2018-04-26 18:15:20 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
if (c.conditional && !c.cond_true)
|
2018-04-26 02:29:26 +02:00
|
|
|
return;
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
S9xSetByteFree (c.saved_byte, c.address);
|
|
|
|
c.cond_true = false;
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
void S9xDeleteCheatGroup(uint32 g)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2018-04-26 02:29:26 +02:00
|
|
|
unsigned int i;
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
if (g >= Cheat.group.size())
|
2018-04-26 02:29:26 +02:00
|
|
|
return;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
for (i = 0; i < Cheat.group[g].cheat.size(); i++)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
S9xDisableCheat(Cheat.group[g].cheat[i]);
|
2018-04-26 02:29:26 +02:00
|
|
|
}
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
Cheat.group.erase(Cheat.group.begin() + g);
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
void S9xDeleteCheats(void)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
for (size_t i = 0; i < Cheat.group.size(); i++)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
S9xDisableCheatGroup(i);
|
2018-04-26 02:29:26 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
Cheat.group.clear();
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
void S9xEnableCheat(SCheat &c)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2018-04-26 02:29:26 +02:00
|
|
|
uint8 byte;
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
if (c.enabled)
|
2018-04-26 02:29:26 +02:00
|
|
|
return;
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
c.enabled = true;
|
2018-04-26 18:15:20 +02:00
|
|
|
|
|
|
|
if (!Cheat.enabled)
|
|
|
|
return;
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
byte = S9xGetByteFree(c.address);
|
2018-04-26 02:29:26 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
if (c.conditional)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
if (byte != c.cond_byte)
|
2018-04-26 02:29:26 +02:00
|
|
|
return;
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
c.cond_true = true;
|
2018-04-26 02:29:26 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
c.saved_byte = byte;
|
|
|
|
S9xSetByteFree(c.byte, c.address);
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
void S9xEnableCheatGroup(uint32 num)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
for (auto &c : Cheat.group[num].cheat)
|
|
|
|
S9xEnableCheat(c);
|
|
|
|
|
|
|
|
Cheat.group[num].enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void S9xDisableCheatGroup(uint32 num)
|
|
|
|
{
|
|
|
|
for (auto &c : Cheat.group[num].cheat)
|
|
|
|
S9xDisableCheat(c);
|
2018-04-26 02:29:26 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
Cheat.group[num].enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_all_hex(const std::string &code)
|
|
|
|
{
|
|
|
|
for (const auto &c : code)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
if ((c < '0' || c > '9') &&
|
|
|
|
(c < 'a' || c > 'f') &&
|
|
|
|
(c < 'A' || c > 'F'))
|
|
|
|
return false;
|
2018-04-26 02:29:26 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
return true;
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
bool S9xProActionReplayToRaw(const std::string &code, uint32 &address, uint8 &byte)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
if (code.length() != 8 || !is_all_hex(code))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint32 data = std::strtoul(code.c_str(), nullptr, 16);
|
|
|
|
|
|
|
|
address = data >> 8;
|
|
|
|
byte = (uint8)data;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool S9xGameGenieToRaw(const std::string &code, uint32 &address, uint8 &byte)
|
|
|
|
{
|
|
|
|
if (code.length() != 9)
|
|
|
|
return false;
|
|
|
|
if (code[4] != '-')
|
|
|
|
return false;
|
|
|
|
if (!is_all_hex(code.substr(0, 4)))
|
|
|
|
return false;
|
|
|
|
if (!is_all_hex(code.substr(5, 4)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto new_code = code.substr(0, 4) + code.substr(5, 4);
|
|
|
|
|
|
|
|
static const char *real_hex = "0123456789ABCDEF";
|
|
|
|
static const char *genie_hex = "DF4709156BC8A23E";
|
2018-04-26 02:29:26 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
for (auto &c : new_code)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
c = toupper(c);
|
|
|
|
|
|
|
|
for (int i = 0; i < 16; i++)
|
|
|
|
{
|
|
|
|
if (genie_hex[i] == c)
|
|
|
|
{
|
|
|
|
c = real_hex[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-04-26 02:29:26 +02:00
|
|
|
}
|
2018-04-26 18:15:20 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
uint32 data = strtoul(new_code.c_str(), nullptr, 16);
|
|
|
|
byte = (uint8)(data >> 24);
|
|
|
|
address = data & 0xffffff;
|
|
|
|
address = ((address & 0x003c00) << 10) +
|
|
|
|
((address & 0x00003c) << 14) +
|
|
|
|
((address & 0xf00000) >> 8) +
|
|
|
|
((address & 0x000003) << 10) +
|
|
|
|
((address & 0x00c000) >> 6) +
|
|
|
|
((address & 0x0f0000) >> 12) +
|
|
|
|
((address & 0x0003c0) >> 6);
|
|
|
|
|
|
|
|
return true;
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
SCheat S9xTextToCheat(const std::string &text)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2018-04-26 02:29:26 +02:00
|
|
|
SCheat c;
|
|
|
|
unsigned int byte = 0;
|
|
|
|
unsigned int cond_byte = 0;
|
|
|
|
|
|
|
|
c.enabled = false;
|
|
|
|
c.conditional = false;
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
if (S9xGameGenieToRaw(text, c.address, c.byte))
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
|
|
|
byte = c.byte;
|
|
|
|
}
|
2022-05-11 03:47:12 +02:00
|
|
|
else if (S9xProActionReplayToRaw(text, c.address, c.byte))
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
|
|
|
byte = c.byte;
|
|
|
|
}
|
2022-05-11 03:47:12 +02:00
|
|
|
else if (sscanf(text.c_str(), "%x = %x ? %x", &c.address, &cond_byte, &byte) == 3)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
|
|
|
c.conditional = true;
|
|
|
|
}
|
2022-05-11 03:47:12 +02:00
|
|
|
else if (sscanf(text.c_str(), "%x = %x", &c.address, &byte) == 2)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
|
|
|
}
|
2022-05-11 03:47:12 +02:00
|
|
|
else if (sscanf(text.c_str(), "%x / %x / %x", &c.address, &cond_byte, &byte) == 3)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
|
|
|
c.conditional = true;
|
|
|
|
}
|
2023-08-23 22:24:02 +02:00
|
|
|
else if (sscanf(text.c_str(), "%x : %x", &c.address, &byte) == 2)
|
|
|
|
{
|
|
|
|
}
|
2022-05-11 03:47:12 +02:00
|
|
|
else if (sscanf(text.c_str(), "%x / %x", &c.address, &byte) == 2)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c.address = 0;
|
|
|
|
byte = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
c.byte = byte;
|
|
|
|
c.cond_byte = cond_byte;
|
|
|
|
|
|
|
|
return c;
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
std::vector<std::string> split_string(const std::string &str, unsigned char delim)
|
|
|
|
{
|
|
|
|
std::vector<std::string> tokens;
|
|
|
|
size_t pos = 0;
|
|
|
|
size_t index;
|
|
|
|
|
|
|
|
while (pos < str.length())
|
|
|
|
{
|
|
|
|
index = str.find(delim, pos);
|
|
|
|
if (index == std::string::npos)
|
|
|
|
{
|
|
|
|
if (pos < str.length())
|
|
|
|
{
|
|
|
|
tokens.push_back(trim(str.substr(pos)));
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (index > pos)
|
|
|
|
{
|
|
|
|
tokens.push_back(trim(str.substr(pos, index - pos)));
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = index + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
SCheatGroup S9xCreateCheatGroup(const std::string &name, const std::string &cheat)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2018-04-26 02:29:26 +02:00
|
|
|
SCheatGroup g;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
g.name = name;
|
2018-04-26 02:29:26 +02:00
|
|
|
g.enabled = false;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
auto cheats = split_string(cheat, '+');
|
|
|
|
for (const auto &c : cheats)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
SCheat new_cheat = S9xTextToCheat(c);
|
|
|
|
if (new_cheat.address)
|
|
|
|
g.cheat.push_back(new_cheat);
|
2018-04-26 02:29:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return g;
|
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
int S9xAddCheatGroup(const std::string &name, const std::string &cheat)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
SCheatGroup g = S9xCreateCheatGroup(name, cheat);
|
|
|
|
if (g.cheat.size() == 0)
|
2018-04-26 02:29:26 +02:00
|
|
|
return -1;
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
Cheat.group.push_back(g);
|
2018-04-26 02:29:26 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
return Cheat.group.size() - 1;
|
2018-04-26 02:29:26 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
int S9xModifyCheatGroup(uint32 num, const std::string &name, const std::string &cheat)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
if (num >= Cheat.group.size())
|
2018-04-27 22:42:19 +02:00
|
|
|
return -1;
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
S9xDisableCheatGroup(num);
|
2018-04-26 02:29:26 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
Cheat.group[num] = S9xCreateCheatGroup(name, cheat);
|
2018-04-26 02:29:26 +02:00
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
std::string S9xCheatToText(const SCheat &c)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2023-02-03 23:31:11 +01:00
|
|
|
char output[256]{};
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
if (c.conditional)
|
2023-02-03 23:31:11 +01:00
|
|
|
sprintf(output, "%06x=%02x?%02x", c.address, c.cond_byte, c.byte);
|
|
|
|
else
|
|
|
|
sprintf(output, "%06x=%02x", c.address, c.byte);
|
2018-04-26 02:29:26 +02:00
|
|
|
|
2023-02-03 23:31:11 +01:00
|
|
|
return std::string(output);
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
std::string S9xCheatGroupToText(SCheatGroup &g)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2018-04-27 22:56:26 +02:00
|
|
|
std::string text = "";
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
for (size_t i = 0; i < g.cheat.size(); i++)
|
2018-04-27 22:56:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
text += S9xCheatToText(g.cheat[i]);
|
|
|
|
if (i != g.cheat.size() - 1)
|
|
|
|
text += "+";
|
2018-04-27 22:56:26 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
return text;
|
2018-04-27 22:42:19 +02:00
|
|
|
}
|
2018-04-26 02:29:26 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
std::string S9xCheatValidate(const std::string &code_string)
|
2018-04-27 22:42:19 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
SCheatGroup g = S9xCreateCheatGroup("temp", code_string);
|
2018-04-27 22:42:19 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
if (g.cheat.size() > 0)
|
2018-04-27 22:56:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
return S9xCheatGroupToText(g);
|
2018-04-27 22:56:26 +02:00
|
|
|
}
|
2018-04-27 22:42:19 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
return "";
|
2018-04-27 22:42:19 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
std::string S9xCheatGroupToText(uint32 num)
|
2018-04-27 22:42:19 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
if (num >= Cheat.group.size())
|
|
|
|
return "";
|
2018-04-26 02:29:26 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
return S9xCheatGroupToText(Cheat.group[num]);
|
2018-04-26 02:29:26 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
void S9xUpdateCheatsInMemory(void)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2018-04-26 18:15:20 +02:00
|
|
|
if (!Cheat.enabled)
|
|
|
|
return;
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
for (auto &group : Cheat.group)
|
|
|
|
for (auto &cheat : group.cheat)
|
|
|
|
S9xUpdateCheatInMemory(cheat);
|
2018-04-26 02:29:26 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
static bool S9xCheatIsDuplicate(const std::string &name, const std::string &code)
|
2018-04-28 03:35:20 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
for (size_t i = 0; i < Cheat.group.size(); i++)
|
2018-04-28 03:35:20 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
if (Cheat.group[i].name == name)
|
2018-04-28 03:35:20 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
auto code_string = S9xCheatGroupToText(i);
|
|
|
|
auto validated_string = S9xCheatValidate(code);
|
2018-04-28 03:35:20 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
if (validated_string == code_string)
|
|
|
|
return true;
|
2018-04-28 03:35:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
return false;
|
2018-04-28 03:35:20 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
static void S9xLoadCheatsFromBMLNode(bml_node &n)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
for (auto &c : n.child)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
if (strcasecmp(c.name.c_str(), "cheat"))
|
|
|
|
continue;
|
2018-04-26 02:29:26 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
auto subnode = c.find_subnode("code");
|
|
|
|
if (!subnode)
|
|
|
|
continue;
|
|
|
|
std::string code = subnode->data;
|
2018-04-26 02:29:26 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
std::string name;
|
|
|
|
subnode = c.find_subnode("name");
|
|
|
|
if (subnode)
|
|
|
|
name = subnode->data;
|
2018-04-26 02:29:26 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
bool enable = false;
|
|
|
|
if (c.find_subnode("enable"))
|
|
|
|
enable = true;
|
2018-04-26 02:29:26 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
if (S9xCheatIsDuplicate(name, code))
|
|
|
|
continue;
|
2018-04-26 02:29:26 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
auto index = S9xAddCheatGroup(name, code);
|
|
|
|
if (enable)
|
|
|
|
S9xEnableCheatGroup(index);
|
2018-04-26 02:29:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
static bool8 S9xLoadCheatFileClassic(const std::string &filename)
|
2018-04-26 19:01:51 +02:00
|
|
|
{
|
|
|
|
FILE *fs;
|
|
|
|
uint8 data[28];
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
fs = fopen(filename.c_str(), "rb");
|
2018-04-26 19:01:51 +02:00
|
|
|
if (!fs)
|
|
|
|
return (FALSE);
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
while (fread(data, 1, 28, fs) == 28)
|
2018-04-26 19:01:51 +02:00
|
|
|
{
|
|
|
|
SCheat c;
|
|
|
|
c.enabled = (data[0] & 4) == 0;
|
|
|
|
c.byte = data[1];
|
2022-05-11 03:47:12 +02:00
|
|
|
c.address = data[2] | (data[3] << 8) | (data[4] << 16);
|
2018-04-26 19:01:51 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
std::string name((const char *)&data[8], 20);
|
2023-02-03 23:31:11 +01:00
|
|
|
char code[32]{};
|
|
|
|
sprintf(code, "%x=%x", c.address, c.byte);
|
|
|
|
std::string cheat(code);
|
2022-05-11 03:47:12 +02:00
|
|
|
S9xAddCheatGroup(name, cheat);
|
2018-04-26 19:01:51 +02:00
|
|
|
|
|
|
|
if (c.enabled)
|
2022-05-11 03:47:12 +02:00
|
|
|
S9xEnableCheatGroup(Cheat.group.size() - 1);
|
2018-04-26 19:01:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fs);
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
return TRUE;
|
2018-04-26 19:01:51 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
bool8 S9xLoadCheatFile(const std::string &filename)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2019-05-15 00:42:41 +02:00
|
|
|
bml_node bml;
|
|
|
|
if (!bml.parse_file(filename))
|
2018-04-26 19:01:51 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
return S9xLoadCheatFileClassic(filename);
|
2018-04-26 19:01:51 +02:00
|
|
|
}
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-05-15 00:42:41 +02:00
|
|
|
bml_node *n = bml.find_subnode("cheat");
|
2018-04-26 02:29:26 +02:00
|
|
|
if (n)
|
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
S9xLoadCheatsFromBMLNode(bml);
|
2018-04-26 02:29:26 +02:00
|
|
|
}
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2018-04-26 19:01:51 +02:00
|
|
|
if (!n)
|
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
return S9xLoadCheatFileClassic(filename);
|
2018-04-26 19:01:51 +02:00
|
|
|
}
|
|
|
|
|
2018-04-26 02:29:26 +02:00
|
|
|
return (TRUE);
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
bool8 S9xSaveCheatFile(const std::string &filename)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2018-04-26 02:29:26 +02:00
|
|
|
unsigned int i;
|
|
|
|
FILE *file = NULL;
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
if (Cheat.group.size() == 0)
|
2018-04-26 18:15:20 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
remove(filename.c_str());
|
2018-04-26 02:29:26 +02:00
|
|
|
return TRUE;
|
2018-04-26 18:15:20 +02:00
|
|
|
}
|
2018-04-26 02:29:26 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
file = fopen(filename.c_str(), "w");
|
2018-04-26 02:29:26 +02:00
|
|
|
|
|
|
|
if (!file)
|
|
|
|
return FALSE;
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
for (i = 0; i < Cheat.group.size(); i++)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2023-02-03 23:31:11 +01:00
|
|
|
fprintf(file,
|
|
|
|
"cheat\n"
|
|
|
|
" name: %s\n"
|
|
|
|
" code: %s\n"
|
|
|
|
"%s\n",
|
|
|
|
Cheat.group[i].name.c_str(),
|
|
|
|
S9xCheatGroupToText(i).c_str(),
|
|
|
|
Cheat.group[i].enabled ? " enable\n" : "");
|
2018-04-26 02:29:26 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
fclose(file);
|
2018-04-26 02:29:26 +02:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
void S9xCheatsDisable(void)
|
2018-04-26 18:15:20 +02:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (!Cheat.enabled)
|
|
|
|
return;
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
for (i = 0; i < Cheat.group.size(); i++)
|
2018-04-26 18:15:20 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
if (Cheat.group[i].enabled)
|
2018-04-26 18:15:20 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
S9xDisableCheatGroup(i);
|
|
|
|
Cheat.group[i].enabled = TRUE;
|
2018-04-26 18:15:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Cheat.enabled = FALSE;
|
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
void S9xCheatsEnable(void)
|
2018-04-26 18:15:20 +02:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (Cheat.enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Cheat.enabled = TRUE;
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
for (i = 0; i < Cheat.group.size(); i++)
|
2018-04-26 18:15:20 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
if (Cheat.group[i].enabled)
|
2018-04-26 18:15:20 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
Cheat.group[i].enabled = FALSE;
|
|
|
|
S9xEnableCheatGroup(i);
|
2018-04-26 18:15:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
int S9xImportCheatsFromDatabase(const std::string &filename)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
|
|
|
char sha256_txt[65];
|
|
|
|
char hextable[] = "0123456789abcdef";
|
|
|
|
unsigned int i;
|
|
|
|
|
2019-05-15 00:42:41 +02:00
|
|
|
bml_node bml;
|
|
|
|
if (!bml.parse_file(filename))
|
|
|
|
return -1; // No file
|
2018-04-26 02:29:26 +02:00
|
|
|
|
|
|
|
for (i = 0; i < 32; i++)
|
|
|
|
{
|
|
|
|
sha256_txt[i * 2] = hextable[Memory.ROMSHA256[i] >> 4];
|
|
|
|
sha256_txt[i * 2 + 1] = hextable[Memory.ROMSHA256[i] & 0xf];
|
|
|
|
}
|
|
|
|
sha256_txt[64] = '\0';
|
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
for (auto &c : bml.child)
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
if (!strcasecmp(c.name.c_str(), "cartridge"))
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
auto n = c.find_subnode("sha256");
|
2018-04-26 02:29:26 +02:00
|
|
|
|
2022-05-11 03:47:12 +02:00
|
|
|
if (n && !strcasecmp(n->data.c_str(), sha256_txt))
|
2018-04-26 02:29:26 +02:00
|
|
|
{
|
2022-05-11 03:47:12 +02:00
|
|
|
S9xLoadCheatsFromBMLNode(c);
|
|
|
|
return 0;
|
2018-04-26 02:29:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-28 03:35:20 +02:00
|
|
|
return -2; /* No codes */
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|