-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnapshot.c
247 lines (196 loc) · 6.43 KB
/
snapshot.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
/* snapshot.c: snapshot handling routines
Copyright (c) 1999-2004 Philip Kendall
$Id: snapshot.c 3703 2008-06-30 20:36:11Z pak21 $
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.
Author contact information:
E-mail: [email protected]
*/
#include <config.h>
#include <libspectrum.h>
#include "ay.h"
#include "disk/beta.h"
#include "disk/plusd.h"
#include "fuse.h"
#include "if2.h"
#include "joystick.h"
#include "machine.h"
#include "memory.h"
#include "module.h"
#include "scld.h"
#include "slt.h"
#include "snapshot.h"
#include "ui/ui.h"
#include "ula.h"
#include "utils.h"
#include "z80/z80.h"
#include "zxatasp.h"
#include "zxcf.h"
#ifdef PSP
/* 'filename' is used to pacify the file format determining routine */
int snapshot_read_file(const char *filename, FILE *fptr)
#else
int snapshot_read( const char *filename )
#endif
{
utils_file file;
libspectrum_snap *snap = libspectrum_snap_alloc();
int error;
#ifdef PSP
{
/* PSP version reads from an open and positioned file stream */
long initial_pos = ftell(fptr);
fseek(fptr, 0, SEEK_END);
file.length = ftell(fptr) - initial_pos; /* determine size of block */
fseek(fptr, initial_pos, SEEK_SET); /* regress to previous position */
if (!(file.buffer = malloc(file.length)))
error = 1;
else if ((error = (fread(file.buffer, file.length, 1, fptr) != 1)))
free(file.buffer);
}
#else
error = utils_read_file( filename, &file );
#endif
if( error ) { libspectrum_snap_free( snap ); return error; }
error = libspectrum_snap_read( snap, file.buffer, file.length,
LIBSPECTRUM_ID_UNKNOWN, filename );
if( error ) {
utils_close_file( &file ); libspectrum_snap_free( snap );
return error;
}
if( utils_close_file( &file ) ) {
libspectrum_snap_free( snap );
return 1;
}
error = snapshot_copy_from( snap );
if( error ) { libspectrum_snap_free( snap ); return error; }
error = libspectrum_snap_free( snap ); if( error ) return error;
return 0;
}
int
snapshot_read_buffer( const unsigned char *buffer, size_t length,
libspectrum_id_t type )
{
libspectrum_snap *snap = libspectrum_snap_alloc();
int error;
error = libspectrum_snap_read( snap, buffer, length, type, NULL );
if( error ) { libspectrum_snap_free( snap ); return error; }
error = snapshot_copy_from( snap );
if( error ) { libspectrum_snap_free( snap ); return error; }
error = libspectrum_snap_free( snap ); if( error ) return error;
return 0;
}
static int
try_fallback_machine( libspectrum_machine machine )
{
int error;
/* If we failed on a +3 or +3e snapshot, try falling back to +2A (in
case we were compiled without lib765) */
if( machine == LIBSPECTRUM_MACHINE_PLUS3 ||
machine == LIBSPECTRUM_MACHINE_PLUS3E ) {
error = machine_select( LIBSPECTRUM_MACHINE_PLUS2A );
if( error ) {
ui_error( UI_ERROR_ERROR,
"Loading a %s snapshot, but neither that nor %s available",
libspectrum_machine_name( machine ),
libspectrum_machine_name( LIBSPECTRUM_MACHINE_PLUS2A ) );
return 1;
} else {
ui_error( UI_ERROR_WARNING,
"Loading a %s snapshot, but that's not available. "
"Using %s instead",
libspectrum_machine_name( machine ),
libspectrum_machine_name( LIBSPECTRUM_MACHINE_PLUS2A ) );
}
} else { /* Not trying a +3 or +3e snapshot */
ui_error( UI_ERROR_ERROR,
"Loading a %s snapshot, but that's not available",
libspectrum_machine_name( machine ) );
}
return 0;
}
int
snapshot_copy_from( libspectrum_snap *snap )
{
int capabilities, error;
libspectrum_machine machine;
module_snapshot_enabled( snap );
machine = libspectrum_snap_machine( snap );
if( machine != machine_current->machine ) {
error = machine_select( machine );
if( error ) {
error = try_fallback_machine( machine ); if( error ) return error;
}
} else {
machine_reset( 0 );
}
capabilities = machine_current->capabilities;
module_snapshot_from( snap );
return 0;
}
#ifdef PSP
/* 'filename' is used to pacify the file format determining routine */
int snapshot_write_file(const char *filename, FILE *fptr)
#else
int snapshot_write( const char *filename )
#endif
{
libspectrum_id_t type;
libspectrum_class_t class;
libspectrum_snap *snap;
unsigned char *buffer; size_t length;
int flags;
int error;
/* Work out what sort of file we want from the filename; default to
.szx if we couldn't guess */
error = libspectrum_identify_file_with_class( &type, &class, filename, NULL,
0 );
if( error ) return error;
if( class != LIBSPECTRUM_CLASS_SNAPSHOT || type == LIBSPECTRUM_ID_UNKNOWN )
type = LIBSPECTRUM_ID_SNAPSHOT_SZX;
snap = libspectrum_snap_alloc();
error = snapshot_copy_to( snap );
if( error ) { libspectrum_snap_free( snap ); return error; }
flags = 0;
length = 0;
error = libspectrum_snap_write( &buffer, &length, &flags, snap, type,
fuse_creator, 0 );
if( error ) { libspectrum_snap_free( snap ); return error; }
if( flags & LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS ) {
ui_error(
UI_ERROR_WARNING,
"A large amount of information has been lost in conversion; the snapshot probably won't work"
);
} else if( flags & LIBSPECTRUM_FLAG_SNAPSHOT_MINOR_INFO_LOSS ) {
ui_error(
UI_ERROR_WARNING,
"Some information has been lost in conversion; the snapshot may not work"
);
}
error = libspectrum_snap_free( snap );
if( error ) { free( buffer ); return 1; }
#ifdef PSP
error = (fwrite(buffer, length, 1, fptr) != 1);
#else
error = utils_write_file( filename, buffer, length );
#endif
if( error ) { free( buffer ); return error; }
free( buffer );
return 0;
}
int
snapshot_copy_to( libspectrum_snap *snap )
{
libspectrum_snap_set_machine( snap, machine_current->machine );
module_snapshot_to( snap );
return 0;
}