-
Notifications
You must be signed in to change notification settings - Fork 16
/
zstate.c
1446 lines (1237 loc) · 43 KB
/
zstate.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 1997-2008 ZSNES Team ( zsKnight, _Demo_, pagefault, Nach )
http://www.zsnes.com
http://sourceforge.net/projects/zsnes
https://zsnes.bountysource.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifdef __UNIXSDL__
#include "gblhdr.h"
#else
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#ifdef __WIN32__
#include <io.h>
#else
#include <unistd.h>
#endif
#endif
#include "asm_call.h"
#include "c_vcache.h"
#include "cfg.h"
#include "chips/c4proc.h"
#include "chips/dsp4emu.h"
#include "chips/msu1emu.h"
#include "chips/sa1regs.h"
#include "cpu/execute.h"
#include "cpu/regs.h"
#include "cpu/regsw.h"
#include "cpu/spc700.h"
#include "endmem.h"
#include "gblvars.h"
#include "gui/c_guiwindp.h"
#include "init.h"
#include "initc.h"
#include "input.h"
#include "ui.h"
#include "video/procvid.h"
#include "zmovie.h"
#include "zpath.h"
#include "zstate.h"
#include <stdarg.h>
#define NUMCONV_FR3
#define NUMCONV_FW3
#include "numconv.h"
#define clim()
#define stim()
void SA1UpdateDPageC(), unpackfunct(), repackfunct();
void PrepareOffset(), ResetOffset(), initpitch(), UpdateBanksSDD1();
void procexecloop();
void copy_spc7110_state_data(uint8_t**, void (*)(unsigned char**, void*, size_t), bool);
extern uint8_t intrset, cycpl, cycphb, xdbt, xpbt;
extern uint8_t xirqb, curnmi;
extern uint16_t stackand, stackor, xat, xst, xdt, xxt, xyt;
u4 Totalbyteloaded;
static void copy_snes_data(uint8_t** buffer, void (*copy_func)(uint8_t**, void*, size_t))
{
// 65816 status, etc.
copy_func(buffer, &curcyc, 1);
copy_func(buffer, &curypos, 2);
copy_func(buffer, &cacheud, 1);
copy_func(buffer, &ccud, 1);
copy_func(buffer, &intrset, 1);
copy_func(buffer, &cycpl, 1);
copy_func(buffer, &cycphb, 1);
copy_func(buffer, &spcon, 1);
copy_func(buffer, &stackand, 2);
copy_func(buffer, &stackor, 2);
copy_func(buffer, &xat, 2);
copy_func(buffer, &xdbt, 1);
copy_func(buffer, &xpbt, 1);
copy_func(buffer, &xst, 2);
copy_func(buffer, &xdt, 2);
copy_func(buffer, &xxt, 2);
copy_func(buffer, &xyt, 2);
copy_func(buffer, &xp, 1);
copy_func(buffer, &xe, 1);
copy_func(buffer, &xpc, 2);
copy_func(buffer, &xirqb, 1);
copy_func(buffer, &debugger, 1);
copy_func(buffer, &Curtableaddr, 4);
copy_func(buffer, &curnmi, 1);
// SPC Timers
copy_func(buffer, &cycpbl, 4);
copy_func(buffer, &cycpblt, 4);
// SNES PPU Register status
copy_func(buffer, &sndrot, 3019);
}
static void copy_spc_data(uint8_t** buffer, void (*copy_func)(uint8_t**, void*, size_t))
{
// SPC stuff, DSP stuff
copy_func(buffer, SPCRAM, PHspcsave);
copy_func(buffer, BRRBuffer, PHdspsave);
copy_func(buffer, &DSPMem, sizeof(DSPMem));
}
static void copy_extra_data(uint8_t** buffer, void (*copy_func)(uint8_t**, void*, size_t))
{
copy_func(buffer, &soundcycleft, 4);
copy_func(buffer, &curexecstate, 4);
copy_func(buffer, &nmiprevaddrl, 4);
copy_func(buffer, &nmiprevaddrh, 4);
copy_func(buffer, &nmirept, 4);
copy_func(buffer, &nmiprevline, 4);
copy_func(buffer, &nmistatus, 4);
copy_func(buffer, &joycontren, 4);
copy_func(buffer, &NextLineCache, 1);
copy_func(buffer, &spc700read, 10 * 4);
copy_func(buffer, &timer2upd, 4);
copy_func(buffer, &xa, 14 * 4);
copy_func(buffer, &spcnumread, 1);
copy_func(buffer, &opcd, 6 * 4);
copy_func(buffer, &HIRQCycNext, 4);
copy_func(buffer, &HIRQNextExe, 1);
copy_func(buffer, &oamaddr, 14 * 4);
copy_func(buffer, &prevoamptr, 1);
}
static size_t load_save_size;
enum copy_state_method { csm_save_zst_new,
csm_load_zst_new,
csm_load_zst_old,
csm_save_rewind,
csm_load_rewind };
static void copy_state_data(uint8_t* buffer, void (*copy_func)(uint8_t**, void*, size_t), enum copy_state_method method)
{
copy_snes_data(&buffer, copy_func);
// WRAM (128k), VRAM (64k)
copy_func(&buffer, wramdata, 8192 * 16);
copy_func(&buffer, vram, 4096 * 16);
if (spcon) {
copy_spc_data(&buffer, copy_func);
/*
if (buffer) //Rewind stuff
{
copy_func(&buffer, &echoon0, PHdspsave2);
}
*/
}
if (C4Enable) {
copy_func(&buffer, C4Ram, 2048 * 4);
}
if (SFXEnable) {
copy_func(&buffer, sfxramdata, 8192 * 16);
copy_func(&buffer, &SfxR0, PHnum2writesfxreg);
}
if (SA1Enable) {
copy_func(&buffer, &SA1Mode, PHnum2writesa1reg);
copy_func(&buffer, SA1RAMArea, 8192 * 16);
if (method != csm_load_zst_old) {
copy_func(&buffer, &SA1Status, 3);
copy_func(&buffer, &SA1xpc, 1 * 4);
copy_func(&buffer, &sa1dmaptr, 2 * 4);
}
}
if (DSP1Enable && (method != csm_load_zst_old)) {
copy_func(&buffer, &DSP1COp, 70 + 128);
copy_func(&buffer, &Op00Multiplicand, 3 * 4 + 128);
copy_func(&buffer, &Op10Coefficient, 4 * 4 + 128);
copy_func(&buffer, &Op04Angle, 4 * 4 + 128);
copy_func(&buffer, &Op08X, 5 * 4 + 128);
copy_func(&buffer, &Op18X, 5 * 4 + 128);
copy_func(&buffer, &Op28X, 4 * 4 + 128);
copy_func(&buffer, &Op0CA, 5 * 4 + 128);
copy_func(&buffer, &Op02FX, 11 * 4 + 3 * 4 + 28 * 8 + 128);
copy_func(&buffer, &Op0AVS, 5 * 4 + 14 * 8 + 128);
copy_func(&buffer, &Op06X, 6 * 4 + 10 * 8 + 4 + 128);
copy_func(&buffer, &Op01m, 4 * 4 + 128);
copy_func(&buffer, &Op0DX, 6 * 4 + 128);
copy_func(&buffer, &Op03F, 6 * 4 + 128);
copy_func(&buffer, &Op14Zr, 9 * 4 + 128);
copy_func(&buffer, &Op0EH, 4 * 4 + 128);
}
if (SETAEnable) {
copy_func(&buffer, setaramdata, 256 * 16);
// Todo: copy the SetaCmdEnable? For completeness we should do it
// but currently we ignore it anyway.
}
if (SPC7110Enable) {
copy_func(&buffer, &SPCMultA, PHnum2writespc7110reg);
copy_spc7110_state_data(&buffer, copy_func, (method == csm_load_zst_new) || (method == csm_load_rewind));
}
if (DSP4Enable) {
copy_func(&buffer, &DSP4.waiting4command, sizeof(DSP4.waiting4command));
copy_func(&buffer, &DSP4.half_command, sizeof(DSP4.half_command));
copy_func(&buffer, &DSP4.command, sizeof(DSP4.command));
copy_func(&buffer, &DSP4.in_count, sizeof(DSP4.in_count));
copy_func(&buffer, &DSP4.in_index, sizeof(DSP4.in_index));
copy_func(&buffer, &DSP4.out_count, sizeof(DSP4.out_count));
copy_func(&buffer, &DSP4.out_index, sizeof(DSP4.out_index));
copy_func(&buffer, &DSP4.parameters, sizeof(DSP4.parameters));
copy_func(&buffer, &DSP4.output, sizeof(DSP4.output));
copy_func(&buffer, &DSP4_vars.DSP4_Logic, sizeof(DSP4_vars.DSP4_Logic));
copy_func(&buffer, &DSP4_vars.lcv, sizeof(DSP4_vars.lcv));
copy_func(&buffer, &DSP4_vars.distance, sizeof(DSP4_vars.distance));
copy_func(&buffer, &DSP4_vars.raster, sizeof(DSP4_vars.raster));
copy_func(&buffer, &DSP4_vars.segments, sizeof(DSP4_vars.segments));
copy_func(&buffer, &DSP4_vars.world_x, sizeof(DSP4_vars.world_x));
copy_func(&buffer, &DSP4_vars.world_y, sizeof(DSP4_vars.world_y));
copy_func(&buffer, &DSP4_vars.world_dx, sizeof(DSP4_vars.world_dx));
copy_func(&buffer, &DSP4_vars.world_dy, sizeof(DSP4_vars.world_dy));
copy_func(&buffer, &DSP4_vars.world_ddx, sizeof(DSP4_vars.world_ddx));
copy_func(&buffer, &DSP4_vars.world_ddy, sizeof(DSP4_vars.world_ddy));
copy_func(&buffer, &DSP4_vars.world_xenv, sizeof(DSP4_vars.world_xenv));
copy_func(&buffer, &DSP4_vars.world_yofs, sizeof(DSP4_vars.world_yofs));
copy_func(&buffer, &DSP4_vars.view_x1, sizeof(DSP4_vars.view_x1));
copy_func(&buffer, &DSP4_vars.view_y1, sizeof(DSP4_vars.view_y1));
copy_func(&buffer, &DSP4_vars.view_x2, sizeof(DSP4_vars.view_x2));
copy_func(&buffer, &DSP4_vars.view_y2, sizeof(DSP4_vars.view_y2));
copy_func(&buffer, &DSP4_vars.view_dx, sizeof(DSP4_vars.view_dx));
copy_func(&buffer, &DSP4_vars.view_dy, sizeof(DSP4_vars.view_dy));
copy_func(&buffer, &DSP4_vars.view_xofs1, sizeof(DSP4_vars.view_xofs1));
copy_func(&buffer, &DSP4_vars.view_yofs1, sizeof(DSP4_vars.view_yofs1));
copy_func(&buffer, &DSP4_vars.view_xofs2, sizeof(DSP4_vars.view_xofs2));
copy_func(&buffer, &DSP4_vars.view_yofs2, sizeof(DSP4_vars.view_yofs2));
copy_func(&buffer, &DSP4_vars.view_yofsenv, sizeof(DSP4_vars.view_yofsenv));
copy_func(&buffer, &DSP4_vars.view_turnoff_x, sizeof(DSP4_vars.view_turnoff_x));
copy_func(&buffer, &DSP4_vars.view_turnoff_dx, sizeof(DSP4_vars.view_turnoff_dx));
copy_func(&buffer, &DSP4_vars.viewport_cx, sizeof(DSP4_vars.viewport_cx));
copy_func(&buffer, &DSP4_vars.viewport_cy, sizeof(DSP4_vars.viewport_cy));
copy_func(&buffer, &DSP4_vars.viewport_left, sizeof(DSP4_vars.viewport_left));
copy_func(&buffer, &DSP4_vars.viewport_right, sizeof(DSP4_vars.viewport_right));
copy_func(&buffer, &DSP4_vars.viewport_top, sizeof(DSP4_vars.viewport_top));
copy_func(&buffer, &DSP4_vars.viewport_bottom, sizeof(DSP4_vars.viewport_bottom));
copy_func(&buffer, &DSP4_vars.sprite_x, sizeof(DSP4_vars.sprite_x));
copy_func(&buffer, &DSP4_vars.sprite_y, sizeof(DSP4_vars.sprite_y));
copy_func(&buffer, &DSP4_vars.sprite_attr, sizeof(DSP4_vars.sprite_attr));
copy_func(&buffer, &DSP4_vars.sprite_size, sizeof(DSP4_vars.sprite_size));
copy_func(&buffer, &DSP4_vars.sprite_clipy, sizeof(DSP4_vars.sprite_clipy));
copy_func(&buffer, &DSP4_vars.sprite_count, sizeof(DSP4_vars.sprite_count));
copy_func(&buffer, &DSP4_vars.poly_clipLf, sizeof(DSP4_vars.poly_clipLf));
copy_func(&buffer, &DSP4_vars.poly_clipRt, sizeof(DSP4_vars.poly_clipRt));
copy_func(&buffer, &DSP4_vars.poly_ptr, sizeof(DSP4_vars.poly_ptr));
copy_func(&buffer, &DSP4_vars.poly_raster, sizeof(DSP4_vars.poly_raster));
copy_func(&buffer, &DSP4_vars.poly_top, sizeof(DSP4_vars.poly_top));
copy_func(&buffer, &DSP4_vars.poly_bottom, sizeof(DSP4_vars.poly_bottom));
copy_func(&buffer, &DSP4_vars.poly_cx, sizeof(DSP4_vars.poly_cx));
copy_func(&buffer, &DSP4_vars.poly_start, sizeof(DSP4_vars.poly_start));
copy_func(&buffer, &DSP4_vars.poly_plane, sizeof(DSP4_vars.poly_plane));
copy_func(&buffer, &DSP4_vars.OAM_attr, sizeof(DSP4_vars.OAM_attr));
copy_func(&buffer, &DSP4_vars.OAM_index, sizeof(DSP4_vars.OAM_index));
copy_func(&buffer, &DSP4_vars.OAM_bits, sizeof(DSP4_vars.OAM_bits));
copy_func(&buffer, &DSP4_vars.OAM_RowMax, sizeof(DSP4_vars.OAM_RowMax));
copy_func(&buffer, &DSP4_vars.OAM_Row, sizeof(DSP4_vars.OAM_Row));
}
if (MSUEnable) {
copy_func(&buffer, &MSU_Track_Position, sizeof(MSU_Track_Position));
copy_func(&buffer, &MSU_StatusRead, sizeof(MSU_StatusRead));
copy_func(&buffer, &MSU_MusicVolume, sizeof(MSU_MusicVolume));
}
if (method != csm_load_zst_old) {
copy_extra_data(&buffer, copy_func);
// We don't load SRAM from new states if box isn't checked
if ((method != csm_load_zst_new) || SRAMState) {
copy_func(&buffer, sram, ramsize);
}
if ((method == csm_save_rewind) || (method == csm_load_rewind)) {
copy_func(&buffer, &tempesi, 4);
copy_func(&buffer, &tempedi, 4);
copy_func(&buffer, &tempedx, 4);
copy_func(&buffer, &tempebp, 4);
}
}
}
static void memcpyinc(uint8_t** dest, void* src, size_t len)
{
memcpy(*dest, src, len);
*dest += len;
}
static void memcpyrinc(uint8_t** src, void* dest, size_t len)
{
memcpy(dest, *src, len);
*src += len;
}
extern uint32_t RewindTimer, DblRewTimer;
uint8_t* StateBackup = 0;
uint8_t AllocatedRewindStates, LatestRewindPos, EarliestRewindPos;
bool RewindPosPassed;
size_t rewind_state_size, cur_zst_size, old_zst_size;
void zmv_rewind_save(size_t, bool);
void zmv_rewind_load(size_t, bool);
void ClearCacheCheck()
{
memset(vidmemch2, 1, sizeof(vidmemch2));
memset(vidmemch4, 1, sizeof(vidmemch4));
memset(vidmemch8, 1, sizeof(vidmemch8));
}
// Code to handle special frames for pausing, and desync checking
uint8_t *SpecialPauseBackup = 0, PauseFrameMode = 0;
/*
Pause frame modes
0 - no pause frame stored
1 - pause frame ready to be stored
2 - pause frame stored
3 - pause frame ready for reload
*/
void BackupPauseFrame()
{
if (SpecialPauseBackup) {
copy_state_data(SpecialPauseBackup, memcpyinc, csm_save_rewind);
PauseFrameMode = 2;
}
}
void RestorePauseFrame()
{
if (SpecialPauseBackup) {
copy_state_data(SpecialPauseBackup, memcpyrinc, csm_load_rewind);
// ClearCacheCheck();
PauseFrameMode = 0;
}
}
void DeallocPauseFrame()
{
if (SpecialPauseBackup) {
free(SpecialPauseBackup);
}
}
#define ActualRewindFrames (uint32_t)(RewindFrames * (romispal ? 10 : 12))
void BackupCVFrame()
{
uint8_t* RewindBufferPos = StateBackup + LatestRewindPos * rewind_state_size;
if (MovieProcessing == MOVIE_PLAYBACK) {
zmv_rewind_save(LatestRewindPos, true);
} else if (MovieProcessing == MOVIE_RECORD) {
zmv_rewind_save(LatestRewindPos, false);
}
copy_state_data(RewindBufferPos, memcpyinc, csm_save_rewind);
if (RewindPosPassed) {
EarliestRewindPos = (EarliestRewindPos + 1) % AllocatedRewindStates;
RewindPosPassed = false;
}
// printf("Backing up in #%u, earliest: #%u, allocated: %u\n", LatestRewindPos, EarliestRewindPos, AllocatedRewindStates);
LatestRewindPos = (LatestRewindPos + 1) % AllocatedRewindStates;
if (LatestRewindPos == EarliestRewindPos) {
RewindPosPassed = true;
}
RewindTimer = ActualRewindFrames;
DblRewTimer += (DblRewTimer) ? 0 : ActualRewindFrames;
// printf("New backup slot: #%u, timer %u, check %u\n", LatestRewindPos, RewindTimer, DblRewTimer);
}
void RestoreCVFrame()
{
uint8_t* RewindBufferPos;
if (LatestRewindPos != ((EarliestRewindPos + 1) % AllocatedRewindStates)) {
if (DblRewTimer > ActualRewindFrames) {
if (LatestRewindPos == 1 || AllocatedRewindStates == 1) {
LatestRewindPos = AllocatedRewindStates - 1;
} else {
LatestRewindPos = (LatestRewindPos) ? LatestRewindPos - 2 : AllocatedRewindStates - 2;
}
} else {
LatestRewindPos = (LatestRewindPos) ? LatestRewindPos - 1 : AllocatedRewindStates - 1;
}
} else {
LatestRewindPos = EarliestRewindPos;
}
RewindBufferPos = StateBackup + LatestRewindPos * rewind_state_size;
// printf("Restoring from #%u, earliest: #%u\n", LatestRewindPos, EarliestRewindPos);
if (MovieProcessing == MOVIE_RECORD) {
zmv_rewind_load(LatestRewindPos, false);
} else {
if (MovieProcessing == MOVIE_PLAYBACK) {
zmv_rewind_load(LatestRewindPos, true);
}
if (PauseRewind || EMUPause) {
PauseFrameMode = EMUPause = true;
}
}
copy_state_data(RewindBufferPos, memcpyrinc, csm_load_rewind);
ClearCacheCheck();
LatestRewindPos = (LatestRewindPos + 1) % AllocatedRewindStates;
RewindTimer = ActualRewindFrames;
DblRewTimer = 2 * ActualRewindFrames;
}
void SetupRewindBuffer()
{
// For special rewind case to help out pauses
DeallocPauseFrame();
SpecialPauseBackup = malloc(rewind_state_size);
// For standard rewinds
if (StateBackup) {
free(StateBackup);
}
for (; RewindStates; RewindStates--) {
StateBackup = 0;
StateBackup = (uint8_t*)malloc(rewind_state_size * RewindStates);
if (StateBackup) {
break;
}
}
AllocatedRewindStates = RewindStates;
}
void DeallocRewindBuffer()
{
if (StateBackup) {
free(StateBackup);
}
}
static size_t state_size;
static void state_size_tally(uint8_t** dest, void* src, size_t len)
{
state_size += len;
}
void InitRewindVars()
{
uint8_t almost_useless_array[1]; // An array is needed for copy_state_data to give the correct size
state_size = 0;
copy_state_data(almost_useless_array, state_size_tally, csm_save_rewind);
rewind_state_size = state_size;
SetupRewindBuffer();
LatestRewindPos = 0;
EarliestRewindPos = 0;
RewindPosPassed = false;
RewindTimer = 1;
DblRewTimer = 1;
}
void InitRewindVarsForMovie()
{
LatestRewindPos = 0;
EarliestRewindPos = 0;
RewindPosPassed = false;
RewindTimer = 1;
DblRewTimer = 1;
}
// This is used to preserve system load state between game loads
static uint8_t* BackupSystemBuffer = 0;
void BackupSystemVars(void)
{
uint8_t* buffer;
if (!BackupSystemBuffer) {
state_size = 0;
copy_snes_data(&buffer, state_size_tally);
copy_spc_data(&buffer, state_size_tally);
copy_extra_data(&buffer, state_size_tally);
BackupSystemBuffer = (uint8_t*)malloc(state_size);
}
if (BackupSystemBuffer) {
buffer = BackupSystemBuffer;
copy_snes_data(&buffer, memcpyinc);
copy_spc_data(&buffer, memcpyinc);
copy_extra_data(&buffer, memcpyinc);
}
}
void RestoreSystemVars(void)
{
if (BackupSystemBuffer) {
uint8_t* buffer = BackupSystemBuffer;
InitRewindVars();
copy_snes_data(&buffer, memcpyrinc);
copy_spc_data(&buffer, memcpyrinc);
copy_extra_data(&buffer, memcpyrinc);
}
}
void DeallocSystemVars()
{
if (BackupSystemBuffer) {
free(BackupSystemBuffer);
}
}
extern uintptr_t Voice0BufPtr, Voice1BufPtr, Voice2BufPtr, Voice3BufPtr;
extern uintptr_t Voice4BufPtr, Voice5BufPtr, Voice6BufPtr, Voice7BufPtr;
void PrepareSaveState()
{
spcPCRam -= (uintptr_t)SPCRAM;
spcRamDP -= (uintptr_t)SPCRAM;
Voice0BufPtr -= (uintptr_t)spcBuffera;
Voice1BufPtr -= (uintptr_t)spcBuffera;
Voice2BufPtr -= (uintptr_t)spcBuffera;
Voice3BufPtr -= (uintptr_t)spcBuffera;
Voice4BufPtr -= (uintptr_t)spcBuffera;
Voice5BufPtr -= (uintptr_t)spcBuffera;
Voice6BufPtr -= (uintptr_t)spcBuffera;
Voice7BufPtr -= (uintptr_t)spcBuffera;
}
extern uintptr_t SA1Stat;
extern uint8_t IRAM[2049], *SA1Ptr, *SA1RegPCS, *CurBWPtr, *SA1BWPtr, *SNSBWPtr;
void SaveSA1()
{
SA1Stat &= 0xFFFFFF00;
SA1Ptr -= (uintptr_t)SA1RegPCS;
if (SA1RegPCS == IRAM) {
SA1Stat = (SA1Stat & 0xFFFFFF00) + 1;
}
if (SA1RegPCS == IRAM - 0x3000) {
SA1Stat = (SA1Stat & 0xFFFFFF00) + 2;
}
SA1RegPCS -= (uintptr_t)romdata;
CurBWPtr -= (uintptr_t)romdata;
SA1BWPtr -= (uintptr_t)romdata;
SNSBWPtr -= (uintptr_t)romdata;
}
void RestoreSA1()
{
SA1RegPCS += (uintptr_t)romdata;
CurBWPtr += (uintptr_t)romdata;
SA1BWPtr += (uintptr_t)romdata;
SNSBWPtr += (uintptr_t)romdata;
if ((SA1Stat & 0xFF) == 1) {
SA1RegPCS = IRAM;
}
if ((SA1Stat & 0xFF) == 2) {
SA1RegPCS = IRAM - 0x3000;
}
SA1Ptr += (uintptr_t)SA1RegPCS;
}
#define ResState(Voice_BufPtr) \
Voice_BufPtr += (uintptr_t)spcBuffera; \
if (Voice_BufPtr >= (uintptr_t)spcBuffera + 65536 * 4) { \
Voice_BufPtr = (uintptr_t)spcBuffera; \
}
void ResetState()
{
spcPCRam += (uintptr_t)SPCRAM;
spcRamDP += (uintptr_t)SPCRAM;
ResState(Voice0BufPtr);
ResState(Voice1BufPtr);
ResState(Voice2BufPtr);
ResState(Voice3BufPtr);
ResState(Voice4BufPtr);
ResState(Voice5BufPtr);
ResState(Voice6BufPtr);
ResState(Voice7BufPtr);
}
extern uint32_t SfxRomBuffer, SfxCROM;
extern uint32_t SfxLastRamAdr, SfxRAMMem;
static FILE* fhandle;
void CapturePicture();
static void write_save_state_data(uint8_t** dest, void* data, size_t len)
{
fwrite(data, 1, len, fhandle);
}
static const char zst_header_old[] = "ZSNES Save State File V0.6\x1a\x3c";
static const char zst_header_cur[] = "ZSNES Save State File V143\x1a\x8f";
void calculate_state_sizes()
{
state_size = 0;
copy_state_data(0, state_size_tally, csm_save_zst_new);
cur_zst_size = state_size + sizeof(zst_header_cur) - 1;
state_size = 0;
copy_state_data(0, state_size_tally, csm_load_zst_old);
old_zst_size = state_size + sizeof(zst_header_old) - 1;
}
uint32_t current_zst = 0;
uint32_t newest_zst = 0;
time_t newestfiledate;
char* zst_name()
{
static char buffer[7];
if ((MovieProcessing == MOVIE_PLAYBACK) || (MovieProcessing == MOVIE_RECORD)) {
sprintf(buffer, "%.2d.zst", (unsigned int)current_zst);
return (buffer);
}
strcpy(buffer, "zst");
if (current_zst) {
buffer[2] = (current_zst % 10) + '0';
if (current_zst > 9) {
buffer[1] = (current_zst / 10) + '0';
}
}
setextension(ZStateName, buffer);
return (ZStateName);
}
void zst_determine_newest(void)
{
struct stat filestat;
char* zst_path;
if (MovieInProgress()) {
mzt_chdir_up();
zst_path = ZMoviePath;
} else {
zst_path = ZSStatePath;
}
if (!stat_dir(zst_path, zst_name(), &filestat) && filestat.st_mtime > newestfiledate) {
newestfiledate = filestat.st_mtime;
newest_zst = current_zst;
}
if (MovieInProgress()) {
mzt_chdir_down();
}
}
void zst_init()
{
newestfiledate = 0;
if (LatestSave) {
for (current_zst = 0; current_zst < 100; current_zst++) {
zst_determine_newest();
}
current_zst = newest_zst;
zst_name();
}
}
int zst_exists(void)
{
int ret;
char* zst_path;
if (MovieInProgress()) {
mzt_chdir_up();
zst_path = ZMoviePath;
} else {
zst_path = ZSStatePath;
}
ret = access_dir(zst_path, zst_name(), F_OK) ? 0 : 1;
if (MovieInProgress()) {
mzt_chdir_down();
}
return (ret);
}
static bool zst_save_compressed(FILE* fp)
{
size_t data_size = cur_zst_size - (sizeof(zst_header_cur) - 1);
uint8_t* buffer = 0;
bool worked = false;
if ((buffer = (uint8_t*)malloc(data_size))) {
unsigned long compressed_size = compressBound(data_size);
uint8_t* compressed_buffer = 0;
if ((compressed_buffer = (uint8_t*)malloc(compressed_size))) {
copy_state_data(buffer, memcpyinc, csm_save_zst_new);
if (compress2(compressed_buffer, &compressed_size, buffer, data_size, Z_BEST_COMPRESSION) == Z_OK) {
fwrite3(compressed_size, fp);
fwrite(compressed_buffer, 1, compressed_size, fp);
worked = true;
}
free(compressed_buffer);
}
free(buffer);
}
if (!worked) // Compression failed for whatever reason
{
fwrite3(cur_zst_size | 0x00800000, fp); // Uncompressed ZST will never break 8MB
}
return (worked);
}
void zst_save(FILE* fp, bool Thumbnail, bool Compress)
{
PrepareOffset();
PrepareSaveState();
unpackfunct();
if (SFXEnable) {
SfxRomBuffer -= SfxCROM;
SfxLastRamAdr -= SfxRAMMem;
}
if (SA1Enable) {
SaveSA1(); // Convert SA-1 stuff to standard, non displacement format
}
if (!Compress || !zst_save_compressed(fp)) // If we don't want compressed or compression failed
{
fwrite(zst_header_cur, 1, sizeof(zst_header_cur) - 1, fp); //-1 for null
fhandle = fp; // Set global file handle
copy_state_data(0, write_save_state_data, csm_save_zst_new);
if (Thumbnail) {
CapturePicture();
fwrite(PrevPicture, 1, sizeof(PrevPicture), fp);
}
}
if (SFXEnable) {
SfxRomBuffer += SfxCROM;
SfxLastRamAdr += SfxRAMMem;
}
if (SA1Enable) {
RestoreSA1(); // Convert back SA-1 stuff
}
ResetOffset();
ResetState();
}
/*
Merges all the passed strings into buffer. Make sure to pass an extra parameter as 0 after all the strings.
Copies at most buffer_len characters. Result is always null terminated.
Returns how many bytes are needed to store all strings.
Thus if return is <= buffer_len, everything was copied.
*/
static size_t string_merge(char* buffer, size_t buffer_len, ...)
{
char* s;
size_t copied = 0, needed = 0;
va_list ap;
va_start(ap, buffer_len);
if (buffer && buffer_len) {
*buffer = 0;
}
while ((s = va_arg(ap, char*))) {
needed += strlen(s);
if (buffer && (copied + 1 < buffer_len)) {
strncpy(buffer + copied, s, buffer_len - copied);
buffer[buffer_len - 1] = 0;
copied += strlen(buffer + copied);
}
}
va_end(ap);
return (needed + 1);
}
static char txtmsg[30];
void set_state_message(char* prefix, char* suffix)
{
char num[3];
sprintf(num, "%d", (unsigned int)current_zst);
string_merge(txtmsg, sizeof(txtmsg), prefix, isextension(ZStateName, "zss") ? "AUTO" : num, suffix, 0);
Msgptr = txtmsg;
MessageOn = MsgCount;
}
void statesaver(void)
{
if (MovieProcessing == MOVIE_RECORD) {
//'Auto increment savestate slot' code
current_zst += AutoIncSaveSlot;
current_zst %= 100;
if (mzt_save(current_zst, !!cbitmode, false)) {
set_state_message("RR STATE ", " SAVED.");
} else {
current_zst += 100 - AutoIncSaveSlot;
current_zst %= 100;
}
return;
}
if ((MovieProcessing == MOVIE_PLAYBACK) || (MovieProcessing == MOVIE_DUMPING_NEW)) {
//'Auto increment savestate slot' code
current_zst += AutoIncSaveSlot;
current_zst %= 100;
if (mzt_save(current_zst, !!cbitmode, true)) {
set_state_message("RR STATE ", " SAVED.");
} else {
current_zst += 100 - AutoIncSaveSlot;
current_zst %= 100;
}
return;
}
clim();
//'Auto increment savestate slot' code
if (!isextension(ZStateName, "zss")) {
current_zst += (char)AutoIncSaveSlot;
current_zst %= 100;
zst_name();
}
if ((fhandle = fopen_dir(ZSStatePath, ZStateName, "wb"))) {
zst_save(fhandle, !!cbitmode, false);
fclose(fhandle);
// Display message onscreen, 'STATE XX SAVED.'
set_state_message("STATE ", " SAVED.");
} else {
// Display message onscreen, 'UNABLE TO SAVE.'
Msgptr = "UNABLE TO SAVE.";
MessageOn = MsgCount;
if (!isextension(ZStateName, "zss")) {
current_zst += 100 - (char)AutoIncSaveSlot;
current_zst %= 100;
zst_name();
}
}
stim();
}
extern uint32_t SfxMemTable[256], SfxCPB;
extern uint32_t SfxPBR, SfxROMBR, SfxRAMBR, SCBRrel, SfxSCBR;
extern uint8_t ioportval;
extern u1 nexthdma;
static void read_save_state_data(uint8_t** dest, void* data, size_t len)
{
load_save_size += fread(data, 1, len, fhandle);
}
static bool zst_load_compressed(FILE* fp, size_t compressed_size)
{
unsigned long data_size = cur_zst_size - (sizeof(zst_header_cur) - 1);
uint8_t* buffer = 0;
bool worked = false;
if ((buffer = (uint8_t*)malloc(data_size))) {
uint8_t* compressed_buffer = 0;
if ((compressed_buffer = (uint8_t*)malloc(compressed_size))) {
fread(compressed_buffer, 1, compressed_size, fp);
if (uncompress(buffer, &data_size, compressed_buffer, compressed_size) == Z_OK) {
copy_state_data(buffer, memcpyrinc, csm_load_zst_new);
worked = true;
}
free(compressed_buffer);
}
free(buffer);
}
return (worked);
}
bool zst_load(FILE* fp, size_t Compressed)
{
size_t zst_version = 0;
if (Compressed) {
if (!zst_load_compressed(fp, Compressed)) {
return (false);
}
} else {
char zst_header_check[sizeof(zst_header_cur) - 1];
Totalbyteloaded += fread(zst_header_check, 1, sizeof(zst_header_check), fp);
if (!memcmp(zst_header_check, zst_header_cur, sizeof(zst_header_check) - 2)) {
zst_version = 143; // v1.43+
}
if (!memcmp(zst_header_check, zst_header_old, sizeof(zst_header_check) - 2)) {
zst_version = 60; // v0.60 - v1.42
}
if (!zst_version) {
return (false);
} // Pre v0.60 saves are no longer loaded
load_save_size = 0;
fhandle = fp; // Set global file handle
copy_state_data(0, read_save_state_data, (zst_version == 143) ? csm_load_zst_new : csm_load_zst_old);
Totalbyteloaded += load_save_size;
}
if (SFXEnable) {
SfxCPB = SfxMemTable[(SfxPBR & 0xFF)];
SfxCROM = SfxMemTable[(SfxROMBR & 0xFF)];
SfxRAMMem = (uintptr_t)sfxramdata + ((SfxRAMBR & 0xFF) << 16);
SfxRomBuffer += SfxCROM;
SfxLastRamAdr += SfxRAMMem;
SCBRrel = (SfxSCBR << 10) + (uintptr_t)sfxramdata;
}
if (SA1Enable) {
RestoreSA1(); // Convert back SA-1 stuff
SA1UpdateDPageC();
}
if (SDD1Enable) {
UpdateBanksSDD1();
}
// Clear cache check if state loaded
ClearCacheCheck();
if (zst_version < 143) // Set new vars which old states did not have
{
prevoamptr = 0xFF;
ioportval = 0xFF;
spcnumread = 0;
}
if (MovieProcessing != MOVIE_RECORD) {
nexthdma = 0;
}
repackfunct();
initpitch();
ResetOffset();
ResetState();
procexecloop();
return (true);
}