Repository navigation
Expand file tree
/
Copy pathstate.cpp
More file actions
1023 lines (952 loc) · 30.5 KB
/
Copy pathstate.cpp
File metadata and controls
1023 lines (952 loc) · 30.5 KB
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
/*
* Save/Load state implementation for InfoNES on Pico.
*
* Design goals:
* - Deterministic restoration of CPU, PPU, APU, controller, timing and mapper state.
* - Correct handling of both CHR ROM and CHR RAM cartridges.
* - Minimal coupling: only serializes globals actually required to resume execution.
* - FatFs (ff.h) used instead of stdio.
*
* Important points:
* - PPURAM is always saved, even for CHR ROM (only name tables / palette are meaningful then).
* - Pattern table selection (BG / Sprite) is recomputed from PPU_R0 after load.
* - Bank indices are stored instead of raw pointers (portable across address relocation).
* - Mapper and APU offer optional variable‑size blobs via callback hooks.
* - Flags: bit0 in header = CHR RAM present (NesHeader.byVRomSize == 0).
*/
#include "state.h"
#include "InfoNES.h"
#include "InfoNES_System.h"
#include "InfoNES_Mapper.h"
#include "InfoNES_pAPU.h"
#include "K6502.h"
#include "ff.h"
#include <cstring>
#include <cstdio>
#include <memory>
#include "FrensHelpers.h"
/* -------- Extern declarations (core emulator globals) -------- */
// 6502 registers and timing
extern WORD PC;
extern BYTE SP;
extern BYTE F; // Processor status flags
extern BYTE A;
extern BYTE X;
extern BYTE Y;
extern BYTE IRQ_State; // Current asserted level (internal logic)
extern BYTE IRQ_Wiring; // Config / enable mask
extern BYTE NMI_State;
extern BYTE NMI_Wiring;
extern int g_wPassedClocks; // Total cycles elapsed
extern int g_wCurrentClocks; // Cycles consumed in current step/frame
// Memory regions
extern BYTE *RAM; // 2KB internal RAM
extern BYTE *SRAM; // Battery-backed (mapper dependent)
extern BYTE *PPURAM; // PPU address space for CHR RAM + name tables + palette
extern BYTE *SPRRAM; // Sprite (OAM) memory
extern BYTE *PPUBANK[16]; // 16 x 1KB PPU bank pointers
extern BYTE *ROM; // PRG ROM base
extern BYTE *ROMBANK[4]; // 4 x 8KB PRG bank pointers (typically 16KB/32KB mapped)
extern BYTE *VROM; // CHR ROM base (NULL if CHR RAM)
extern struct NesHeader_tag NesHeader;
extern WORD MapperNo;
extern BYTE ROM_Mirroring; // Current mirroring type
extern BYTE ROM_FourScr; // iNES header byte 6 bit 3
// PPU register/state
extern BYTE PPU_R0, PPU_R1, PPU_R2, PPU_R3, PPU_R7;
extern BYTE PPU_Scr_H_Byte; // Horizontal coarse scroll byte
extern BYTE PPU_Scr_H_Bit; // Horizontal fine scroll bits
extern WORD PPU_Addr; // VRAM address latch
extern WORD PPU_Temp; // Temp VRAM address (loopy register scratch)
extern WORD PPU_Increment; // VRAM increment (1 or 32)
extern WORD PPU_Scanline; // Current scanline
extern BYTE PPU_NameTableBank; // Base nametable bank index
extern WORD PPU_SP_Height; // Sprite height (8 or 16)
extern int SpriteJustHit; // Sprite zero hit latch
extern BYTE byVramWriteEnable; // 1 if CHR RAM writable
extern BYTE PPU_Latch_Flag; // Write toggle for $2005/$2006
extern BYTE PPU_UpDown_Clip; // Vertical clipping mode
extern BYTE FrameIRQ_Enable; // Mapper frame IRQ enable
extern WORD FrameStep; // Frame step counter
// Frame / rendering stats
extern WORD FrameSkip;
extern WORD FrameCnt;
extern WORD PalTable[32]; // Current palette (decoded to internal format)
// APU registers
extern BYTE APU_Reg[0x18];
//extern struct ApuEvent_t ApuEventQueue[APU_EVENT_MAX]; // not needed here
extern int cur_event;
extern WORD entertime;
// extern BYTE wave_buffers[5][735]; // Not needed here
extern BYTE ApuCtrl;
extern BYTE ApuCtrlNew;
extern int ApuQuality;
extern DWORD ApuPulseMagic;
extern DWORD ApuTriangleMagic;
extern DWORD ApuNoiseMagic;
extern unsigned int ApuSamplesPerSync16;
extern unsigned int ApuCyclesPerSample;
extern unsigned int ApuSampleRate;
extern DWORD ApuCycleRate;
extern BYTE ApuC1a,
ApuC1b, ApuC1c, ApuC1d;
extern BYTE *ApuC1Wave;
extern DWORD ApuC1Skip;
extern DWORD ApuC1Index;
extern int32_t ApuC1EnvPhase;
extern BYTE ApuC1EnvVol;
extern BYTE ApuC1Atl;
extern int32_t ApuC1SweepPhase;
extern DWORD ApuC1Freq;
extern BYTE ApuC2a, ApuC2b, ApuC2c, ApuC2d;
extern BYTE *ApuC2Wave;
extern DWORD ApuC2Skip;
extern DWORD ApuC2Index;
extern int32_t ApuC2EnvPhase;
extern BYTE ApuC2EnvVol;
extern BYTE ApuC2Atl;
extern int32_t ApuC2SweepPhase;
extern DWORD ApuC2Freq;
extern BYTE ApuC3a, ApuC3b, ApuC3c, ApuC3d;
extern DWORD ApuC3Skip;
extern DWORD ApuC3Index;
extern BYTE ApuC3Atl;
extern DWORD ApuC3Llc; /* Linear Length Counter */
extern bool ApuC3ReloadFlag;
extern BYTE ApuC4a, ApuC4b, ApuC4c, ApuC4d;
extern DWORD ApuC4Sr; /* Shift register */
extern DWORD ApuC4Fdc; /* Frequency divide counter */
extern DWORD ApuC4Skip;
extern DWORD ApuC4Index;
extern BYTE ApuC4Atl;
extern BYTE ApuC4EnvVol;
extern int32_t ApuC4EnvPhase;
extern BYTE ApuC5Reg[4];
extern BYTE ApuC5Enable;
extern BYTE ApuC5Looping;
extern BYTE ApuC5CurByte;
extern BYTE ApuC5DpcmValue;
extern int ApuC5Freq;
extern int ApuC5Phaseacc;
extern WORD ApuC5Address, ApuC5CacheAddr;
extern int ApuC5DmaLength, ApuC5CacheDmaLength;
// End of APU externs
// Controller latch/state
extern DWORD PAD1_Latch;
extern DWORD PAD2_Latch;
extern DWORD PAD_System;
extern DWORD PAD1_Bit;
extern DWORD PAD2_Bit;
// Mirroring function
extern void InfoNES_Mirroring(int nType);
/* -------- Optional mapper and APU blob hooks -------- */
extern "C"
{
// void Mapper_Save(void *&blob, size_t &size);
// int Mapper_Load(const void *blob, size_t size);
void pAPU_Save(void *&blob, size_t &size);
int pAPU_Load(const void *blob, size_t size);
}
// // Weak defaults (if mapper/APU do not implement custom serialization)
// __attribute__((weak)) void Mapper_Save(void *&blob, size_t &size)
// {
// blob = nullptr;
// size = 0;
// }
// __attribute__((weak)) int Mapper_Load(const void *blob, size_t size)
// {
// (void)blob;
// (void)size;
// return 0;
// }
__attribute__((weak)) void pAPU_Save(void *&blob, size_t &size)
{
blob = nullptr;
size = 0;
}
__attribute__((weak)) int pAPU_Load(const void *blob, size_t size)
{
(void)blob;
(void)size;
return 0;
}
/* -------- File format structures -------- */
#define SAVESTATEFILE_VERSION 1
// The file holds the whole $0000-$1FFF CPU window, as it did when RAM was
// allocated at that size; only the first RAM_SIZE bytes are real RAM.
#define RAM_STATE_SIZE 0x2000
static_assert(RAM_STATE_SIZE % RAM_SIZE == 0, "RAM mirrors must tile the state window");
#define SAVESTATE_FLAG_CHR_RAM 0x01
#define SAVESTATE_FLAG_REGION_SHIFT 1
#define SAVESTATE_FLAG_REGION_MASK (0x3u << SAVESTATE_FLAG_REGION_SHIFT)
#define SAVESTATE_FLAG_MAPPER_PRG_RAM 0x08 // MapperPrgRam follows the name table RAM
#define SAVESTATE_FLAG_DMC_IRQ 0x10 // ApuDmcIrq follows the APU blob
// Backwards compatibility: the old "PAL flag" was just bit 1, which now reads
// as INFONES_REGION_PAL=1 in the 2-bit region field. NTSC=0 (no flag) and
// Dendy=2 fit in bits 1..2 without colliding with old saves.
struct SaveHeader
{
char magic[8]; // "INFOST\1" magic identifier
uint32_t version; // Increment if layout changes
uint32_t mapperNo; // For compatibility validation
uint32_t flags; // bit0: CHR RAM present, bits1..2: region (0/1/2)
};
struct SaveCore
{
// CPU
WORD PC;
BYTE SP;
BYTE F;
BYTE A;
BYTE X;
BYTE Y;
BYTE IRQ_State;
BYTE IRQ_Wiring;
BYTE NMI_State;
BYTE NMI_Wiring;
int g_wPassedClocks;
int g_wCurrentClocks;
// PPU primary registers and dynamic state
BYTE PPU_R0, PPU_R1, PPU_R2, PPU_R3, PPU_R7;
WORD PPU_Addr, PPU_Temp;
WORD PPU_Increment;
WORD PPU_Scanline;
BYTE PPU_NameTableBank;
WORD PPU_SP_Height;
BYTE PPU_Scr_H_Byte;
BYTE PPU_Scr_H_Bit;
BYTE PPU_Latch_Flag;
BYTE PPU_UpDown_Clip;
BYTE FrameIRQ_Enable;
WORD FrameStep;
int SpriteJustHit;
// Timing / misc
WORD FrameSkip;
WORD FrameCnt;
// 1 when PPURAM[0x3F00..0x3F1F] holds the palette. Builds before the color
// palette setting wrote 0 here (the byte was the ChrBuf update flag) and
// stored palette writes made through $3F20-$3FFF only at that address.
BYTE palRamValid;
BYTE byVramWriteEnable;
BYTE ROM_Mirroring;
BYTE reserved0; // Padding/alignment
// Palette snapshot
WORD PalTable[32];
// PPU bank indices (1KB units) and PRG bank indices (8KB)
BYTE ppuBankIndex[16];
uint16_t prgBankIndex[4];
// APU register block
BYTE APU_Reg[0x18];
// APU externals snapshot
int cur_event;
WORD entertime;
BYTE ApuCtrl;
BYTE ApuCtrlNew;
int ApuQuality;
DWORD ApuPulseMagic;
DWORD ApuTriangleMagic;
DWORD ApuNoiseMagic;
unsigned int ApuSamplesPerSync16;
unsigned int ApuCyclesPerSample;
unsigned int ApuSampleRate;
DWORD ApuCycleRate;
BYTE ApuC1a, ApuC1b, ApuC1c, ApuC1d;
DWORD ApuC1Skip;
DWORD ApuC1Index;
int32_t ApuC1EnvPhase;
BYTE ApuC1EnvVol;
BYTE ApuC1Atl;
int32_t ApuC1SweepPhase;
DWORD ApuC1Freq;
BYTE ApuC2a, ApuC2b, ApuC2c, ApuC2d;
DWORD ApuC2Skip;
DWORD ApuC2Index;
int32_t ApuC2EnvPhase;
BYTE ApuC2EnvVol;
BYTE ApuC2Atl;
int32_t ApuC2SweepPhase;
DWORD ApuC2Freq;
BYTE ApuC3a, ApuC3b, ApuC3c, ApuC3d;
DWORD ApuC3Skip;
DWORD ApuC3Index;
BYTE ApuC3Atl;
DWORD ApuC3Llc;
bool ApuC3ReloadFlag;
BYTE ApuC4a, ApuC4b, ApuC4c, ApuC4d;
DWORD ApuC4Sr;
DWORD ApuC4Fdc;
DWORD ApuC4Skip;
DWORD ApuC4Index;
BYTE ApuC4Atl;
BYTE ApuC4EnvVol;
int32_t ApuC4EnvPhase;
BYTE ApuC5Reg[4];
BYTE ApuC5Enable;
BYTE ApuC5Looping;
BYTE ApuC5CurByte;
BYTE ApuC5DpcmValue;
int ApuC5Freq;
int ApuC5Phaseacc;
WORD ApuC5Address, ApuC5CacheAddr;
int ApuC5DmaLength, ApuC5CacheDmaLength;
// Controller latch + shift positions
DWORD PAD1_Latch;
DWORD PAD2_Latch;
DWORD PAD_System;
DWORD PAD1_Bit;
DWORD PAD2_Bit;
};
/* -------- Helpers -------- */
// Return the base pointer a PPUBANK slot is addressed from.
// Slots 0-7 are the pattern tables: CHR ROM, a mapper-owned CHR RAM buffer, or
// the CHR RAM the core keeps at the bottom of PPURAM.
// Slots 8-11 are the nametables: mapper-owned nametable RAM if the board has
// any (mapper 111 banks it), otherwise PPURAM. Slots 12-15 (the $3000 mirror
// and the palette area) are always PPURAM, so they must never be measured
// against the CHR or nametable base.
static inline BYTE *ppuBankBase(int slot)
{
if (slot >= 12)
return PPURAM;
if (slot >= 8)
return MapperNtRam ? MapperNtRam : PPURAM;
if (NesHeader.byVRomSize > 0)
return VROM;
if (MapperChrRam)
return MapperChrRam;
return PPURAM;
}
// True when the mapper's name table RAM has to travel in the state file on its
// own. Mappers 30 and 111 carve both their CHR RAM and their name table RAM out
// of one allocation, in which case MapperChrRam already covers it.
static inline bool mapperNtRamNeedsFile()
{
if (!MapperNtRam || !MapperNtRamSize)
return false;
if (MapperChrRam &&
MapperNtRam >= MapperChrRam &&
MapperNtRam + MapperNtRamSize <= MapperChrRam + MapperChrRamSize)
return false;
return true;
}
// Convert current PPUBANK / ROMBANK pointers to linear indices for serialization
static void fillBankIndices(SaveCore &c)
{
for (int i = 0; i < 16; i++)
c.ppuBankIndex[i] = (BYTE)((PPUBANK[i] - ppuBankBase(i)) / 0x400);
for (int i = 0; i < 4; i++)
c.prgBankIndex[i] = (uint16_t)((ROMBANK[i] - ROM) / 0x2000);
}
// Rebuild PPUBANK pointers from stored indices
static void restorePPUBanks(const SaveCore &c)
{
for (int i = 0; i < 16; i++)
PPUBANK[i] = ppuBankBase(i) + c.ppuBankIndex[i] * 0x400;
}
// Rebuild PRG banks
static void restorePRGBanks(const SaveCore &c)
{
for (int i = 0; i < 4; i++)
ROMBANK[i] = ROM + c.prgBankIndex[i] * 0x2000;
}
/* -------- Public API: Save -------- */
int Emulator_SaveState(const char *path)
{
SaveHeader hdr{};
memcpy(hdr.magic, "INFOST\1", 8);
hdr.version = SAVESTATEFILE_VERSION;
hdr.mapperNo = MapperNo;
hdr.flags = 0;
if (NesHeader.byVRomSize == 0) hdr.flags |= SAVESTATE_FLAG_CHR_RAM;
hdr.flags |= ((uint32_t)InfoNES_GetRegion() << SAVESTATE_FLAG_REGION_SHIFT)
& SAVESTATE_FLAG_REGION_MASK;
if (MapperPrgRam) hdr.flags |= SAVESTATE_FLAG_MAPPER_PRG_RAM;
hdr.flags |= SAVESTATE_FLAG_DMC_IRQ;
struct SaveCore *coreDyn;
coreDyn = (struct SaveCore *)Frens::f_malloc(sizeof(SaveCore));
// Use dynamic allocation to avoid stack overflow on constrained systems
if (!coreDyn) {
printf("SaveState: out of memory for core data\n");
return -1;
}
// Initialize to zero to avoid uninitialized padding bytes
memset(coreDyn, 0, sizeof(SaveCore));
SaveCore &core = *coreDyn;
// CPU registers & timing
core.PC = PC;
core.SP = SP;
core.F = F;
core.A = A;
core.X = X;
core.Y = Y;
core.IRQ_State = IRQ_State;
core.IRQ_Wiring = IRQ_Wiring;
core.NMI_State = NMI_State;
core.NMI_Wiring = NMI_Wiring;
core.g_wPassedClocks = g_wPassedClocks;
core.g_wCurrentClocks = g_wCurrentClocks;
// PPU registers/state
core.PPU_R0 = PPU_R0;
core.PPU_R1 = PPU_R1;
core.PPU_R2 = PPU_R2;
core.PPU_R3 = PPU_R3;
core.PPU_R7 = PPU_R7;
core.PPU_Addr = PPU_Addr;
core.PPU_Temp = PPU_Temp;
core.PPU_Increment = PPU_Increment;
core.PPU_Scanline = PPU_Scanline;
core.PPU_NameTableBank = PPU_NameTableBank;
core.PPU_SP_Height = PPU_SP_Height;
core.PPU_Scr_H_Byte = PPU_Scr_H_Byte;
core.PPU_Scr_H_Bit = PPU_Scr_H_Bit;
core.PPU_Latch_Flag = PPU_Latch_Flag;
core.PPU_UpDown_Clip = PPU_UpDown_Clip;
core.FrameIRQ_Enable = FrameIRQ_Enable;
core.FrameStep = FrameStep;
core.SpriteJustHit = SpriteJustHit;
// Misc/frame
core.FrameSkip = FrameSkip;
core.FrameCnt = FrameCnt;
core.palRamValid = 1;
core.byVramWriteEnable = byVramWriteEnable;
core.ROM_Mirroring = ROM_Mirroring;
memcpy(core.PalTable, PalTable, sizeof core.PalTable);
memcpy(core.APU_Reg, APU_Reg, sizeof core.APU_Reg);
// APU externals
core.cur_event = cur_event;
core.entertime = entertime;
core.ApuCtrl = ApuCtrl;
core.ApuCtrlNew = ApuCtrlNew;
core.ApuQuality = ApuQuality;
core.ApuPulseMagic = ApuPulseMagic;
core.ApuTriangleMagic = ApuTriangleMagic;
core.ApuNoiseMagic = ApuNoiseMagic;
core.ApuSamplesPerSync16 = ApuSamplesPerSync16;
core.ApuCyclesPerSample = ApuCyclesPerSample;
core.ApuSampleRate = ApuSampleRate;
core.ApuCycleRate = ApuCycleRate;
core.ApuC1a = ApuC1a;
core.ApuC1b = ApuC1b;
core.ApuC1c = ApuC1c;
core.ApuC1d = ApuC1d;
core.ApuC1Skip = ApuC1Skip;
core.ApuC1Index = ApuC1Index;
core.ApuC1EnvPhase = ApuC1EnvPhase;
core.ApuC1EnvVol = ApuC1EnvVol;
core.ApuC1Atl = ApuC1Atl;
core.ApuC1SweepPhase = ApuC1SweepPhase;
core.ApuC1Freq = ApuC1Freq;
core.ApuC2a = ApuC2a;
core.ApuC2b = ApuC2b;
core.ApuC2c = ApuC2c;
core.ApuC2d = ApuC2d;
core.ApuC2Skip = ApuC2Skip;
core.ApuC2Index = ApuC2Index;
core.ApuC2EnvPhase = ApuC2EnvPhase;
core.ApuC2EnvVol = ApuC2EnvVol;
core.ApuC2Atl = ApuC2Atl;
core.ApuC2SweepPhase = ApuC2SweepPhase;
core.ApuC2Freq = ApuC2Freq;
core.ApuC3a = ApuC3a;
core.ApuC3b = ApuC3b;
core.ApuC3c = ApuC3c;
core.ApuC3d = ApuC3d;
core.ApuC3Skip = ApuC3Skip;
core.ApuC3Index = ApuC3Index;
core.ApuC3Atl = ApuC3Atl;
core.ApuC3Llc = ApuC3Llc;
core.ApuC3ReloadFlag = ApuC3ReloadFlag;
core.ApuC4a = ApuC4a;
core.ApuC4b = ApuC4b;
core.ApuC4c = ApuC4c;
core.ApuC4d = ApuC4d;
core.ApuC4Sr = ApuC4Sr;
core.ApuC4Fdc = ApuC4Fdc;
core.ApuC4Skip = ApuC4Skip;
core.ApuC4Index = ApuC4Index;
core.ApuC4Atl = ApuC4Atl;
core.ApuC4EnvVol = ApuC4EnvVol;
core.ApuC4EnvPhase = ApuC4EnvPhase;
memcpy(core.ApuC5Reg, ApuC5Reg, sizeof core.ApuC5Reg);
core.ApuC5Enable = ApuC5Enable;
core.ApuC5Looping = ApuC5Looping;
core.ApuC5CurByte = ApuC5CurByte;
core.ApuC5DpcmValue = ApuC5DpcmValue;
core.ApuC5Freq = ApuC5Freq;
core.ApuC5Phaseacc = ApuC5Phaseacc;
core.ApuC5Address = ApuC5Address;
core.ApuC5CacheAddr = ApuC5CacheAddr;
core.ApuC5DmaLength = ApuC5DmaLength;
core.ApuC5CacheDmaLength = ApuC5CacheDmaLength;
// Controller
core.PAD1_Latch = PAD1_Latch;
core.PAD2_Latch = PAD2_Latch;
core.PAD_System = PAD_System;
core.PAD1_Bit = PAD1_Bit;
core.PAD2_Bit = PAD2_Bit;
// Bank indices
fillBankIndices(core);
// Optional mapper / APU supplemental data
void *mapperBlob = nullptr;
size_t mapperSize = MapperBlobSize ? MapperBlobSize() : 0;
if (mapperSize > 0 && MapperSaveBlob) {
mapperBlob = Frens::f_malloc(mapperSize);
if (!mapperBlob) {
printf("SaveState: out of memory for mapper blob (%u bytes)\n", (unsigned)mapperSize);
Frens::f_free(coreDyn);
return -1;
}
MapperSaveBlob((BYTE *)mapperBlob);
// } else {
// Mapper_Save(mapperBlob, mapperSize);
}
void *apuBlob = nullptr;
size_t apuSize = 0;
pAPU_Save(apuBlob, apuSize);
// Open file (overwrite)
FIL fp;
if (f_open(&fp, path, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) {
printf("SaveState: failed to open file %s\n", path);
Frens::f_free(coreDyn);
if (mapperBlob) Frens::f_free(mapperBlob);
return -1;
}
auto w = [&](const void *buf, UINT len) -> bool
{
UINT bw;
return f_write(&fp, buf, len, &bw) == FR_OK && bw == len;
};
// RAM followed by its mirrors, filling the RAM_STATE_SIZE window.
auto wRam = [&]() -> bool
{
for (UINT ofs = 0; ofs < RAM_STATE_SIZE; ofs += RAM_SIZE)
if (!w(RAM, RAM_SIZE))
return false;
return true;
};
// Serialize header + core struct + primary RAM regions
if (!w(&hdr, sizeof hdr) ||
!w(&core, sizeof core) ||
!wRam() ||
!w(SPRRAM, SPRRAM_SIZE) ||
!w(SRAM, SRAM_SIZE))
{
f_close(&fp);
printf("SaveState: failed to write core data\n");
Frens::f_free(coreDyn);
if (mapperBlob) Frens::f_free(mapperBlob);
return -1;
}
Frens::f_free(coreDyn);
// Always save entire PPURAM (pattern for CHR RAM + nametables + palette area)
if (!w(PPURAM, PPURAM_SIZE))
{
f_close(&fp);
printf("SaveState: failed to write PPURAM\n");
if (mapperBlob) Frens::f_free(mapperBlob);
return -1;
}
// CHR RAM the mapper keeps outside PPURAM (mappers 13, 30, 85, 96). Written
// straight from the buffer so no second copy has to be allocated - an RP2040
// has no room for one.
if (MapperChrRam && !w(MapperChrRam, MapperChrRamSize))
{
f_close(&fp);
printf("SaveState: failed to write mapper CHR RAM\n");
if (mapperBlob) Frens::f_free(mapperBlob);
return -1;
}
// Name table RAM the mapper keeps outside PPURAM (mapper 111). Skipped when
// it is a sub-range of the CHR RAM just written - those boards have a single
// SRAM chip, and writing it twice would desync save from load.
if (mapperNtRamNeedsFile() && !w(MapperNtRam, MapperNtRamSize))
{
f_close(&fp);
printf("SaveState: failed to write mapper name table RAM\n");
if (mapperBlob) Frens::f_free(mapperBlob);
return -1;
}
// PRG RAM the mapper keeps outside SRAM (MMC5). Written straight from the
// buffer for the same reason as the CHR RAM above.
if (MapperPrgRam && !w(MapperPrgRam, MapperPrgRamSize))
{
f_close(&fp);
printf("SaveState: failed to write mapper PRG RAM\n");
if (mapperBlob) Frens::f_free(mapperBlob);
return -1;
}
// Mapper / APU blobs length + payload
if (!w(&mapperSize, sizeof mapperSize))
{
f_close(&fp);
printf("SaveState: failed to write mapper blob size\n");
if (mapperBlob) Frens::f_free(mapperBlob);
return -1;
}
if (mapperSize && !w(mapperBlob, mapperSize))
{
f_close(&fp);
printf("SaveState: failed to write mapper blob\n");
if (mapperBlob) Frens::f_free(mapperBlob);
return -1;
}
if (!w(&apuSize, sizeof apuSize))
{
f_close(&fp);
printf("SaveState: failed to write APU blob size\n");
if (mapperBlob) Frens::f_free(mapperBlob);
return -1;
}
if (apuSize && !w(apuBlob, apuSize))
{
f_close(&fp);
printf("SaveState: failed to write APU blob\n");
if (mapperBlob) Frens::f_free(mapperBlob);
return -1;
}
if (!w(&ApuDmcIrq, sizeof ApuDmcIrq))
{
f_close(&fp);
printf("SaveState: failed to write DMC IRQ state\n");
if (mapperBlob) Frens::f_free(mapperBlob);
return -1;
}
if (mapperBlob) Frens::f_free(mapperBlob);
f_close(&fp);
return 0;
}
/* -------- Public API: Load -------- */
int Emulator_LoadState(const char *path)
{
FIL fp;
if (f_open(&fp, path, FA_READ) != FR_OK) {
printf("LoadState: failed to open file %s\n", path);
return -1;
}
auto r = [&](void *buf, UINT len) -> bool
{
UINT br;
return f_read(&fp, buf, len, &br) == FR_OK && br == len;
};
SaveHeader hdr{};
if (!r(&hdr, sizeof hdr) ||
memcmp(hdr.magic, "INFOST\1", 8) != 0 ||
hdr.version != SAVESTATEFILE_VERSION ||
hdr.mapperNo != MapperNo)
{
printf("LoadState: invalid header\n");
f_close(&fp);
return -1;
}
// Region must match the running emulator. The save carries region-dependent
// APU magic numbers and timing; loading across regions would mix PAL APU
// values with NTSC PPU constants (or vice versa) and run at the wrong speed.
int savedRegion = (hdr.flags & SAVESTATE_FLAG_REGION_MASK) >> SAVESTATE_FLAG_REGION_SHIFT;
if (savedRegion != InfoNES_GetRegion())
{
static const char *names[] = { "NTSC", "PAL", "Dendy" };
printf("LoadState: region mismatch (save=%s, current=%s)\n",
names[savedRegion & 3], names[InfoNES_GetRegion() & 3]);
f_close(&fp);
return -1;
}
// A mapper with PRG RAM of its own (MMC5) needs it in the file, and states
// saved before that was added do not have it. Refuse those here, before any
// live buffer is overwritten, instead of misreading the rest of the file.
if (((hdr.flags & SAVESTATE_FLAG_MAPPER_PRG_RAM) != 0) != (MapperPrgRam != nullptr))
{
printf("LoadState: mapper PRG RAM layout differs from this build, refusing state\n");
f_close(&fp);
return -1;
}
struct SaveCore *coreDyn;
coreDyn = (struct SaveCore *)Frens::f_malloc(sizeof(SaveCore));
// Use dynamic allocation to avoid stack overflow on constrained systems
if (!coreDyn) {
f_close(&fp);
printf("LoadState: out of memory for core data\n");
return -1;
}
SaveCore &core = *coreDyn;
// The mirror bytes after RAM are skipped by reading them into PPURAM, which
// is read in full from the file right after this block, so no extra buffer
// is needed.
static_assert(RAM_STATE_SIZE - RAM_SIZE <= PPURAM_SIZE, "RAM mirror skip must fit in PPURAM");
if (!r(&core, sizeof core) ||
!r(RAM, RAM_SIZE) ||
!r(PPURAM, RAM_STATE_SIZE - RAM_SIZE) ||
!r(SPRRAM, SPRRAM_SIZE) ||
!r(SRAM, SRAM_SIZE))
{
f_close(&fp);
printf("LoadState: failed to read core data\n");
Frens::f_free(coreDyn);
return -1;
}
// Restore PPURAM (nametables + palette + CHR RAM if present)
if (!r(PPURAM, PPURAM_SIZE))
{
f_close(&fp);
printf("LoadState: failed to read PPURAM\n");
Frens::f_free(coreDyn);
return -1;
}
// CHR RAM stored outside PPURAM (see Emulator_SaveState)
if (MapperChrRam && !r(MapperChrRam, MapperChrRamSize))
{
f_close(&fp);
printf("LoadState: failed to read mapper CHR RAM\n");
Frens::f_free(coreDyn);
return -1;
}
// Name table RAM stored outside PPURAM (see Emulator_SaveState)
if (mapperNtRamNeedsFile() && !r(MapperNtRam, MapperNtRamSize))
{
f_close(&fp);
printf("LoadState: failed to read mapper name table RAM\n");
Frens::f_free(coreDyn);
return -1;
}
// PRG RAM stored outside SRAM (see Emulator_SaveState)
if (MapperPrgRam && !r(MapperPrgRam, MapperPrgRamSize))
{
f_close(&fp);
printf("LoadState: failed to read mapper PRG RAM\n");
Frens::f_free(coreDyn);
return -1;
}
// Mapper blob
size_t mapperSize = 0;
if (!r(&mapperSize, sizeof mapperSize))
{
f_close(&fp);
printf("LoadState: failed to read mapper blob size\n");
Frens::f_free(coreDyn);
return -1;
}
// mapper blob buffer
// Sanity bound: mapper blobs are small structs. A wild size here means a
// stale or corrupt file, and f_malloc panics on an absurd PSRAM request.
if (mapperSize > 0x10000)
{
f_close(&fp);
printf("LoadState: bogus mapper blob size %u, refusing state\n", (unsigned)mapperSize);
Frens::f_free(coreDyn);
return -1;
}
// The blob is a raw struct, so a file written when this mapper's blob had a
// different layout cannot be read back. Sizes do change: mapper 30 grew from
// one byte to a struct when flash emulation landed, and a mapper that gained
// a blob it never had would be handed a buffer smaller than it reads. Without
// this the mismatch is an out-of-bounds read on the heap.
if (mapperSize && MapperLoadBlob && MapperBlobSize &&
mapperSize != (size_t)MapperBlobSize())
{
f_close(&fp);
printf("LoadState: mapper blob is %u bytes, this build expects %u - refusing state\n",
(unsigned)mapperSize, (unsigned)MapperBlobSize());
Frens::f_free(coreDyn);
return -1;
}
BYTE *mapperBuf = nullptr;
if (mapperSize && MapperLoadBlob)
{
mapperBuf = (BYTE *)Frens::f_malloc(mapperSize);
if (!mapperBuf)
{
f_close(&fp);
printf("LoadState: out of memory for mapper blob (%u bytes)\n", (unsigned)mapperSize);
Frens::f_free(coreDyn);
return -1;
}
if (!r(mapperBuf, mapperSize))
{
f_close(&fp);
printf("LoadState: failed to read mapper blob\n");
Frens::f_free(coreDyn);
Frens::f_free(mapperBuf);
return -1;
}
}
// APU blob
size_t apuSize = 0;
if (!r(&apuSize, sizeof apuSize))
{
f_close(&fp);
printf("LoadState: failed to read APU blob size\n");
Frens::f_free(coreDyn);
return -1;
}
std::unique_ptr<BYTE[]> apuBuf;
if (apuSize)
{
apuBuf.reset(new BYTE[apuSize]);
if (!r(apuBuf.get(), apuSize))
{
f_close(&fp);
printf("LoadState: failed to read APU blob\n");
Frens::f_free(coreDyn);
return -1;
}
}
// DMC IRQ timing. States saved before it existed restart it idle.
ApuDmcIrq_t dmcIrq;
bool haveDmcIrq = (hdr.flags & SAVESTATE_FLAG_DMC_IRQ) != 0;
if (haveDmcIrq && !r(&dmcIrq, sizeof dmcIrq))
{
f_close(&fp);
printf("LoadState: failed to read DMC IRQ state\n");
Frens::f_free(coreDyn);
if (mapperBuf) Frens::f_free(mapperBuf);
return -1;
}
f_close(&fp);
// CPU restore
PC = core.PC;
SP = core.SP;
F = core.F;
A = core.A;
X = core.X;
Y = core.Y;
IRQ_State = core.IRQ_State;
IRQ_Wiring = core.IRQ_Wiring;
NMI_State = core.NMI_State;
NMI_Wiring = core.NMI_Wiring;
g_wPassedClocks = core.g_wPassedClocks;
g_wCurrentClocks = core.g_wCurrentClocks;
// PPU restore
PPU_R0 = core.PPU_R0;
PPU_R1 = core.PPU_R1;
PPU_R2 = core.PPU_R2;
PPU_R3 = core.PPU_R3;
PPU_R7 = core.PPU_R7;
PPU_Addr = core.PPU_Addr;
PPU_Temp = core.PPU_Temp;
PPU_Increment = core.PPU_Increment;
PPU_Scanline = core.PPU_Scanline;
PPU_NameTableBank = core.PPU_NameTableBank;
PPU_SP_Height = core.PPU_SP_Height;
PPU_Scr_H_Byte = core.PPU_Scr_H_Byte;
PPU_Scr_H_Bit = core.PPU_Scr_H_Bit;
PPU_Latch_Flag = core.PPU_Latch_Flag;
PPU_UpDown_Clip = core.PPU_UpDown_Clip;
FrameIRQ_Enable = core.FrameIRQ_Enable;
FrameStep = core.FrameStep;
SpriteJustHit = core.SpriteJustHit;
// Misc restore
FrameSkip = core.FrameSkip;
FrameCnt = core.FrameCnt;
byVramWriteEnable = core.byVramWriteEnable;
ROM_Mirroring = core.ROM_Mirroring;
memcpy(PalTable, core.PalTable, sizeof PalTable);
// The saved colors were decoded with the color palette selected at save
// time; decode the restored palette RAM again with the one selected now.
// An older state may lack part of its palette RAM, so it keeps its colors.
if (core.palRamValid)
InfoNES_RefreshPalTable();
memcpy(APU_Reg, core.APU_Reg, sizeof APU_Reg);
// APU externals restore
cur_event = core.cur_event;
entertime = core.entertime;
ApuCtrl = core.ApuCtrl;
ApuCtrlNew = core.ApuCtrlNew;
ApuQuality = core.ApuQuality;
ApuPulseMagic = core.ApuPulseMagic;
ApuTriangleMagic = core.ApuTriangleMagic;
ApuNoiseMagic = core.ApuNoiseMagic;
ApuSamplesPerSync16 = core.ApuSamplesPerSync16;
ApuCyclesPerSample = core.ApuCyclesPerSample;
ApuSampleRate = core.ApuSampleRate;
ApuCycleRate = core.ApuCycleRate;
ApuC1a = core.ApuC1a;
ApuC1b = core.ApuC1b;
ApuC1c = core.ApuC1c;
ApuC1d = core.ApuC1d;
ApuC1Skip = core.ApuC1Skip;
ApuC1Index = core.ApuC1Index;
ApuC1EnvPhase = core.ApuC1EnvPhase;
ApuC1EnvVol = core.ApuC1EnvVol;
ApuC1Atl = core.ApuC1Atl;
ApuC1SweepPhase = core.ApuC1SweepPhase;
ApuC1Freq = core.ApuC1Freq;
ApuC2a = core.ApuC2a;
ApuC2b = core.ApuC2b;
ApuC2c = core.ApuC2c;
ApuC2d = core.ApuC2d;
ApuC2Skip = core.ApuC2Skip;
ApuC2Index = core.ApuC2Index;
ApuC2EnvPhase = core.ApuC2EnvPhase;
ApuC2EnvVol = core.ApuC2EnvVol;
ApuC2Atl = core.ApuC2Atl;
ApuC2SweepPhase = core.ApuC2SweepPhase;
ApuC2Freq = core.ApuC2Freq;
ApuC3a = core.ApuC3a;
ApuC3b = core.ApuC3b;
ApuC3c = core.ApuC3c;
ApuC3d = core.ApuC3d;
ApuC3Skip = core.ApuC3Skip;
ApuC3Index = core.ApuC3Index;
ApuC3Atl = core.ApuC3Atl;
ApuC3Llc = core.ApuC3Llc;
ApuC3ReloadFlag = core.ApuC3ReloadFlag;
ApuC4a = core.ApuC4a;
ApuC4b = core.ApuC4b;
ApuC4c = core.ApuC4c;
ApuC4d = core.ApuC4d;
ApuC4Sr = core.ApuC4Sr;
ApuC4Fdc = core.ApuC4Fdc;
ApuC4Skip = core.ApuC4Skip;
ApuC4Index = core.ApuC4Index;
ApuC4Atl = core.ApuC4Atl;
ApuC4EnvVol = core.ApuC4EnvVol;
ApuC4EnvPhase = core.ApuC4EnvPhase;
memcpy(ApuC5Reg, core.ApuC5Reg, sizeof ApuC5Reg);
ApuC5Enable = core.ApuC5Enable;
ApuC5Looping = core.ApuC5Looping;
ApuC5CurByte = core.ApuC5CurByte;
ApuC5DpcmValue = core.ApuC5DpcmValue;
ApuC5Freq = core.ApuC5Freq;
ApuC5Phaseacc = core.ApuC5Phaseacc;
ApuC5Address = core.ApuC5Address;
ApuC5CacheAddr = core.ApuC5CacheAddr;
ApuC5DmaLength = core.ApuC5DmaLength;
ApuC5CacheDmaLength = core.ApuC5CacheDmaLength;
// Controller restore
PAD1_Latch = core.PAD1_Latch;
PAD2_Latch = core.PAD2_Latch;
PAD_System = core.PAD_System;
PAD1_Bit = core.PAD1_Bit;
PAD2_Bit = core.PAD2_Bit;
// Rebind banks & mirroring
restorePPUBanks(core);
restorePRGBanks(core);
// Must match InfoNES_SetupPPU: a four-screen cart has to come back on four
// name tables, and the MMC3 family's Map*LoadBlob guards on !ROM_FourScr so
// it will not put it back itself.
InfoNES_Mirroring(ROM_FourScr ? 4 : ROM_Mirroring);
Frens::f_free(coreDyn);
// Mapper / APU custom state restore
// if (Mapper_Load(mapperBuf.get(), mapperSize) < 0) {