|
|
1.1 root 1: /* 1.1.1.4 ! root 2: Hatari - memorySnapShot.c ! 3: ! 4: This file is distributed under the GNU Public License, version 2 or at ! 5: your option any later version. Read the file gpl.txt for details. 1.1 root 6: 7: Memory Snapshot 8: 9: This handles the saving/restoring of the emulator's state so any game/application can be saved 10: and restored at any time. This is quite complicated as we need to store all STRam, all chip states, 11: all emulation variables and then things get really complicated as we need to restore file handles 12: and such like. 13: To help keep things simple each file has one function which is used to save/restore all variables 14: that are local to it. We use one function to reduce redundancy and the function 'MemorySnapShot_Store' 15: decides if it should save or restore the data. 16: */ 1.1.1.4 ! root 17: static char rcsid[] = "Hatari $Id: memorySnapShot.c,v 1.4 2003/04/05 22:25:02 thothy Exp $"; ! 18: ! 19: #include <SDL_types.h> 1.1 root 20: 21: #include "main.h" 22: #include "debug.h" 23: #include "dialog.h" 24: #include "fdc.h" 25: #include "file.h" 26: #include "floppy.h" 27: #include "gemdos.h" 28: #include "ikbd.h" 29: #include "int.h" 30: #include "m68000.h" 31: #include "memorySnapShot.h" 32: #include "mfp.h" 33: #include "psg.h" 34: #include "reset.h" 35: #include "sound.h" 36: #include "tos.h" 37: #include "video.h" 1.1.1.2 root 38: 1.1 root 39: 40: //#define COMPRESS_MEMORYSNAPSHOT // Compress snapshots to reduce disc space used 41: 42: //HFILE CaptureFile; 43: //OFSTRUCT CaptureFileInfo; 44: BOOL bCaptureSave, bCaptureError; 45: BOOL bSaveMemoryState=FALSE, bRestoreMemoryState=FALSE; 46: char szSnapShotFileName[MAX_FILENAME_LENGTH]; 47: 48: //----------------------------------------------------------------------- 49: /* 50: Check if need to save/restore emulation memory state, via flag 'bSaveMemoryState', and 'bRestoreMemoryState' 51: */ 52: void MemorySnapShot_CheckSaveRestore(void) 53: { 54: // Is chose to save memory state, one of these two flags will be set 55: /*FIXME*/ 56: /* 57: if (bSaveMemoryState || bRestoreMemoryState) { 58: Main_PauseEmulation(); // Hold things... 59: View_ToggleWindowsMouse(MOUSE_WINDOWS); // Put mouse into ST mode 60: View_LimitCursorToScreen(); // Free mouse from Window constraints 61: 62: // Do we need user to enter a filename? 63: if (strlen(ConfigureParams.Memory.szMemoryCaptureFileName)<=0) { 64: if (!File_OpenSelectDlg(hWnd,ConfigureParams.Memory.szMemoryCaptureFileName,FILEFILTER_MEMORYFILE,FALSE,bSaveMemoryState)) 65: bSaveMemoryState = bRestoreMemoryState = FALSE; 66: } 67: 68: // Do save/load 69: if (bSaveMemoryState) 70: MemorySnapShot_Capture(ConfigureParams.Memory.szMemoryCaptureFileName); 71: else if (bRestoreMemoryState) 72: MemorySnapShot_Restore(ConfigureParams.Memory.szMemoryCaptureFileName); 73: bSaveMemoryState = bRestoreMemoryState = FALSE; 74: 75: View_LimitCursorToClient(); // And limit mouse in Window 76: View_ToggleWindowsMouse(MOUSE_ST); // Put mouse into ST mode 77: Main_UnPauseEmulation(); // And off we go... 78: } 79: */ 80: } 81: 82: //----------------------------------------------------------------------- 83: /* 84: Get filename to save/restore to, so can compress 85: */ 86: char *MemorySnapShot_CompressBegin(char *pszFileName) 87: { 88: #ifdef COMPRESS_MEMORYSNAPSHOT 89: // Create temporary filename 90: sprintf(szSnapShotFileName,"%s/%s",szWorkingDir,"snapshot.mem"); 91: return(szSnapShotFileName); 92: #else //COMPRESS_MEMORYSNAPSHOT 93: return(pszFileName); 94: #endif //COMPRESS_MEMORYSNAPSHOT 95: } 96: 97: //----------------------------------------------------------------------- 98: /* 99: Compress memory snap shot 100: */ 101: void MemorySnapShot_CompressEnd(char *pszFileName) 102: { 103: #ifdef COMPRESS_MEMORYSNAPSHOT 104: // Compress file from temporary to final destination 105: Compress_Pack_File(szSnapShotFileName,pszFileName); 106: // And delete temporary 107: File_Delete(szSnapShotFileName); 108: #endif //COMPRESS_MEMORYSNAPSHOT 109: } 110: 111: //----------------------------------------------------------------------- 112: /* 113: Uncompress memory snap shot to temporary file 114: */ 115: char *MemorySnapShot_UnCompressBegin(char *pszFileName) 116: { 117: #ifdef COMPRESS_MEMORYSNAPSHOT 118: // Uncompress to temporary file 119: sprintf(szSnapShotFileName,"%s/%s",szWorkingDir,"snapshot.mem"); 120: Compress_UnPack_File(pszFileName,szSnapShotFileName); 121: return(szSnapShotFileName); 122: #else //COMPRESS_MEMORYSNAPSHOT 123: return(pszFileName); 124: #endif //COMPRESS_MEMORYSNAPSHOT 125: } 126: 127: //----------------------------------------------------------------------- 128: /* 129: Clean up after uncompression 130: */ 131: void MemorySnapShot_UnCompressEnd(void) 132: { 133: // And delete temporary 134: File_Delete(szSnapShotFileName); 135: } 136: 137: //----------------------------------------------------------------------- 138: /* 139: Open/Create snapshot file, and set flag so 'MemorySnapShot_Store' knows how to handle data 140: */ 141: BOOL MemorySnapShot_OpenFile(char *pszFileName,BOOL bSave) 142: { 143: /* FIXME */ 144: /* 1.1.1.3 root 145: char szString[256]; 146: char VersionString[VERSION_STRING_SIZE]; 147: 1.1 root 148: // Set error 149: bCaptureError = FALSE; 150: 151: // Open file, set flag so 'MemorySnapShot_Store' can load to/save from file 152: if (bSave) { 153: // Save 154: CaptureFile = OpenFile(pszFileName,&CaptureFileInfo,OF_CREATE | OF_WRITE); 155: if (CaptureFile==HFILE_ERROR) { 156: bCaptureError = TRUE; 157: return(FALSE); 158: } 159: bCaptureSave = TRUE; 160: // Store version string 161: MemorySnapShot_Store(VERSION_STRING,VERSION_STRING_SIZE); 162: } 163: else { 164: // Restore 165: CaptureFile = OpenFile(pszFileName,&CaptureFileInfo,OF_READ); 166: if (CaptureFile==HFILE_ERROR) { 167: bCaptureError = TRUE; 168: return(FALSE); 169: } 170: bCaptureSave = FALSE; 171: // Restore version string 172: MemorySnapShot_Store(VersionString,VERSION_STRING_SIZE); 173: // Does match current version? 174: if (stricmp(VersionString,VERSION_STRING)) { 175: // No, inform user and error 176: sprintf(szString,"Unable to Restore Memory State.\nFile is only compatible with Hatari v%s",VersionString); 177: Main_Message(szString,PROG_NAME,MB_OK | MB_ICONSTOP); 178: bCaptureError = TRUE; 179: return(FALSE); 180: } 181: } 182: 183: // All OK 184: return(TRUE); 185: */ 186: return FALSE; 187: } 188: 189: //----------------------------------------------------------------------- 190: /* 191: Close snapshot file 192: */ 193: void MemorySnapShot_CloseFile(void) 194: { 195: //FIXME _lclose(CaptureFile); 196: } 197: 198: //----------------------------------------------------------------------- 199: /* 200: Save/Restore data to/from file 201: */ 202: void MemorySnapShot_Store(void *pData, int Size) 203: { 204: /*FIXME*/ 205: /* 1.1.1.3 root 206: long nBytes; 207: 1.1 root 208: // Check no file errors 209: if (CaptureFile!=HFILE_ERROR) { 210: // Saving or Restoring? 211: if (bCaptureSave) 212: nBytes = _hwrite(CaptureFile,(char *)pData,Size); 213: else 214: nBytes = _hread(CaptureFile,(char *)pData,Size); 215: 216: // Did save OK? 217: if (nBytes==HFILE_ERROR) 218: bCaptureError = TRUE; 219: else if (nBytes!=Size) 220: bCaptureError = TRUE; 221: } 222: */ 223: } 224: 225: //----------------------------------------------------------------------- 226: /* 227: Save 'snapshot' of memory/chips/emulation variables 228: */ 229: void MemorySnapShot_Capture(char *pszFileName) 230: { 231: /*FIXME*/ 232: /* 1.1.1.3 root 233: char *pszSnapShotFileName; 234: 1.1 root 235: // Wait... 236: SetCursor(Cursors[CURSOR_HOURGLASS]); 237: 238: // If to be compressed, return temporary filename 239: pszSnapShotFileName = MemorySnapShot_CompressBegin(pszFileName); 240: 241: // Set to 'saving' 242: if (MemorySnapShot_OpenFile(pszSnapShotFileName,TRUE)) { 243: // Capture each files details 244: Main_MemorySnapShot_Capture(TRUE); 245: FDC_MemorySnapShot_Capture(TRUE); 246: Floppy_MemorySnapShot_Capture(TRUE); 247: GemDOS_MemorySnapShot_Capture(TRUE); 248: IKBD_MemorySnapShot_Capture(TRUE); 249: Int_MemorySnapShot_Capture(TRUE); 250: M68000_MemorySnapShot_Capture(TRUE); 251: M68000_Decode_MemorySnapShot_Capture(TRUE); 252: MFP_MemorySnapShot_Capture(TRUE); 253: PSG_MemorySnapShot_Capture(TRUE); 254: Sound_MemorySnapShot_Capture(TRUE); 255: TOS_MemorySnapShot_Capture(TRUE); 256: Video_MemorySnapShot_Capture(TRUE); 257: 258: // And close 259: MemorySnapShot_CloseFile(); 260: } 261: 262: // And compress, if need to 263: MemorySnapShot_CompressEnd(pszFileName); 264: 265: // We're back 266: SetCursor(Cursors[CURSOR_ARROW]); 267: 268: // Did error 269: if (bCaptureError) 270: Main_Message("Unable to Save Memory State to file.",PROG_NAME,MB_OK | MB_ICONSTOP); 271: else 272: Main_Message("Memory State file saved.",PROG_NAME,MB_OK | MB_ICONINFORMATION); 273: */ 274: } 275: 276: //----------------------------------------------------------------------- 277: /* 278: Restore 'snapshot' of memory/chips/emulation variables 279: */ 280: void MemorySnapShot_Restore(char *pszFileName) 281: { 282: /*FIXME*/ 283: /* 1.1.1.3 root 284: char *pszSnapShotFileName; 285: 1.1 root 286: // Wait... 287: SetCursor(Cursors[CURSOR_HOURGLASS]); 288: 289: // If to be uncompressed, return temporary filename 290: pszSnapShotFileName = MemorySnapShot_UnCompressBegin(pszFileName); 291: 292: // Set to 'restore' 293: if (MemorySnapShot_OpenFile(pszSnapShotFileName,FALSE)) { 294: // Reset emulator to get things running 295: Reset_Cold(); 296: 297: // Capture each files details 298: Main_MemorySnapShot_Capture(FALSE); 299: FDC_MemorySnapShot_Capture(FALSE); 300: Floppy_MemorySnapShot_Capture(FALSE); 301: GemDOS_MemorySnapShot_Capture(FALSE); 302: IKBD_MemorySnapShot_Capture(FALSE); 303: Int_MemorySnapShot_Capture(FALSE); 304: M68000_MemorySnapShot_Capture(FALSE); 305: M68000_Decode_MemorySnapShot_Capture(FALSE); 306: MFP_MemorySnapShot_Capture(FALSE); 307: PSG_MemorySnapShot_Capture(FALSE); 308: Sound_MemorySnapShot_Capture(FALSE); 309: TOS_MemorySnapShot_Capture(FALSE); 310: Video_MemorySnapShot_Capture(FALSE); 311: 312: // And close 313: MemorySnapShot_CloseFile(); 314: } 315: 316: // And clean up 317: MemorySnapShot_UnCompressEnd(); 318: 319: // We're back 320: SetCursor(Cursors[CURSOR_ARROW]); 321: 322: // Did error 323: if (bCaptureError) 324: Main_Message("Unable to Restore Memory State from file.",PROG_NAME,MB_OK | MB_ICONSTOP); 325: else 326: Main_Message("Memory State file restored.",PROG_NAME,MB_OK | MB_ICONINFORMATION); 327: */ 328: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.