| 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 <boolean.h>cxx . . $ `<file>/boolean.h`/ |
| 23 | |
| 24 | #include "driver.h"cxx . . $ `<file>/mednafen/driver.h`/ |
| 25 | #include "general.h"cxx . . $ `<file>/mednafen/general.h`/ |
| 26 | #include "state.h"cxx . . $ `<file>/mednafen/state.h`/ |
| 27 | |
| 28 | #define RLSBcxx . . $ `mednafen/state.cpp:28:9`! MDFNSTATE_RLSBcxx . . $ `mednafen/state.h:29: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 . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/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:40:9`!) |
| 158 | { |
| 159 | |
| 160 | } |
| 161 | else if(sf->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_RLSB64cxx . . $ `mednafen/state.h:38:9`!) |
| 162 | Endian_A64_Swap(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:32:9`!) |
| 164 | Endian_A32_Swap(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:35:9`!) |
| 166 | Endian_A16_Swap(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 | FlipByteOrder((uint8_tcxx . . $ uint8_t#*)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:40: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:40:9`!) |
| 186 | { |
| 187 | |
| 188 | } |
| 189 | else if(sf->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_RLSB64cxx . . $ `mednafen/state.h:38: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:32: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:35: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 | FlipByteOrder((uint8_tcxx . . $ uint8_t#*)sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., bytesize); |
| 197 | |
| 198 | sf++; |
| 199 | } |
| 200 | |
| 201 | return(true); |
| 202 | } |
| 203 | |
| 204 | static int WriteStateChunkcxx . . $ WriteStateChunk(3cb24b6ce25cfb57).(StateMemcxx . . $ StateMem# *st, const char *sname, SFORMATcxx . . $ SFORMAT# *sf) |
| 205 | { |
| 206 | int32cxx . . $ int32# data_start_pos; |
| 207 | int32cxx . . $ int32# end_pos; |
| 208 | |
| 209 | uint8cxx . . $ 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_writecxx . . $ smem_write(f3982eecb66031e).(st, sname_tmp, 32); |
| 218 | |
| 219 | smem_write32lecxx . . $ smem_write32le(776f7c4058af06c3).(st, 0); // We'll come back and write this later. |
| 220 | |
| 221 | data_start_pos = smem_tellcxx . . $ smem_tell(11cbcd7e5e652dd0).(st); |
| 222 | |
| 223 | if(!SubWritecxx . . $ SubWrite(ad14fc7377575df3).(st, sf)) |
| 224 | return(0); |
| 225 | |
| 226 | end_pos = smem_tellcxx . . $ smem_tell(11cbcd7e5e652dd0).(st); |
| 227 | |
| 228 | smem_seekcxx . . $ smem_seek(bece8f0ac61bc7de).(st, data_start_pos - 4, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 229 | smem_write32lecxx . . $ smem_write32le(776f7c4058af06c3).(st, end_pos - data_start_pos); |
| 230 | smem_seekcxx . . $ smem_seek(bece8f0ac61bc7de).(st, end_pos, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 231 | |
| 232 | return(end_pos - data_start_pos); |
| 233 | } |
| 234 | |
| 235 | struct compare_cstrcxx . . $ compare_cstr# |
| 236 | { |
| 237 | bool operatorcxx . . $ compare_cstr#`operator()`(f735519873e6c132).()(const char *s1, const char *s2) const |
| 238 | { |
| 239 | return(strcmp(s1, s2) < 0); |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | typedef stdcxx . . $ std/::mapcxx . . $ std/map#<const char *, SFORMATcxx . . $ SFORMAT# *, compare_cstrcxx . . $ compare_cstr#> SFMap_tcxx . . $ SFMap_t#; |
| 244 | |
| 245 | static void MakeSFMapcxx . . $ MakeSFMap(e04155f555d74b64).(SFORMATcxx . . $ SFORMAT# *sf, SFMap_tcxx . . $ SFMap_t# &sfmap) |
| 246 | { |
| 247 | 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. |
| 248 | { |
| 249 | if(!sf->sizecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#size. || !sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v.) |
| 250 | { |
| 251 | sf++; |
| 252 | continue; |
| 253 | } |
| 254 | |
| 255 | if(sf->sizecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#size. == (uint32cxx . . $ uint32#)~0) /* Link to another SFORMAT structure. */ |
| 256 | MakeSFMapcxx . . $ MakeSFMap(e04155f555d74b64).((SFORMATcxx . . $ SFORMAT# *)sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., sfmap); |
| 257 | else |
| 258 | { |
| 259 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(sf->name); |
| 260 | |
| 261 | if(sfmap.findcxx . . $ std/map#find(36f8d56706583706).(sf->namecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#name.) !=cxx . . $ std/`operator!=`(be6a71a859368080). sfmap.endcxx . . $ std/map#end(7e4de33083b6e738).()) |
| 262 | printf("Duplicate save state variable in internal emulator structures(CLUB THE PROGRAMMERS WITH BREADSTICKS): %s\n", sf->namecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#name.); |
| 263 | |
| 264 | sfmap[cxx . . $ std/map#`operator[]`(bbd415b616967878).sf->namecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#name.] = sf; |
| 265 | } |
| 266 | |
| 267 | sf++; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // Fast raw chunk reader |
| 272 | static void DOReadChunkcxx . . $ DOReadChunk(4245d66670007096).(StateMemcxx . . $ StateMem# *st, SFORMATcxx . . $ SFORMAT# *sf) |
| 273 | { |
| 274 | 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. |
| 275 | // These two should both be zero only at the end of a struct. |
| 276 | { |
| 277 | if(!sf->sizecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#size. || !sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v.) |
| 278 | { |
| 279 | sf++; |
| 280 | continue; |
| 281 | } |
| 282 | |
| 283 | if(sf->sizecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#size. == (uint32cxx . . $ uint32#) ~0) // Link to another SFORMAT struct |
| 284 | { |
| 285 | DOReadChunkcxx . . $ DOReadChunk(4245d66670007096).(st, (SFORMATcxx . . $ SFORMAT# *)sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v.); |
| 286 | sf++; |
| 287 | continue; |
| 288 | } |
| 289 | |
| 290 | int32cxx . . $ int32# bytesize = sf->sizecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#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->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_BOOLcxx . . $ `mednafen/state.h:40:9`!) |
| 296 | bytesize *= sizeof(bool); |
| 297 | |
| 298 | smem_readcxx . . $ smem_read(f3982eecb66031e).(st, (uint8cxx . . $ uint8# *)sf->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., bytesize); |
| 299 | sf++; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | static int ReadStateChunkcxx . . $ ReadStateChunk(96770ba063a35803).(StateMemcxx . . $ StateMem# *st, SFORMATcxx . . $ SFORMAT# *sf, int size) |
| 304 | { |
| 305 | int temp; |
| 306 | |
| 307 | { |
| 308 | SFMap_tcxx . . $ SFMap_t# sfmapcxx . . $ std/map#map(49f6e7a06ebc5aa8).; |
| 309 | SFMap_tcxx . . $ SFMap_t# sfmap_foundcxx . . $ std/map#map(49f6e7a06ebc5aa8).; // Used for identifying variables that are missing in the save state. |
| 310 | |
| 311 | MakeSFMapcxx . . $ MakeSFMap(e04155f555d74b64).(sf, sfmap); |
| 312 | |
| 313 | temp = smem_tellcxx . . $ smem_tell(11cbcd7e5e652dd0).(st); |
| 314 | while(smem_tellcxx . . $ smem_tell(11cbcd7e5e652dd0).(st) < (temp + size)) |
| 315 | { |
| 316 | uint32cxx . . $ uint32# recorded_size; // In bytes |
| 317 | uint8cxx . . $ 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_readcxx . . $ smem_read(f3982eecb66031e).(st, toa, 1) != 1) |
| 320 | { |
| 321 | puts("Unexpected EOF"); |
| 322 | return(0); |
| 323 | } |
| 324 | |
| 325 | if(smem_readcxx . . $ smem_read(f3982eecb66031e).(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_read32lecxx . . $ smem_read32le(24a07498b85603a0).(st, &recorded_size); |
| 334 | |
| 335 | SFMap_tcxx . . $ SFMap_t#::iteratorcxx . . $ std/map#iterator# sfmitcxx . . $ std/_Rb_tree_iterator#_Rb_tree_iterator(ced63f7c635d850d).; |
| 336 | |
| 337 | sfmit =cxx . . $ std/_Rb_tree_iterator#`operator=`(3286246de5d22236). sfmap.findcxx . . $ std/map#find(36f8d56706583706).((char *)toa + 1); |
| 338 | |
| 339 | if(sfmit !=cxx . . $ std/`operator!=`(be6a71a859368080). sfmap.endcxx . . $ std/map#end(7e4de33083b6e738).()) |
| 340 | { |
| 341 | SFORMATcxx . . $ SFORMAT# *tmp = sfmit->cxx . . $ std/_Rb_tree_iterator#`operator->`(34d6acfd341ba7ab).secondcxx . . $ std/pair#second.; |
| 342 | uint32cxx . . $ uint32# expected_size = tmp->sizecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#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_seekcxx . . $ smem_seek(bece8f0ac61bc7de).(st, recorded_size, SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!) < 0) |
| 348 | { |
| 349 | puts("Seek error"); |
| 350 | return(0); |
| 351 | } |
| 352 | } |
| 353 | else |
| 354 | { |
| 355 | sfmap_found[cxx . . $ std/map#`operator[]`(bbd415b616967878).tmp->namecxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#name.] = tmp; |
| 356 | |
| 357 | smem_readcxx . . $ smem_read(f3982eecb66031e).(st, (uint8cxx . . $ uint8# *)tmp->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., expected_size); |
| 358 | |
| 359 | if(tmp->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_BOOLcxx . . $ `mednafen/state.h:40:9`!) |
| 360 | { |
| 361 | // Converting downwards is necessary for the case of sizeof(bool) > 1 |
| 362 | for(int32cxx . . $ int32# bool_monster = expected_size - 1; bool_monster >= 0; bool_monster--) |
| 363 | { |
| 364 | ((bool *)tmp->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v.)[bool_monster] = ((uint8cxx . . $ uint8# *)tmp->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v.)[bool_monster]; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | if(tmp->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_RLSB64cxx . . $ `mednafen/state.h:38:9`!) |
| 369 | Endian_A64_LE_to_NE(tmp->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., expected_size / sizeof(uint64cxx . . $ uint64#)); |
| 370 | else if(tmp->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_RLSB32cxx . . $ `mednafen/state.h:32:9`!) |
| 371 | Endian_A32_LE_to_NE(tmp->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., expected_size / sizeof(uint32cxx . . $ uint32#)); |
| 372 | else if(tmp->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & MDFNSTATE_RLSB16cxx . . $ `mednafen/state.h:35:9`!) |
| 373 | Endian_A16_LE_to_NE(tmp->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., expected_size / sizeof(uint16cxx . . $ uint16#)); |
| 374 | else if(tmp->flagscxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#flags. & RLSBcxx . . $ `mednafen/state.cpp:28:9`!) |
| 375 | FlipByteOrder((uint8_tcxx . . $ uint8_t#*)tmp->vcxx . . $ $anonymous_type_8d6ca9fa7c8baca5_1#v., expected_size); |
| 376 | } |
| 377 | } |
| 378 | else |
| 379 | { |
| 380 | printf("Unknown variable in save state: %s\n", toa + 1); |
| 381 | if(smem_seekcxx . . $ smem_seek(bece8f0ac61bc7de).(st, recorded_size, SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!) < 0) |
| 382 | { |
| 383 | puts("Seek error"); |
| 384 | return(0); |
| 385 | } |
| 386 | } |
| 387 | } // while(...) |
| 388 | |
| 389 | 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).) |
| 390 | { |
| 391 | 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).()) |
| 392 | { |
| 393 | 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.); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(smem_tell(st) == (temp + size)); |
| 398 | } |
| 399 | return 1; |
| 400 | } |
| 401 | |
| 402 | static int CurrentStatecxx . . $ CurrentState. = 0; |
| 403 | |
| 404 | /* This function is called by the game driver(NES, GB, GBA) to save a state. */ |
| 405 | int MDFNSS_StateActioncxx . . $ MDFNSS_StateAction(eba9425d234c8c23).(void *st_p, int load, int data_only, stdcxx . . $ std/::vectorcxx . . $ std/vector# <SSDescriptorcxx . . $ SSDescriptor#> §ions) |
| 406 | { |
| 407 | StateMemcxx . . $ StateMem# *st = (StateMemcxx . . $ StateMem#*)st_p; |
| 408 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<SSDescriptor>::iteratorcxx . . $ std/vector#iterator# sectioncxx . . $ __gnu_cxx/__normal_iterator#__normal_iterator(ced63f7c635d850d).; |
| 409 | |
| 410 | if(load) |
| 411 | { |
| 412 | { |
| 413 | char sname[32]; |
| 414 | |
| 415 | 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).) |
| 416 | { |
| 417 | int found = 0; |
| 418 | uint32cxx . . $ uint32# tmp_size; |
| 419 | uint32cxx . . $ uint32# total = 0; |
| 420 | |
| 421 | while(smem_readcxx . . $ smem_read(f3982eecb66031e).(st, (uint8cxx . . $ uint8# *)sname, 32) == 32) |
| 422 | { |
| 423 | if(smem_read32lecxx . . $ smem_read32le(24a07498b85603a0).(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->cxx . . $ __gnu_cxx/__normal_iterator#`operator->`(65c2ec7035a77d3e).namecxx . . $ SSDescriptor#name., 32)) |
| 430 | { |
| 431 | if(!ReadStateChunkcxx . . $ ReadStateChunk(96770ba063a35803).(st, section->cxx . . $ __gnu_cxx/__normal_iterator#`operator->`(65c2ec7035a77d3e).sfcxx . . $ SSDescriptor#sf., tmp_size)) |
| 432 | { |
| 433 | printf("Error reading chunk: %s\n", section->cxx . . $ __gnu_cxx/__normal_iterator#`operator->`(65c2ec7035a77d3e).namecxx . . $ SSDescriptor#name.); |
| 434 | return(0); |
| 435 | } |
| 436 | found = 1; |
| 437 | break; |
| 438 | } |
| 439 | else |
| 440 | { |
| 441 | if(smem_seekcxx . . $ smem_seek(bece8f0ac61bc7de).(st, tmp_size, SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!) < 0) |
| 442 | { |
| 443 | puts("Chunk seek failure"); |
| 444 | return(0); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | if(smem_seekcxx . . $ smem_seek(bece8f0ac61bc7de).(st, -total, SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!) < 0) |
| 449 | { |
| 450 | puts("Reverse seek error"); |
| 451 | return(0); |
| 452 | } |
| 453 | if(!found && !section->cxx . . $ __gnu_cxx/__normal_iterator#`operator->`(65c2ec7035a77d3e).optionalcxx . . $ SSDescriptor#optional.) // Not found. We are sad! |
| 454 | { |
| 455 | printf("Section missing: %.32s\n", section->cxx . . $ __gnu_cxx/__normal_iterator#`operator->`(65c2ec7035a77d3e).namecxx . . $ SSDescriptor#name.); |
| 456 | return(0); |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | else |
| 462 | { |
| 463 | 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).) |
| 464 | { |
| 465 | 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.)) |
| 466 | return(0); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | return(1); |
| 471 | } |
| 472 | |
| 473 | int MDFNSS_StateActioncxx . . $ MDFNSS_StateAction(3f628545d3b0df83).(void *st_p, int load, int data_only, SFORMATcxx . . $ SFORMAT# *sf, const char *name, bool optional) |
| 474 | { |
| 475 | StateMemcxx . . $ StateMem# *st = (StateMemcxx . . $ StateMem#*)st_p; |
| 476 | stdcxx . . $ std/::vectorcxx . . $ std/vector# <SSDescriptorcxx . . $ SSDescriptor#> lovecxx . . $ std/vector#vector(49f6e7a06ebc5aa8).; |
| 477 | |
| 478 | love.push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(SSDescriptorcxx . . $ SSDescriptor#SSDescriptor(3e11acb8200f8958).(sf, name, optional)); |
| 479 | return(MDFNSS_StateActioncxx . . $ MDFNSS_StateAction(eba9425d234c8c23).(st, load, 0, love)); |
| 480 | } |
| 481 | |
| 482 | int MDFNSS_SaveSMcxx . . $ MDFNSS_SaveSM(d855d810c36bab0b).(void *st_p, int, int, const void*, const void*, const void*) |
| 483 | { |
| 484 | StateMemcxx . . $ StateMem# *st = (StateMemcxx . . $ StateMem#*)st_p; |
| 485 | static const char *header_magic = "MDFNSVST"; |
| 486 | uint8cxx . . $ 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_NUMERICcxx . . $ `<built-in>:487:9`!); |
| 493 | MDFN_en32lsb(header + 24, neowidth); |
| 494 | MDFN_en32lsb(header + 28, neoheight); |
| 495 | smem_writecxx . . $ smem_write(f3982eecb66031e).(st, header, 32); |
| 496 | |
| 497 | if(!MDFNGameInfocxx . . $ MDFNGameInfo.->StateActioncxx . . $ $anonymous_type_643ef407c229d02_12#StateAction.(st, 0, 0)) |
| 498 | return(0); |
| 499 | |
| 500 | uint32cxx . . $ uint32# sizy = smem_tellcxx . . $ smem_tell(11cbcd7e5e652dd0).(st); |
| 501 | smem_seekcxx . . $ smem_seek(bece8f0ac61bc7de).(st, 16 + 4, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 502 | smem_write32lecxx . . $ smem_write32le(776f7c4058af06c3).(st, sizy); |
| 503 | |
| 504 | return(1); |
| 505 | } |
| 506 | |
| 507 | int MDFNSS_LoadSMcxx . . $ MDFNSS_LoadSM(4d015e8c0eefb98e).(void *st_p, int, int) |
| 508 | { |
| 509 | StateMemcxx . . $ StateMem# *st = (StateMemcxx . . $ StateMem#*)st_p; |
| 510 | uint8cxx . . $ uint8# header[32]; |
| 511 | uint32cxx . . $ uint32# stateversion; |
| 512 | |
| 513 | smem_readcxx . . $ smem_read(f3982eecb66031e).(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(MDFNGameInfocxx . . $ MDFNGameInfo.->StateActioncxx . . $ $anonymous_type_643ef407c229d02_12#StateAction.(st, stateversion, 0)); |
| 521 | } |