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"
19
20 #include <string.h>
21
22 #include <boolean.h>
23
24 #include "driver.h"
25 #include "general.h"
26 #include "state.h"
27
28 #define RLSB MDFNSTATE_RLSB //0x80000000
29
30 int32 smem_read(StateMem *st, void *buffer, uint32 len)
31 {
32 if ((len + st->loc) > st->len)
33 return 0;
34
35 memcpy(buffer, st->data + st->loc, len);
36 st->loc += len;
37
38 return(len);
39 }
40
41 int32 smem_write(StateMem *st, void *buffer, uint32 len)
42 {
43 if ((len + st->loc) > st->malloced)
44 {
45 uint32 newsize = (st->malloced >= 32768) ? st->malloced : (st->initial_malloc ? st->initial_malloc : 32768);
46
47 while(newsize < (len + st->loc))
48 newsize *= 2;
49 st->data = (uint8 *)realloc(st->data, newsize);
50 st->malloced = newsize;
51 }
52 memcpy(st->data + st->loc, buffer, len);
53 st->loc += len;
54
55 if (st->loc > st->len)
56 st->len = st->loc;
57
58 return(len);
59 }
60
61 int32 smem_putc(StateMem *st, int value)
62 {
63 uint8 tmpval = value;
64 if(smem_write(st, &tmpval, 1) != 1)
65 return(-1);
66 return(1);
67 }
68
69 int32 smem_tell(StateMem *st)
70 {
71 return(st->loc);
72 }
73
74 int32 smem_seek(StateMem *st, uint32 offset, int whence)
75 {
76 switch(whence)
77 {
78 case SEEK_SET: st->loc = offset; break;
79 case SEEK_END: st->loc = st->len - offset; break;
80 case SEEK_CUR: st->loc += offset; break;
81 }
82
83 if(st->loc > st->len)
84 {
85 st->loc = st->len;
86 return(-1);
87 }
88
89 if(st->loc < 0)
90 {
91 st->loc = 0;
92 return(-1);
93 }
94
95 return(0);
96 }
97
98 int smem_write32le(StateMem *st, uint32 b)
99 {
100 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_write(st, s, 4)<4)?0:4);
106 }
107
108 int smem_read32le(StateMem *st, uint32 *b)
109 {
110 uint8 s[4];
111
112 if(smem_read(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 SubWrite(StateMem *st, SFORMAT *sf, const char *name_prefix = NULL)
121 {
122 while(sf->size || sf->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->size || !sf->v)
125 {
126 sf++;
127 continue;
128 }
129
130 if(sf->size == (uint32)~0) /* Link to another struct. */
131 {
132 if(!SubWrite(st, (SFORMAT *)sf->v, name_prefix))
133 return(0);
134
135 sf++;
136 continue;
137 }
138
139 int32 bytesize = sf->size;
140
141 char nameo[1 + 256];
142 int slen;
143
144 slen = snprintf(nameo + 1, 256, "%s%s", name_prefix ? name_prefix : "", sf->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->name, name_prefix, nameo, slen);
150 slen = 255;
151 }
152
153 smem_write(st, nameo, 1 + nameo[0]);
154 smem_write32le(st, bytesize);
155
156 /* Flip the byte order... */
157 if(sf->flags & MDFNSTATE_BOOL)
158 {
159
160 }
161 else if(sf->flags & MDFNSTATE_RLSB64)
162 Endian_A64_Swap(sf->v, bytesize / sizeof(uint64));
163 else if(sf->flags & MDFNSTATE_RLSB32)
164 Endian_A32_Swap(sf->v, bytesize / sizeof(uint32));
165 else if(sf->flags & MDFNSTATE_RLSB16)
166 Endian_A16_Swap(sf->v, bytesize / sizeof(uint16));
167 else if(sf->flags & RLSB)
168 FlipByteOrder((uint8_t*)sf->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->flags & MDFNSTATE_BOOL)
173 {
174 for(int32 bool_monster = 0; bool_monster < bytesize; bool_monster++)
175 {
176 uint8 tmp_bool = ((bool *)sf->v)[bool_monster];
177 //printf("Bool write: %.31s\n", sf->name);
178 smem_write(st, &tmp_bool, 1);
179 }
180 }
181 else
182 smem_write(st, (uint8 *)sf->v, bytesize);
183
184 /* Now restore the original byte order. */
185 if(sf->flags & MDFNSTATE_BOOL)
186 {
187
188 }
189 else if(sf->flags & MDFNSTATE_RLSB64)
190 Endian_A64_LE_to_NE(sf->v, bytesize / sizeof(uint64));
191 else if(sf->flags & MDFNSTATE_RLSB32)
192 Endian_A32_LE_to_NE(sf->v, bytesize / sizeof(uint32));
193 else if(sf->flags & MDFNSTATE_RLSB16)
194 Endian_A16_LE_to_NE(sf->v, bytesize / sizeof(uint16));
195 else if(sf->flags & RLSB)
196 FlipByteOrder((uint8_t*)sf->v, bytesize);
197
198 sf++;
199 }
200
201 return(true);
202 }
203
204 static int WriteStateChunk(StateMem *st, const char *sname, SFORMAT *sf)
205 {
206 int32 data_start_pos;
207 int32 end_pos;
208
209 uint8 sname_tmp[32];
210
211 memset(sname_tmp, 0, sizeof(sname_tmp));
212 strncpy((char *)sname_tmp, sname, 32);
213
214 if(strlen(sname) > 32)
215 printf("Warning: section name is too long: %s\n", sname);
216
217 smem_write(st, sname_tmp, 32);
218
219 smem_write32le(st, 0); // We'll come back and write this later.
220
221 data_start_pos = smem_tell(st);
222
223 if(!SubWrite(st, sf))
224 return(0);
225
226 end_pos = smem_tell(st);
227
228 smem_seek(st, data_start_pos - 4, SEEK_SET);
229 smem_write32le(st, end_pos - data_start_pos);
230 smem_seek(st, end_pos, SEEK_SET);
231
232 return(end_pos - data_start_pos);
233 }
234
235 struct compare_cstr
236 {
237 bool operator()(const char *s1, const char *s2) const
238 {
239 return(strcmp(s1, s2) < 0);
240 }
241 };
242
243 typedef std::map<const char *, SFORMAT *, compare_cstr> SFMap_t;
244
245 static void MakeSFMap(SFORMAT *sf, SFMap_t &sfmap)
246 {
247 while(sf->size || sf->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.
248 {
249 if(!sf->size || !sf->v)
250 {
251 sf++;
252 continue;
253 }
254
255 if(sf->size == (uint32)~0) /* Link to another SFORMAT structure. */
256 MakeSFMap((SFORMAT *)sf->v, sfmap);
257 else
258 {
259 assert(sf->name);
260
261 if(sfmap.find(sf->name) != sfmap.end())
262 printf("Duplicate save state variable in internal emulator structures(CLUB THE PROGRAMMERS WITH BREADSTICKS): %s\n", sf->name);
263
264 sfmap[sf->name] = sf;
265 }
266
267 sf++;
268 }
269 }
270
271 // Fast raw chunk reader
272 static void DOReadChunk(StateMem *st, SFORMAT *sf)
273 {
274 while(sf->size || sf->name) // Size can sometimes be zero, so also check for the text name.
275 // These two should both be zero only at the end of a struct.
276 {
277 if(!sf->size || !sf->v)
278 {
279 sf++;
280 continue;
281 }
282
283 if(sf->size == (uint32) ~0) // Link to another SFORMAT struct
284 {
285 DOReadChunk(st, (SFORMAT *)sf->v);
286 sf++;
287 continue;
288 }
289
290 int32 bytesize = sf->size;
291
292 // Loading raw data, bool types are stored as they appear in memory, not as single bytes in the full state format.
293 // In the SFORMAT structure, the size member for bool entries is the number of bool elements, not the total in-memory size,
294 // so we adjust it here.
295 if(sf->flags & MDFNSTATE_BOOL)
296 bytesize *= sizeof(bool);
297
298 smem_read(st, (uint8 *)sf->v, bytesize);
299 sf++;
300 }
301 }
302
303 static int ReadStateChunk(StateMem *st, SFORMAT *sf, int size)
304 {
305 int temp;
306
307 {
308 SFMap_t sfmap;
309 SFMap_t sfmap_found; // Used for identifying variables that are missing in the save state.
310
311 MakeSFMap(sf, sfmap);
312
313 temp = smem_tell(st);
314 while(smem_tell(st) < (temp + size))
315 {
316 uint32 recorded_size; // In bytes
317 uint8 toa[1 + 256]; // Don't change to char unless cast toa[0] to unsigned to smem_read() and other places.
318
319 if(smem_read(st, toa, 1) != 1)
320 {
321 puts("Unexpected EOF");
322 return(0);
323 }
324
325 if(smem_read(st, toa + 1, toa[0]) != toa[0])
326 {
327 puts("Unexpected EOF?");
328 return 0;
329 }
330
331 toa[1 + toa[0]] = 0;
332
333 smem_read32le(st, &recorded_size);
334
335 SFMap_t::iterator sfmit;
336
337 sfmit = sfmap.find((char *)toa + 1);
338
339 if(sfmit != sfmap.end())
340 {
341 SFORMAT *tmp = sfmit->second;
342 uint32 expected_size = tmp->size; // In bytes
343
344 if(recorded_size != expected_size)
345 {
346 printf("Variable in save state wrong size: %s. Need: %d, got: %d\n", toa + 1, expected_size, recorded_size);
347 if(smem_seek(st, recorded_size, SEEK_CUR) < 0)
348 {
349 puts("Seek error");
350 return(0);
351 }
352 }
353 else
354 {
355 sfmap_found[tmp->name] = tmp;
356
357 smem_read(st, (uint8 *)tmp->v, expected_size);
358
359 if(tmp->flags & MDFNSTATE_BOOL)
360 {
361 // Converting downwards is necessary for the case of sizeof(bool) > 1
362 for(int32 bool_monster = expected_size - 1; bool_monster >= 0; bool_monster--)
363 {
364 ((bool *)tmp->v)[bool_monster] = ((uint8 *)tmp->v)[bool_monster];
365 }
366 }
367
368 if(tmp->flags & MDFNSTATE_RLSB64)
369 Endian_A64_LE_to_NE(tmp->v, expected_size / sizeof(uint64));
370 else if(tmp->flags & MDFNSTATE_RLSB32)
371 Endian_A32_LE_to_NE(tmp->v, expected_size / sizeof(uint32));
372 else if(tmp->flags & MDFNSTATE_RLSB16)
373 Endian_A16_LE_to_NE(tmp->v, expected_size / sizeof(uint16));
374 else if(tmp->flags & RLSB)
375 FlipByteOrder((uint8_t*)tmp->v, expected_size);
376 }
377 }
378 else
379 {
380 printf("Unknown variable in save state: %s\n", toa + 1);
381 if(smem_seek(st, recorded_size, SEEK_CUR) < 0)
382 {
383 puts("Seek error");
384 return(0);
385 }
386 }
387 } // while(...)
388
389 for(SFMap_t::const_iterator it = sfmap.begin(); it != sfmap.end(); it++)
390 {
391 if(sfmap_found.find(it->second->name) == sfmap_found.end())
392 {
393 printf("Variable missing from save state: %s\n", it->second->name);
394 }
395 }
396
397 assert(smem_tell(st) == (temp + size));
398 }
399 return 1;
400 }
401
402 static int CurrentState = 0;
403
404 /* This function is called by the game driver(NES, GB, GBA) to save a state. */
405 int MDFNSS_StateAction(void *st_p, int load, int data_only, std::vector <SSDescriptor> &sections)
406 {
407 StateMem *st = (StateMem*)st_p;
408 std::vector<SSDescriptor>::iterator section;
409
410 if(load)
411 {
412 {
413 char sname[32];
414
415 for(section = sections.begin(); section != sections.end(); section++)
416 {
417 int found = 0;
418 uint32 tmp_size;
419 uint32 total = 0;
420
421 while(smem_read(st, (uint8 *)sname, 32) == 32)
422 {
423 if(smem_read32le(st, &tmp_size) != 4)
424 return(0);
425
426 total += tmp_size + 32 + 4;
427
428 // Yay, we found the section
429 if(!strncmp(sname, section->name, 32))
430 {
431 if(!ReadStateChunk(st, section->sf, tmp_size))
432 {
433 printf("Error reading chunk: %s\n", section->name);
434 return(0);
435 }
436 found = 1;
437 break;
438 }
439 else
440 {
441 if(smem_seek(st, tmp_size, SEEK_CUR) < 0)
442 {
443 puts("Chunk seek failure");
444 return(0);
445 }
446 }
447 }
448 if(smem_seek(st, -total, SEEK_CUR) < 0)
449 {
450 puts("Reverse seek error");
451 return(0);
452 }
453 if(!found && !section->optional) // Not found. We are sad!
454 {
455 printf("Section missing: %.32s\n", section->name);
456 return(0);
457 }
458 }
459 }
460 }
461 else
462 {
463 for(section = sections.begin(); section != sections.end(); section++)
464 {
465 if(!WriteStateChunk(st, section->name, section->sf))
466 return(0);
467 }
468 }
469
470 return(1);
471 }
472
473 int MDFNSS_StateAction(void *st_p, int load, int data_only, SFORMAT *sf, const char *name, bool optional)
474 {
475 StateMem *st = (StateMem*)st_p;
476 std::vector <SSDescriptor> love;
477
478 love.push_back(SSDescriptor(sf, name, optional));
479 return(MDFNSS_StateAction(st, load, 0, love));
480 }
481
482 int MDFNSS_SaveSM(void *st_p, int, int, const void*, const void*, const void*)
483 {
484 StateMem *st = (StateMem*)st_p;
485 static const char *header_magic = "MDFNSVST";
486 uint8 header[32];
487 int neowidth = 0, neoheight = 0;
488
489 memset(header, 0, sizeof(header));
490 memcpy(header, header_magic, 8);
491
492 MDFN_en32lsb(header + 16, MEDNAFEN_VERSION_NUMERIC);
493 MDFN_en32lsb(header + 24, neowidth);
494 MDFN_en32lsb(header + 28, neoheight);
495 smem_write(st, header, 32);
496
497 if(!MDFNGameInfo->StateAction(st, 0, 0))
498 return(0);
499
500 uint32 sizy = smem_tell(st);
501 smem_seek(st, 16 + 4, SEEK_SET);
502 smem_write32le(st, sizy);
503
504 return(1);
505 }
506
507 int MDFNSS_LoadSM(void *st_p, int, int)
508 {
509 StateMem *st = (StateMem*)st_p;
510 uint8 header[32];
511 uint32 stateversion;
512
513 smem_read(st, header, 32);
514
515 if(memcmp(header, "MEDNAFENSVESTATE", 16) && memcmp(header, "MDFNSVST", 8))
516 return(0);
517
518 stateversion = MDFN_de32lsb(header + 16);
519
520 return(MDFNGameInfo->StateAction(st, stateversion, 0));
521 }