#include "psxcommon.h"
#include "r3000a.h"
#include "psxmem.h"
+#include "misc.h"
+#include "../frontend/plugin_lib.h" // in_keystate for D4
#include "cheat.h"
Cheats[NumCheats].Descr = strdup(buf + 1);
Cheats[NumCheats].Enabled = 0;
}
+ Cheats[NumCheats].WasEnabled = 0;
Cheats[NumCheats].First = NumCodes;
// apply all enabled cheats
void ApplyCheats() {
int i, j, k, endindex;
+ int was_enabled;
for (i = 0; i < NumCheats; i++) {
+ was_enabled = Cheats[i].WasEnabled;
if (!Cheats[i].Enabled) {
- continue;
+ if (!Cheats[i].WasEnabled)
+ continue;
}
+ Cheats[i].WasEnabled = Cheats[i].Enabled;
// process all cheat codes
endindex = Cheats[i].First + Cheats[i].n;
u16 val = CheatCodes[j].Val;
u32 taddr;
+ if (!was_enabled) {
+ switch (type) {
+ case CHEAT_CONST16:
+ CheatCodes[j].OldVal = psxMu16(addr);
+ break;
+ case CHEAT_CONST8:
+ CheatCodes[j].OldVal = psxMu8(addr);
+ break;
+ }
+ }
+ else if (!Cheats[i].Enabled) {
+ val = CheatCodes[j].OldVal;
+ if (type != CHEAT_CONST16 && type != CHEAT_CONST8)
+ continue;
+ }
+
switch (type) {
case CHEAT_CONST8:
psxMu8ref(addr) = (u8)val;
psxMu16ref(addr) = SWAPu16(val);
break;
+ case CHEAT_SCRATCHPAD16: // 1F
+ psxHs16ref(addr) = SWAPu16(val);
+ break;
+
case CHEAT_INC16:
psxMu16ref(addr) = SWAPu16(psxMu16(addr) + val);
break;
if (PSXMu16(addr) <= val)
j++; // skip the next code
break;
+
+ case CHEAT_BUTTONS1_16: { // D4
+ u16 keys = in_keystate[0];
+ keys = (keys << 8) | (keys >> 8);
+ if (keys != val)
+ j++; // skip the next code
+ break;
+ }
+
+ default:
+ SysPrintf("unhandled cheat %d,%d code %08X\n",
+ i, j, CheatCodes[j].Addr);
+ Cheats[i].WasEnabled = Cheats[i].Enabled = 0;
+ break;
}
}
}
}
}
- Cheats[NumCheats].Descr = strdup(descr[0] ? descr : _("(Untitled)"));
Cheats[NumCheats].Enabled = 0;
+ Cheats[NumCheats].WasEnabled = 0;
Cheats[NumCheats].First = NumCodes;
Cheats[NumCheats].n = 0;
p2 = code;
while (c) {
- unsigned int t1, t2;
+ unsigned int t1, t2, r;
while (*p2 != '\n' && *p2 != '\0')
p2++;
t1 = 0;
t2 = 0;
- sscanf(p1, "%x %x", &t1, &t2);
+ r = sscanf(p1, "%x %x", &t1, &t2);
- if (t1 > 0x10000000) {
+ if (r != 2)
+ SysPrintf("cheat %d: couldn't parse '%s'\n", NumCodes, p1);
+ else if (t1 >= 0x10000000) {
if (NumCodes >= NumCodesAllocated) {
NumCodesAllocated += ALLOC_INCREMENT;
return -1;
}
+ Cheats[NumCheats].Descr = strdup(descr[0] ? descr : _("(Untitled)"));
NumCheats++;
return 0;
}
assert(index >= 0 && index < NumCheats);
free(Cheats[index].Descr);
+ Cheats[index].Descr = NULL;
while (index < NumCheats - 1) {
Cheats[index] = Cheats[index + 1];