forked from libretro/libretro-fceumm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfceu-state.c
349 lines (294 loc) · 7.85 KB
/
fceu-state.c
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#ifdef TARGET_GNW
#include "build/config.h"
#endif
#if !defined(TARGET_GNW) || (defined(TARGET_GNW) && defined(ENABLE_EMULATOR_NES) && FORCE_NOFRENDO == 0)
/* FCE Ultra - NES/Famicom Emulator
*
* Copyright notice for this file:
* Copyright (C) 2002 Xodnizel
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#endif
#include "fceu-types.h"
#include "x6502.h"
#include "fceu.h"
#include "fceu-sound.h"
#include "fceu-endian.h"
#include "fds.h"
#include "general.h"
#include "fceu-state.h"
#include "fceu-memory.h"
#include "ppu.h"
#include "video.h"
static void (*SPreSave)(void);
static void (*SPostSave)(void);
/* static int SaveStateStatus[10]; */
static SFORMAT SFMDATA[64];
int SFEXINDEX;
#define RLSB FCEUSTATE_RLSB /* 0x80000000 */
extern SFORMAT FCEUPPU_STATEINFO[];
extern SFORMAT FCEUSND_STATEINFO[];
extern SFORMAT FCEUCTRL_STATEINFO[];
SFORMAT SFCPU[] = {
{ &X.PC, 2 | RLSB, "PC\0" },
{ &X.A, 1, "A\0\0" },
{ &X.X, 1, "X\0\0" },
{ &X.Y, 1, "Y\0\0" },
{ &X.S, 1, "S\0\0" },
{ &X.P, 1, "P\0\0" },
{ &X.DB, 1, "DB"},
{ RAM, 0x800, "RAM" },
{ 0 }
};
SFORMAT SFCPUC[] = {
{ &X.jammed, 1, "JAMM" },
{ &X.IRQlow, 4 | RLSB, "IQLB" },
{ &X.tcount, 4 | RLSB, "ICoa" },
{ &X.count, 4 | RLSB, "ICou" },
{ ×tampbase, sizeof(timestampbase) | RLSB, "TSBS" },
{ &X.mooPI, 1, "MooP"},
{ 0 }
};
static int SubWrite(memstream_t *mem, SFORMAT *sf)
{
uint32 acc = 0;
while(sf->v)
{
if(sf->s == (~(uint32)0)) /* Link to another struct. */
{
uint32 tmp;
if(!(tmp = SubWrite(mem, (SFORMAT *)sf->v)))
return(0);
acc += tmp;
sf++;
continue;
}
acc += 8; /* Description + size */
acc += sf->s & (~RLSB);
if(mem) /* Are we writing or calculating the size of this block? */
{
memstream_write(mem, sf->desc, 4);
write32le_mem(sf->s & (~RLSB), mem);
#ifdef MSB_FIRST
if(sf->s & RLSB)
FlipByteOrder((uint8 *)sf->v, sf->s & (~RLSB));
#endif
memstream_write(mem, (char *)sf->v, sf->s & (~RLSB));
/* Now restore the original byte order. */
#ifdef MSB_FIRST
if(sf->s & RLSB)
FlipByteOrder((uint8 *)sf->v, sf->s & (~RLSB));
#endif
}
sf++;
}
return acc;
}
static int WriteStateChunk(memstream_t *mem, int type, SFORMAT *sf)
{
int bsize;
memstream_putc(mem, type);
bsize = SubWrite(0, sf);
write32le_mem(bsize, mem);
if (!SubWrite(mem, sf))
return 0;
return bsize + 5;
}
static SFORMAT *CheckS(SFORMAT *sf, uint32 tsize, char *desc)
{
while (sf->v)
{
if (sf->s == (~(uint32)0))
{ /* Link to another SFORMAT structure. */
SFORMAT *tmp;
if ((tmp = CheckS((SFORMAT*)sf->v, tsize, desc)))
return(tmp);
sf++;
continue;
}
if (!strncmp(desc, sf->desc, 4))
{
if (tsize != (sf->s & (~RLSB)))
return(0);
return(sf);
}
sf++;
}
return(0);
}
static int ReadStateChunk(memstream_t *mem, SFORMAT *sf, int size)
{
SFORMAT *tmp;
uint64 temp;
temp = memstream_pos(mem);
while(memstream_pos(mem) < (temp + size))
{
uint32 tsize;
char toa[4];
if(memstream_read(mem, toa, 4) <= 0)
return 0;
read32le_mem(&tsize, mem);
if((tmp = CheckS(sf, tsize, toa)))
{
memstream_read(mem, (char *)tmp->v, tmp->s & (~RLSB));
#ifdef MSB_FIRST
if(tmp->s & RLSB)
FlipByteOrder((uint8 *)tmp->v, tmp->s & (~RLSB));
#endif
}
else
memstream_seek(mem, tsize, SEEK_CUR);
}
return 1;
}
static int ReadStateChunks(memstream_t *st, int32 totalsize)
{
int t;
uint32 size;
int ret = 1;
while (totalsize > 0)
{
t = memstream_getc(st);
if (t == EOF)
break;
if (!read32le_mem(&size, st))
break;
totalsize -= size + 5;
switch(t)
{
case 1:
if (!ReadStateChunk(st, SFCPU, size))
ret = 0;
break;
case 2:
if (!ReadStateChunk(st, SFCPUC, size))
ret = 0;
/* else
X.mooPI = X.P; */ /* Quick and dirty hack. */
break;
case 3:
if (!ReadStateChunk(st, FCEUPPU_STATEINFO, size))
ret = 0;
break;
case 4:
if (!ReadStateChunk(st, FCEUCTRL_STATEINFO, size))
ret = 0;
break;
case 5:
if (!ReadStateChunk(st, FCEUSND_STATEINFO, size))
ret = 0;
break;
case 0x10:
if (!ReadStateChunk(st, SFMDATA, size))
ret = 0;
break;
default:
if (memstream_seek(st, size, SEEK_CUR) < 0)
goto endo;
break;
}
}
endo:
return ret;
}
void FCEUSS_Save_Mem(void)
{
memstream_t *mem = memstream_open(1);
uint32 totalsize;
uint8 header[16] = {0};
// Set to FF so it can be updated in flash later
memset(header,0xff,16);
header[0] = 'F';
header[1] = 'C';
header[2] = 'S';
header[3] = 0xFF;
FCEU_en32lsb(header + 8, FCEU_VERSION_NUMERIC);
memstream_write(mem, header, 16);
FCEUPPU_SaveState();
totalsize = WriteStateChunk(mem, 1, SFCPU);
totalsize += WriteStateChunk(mem, 2, SFCPUC);
totalsize += WriteStateChunk(mem, 3, FCEUPPU_STATEINFO);
totalsize += WriteStateChunk(mem, 4, FCEUCTRL_STATEINFO);
totalsize += WriteStateChunk(mem, 5, FCEUSND_STATEINFO);
if (SPreSave)
SPreSave();
totalsize += WriteStateChunk(mem, 0x10, SFMDATA);
if (SPreSave)
SPostSave();
memstream_seek(mem, 4, SEEK_SET);
write32le_mem(totalsize, mem);
memstream_close(mem);
}
void FCEUSS_Load_Mem(void)
{
memstream_t *mem = memstream_open(0);
uint8 header[16];
int stateversion;
int totalsize;
int x;
memstream_read(mem, header, 16);
if (memcmp(header, "FCS", 3) != 0)
return;
if (header[3] == 0xFF)
stateversion = FCEU_de32lsb(header + 8);
else
stateversion = header[3] * 100;
totalsize = FCEU_de32lsb(header + 4);
x = ReadStateChunks(mem, totalsize);
if (stateversion < 9500)
X.IRQlow = 0;
if (GameStateRestore)
GameStateRestore(stateversion);
if (x)
{
FCEUPPU_LoadState(stateversion);
FCEUSND_LoadState(stateversion);
}
memstream_close(mem);
}
void ResetExState(void (*PreSave)(void), void (*PostSave)(void))
{
SPreSave = PreSave;
SPostSave = PostSave;
SFEXINDEX = 0;
SFMDATA[0].s = 0;
}
void AddExState(void *v, uint32 s, int type, char *desc)
{
/* prevent adding a terminator to the list if a NULL pointer was provided */
if (v == NULL) return;
memset(SFMDATA[SFEXINDEX].desc, 0, sizeof(SFMDATA[SFEXINDEX].desc));
if (desc)
strncpy(SFMDATA[SFEXINDEX].desc, desc, sizeof(SFMDATA[SFEXINDEX].desc));
SFMDATA[SFEXINDEX].v = v;
SFMDATA[SFEXINDEX].s = s;
if (type)
SFMDATA[SFEXINDEX].s |= RLSB;
if (SFEXINDEX < 63)
SFEXINDEX++;
SFMDATA[SFEXINDEX].v = 0; /* End marker. */
}
void FCEU_DrawSaveStates(uint8 *XBuf)
{
}
#endif