Commit 5e645528 authored by Libretro-Admin's avatar Libretro-Admin
Browse files

Cut down on cheats code

parent deda34b4
......@@ -142,268 +142,3 @@ const char* S9xGameGenieToRaw(const char* code, uint32* address, uint8* byte)
return (NULL);
}
void S9xStartCheatSearch(SCheatData* d)
{
memmove(d->CWRAM, d->RAM, 0x20000);
memmove(d->CSRAM, d->SRAM, 0x10000);
memmove(d->CIRAM, &d->FillRAM [0x3000], 0x2000);
memset((char*) d->WRAM_BITS, 0xff, 0x20000 >> 3);
memset((char*) d->SRAM_BITS, 0xff, 0x10000 >> 3);
memset((char*) d->IRAM_BITS, 0xff, 0x2000 >> 3);
}
#define BIT_CLEAR(a,v) \
(a)[(v) >> 5] &= ~(1 << ((v) & 31))
#define BIT_SET(a,v) \
(a)[(v) >> 5] |= 1 << ((v) & 31)
#define TEST_BIT(a,v) \
((a)[(v) >> 5] & (1 << ((v) & 31)))
#define _C(c,a,b) \
((c) == S9X_LESS_THAN ? (a) < (b) : \
(c) == S9X_GREATER_THAN ? (a) > (b) : \
(c) == S9X_LESS_THAN_OR_EQUAL ? (a) <= (b) : \
(c) == S9X_GREATER_THAN_OR_EQUAL ? (a) >= (b) : \
(c) == S9X_EQUAL ? (a) == (b) : \
(a) != (b))
#define _D(s,m,o) \
((s) == S9X_8_BITS ? (uint8) (*((m) + (o))) : \
(s) == S9X_16_BITS ? ((uint16) (*((m) + (o)) + (*((m) + (o) + 1) << 8))) : \
(s) == S9X_24_BITS ? ((uint32) (*((m) + (o)) + (*((m) + (o) + 1) << 8) + (*((m) + (o) + 2) << 16))) : \
((uint32) (*((m) + (o)) + (*((m) + (o) + 1) << 8) + (*((m) + (o) + 2) << 16) + (*((m) + (o) + 3) << 24))))
#define _DS(s,m,o) \
((s) == S9X_8_BITS ? ((int8) *((m) + (o))) : \
(s) == S9X_16_BITS ? ((int16) (*((m) + (o)) + (*((m) + (o) + 1) << 8))) : \
(s) == S9X_24_BITS ? (((int32) ((*((m) + (o)) + (*((m) + (o) + 1) << 8) + (*((m) + (o) + 2) << 16)) << 8)) >> 8): \
((int32) (*((m) + (o)) + (*((m) + (o) + 1) << 8) + (*((m) + (o) + 2) << 16) + (*((m) + (o) + 3) << 24))))
void S9xSearchForChange(SCheatData* d, S9xCheatComparisonType cmp,
S9xCheatDataSize size, bool8 is_signed, bool8 update)
{
int l;
switch (size)
{
case S9X_8_BITS:
l = 0;
break;
case S9X_16_BITS:
l = 1;
break;
case S9X_24_BITS:
l = 2;
break;
default:
case S9X_32_BITS:
l = 3;
break;
}
int i;
if (is_signed)
{
for (i = 0; i < 0x20000 - l; i++)
{
if (TEST_BIT(d->WRAM_BITS, i) &&
_C(cmp, _DS(size, d->RAM, i), _DS(size, d->CWRAM, i)))
{
if (update)
d->CWRAM [i] = d->RAM [i];
}
else
BIT_CLEAR(d->WRAM_BITS, i);
}
for (i = 0; i < 0x10000 - l; i++)
{
if (TEST_BIT(d->SRAM_BITS, i) &&
_C(cmp, _DS(size, d->SRAM, i), _DS(size, d->CSRAM, i)))
{
if (update)
d->CSRAM [i] = d->SRAM [i];
}
else
BIT_CLEAR(d->SRAM_BITS, i);
}
for (i = 0; i < 0x2000 - l; i++)
{
if (TEST_BIT(d->IRAM_BITS, i) &&
_C(cmp, _DS(size, d->FillRAM + 0x3000, i), _DS(size, d->CIRAM, i)))
{
if (update)
d->CIRAM [i] = d->FillRAM [i + 0x3000];
}
else
BIT_CLEAR(d->IRAM_BITS, i);
}
}
else
{
for (i = 0; i < 0x20000 - l; i++)
{
if (TEST_BIT(d->WRAM_BITS, i) &&
_C(cmp, _D(size, d->RAM, i), _D(size, d->CWRAM, i)))
{
if (update)
d->CWRAM [i] = d->RAM [i];
}
else
BIT_CLEAR(d->WRAM_BITS, i);
}
for (i = 0; i < 0x10000 - l; i++)
{
if (TEST_BIT(d->SRAM_BITS, i) &&
_C(cmp, _D(size, d->SRAM, i), _D(size, d->CSRAM, i)))
{
if (update)
d->CSRAM [i] = d->SRAM [i];
}
else
BIT_CLEAR(d->SRAM_BITS, i);
}
for (i = 0; i < 0x2000 - l; i++)
{
if (TEST_BIT(d->IRAM_BITS, i) &&
_C(cmp, _D(size, d->FillRAM + 0x3000, i), _D(size, d->CIRAM, i)))
{
if (update)
d->CIRAM [i] = d->FillRAM [i + 0x3000];
}
else
BIT_CLEAR(d->IRAM_BITS, i);
}
}
}
void S9xSearchForValue(SCheatData* d, S9xCheatComparisonType cmp,
S9xCheatDataSize size, uint32 value,
bool8 is_signed, bool8 update)
{
int l;
switch (size)
{
case S9X_8_BITS:
l = 0;
break;
case S9X_16_BITS:
l = 1;
break;
case S9X_24_BITS:
l = 2;
break;
default:
case S9X_32_BITS:
l = 3;
break;
}
int i;
if (is_signed)
{
for (i = 0; i < 0x20000 - l; i++)
{
if (TEST_BIT(d->WRAM_BITS, i) &&
_C(cmp, _DS(size, d->RAM, i), (int32) value))
{
if (update)
d->CWRAM [i] = d->RAM [i];
}
else
BIT_CLEAR(d->WRAM_BITS, i);
}
for (i = 0; i < 0x10000 - l; i++)
{
if (TEST_BIT(d->SRAM_BITS, i) &&
_C(cmp, _DS(size, d->SRAM, i), (int32) value))
{
if (update)
d->CSRAM [i] = d->SRAM [i];
}
else
BIT_CLEAR(d->SRAM_BITS, i);
}
for (i = 0; i < 0x2000 - l; i++)
{
if (TEST_BIT(d->IRAM_BITS, i) &&
_C(cmp, _DS(size, d->FillRAM + 0x3000, i), (int32) value))
{
if (update)
d->CIRAM [i] = d->FillRAM [i + 0x3000];
}
else
BIT_CLEAR(d->IRAM_BITS, i);
}
}
else
{
for (i = 0; i < 0x20000 - l; i++)
{
if (TEST_BIT(d->WRAM_BITS, i) &&
_C(cmp, _D(size, d->RAM, i), value))
{
if (update)
d->CWRAM [i] = d->RAM [i];
}
else
BIT_CLEAR(d->WRAM_BITS, i);
}
for (i = 0; i < 0x10000 - l; i++)
{
if (TEST_BIT(d->SRAM_BITS, i) &&
_C(cmp, _D(size, d->SRAM, i), value))
{
if (update)
d->CSRAM [i] = d->SRAM [i];
}
else
BIT_CLEAR(d->SRAM_BITS, i);
}
for (i = 0; i < 0x2000 - l; i++)
{
if (TEST_BIT(d->IRAM_BITS, i) &&
_C(cmp, _D(size, d->FillRAM + 0x3000, i), value))
{
if (update)
d->CIRAM [i] = d->FillRAM [i + 0x3000];
}
else
BIT_CLEAR(d->IRAM_BITS, i);
}
}
}
void S9xOutputCheatSearchResults(SCheatData* d)
{
int i;
for (i = 0; i < 0x20000; i++)
{
if (TEST_BIT(d->WRAM_BITS, i))
printf("WRAM: %05x: %02x\n", i, d->RAM [i]);
}
for (i = 0; i < 0x10000; i++)
{
if (TEST_BIT(d->SRAM_BITS, i))
printf("SRAM: %04x: %02x\n", i, d->SRAM [i]);
}
for (i = 0; i < 0x2000; i++)
{
if (TEST_BIT(d->IRAM_BITS, i))
printf("IRAM: %05x: %02x\n", i, d->FillRAM [i + 0x3000]);
}
}
......@@ -96,14 +96,5 @@ void S9xAddCheat(bool8 enable, bool8 save_current_value, uint32 address,
void S9xDeleteCheats();
void S9xDeleteCheat(uint32 which1);
bool8 S9xLoadCheatFile(const char* filename);
bool8 S9xSaveCheatFile(const char* filename);
void S9xStartCheatSearch(SCheatData*);
void S9xSearchForChange(SCheatData*, S9xCheatComparisonType cmp,
S9xCheatDataSize size, bool8 is_signed, bool8 update);
void S9xSearchForValue(SCheatData*, S9xCheatComparisonType cmp,
S9xCheatDataSize size, uint32 value,
bool8 is_signed, bool8 update);
void S9xOutputCheatSearchResults(SCheatData*);
#endif
......@@ -164,69 +164,5 @@ bool8 S9xLoadCheatFile(const char* filename)
{
Cheat.num_cheats = 0;
FILE* fs = fopen(filename, "rb");
uint8 data [28];
if (!fs)
return (FALSE);
while (fread((void*) data, 1, 28, fs) == 28)
{
Cheat.c [Cheat.num_cheats].enabled = (data [0] & 4) == 0;
Cheat.c [Cheat.num_cheats].byte = data [1];
Cheat.c [Cheat.num_cheats].address = data [2] | (data [3] << 8) | (data [4] << 16);
Cheat.c [Cheat.num_cheats].saved_byte = data [5];
Cheat.c [Cheat.num_cheats].saved = (data [0] & 8) != 0;
memmove(Cheat.c [Cheat.num_cheats].name, &data [8], 20);
Cheat.c [Cheat.num_cheats++].name [20] = 0;
}
fclose(fs);
return (TRUE);
}
bool8 S9xSaveCheatFile(const char* filename)
{
if (Cheat.num_cheats == 0)
{
(void) remove(filename);
return (TRUE);
}
FILE* fs = fopen(filename, "wb");
uint8 data [28];
if (!fs)
return (FALSE);
uint32 i;
for (i = 0; i < Cheat.num_cheats; i++)
{
memset(data, 0, 28);
if (i == 0)
{
data [6] = 254;
data [7] = 252;
}
if (!Cheat.c [i].enabled)
data [0] |= 4;
if (Cheat.c [i].saved)
data [0] |= 8;
data [1] = Cheat.c [i].byte;
data [2] = (uint8) Cheat.c [i].address;
data [3] = (uint8)(Cheat.c [i].address >> 8);
data [4] = (uint8)(Cheat.c [i].address >> 16);
data [5] = Cheat.c [i].saved_byte;
memmove(&data [8], Cheat.c [i].name, 19);
if (fwrite(data, 28, 1, fs) != 1)
{
fclose(fs);
return (FALSE);
}
}
return (fclose(fs) == 0);
}
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment