int MDFNSS_StateAction(void* st_p, int load, SFORMAT* sf, const char* name) { StateMem* st = (StateMem*)st_p; if (load) { char sname[32]; int found = 0; uint32 tmp_size; uint32 total = 0; while (smem_read(st, (uint8*)sname, 32) == 32) { if (smem_read32le(st, &tmp_size) != 4) return (0); total += tmp_size + 32 + 4; // Yay, we found the section if (!strncmp(sname, name, 32)) { if (!ReadStateChunk(st, sf, tmp_size)) { printf("Error reading chunk: %s\n", name); return (0); } found = 1; break; } else { if (smem_seek(st, tmp_size, SEEK_CUR) < 0) { puts("Chunk seek failure"); return (0); } } } if (smem_seek(st, -total, SEEK_CUR) < 0) { puts("Reverse seek error"); return (0); } if (!found) // Not found. We are sad! { printf("Section missing: %.32s\n", name); return (0); } } else { if (!WriteStateChunk(st, name, sf)) return (0); } return (1); }
/* This function is called by the game driver(NES, GB, GBA) to save a state. */ static int MDFNSS_StateAction_internal(void *st_p, int load, int data_only, SSDescriptor *section) { StateMem *st = (StateMem*)st_p; if(load) { char sname[32]; int found = 0; uint32_t tmp_size; uint32_t total = 0; while(smem_read(st, (uint8_t *)sname, 32) == 32) { if(smem_read32le(st, &tmp_size) != 4) return(0); total += tmp_size + 32 + 4; // Yay, we found the section if(!strncmp(sname, section->name, 32)) { if(!ReadStateChunk(st, section->sf, tmp_size)) { printf("Error reading chunk: %s\n", section->name); return(0); } found = 1; break; } else { if(smem_seek(st, tmp_size, SEEK_CUR) < 0) { puts("Chunk seek failure"); return(0); } } } if(smem_seek(st, -total, SEEK_CUR) < 0) { puts("Reverse seek error"); return(0); } if(!found && !section->optional) // Not found. We are sad! { printf("Section missing: %.32s\n", section->name); return(0); } } else { if(!WriteStateChunk(st, section->name, section->sf)) return(0); } return(1); }
static int ReadStateChunk(StateMem *st, SFORMAT *sf, int size, int data_only) { SFORMAT *tmp; int temp; if(data_only) { DOReadChunk(st, sf); } else { temp = smem_tell(st); while(smem_tell(st) < temp + size) { uint32 tsize; char toa[32]; if(smem_read(st, toa, 32) <= 0) { puts("Unexpected EOF?"); return 0; } smem_read32le(st, &tsize); if((tmp=CheckS(sf,tsize,toa))) { int32 bytesize = tmp->s&(~(MDFNSTATE_RLSB32 | MDFNSTATE_RLSB16 | RLSB)); smem_read(st, (uint8 *)tmp->v, bytesize); if(tmp->s & MDFNSTATE_RLSB32) Endian_A32_LE_to_NE(tmp->v, bytesize / sizeof(uint32)); else if(tmp->s & MDFNSTATE_RLSB16) Endian_A16_LE_to_NE(tmp->v, bytesize / sizeof(uint16)); else if(tmp->s&RLSB) Endian_V_LE_to_NE(tmp->v, bytesize); } else if(smem_seek(st,tsize,SEEK_CUR) < 0) { puts("Seek error"); return(0); } } // while(...) } return 1; }
static int ReadStateChunk(StateMem *st, SFORMAT *sf, int size) { int temp = st->loc; while (st->loc < (temp + size)) { uint32_t recorded_size; // In bytes uint8_t toa[1 + 256]; // Don't change to char unless cast toa[0] to unsigned to smem_read() and other places. if(smem_read(st, toa, 1) != 1) { puts("Unexpected EOF"); return(0); } if(smem_read(st, toa + 1, toa[0]) != toa[0]) { puts("Unexpected EOF?"); return 0; } toa[1 + toa[0]] = 0; smem_read32le(st, &recorded_size); SFORMAT *tmp = FindSF((char*)toa + 1, sf); if(tmp) { uint32_t expected_size = tmp->size; // In bytes if(recorded_size != expected_size) { printf("Variable in save state wrong size: %s. Need: %d, got: %d\n", toa + 1, expected_size, recorded_size); if(smem_seek(st, recorded_size, SEEK_CUR) < 0) { puts("Seek error"); return(0); } } else { smem_read(st, (uint8_t *)tmp->v, expected_size); if(tmp->flags & MDFNSTATE_BOOL) { // Converting downwards is necessary for the case of sizeof(bool) > 1 for(int32_t bool_monster = expected_size - 1; bool_monster >= 0; bool_monster--) { ((bool *)tmp->v)[bool_monster] = ((uint8_t *)tmp->v)[bool_monster]; } } #ifdef MSB_FIRST if(tmp->flags & MDFNSTATE_RLSB64) Endian_A64_LE_to_NE(tmp->v, expected_size / sizeof(uint64_t)); else if(tmp->flags & MDFNSTATE_RLSB32) Endian_A32_LE_to_NE(tmp->v, expected_size / sizeof(uint32_t)); else if(tmp->flags & MDFNSTATE_RLSB16) Endian_A16_LE_to_NE(tmp->v, expected_size / sizeof(uint16_t)); else if(tmp->flags & RLSB) Endian_V_LE_to_NE(tmp->v, expected_size); #endif } } else { printf("Unknown variable in save state: %s\n", toa + 1); if(smem_seek(st, recorded_size, SEEK_CUR) < 0) { puts("Seek error"); return(0); } } } // while(...) assert(st->loc == (temp + size)); return 1; }
/* This function is called by the game driver(NES, GB, GBA) to save a state. */ int MDFNSS_StateAction(StateMem *st, int load, int data_only, std::vector <SSDescriptor> §ions) { std::vector<SSDescriptor>::iterator section; if(load) { char sname[4]; for(section = sections.begin(); section != sections.end(); section++) { if(data_only) { ReadStateChunk(st, section->sf, ~0, 1); } else { int found = 0; uint32 tmp_size; uint32 total = 0; while(smem_read(st, (uint8 *)sname, 4) == 4) { if(!smem_read32le(st, &tmp_size)) return(0); total += tmp_size + 8; // Yay, we found the section if(!memcmp(sname, section->name, 4)) { if(!ReadStateChunk(st, section->sf, tmp_size, 0)) { printf("Error reading chunk: %.4s\n", section->name); return(0); } found = 1; break; } else { //puts("SEEK"); if(smem_seek(st, tmp_size, SEEK_CUR) < 0) { puts("Chunk seek failure"); return(0); } } } if(smem_seek(st, -(int)total, SEEK_CUR) < 0) { puts("Reverse seek error"); return(0); } if(!found && !section->optional) // Not found. We are sad! { printf("Chunk missing: %.4s\n", section->name); return(0); } } } } else for(section = sections.begin(); section != sections.end(); section++) { if(!WriteStateChunk(st, section->name, section->sf, data_only)) return(0); } return(1); }
static int ReadStateChunk(StateMem *st, SFORMAT *sf, int size, int data_only) { int temp; if(data_only) { DOReadChunk(st, sf); } else { SFMap_t sfmap; SFMap_t sfmap_found; // Used for identify variables that are missing in the save state. MakeSFMap(sf, sfmap); temp = smem_tell(st); while(smem_tell(st) < (temp + size)) { uint32 tsize; char toa[1 + 256]; if(smem_read(st, toa, 1) != 1) { puts("Unexpected EOF"); return(0); } if(smem_read(st, toa + 1, toa[0]) != toa[0]) { puts("Unexpected EOF?"); return 0; } toa[1 + toa[0]] = 0; smem_read32le(st, &tsize); SFMap_t::iterator sfmit; sfmit = sfmap.find(toa + 1); if(sfmit != sfmap.end()) { SFORMAT *tmp = sfmit->second; int32 bytesize = tmp->size; sfmap_found[tmp->name] = tmp; smem_read(st, (uint8 *)tmp->v, bytesize); if(tmp->flags & MDFNSTATE_BOOL) { // Converting downwards is necessary for the case of sizeof(bool) > 1 for(int32 bool_monster = bytesize - 1; bool_monster >= 0; bool_monster--) { ((bool *)tmp->v)[bool_monster] = (bool)((uint8 *)tmp->v)[bool_monster]; } } if(tmp->flags & MDFNSTATE_RLSB64) Endian_A64_LE_to_NE(tmp->v, bytesize / sizeof(uint64)); else if(tmp->flags & MDFNSTATE_RLSB32) Endian_A32_LE_to_NE(tmp->v, bytesize / sizeof(uint32)); else if(tmp->flags & MDFNSTATE_RLSB16) Endian_A16_LE_to_NE(tmp->v, bytesize / sizeof(uint16)); else if(tmp->flags & RLSB) Endian_V_LE_to_NE(tmp->v, bytesize); } else { printf("Unknown variable in save state: %s\n", toa + 1); if(smem_seek(st, tsize, SEEK_CUR) < 0) { puts("Seek error"); return(0); } } } // while(...) for(SFMap_t::const_iterator it = sfmap.begin(); it != sfmap.end(); it++) { if(sfmap_found.find(it->second->name) == sfmap_found.end()) { printf("Variable missing from save state: %s\n", it->second->name); } } assert(smem_tell(st) == (temp + size)); } return 1; }
/* This function is called by the game driver(NES, GB, GBA) to save a state. */ int MDFNSS_StateAction(void *st_p, int load, int data_only, std::vector <SSDescriptor> §ions) { StateMem *st = (StateMem*)st_p; std::vector<SSDescriptor>::iterator section; if(load) { { char sname[32]; for(section = sections.begin(); section != sections.end(); section++) { int found = 0; uint32 tmp_size; uint32 total = 0; while(smem_read(st, (uint8 *)sname, 32) == 32) { if(smem_read32le(st, &tmp_size) != 4) return(0); total += tmp_size + 32 + 4; // Yay, we found the section if(!strncmp(sname, section->name, 32)) { if(!ReadStateChunk(st, section->sf, tmp_size)) { printf("Error reading chunk: %s\n", section->name); return(0); } found = 1; break; } else { if(smem_seek(st, tmp_size, SEEK_CUR) < 0) { puts("Chunk seek failure"); return(0); } } } if(smem_seek(st, -total, SEEK_CUR) < 0) { puts("Reverse seek error"); return(0); } if(!found && !section->optional) // Not found. We are sad! { printf("Section missing: %.32s\n", section->name); return(0); } } } } else { for(section = sections.begin(); section != sections.end(); section++) { if(!WriteStateChunk(st, section->name, section->sf)) return(0); } } return(1); }
static int ReadStateChunk(StateMem *st, SFORMAT *sf, int size) { int temp; { SFMap_t sfmap; SFMap_t sfmap_found; // Used for identifying variables that are missing in the save state. MakeSFMap(sf, sfmap); temp = smem_tell(st); while(smem_tell(st) < (temp + size)) { uint32 recorded_size; // In bytes uint8 toa[1 + 256]; // Don't change to char unless cast toa[0] to unsigned to smem_read() and other places. if(smem_read(st, toa, 1) != 1) { puts("Unexpected EOF"); return(0); } if(smem_read(st, toa + 1, toa[0]) != toa[0]) { puts("Unexpected EOF?"); return 0; } toa[1 + toa[0]] = 0; smem_read32le(st, &recorded_size); SFMap_t::iterator sfmit; sfmit = sfmap.find((char *)toa + 1); if(sfmit != sfmap.end()) { SFORMAT *tmp = sfmit->second; uint32 expected_size = tmp->size; // In bytes if(recorded_size != expected_size) { printf("Variable in save state wrong size: %s. Need: %d, got: %d\n", toa + 1, expected_size, recorded_size); if(smem_seek(st, recorded_size, SEEK_CUR) < 0) { puts("Seek error"); return(0); } } else { sfmap_found[tmp->name] = tmp; smem_read(st, (uint8 *)tmp->v, expected_size); if(tmp->flags & MDFNSTATE_BOOL) { // Converting downwards is necessary for the case of sizeof(bool) > 1 for(int32 bool_monster = expected_size - 1; bool_monster >= 0; bool_monster--) { ((bool *)tmp->v)[bool_monster] = ((uint8 *)tmp->v)[bool_monster]; } } if(tmp->flags & MDFNSTATE_RLSB64) Endian_A64_LE_to_NE(tmp->v, expected_size / sizeof(uint64)); else if(tmp->flags & MDFNSTATE_RLSB32) Endian_A32_LE_to_NE(tmp->v, expected_size / sizeof(uint32)); else if(tmp->flags & MDFNSTATE_RLSB16) Endian_A16_LE_to_NE(tmp->v, expected_size / sizeof(uint16)); else if(tmp->flags & RLSB) Endian_V_LE_to_NE(tmp->v, expected_size); } } else { printf("Unknown variable in save state: %s\n", toa + 1); if(smem_seek(st, recorded_size, SEEK_CUR) < 0) { puts("Seek error"); return(0); } } } // while(...) for(SFMap_t::const_iterator it = sfmap.begin(); it != sfmap.end(); it++) { if(sfmap_found.find(it->second->name) == sfmap_found.end()) { printf("Variable missing from save state: %s\n", it->second->name); } } assert(smem_tell(st) == (temp + size)); } return 1; }