-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSavestates.cpp
More file actions
297 lines (223 loc) · 10.1 KB
/
Copy pathSavestates.cpp
File metadata and controls
297 lines (223 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// *****************************************************************************
// include libretro headers
#include "glsym/glsym.h"
// include emulator headers
#include "Savestates.hpp"
#include "VideoOutput.hpp"
#include "Globals.hpp"
#include "Logging.hpp"
// include C/C++ headers
#include <string.h> // [ ANSI C ] Strings
// declare used namespaces
using namespace V32;
// *****************************************************************************
// =============================================================================
// SERIALIZATION (SAVE CONSOLE STATE TO BUFFER)
// =============================================================================
void SaveCPUState( CPUState& State )
{
// all fields should be adjacent in memory
// so read registers and flags together
memcpy( &State, Console.CPU.Registers, sizeof(CPUState) );
}
// -----------------------------------------------------------------------------
void SaveGPUState( GPUState& State )
{
V32GPU& GPU = Console.GPU;
// read all registers as adjacent
memcpy( State.Registers, &GPU.Command, sizeof(State.Registers) );
// copy the BIOS texture
memcpy( &State.BiosTexture, &GPU.BiosTexture, sizeof(GPUTexture) );
// copy only the needed cartridge textures
unsigned TexturesSize = sizeof(GPUTexture) * GPU.LoadedCartridgeTextures;
memcpy( State.CartridgeTextures, &GPU.CartridgeTextures[ 0 ], TexturesSize );
}
// -----------------------------------------------------------------------------
void SaveSPUState( SPUState& State )
{
V32SPU& SPU = Console.SPU;
// read all registers as adjacent
memcpy( State.Registers, &SPU.Command, sizeof(State.Registers) );
// read all channels as adjacent
memcpy( State.Channels, &SPU.Channels, sizeof(State.Channels) );
// copy the BIOS sound
memcpy( &State.BiosSound, &SPU.BiosSound, sizeof(SPUSoundState) );
// copy only the needed cartridge sounds
// (size will stay the same, but speed will increase)
unsigned CartridgeSounds = SPU.LoadedCartridgeSounds;
// do not read data for all sounds as a block!!
// we don't want to copy the sample vector in each sound
for( unsigned SoundID = 0; SoundID < CartridgeSounds; SoundID++ )
memcpy( &State.CartridgeSounds[ SoundID ], &SPU.CartridgeSounds[ SoundID ], sizeof(SPUSoundState) );
}
// -----------------------------------------------------------------------------
void SaveGamepadControllerState( GamepadControllerState& State )
{
State.SelectedGamepad = Console.GamepadController.SelectedGamepad;
// read all gamepad states as adjacent
memcpy( State.GamepadStates, Console.GamepadController.RealTimeGamepadStates, sizeof(State.GamepadStates) );
}
// -----------------------------------------------------------------------------
void SaveOtherConsoleState( OtherConsoleState& State )
{
// save state for minor chips
memcpy( State.TimerRegisters, &Console.Timer.CurrentDate, sizeof(State.TimerRegisters) );
State.RNGCurrentValue = Console.RNG.CurrentValue;
// save the full RAM
memcpy( State.RAM, &Console.RAM.Memory[ 0 ], sizeof(State.RAM) );
}
// -----------------------------------------------------------------------------
void SaveGameInfo( ROMInfo& Info )
{
// ensure title does not exceed 64 bytes and
// that its unused characters are all null
memset( Info.Title, 0, sizeof(Info.Title) );
strncpy( Info.Title, Console.CartridgeController.CartridgeTitle.c_str(), sizeof(Info.Title) - 1 );
Info.Version = Console.CartridgeController.CartridgeVersion;
Info.Revision = Console.CartridgeController.CartridgeRevision;
Info.ProgramROMSize = Console.CartridgeController.MemorySize;
Info.NumberOfTextures = Console.CartridgeController.NumberOfTextures;
Info.NumberOfSounds = Console.CartridgeController.NumberOfSounds;
}
// -----------------------------------------------------------------------------
void SaveBiosInfo( ROMInfo& Info )
{
// ensure title does not exceed 64 bytes and
// that its unused characters are all null
memset( Info.Title, 0, sizeof(Info.Title) );
strncpy( Info.Title, Console.BiosTitle.c_str(), sizeof(Info.Title) - 1 );
Info.Version = Console.BiosVersion;
Info.Revision = Console.BiosRevision;
Info.ProgramROMSize = Console.BiosProgramROM.MemorySize;
Info.NumberOfTextures = 1;
Info.NumberOfSounds = 1;
}
// -----------------------------------------------------------------------------
bool SaveState( ConsoleState* State )
{
// save info to identify the game and BIOS
SaveGameInfo( State->Game );
SaveBiosInfo( State->Bios );
// save console state
SaveCPUState( State->CPU );
SaveGPUState( State->GPU );
SaveSPUState( State->SPU );
SaveGamepadControllerState( State->GamepadController );
SaveOtherConsoleState( State->Others );
// saving should never fail
return true;
}
// =============================================================================
// DESERIALIZATION (LOAD CONSOLE STATE FROM BUFFER)
// =============================================================================
void LoadCPUState( const CPUState& State )
{
// all fields should be adjacent in memory
// so write registers and flags together
memcpy( Console.CPU.Registers, &State, sizeof(CPUState) );
}
// -----------------------------------------------------------------------------
bool LoadGPUState( const GPUState& State )
{
V32GPU& GPU = Console.GPU;
// write all registers as adjacent
memcpy( &GPU.Command, State.Registers, sizeof(State.Registers) );
// copy the BIOS texture
memcpy( &GPU.BiosTexture, &State.BiosTexture, sizeof(GPUTexture) );
// copy only the needed cartridge textures
unsigned TexturesSize = sizeof(GPUTexture) * GPU.LoadedCartridgeTextures;
memcpy( &GPU.CartridgeTextures[ 0 ], State.CartridgeTextures, TexturesSize );
// update GPU pointers for the loaded selections
if( GPU.SelectedTexture == -1 )
GPU.PointedTexture = &GPU.BiosTexture;
else
GPU.PointedTexture = &GPU.CartridgeTextures[ GPU.SelectedTexture ];
GPU.PointedRegion = &GPU.PointedTexture->Regions[ GPU.SelectedRegion ];
// reset any previous OpenGL errors
while( glGetError() != GL_NO_ERROR )
{
// (empty block instead of ";" to avoid warnings)
}
// make the needed updates in video output
Video.SelectTexture( GPU.SelectedTexture );
Video.SetMultiplyColor( GPU.MultiplyColor );
Video.SetBlendingMode( (IOPortValues)GPU.ActiveBlending );
// check for success
return (glGetError() == GL_NO_ERROR);
}
// -----------------------------------------------------------------------------
void LoadSPUState( const SPUState& State )
{
V32SPU& SPU = Console.SPU;
// write all registers as adjacent
memcpy( &SPU.Command, State.Registers, sizeof(State.Registers) );
// write all channels as adjacent
memcpy( &SPU.Channels, State.Channels, sizeof(State.Channels) );
// copy the BIOS sound
memcpy( &SPU.BiosSound, &State.BiosSound, sizeof(SPUSoundState) );
// copy only the needed cartridge sounds
// (size will stay the same, but speed will increase)
unsigned CartridgeSounds = SPU.LoadedCartridgeSounds;
// do not load data for all sounds as a block!!
// we must not overwrite the sample vector in each sound
for( unsigned SoundID = 0; SoundID < CartridgeSounds; SoundID++ )
memcpy( &SPU.CartridgeSounds[ SoundID ], &State.CartridgeSounds[ SoundID ], sizeof(SPUSoundState) );
// make the needed updates in audio objects
V32Word WordValue;
WordValue.AsInteger = SPU.SelectedSound;
SPU.WritePort( (int32_t)SPU_LocalPorts::SelectedSound, WordValue );
WordValue.AsInteger = SPU.SelectedChannel;
SPU.WritePort( (int32_t)SPU_LocalPorts::SelectedChannel, WordValue );
// update SPU pointers for the loaded selections
if( SPU.SelectedSound == -1 )
SPU.PointedSound = &SPU.BiosSound;
else
SPU.PointedSound = &SPU.CartridgeSounds[ SPU.SelectedSound ];
SPU.PointedChannel = &SPU.Channels[ SPU.SelectedChannel ];
}
// -----------------------------------------------------------------------------
void LoadGamepadControllerState( const GamepadControllerState& State )
{
// write the single exposed register
Console.GamepadController.SelectedGamepad = State.SelectedGamepad;
// write all gamepad states as adjacent
memcpy( Console.GamepadController.RealTimeGamepadStates, State.GamepadStates, sizeof(State.GamepadStates) );
}
// -----------------------------------------------------------------------------
void LoadOtherConsoleState( const OtherConsoleState& State )
{
// load state for minor chips
memcpy( &Console.Timer.CurrentDate, State.TimerRegisters, sizeof(State.TimerRegisters) );
Console.RNG.CurrentValue = State.RNGCurrentValue;
// load the full RAM
memcpy( &Console.RAM.Memory[ 0 ], State.RAM, sizeof(State.RAM) );
}
// -----------------------------------------------------------------------------
bool LoadState( const ConsoleState* State )
{
// try to identify the game and BIOS and see if they
// match current ones, to avoid loading incompatible states
ROMInfo CurrentGame, CurrentBios;
SaveGameInfo( CurrentGame );
SaveBiosInfo( CurrentBios );
if( memcmp( &State->Game, &CurrentGame, sizeof(ROMInfo) ) )
{
LOG( "ERROR: Cannot load saved state. Current cartridge is not the same one that was saved" );
return false;
}
if( memcmp( &State->Bios, &CurrentBios, sizeof(ROMInfo) ) )
{
LOG( "ERROR: Cannot load saved state. Current BIOS is not the same one that was used when saving" );
return false;
}
// load console state
// (first do stages that cannot fail)
LoadCPUState( State->CPU );
LoadSPUState( State->SPU );
LoadGamepadControllerState( State->GamepadController );
LoadOtherConsoleState( State->Others );
// now check for success at this stage
if( !LoadGPUState( State->GPU ) )
return false;
return true;
}