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

  ViewVC Help
Powered by ViewVC 1.1.22