1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
#include "../copyright"
#ifdef WANT_CHEATS
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "snes9x.h"
#include "cheats.h"
#include "memmap.h"
extern SCheatData Cheat;
void S9xInitCheatData()
{
Cheat.RAM = Memory.RAM;
Cheat.SRAM = Memory.SRAM;
Cheat.FillRAM = Memory.FillRAM;
}
void S9xAddCheat(bool enable, bool save_current_value,
uint32_t address, uint8_t byte)
{
if (Cheat.num_cheats < sizeof(Cheat.c) / sizeof(Cheat.c [0]))
{
Cheat.c [Cheat.num_cheats].address = address;
Cheat.c [Cheat.num_cheats].byte = byte;
Cheat.c [Cheat.num_cheats].enabled = enable;
if (save_current_value)
{
Cheat.c [Cheat.num_cheats].saved_byte = S9xGetByte(address);
Cheat.c [Cheat.num_cheats].saved = true;
}
Cheat.num_cheats++;
if (enable)
S9xApplyCheat(Cheat.num_cheats - 1);
}
}
void S9xDeleteCheat(uint32_t which1)
{
if (which1 < Cheat.num_cheats)
{
if (Cheat.c [which1].enabled)
S9xRemoveCheat(which1);
// memmove required: Overlapping addresses [Neb]
memmove(&Cheat.c [which1], &Cheat.c [which1 + 1],
sizeof(Cheat.c [0]) * (Cheat.num_cheats - which1 - 1));
Cheat.num_cheats--; //MK: This used to set it to 0??
}
}
void S9xDeleteCheats()
{
S9xRemoveCheats();
Cheat.num_cheats = 0;
}
void S9xEnableCheat(uint32_t which1)
{
if (which1 < Cheat.num_cheats && !Cheat.c [which1].enabled)
{
Cheat.c [which1].enabled = true;
S9xApplyCheat(which1);
}
}
void S9xDisableCheat(uint32_t which1)
{
if (which1 < Cheat.num_cheats && Cheat.c [which1].enabled)
{
S9xRemoveCheat(which1);
Cheat.c [which1].enabled = false;
}
}
void S9xRemoveCheat(uint32_t which1)
{
if (Cheat.c [which1].saved)
{
uint32_t address = Cheat.c [which1].address;
int block = (address >> MEMMAP_SHIFT) & MEMMAP_MASK;
uint8_t* ptr = Memory.Map [block];
if (ptr >= (uint8_t*) MAP_LAST)
*(ptr + (address & 0xffff)) = Cheat.c [which1].saved_byte;
else
S9xSetByte(Cheat.c [which1].saved_byte, address);
// Unsave the address for the next call to S9xRemoveCheat.
Cheat.c [which1].saved = false;
}
}
void S9xApplyCheat(uint32_t which1)
{
uint32_t address = Cheat.c [which1].address;
if (!Cheat.c [which1].saved)
Cheat.c [which1].saved_byte = S9xGetByte(address);
int block = (address >> MEMMAP_SHIFT) & MEMMAP_MASK;
uint8_t* ptr = Memory.Map [block];
if (ptr >= (uint8_t*) MAP_LAST)
*(ptr + (address & 0xffff)) = Cheat.c [which1].byte;
else
S9xSetByte(Cheat.c [which1].byte, address);
Cheat.c [which1].saved = true;
}
void S9xApplyCheats()
{
uint32_t i;
if (Settings.ApplyCheats)
{
for (i = 0; i < Cheat.num_cheats; i++)
if (Cheat.c [i].enabled)
S9xApplyCheat(i);
}
}
void S9xRemoveCheats()
{
uint32_t i;
for (i = 0; i < Cheat.num_cheats; i++)
if (Cheat.c [i].enabled)
S9xRemoveCheat(i);
}
bool S9xLoadCheatFile(const char* filename)
{
Cheat.num_cheats = 0;
FILE* fs = fopen(filename, "rb");
uint8_t data [8 + MAX_SFCCHEAT_NAME];
if (!fs)
return (false);
while (fread((void*) data, 1, 8 + MAX_SFCCHEAT_NAME,
fs) == 8 + MAX_SFCCHEAT_NAME)
{
if (data[6] != 254 || data[7] != 252)
{
fclose(fs);
return (false);
}
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;
memcpy(Cheat.c [Cheat.num_cheats].name, &data [8], MAX_SFCCHEAT_NAME - 1);
Cheat.c [Cheat.num_cheats++].name [MAX_SFCCHEAT_NAME - 1] = 0;
}
fclose(fs);
return (true);
}
bool S9xSaveCheatFile(const char* filename)
{
if (Cheat.num_cheats == 0)
{
(void) remove(filename);
return (true);
}
FILE* fs = fopen(filename, "wb");
uint8_t data [8 + MAX_SFCCHEAT_NAME];
if (!fs)
return (false);
uint32_t i;
for (i = 0; i < Cheat.num_cheats; i++)
{
memset(data, 0, 8 + MAX_SFCCHEAT_NAME);
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_t) Cheat.c [i].address;
data [3] = (uint8_t)(Cheat.c [i].address >> 8);
data [4] = (uint8_t)(Cheat.c [i].address >> 16);
data [5] = Cheat.c [i].saved_byte;
memcpy(&data [8], Cheat.c [i].name, MAX_SFCCHEAT_NAME - 1);
if (fwrite(data, 8 + MAX_SFCCHEAT_NAME, 1, fs) != 1)
{
fclose(fs);
return (false);
}
}
fclose(fs);
return (true);
}
#endif
|