Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
RishiRecon
GitHub Repository: RishiRecon/exploits
Path: blob/main/misc/emulator/xnes/snes9x/cheats2.cpp
28515 views
1
/***********************************************************************************
2
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
3
4
(c) Copyright 1996 - 2002 Gary Henderson ([email protected]),
5
Jerremy Koot ([email protected])
6
7
(c) Copyright 2002 - 2004 Matthew Kendora
8
9
(c) Copyright 2002 - 2005 Peter Bortas ([email protected])
10
11
(c) Copyright 2004 - 2005 Joel Yliluoma (http://iki.fi/bisqwit/)
12
13
(c) Copyright 2001 - 2006 John Weidman ([email protected])
14
15
(c) Copyright 2002 - 2006 funkyass ([email protected]),
16
Kris Bleakley ([email protected])
17
18
(c) Copyright 2002 - 2010 Brad Jorsch ([email protected]),
19
Nach ([email protected]),
20
21
(c) Copyright 2002 - 2011 zones ([email protected])
22
23
(c) Copyright 2006 - 2007 nitsuja
24
25
(c) Copyright 2009 - 2011 BearOso,
26
OV2
27
28
29
BS-X C emulator code
30
(c) Copyright 2005 - 2006 Dreamer Nom,
31
zones
32
33
C4 x86 assembler and some C emulation code
34
(c) Copyright 2000 - 2003 _Demo_ ([email protected]),
35
Nach,
36
zsKnight ([email protected])
37
38
C4 C++ code
39
(c) Copyright 2003 - 2006 Brad Jorsch,
40
Nach
41
42
DSP-1 emulator code
43
(c) Copyright 1998 - 2006 _Demo_,
44
Andreas Naive ([email protected]),
45
Gary Henderson,
46
Ivar ([email protected]),
47
John Weidman,
48
Kris Bleakley,
49
Matthew Kendora,
50
Nach,
51
neviksti ([email protected])
52
53
DSP-2 emulator code
54
(c) Copyright 2003 John Weidman,
55
Kris Bleakley,
56
Lord Nightmare ([email protected]),
57
Matthew Kendora,
58
neviksti
59
60
DSP-3 emulator code
61
(c) Copyright 2003 - 2006 John Weidman,
62
Kris Bleakley,
63
Lancer,
64
z80 gaiden
65
66
DSP-4 emulator code
67
(c) Copyright 2004 - 2006 Dreamer Nom,
68
John Weidman,
69
Kris Bleakley,
70
Nach,
71
z80 gaiden
72
73
OBC1 emulator code
74
(c) Copyright 2001 - 2004 zsKnight,
75
pagefault ([email protected]),
76
Kris Bleakley
77
Ported from x86 assembler to C by sanmaiwashi
78
79
SPC7110 and RTC C++ emulator code used in 1.39-1.51
80
(c) Copyright 2002 Matthew Kendora with research by
81
zsKnight,
82
John Weidman,
83
Dark Force
84
85
SPC7110 and RTC C++ emulator code used in 1.52+
86
(c) Copyright 2009 byuu,
87
neviksti
88
89
S-DD1 C emulator code
90
(c) Copyright 2003 Brad Jorsch with research by
91
Andreas Naive,
92
John Weidman
93
94
S-RTC C emulator code
95
(c) Copyright 2001 - 2006 byuu,
96
John Weidman
97
98
ST010 C++ emulator code
99
(c) Copyright 2003 Feather,
100
John Weidman,
101
Kris Bleakley,
102
Matthew Kendora
103
104
Super FX x86 assembler emulator code
105
(c) Copyright 1998 - 2003 _Demo_,
106
pagefault,
107
zsKnight
108
109
Super FX C emulator code
110
(c) Copyright 1997 - 1999 Ivar,
111
Gary Henderson,
112
John Weidman
113
114
Sound emulator code used in 1.5-1.51
115
(c) Copyright 1998 - 2003 Brad Martin
116
(c) Copyright 1998 - 2006 Charles Bilyue'
117
118
Sound emulator code used in 1.52+
119
(c) Copyright 2004 - 2007 Shay Green ([email protected])
120
121
SH assembler code partly based on x86 assembler code
122
(c) Copyright 2002 - 2004 Marcus Comstedt ([email protected])
123
124
2xSaI filter
125
(c) Copyright 1999 - 2001 Derek Liauw Kie Fa
126
127
HQ2x, HQ3x, HQ4x filters
128
(c) Copyright 2003 Maxim Stepin ([email protected])
129
130
NTSC filter
131
(c) Copyright 2006 - 2007 Shay Green
132
133
GTK+ GUI code
134
(c) Copyright 2004 - 2011 BearOso
135
136
Win32 GUI code
137
(c) Copyright 2003 - 2006 blip,
138
funkyass,
139
Matthew Kendora,
140
Nach,
141
nitsuja
142
(c) Copyright 2009 - 2011 OV2
143
144
Mac OS GUI code
145
(c) Copyright 1998 - 2001 John Stiles
146
(c) Copyright 2001 - 2011 zones
147
148
149
Specific ports contains the works of other authors. See headers in
150
individual files.
151
152
153
Snes9x homepage: http://www.snes9x.com/
154
155
Permission to use, copy, modify and/or distribute Snes9x in both binary
156
and source form, for non-commercial purposes, is hereby granted without
157
fee, providing that this license information and copyright notice appear
158
with all copies and any derived work.
159
160
This software is provided 'as-is', without any express or implied
161
warranty. In no event shall the authors be held liable for any damages
162
arising from the use of this software or it's derivatives.
163
164
Snes9x is freeware for PERSONAL USE only. Commercial users should
165
seek permission of the copyright holders first. Commercial use includes,
166
but is not limited to, charging money for Snes9x or software derived from
167
Snes9x, including Snes9x or derivatives in commercial game bundles, and/or
168
using Snes9x as a promotion for your commercial product.
169
170
The copyright holders request that bug fixes and improvements to the code
171
should be forwarded to them so everyone can benefit from the modifications
172
in future versions.
173
174
Super NES and Super Nintendo Entertainment System are trademarks of
175
Nintendo Co., Limited and its subsidiary companies.
176
***********************************************************************************/
177
178
179
#include "snes9x.h"
180
#include "memmap.h"
181
#include "cheats.h"
182
183
static uint8 S9xGetByteFree (uint32);
184
static void S9xSetByteFree (uint8, uint32);
185
186
187
static uint8 S9xGetByteFree (uint32 address)
188
{
189
uint32 Cycles = CPU.Cycles;
190
uint8 byte;
191
192
byte = S9xGetByte(address);
193
CPU.Cycles = Cycles;
194
195
return (byte);
196
}
197
198
static void S9xSetByteFree (uint8 byte, uint32 address)
199
{
200
uint32 Cycles = CPU.Cycles;
201
202
S9xSetByte(byte, address);
203
CPU.Cycles = Cycles;
204
}
205
206
void S9xInitWatchedAddress (void)
207
{
208
for (unsigned int i = 0; i < sizeof(watches) / sizeof(watches[0]); i++)
209
watches[i].on = false;
210
211
}
212
213
void S9xInitCheatData (void)
214
{
215
Cheat.RAM = Memory.RAM;
216
Cheat.SRAM = Memory.SRAM;
217
Cheat.FillRAM = Memory.FillRAM;
218
}
219
220
void S9xAddCheat (bool8 enable, bool8 save_current_value, uint32 address, uint8 byte)
221
{
222
if (Cheat.num_cheats < sizeof(Cheat.c) / sizeof(Cheat.c[0]))
223
{
224
Cheat.c[Cheat.num_cheats].address = address;
225
Cheat.c[Cheat.num_cheats].byte = byte;
226
Cheat.c[Cheat.num_cheats].enabled = enable;
227
228
if (save_current_value)
229
{
230
Cheat.c[Cheat.num_cheats].saved_byte = S9xGetByteFree(address);
231
Cheat.c[Cheat.num_cheats].saved = TRUE;
232
}
233
234
Cheat.num_cheats++;
235
}
236
}
237
238
void S9xDeleteCheat (uint32 which1)
239
{
240
if (which1 < Cheat.num_cheats)
241
{
242
if (Cheat.c[which1].enabled)
243
S9xRemoveCheat(which1);
244
245
memmove(&Cheat.c[which1], &Cheat.c[which1 + 1], sizeof(Cheat.c[0]) * (Cheat.num_cheats - which1 - 1));
246
247
Cheat.num_cheats--;
248
}
249
}
250
251
void S9xDeleteCheats (void)
252
{
253
S9xRemoveCheats();
254
Cheat.num_cheats = 0;
255
}
256
257
void S9xRemoveCheat (uint32 which1)
258
{
259
if (Cheat.c[which1].saved)
260
{
261
uint32 address = Cheat.c[which1].address;
262
263
int block = (address & 0xffffff) >> MEMMAP_SHIFT;
264
uint8 *ptr = Memory.Map[block];
265
266
if (ptr >= (uint8 *) CMemory::MAP_LAST)
267
*(ptr + (address & 0xffff)) = Cheat.c[which1].saved_byte;
268
else
269
S9xSetByteFree(Cheat.c[which1].saved_byte, address);
270
}
271
}
272
273
void S9xRemoveCheats (void)
274
{
275
for (uint32 i = 0; i < Cheat.num_cheats; i++)
276
if (Cheat.c[i].enabled)
277
S9xRemoveCheat(i);
278
}
279
280
void S9xEnableCheat (uint32 which1)
281
{
282
if (which1 < Cheat.num_cheats && !Cheat.c[which1].enabled)
283
{
284
Cheat.c[which1].enabled = TRUE;
285
S9xApplyCheat(which1);
286
}
287
}
288
289
void S9xDisableCheat (uint32 which1)
290
{
291
if (which1 < Cheat.num_cheats && Cheat.c[which1].enabled)
292
{
293
S9xRemoveCheat(which1);
294
Cheat.c[which1].enabled = FALSE;
295
}
296
}
297
298
void S9xApplyCheat (uint32 which1)
299
{
300
uint32 address = Cheat.c[which1].address;
301
302
if (!Cheat.c[which1].saved)
303
{
304
Cheat.c[which1].saved_byte = S9xGetByteFree(address);
305
Cheat.c[which1].saved = TRUE;
306
}
307
308
int block = (address & 0xffffff) >> MEMMAP_SHIFT;
309
uint8 *ptr = Memory.Map[block];
310
311
if (ptr >= (uint8 *) CMemory::MAP_LAST)
312
*(ptr + (address & 0xffff)) = Cheat.c[which1].byte;
313
else
314
S9xSetByteFree(Cheat.c[which1].byte, address);
315
}
316
317
void S9xApplyCheats (void)
318
{
319
if (Settings.ApplyCheats)
320
{
321
for (uint32 i = 0; i < Cheat.num_cheats; i++)
322
if (Cheat.c[i].enabled)
323
S9xApplyCheat(i);
324
}
325
}
326
327
bool8 S9xLoadCheatFile (const char *filename)
328
{
329
FILE *fs;
330
uint8 data[28];
331
332
Cheat.num_cheats = 0;
333
334
fs = fopen(filename, "rb");
335
if (!fs)
336
return (FALSE);
337
338
while (fread((void *) data, 1, 28, fs) == 28)
339
{
340
Cheat.c[Cheat.num_cheats].enabled = (data[0] & 4) == 0;
341
Cheat.c[Cheat.num_cheats].byte = data[1];
342
Cheat.c[Cheat.num_cheats].address = data[2] | (data[3] << 8) | (data[4] << 16);
343
Cheat.c[Cheat.num_cheats].saved_byte = data[5];
344
Cheat.c[Cheat.num_cheats].saved = (data[0] & 8) != 0;
345
memmove(Cheat.c[Cheat.num_cheats].name, &data[8], 20);
346
Cheat.c[Cheat.num_cheats++].name[20] = 0;
347
}
348
349
fclose(fs);
350
351
return (TRUE);
352
}
353
354
bool8 S9xSaveCheatFile (const char *filename)
355
{
356
if (Cheat.num_cheats == 0)
357
{
358
remove(filename);
359
return (TRUE);
360
}
361
362
FILE *fs;
363
uint8 data[28];
364
365
fs = fopen(filename, "wb");
366
if (!fs)
367
return (FALSE);
368
369
for (uint32 i = 0; i < Cheat.num_cheats; i++)
370
{
371
ZeroMemory(data, 28);
372
373
if (i == 0)
374
{
375
data[6] = 254;
376
data[7] = 252;
377
}
378
379
if (!Cheat.c[i].enabled)
380
data[0] |= 4;
381
382
if (Cheat.c[i].saved)
383
data[0] |= 8;
384
385
data[1] = Cheat.c[i].byte;
386
data[2] = (uint8) (Cheat.c[i].address >> 0);
387
data[3] = (uint8) (Cheat.c[i].address >> 8);
388
data[4] = (uint8) (Cheat.c[i].address >> 16);
389
data[5] = Cheat.c[i].saved_byte;
390
391
memmove(&data[8], Cheat.c[i].name, 19);
392
393
if (fwrite(data, 28, 1, fs) != 1)
394
{
395
fclose(fs);
396
return (FALSE);
397
}
398
}
399
400
return (fclose(fs) == 0);
401
}
402
403