forked from notaz/gpsp
-
Notifications
You must be signed in to change notification settings - Fork 53
/
savestate.c
197 lines (171 loc) · 5.23 KB
/
savestate.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
/* gameplaySP
*
* Copyright (C) 2023 David Guillen Fandos <david@davidgf.net>
*
* 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 "common.h"
const u8 *state_mem_read_ptr;
u8 *state_mem_write_ptr;
bool bson_contains_key(const u8 *srcp, const char *key, u8 keytype)
{
unsigned keyl = strlen(key) + 1;
unsigned doclen = bson_read_u32(srcp);
const u8* p = &srcp[4];
while (*p != 0 && (p - srcp) < doclen) {
u8 tp = *p;
unsigned tlen = strlen((char*)&p[1]) + 1;
if (keyl == tlen && !memcmp(key, &p[1], tlen))
return tp == keytype; // Found it, check type
p += 1 + tlen;
if (tp == BSON_TYPE_DOC || tp == BSON_TYPE_ARR)
p += bson_read_u32(p);
else if (tp == BSON_TYPE_BIN)
p += bson_read_u32(p) + 1 + 4;
else if (tp == BSON_TYPE_INT32)
p += 4;
}
return false;
}
const u8* bson_find_key(const u8 *srcp, const char *key)
{
unsigned keyl = strlen(key) + 1;
unsigned doclen = bson_read_u32(srcp);
const u8* p = &srcp[4];
while (*p != 0 && (p - srcp) < doclen) {
u8 tp = *p;
unsigned tlen = strlen((char*)&p[1]) + 1;
if (keyl == tlen && !memcmp(key, &p[1], tlen))
return &p[tlen + 1];
p += 1 + tlen;
if (tp == BSON_TYPE_DOC || tp == BSON_TYPE_ARR)
p += bson_read_u32(p);
else if (tp == BSON_TYPE_BIN)
p += bson_read_u32(p) + 1 + 4;
else if (tp == BSON_TYPE_INT32)
p += 4;
}
return NULL;
}
bool bson_read_int32(const u8 *srcp, const char *key, u32* value)
{
const u8* p = srcp ? bson_find_key(srcp, key) : NULL;
if (!p)
return false;
*value = bson_read_u32(p);
return true;
}
bool bson_read_int32_array(const u8 *srcp, const char *key, u32* value, unsigned cnt)
{
const u8* p = srcp ? bson_find_key(srcp, key) : NULL;
if (p) {
unsigned arrsz = bson_read_u32(p);
p += 4;
if (arrsz < 5)
return false;
arrsz = (arrsz - 5) >> 3;
while (arrsz--) {
p += 4; // type and name
*value++ = bson_read_u32(p);
p += 4; // value
}
return true;
}
*value = bson_read_u32(p);
return false;
}
bool bson_read_bytes(const u8 *srcp, const char *key, void* buffer, unsigned cnt)
{
const u8* p = srcp ? bson_find_key(srcp, key) : NULL;
if (p) {
unsigned bufsz = bson_read_u32(p);
if (bufsz != cnt)
return false;
// Skip byte array type and size
memcpy(buffer, &p[5], cnt);
return true;
}
return false;
}
bool gba_load_state(const void* src)
{
u32 i, tmp;
u8* srcptr = (u8*)src;
u32 docsize = bson_read_u32(srcptr);
if (docsize != GBA_STATE_MEM_SIZE)
return false;
if (!bson_read_int32(srcptr, "info-magic", &tmp) || tmp != GBA_STATE_MAGIC)
return false;
if (!bson_read_int32(srcptr, "info-version", &tmp) || tmp != GBA_STATE_VERSION)
return false;
// Validate that the state file makes sense before unconditionally reading it.
if (!cpu_check_savestate(srcptr) ||
!input_check_savestate(srcptr) ||
!main_check_savestate(srcptr) ||
!memory_check_savestate(srcptr) ||
!sound_check_savestate(srcptr))
return false;
if (!(cpu_read_savestate(srcptr) &&
input_read_savestate(srcptr) &&
main_read_savestate(srcptr) &&
memory_read_savestate(srcptr) &&
sound_read_savestate(srcptr)))
{
// TODO: this should not happen if the validation above is accurate.
return false;
}
// Generate converted palette (since it is not saved)
for(i = 0; i < 512; i++)
{
palette_ram_converted[i] = convert_palette(eswap16(palette_ram[i]));
}
video_reload_counters();
// Reset most of the frame state and dynarec state
#ifdef HAVE_DYNAREC
if (dynarec_enable)
flush_dynarec_caches();
#endif
instruction_count = 0;
reg[OAM_UPDATED] = 1;
return true;
}
void gba_save_state(void* dst)
{
u8 *stptr = (u8*)dst;
u8 *wrptr = (u8*)dst;
// Initial bson size
bson_write_u32(wrptr, 0);
// Add some info fields
bson_write_int32(wrptr, "info-magic", GBA_STATE_MAGIC);
bson_write_int32(wrptr, "info-version", GBA_STATE_VERSION);
wrptr += cpu_write_savestate(wrptr);
wrptr += input_write_savestate(wrptr);
wrptr += main_write_savestate(wrptr);
wrptr += memory_write_savestate(wrptr);
wrptr += sound_write_savestate(wrptr);
// The padding space is pushed into a padding field for easy parsing
{
unsigned padsize = GBA_STATE_MEM_SIZE - (wrptr - stptr);
padsize -= 1 + 9 + 4 + 1 + 1;
*wrptr++ = 0x05; // Byte array
bson_write_cstring(wrptr, "zpadding");
bson_write_u32(wrptr, padsize);
*wrptr++ = 0;
wrptr += padsize;
}
*wrptr++ = 0;
// Update the doc size
bson_write_u32(stptr, wrptr - stptr);
}