2 * Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
4 * (c) Copyright 1996 - 2001 Gary Henderson (gary.henderson@ntlworld.com) and
5 * Jerremy Koot (jkoot@snes9x.com)
7 * Super FX C emulator code
8 * (c) Copyright 1997 - 1999 Ivar (ivar@snes9x.com) and
10 * Super FX assembler emulator code (c) Copyright 1998 zsKnight and _Demo_.
12 * DSP1 emulator code (c) Copyright 1998 Ivar, _Demo_ and Gary Henderson.
13 * C4 asm and some C emulation code (c) Copyright 2000 zsKnight and _Demo_.
14 * C4 C code (c) Copyright 2001 Gary Henderson (gary.henderson@ntlworld.com).
16 * DOS port code contains the works of other authors. See headers in
19 * Snes9x homepage: http://www.snes9x.com
21 * Permission to use, copy, modify and distribute Snes9x in both binary and
22 * source form, for non-commercial purposes, is hereby granted without fee,
23 * providing that this license information and copyright notice appear with
24 * all copies and any derived work.
26 * This software is provided 'as-is', without any express or implied
27 * warranty. In no event shall the authors be held liable for any damages
28 * arising from the use of this software.
30 * Snes9x is freeware for PERSONAL USE only. Commercial users should
31 * seek permission of the copyright holders first. Commercial use includes
32 * charging money for Snes9x or software derived from Snes9x.
34 * The copyright holders request that bug fixes and improvements to the code
35 * should be forwarded to them so everyone can benefit from the modifications
38 * Super NES and Super Nintendo Entertainment System are trademarks of
39 * Nintendo Co., Limited and its subsidiary companies.
48 extern SCheatData Cheat;
50 void S9xInitCheatData ()
52 Cheat.RAM = Memory.RAM;
54 Cheat.FillRAM = Memory.FillRAM;
57 void S9xAddCheat (bool8 enable, bool8 save_current_value,
58 uint32 address, uint8 byte)
60 if (Cheat.num_cheats < sizeof (Cheat.c) / sizeof (Cheat. c [0]))
62 Cheat.c [Cheat.num_cheats].address = address;
63 Cheat.c [Cheat.num_cheats].byte = byte;
64 Cheat.c [Cheat.num_cheats].enabled = TRUE;
65 if (save_current_value)
67 Cheat.c [Cheat.num_cheats].saved_byte = S9xGetByte (address);
68 Cheat.c [Cheat.num_cheats].saved = TRUE;
74 void S9xDeleteCheat (uint32 which1)
76 if (which1 < Cheat.num_cheats)
78 if (Cheat.c [which1].enabled)
79 S9xRemoveCheat (which1);
81 memmove (&Cheat.c [which1], &Cheat.c [which1 + 1],
82 sizeof (Cheat.c [0]) * (Cheat.num_cheats - which1 - 1));
87 void S9xDeleteCheats ()
93 void S9xEnableCheat (uint32 which1)
95 if (which1 < Cheat.num_cheats && !Cheat.c [which1].enabled)
97 Cheat.c [which1].enabled = TRUE;
98 S9xApplyCheat (which1);
102 void S9xDisableCheat (uint32 which1)
104 if (which1 < Cheat.num_cheats && Cheat.c [which1].enabled)
106 S9xRemoveCheat (which1);
107 Cheat.c [which1].enabled = FALSE;
111 void S9xRemoveCheat (uint32 which1)
113 if (Cheat.c [which1].saved)
115 uint32 address = Cheat.c [which1].address;
117 int block = (address >> MEMMAP_SHIFT) & MEMMAP_MASK;
118 uint8 *ptr = Memory.Map [block];
120 if (ptr >= (uint8 *) CMemory::MAP_LAST)
121 *(ptr + (address & 0xffff)) = Cheat.c [which1].saved_byte;
123 S9xSetByte (address, Cheat.c [which1].saved_byte);
127 void S9xApplyCheat (uint32 which1)
129 uint32 address = Cheat.c [which1].address;
131 if (!Cheat.c [which1].saved)
132 Cheat.c [which1].saved_byte = S9xGetByte (address);
134 int block = (address >> MEMMAP_SHIFT) & MEMMAP_MASK;
135 uint8 *ptr = Memory.Map [block];
137 if (ptr >= (uint8 *) CMemory::MAP_LAST)
138 *(ptr + (address & 0xffff)) = Cheat.c [which1].byte;
140 S9xSetByte (address, Cheat.c [which1].byte);
141 Cheat.c [which1].saved = TRUE;
144 void S9xApplyCheats ()
146 if (Settings.ApplyCheats)
148 for (uint32 i = 0; i < Cheat.num_cheats; i++)
149 if (Cheat.c [i].enabled)
154 void S9xRemoveCheats ()
156 for (uint32 i = 0; i < Cheat.num_cheats; i++)
157 if (Cheat.c [i].enabled)
161 bool8 S9xLoadCheatFile (const char *filename)
163 Cheat.num_cheats = 0;
165 FILE *fs = fopen (filename, "rb");
171 while (fread ((void *) data, 1, 28, fs) == 28)
173 Cheat.c [Cheat.num_cheats].enabled = (data [0] & 4) == 0;
174 Cheat.c [Cheat.num_cheats].byte = data [1];
175 Cheat.c [Cheat.num_cheats].address = data [2] | (data [3] << 8) | (data [4] << 16);
176 Cheat.c [Cheat.num_cheats].saved_byte = data [5];
177 Cheat.c [Cheat.num_cheats].saved = (data [0] & 8) != 0;
178 memmove (Cheat.c [Cheat.num_cheats].name, &data [8], 20);
179 Cheat.c [Cheat.num_cheats++].name [20] = 0;
186 bool8 S9xSaveCheatFile (const char *filename)
188 if (Cheat.num_cheats == 0)
191 (void) remove (filename);
196 FILE *fs = fopen (filename, "wb");
203 for (i = 0; i < Cheat.num_cheats; i++)
205 memset (data, 0, 28);
211 if (!Cheat.c [i].enabled)
214 if (Cheat.c [i].saved)
217 data [1] = Cheat.c [i].byte;
218 data [2] = (uint8) Cheat.c [i].address;
219 data [3] = (uint8) (Cheat.c [i].address >> 8);
220 data [4] = (uint8) (Cheat.c [i].address >> 16);
221 data [5] = Cheat.c [i].saved_byte;
223 memmove (&data [8], Cheat.c [i].name, 19);
224 if (fwrite (data, 28, 1, fs) != 1)
230 return (fclose (fs) == 0);