| 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 "driver.h" |
| 23 | #include "general.h" |
| 24 | #include "state.h" |
| 25 | #include "video.h" |
| 26 | #include "msvc_compat.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_NE_to_LE(sf->v, bytesize / sizeof(uint64)); |
| 163 | else if(sf->flags & MDFNSTATE_RLSB32) |
| 164 | Endian_A32_NE_to_LE(sf->v, bytesize / sizeof(uint32)); |
| 165 | else if(sf->flags & MDFNSTATE_RLSB16) |
| 166 | Endian_A16_NE_to_LE(sf->v, bytesize / sizeof(uint16)); |
| 167 | else if(sf->flags & RLSB) |
| 168 | Endian_V_NE_to_LE(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 | Endian_V_LE_to_NE(sf->v, bytesize); |
| 197 | sf++; |
| 198 | } |
| 199 | |
| 200 | return(TRUE); |
| 201 | } |
| 202 | |
| 203 | static int WriteStateChunk(StateMem *st, const char *sname, SFORMAT *sf) |
| 204 | { |
| 205 | int32 data_start_pos; |
| 206 | int32 end_pos; |
| 207 | |
| 208 | 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_write(st, sname_tmp, 32); |
| 217 | |
| 218 | smem_write32le(st, 0); // We'll come back and write this later. |
| 219 | |
| 220 | data_start_pos = smem_tell(st); |
| 221 | |
| 222 | if(!SubWrite(st, sf)) |
| 223 | return(0); |
| 224 | |
| 225 | end_pos = smem_tell(st); |
| 226 | |
| 227 | smem_seek(st, data_start_pos - 4, SEEK_SET); |
| 228 | smem_write32le(st, end_pos - data_start_pos); |
| 229 | smem_seek(st, end_pos, SEEK_SET); |
| 230 | |
| 231 | return(end_pos - data_start_pos); |
| 232 | } |
| 233 | |
| 234 | struct compare_cstr |
| 235 | { |
| 236 | bool operator()(const char *s1, const char *s2) const |
| 237 | { |
| 238 | return(strcmp(s1, s2) < 0); |
| 239 | } |
| 240 | }; |
| 241 | |
| 242 | typedef std::map<const char *, SFORMAT *, compare_cstr> SFMap_t; |
| 243 | |
| 244 | static void MakeSFMap(SFORMAT *sf, SFMap_t &sfmap) |
| 245 | { |
| 246 | 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. |
| 247 | { |
| 248 | if(!sf->size || !sf->v) |
| 249 | { |
| 250 | sf++; |
| 251 | continue; |
| 252 | } |
| 253 | |
| 254 | if(sf->size == (uint32)~0) /* Link to another SFORMAT structure. */ |
| 255 | MakeSFMap((SFORMAT *)sf->v, sfmap); |
| 256 | else |
| 257 | { |
| 258 | assert(sf->name); |
| 259 | |
| 260 | if(sfmap.find(sf->name) != sfmap.end()) |
| 261 | printf("Duplicate save state variable in internal emulator structures(CLUB THE PROGRAMMERS WITH BREADSTICKS): %s\n", sf->name); |
| 262 | |
| 263 | sfmap[sf->name] = sf; |
| 264 | } |
| 265 | |
| 266 | sf++; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // Fast raw chunk reader |
| 271 | static void DOReadChunk(StateMem *st, SFORMAT *sf) |
| 272 | { |
| 273 | while(sf->size || sf->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->size || !sf->v) |
| 277 | { |
| 278 | sf++; |
| 279 | continue; |
| 280 | } |
| 281 | |
| 282 | if(sf->size == (uint32) ~0) // Link to another SFORMAT struct |
| 283 | { |
| 284 | DOReadChunk(st, (SFORMAT *)sf->v); |
| 285 | sf++; |
| 286 | continue; |
| 287 | } |
| 288 | |
| 289 | int32 bytesize = sf->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->flags & MDFNSTATE_BOOL) |
| 295 | bytesize *= sizeof(bool); |
| 296 | |
| 297 | smem_read(st, (uint8 *)sf->v, bytesize); |
| 298 | sf++; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | static int ReadStateChunk(StateMem *st, SFORMAT *sf, int size) |
| 303 | { |
| 304 | int temp; |
| 305 | |
| 306 | { |
| 307 | SFMap_t sfmap; |
| 308 | SFMap_t sfmap_found; // Used for identifying variables that are missing in the save state. |
| 309 | |
| 310 | MakeSFMap(sf, sfmap); |
| 311 | |
| 312 | temp = smem_tell(st); |
| 313 | while(smem_tell(st) < (temp + size)) |
| 314 | { |
| 315 | uint32 recorded_size; // In bytes |
| 316 | 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_read(st, toa, 1) != 1) |
| 319 | { |
| 320 | puts("Unexpected EOF"); |
| 321 | return(0); |
| 322 | } |
| 323 | |
| 324 | if(smem_read(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_read32le(st, &recorded_size); |
| 333 | |
| 334 | SFMap_t::iterator sfmit; |
| 335 | |
| 336 | sfmit = sfmap.find((char *)toa + 1); |
| 337 | |
| 338 | if(sfmit != sfmap.end()) |
| 339 | { |
| 340 | SFORMAT *tmp = sfmit->second; |
| 341 | uint32 expected_size = tmp->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_seek(st, recorded_size, SEEK_CUR) < 0) |
| 347 | { |
| 348 | puts("Seek error"); |
| 349 | return(0); |
| 350 | } |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | sfmap_found[tmp->name] = tmp; |
| 355 | |
| 356 | smem_read(st, (uint8 *)tmp->v, expected_size); |
| 357 | |
| 358 | if(tmp->flags & MDFNSTATE_BOOL) |
| 359 | { |
| 360 | // Converting downwards is necessary for the case of sizeof(bool) > 1 |
| 361 | for(int32 bool_monster = expected_size - 1; bool_monster >= 0; bool_monster--) |
| 362 | { |
| 363 | ((bool *)tmp->v)[bool_monster] = ((uint8 *)tmp->v)[bool_monster]; |
| 364 | } |
| 365 | } |
| 366 | if(tmp->flags & MDFNSTATE_RLSB64) |
| 367 | Endian_A64_LE_to_NE(tmp->v, expected_size / sizeof(uint64)); |
| 368 | else if(tmp->flags & MDFNSTATE_RLSB32) |
| 369 | Endian_A32_LE_to_NE(tmp->v, expected_size / sizeof(uint32)); |
| 370 | else if(tmp->flags & MDFNSTATE_RLSB16) |
| 371 | Endian_A16_LE_to_NE(tmp->v, expected_size / sizeof(uint16)); |
| 372 | else if(tmp->flags & RLSB) |
| 373 | Endian_V_LE_to_NE(tmp->v, expected_size); |
| 374 | } |
| 375 | } |
| 376 | else |
| 377 | { |
| 378 | printf("Unknown variable in save state: %s\n", toa + 1); |
| 379 | if(smem_seek(st, recorded_size, SEEK_CUR) < 0) |
| 380 | { |
| 381 | puts("Seek error"); |
| 382 | return(0); |
| 383 | } |
| 384 | } |
| 385 | } // while(...) |
| 386 | |
| 387 | for(SFMap_t::const_iterator it = sfmap.begin(); it != sfmap.end(); it++) |
| 388 | { |
| 389 | if(sfmap_found.find(it->second->name) == sfmap_found.end()) |
| 390 | { |
| 391 | printf("Variable missing from save state: %s\n", it->second->name); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | assert(smem_tell(st) == (temp + size)); |
| 396 | } |
| 397 | return 1; |
| 398 | } |
| 399 | |
| 400 | static int CurrentState = 0; |
| 401 | |
| 402 | /* This function is called by the game driver(NES, GB, GBA) to save a state. */ |
| 403 | int MDFNSS_StateAction(void *st_p, int load, int data_only, std::vector <SSDescriptor> §ions) |
| 404 | { |
| 405 | StateMem *st = (StateMem*)st_p; |
| 406 | std::vector<SSDescriptor>::iterator section; |
| 407 | |
| 408 | if(load) |
| 409 | { |
| 410 | { |
| 411 | char sname[32]; |
| 412 | |
| 413 | for(section = sections.begin(); section != sections.end(); section++) |
| 414 | { |
| 415 | int found = 0; |
| 416 | uint32 tmp_size; |
| 417 | uint32 total = 0; |
| 418 | |
| 419 | while(smem_read(st, (uint8 *)sname, 32) == 32) |
| 420 | { |
| 421 | if(smem_read32le(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->name, 32)) |
| 428 | { |
| 429 | if(!ReadStateChunk(st, section->sf, tmp_size)) |
| 430 | { |
| 431 | printf("Error reading chunk: %s\n", section->name); |
| 432 | return(0); |
| 433 | } |
| 434 | found = 1; |
| 435 | break; |
| 436 | } |
| 437 | else |
| 438 | { |
| 439 | if(smem_seek(st, tmp_size, SEEK_CUR) < 0) |
| 440 | { |
| 441 | puts("Chunk seek failure"); |
| 442 | return(0); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | if(smem_seek(st, -total, SEEK_CUR) < 0) |
| 447 | { |
| 448 | puts("Reverse seek error"); |
| 449 | return(0); |
| 450 | } |
| 451 | if(!found && !section->optional) // Not found. We are sad! |
| 452 | { |
| 453 | printf("Section missing: %.32s\n", section->name); |
| 454 | return(0); |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | else |
| 460 | { |
| 461 | for(section = sections.begin(); section != sections.end(); section++) |
| 462 | { |
| 463 | if(!WriteStateChunk(st, section->name, section->sf)) |
| 464 | return(0); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | return(1); |
| 469 | } |
| 470 | |
| 471 | int MDFNSS_StateAction(void *st_p, int load, int data_only, SFORMAT *sf, const char *name, bool optional) |
| 472 | { |
| 473 | StateMem *st = (StateMem*)st_p; |
| 474 | std::vector <SSDescriptor> love; |
| 475 | |
| 476 | love.push_back(SSDescriptor(sf, name, optional)); |
| 477 | return(MDFNSS_StateAction(st, load, 0, love)); |
| 478 | } |
| 479 | |
| 480 | int MDFNSS_SaveSM(void *st_p, int, int, const void*, const void*, const void*) |
| 481 | { |
| 482 | StateMem *st = (StateMem*)st_p; |
| 483 | static const char *header_magic = "MDFNSVST"; |
| 484 | 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_NUMERIC); |
| 491 | MDFN_en32lsb(header + 24, neowidth); |
| 492 | MDFN_en32lsb(header + 28, neoheight); |
| 493 | smem_write(st, header, 32); |
| 494 | |
| 495 | if(!MDFNGameInfo->StateAction(st, 0, 0)) |
| 496 | return(0); |
| 497 | |
| 498 | uint32 sizy = smem_tell(st); |
| 499 | smem_seek(st, 16 + 4, SEEK_SET); |
| 500 | smem_write32le(st, sizy); |
| 501 | |
| 502 | return(1); |
| 503 | } |
| 504 | |
| 505 | int MDFNSS_LoadSM(void *st_p, int, int) |
| 506 | { |
| 507 | StateMem *st = (StateMem*)st_p; |
| 508 | uint8 header[32]; |
| 509 | uint32 stateversion; |
| 510 | |
| 511 | smem_read(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(MDFNGameInfo->StateAction(st, stateversion, 0)); |
| 519 | } |