/[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 283 - (show annotations) (download)
Thu Dec 23 12:39:52 2010 UTC (9 years, 1 month ago) by william
File MIME type: text/plain
File size: 7580 byte(s)
Auto Commited Import of: pcsx2-0.9.7-DEBUG (upstream: v0.9.7.4132 local: v0.9.7.282-latest) 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 = (0x9A01 << 16) | 0x0000;
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 namespace Exception
35 {
36 // ---------------------------------------------------------------------------------------
37 // Savestate Exceptions:
38 // UnsupportedStateVersion / StateCrcMismatch
39 // ---------------------------------------------------------------------------------------
40
41 // thrown when the savestate being loaded isn't supported.
42 //
43 class UnsupportedStateVersion : public SaveStateLoadError
44 {
45 DEFINE_EXCEPTION_COPYTORS( UnsupportedStateVersion, SaveStateLoadError )
46 DEFINE_EXCEPTION_MESSAGES( UnsupportedStateVersion )
47
48 public:
49 u32 Version; // version number of the unsupported state.
50
51 protected:
52 UnsupportedStateVersion() {}
53
54 public:
55 explicit UnsupportedStateVersion( int version )
56 {
57 Version = version;
58 }
59
60 virtual wxString FormatDiagnosticMessage() const;
61 virtual wxString FormatDisplayMessage() const;
62 };
63
64 // A recoverable exception thrown when the CRC of the savestate does not match the
65 // CRC returned by the Cdvd driver.
66 // [feature not implemented yet]
67 //
68 class StateCrcMismatch : public SaveStateLoadError
69 {
70 DEFINE_EXCEPTION_COPYTORS( StateCrcMismatch, SaveStateLoadError )
71 DEFINE_EXCEPTION_MESSAGES( StateCrcMismatch )
72
73 public:
74 u32 Crc_Savestate;
75 u32 Crc_Cdvd;
76
77 protected:
78 StateCrcMismatch() {}
79
80 public:
81 StateCrcMismatch( u32 crc_save, u32 crc_cdvd )
82 {
83 Crc_Savestate = crc_save;
84 Crc_Cdvd = crc_cdvd;
85 }
86
87 virtual wxString FormatDiagnosticMessage() const;
88 virtual wxString FormatDisplayMessage() const;
89 };
90 }
91
92 // --------------------------------------------------------------------------------------
93 // SaveStateBase class
94 // --------------------------------------------------------------------------------------
95 // Provides the base API for both loading and saving savestates. Normally you'll want to
96 // use one of the four "functional" derived classes rather than this class directly: gzLoadingState, gzSavingState (gzipped disk-saved
97 // states), and memLoadingState, memSavingState (uncompressed memory states).
98 class SaveStateBase
99 {
100 protected:
101 VmStateBuffer* m_memory;
102 char m_tagspace[32];
103
104 u32 m_version; // version of the savestate being loaded.
105
106 int m_idx; // current read/write index of the allocation
107
108 bool m_DidBios;
109
110 public:
111 SaveStateBase( VmStateBuffer& memblock );
112 SaveStateBase( VmStateBuffer* memblock );
113 virtual ~SaveStateBase() { }
114
115 static wxString GetFilename( int slot );
116
117 // Gets the version of savestate that this object is acting on.
118 // The version refers to the low 16 bits only (high 16 bits classifies Pcsx2 build types)
119 u32 GetVersion() const
120 {
121 return (m_version & 0xffff);
122 }
123
124 // Loads or saves the entire emulation state.
125 // Note: The Cpu state must be reset, and plugins *open*, prior to Defrosting
126 // (loading) a state!
127 virtual SaveStateBase& FreezeAll();
128
129 virtual SaveStateBase& FreezeMainMemory();
130 virtual SaveStateBase& FreezeBios();
131 virtual SaveStateBase& FreezeInternals();
132 virtual SaveStateBase& FreezePlugins();
133
134 // Loads or saves an arbitrary data type. Usable on atomic types, structs, and arrays.
135 // For dynamically allocated pointers use FreezeMem instead.
136 template<typename T>
137 void Freeze( T& data )
138 {
139 FreezeMem( const_cast<void*>((void*)&data), sizeof( T ) );
140 }
141
142 // FreezeLegacy can be used to load structures short of their full size, which is
143 // useful for loading structures that have had new stuff added since a previous version.
144 template<typename T>
145 void FreezeLegacy( T& data, int sizeOfNewStuff )
146 {
147 FreezeMem( &data, sizeof( T ) - sizeOfNewStuff );
148 }
149
150 void PrepBlock( int size );
151
152 uint GetCurrentPos() const
153 {
154 return m_idx;
155 }
156
157 u8* GetBlockPtr()
158 {
159 return m_memory->GetPtr(m_idx);
160 }
161
162 u8* GetPtrEnd() const
163 {
164 return m_memory->GetPtrEnd();
165 }
166
167 void CommitBlock( int size )
168 {
169 m_idx += size;
170 }
171
172 // Freezes an identifier value into the savestate for troubleshooting purposes.
173 // Identifiers can be used to determine where in a savestate that data has become
174 // skewed (if the value does not match then the error occurs somewhere prior to that
175 // position).
176 void FreezeTag( const char* src );
177
178 // Returns true if this object is a StateLoading type object.
179 bool IsLoading() const { return !IsSaving(); }
180
181 // Loads or saves a memory block.
182 virtual void FreezeMem( void* data, int size )=0;
183
184 // Returns true if this object is a StateSaving type object.
185 virtual bool IsSaving() const=0;
186
187 public:
188 // note: gsFreeze() needs to be public because of the GSState recorder.
189 void gsFreeze();
190
191 protected:
192 void Init( VmStateBuffer* memblock );
193
194 // Load/Save functions for the various components of our glorious emulator!
195
196 void rcntFreeze();
197 void vuMicroFreeze();
198 void vif0Freeze();
199 void vif1Freeze();
200 #ifdef ENABLE_NEW_IOPDMA
201 void iopDmacFreeze();
202 #endif
203 void sifFreeze();
204 void ipuFreeze();
205 void ipuDmaFreeze();
206 void gifFreeze();
207 void sprFreeze();
208
209 void sioFreeze();
210 void cdrFreeze();
211 void cdvdFreeze();
212 void psxRcntFreeze();
213 void sio2Freeze();
214
215 void gifPathFreeze(); // called by gsFreeze
216
217 void deci2Freeze();
218 };
219
220 // --------------------------------------------------------------------------------------
221 // Saving and Loading Specialized Implementations...
222 // --------------------------------------------------------------------------------------
223
224 class memSavingState : public SaveStateBase
225 {
226 typedef SaveStateBase _parent;
227
228 protected:
229 static const int ReallocThreshold = _1mb / 4; // 256k reallocation block size.
230 static const int MemoryBaseAllocSize = _8mb; // 8 meg base alloc when PS2 main memory is excluded
231
232 public:
233 virtual ~memSavingState() throw() { }
234 memSavingState( VmStateBuffer& save_to );
235 memSavingState( VmStateBuffer* save_to );
236
237 void MakeRoomForData();
238
239 void FreezeMem( void* data, int size );
240 memSavingState& FreezeAll();
241
242 bool IsSaving() const { return true; }
243 };
244
245 class memLoadingState : public SaveStateBase
246 {
247 public:
248 virtual ~memLoadingState() throw();
249
250 memLoadingState( const VmStateBuffer& load_from );
251 memLoadingState( const VmStateBuffer* load_from );
252
253 void FreezeMem( void* data, int size );
254
255 bool IsSaving() const { return false; }
256 bool IsFinished() const { return m_idx >= m_memory->GetSizeInBytes(); }
257 };
258

  ViewVC Help
Powered by ViewVC 1.1.22