png screenshot support
[drnoksnes] / cheats2.cpp
1 /*
2  * Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
3  *
4  * (c) Copyright 1996 - 2001 Gary Henderson (gary.henderson@ntlworld.com) and
5  *                           Jerremy Koot (jkoot@snes9x.com)
6  *
7  * Super FX C emulator code 
8  * (c) Copyright 1997 - 1999 Ivar (ivar@snes9x.com) and
9  *                           Gary Henderson.
10  * Super FX assembler emulator code (c) Copyright 1998 zsKnight and _Demo_.
11  *
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).
15  *
16  * DOS port code contains the works of other authors. See headers in
17  * individual files.
18  *
19  * Snes9x homepage: http://www.snes9x.com
20  *
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.
25  *
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.
29  *
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.
33  *
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
36  * in future versions.
37  *
38  * Super NES and Super Nintendo Entertainment System are trademarks of
39  * Nintendo Co., Limited and its subsidiary companies.
40  */
41 #include <stdio.h>
42 #include <ctype.h>
43 #include <string.h>
44 #include "snes9x.h"
45 #include "cheats.h"
46 #include "memmap.h"
47
48 extern SCheatData Cheat;
49
50 void S9xInitCheatData ()
51 {
52     Cheat.RAM = Memory.RAM;
53     Cheat.SRAM = ::SRAM;
54     Cheat.FillRAM = Memory.FillRAM;
55 }
56
57 void S9xAddCheat (bool8 enable, bool8 save_current_value, 
58                   uint32 address, uint8 byte)
59 {
60     if (Cheat.num_cheats < sizeof (Cheat.c) / sizeof (Cheat. c [0]))
61     {
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)
66         {
67             Cheat.c [Cheat.num_cheats].saved_byte = S9xGetByte (address);
68             Cheat.c [Cheat.num_cheats].saved = TRUE;
69         }
70         Cheat.num_cheats++;
71     }
72 }
73
74 void S9xDeleteCheat (uint32 which1)
75 {
76     if (which1 < Cheat.num_cheats)
77     {
78         if (Cheat.c [which1].enabled)
79             S9xRemoveCheat (which1);
80
81         memmove (&Cheat.c [which1], &Cheat.c [which1 + 1],
82                  sizeof (Cheat.c [0]) * (Cheat.num_cheats - which1 - 1));
83         Cheat.num_cheats = 0;
84     }
85 }
86
87 void S9xDeleteCheats ()
88 {
89     S9xRemoveCheats ();
90     Cheat.num_cheats = 0;
91 }
92
93 void S9xEnableCheat (uint32 which1)
94 {
95     if (which1 < Cheat.num_cheats && !Cheat.c [which1].enabled)
96     {
97         Cheat.c [which1].enabled = TRUE;
98         S9xApplyCheat (which1);
99     }
100 }
101
102 void S9xDisableCheat (uint32 which1)
103 {
104     if (which1 < Cheat.num_cheats && Cheat.c [which1].enabled)
105     {
106         S9xRemoveCheat (which1);
107         Cheat.c [which1].enabled = FALSE;
108     }
109 }
110
111 void S9xRemoveCheat (uint32 which1)
112 {
113     if (Cheat.c [which1].saved)
114     {
115         uint32 address = Cheat.c [which1].address;
116
117         int block = (address >> MEMMAP_SHIFT) & MEMMAP_MASK;
118         uint8 *ptr = Memory.Map [block];
119             
120         if (ptr >= (uint8 *) CMemory::MAP_LAST)
121             *(ptr + (address & 0xffff)) = Cheat.c [which1].saved_byte;
122         else
123             S9xSetByte (address, Cheat.c [which1].saved_byte);
124     }
125 }
126
127 void S9xApplyCheat (uint32 which1)
128 {
129     uint32 address = Cheat.c [which1].address;
130
131     if (!Cheat.c [which1].saved)
132         Cheat.c [which1].saved_byte = S9xGetByte (address);
133
134     int block = (address >> MEMMAP_SHIFT) & MEMMAP_MASK;
135     uint8 *ptr = Memory.Map [block];
136     
137     if (ptr >= (uint8 *) CMemory::MAP_LAST)
138         *(ptr + (address & 0xffff)) = Cheat.c [which1].byte;
139     else
140         S9xSetByte (address, Cheat.c [which1].byte);
141     Cheat.c [which1].saved = TRUE;
142 }
143
144 void S9xApplyCheats ()
145 {
146     if (Settings.ApplyCheats)
147     {
148         for (uint32 i = 0; i < Cheat.num_cheats; i++)
149             if (Cheat.c [i].enabled)
150                 S9xApplyCheat (i);
151     }
152 }
153
154 void S9xRemoveCheats ()
155 {
156     for (uint32 i = 0; i < Cheat.num_cheats; i++)
157         if (Cheat.c [i].enabled)
158             S9xRemoveCheat (i);
159 }
160
161 bool8 S9xLoadCheatFile (const char *filename)
162 {
163     Cheat.num_cheats = 0;
164
165     FILE *fs = fopen (filename, "rb");
166     uint8 data [28];
167
168     if (!fs)
169         return (FALSE);
170
171     while (fread ((void *) data, 1, 28, fs) == 28)
172     {
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;
180     }
181     fclose (fs);
182
183     return (TRUE);
184 }
185
186 bool8 S9xSaveCheatFile (const char *filename)
187 {
188     if (Cheat.num_cheats == 0)
189     {
190 #ifndef _SNESPPC
191         (void) remove (filename);
192 #endif
193         return (TRUE);
194     }
195
196     FILE *fs = fopen (filename, "wb");
197     uint8 data [28];
198
199     if (!fs)
200         return (FALSE);
201
202     uint32 i;
203     for (i = 0; i < Cheat.num_cheats; i++)
204     {
205         memset (data, 0, 28);
206         if (i == 0)
207         {
208             data [6] = 254;
209             data [7] = 252;
210         }
211         if (!Cheat.c [i].enabled)
212             data [0] |= 4;
213
214         if (Cheat.c [i].saved)
215             data [0] |= 8;
216
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;
222
223         memmove (&data [8], Cheat.c [i].name, 19);
224         if (fwrite (data, 28, 1, fs) != 1)
225         {
226             fclose (fs);
227             return (FALSE);
228         }
229     }
230     return (fclose (fs) == 0);
231 }
232