1 /*
2 * UAE - The Un*x Amiga Emulator
3 *
4 * Save/restore emulator state
5 *
6 * (c) 1999-2001 Toni Wilen
7 *
8 * see below for ASF-structure
9 */
10
11 /* Features:
12 *
13 * - full CPU state (68000/68010/68020/68030/68040/68060)
14 * - FPU (68881/68882/68040/68060)
15 * - full CIA-A and CIA-B state (with all internal registers)
16 * - saves all custom registers and audio internal state.
17 * - Chip, Bogo, Fast, Z3 and Picasso96 RAM supported
18 * - disk drive type, imagefile, track and motor state
19 * - Kickstart ROM version, address and size is saved. This data is not used during restore yet.
20 * - Action Replay state is saved
21 */
22
23 /* Notes:
24 *
25 * - blitter state is not saved, blitter is forced to finish immediately if it
26 * was active
27 * - disk DMA state is completely saved
28 * - does not ask for statefile name and description. Currently uses DF0's disk
29 * image name (".adf" is replaced with ".asf")
30 * - only Amiga state is restored, harddisk support, autoconfig, expansion boards etc..
31 * are not saved/restored (and probably never will).
32 * - use this for saving games that can't be saved to disk
33 */
34
35 /* Usage :
36 *
37 * save:
38 *
39 * set savestate_state = STATE_DOSAVE, savestate_filename = "..."
40 *
41 * restore:
42 *
43 * set savestate_state = STATE_DORESTORE, savestate_filename = "..."
44 *
45 */
46
47 #define OPEN_LOG 0
48
49 #include "sysconfig.h"
50 #include "sysdeps.h"
51
52 #include "options.h"
53 #include "memory_uae.h"
54 #include "zfile.h"
55 #include "ar.h"
56 #include "autoconf.h"
57 #include "custom.h"
58 #include "newcpu.h"
59 #include "filesys.h"
60 #include "savestate.h"
61 #include "uae.h"
62 #include "gui.h"
63 #include "audio.h"
64 #include "filesys.h"
65 #include "inputrecord.h"
66 #include "disk.h"
67 #include "misc.h"
68
69 int savestate_state = 0;
70 static int savestate_first_capture;
71
72 static bool new_blitter = false;
73
74 static int replaycounter;
75
76 struct zfile *savestate_file;
77 static int savestate_docompress, savestate_specialdump, savestate_nodialogs;
78
79 TCHAR savestate_fname[MAX_DPATH];
80
81 #define STATEFILE_ALLOC_SIZE 600000
82 static int statefile_alloc;
83 static int staterecords_max = 1000;
84 static int staterecords_first = 0;
85 static struct zfile *staterecord_statefile;
86 struct staterecord
87 {
88 int len;
89 int inuse;
90 uae_u8 *cpu;
91 uae_u8 *data;
92 uae_u8 *end;
93 int inprecoffset;
94 };
95
96 static struct staterecord **staterecords;
97
state_incompatible_warn(void)98 static void state_incompatible_warn (void)
99 {
100 static int warned;
101 int dowarn = 0;
102 int i;
103
104 #ifdef BSDSOCKET
105 if (currprefs.socket_emu)
106 dowarn = 1;
107 #endif
108 #ifdef UAESERIAL
109 if (currprefs.uaeserial)
110 dowarn = 1;
111 #endif
112 #ifdef SCSIEMU
113 if (currprefs.scsi)
114 dowarn = 1;
115 #endif
116 #ifdef CATWEASEL
117 if (currprefs.catweasel)
118 dowarn = 1;
119 #endif
120 #ifdef FILESYS
121 for(i = 0; i < currprefs.mountitems; i++) {
122 struct mountedinfo mi;
123 int type = get_filesys_unitconfig (&currprefs, i, &mi);
124 if (mi.ismounted && type != FILESYS_VIRTUAL && type != FILESYS_HARDFILE && type != FILESYS_HARDFILE_RDB)
125 dowarn = 1;
126 }
127 #endif
128 if (!warned && dowarn) {
129 warned = 1;
130 notify_user (NUMSG_STATEHD);
131 }
132 }
133
134 /* functions for reading/writing bytes, shorts and longs in big-endian
135 * format independent of host machine's endianess */
136
137 static uae_u8 *storepos;
save_store_pos_func(uae_u8 ** dstp)138 void save_store_pos_func (uae_u8 **dstp)
139 {
140 storepos = *dstp;
141 *dstp += 4;
142 }
save_store_size_func(uae_u8 ** dstp)143 void save_store_size_func (uae_u8 **dstp)
144 {
145 uae_u8 *p = storepos;
146 save_u32_func (&p, *dstp - storepos);
147 }
restore_store_pos_func(uae_u8 ** srcp)148 void restore_store_pos_func (uae_u8 **srcp)
149 {
150 storepos = *srcp;
151 *srcp += 4;
152 }
restore_store_size_func(uae_u8 ** srcp)153 void restore_store_size_func (uae_u8 **srcp)
154 {
155 uae_u8 *p = storepos;
156 uae_u32 len = restore_u32_func (&p);
157 *srcp = storepos + len;
158 }
159
save_u32_func(uae_u8 ** dstp,uae_u32 v)160 void save_u32_func (uae_u8 **dstp, uae_u32 v)
161 {
162 uae_u8 *dst = *dstp;
163 *dst++ = (uae_u8)(v >> 24);
164 *dst++ = (uae_u8)(v >> 16);
165 *dst++ = (uae_u8)(v >> 8);
166 *dst++ = (uae_u8)(v >> 0);
167 *dstp = dst;
168 }
save_u64_func(uae_u8 ** dstp,uae_u64 v)169 void save_u64_func (uae_u8 **dstp, uae_u64 v)
170 {
171 save_u32_func (dstp, (uae_u32)(v >> 32));
172 save_u32_func (dstp, (uae_u32)v);
173 }
save_u16_func(uae_u8 ** dstp,uae_u16 v)174 void save_u16_func (uae_u8 **dstp, uae_u16 v)
175 {
176 uae_u8 *dst = *dstp;
177 *dst++ = (uae_u8)(v >> 8);
178 *dst++ = (uae_u8)(v >> 0);
179 *dstp = dst;
180 }
save_u8_func(uae_u8 ** dstp,uae_u8 v)181 void save_u8_func (uae_u8 **dstp, uae_u8 v)
182 {
183 uae_u8 *dst = *dstp;
184 *dst++ = v;
185 *dstp = dst;
186 }
save_string_func(uae_u8 ** dstp,const TCHAR * from)187 void save_string_func (uae_u8 **dstp, const TCHAR *from)
188 {
189 uae_u8 *dst = *dstp;
190 char *s, *s2;
191 s2 = s = uutf8 (from);
192 while (s && *s)
193 *dst++ = *s++;
194 *dst++ = 0;
195 *dstp = dst;
196 xfree (s2);
197 }
save_path_func(uae_u8 ** dstp,const TCHAR * from,int type)198 void save_path_func (uae_u8 **dstp, const TCHAR *from, int type)
199 {
200 save_string_func (dstp, from);
201 }
202
restore_u32_func(uae_u8 ** dstp)203 uae_u32 restore_u32_func (uae_u8 **dstp)
204 {
205 uae_u32 v;
206 uae_u8 *dst = *dstp;
207 v = (dst[0] << 24) | (dst[1] << 16) | (dst[2] << 8) | (dst[3]);
208 *dstp = dst + 4;
209 return v;
210 }
restore_u64_func(uae_u8 ** dstp)211 uae_u64 restore_u64_func (uae_u8 **dstp)
212 {
213 uae_u64 v;
214
215 v = restore_u32_func (dstp);
216 v <<= 32;
217 v |= restore_u32_func (dstp);
218 return v;
219 }
restore_u16_func(uae_u8 ** dstp)220 uae_u16 restore_u16_func (uae_u8 **dstp)
221 {
222 uae_u16 v;
223 uae_u8 *dst = *dstp;
224 v=(dst[0] << 8) | (dst[1]);
225 *dstp = dst + 2;
226 return v;
227 }
restore_u8_func(uae_u8 ** dstp)228 uae_u8 restore_u8_func (uae_u8 **dstp)
229 {
230 uae_u8 v;
231 uae_u8 *dst = *dstp;
232 v = dst[0];
233 *dstp = dst + 1;
234 return v;
235 }
restore_string_func(uae_u8 ** dstp)236 TCHAR *restore_string_func (uae_u8 **dstp)
237 {
238 int len;
239 uae_u8 v;
240 uae_u8 *dst = *dstp;
241 char *top, *to;
242 TCHAR *s;
243
244 len = strlen ((char*)dst) + 1;
245 top = to = xmalloc (char, len);
246 do {
247 v = *dst++;
248 *top++ = v;
249 } while (v);
250 *dstp = dst;
251 s = utf8u (to);
252 xfree (to);
253 return s;
254 }
restore_path_func(uae_u8 ** dstp,int type)255 TCHAR *restore_path_func (uae_u8 **dstp, int type)
256 {
257 TCHAR *newpath;
258 TCHAR *s;
259 /// REMOVEME: nowhere used: TCHAR *out = NULL;
260 TCHAR tmp[MAX_DPATH], tmp2[MAX_DPATH];
261
262 s = restore_string_func (dstp);
263 if (s[0] == 0)
264 return s;
265 if (zfile_exists (s))
266 return s;
267 if (type == SAVESTATE_PATH_HD)
268 return s;
269 getfilepart (tmp, sizeof tmp / sizeof (TCHAR), s);
270 if (zfile_exists (tmp)) {
271 xfree (s);
272 return my_strdup (tmp);
273 }
274 for (int i = 0; i < MAX_PATHS; i++) {
275 newpath = NULL;
276 if (type == SAVESTATE_PATH_FLOPPY)
277 newpath = currprefs.path_floppy.path[i];
278 else if (type == SAVESTATE_PATH_VDIR || type == SAVESTATE_PATH_HDF)
279 newpath = currprefs.path_hardfile.path[i];
280 else if (type == SAVESTATE_PATH_CD)
281 newpath = currprefs.path_cd.path[i];
282 if (newpath == NULL || newpath[0] == 0)
283 break;
284 _tcscpy (tmp2, newpath);
285 fixtrailing (tmp2);
286 _tcscat (tmp2, tmp);
287 //fullpath (tmp2, sizeof tmp2 / sizeof (TCHAR));
288 if (zfile_exists (tmp2)) {
289 xfree (s);
290 return my_strdup (tmp2);
291 }
292 }
293 getpathpart(tmp2, sizeof tmp2 / sizeof (TCHAR), savestate_fname);
294 _tcscat (tmp2, tmp);
295 if (zfile_exists (tmp2)) {
296 xfree (s);
297 return my_strdup (tmp2);
298 }
299 return s;
300 }
301
302 #ifdef SAVESTATE
303 /* read and write IFF-style hunks */
304
save_chunk(struct zfile * f,uae_u8 * chunk,size_t len,TCHAR * name,int compress)305 static void save_chunk (struct zfile *f, uae_u8 *chunk, size_t len, TCHAR *name, int compress)
306 {
307 uae_u8 tmp[8], *dst;
308 uae_u8 zero[4]= { 0, 0, 0, 0 };
309 uae_u32 flags;
310 size_t pos;
311 size_t chunklen, len2;
312 char *s;
313
314 if (!chunk)
315 return;
316
317 if (compress < 0) {
318 zfile_fwrite (chunk, 1, len, f);
319 return;
320 }
321
322 /* chunk name */
323 s = ua (name);
324 zfile_fwrite (s, 1, 4, f);
325 xfree (s);
326 pos = zfile_ftell (f);
327 /* chunk size */
328 dst = &tmp[0];
329 chunklen = len + 4 + 4 + 4;
330 save_u32 (chunklen);
331 zfile_fwrite (&tmp[0], 1, 4, f);
332 /* chunk flags */
333 flags = 0;
334 dst = &tmp[0];
335 save_u32 (flags | compress);
336 zfile_fwrite (&tmp[0], 1, 4, f);
337 /* chunk data */
338 if (compress) {
339 int tmplen = len;
340 size_t opos;
341 dst = &tmp[0];
342 save_u32 (len);
343 opos = zfile_ftell (f);
344 zfile_fwrite (&tmp[0], 1, 4, f);
345 len = zfile_zcompress (f, chunk, len);
346 if (len > 0) {
347 zfile_fseek (f, pos, SEEK_SET);
348 dst = &tmp[0];
349 save_u32 (len + 4 + 4 + 4 + 4);
350 zfile_fwrite (&tmp[0], 1, 4, f);
351 zfile_fseek (f, 0, SEEK_END);
352 } else {
353 len = tmplen;
354 compress = 0;
355 zfile_fseek (f, opos, SEEK_SET);
356 dst = &tmp[0];
357 save_u32 (flags);
358 zfile_fwrite (&tmp[0], 1, 4, f);
359 }
360 }
361 if (!compress)
362 zfile_fwrite (chunk, 1, len, f);
363 /* alignment */
364 len2 = 4 - (len & 3);
365 if (len2)
366 zfile_fwrite (zero, 1, len2, f);
367
368 write_log (_T("Chunk '%s' chunk size %d (%d)\n"), name, chunklen, len);
369 }
370
restore_chunk(struct zfile * f,TCHAR * name,size_t * len,size_t * totallen,size_t * filepos)371 static uae_u8 *restore_chunk (struct zfile *f, TCHAR *name, size_t *len, size_t *totallen, size_t *filepos)
372 {
373 uae_u8 tmp[6], dummy[4], *mem, *src;
374 uae_u32 flags;
375 int len2;
376
377 *totallen = 0;
378 /* chunk name */
379 zfile_fread (tmp, 1, 4, f);
380 tmp[4] = 0;
381 au_copy (name, 5, (char*)tmp);
382 /* chunk size */
383 zfile_fread (tmp, 1, 4, f);
384 src = tmp;
385 len2 = restore_u32 () - 4 - 4 - 4;
386 if (len2 < 0)
387 len2 = 0;
388 *len = len2;
389 if (len2 == 0) {
390 *filepos = zfile_ftell (f);
391 return 0;
392 }
393
394 /* chunk flags */
395 zfile_fread (tmp, 1, 4, f);
396 src = tmp;
397 flags = restore_u32 ();
398 *totallen = *len;
399 if (flags & 1) {
400 zfile_fread (tmp, 1, 4, f);
401 src = tmp;
402 *totallen = restore_u32 ();
403 *filepos = zfile_ftell (f) - 4 - 4 - 4;
404 len2 -= 4;
405 } else {
406 *filepos = zfile_ftell (f) - 4 - 4;
407 }
408 /* chunk data. RAM contents will be loaded during the reset phase,
409 no need to malloc multiple megabytes here. */
410 if (_tcscmp (name, _T("CRAM")) != 0
411 && _tcscmp (name, _T("BRAM")) != 0
412 && _tcscmp (name, _T("FRAM")) != 0
413 && _tcscmp (name, _T("ZRAM")) != 0
414 && _tcscmp (name, _T("ZCRM")) != 0
415 && _tcscmp (name, _T("PRAM")) != 0
416 && _tcscmp (name, _T("A3K1")) != 0
417 && _tcscmp (name, _T("A3K2")) != 0
418 && _tcscmp (name, _T("BORO")) != 0
419 )
420 {
421 /* extra bytes at the end needed to handle old statefiles that now have new fields */
422 mem = xcalloc (uae_u8, *totallen + 100);
423 if (!mem)
424 return NULL;
425 if (flags & 1) {
426 zfile_zuncompress (mem, *totallen, f, len2);
427 } else {
428 zfile_fread (mem, 1, len2, f);
429 }
430 } else {
431 mem = 0;
432 zfile_fseek (f, len2, SEEK_CUR);
433 }
434
435 /* alignment */
436 len2 = 4 - (len2 & 3);
437 if (len2)
438 zfile_fread (dummy, 1, len2, f);
439 return mem;
440 }
441
restore_ram(size_t filepos,uae_u8 * memory)442 void restore_ram (size_t filepos, uae_u8 *memory)
443 {
444 uae_u8 tmp[8];
445 uae_u8 *src = tmp;
446 int size, fullsize;
447 uae_u32 flags;
448
449 if (filepos == 0 || memory == NULL)
450 return;
451 zfile_fseek (savestate_file, filepos, SEEK_SET);
452 zfile_fread (tmp, 1, sizeof tmp, savestate_file);
453 size = restore_u32 ();
454 flags = restore_u32 ();
455 size -= 4 + 4 + 4;
456 if (flags & 1) {
457 zfile_fread (tmp, 1, 4, savestate_file);
458 src = tmp;
459 fullsize = restore_u32 ();
460 size -= 4;
461 zfile_zuncompress (memory, fullsize, savestate_file, size);
462 } else {
463 zfile_fread (memory, 1, size, savestate_file);
464 }
465 }
466
restore_log(uae_u8 * src)467 static uae_u8 *restore_log (uae_u8 *src)
468 {
469 #if OPEN_LOG > 0
470 TCHAR *s = utf8u (src);
471 write_log (_T("%s\n"), s);
472 xfree (s);
473 #endif
474 src += strlen ((char*)src) + 1;
475 return src;
476 }
477
restore_header(uae_u8 * src)478 static void restore_header (uae_u8 *src)
479 {
480 TCHAR *emuname, *emuversion, *description;
481
482 restore_u32 ();
483 emuname = restore_string ();
484 emuversion = restore_string ();
485 description = restore_string ();
486 write_log (_T("Saved with: '%s %s', description: '%s'\n"),
487 emuname, emuversion, description);
488 xfree (description);
489 xfree (emuversion);
490 xfree (emuname);
491 }
492
493 /* restore all subsystems */
494
restore_state(const TCHAR * filename)495 void restore_state (const TCHAR *filename)
496 {
497 struct zfile *f;
498 uae_u8 *chunk,*end;
499 TCHAR name[5];
500 size_t len, totallen;
501 size_t filepos, filesize;
502 int z3num;
503
504 chunk = 0;
505 f = zfile_fopen (filename, _T("rb"), ZFD_NORMAL);
506 if (!f)
507 goto error;
508 zfile_fseek (f, 0, SEEK_END);
509 filesize = zfile_ftell (f);
510 zfile_fseek (f, 0, SEEK_SET);
511 savestate_state = STATE_RESTORE;
512 savestate_init ();
513
514 chunk = restore_chunk (f, name, &len, &totallen, &filepos);
515 if (!chunk || _tcsncmp (name, _T("ASF "), 4)) {
516 write_log (_T("%s is not an AmigaStateFile\n"), filename);
517 gui_message ("Cannot restore state from '%s'.\nIt is not an AmigaStateFile.\n", filename);
518 goto error;
519 }
520 write_log (_T("STATERESTORE: '%s'\n"), filename);
521 config_changed = 1;
522 savestate_file = f;
523 restore_header (chunk);
524 xfree (chunk);
525 restore_cia_start ();
526 changed_prefs.bogomem_size = 0;
527 changed_prefs.chipmem_size = 0;
528 changed_prefs.fastmem_size = 0;
529 changed_prefs.z3fastmem_size = 0;
530 changed_prefs.z3fastmem2_size = 0;
531 changed_prefs.mbresmem_low_size = 0;
532 changed_prefs.mbresmem_high_size = 0;
533 z3num = 0;
534 for (;;) {
535 name[0] = 0;
536 chunk = end = restore_chunk (f, name, &len, &totallen, &filepos);
537 write_log (_T("Chunk '%s' size %d (%d)\n"), name, len, totallen);
538 if (!_tcscmp (name, _T("END "))) {
539 #ifdef _DEBUG
540 if (filesize > filepos + 8)
541 continue;
542 #endif
543 break;
544 }
545 if (!_tcscmp (name, _T("CRAM"))) {
546 restore_cram (totallen, filepos);
547 continue;
548 } else if (!_tcscmp (name, _T("BRAM"))) {
549 restore_bram (totallen, filepos);
550 continue;
551 } else if (!_tcscmp (name, _T("A3K1"))) {
552 restore_a3000lram (totallen, filepos);
553 continue;
554 } else if (!_tcscmp (name, _T("A3K2"))) {
555 restore_a3000hram (totallen, filepos);
556 continue;
557 #ifdef AUTOCONFIG
558 } else if (!_tcscmp (name, _T("FRAM"))) {
559 restore_fram (totallen, filepos);
560 continue;
561 } else if (!_tcscmp (name, _T("ZRAM"))) {
562 restore_zram (totallen, filepos, z3num++);
563 continue;
564 } else if (!_tcscmp (name, _T("ZCRM"))) {
565 restore_zram (totallen, filepos, -1);
566 continue;
567 } else if (!_tcscmp (name, _T("BORO"))) {
568 restore_bootrom (totallen, filepos);
569 continue;
570 #endif
571 #ifdef PICASSO96
572 } else if (!_tcscmp (name, _T("PRAM"))) {
573 restore_pram (totallen, filepos);
574 continue;
575 #endif
576 } else if (!_tcscmp (name, _T("CYCS"))) {
577 end = restore_cycles (chunk);
578 } else if (!_tcscmp (name, _T("CPU "))) {
579 end = restore_cpu (chunk);
580 } else if (!_tcscmp (name, _T("CPUX")))
581 end = restore_cpu_extra (chunk);
582 else if (!_tcscmp (name, _T("CPUT")))
583 end = restore_cpu_trace (chunk);
584 #ifdef FPUEMU
585 else if (!_tcscmp (name, _T("FPU ")))
586 end = restore_fpu (chunk);
587 #endif
588 #ifdef MMUEMU
589 else if (!_tcscmp (name, _T("MMU ")))
590 end = restore_mmu (chunk);
591 #endif
592 else if (!_tcscmp (name, _T("AGAC")))
593 end = restore_custom_agacolors (chunk);
594 else if (!_tcscmp (name, _T("SPR0")))
595 end = restore_custom_sprite (0, chunk);
596 else if (!_tcscmp (name, _T("SPR1")))
597 end = restore_custom_sprite (1, chunk);
598 else if (!_tcscmp (name, _T("SPR2")))
599 end = restore_custom_sprite (2, chunk);
600 else if (!_tcscmp (name, _T("SPR3")))
601 end = restore_custom_sprite (3, chunk);
602 else if (!_tcscmp (name, _T("SPR4")))
603 end = restore_custom_sprite (4, chunk);
604 else if (!_tcscmp (name, _T("SPR5")))
605 end = restore_custom_sprite (5, chunk);
606 else if (!_tcscmp (name, _T("SPR6")))
607 end = restore_custom_sprite (6, chunk);
608 else if (!_tcscmp (name, _T("SPR7")))
609 end = restore_custom_sprite (7, chunk);
610 else if (!_tcscmp (name, _T("CIAA")))
611 end = restore_cia (0, chunk);
612 else if (!_tcscmp (name, _T("CIAB")))
613 end = restore_cia (1, chunk);
614 else if (!_tcscmp (name, _T("CHIP")))
615 end = restore_custom (chunk);
616 else if (!_tcscmp (name, _T("CINP")))
617 end = restore_input (chunk);
618 else if (!_tcscmp (name, _T("CHPX")))
619 end = restore_custom_extra (chunk);
620 else if (!_tcscmp (name, _T("CHPD")))
621 end = restore_custom_event_delay (chunk);
622 else if (!_tcscmp (name, _T("AUD0")))
623 end = restore_audio (0, chunk);
624 else if (!_tcscmp (name, _T("AUD1")))
625 end = restore_audio (1, chunk);
626 else if (!_tcscmp (name, _T("AUD2")))
627 end = restore_audio (2, chunk);
628 else if (!_tcscmp (name, _T("AUD3")))
629 end = restore_audio (3, chunk);
630 else if (!_tcscmp (name, _T("BLIT")))
631 end = restore_blitter (chunk);
632 else if (!_tcscmp (name, _T("BLTX")))
633 end = restore_blitter_new (chunk);
634 else if (!_tcscmp (name, _T("DISK")))
635 end = restore_floppy (chunk);
636 else if (!_tcscmp (name, _T("DSK0")))
637 end = restore_disk (0, chunk);
638 else if (!_tcscmp (name, _T("DSK1")))
639 end = restore_disk (1, chunk);
640 else if (!_tcscmp (name, _T("DSK2")))
641 end = restore_disk (2, chunk);
642 else if (!_tcscmp (name, _T("DSK3")))
643 end = restore_disk (3, chunk);
644 else if (!_tcscmp (name, _T("DSD0")))
645 end = restore_disk2 (0, chunk);
646 else if (!_tcscmp (name, _T("DSD1")))
647 end = restore_disk2 (1, chunk);
648 else if (!_tcscmp (name, _T("DSD2")))
649 end = restore_disk2 (2, chunk);
650 else if (!_tcscmp (name, _T("DSD3")))
651 end = restore_disk2 (3, chunk);
652 else if (!_tcscmp (name, _T("KEYB")))
653 end = restore_keyboard (chunk);
654 #ifdef AUTOCONFIG
655 else if (!_tcscmp (name, _T("EXPA")))
656 end = restore_expansion (chunk);
657 #endif
658 else if (!_tcscmp (name, _T("ROM ")))
659 end = restore_rom (chunk);
660 #ifdef PICASSO96
661 else if (!_tcscmp (name, _T("P96 ")))
662 end = restore_p96 (chunk);
663 #endif
664 #ifdef ACTION_REPLAY
665 else if (!_tcscmp (name, _T("ACTR")))
666 end = restore_action_replay (chunk);
667 else if (!_tcscmp (name, _T("HRTM")))
668 end = restore_hrtmon (chunk);
669 #endif
670 #ifdef FILESYS
671 else if (!_tcscmp (name, _T("FSYS")))
672 end = restore_filesys (chunk);
673 else if (!_tcscmp (name, _T("FSYC")))
674 end = restore_filesys_common (chunk);
675 #endif
676 #ifdef CD32
677 else if (!_tcscmp (name, _T("CD32")))
678 end = restore_akiko (chunk);
679 #endif
680 #ifdef CDTV
681 else if (!_tcscmp (name, _T("CDTV")))
682 end = restore_cdtv (chunk);
683 else if (!_tcscmp (name, _T("DMAC")))
684 end = restore_cdtv_dmac (chunk);
685 #endif
686 #ifdef SCSI
687 else if (!_tcscmp (name, _T("DMC2")))
688 end = restore_scsi_dmac (chunk);
689 else if (!_tcscmp (name, _T("SCSI")))
690 end = restore_scsi_hd (chunk);
691 #endif
692 #ifdef GAYLE
693 else if (!_tcscmp (name, _T("GAYL")))
694 end = restore_gayle (chunk);
695 else if (!_tcscmp (name, _T("IDE ")))
696 end = restore_ide (chunk);
697 #endif
698 #ifdef CD32
699 else if (!_tcsncmp (name, _T("CDU"), 3))
700 end = restore_cd (name[3] - '0', chunk);
701 #endif
702 #ifdef A2065
703 else if (!_tcsncmp (name, _T("2065"), 4))
704 end = restore_a2065 (chunk);
705 #endif
706 else if (!_tcsncmp (name, _T("DMWP"), 4))
707 end = restore_debug_memwatch (chunk);
708
709 else if (!_tcscmp (name, _T("CONF")))
710 end = restore_configuration (chunk);
711 else if (!_tcscmp (name, _T("LOG ")))
712 end = restore_log (chunk);
713 else {
714 end = chunk + len;
715 write_log (_T("unknown chunk '%s' size %d bytes\n"), name, len);
716 }
717 if (end == NULL)
718 write_log (_T("Chunk '%s', size %d bytes was not accepted!\n"),
719 name, len);
720 else if (totallen != (size_t)(end - chunk) )
721 write_log (_T("Chunk '%s' total size %d bytes but read %d bytes!\n"),
722 name, totallen, end - chunk);
723 xfree (chunk);
724 }
725 // target_addtorecent (filename, 0);
726 return;
727
728 error:
729 savestate_state = 0;
730 savestate_file = 0;
731 xfree (chunk);
732 if (f)
733 zfile_fclose (f);
734 }
735
savestate_restore_finish(void)736 void savestate_restore_finish (void)
737 {
738 if (!isrestore ())
739 return;
740 zfile_fclose (savestate_file);
741 savestate_file = 0;
742 restore_cpu_finish ();
743 restore_audio_finish ();
744 restore_disk_finish ();
745 restore_blitter_finish ();
746 #ifdef CD32
747 restore_akiko_finish ();
748 #endif
749 #ifdef CDTV
750 restore_cdtv_finish ();
751 #endif
752 #ifdef PICASSO96
753 restore_p96_finish ();
754 #endif
755 #ifdef A2065
756 restore_a2065_finish ();
757 #endif
758 restore_cia_finish ();
759 restore_debug_memwatch_finish ();
760 savestate_state = 0;
761 init_hz_normal ();
762 audio_activate ();
763 }
764
765 /* 1=compressed,2=not compressed,3=ram dump,4=audio dump */
savestate_initsave(const TCHAR * filename,int mode,int nodialogs,bool save)766 void savestate_initsave (const TCHAR *filename, int mode, int nodialogs, bool save)
767 {
768 if (filename == NULL) {
769 savestate_fname[0] = 0;
770 savestate_docompress = 0;
771 savestate_specialdump = 0;
772 savestate_nodialogs = 0;
773 return;
774 }
775 _tcscpy (savestate_fname, filename);
776 savestate_docompress = (mode == 1) ? 1 : 0;
777 savestate_specialdump = (mode == 3) ? 1 : (mode == 4) ? 2 : 0;
778 savestate_nodialogs = nodialogs;
779 new_blitter = false;
780 if (save) {
781 savestate_free ();
782 inprec_close (true);
783 }
784 }
785
save_rams(struct zfile * f,int comp)786 static void save_rams (struct zfile *f, int comp)
787 {
788 uae_u8 *dst;
789 int len;
790
791 dst = save_cram (&len);
792 save_chunk (f, dst, len, _T("CRAM"), comp);
793 dst = save_bram (&len);
794 save_chunk (f, dst, len, _T("BRAM"), comp);
795 dst = save_a3000lram (&len);
796 save_chunk (f, dst, len, _T("A3K1"), comp);
797 dst = save_a3000hram (&len);
798 save_chunk (f, dst, len, _T("A3K2"), comp);
799 #ifdef AUTOCONFIG
800 dst = save_fram (&len);
801 save_chunk (f, dst, len, _T("FRAM"), comp);
802 dst = save_zram (&len, 0);
803 save_chunk (f, dst, len, _T("ZRAM"), comp);
804 dst = save_zram (&len, 1);
805 save_chunk (f, dst, len, _T("ZRAM"), comp);
806 dst = save_zram (&len, -1);
807 save_chunk (f, dst, len, _T("ZCRM"), comp);
808 dst = save_bootrom (&len);
809 save_chunk (f, dst, len, _T("BORO"), comp);
810 #endif
811 #ifdef PICASSO96
812 dst = save_pram (&len);
813 save_chunk (f, dst, len, _T("PRAM"), comp);
814 #endif
815 }
816
817 /* Save all subsystems */
818
save_state_internal(struct zfile * f,const TCHAR * description,int comp,bool savepath)819 static int save_state_internal (struct zfile *f, const TCHAR *description, int comp, bool savepath)
820 {
821 uae_u8 endhunk[] = { 'E', 'N', 'D', ' ', 0, 0, 0, 8 };
822 uae_u8 header[1000];
823 TCHAR tmp[100];
824 uae_u8 *dst;
825 TCHAR name[5];
826 int i, len;
827
828 write_log (_T("STATESAVE (%s):\n"), f ? zfile_getname (f) : _T("<internal>"));
829 dst = header;
830 save_u32 (0);
831 save_string (_T("UAE"));
832 _stprintf (tmp, _T("%d.%d.%d"), UAEMAJOR, UAEMINOR, UAESUBREV);
833 save_string (tmp);
834 save_string (description);
835 save_chunk (f, header, dst-header, _T("ASF "), 0);
836
837 dst = save_cycles (&len, 0);
838 save_chunk (f, dst, len, _T("CYCS"), 0);
839 xfree (dst);
840
841 dst = save_cpu (&len, 0);
842 save_chunk (f, dst, len, _T("CPU "), 0);
843 xfree (dst);
844
845 dst = save_cpu_extra (&len, 0);
846 save_chunk (f, dst, len, _T("CPUX"), 0);
847 xfree (dst);
848
849 dst = save_cpu_trace (&len, 0);
850 save_chunk (f, dst, len, _T("CPUT"), 0);
851 xfree (dst);
852
853 #ifdef FPUEMU
854 dst = save_fpu (&len,0 );
855 save_chunk (f, dst, len, _T("FPU "), 0);
856 xfree (dst);
857 #endif
858
859 #ifdef MMUEMU
860 dst = save_mmu (&len, 0);
861 save_chunk (f, dst, len, _T("MMU "), 0);
862 xfree (dst);
863 #endif
864
865 _tcscpy(name, _T("DSKx"));
866 for (i = 0; i < 4; i++) {
867 dst = save_disk (i, &len, 0, savepath);
868 if (dst) {
869 name[3] = i + '0';
870 save_chunk (f, dst, len, name, 0);
871 xfree (dst);
872 }
873 }
874 _tcscpy(name, _T("DSDx"));
875 for (i = 0; i < 4; i++) {
876 dst = save_disk2 (i, &len, 0);
877 if (dst) {
878 name[3] = i + '0';
879 save_chunk (f, dst, len, name, comp);
880 xfree (dst);
881 }
882 }
883
884
885 dst = save_floppy (&len, 0);
886 save_chunk (f, dst, len, _T("DISK"), 0);
887 xfree (dst);
888
889 dst = save_custom (&len, 0, 0);
890 save_chunk (f, dst, len, _T("CHIP"), 0);
891 xfree (dst);
892
893 dst = save_custom_extra (&len, 0);
894 save_chunk (f, dst, len, _T("CHPX"), 0);
895 xfree (dst);
896
897 dst = save_custom_event_delay (&len, 0);
898 save_chunk (f, dst, len, _T("CHPD"), 0);
899 xfree (dst);
900
901 dst = save_blitter_new (&len, 0);
902 save_chunk (f, dst, len, _T("BLTX"), 0);
903 xfree (dst);
904 if (new_blitter == false) {
905 dst = save_blitter (&len, 0);
906 save_chunk (f, dst, len, _T("BLIT"), 0);
907 xfree (dst);
908 }
909
910 dst = save_input (&len, 0);
911 save_chunk (f, dst, len, _T("CINP"), 0);
912 xfree (dst);
913
914 dst = save_custom_agacolors (&len, 0);
915 save_chunk (f, dst, len, _T("AGAC"), 0);
916 xfree (dst);
917
918 _tcscpy (name, _T("SPRx"));
919 for (i = 0; i < 8; i++) {
920 dst = save_custom_sprite (i, &len, 0);
921 name[3] = i + '0';
922 save_chunk (f, dst, len, name, 0);
923 xfree (dst);
924 }
925
926 _tcscpy (name, _T("AUDx"));
927 for (i = 0; i < 4; i++) {
928 dst = save_audio (i, &len, 0);
929 name[3] = i + '0';
930 save_chunk (f, dst, len, name, 0);
931 xfree (dst);
932 }
933
934 dst = save_cia (0, &len, 0);
935 save_chunk (f, dst, len, _T("CIAA"), 0);
936 xfree (dst);
937
938 dst = save_cia (1, &len, 0);
939 save_chunk (f, dst, len, _T("CIAB"), 0);
940 xfree (dst);
941
942 dst = save_keyboard (&len, NULL);
943 save_chunk (f, dst, len, _T("KEYB"), 0);
944 xfree (dst);
945
946 #ifdef AUTOCONFIG
947 dst = save_expansion (&len, 0);
948 save_chunk (f, dst, len, _T("EXPA"), 0);
949 #endif
950 #ifdef A2065
951 dst = save_a2065 (&len, NULL);
952 save_chunk (f, dst, len, _T("2065"), 0);
953 #endif
954 #ifdef PICASSO96
955 dst = save_p96 (&len, 0);
956 save_chunk (f, dst, len, _T("P96 "), 0);
957 #endif
958 save_rams (f, comp);
959
960 dst = save_rom (1, &len, 0);
961 do {
962 if (!dst)
963 break;
964 save_chunk (f, dst, len, _T("ROM "), 0);
965 xfree (dst);
966 } while ((dst = save_rom (0, &len, 0)));
967
968 #ifdef CD32
969 dst = save_akiko (&len, NULL);
970 save_chunk (f, dst, len, _T("CD32"), 0);
971 xfree (dst);
972 #endif
973 #ifdef CDTV
974 dst = save_cdtv (&len, NULL);
975 save_chunk (f, dst, len, _T("CDTV"), 0);
976 xfree (dst);
977 dst = save_cdtv_dmac (&len, NULL);
978 save_chunk (f, dst, len, _T("DMAC"), 0);
979 xfree (dst);
980 #endif
981
982 #ifdef ACTION_REPLAY
983 dst = save_action_replay (&len, NULL);
984 save_chunk (f, dst, len, _T("ACTR"), comp);
985 dst = save_hrtmon (&len, NULL);
986 save_chunk (f, dst, len, _T("HRTM"), comp);
987 #endif
988 #ifdef FILESYS
989 dst = save_filesys_common (&len);
990 if (dst) {
991 save_chunk (f, dst, len, _T("FSYC"), 0);
992 for (i = 0; i < nr_units (); i++) {
993 dst = save_filesys (i, &len);
994 if (dst) {
995 save_chunk (f, dst, len, _T("FSYS"), 0);
996 xfree (dst);
997 }
998 }
999 }
1000 #endif
1001 #ifdef GAYLE
1002 dst = save_gayle (&len, NULL);
1003 if (dst) {
1004 save_chunk (f, dst, len, _T("GAYL"), 0);
1005 xfree(dst);
1006 }
1007 for (i = 0; i < 4; i++) {
1008 dst = save_ide (i, &len, NULL);
1009 if (dst) {
1010 save_chunk (f, dst, len, _T("IDE "), 0);
1011 xfree (dst);
1012 }
1013 }
1014 #endif
1015 #ifdef CDTV
1016 for (i = 0; i < MAX_TOTAL_SCSI_DEVICES; i++) {
1017 dst = save_cd (i, &len);
1018 if (dst) {
1019 _stprintf (name, _T("CDU%d"), i);
1020 save_chunk (f, dst, len, name, 0);
1021 }
1022 }
1023 #endif
1024 dst = save_debug_memwatch (&len, NULL);
1025 if (dst) {
1026 save_chunk (f, dst, len, _T("DMWP"), 0);
1027 xfree(dst);
1028 }
1029
1030 /* add fake END tag, makes it easy to strip CONF and LOG hunks */
1031 /* move this if you want to use CONF or LOG hunks when restoring state */
1032 zfile_fwrite (endhunk, 1, 8, f);
1033
1034 dst = save_configuration (&len, false);
1035 if (dst) {
1036 save_chunk (f, dst, len, _T("CONF"), comp);
1037 xfree(dst);
1038 }
1039 len = 30000;
1040
1041 zfile_fwrite (endhunk, 1, 8, f);
1042
1043 return 1;
1044 }
1045
save_state(const TCHAR * filename,const TCHAR * description)1046 int save_state (const TCHAR *filename, const TCHAR *description)
1047 {
1048 struct zfile *f;
1049 int comp = savestate_docompress;
1050
1051 if (!savestate_specialdump && !savestate_nodialogs) {
1052 state_incompatible_warn ();
1053 if (!save_filesys_cando ()) {
1054 gui_message (_T("Filesystem active. Try again later."));
1055 return -1;
1056 }
1057 }
1058 new_blitter = false;
1059 savestate_nodialogs = 0;
1060 custom_prepare_savestate ();
1061 f = zfile_fopen (filename, _T("w+b"), 0);
1062 if (!f)
1063 return 0;
1064 if (savestate_specialdump) {
1065 size_t pos;
1066 if (savestate_specialdump == 2)
1067 write_wavheader (f, 0, 22050);
1068 pos = zfile_ftell (f);
1069 save_rams (f, -1);
1070 if (savestate_specialdump == 2) {
1071 int len, len2, i;
1072 uae_u8 *tmp;
1073 len = zfile_ftell (f) - pos;
1074 tmp = xmalloc (uae_u8, len);
1075 zfile_fseek(f, pos, SEEK_SET);
1076 len2 = zfile_fread (tmp, 1, len, f);
1077 for (i = 0; i < len2; i++)
1078 tmp[i] += 0x80;
1079 write_wavheader (f, len, 22050);
1080 zfile_fwrite (tmp, len2, 1, f);
1081 xfree (tmp);
1082 }
1083 zfile_fclose (f);
1084 return 1;
1085 }
1086 int v = save_state_internal (f, description, comp, true);
1087 if (v)
1088 write_log (_T("Save of '%s' complete\n"), filename);
1089 zfile_fclose (f);
1090 savestate_state = 0;
1091 return v;
1092 }
1093
savestate_quick(int slot,int save)1094 void savestate_quick (int slot, int save)
1095 {
1096 int i, len = _tcslen (savestate_fname);
1097 i = len - 1;
1098 while (i >= 0 && savestate_fname[i] != '_')
1099 i--;
1100 if (i < len - 6 || i <= 0) { /* "_?.uss" */
1101 i = len - 1;
1102 while (i >= 0 && savestate_fname[i] != '.')
1103 i--;
1104 if (i <= 0) {
1105 write_log (_T("savestate name skipped '%s'\n"), savestate_fname);
1106 return;
1107 }
1108 }
1109 _tcscpy (savestate_fname + i, _T(".uss"));
1110 if (slot > 0)
1111 _stprintf (savestate_fname + i, _T("_%d.uss"), slot);
1112 if (save) {
1113 write_log (_T("saving '%s'\n"), savestate_fname);
1114 savestate_docompress = 1;
1115 save_state (savestate_fname, _T(""));
1116 } else {
1117 if (!zfile_exists (savestate_fname)) {
1118 write_log (_T("staterestore, file '%s' not found\n"), savestate_fname);
1119 return;
1120 }
1121 savestate_state = STATE_DORESTORE;
1122 write_log (_T("staterestore starting '%s'\n"), savestate_fname);
1123 }
1124 }
1125
savestate_check(void)1126 bool savestate_check (void)
1127 {
1128 if (vpos == 0 && !savestate_state) {
1129 if (hsync_counter == 0 && input_play == INPREC_PLAY_NORMAL)
1130 savestate_memorysave ();
1131 savestate_capture (0);
1132 }
1133 if (savestate_state == STATE_DORESTORE) {
1134 savestate_state = STATE_RESTORE;
1135 return true;
1136 } else if (savestate_state == STATE_DOREWIND) {
1137 savestate_state = STATE_REWIND;
1138 return true;
1139 }
1140 return false;
1141 }
1142
1143 static int rewindmode;
1144
1145
canrewind(int pos)1146 static struct staterecord *canrewind (int pos)
1147 {
1148 if (pos < 0)
1149 pos += staterecords_max;
1150 if (!staterecords)
1151 return 0;
1152 if (staterecords[pos] == NULL)
1153 return NULL;
1154 if (staterecords[pos]->inuse == 0)
1155 return NULL;
1156 if ((pos + 1) % staterecords_max == staterecords_first)
1157 return NULL;
1158 return staterecords[pos];
1159 }
1160
savestate_dorewind(int pos)1161 int savestate_dorewind (int pos)
1162 {
1163 rewindmode = pos;
1164 if (pos < 0)
1165 pos = replaycounter - 1;
1166 if (canrewind (pos)) {
1167 savestate_state = STATE_DOREWIND;
1168 write_log (_T("dorewind %d (%010d/%03d) -> %d\n"), replaycounter - 1, hsync_counter, vsync_counter, pos);
1169 return 1;
1170 }
1171 return 0;
1172 }
1173
savestate_rewind(void)1174 void savestate_rewind (void)
1175 {
1176 uae_u32 len;
1177 int i, dummy;
1178 uae_u8 *p, *p2;
1179 struct staterecord *st;
1180 int pos;
1181 bool rewind = false;
1182
1183 if (hsync_counter % currprefs.statecapturerate <= 25 && rewindmode <= -2) {
1184 pos = replaycounter - 2;
1185 rewind = true;
1186 } else {
1187 pos = replaycounter - 1;
1188 }
1189 st = canrewind (pos);
1190 if (!st) {
1191 rewind = false;
1192 pos = replaycounter - 1;
1193 st = canrewind (pos);
1194 if (!st)
1195 return;
1196 }
1197 p = st->data;
1198 p2 = st->end;
1199 write_log (_T("rewinding %d -> %d\n"), replaycounter - 1, pos);
1200 hsync_counter = restore_u32_func (&p);
1201 vsync_counter = restore_u32_func (&p);
1202 p = restore_cpu (p);
1203 p = restore_cycles (p);
1204 p = restore_cpu_extra (p);
1205 if (restore_u32_func (&p))
1206 p = restore_cpu_trace (p);
1207 #ifdef FPUEMU
1208 if (restore_u32_func (&p))
1209 p = restore_fpu (p);
1210 #endif
1211 for (i = 0; i < 4; i++) {
1212 p = restore_disk (i, p);
1213 if (restore_u32_func (&p))
1214 p = restore_disk2 (i, p);
1215 }
1216 p = restore_floppy (p);
1217 p = restore_custom (p);
1218 p = restore_custom_extra (p);
1219 if (restore_u32_func (&p))
1220 p = restore_custom_event_delay (p);
1221 p = restore_blitter_new (p);
1222 p = restore_custom_agacolors (p);
1223 for (i = 0; i < 8; i++) {
1224 p = restore_custom_sprite (i, p);
1225 }
1226 for (i = 0; i < 4; i++) {
1227 p = restore_audio (i, p);
1228 }
1229 p = restore_cia (0, p);
1230 p = restore_cia (1, p);
1231 p = restore_keyboard (p);
1232 p = restore_inputstate (p);
1233 #ifdef AUTOCONFIG
1234 p = restore_expansion (p);
1235 #endif
1236 #ifdef PICASSO96
1237 if (restore_u32_func (&p))
1238 p = restore_p96 (p);
1239 #endif
1240 len = restore_u32_func (&p);
1241 memcpy (chipmemory, p, currprefs.chipmem_size > len ? len : currprefs.chipmem_size);
1242 p += len;
1243 len = restore_u32_func (&p);
1244 memcpy (save_bram (&dummy), p, currprefs.bogomem_size > len ? len : currprefs.bogomem_size);
1245 p += len;
1246 #ifdef AUTOCONFIG
1247 len = restore_u32_func (&p);
1248 memcpy (save_fram (&dummy), p, currprefs.fastmem_size > len ? len : currprefs.fastmem_size);
1249 p += len;
1250 len = restore_u32_func (&p);
1251 memcpy (save_zram (&dummy, 0), p, currprefs.z3fastmem_size > len ? len : currprefs.z3fastmem_size);
1252 p += len;
1253 #endif
1254 #ifdef ACTION_REPLAY
1255 if (restore_u32_func (&p))
1256 p = restore_action_replay (p);
1257 if (restore_u32_func (&p))
1258 p = restore_hrtmon (p);
1259 #endif
1260 #ifdef CD32
1261 if (restore_u32_func (&p))
1262 p = restore_akiko (p);
1263 #endif
1264 #ifdef CDTV
1265 if (restore_u32_func (&p))
1266 p = restore_cdtv (p);
1267 if (restore_u32_func (&p))
1268 p = restore_cdtv_dmac (p);
1269 #endif
1270 #ifdef SCSCI
1271 if (restore_u32_func (&p))
1272 p = restore_scsi_dmac (p);
1273 #endif
1274 #ifdef GAYLE
1275 if (restore_u32_func (&p))
1276 p = restore_gayle (p);
1277 for (i = 0; i < 4; i++) {
1278 if (restore_u32_func (&p))
1279 p = restore_ide (p);
1280 }
1281 #endif
1282 p += 4;
1283 if (p != p2) {
1284 gui_message (_T("reload failure, address mismatch %p != %p"), p, p2);
1285 uae_reset (0, 0);
1286 return;
1287 }
1288 inprec_setposition (st->inprecoffset, pos);
1289 write_log (_T("state %d restored. (%010d/%03d)\n"), pos, hsync_counter, vsync_counter);
1290 if (rewind) {
1291 replaycounter--;
1292 if (replaycounter < 0)
1293 replaycounter += staterecords_max;
1294 st = canrewind (replaycounter);
1295 st->inuse = 0;
1296 }
1297
1298 }
1299
1300 #define BS 10000
1301
bufcheck(struct staterecord * sr,uae_u8 * p,int len)1302 STATIC_INLINE int bufcheck (struct staterecord *sr, uae_u8 *p, int len)
1303 {
1304 if (p - sr->data + BS + len >= sr->len)
1305 return 1;
1306 return 0;
1307 }
1308
savestate_memorysave(void)1309 void savestate_memorysave (void)
1310 {
1311 new_blitter = true;
1312 // create real statefile in memory too for later saving
1313 zfile_fclose (staterecord_statefile);
1314 staterecord_statefile = zfile_fopen_empty (NULL, _T("statefile.inp.uss"), 0);
1315 if (staterecord_statefile)
1316 save_state_internal (staterecord_statefile, _T("rerecording"), 1, false);
1317 }
1318
savestate_capture(int force)1319 void savestate_capture (int force)
1320 {
1321 uae_u8 *p, *p2, *p3, *dst;
1322 int i, len, tlen, retrycnt;
1323 struct staterecord *st;
1324 bool firstcapture = false;
1325
1326 #ifdef FILESYS
1327 if (nr_units ())
1328 return;
1329 #endif
1330 if (!staterecords)
1331 return;
1332 if (!input_record)
1333 return;
1334 if (currprefs.statecapturerate && hsync_counter == 0 && input_record == INPREC_RECORD_START && savestate_first_capture > 0) {
1335 // first capture
1336 force = true;
1337 firstcapture = true;
1338 } else if (savestate_first_capture < 0) {
1339 force = true;
1340 firstcapture = false;
1341 }
1342 if (!force) {
1343 if (currprefs.statecapturerate <= 0)
1344 return;
1345 if (hsync_counter % currprefs.statecapturerate)
1346 return;
1347 }
1348 savestate_first_capture = false;
1349
1350 retrycnt = 0;
1351 retry2:
1352 st = staterecords[replaycounter];
1353 if (st == NULL) {
1354 st = (struct staterecord*)xmalloc (uae_u8, statefile_alloc);
1355 st->len = statefile_alloc;
1356 } else if (retrycnt > 0) {
1357 write_log (_T("realloc %d -> %d\n"), st->len, st->len + STATEFILE_ALLOC_SIZE);
1358 st->len += STATEFILE_ALLOC_SIZE;
1359 st = (struct staterecord*)xrealloc (uae_u8, st, st->len);
1360 }
1361 if (st->len > statefile_alloc)
1362 statefile_alloc = st->len;
1363 st->inuse = 0;
1364 st->data = (uae_u8*)(st + 1);
1365 staterecords[replaycounter] = st;
1366 retrycnt++;
1367 p = p2 = st->data;
1368 tlen = 0;
1369 save_u32_func (&p, hsync_counter);
1370 save_u32_func (&p, vsync_counter);
1371 tlen += 8;
1372
1373 if (bufcheck (st, p, 0))
1374 goto retry;
1375 st->cpu = p;
1376 save_cpu (&len, p);
1377 tlen += len;
1378 p += len;
1379
1380 if (bufcheck (st, p, 0))
1381 goto retry;
1382 save_cycles (&len, p);
1383 tlen += len;
1384 p += len;
1385
1386 if (bufcheck (st, p, 0))
1387 goto retry;
1388 save_cpu_extra (&len, p);
1389 tlen += len;
1390 p += len;
1391
1392 if (bufcheck (st, p, 0))
1393 goto retry;
1394 p3 = p;
1395 save_u32_func (&p, 0);
1396 tlen += 4;
1397 if (save_cpu_trace (&len, p)) {
1398 save_u32_func (&p3, 1);
1399 tlen += len;
1400 p += len;
1401 }
1402
1403 #ifdef FPUEMU
1404 if (bufcheck (st, p, 0))
1405 goto retry;
1406 p3 = p;
1407 save_u32_func (&p, 0);
1408 tlen += 4;
1409 if (save_fpu (&len, p)) {
1410 save_u32_func (&p3, 1);
1411 tlen += len;
1412 p += len;
1413 }
1414 #endif
1415 for (i = 0; i < 4; i++) {
1416 if (bufcheck (st, p, 0))
1417 goto retry;
1418 save_disk (i, &len, p, true);
1419 tlen += len;
1420 p += len;
1421 p3 = p;
1422 save_u32_func (&p, 0);
1423 tlen += 4;
1424 if (save_disk2 (i, &len, p)) {
1425 save_u32_func (&p3, 1);
1426 tlen += len;
1427 p += len;
1428 }
1429 }
1430
1431 if (bufcheck (st, p, 0))
1432 goto retry;
1433 save_floppy (&len, p);
1434 tlen += len;
1435 p += len;
1436
1437 if (bufcheck (st, p, 0))
1438 goto retry;
1439 save_custom (&len, p, 0);
1440 tlen += len;
1441 p += len;
1442
1443 if (bufcheck (st, p, 0))
1444 goto retry;
1445 save_custom_extra (&len, p);
1446 tlen += len;
1447 p += len;
1448
1449 if (bufcheck (st, p, 0))
1450 goto retry;
1451 p3 = p;
1452 save_u32_func (&p, 0);
1453 tlen += 4;
1454 if (save_custom_event_delay (&len, p)) {
1455 save_u32_func (&p3, 1);
1456 tlen += len;
1457 p += len;
1458 }
1459
1460 if (bufcheck (st, p, 0))
1461 goto retry;
1462 save_blitter_new (&len, p);
1463 tlen += len;
1464 p += len;
1465
1466 if (bufcheck (st, p, 0))
1467 goto retry;
1468 save_custom_agacolors (&len, p);
1469 tlen += len;
1470 p += len;
1471 for (i = 0; i < 8; i++) {
1472 if (bufcheck (st, p, 0))
1473 goto retry;
1474 save_custom_sprite (i, &len, p);
1475 tlen += len;
1476 p += len;
1477 }
1478
1479 for (i = 0; i < 4; i++) {
1480 if (bufcheck (st, p, 0))
1481 goto retry;
1482 save_audio (i, &len, p);
1483 tlen += len;
1484 p += len;
1485 }
1486
1487 if (bufcheck (st, p, len))
1488 goto retry;
1489 save_cia (0, &len, p);
1490 tlen += len;
1491 p += len;
1492
1493 if (bufcheck (st, p, len))
1494 goto retry;
1495 save_cia (1, &len, p);
1496 tlen += len;
1497 p += len;
1498
1499 if (bufcheck (st, p, len))
1500 goto retry;
1501 save_keyboard (&len, p);
1502 tlen += len;
1503 p += len;
1504
1505 if (bufcheck (st, p, len))
1506 goto retry;
1507 save_inputstate (&len, p);
1508 tlen += len;
1509 p += len;
1510
1511 #ifdef AUTOCONFIG
1512 if (bufcheck (st, p, len))
1513 goto retry;
1514 save_expansion (&len, p);
1515 tlen += len;
1516 p += len;
1517 #endif
1518
1519 #ifdef PICASSO96
1520 if (bufcheck (st, p, 0))
1521 goto retry;
1522 p3 = p;
1523 save_u32_func (&p, 0);
1524 tlen += 4;
1525 if (save_p96 (&len, p)) {
1526 save_u32_func (&p3, 1);
1527 tlen += len;
1528 p += len;
1529 }
1530 #endif
1531
1532 dst = save_cram (&len);
1533 if (bufcheck (st, p, len))
1534 goto retry;
1535 save_u32_func (&p, len);
1536 memcpy (p, dst, len);
1537 tlen += len + 4;
1538 p += len;
1539 dst = save_bram (&len);
1540 if (bufcheck (st, p, len))
1541 goto retry;
1542 save_u32_func (&p, len);
1543 memcpy (p, dst, len);
1544 tlen += len + 4;
1545 p += len;
1546 #ifdef AUTOCONFIG
1547 dst = save_fram (&len);
1548 if (bufcheck (st, p, len))
1549 goto retry;
1550 save_u32_func (&p, len);
1551 memcpy (p, dst, len);
1552 tlen += len + 4;
1553 p += len;
1554 dst = save_zram (&len, 0);
1555 if (bufcheck (st, p, len))
1556 goto retry;
1557 save_u32_func (&p, len);
1558 memcpy (p, dst, len);
1559 tlen += len + 4;
1560 p += len;
1561 #endif
1562 #ifdef ACTION_REPLAY
1563 if (bufcheck (st, p, 0))
1564 goto retry;
1565 p3 = p;
1566 save_u32_func (&p, 0);
1567 tlen += 4;
1568 if (save_action_replay (&len, p)) {
1569 save_u32_func (&p3, 1);
1570 tlen += len;
1571 p += len;
1572 }
1573 if (bufcheck (st, p, 0))
1574 goto retry;
1575 p3 = p;
1576 save_u32_func (&p, 0);
1577 tlen += 4;
1578 if (save_hrtmon (&len, p)) {
1579 save_u32_func (&p3, 1);
1580 tlen += len;
1581 p += len;
1582 }
1583 #endif
1584 #ifdef CD32
1585 if (bufcheck (st, p, 0))
1586 goto retry;
1587 p3 = p;
1588 save_u32_func (&p, 0);
1589 tlen += 4;
1590 if (save_akiko (&len, p)) {
1591 save_u32_func (&p3, 1);
1592 tlen += len;
1593 p += len;
1594 }
1595 #endif
1596 #ifdef CDTV
1597 if (bufcheck (st, p, 0))
1598 goto retry;
1599 p3 = p;
1600 save_u32_func (&p, 0);
1601 tlen += 4;
1602 if (save_cdtv (&len, p)) {
1603 save_u32_func (&p3, 1);
1604 tlen += len;
1605 p += len;
1606 }
1607 if (bufcheck (st, p, 0))
1608 goto retry;
1609 p3 = p;
1610 save_u32_func (&p, 0);
1611 tlen += 4;
1612 if (save_cdtv_dmac (&len, p)) {
1613 save_u32_func (&p3, 1);
1614 tlen += len;
1615 p += len;
1616 }
1617 #endif
1618 #ifdef SCSI
1619 if (bufcheck (st, p, 0))
1620 goto retry;
1621 p3 = p;
1622 save_u32_func (&p, 0);
1623 tlen += 4;
1624 if (save_scsi_dmac (&len, p)) {
1625 save_u32_func (&p3, 1);
1626 tlen += len;
1627 p += len;
1628 }
1629 #endif
1630 #ifdef GAYLE
1631 if (bufcheck (st, p, 0))
1632 goto retry;
1633 p3 = p;
1634 save_u32_func (&p, 0);
1635 tlen += 4;
1636 if (save_gayle (&len, p)) {
1637 save_u32_func (&p3, 1);
1638 tlen += len;
1639 p += len;
1640 }
1641 for (i = 0; i < 4; i++) {
1642 if (bufcheck (st, p, 0))
1643 goto retry;
1644 p3 = p;
1645 save_u32_func (&p, 0);
1646 tlen += 4;
1647 if (save_ide (i, &len, p)) {
1648 save_u32_func (&p3, 1);
1649 tlen += len;
1650 p += len;
1651 }
1652 }
1653 #endif
1654 save_u32_func (&p, tlen);
1655 st->end = p;
1656 st->inuse = 1;
1657 st->inprecoffset = inprec_getposition ();
1658
1659 replaycounter++;
1660 if (replaycounter >= staterecords_max)
1661 replaycounter -= staterecords_max;
1662 if (replaycounter == staterecords_first) {
1663 staterecords_first++;
1664 if (staterecords_first >= staterecords_max)
1665 staterecords_first -= staterecords_max;
1666 }
1667
1668 write_log (_T("state capture %d (%010d/%03d,%d/%d) (%d bytes, alloc %d)\n"),
1669 replaycounter, hsync_counter, vsync_counter,
1670 hsync_counter % current_maxvpos (), current_maxvpos (),
1671 st->end - st->data, statefile_alloc);
1672
1673 if (firstcapture) {
1674 savestate_memorysave ();
1675 input_record++;
1676 for (i = 0; i < 4; i++) {
1677 bool wp = true;
1678 DISK_validate_filename (&currprefs, currprefs.floppyslots[i].df, false, &wp, NULL, NULL);
1679 inprec_recorddiskchange (i, currprefs.floppyslots[i].df, wp);
1680 }
1681 input_record--;
1682 }
1683
1684
1685 return;
1686 retry:
1687 if (retrycnt < 10)
1688 goto retry2;
1689 write_log (_T("can't save, too small capture buffer or out of memory\n"));
1690 return;
1691 }
1692
savestate_free(void)1693 void savestate_free (void)
1694 {
1695 xfree (staterecords);
1696 staterecords = NULL;
1697 }
1698
savestate_capture_request(void)1699 void savestate_capture_request (void)
1700 {
1701 savestate_first_capture = -1;
1702 }
1703
savestate_init(void)1704 void savestate_init (void)
1705 {
1706 savestate_free ();
1707 replaycounter = 0;
1708 staterecords_max = currprefs.statecapturebuffersize;
1709 staterecords = xcalloc (struct staterecord*, staterecords_max);
1710 statefile_alloc = STATEFILE_ALLOC_SIZE;
1711 if (input_record && savestate_state != STATE_DORESTORE) {
1712 zfile_fclose (staterecord_statefile);
1713 staterecord_statefile = NULL;
1714 inprec_close (false);
1715 inprec_open (NULL, NULL);
1716 savestate_first_capture = 1;
1717 }
1718 }
1719
1720
statefile_save_recording(const TCHAR * filename)1721 void statefile_save_recording (const TCHAR *filename)
1722 {
1723 if (!staterecord_statefile)
1724 return;
1725 struct zfile *zf = zfile_fopen (filename, _T("wb"), 0);
1726 if (zf) {
1727 int len = zfile_size (staterecord_statefile);
1728 uae_u8 *data = zfile_getdata (staterecord_statefile, 0, len);
1729 zfile_fwrite (data, len, 1, zf);
1730 xfree (data);
1731 zfile_fclose (zf);
1732 write_log (_T("input statefile '%s' saved\n"), filename);
1733 }
1734 }
1735
1736
1737 /*
1738
1739 My (Toni Wilen <twilen@arabuusimiehet.com>)
1740 proposal for Amiga-emulators' state-save format
1741
1742 Feel free to comment...
1743
1744 This is very similar to IFF-fileformat
1745 Every hunk must end to 4 byte boundary,
1746 fill with zero bytes if needed
1747
1748 version 0.8
1749
1750 HUNK HEADER (beginning of every hunk)
1751
1752 hunk name (4 ascii-characters)
1753 hunk size (including header)
1754 hunk flags
1755
1756 bit 0 = chunk contents are compressed with zlib (maybe RAM chunks only?)
1757
1758 HEADER
1759
1760 "ASF " (AmigaStateFile)
1761
1762 statefile version
1763 emulator name ("uae", "fellow" etc..)
1764 emulator version string (example: "0.8.15")
1765 free user writable comment string
1766
1767 CPU
1768
1769 "CPU "
1770
1771 CPU model 4 (68000,68010,68020,68030,68040,68060)
1772 CPU typeflags bit 0=EC-model or not, bit 31 = clock rate included
1773 D0-D7 8*4=32
1774 A0-A6 7*4=32
1775 PC 4
1776 unused 4
1777 68000 prefetch (IRC) 2
1778 68000 prefetch (IR) 2
1779 USP 4
1780 ISP 4
1781 SR/CCR 2
1782 flags 4 (bit 0=CPU was HALTed)
1783
1784 CPU specific registers
1785
1786 68000: SR/CCR is last saved register
1787 68010: save also DFC,SFC and VBR
1788 68020: all 68010 registers and CAAR,CACR and MSP
1789 etc..
1790
1791 68010+:
1792
1793 DFC 4
1794 SFC 4
1795 VBR 4
1796
1797 68020+:
1798
1799 CAAR 4
1800 CACR 4
1801 MSP 4
1802
1803 68030+:
1804
1805 AC0 4
1806 AC1 4
1807 ACUSR 2
1808 TT0 4
1809 TT1 4
1810
1811 68040+:
1812
1813 ITT0 4
1814 ITT1 4
1815 DTT0 4
1816 DTT1 4
1817 TCR 4
1818 URP 4
1819 SRP 4
1820
1821 68060:
1822
1823 BUSCR 4
1824 PCR 4
1825
1826 All:
1827
1828 Clock in KHz 4 (only if bit 31 in flags)
1829 4 (spare, only if bit 31 in flags)
1830
1831
1832 FPU (only if used)
1833
1834 "FPU "
1835
1836 FPU model 4 (68881/68882/68040/68060)
1837 FPU typeflags 4 (bit 31 = clock rate included)
1838 FP0-FP7 4+4+2 (80 bits)
1839 FPCR 4
1840 FPSR 4
1841 FPIAR 4
1842
1843 Clock in KHz 4 (only if bit 31 in flags)
1844 4 (spare, only if bit 31 in flags)
1845
1846 MMU (when and if MMU is supported in future..)
1847
1848 "MMU "
1849
1850 MMU model 4 (68040)
1851 flags 4 (none defined yet)
1852
1853 CUSTOM CHIPS
1854
1855 "CHIP"
1856
1857 chipset flags 4 OCS=0,ECSAGNUS=1,ECSDENISE=2,AGA=4
1858 ECSAGNUS and ECSDENISE can be combined
1859
1860 DFF000-DFF1FF 352 (0x120 - 0x17f and 0x0a0 - 0xdf excluded)
1861
1862 sprite registers (0x120 - 0x17f) saved with SPRx chunks
1863 audio registers (0x0a0 - 0xdf) saved with AUDx chunks
1864
1865 AGA COLORS
1866
1867 "AGAC"
1868
1869 AGA color 8 banks * 32 registers *
1870 registers LONG (XRGB) = 1024
1871
1872 SPRITE
1873
1874 "SPR0" - "SPR7"
1875
1876
1877 SPRxPT 4
1878 SPRxPOS 2
1879 SPRxCTL 2
1880 SPRxDATA 2
1881 SPRxDATB 2
1882 AGA sprite DATA/DATB 3 * 2 * 2
1883 sprite "armed" status 1
1884
1885 sprites maybe armed in non-DMA mode
1886 use bit 0 only, other bits are reserved
1887
1888
1889 AUDIO
1890 "AUD0" "AUD1" "AUD2" "AUD3"
1891
1892 audio state 1
1893 machine mode
1894 AUDxVOL 1
1895 irq? 1
1896 data_written? 1
1897 internal AUDxLEN 2
1898 AUDxLEN 2
1899 internal AUDxPER 2
1900 AUDxPER 2
1901 internal AUDxLC 4
1902 AUDxLC 4
1903 evtime? 4
1904
1905 BLITTER
1906
1907 "BLIT"
1908
1909 internal blitter state
1910
1911 flags 4
1912 bit 0=blitter active
1913 bit 1=fill carry bit
1914 internal ahold 4
1915 internal bhold 4
1916 internal hsize 2
1917 internal vsize 2
1918
1919 CIA
1920
1921 "CIAA" and "CIAB"
1922
1923 BFE001-BFEF01 16*1 (CIAA)
1924 BFD000-BFDF00 16*1 (CIAB)
1925
1926 internal registers
1927
1928 IRQ mask (ICR) 1 BYTE
1929 timer latches 2 timers * 2 BYTES (LO/HI)
1930 latched tod 3 BYTES (LO/MED/HI)
1931 alarm 3 BYTES (LO/MED/HI)
1932 flags 1 BYTE
1933 bit 0=tod latched (read)
1934 bit 1=tod stopped (write)
1935 div10 counter 1 BYTE
1936
1937 FLOPPY DRIVES
1938
1939 "DSK0" "DSK1" "DSK2" "DSK3"
1940
1941 drive state
1942
1943 drive ID-word 4
1944 state 1 (bit 0: motor on, bit 1: drive disabled, bit 2: current id bit)
1945 rw-head track 1
1946 dskready 1
1947 id-mode 1 (ID mode bit number 0-31)
1948 floppy information
1949
1950 bits from 4
1951 beginning of track
1952 CRC of disk-image 4 (used during restore to check if image
1953 is correct)
1954 disk-image null-terminated
1955 file name
1956
1957 INTERNAL FLOPPY CONTROLLER STATUS
1958
1959 "DISK"
1960
1961 current DMA word 2
1962 DMA word bit offset 1
1963 WORDSYNC found 1 (no=0,yes=1)
1964 hpos of next bit 1
1965 DSKLENGTH status 0=off,1=written once,2=written twice
1966 unused 2
1967
1968 RAM SPACE
1969
1970 "xRAM" (CRAM = chip, BRAM = bogo, FRAM = fast, ZRAM = Z3, P96 = RTG RAM, A3K1/A3K2 = MB RAM)
1971
1972 start address 4 ("bank"=chip/slow/fast etc..)
1973 of RAM "bank"
1974 RAM "bank" size 4
1975 RAM flags 4 (bit 0 = zlib compressed)
1976 RAM "bank" contents
1977
1978 ROM SPACE
1979
1980 "ROM "
1981
1982 ROM start 4
1983 address
1984 size of ROM 4
1985 ROM type 4 KICK=0
1986 ROM flags 4
1987 ROM version 2
1988 ROM revision 2
1989 ROM CRC 4 see below
1990 ROM-image ID-string null terminated, see below
1991 path to rom image
1992 ROM contents (Not mandatory, use hunk size to check if
1993 this hunk contains ROM data or not)
1994
1995 Kickstart ROM:
1996 ID-string is "Kickstart x.x"
1997 ROM version: version in high word and revision in low word
1998 Kickstart ROM version and revision can be found from ROM start
1999 + 12 (version) and +14 (revision)
2000
2001 ROM version and CRC is only meant for emulator to automatically
2002 find correct image from its ROM-directory during state restore.
2003
2004 Usually saving ROM contents is not good idea.
2005
2006 ACTION REPLAY
2007
2008 "ACTR"
2009
2010 Model (1,2,3) 4
2011 path to rom image
2012 RAM space (depends on model)
2013 ROM CRC 4
2014
2015 "CDx "
2016
2017 Flags 4 (bit 0 = scsi, bit 1 = ide, bit 2 = image)
2018 Path (for example image file or drive letter)
2019
2020 END
2021 hunk "END " ends, remember hunk size 8!
2022
2023
2024 EMULATOR SPECIFIC HUNKS
2025
2026 Read only if "emulator name" in header is same as used emulator.
2027 Maybe useful for configuration?
2028
2029 misc:
2030
2031 - save only at position 0,0 before triggering VBLANK interrupt
2032 - all data must be saved in bigendian format
2033 - should we strip all paths from image file names?
2034
2035 */
2036
2037 #endif
2038