/[pcsx2_0.9.7]/trunk/pcsx2/SaveState.h
ViewVC logotype

Contents of /trunk/pcsx2/SaveState.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 62 - (show annotations) (download)
Tue Sep 7 11:08:22 2010 UTC (9 years, 4 months ago) by william
File MIME type: text/plain
File size: 7954 byte(s)
Auto Commited Import of: pcsx2-0.9.7-r3738-debug in ./trunk
1 /* PCSX2 - PS2 Emulator for PCs
2 * Copyright (C) 2002-2010 PCSX2 Dev Team
3 *
4 * PCSX2 is free software: you can redistribute it and/or modify it under the terms
5 * of the GNU Lesser General Public License as published by the Free Software Found-
6 * ation, either version 3 of the License, or (at your option) any later version.
7 *
8 * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 * PURPOSE. See the GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along with PCSX2.
13 * If not, see <http://www.gnu.org/licenses/>.
14 */
15
16 #pragma once
17
18 #include "PS2Edefs.h"
19 #include "System.h"
20
21 // Savestate Versioning!
22 // If you make changes to the savestate version, please increment the value below.
23 // If the change is minor and compatibility with old states is retained, increment
24 // the lower 16 bit value. IF the change is breaking of all compatibility with old
25 // states, increment the upper 16 bit value, and clear the lower 16 bits to 0.
26
27 static const u32 g_SaveVersion = 0x8b490000;
28
29 // this function is meant to be used in the place of GSfreeze, and provides a safe layer
30 // between the GS saving function and the MTGS's needs. :)
31 extern s32 CALLBACK gsSafeFreeze( int mode, freezeData *data );
32
33
34 enum FreezeSectionId
35 {
36 FreezeId_NotSeeking = -2,
37 FreezeId_End,
38
39 // A BIOS tag should always be saved in conjunction with Memory or Registers tags,
40 // but can be skipped if the savestate has only plugins.
41 FreezeId_Bios,
42
43 FreezeId_Memory,
44 FreezeId_Registers,
45
46 FreezeId_Plugin,
47
48 // anything here and beyond we can skip, with a warning
49 FreezeId_Unknown,
50 };
51
52 namespace Exception
53 {
54 // ---------------------------------------------------------------------------------------
55 // Savestate Exceptions:
56 // UnsupportedStateVersion / StateCrcMismatch
57 // ---------------------------------------------------------------------------------------
58
59 // thrown when the savestate being loaded isn't supported.
60 //
61 class UnsupportedStateVersion : public SaveStateLoadError
62 {
63 DEFINE_EXCEPTION_COPYTORS( UnsupportedStateVersion, SaveStateLoadError )
64 DEFINE_EXCEPTION_MESSAGES( UnsupportedStateVersion )
65
66 public:
67 u32 Version; // version number of the unsupported state.
68
69 protected:
70 UnsupportedStateVersion() {}
71
72 public:
73 explicit UnsupportedStateVersion( int version )
74 {
75 Version = version;
76 }
77
78 virtual wxString FormatDiagnosticMessage() const;
79 virtual wxString FormatDisplayMessage() const;
80 };
81
82 // A recoverable exception thrown when the CRC of the savestate does not match the
83 // CRC returned by the Cdvd driver.
84 // [feature not implemented yet]
85 //
86 class StateCrcMismatch : public SaveStateLoadError
87 {
88 DEFINE_EXCEPTION_COPYTORS( StateCrcMismatch, SaveStateLoadError )
89 DEFINE_EXCEPTION_MESSAGES( StateCrcMismatch )
90
91 public:
92 u32 Crc_Savestate;
93 u32 Crc_Cdvd;
94
95 protected:
96 StateCrcMismatch() {}
97
98 public:
99 StateCrcMismatch( u32 crc_save, u32 crc_cdvd )
100 {
101 Crc_Savestate = crc_save;
102 Crc_Cdvd = crc_cdvd;
103 }
104
105 virtual wxString FormatDiagnosticMessage() const;
106 virtual wxString FormatDisplayMessage() const;
107 };
108 }
109
110 // --------------------------------------------------------------------------------------
111 // SaveStateBase class
112 // --------------------------------------------------------------------------------------
113 // Provides the base API for both loading and saving savestates. Normally you'll want to
114 // use one of the four "functional" derived classes rather than this class directly: gzLoadingState, gzSavingState (gzipped disk-saved
115 // states), and memLoadingState, memSavingState (uncompressed memory states).
116 class SaveStateBase
117 {
118 protected:
119 VmStateBuffer* m_memory;
120 char m_tagspace[32];
121
122 u32 m_version; // version of the savestate being loaded.
123
124 int m_idx; // current read/write index of the allocation
125 int m_sectid;
126 int m_pid;
127
128 bool m_DidBios;
129
130 public:
131 SaveStateBase( VmStateBuffer& memblock );
132 SaveStateBase( VmStateBuffer* memblock );
133 virtual ~SaveStateBase() { }
134
135 static wxString GetFilename( int slot );
136
137 // Gets the version of savestate that this object is acting on.
138 // The version refers to the low 16 bits only (high 16 bits classifies Pcsx2 build types)
139 u32 GetVersion() const
140 {
141 return (m_version & 0xffff);
142 }
143
144 // Loads or saves the entire emulation state.
145 // Note: The Cpu state must be reset, and plugins *open*, prior to Defrosting
146 // (loading) a state!
147 virtual void FreezeAll();
148
149 // Loads or saves an arbitrary data type. Usable on atomic types, structs, and arrays.
150 // For dynamically allocated pointers use FreezeMem instead.
151 template<typename T>
152 void Freeze( T& data )
153 {
154 FreezeMem( const_cast<void*>((void*)&data), sizeof( T ) );
155 }
156
157 // FreezeLegacy can be used to load structures short of their full size, which is
158 // useful for loading structures that have had new stuff added since a previous version.
159 template<typename T>
160 void FreezeLegacy( T& data, int sizeOfNewStuff )
161 {
162 FreezeMem( &data, sizeof( T ) - sizeOfNewStuff );
163 }
164
165 void PrepBlock( int size );
166
167 u8* GetBlockPtr()
168 {
169 return m_memory->GetPtr(m_idx);
170 }
171
172 void CommitBlock( int size )
173 {
174 m_idx += size;
175 }
176
177 void WritebackSectionLength( int seekpos, int sectlen, const wxChar* sectname );
178 bool FreezeSection( int seek_section = FreezeId_NotSeeking );
179
180 // Freezes an identifier value into the savestate for troubleshooting purposes.
181 // Identifiers can be used to determine where in a savestate that data has become
182 // skewed (if the value does not match then the error occurs somewhere prior to that
183 // position).
184 void FreezeTag( const char* src );
185
186 // Returns true if this object is a StateLoading type object.
187 bool IsLoading() const { return !IsSaving(); }
188
189 // Loads or saves a memory block.
190 virtual void FreezeMem( void* data, int size )=0;
191
192 // Returns true if this object is a StateSaving type object.
193 virtual bool IsSaving() const=0;
194
195 public:
196 // note: gsFreeze() needs to be public because of the GSState recorder.
197 void gsFreeze();
198
199 protected:
200 void Init( VmStateBuffer* memblock );
201
202 // Load/Save functions for the various components of our glorious emulator!
203
204 void FreezeBios();
205 void FreezeMainMemory();
206 void FreezeRegisters();
207
208 void rcntFreeze();
209 void vuMicroFreeze();
210 void vif0Freeze();
211 void vif1Freeze();
212 #ifdef ENABLE_NEW_IOPDMA
213 void iopDmacFreeze();
214 #endif
215 void sifFreeze();
216 void ipuFreeze();
217 void ipuDmaFreeze();
218 void gifFreeze();
219 void sprFreeze();
220
221 void sioFreeze();
222 void cdrFreeze();
223 void cdvdFreeze();
224 void psxRcntFreeze();
225 void sio2Freeze();
226
227 void gifPathFreeze(); // called by gsFreeze
228
229 void deci2Freeze();
230 };
231
232 // --------------------------------------------------------------------------------------
233 // Saving and Loading Specialized Implementations...
234 // --------------------------------------------------------------------------------------
235
236 class memSavingState : public SaveStateBase
237 {
238 typedef SaveStateBase _parent;
239
240 protected:
241 static const int ReallocThreshold = 0x200000; // 256k reallocation block size.
242 static const int MemoryBaseAllocSize = 0x02b00000; // 45 meg base alloc
243
244 public:
245 virtual ~memSavingState() throw() { }
246 memSavingState( VmStateBuffer& save_to );
247 memSavingState( VmStateBuffer* save_to );
248
249 // Saving of state data to a memory buffer
250 void FreezeMem( void* data, int size );
251 void FreezeAll();
252
253 bool IsSaving() const { return true; }
254 };
255
256 class memLoadingState : public SaveStateBase
257 {
258 public:
259 virtual ~memLoadingState() throw();
260
261 memLoadingState( const VmStateBuffer& load_from );
262 memLoadingState( const VmStateBuffer* load_from );
263
264 // Loading of state data from a memory buffer...
265 void FreezeMem( void* data, int size );
266 bool SeekToSection( PluginsEnum_t pid );
267
268 bool IsSaving() const { return false; }
269 bool IsFinished() const { return m_idx >= m_memory->GetSizeInBytes(); }
270 };
271

  ViewVC Help
Powered by ViewVC 1.1.22