Debian Codegraph @

mednafen/state.cpp

raw symbols SCIP
1 /* Mednafen - Multi-system Emulator
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 #include "mednafen.h"cxx . . $ `<file>/mednafen/mednafen.h`/
19
20 #include <string.h>
21
22 #include "driver.h"cxx . . $ `<file>/mednafen/driver.h`/
23 #include "general.h"cxx . . $ `<file>/mednafen/general.h`/
24 #include "state.h"cxx . . $ `<file>/mednafen/state.h`/
25 #include "video.h"cxx . . $ `<file>/mednafen/video.h`/
26 #include "msvc_compat.h"cxx . . $ `<file>/mednafen/msvc_compat.h`/
27
28 #define RLSBcxx . . $ `mednafen/state.cpp:28:9`! MDFNSTATE_RLSBcxx . . $ `mednafen/state.h:27:9`! //0x80000000
29
30 int32cxx . . $ int32# smem_readcxx . . $ smem_read(f3982eecb66031e).(StateMemcxx . . $ StateMem# *st, void *buffer, uint32cxx . . $ uint32# len)
31 {
32 if ((len + st->loccxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#loc.) > st->lencxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#len.)
33 return 0;
34
35 memcpy(buffer, st->datacxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#data. + st->loccxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#loc., len);
36 st->loccxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#loc. += len;
37
38 return(len);
39 }
40
41 int32cxx . . $ int32# smem_writecxx . . $ smem_write(f3982eecb66031e).(StateMemcxx . . $ StateMem# *st, void *buffer, uint32cxx . . $ uint32# len)
42 {
43 if ((len + st->loccxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#loc.) > st->mallocedcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#malloced.)
44 {
45 uint32cxx . . $ uint32# newsize = (st->mallocedcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#malloced. >= 32768) ? st->mallocedcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#malloced. : (st->initial_malloccxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#initial_malloc. ? st->initial_malloccxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#initial_malloc. : 32768);
46
47 while(newsize < (len + st->loccxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#loc.))
48 newsize *= 2;
49 st->datacxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#data. = (uint8cxx . . $ uint8# *)realloccxx . . $ realloc(42b2a9dd8f084bf2).(st->datacxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#data., newsize);
50 st->mallocedcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#malloced. = newsize;
51 }
52 memcpy(st->datacxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#data. + st->loccxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#loc., buffer, len);
53 st->loccxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#loc. += len;
54
55 if (st->loccxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#loc. > st->lencxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#len.)
56 st->lencxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#len. = st->loccxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#loc.;
57
58 return(len);
59 }
60
61 int32cxx . . $ int32# smem_putccxx . . $ smem_putc(453c298c0ba01c96).(StateMemcxx . . $ StateMem# *st, int value)
62 {
63 uint8cxx . . $ uint8# tmpval = value;
64 if(smem_writecxx . . $ smem_write(f3982eecb66031e).(st, &tmpval, 1) != 1)
65 return(-1);
66 return(1);
67 }
68
69 int32cxx . . $ int32# smem_tellcxx . . $ smem_tell(11cbcd7e5e652dd0).(StateMemcxx . . $ StateMem# *st)
70 {
71 return(st->loccxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#loc.);
72 }
73
74 int32cxx . . $ int32# smem_seekcxx . . $ smem_seek(bece8f0ac61bc7de).(StateMemcxx . . $ StateMem# *st, uint32cxx . . $ uint32# offset, int whence)
75 {
76 switch(whence)
77 {
78 case SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!: st->loccxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#loc. = offset; break;
79 case SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:112:9`!: st->loccxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#loc. = st->lencxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#len. - offset; break;
80 case SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!: st->loccxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#loc. += offset; break;
81 }
82
83 if(st->loccxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#loc. > st->lencxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#len.)
84 {
85 st->loccxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#loc. = st->lencxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#len.;
86 return(-1);
87 }
88
89 if(st->loccxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#loc. < 0)
90 {
91 st->loccxx . . $ $anonymous_type_8d6ca9fa7c8baca5_0#loc. = 0;
92 return(-1);
93 }
94
95 return(0);
96 }
97
98 int smem_write32lecxx . . $ smem_write32le(776f7c4058af06c3).(StateMemcxx . . $ StateMem# *st, uint32cxx . . $ uint32# b)
99 {
100 uint8cxx . . $ uint8# s[4];
101 s[0]=b;
102 s[1]=b>>8;
103 s[2]=b>>16;
104 s[3]=b>>24;
105 return((smem_writecxx . . $ smem_write(f3982eecb66031e).(st, s, 4)<4)?0:4);
106 }
107
108 int smem_read32lecxx . . $ smem_read32le(24a07498b85603a0).(StateMemcxx . . $ StateMem# *st, uint32cxx . . $ uint32# *b)
109 {
110 uint8cxx . . $ uint8# s[4];
111
112 if(smem_readcxx . . $ smem_read(f3982eecb66031e).(st, s, 4) < 4)
113 return(0);
114
115 *b = s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24);
116
117 return(4);
118 }
119
120 static bool SubWritecxx . . $ SubWrite(ad14fc7377575df3).(StateMemcxx . . $ StateMem# *st, SFORMATcxx . . $ SFORMAT# *sf, const char *name_prefix = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!)
121 {
122 while(sf->sizecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#size. || sf->namecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#name.) // Size can sometimes be zero, so also check for the text name. These two should both be zero only at the end of a struct.
123 {
124 if(!sf->sizecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#size. || !sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v.)
125 {
126 sf++;
127 continue;
128 }
129
130 if(sf->sizecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#size. == (uint32cxx . . $ uint32#)~0) /* Link to another struct. */
131 {
132 if(!SubWritecxx . . $ SubWrite(ad14fc7377575df3).(st, (SFORMATcxx . . $ SFORMAT# *)sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., name_prefix))
133 return(0);
134
135 sf++;
136 continue;
137 }
138
139 int32cxx . . $ int32# bytesize = sf->sizecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#size.;
140
141 char nameo[1 + 256];
142 int slen;
143
144 slen = snprintf(nameo + 1, 256, "%s%s", name_prefix ? name_prefix : "", sf->namecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#name.);
145 nameo[0] = slen;
146
147 if(slen >= 255)
148 {
149 printf("Warning: state variable name possibly too long: %s %s %s %d\n", sf->namecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#name., name_prefix, nameo, slen);
150 slen = 255;
151 }
152
153 smem_writecxx . . $ smem_write(f3982eecb66031e).(st, nameo, 1 + nameo[0]);
154 smem_write32lecxx . . $ smem_write32le(776f7c4058af06c3).(st, bytesize);
155
156 /* Flip the byte order... */
157 if(sf->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_BOOLcxx . . $ `mednafen/state.h:38:9`!)
158 {
159
160 }
161 else if(sf->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_RLSB64cxx . . $ `mednafen/state.h:36:9`!)
162 Endian_A64_NE_to_LE(sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., bytesize / sizeof(uint64cxx . . $ uint64#));
163 else if(sf->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_RLSB32cxx . . $ `mednafen/state.h:30:9`!)
164 Endian_A32_NE_to_LE(sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., bytesize / sizeof(uint32cxx . . $ uint32#));
165 else if(sf->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_RLSB16cxx . . $ `mednafen/state.h:33:9`!)
166 Endian_A16_NE_to_LE(sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., bytesize / sizeof(uint16cxx . . $ uint16#));
167 else if(sf->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & RLSBcxx . . $ `mednafen/state.cpp:28:9`!)
168 Endian_V_NE_to_LE(sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., bytesize);
169
170 // Special case for the evil bool type, to convert bool to 1-byte elements.
171 // Don't do it if we're only saving the raw data.
172 if(sf->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_BOOLcxx . . $ `mednafen/state.h:38:9`!)
173 {
174 for(int32cxx . . $ int32# bool_monster = 0; bool_monster < bytesize; bool_monster++)
175 {
176 uint8cxx . . $ uint8# tmp_bool = ((bool *)sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v.)[bool_monster];
177 //printf("Bool write: %.31s\n", sf->name);
178 smem_writecxx . . $ smem_write(f3982eecb66031e).(st, &tmp_bool, 1);
179 }
180 }
181 else
182 smem_writecxx . . $ smem_write(f3982eecb66031e).(st, (uint8cxx . . $ uint8# *)sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., bytesize);
183
184 /* Now restore the original byte order. */
185 if(sf->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_BOOLcxx . . $ `mednafen/state.h:38:9`!)
186 {
187
188 }
189 else if(sf->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_RLSB64cxx . . $ `mednafen/state.h:36:9`!)
190 Endian_A64_LE_to_NE(sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., bytesize / sizeof(uint64cxx . . $ uint64#));
191 else if(sf->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_RLSB32cxx . . $ `mednafen/state.h:30:9`!)
192 Endian_A32_LE_to_NE(sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., bytesize / sizeof(uint32cxx . . $ uint32#));
193 else if(sf->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_RLSB16cxx . . $ `mednafen/state.h:33:9`!)
194 Endian_A16_LE_to_NE(sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., bytesize / sizeof(uint16cxx . . $ uint16#));
195 else if(sf->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & RLSBcxx . . $ `mednafen/state.cpp:28:9`!)
196 Endian_V_LE_to_NE(sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., bytesize);
197 sf++;
198 }
199
200 return(TRUEcxx . . $ `mednafen/mednafen-types.h:113:9`!);
201 }
202
203 static int WriteStateChunkcxx . . $ WriteStateChunk(3cb24b6ce25cfb57).(StateMemcxx . . $ StateMem# *st, const char *sname, SFORMATcxx . . $ SFORMAT# *sf)
204 {
205 int32cxx . . $ int32# data_start_pos;
206 int32cxx . . $ int32# end_pos;
207
208 uint8cxx . . $ uint8# sname_tmp[32];
209
210 memset(sname_tmp, 0, sizeof(sname_tmp));
211 strncpy((char *)sname_tmp, sname, 32);
212
213 if(strlen(sname) > 32)
214 printf("Warning: section name is too long: %s\n", sname);
215
216 smem_writecxx . . $ smem_write(f3982eecb66031e).(st, sname_tmp, 32);
217
218 smem_write32lecxx . . $ smem_write32le(776f7c4058af06c3).(st, 0); // We'll come back and write this later.
219
220 data_start_pos = smem_tellcxx . . $ smem_tell(11cbcd7e5e652dd0).(st);
221
222 if(!SubWritecxx . . $ SubWrite(ad14fc7377575df3).(st, sf))
223 return(0);
224
225 end_pos = smem_tellcxx . . $ smem_tell(11cbcd7e5e652dd0).(st);
226
227 smem_seekcxx . . $ smem_seek(bece8f0ac61bc7de).(st, data_start_pos - 4, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!);
228 smem_write32lecxx . . $ smem_write32le(776f7c4058af06c3).(st, end_pos - data_start_pos);
229 smem_seekcxx . . $ smem_seek(bece8f0ac61bc7de).(st, end_pos, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!);
230
231 return(end_pos - data_start_pos);
232 }
233
234 struct compare_cstrcxx . . $ compare_cstr#
235 {
236 bool operatorcxx . . $ compare_cstr#`operator()`(f735519873e6c132).()(const char *s1, const char *s2) const
237 {
238 return(strcmp(s1, s2) < 0);
239 }
240 };
241
242 typedef stdcxx . . $ std/::mapcxx . . $ std/map#<const char *, SFORMATcxx . . $ SFORMAT# *, compare_cstrcxx . . $ compare_cstr#> SFMap_tcxx . . $ SFMap_t#;
243
244 static void MakeSFMapcxx . . $ MakeSFMap(e04155f555d74b64).(SFORMATcxx . . $ SFORMAT# *sf, SFMap_tcxx . . $ SFMap_t# &sfmap)
245 {
246 while(sf->sizecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#size. || sf->namecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#name.) // Size can sometimes be zero, so also check for the text name. These two should both be zero only at the end of a struct.
247 {
248 if(!sf->sizecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#size. || !sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v.)
249 {
250 sf++;
251 continue;
252 }
253
254 if(sf->sizecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#size. == (uint32cxx . . $ uint32#)~0) /* Link to another SFORMAT structure. */
255 MakeSFMapcxx . . $ MakeSFMap(e04155f555d74b64).((SFORMATcxx . . $ SFORMAT# *)sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., sfmap);
256 else
257 {
258 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(sf->name);
259
260 if(sfmap.findcxx . . $ std/map#find(36f8d56706583706).(sf->namecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#name.) !=cxx . . $ std/`operator!=`(be6a71a859368080). sfmap.endcxx . . $ std/map#end(7e4de33083b6e738).())
261 printf("Duplicate save state variable in internal emulator structures(CLUB THE PROGRAMMERS WITH BREADSTICKS): %s\n", sf->namecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#name.);
262
263 sfmap[cxx . . $ std/map#`operator[]`(bbd415b616967878).sf->namecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#name.] = sf;
264 }
265
266 sf++;
267 }
268 }
269
270 // Fast raw chunk reader
271 static void DOReadChunkcxx . . $ DOReadChunk(4245d66670007096).(StateMemcxx . . $ StateMem# *st, SFORMATcxx . . $ SFORMAT# *sf)
272 {
273 while(sf->sizecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#size. || sf->namecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#name.) // Size can sometimes be zero, so also check for the text name.
274 // These two should both be zero only at the end of a struct.
275 {
276 if(!sf->sizecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#size. || !sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v.)
277 {
278 sf++;
279 continue;
280 }
281
282 if(sf->sizecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#size. == (uint32cxx . . $ uint32#) ~0) // Link to another SFORMAT struct
283 {
284 DOReadChunkcxx . . $ DOReadChunk(4245d66670007096).(st, (SFORMATcxx . . $ SFORMAT# *)sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v.);
285 sf++;
286 continue;
287 }
288
289 int32cxx . . $ int32# bytesize = sf->sizecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#size.;
290
291 // Loading raw data, bool types are stored as they appear in memory, not as single bytes in the full state format.
292 // In the SFORMAT structure, the size member for bool entries is the number of bool elements, not the total in-memory size,
293 // so we adjust it here.
294 if(sf->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_BOOLcxx . . $ `mednafen/state.h:38:9`!)
295 bytesize *= sizeof(bool);
296
297 smem_readcxx . . $ smem_read(f3982eecb66031e).(st, (uint8cxx . . $ uint8# *)sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., bytesize);
298 sf++;
299 }
300 }
301
302 static int ReadStateChunkcxx . . $ ReadStateChunk(96770ba063a35803).(StateMemcxx . . $ StateMem# *st, SFORMATcxx . . $ SFORMAT# *sf, int size)
303 {
304 int temp;
305
306 {
307 SFMap_tcxx . . $ SFMap_t# sfmapcxx . . $ std/map#map(49f6e7a06ebc5aa8).;
308 SFMap_tcxx . . $ SFMap_t# sfmap_foundcxx . . $ std/map#map(49f6e7a06ebc5aa8).; // Used for identifying variables that are missing in the save state.
309
310 MakeSFMapcxx . . $ MakeSFMap(e04155f555d74b64).(sf, sfmap);
311
312 temp = smem_tellcxx . . $ smem_tell(11cbcd7e5e652dd0).(st);
313 while(smem_tellcxx . . $ smem_tell(11cbcd7e5e652dd0).(st) < (temp + size))
314 {
315 uint32cxx . . $ uint32# recorded_size; // In bytes
316 uint8cxx . . $ uint8# toa[1 + 256]; // Don't change to char unless cast toa[0] to unsigned to smem_read() and other places.
317
318 if(smem_readcxx . . $ smem_read(f3982eecb66031e).(st, toa, 1) != 1)
319 {
320 puts("Unexpected EOF");
321 return(0);
322 }
323
324 if(smem_readcxx . . $ smem_read(f3982eecb66031e).(st, toa + 1, toa[0]) != toa[0])
325 {
326 puts("Unexpected EOF?");
327 return 0;
328 }
329
330 toa[1 + toa[0]] = 0;
331
332 smem_read32lecxx . . $ smem_read32le(24a07498b85603a0).(st, &recorded_size);
333
334 SFMap_tcxx . . $ SFMap_t#::iteratorcxx . . $ std/map#iterator# sfmitcxx . . $ std/_Rb_tree_iterator#_Rb_tree_iterator(ced63f7c635d850d).;
335
336 sfmit =cxx . . $ std/_Rb_tree_iterator#`operator=`(3286246de5d22236). sfmap.findcxx . . $ std/map#find(36f8d56706583706).((char *)toa + 1);
337
338 if(sfmit !=cxx . . $ std/`operator!=`(be6a71a859368080). sfmap.endcxx . . $ std/map#end(7e4de33083b6e738).())
339 {
340 SFORMATcxx . . $ SFORMAT# *tmp = sfmit->cxx . . $ std/_Rb_tree_iterator#`operator->`(34d6acfd341ba7ab).secondcxx . . $ std/pair#second.;
341 uint32cxx . . $ uint32# expected_size = tmp->sizecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#size.; // In bytes
342
343 if(recorded_size != expected_size)
344 {
345 printf("Variable in save state wrong size: %s. Need: %d, got: %d\n", toa + 1, expected_size, recorded_size);
346 if(smem_seekcxx . . $ smem_seek(bece8f0ac61bc7de).(st, recorded_size, SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!) < 0)
347 {
348 puts("Seek error");
349 return(0);
350 }
351 }
352 else
353 {
354 sfmap_found[cxx . . $ std/map#`operator[]`(bbd415b616967878).tmp->namecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#name.] = tmp;
355
356 smem_readcxx . . $ smem_read(f3982eecb66031e).(st, (uint8cxx . . $ uint8# *)tmp->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., expected_size);
357
358 if(tmp->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_BOOLcxx . . $ `mednafen/state.h:38:9`!)
359 {
360 // Converting downwards is necessary for the case of sizeof(bool) > 1
361 for(int32cxx . . $ int32# bool_monster = expected_size - 1; bool_monster >= 0; bool_monster--)
362 {
363 ((bool *)tmp->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v.)[bool_monster] = ((uint8cxx . . $ uint8# *)tmp->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v.)[bool_monster];
364 }
365 }
366 if(tmp->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_RLSB64cxx . . $ `mednafen/state.h:36:9`!)
367 Endian_A64_LE_to_NE(tmp->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., expected_size / sizeof(uint64cxx . . $ uint64#));
368 else if(tmp->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_RLSB32cxx . . $ `mednafen/state.h:30:9`!)
369 Endian_A32_LE_to_NE(tmp->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., expected_size / sizeof(uint32cxx . . $ uint32#));
370 else if(tmp->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_RLSB16cxx . . $ `mednafen/state.h:33:9`!)
371 Endian_A16_LE_to_NE(tmp->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., expected_size / sizeof(uint16cxx . . $ uint16#));
372 else if(tmp->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & RLSBcxx . . $ `mednafen/state.cpp:28:9`!)
373 Endian_V_LE_to_NE(tmp->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., expected_size);
374 }
375 }
376 else
377 {
378 printf("Unknown variable in save state: %s\n", toa + 1);
379 if(smem_seekcxx . . $ smem_seek(bece8f0ac61bc7de).(st, recorded_size, SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!) < 0)
380 {
381 puts("Seek error");
382 return(0);
383 }
384 }
385 } // while(...)
386
387 for(SFMap_tcxx . . $ SFMap_t#::const_iteratorcxx . . $ std/map#const_iterator# it = sfmapcxx . . $ std/_Rb_tree_const_iterator#_Rb_tree_const_iterator(dc529b7882ffd73b)..begincxx . . $ std/map#begin(7e4de33083b6e738).(); it !=cxx . . $ std/`operator!=`(a84eb84a7a5e8237). sfmapcxx . . $ std/_Rb_tree_const_iterator#_Rb_tree_const_iterator(dc529b7882ffd73b)..endcxx . . $ std/map#end(7e4de33083b6e738).(); it++cxx . . $ std/_Rb_tree_const_iterator#operator++(bc8647ccc78a993a).)
388 {
389 if(sfmap_found.findcxx . . $ std/map#find(36f8d56706583706).(it->cxx . . $ std/_Rb_tree_const_iterator#`operator->`(8e614c7de0b1573e).secondcxx . . $ std/pair#second.->namecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#name.) ==cxx . . $ std/`operator==`(be6a71a859368080). sfmap_found.endcxx . . $ std/map#end(7e4de33083b6e738).())
390 {
391 printf("Variable missing from save state: %s\n", it->cxx . . $ std/_Rb_tree_const_iterator#`operator->`(8e614c7de0b1573e).secondcxx . . $ std/pair#second.->namecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#name.);
392 }
393 }
394
395 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(smem_tell(st) == (temp + size));
396 }
397 return 1;
398 }
399
400 static int CurrentStatecxx . . $ CurrentState. = 0;
401
402 /* This function is called by the game driver(NES, GB, GBA) to save a state. */
403 int MDFNSS_StateActioncxx . . $ MDFNSS_StateAction(eba9425d234c8c23).(void *st_p, int load, int data_only, stdcxx . . $ std/::vectorcxx . . $ std/vector# <SSDescriptorcxx . . $ SSDescriptor#> &sections)
404 {
405 StateMemcxx . . $ StateMem# *st = (StateMemcxx . . $ StateMem#*)st_p;
406 stdcxx . . $ std/::vectorcxx . . $ std/vector#<SSDescriptor>::iteratorcxx . . $ std/vector#iterator# sectioncxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(ced63f7c635d850d).;
407
408 if(load)
409 {
410 {
411 char sname[32];
412
413 for(section =cxx . . $ __gnu_cxx/__normal_iterator#`operator=`(9050870c3bc69589). sections.begincxx . . $ std/vector#begin(55a6de06420d4903).(); section !=cxx . . $ __gnu_cxx/`operator!=`(30ee64f2df4eef8b). sections.endcxx . . $ std/vector#end(55a6de06420d4903).(); section++cxx . . $ __gnu_cxx/__normal_iterator#operator++(6cefca3db4774be2).)
414 {
415 int found = 0;
416 uint32cxx . . $ uint32# tmp_size;
417 uint32cxx . . $ uint32# total = 0;
418
419 while(smem_readcxx . . $ smem_read(f3982eecb66031e).(st, (uint8cxx . . $ uint8# *)sname, 32) == 32)
420 {
421 if(smem_read32lecxx . . $ smem_read32le(24a07498b85603a0).(st, &tmp_size) != 4)
422 return(0);
423
424 total += tmp_size + 32 + 4;
425
426 // Yay, we found the section
427 if(!strncmp(sname, section->cxx . . $ __gnu_cxx/__normal_iterator#`operator->`(65c2ec7035a77d3e).namecxx . . $ SSDescriptor#name., 32))
428 {
429 if(!ReadStateChunkcxx . . $ ReadStateChunk(96770ba063a35803).(st, section->cxx . . $ __gnu_cxx/__normal_iterator#`operator->`(65c2ec7035a77d3e).sfcxx . . $ SSDescriptor#sf., tmp_size))
430 {
431 printf("Error reading chunk: %s\n", section->cxx . . $ __gnu_cxx/__normal_iterator#`operator->`(65c2ec7035a77d3e).namecxx . . $ SSDescriptor#name.);
432 return(0);
433 }
434 found = 1;
435 break;
436 }
437 else
438 {
439 if(smem_seekcxx . . $ smem_seek(bece8f0ac61bc7de).(st, tmp_size, SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!) < 0)
440 {
441 puts("Chunk seek failure");
442 return(0);
443 }
444 }
445 }
446 if(smem_seekcxx . . $ smem_seek(bece8f0ac61bc7de).(st, -total, SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!) < 0)
447 {
448 puts("Reverse seek error");
449 return(0);
450 }
451 if(!found && !section->cxx . . $ __gnu_cxx/__normal_iterator#`operator->`(65c2ec7035a77d3e).optionalcxx . . $ SSDescriptor#optional.) // Not found. We are sad!
452 {
453 printf("Section missing: %.32s\n", section->cxx . . $ __gnu_cxx/__normal_iterator#`operator->`(65c2ec7035a77d3e).namecxx . . $ SSDescriptor#name.);
454 return(0);
455 }
456 }
457 }
458 }
459 else
460 {
461 for(section =cxx . . $ __gnu_cxx/__normal_iterator#`operator=`(9050870c3bc69589). sections.begincxx . . $ std/vector#begin(55a6de06420d4903).(); section !=cxx . . $ __gnu_cxx/`operator!=`(30ee64f2df4eef8b). sections.endcxx . . $ std/vector#end(55a6de06420d4903).(); section++cxx . . $ __gnu_cxx/__normal_iterator#operator++(6cefca3db4774be2).)
462 {
463 if(!WriteStateChunkcxx . . $ WriteStateChunk(3cb24b6ce25cfb57).(st, section->cxx . . $ __gnu_cxx/__normal_iterator#`operator->`(65c2ec7035a77d3e).namecxx . . $ SSDescriptor#name., section->cxx . . $ __gnu_cxx/__normal_iterator#`operator->`(65c2ec7035a77d3e).sfcxx . . $ SSDescriptor#sf.))
464 return(0);
465 }
466 }
467
468 return(1);
469 }
470
471 int MDFNSS_StateActioncxx . . $ MDFNSS_StateAction(3f628545d3b0df83).(void *st_p, int load, int data_only, SFORMATcxx . . $ SFORMAT# *sf, const char *name, bool optional)
472 {
473 StateMemcxx . . $ StateMem# *st = (StateMemcxx . . $ StateMem#*)st_p;
474 stdcxx . . $ std/::vectorcxx . . $ std/vector# <SSDescriptorcxx . . $ SSDescriptor#> lovecxx . . $ std/vector#vector(49f6e7a06ebc5aa8).;
475
476 love.push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(SSDescriptorcxx . . $ SSDescriptor#SSDescriptor(3e11acb8200f8958).(sf, name, optional));
477 return(MDFNSS_StateActioncxx . . $ MDFNSS_StateAction(eba9425d234c8c23).(st, load, 0, love));
478 }
479
480 int MDFNSS_SaveSMcxx . . $ MDFNSS_SaveSM(d855d810c36bab0b).(void *st_p, int, int, const void*, const void*, const void*)
481 {
482 StateMemcxx . . $ StateMem# *st = (StateMemcxx . . $ StateMem#*)st_p;
483 static const char *header_magic = "MDFNSVST";
484 uint8cxx . . $ uint8# header[32];
485 int neowidth = 0, neoheight = 0;
486
487 memset(header, 0, sizeof(header));
488 memcpy(header, header_magic, 8);
489
490 MDFN_en32lsb(header + 16, MEDNAFEN_VERSION_NUMERICcxx . . $ `<built-in>:492:9`!);
491 MDFN_en32lsb(header + 24, neowidth);
492 MDFN_en32lsb(header + 28, neoheight);
493 smem_writecxx . . $ smem_write(f3982eecb66031e).(st, header, 32);
494
495 if(!MDFNGameInfocxx . . $ MDFNGameInfo.->StateActioncxx . . $ $anonymous_type_643ef407c229d02_12#StateAction.(st, 0, 0))
496 return(0);
497
498 uint32cxx . . $ uint32# sizy = smem_tellcxx . . $ smem_tell(11cbcd7e5e652dd0).(st);
499 smem_seekcxx . . $ smem_seek(bece8f0ac61bc7de).(st, 16 + 4, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!);
500 smem_write32lecxx . . $ smem_write32le(776f7c4058af06c3).(st, sizy);
501
502 return(1);
503 }
504
505 int MDFNSS_LoadSMcxx . . $ MDFNSS_LoadSM(4d015e8c0eefb98e).(void *st_p, int, int)
506 {
507 StateMemcxx . . $ StateMem# *st = (StateMemcxx . . $ StateMem#*)st_p;
508 uint8cxx . . $ uint8# header[32];
509 uint32cxx . . $ uint32# stateversion;
510
511 smem_readcxx . . $ smem_read(f3982eecb66031e).(st, header, 32);
512
513 if(memcmp(header, "MEDNAFENSVESTATE", 16) && memcmp(header, "MDFNSVST", 8))
514 return(0);
515
516 stateversion = MDFN_de32lsb(header + 16);
517
518 return(MDFNGameInfocxx . . $ MDFNGameInfo.->StateActioncxx . . $ $anonymous_type_643ef407c229d02_12#StateAction.(st, stateversion, 0));
519 }