b43a4d9e669df95201bcd58332065c0a3ab9433c
[picodrive.git] / area.c
1 // Savestate handling for emulated Sega/Mega CD machine.
2 // (c) Copyright 2007, Grazvydas "notaz" Ignotas
3
4
5 #include "../pico_int.h"
6
7 // ym2612
8 #include "../sound/ym2612.h"
9
10 // sn76496
11 extern int *sn76496_regs;
12
13 carthw_state_chunk *carthw_chunks;
14 void (*PicoStateProgressCB)(const char *str) = 0;
15
16
17 typedef enum {
18         CHUNK_M68K = 1,
19         CHUNK_RAM,
20         CHUNK_VRAM,
21         CHUNK_ZRAM,
22         CHUNK_CRAM,     // 5
23         CHUNK_VSRAM,
24         CHUNK_MISC,
25         CHUNK_VIDEO,
26         CHUNK_Z80,
27         CHUNK_PSG,      // 10
28         CHUNK_FM,
29         // CD stuff
30         CHUNK_S68K,
31         CHUNK_PRG_RAM,
32         CHUNK_WORD_RAM,
33         CHUNK_PCM_RAM,  // 15
34         CHUNK_BRAM,
35         CHUNK_GA_REGS,
36         CHUNK_PCM,
37         CHUNK_CDC,
38         CHUNK_CDD,      // 20
39         CHUNK_SCD,
40         CHUNK_RC,
41         CHUNK_MISC_CD,
42         CHUNK_DEFAULT_COUNT
43         // CHUNK_CARTHW = 64,   // defined in PicoInt
44 } chunk_name_e;
45
46
47 static char *chunk_names[] = {
48         "INVALID!",
49         "Saving.. M68K state",
50         "Saving.. RAM",
51         "Saving.. VRAM",
52         "Saving.. ZRAM",
53         "Saving.. CRAM",        // 5
54         "Saving.. VSRAM",
55         "Saving.. emu state",
56         "Saving.. VIDEO",
57         "Saving.. Z80 state",
58         "Saving.. PSG",         // 10
59         "Saving.. FM",
60         // CD stuff
61         "Saving.. S68K state",
62         "Saving.. PRG_RAM",
63         "Saving.. WORD_RAM",
64         "Saving.. PCM_RAM",     // 15
65         "Saving.. BRAM",
66         "Saving.. GATE ARRAY regs",
67         "Saving.. PCM state",
68         "Saving.. CDC",
69         "Saving.. CDD",         // 20
70         "Saving.. SCD",
71         "Saving.. GFX chip",
72         "Saving.. MCD state",
73 };
74
75
76 static int write_chunk(chunk_name_e name, int len, void *data, void *file)
77 {
78         size_t bwritten = 0;
79         bwritten += areaWrite(&name, 1, 1, file);
80         bwritten += areaWrite(&len, 1, 4, file);
81         bwritten += areaWrite(data, 1, len, file);
82
83         return (bwritten == len + 4 + 1);
84 }
85
86
87 #define CHECKED_WRITE(name,len,data) { \
88         if (PicoStateProgressCB && name < CHUNK_DEFAULT_COUNT) \
89                 PicoStateProgressCB(chunk_names[name]); \
90         if (!write_chunk(name, len, data, file)) return 1; \
91 }
92
93 #define CHECKED_WRITE_BUFF(name,buff) { \
94         if (PicoStateProgressCB && name < CHUNK_DEFAULT_COUNT) \
95                 PicoStateProgressCB(chunk_names[name]); \
96         if (!write_chunk(name, sizeof(buff), &buff, file)) return 1; \
97 }
98
99 PICO_INTERNAL int PicoCdSaveState(void *file)
100 {
101         unsigned char buff[0x60];
102         void *ym2612_regs = YM2612GetRegs();
103
104         areaWrite("PicoSEXT", 1, 8, file);
105         areaWrite(&PicoVer, 1, 4, file);
106
107         memset(buff, 0, sizeof(buff));
108         PicoAreaPackCpu(buff, 0);
109         CHECKED_WRITE_BUFF(CHUNK_M68K,  buff);
110         CHECKED_WRITE_BUFF(CHUNK_RAM,   Pico.ram);
111         CHECKED_WRITE_BUFF(CHUNK_VRAM,  Pico.vram);
112         CHECKED_WRITE_BUFF(CHUNK_ZRAM,  Pico.zram);
113         CHECKED_WRITE_BUFF(CHUNK_CRAM,  Pico.cram);
114         CHECKED_WRITE_BUFF(CHUNK_VSRAM, Pico.vsram);
115         CHECKED_WRITE_BUFF(CHUNK_MISC,  Pico.m);
116         CHECKED_WRITE_BUFF(CHUNK_VIDEO, Pico.video);
117         if (PicoOpt&7) {
118                 memset(buff, 0, sizeof(buff));
119                 z80_pack(buff);
120                 CHECKED_WRITE_BUFF(CHUNK_Z80, buff);
121         }
122         if (PicoOpt&3)
123                 CHECKED_WRITE(CHUNK_PSG, 28*4, sn76496_regs);
124         if (PicoOpt&1) {
125                 ym2612_pack_state();
126                 CHECKED_WRITE(CHUNK_FM, 0x200+4, ym2612_regs);
127         }
128
129         if (PicoAHW & PAHW_MCD)
130         {
131                 memset(buff, 0, sizeof(buff));
132                 PicoAreaPackCpu(buff, 1);
133                 if (Pico_mcd->s68k_regs[3]&4) // 1M mode?
134                         wram_1M_to_2M(Pico_mcd->word_ram2M);
135                 Pico_mcd->m.hint_vector = *(unsigned short *)(Pico_mcd->bios + 0x72);
136
137                 CHECKED_WRITE_BUFF(CHUNK_S68K,     buff);
138                 CHECKED_WRITE_BUFF(CHUNK_PRG_RAM,  Pico_mcd->prg_ram);
139                 CHECKED_WRITE_BUFF(CHUNK_WORD_RAM, Pico_mcd->word_ram2M); // in 2M format
140                 CHECKED_WRITE_BUFF(CHUNK_PCM_RAM,  Pico_mcd->pcm_ram);
141                 CHECKED_WRITE_BUFF(CHUNK_BRAM,     Pico_mcd->bram);
142                 CHECKED_WRITE_BUFF(CHUNK_GA_REGS,  Pico_mcd->s68k_regs); // GA regs, not CPU regs
143                 CHECKED_WRITE_BUFF(CHUNK_PCM,      Pico_mcd->pcm);
144                 CHECKED_WRITE_BUFF(CHUNK_CDD,      Pico_mcd->cdd);
145                 CHECKED_WRITE_BUFF(CHUNK_CDC,      Pico_mcd->cdc);
146                 CHECKED_WRITE_BUFF(CHUNK_SCD,      Pico_mcd->scd);
147                 CHECKED_WRITE_BUFF(CHUNK_RC,       Pico_mcd->rot_comp);
148                 CHECKED_WRITE_BUFF(CHUNK_MISC_CD,  Pico_mcd->m);
149
150                 if (Pico_mcd->s68k_regs[3]&4) // convert back
151                         wram_2M_to_1M(Pico_mcd->word_ram2M);
152         }
153
154         if (carthw_chunks != NULL)
155         {
156                 carthw_state_chunk *chwc;
157                 if (PicoStateProgressCB)
158                         PicoStateProgressCB("Saving.. cart hw state");
159                 for (chwc = carthw_chunks; chwc->ptr != NULL; chwc++)
160                         CHECKED_WRITE(chwc->chunk, chwc->size, chwc->ptr);
161         }
162
163         return 0;
164 }
165
166 static int g_read_offs = 0;
167
168 #define R_ERROR_RETURN(error) \
169 { \
170         elprintf(EL_STATUS, "PicoCdLoadState @ %x: " error, g_read_offs); \
171         return 1; \
172 }
173
174 // when is eof really set?
175 #define CHECKED_READ(len,data) \
176         if (areaRead(data, 1, len, file) != len) { \
177                 if (len == 1 && areaEof(file)) goto readend; \
178                 R_ERROR_RETURN("areaRead: premature EOF\n"); \
179                 return 1; \
180         } \
181         g_read_offs += len;
182
183 #define CHECKED_READ2(len2,data) \
184         if (len2 != len) { \
185                 elprintf(EL_STATUS, "unexpected len %i, wanted %i (%s)", len, len2, #len2); \
186                 if (len > len2) R_ERROR_RETURN("failed."); \
187                 /* else read anyway and hope for the best.. */ \
188         } \
189         CHECKED_READ(len, data)
190
191 #define CHECKED_READ_BUFF(buff) CHECKED_READ2(sizeof(buff), &buff);
192
193 PICO_INTERNAL int PicoCdLoadState(void *file)
194 {
195         unsigned char buff[0x60], buff_m68k[0x60], buff_s68k[0x60];
196         int ver, len;
197         void *ym2612_regs = YM2612GetRegs();
198
199         g_read_offs = 0;
200         CHECKED_READ(8, buff);
201         if (strncmp((char *)buff, "PicoSMCD", 8) && strncmp((char *)buff, "PicoSEXT", 8))
202                 R_ERROR_RETURN("bad header");
203         CHECKED_READ(4, &ver);
204
205         while (!areaEof(file))
206         {
207                 CHECKED_READ(1, buff);
208                 CHECKED_READ(4, &len);
209                 if (len < 0 || len > 1024*512) R_ERROR_RETURN("bad length");
210                 if (buff[0] > CHUNK_FM && buff[0] <= CHUNK_MISC_CD && !(PicoAHW & PAHW_MCD))
211                         R_ERROR_RETURN("cd chunk in non CD state?");
212
213                 switch (buff[0])
214                 {
215                         case CHUNK_M68K:
216                                 CHECKED_READ_BUFF(buff_m68k);
217                                 break;
218
219                         case CHUNK_Z80:
220                                 CHECKED_READ_BUFF(buff);
221                                 z80_unpack(buff);
222                                 break;
223
224                         case CHUNK_RAM:   CHECKED_READ_BUFF(Pico.ram); break;
225                         case CHUNK_VRAM:  CHECKED_READ_BUFF(Pico.vram); break;
226                         case CHUNK_ZRAM:  CHECKED_READ_BUFF(Pico.zram); break;
227                         case CHUNK_CRAM:  CHECKED_READ_BUFF(Pico.cram); break;
228                         case CHUNK_VSRAM: CHECKED_READ_BUFF(Pico.vsram); break;
229                         case CHUNK_MISC:  CHECKED_READ_BUFF(Pico.m); break;
230                         case CHUNK_VIDEO: CHECKED_READ_BUFF(Pico.video); break;
231                         case CHUNK_PSG:   CHECKED_READ2(28*4, sn76496_regs); break;
232                         case CHUNK_FM:
233                                 CHECKED_READ2(0x200+4, ym2612_regs);
234                                 ym2612_unpack_state();
235                                 break;
236
237                         // cd stuff
238                         case CHUNK_S68K:
239                                 CHECKED_READ_BUFF(buff_s68k);
240                                 break;
241
242                         case CHUNK_PRG_RAM:     CHECKED_READ_BUFF(Pico_mcd->prg_ram); break;
243                         case CHUNK_WORD_RAM:    CHECKED_READ_BUFF(Pico_mcd->word_ram2M); break;
244                         case CHUNK_PCM_RAM:     CHECKED_READ_BUFF(Pico_mcd->pcm_ram); break;
245                         case CHUNK_BRAM:        CHECKED_READ_BUFF(Pico_mcd->bram); break;
246                         case CHUNK_GA_REGS:     CHECKED_READ_BUFF(Pico_mcd->s68k_regs); break;
247                         case CHUNK_PCM:         CHECKED_READ_BUFF(Pico_mcd->pcm); break;
248                         case CHUNK_CDD:         CHECKED_READ_BUFF(Pico_mcd->cdd); break;
249                         case CHUNK_CDC:         CHECKED_READ_BUFF(Pico_mcd->cdc); break;
250                         case CHUNK_SCD:         CHECKED_READ_BUFF(Pico_mcd->scd); break;
251                         case CHUNK_RC:          CHECKED_READ_BUFF(Pico_mcd->rot_comp); break;
252                         case CHUNK_MISC_CD:     CHECKED_READ_BUFF(Pico_mcd->m); break;
253
254                         default:
255                                 if (carthw_chunks != NULL)
256                                 {
257                                         carthw_state_chunk *chwc;
258                                         for (chwc = carthw_chunks; chwc->ptr != NULL; chwc++) {
259                                                 if (chwc->chunk == buff[0]) {
260                                                         CHECKED_READ2(chwc->size, chwc->ptr);
261                                                         goto breakswitch;
262                                                 }
263                                         }
264                                 }
265                                 elprintf(EL_STATUS, "PicoCdLoadState: skipping unknown chunk %i of size %i", buff[0], len);
266                                 areaSeek(file, len, SEEK_CUR);
267                                 break;
268                 }
269                 breakswitch:;
270         }
271
272 readend:
273         if (PicoAHW & PAHW_MCD)
274         {
275                 PicoMemStateLoaded();
276
277                 if (!(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))
278                         cdda_start_play();
279
280                 // must unpack after other CD stuff is loaded
281                 PicoAreaUnpackCpu(buff_s68k, 1);
282         }
283         PicoAreaUnpackCpu(buff_m68k, 0);
284
285         return 0;
286 }
287
288
289 int PicoCdLoadStateGfx(void *file)
290 {
291         int ver, len, found = 0;
292         char buff[8];
293
294         g_read_offs = 0;
295         CHECKED_READ(8, buff);
296         if (strncmp((char *)buff, "PicoSMCD", 8) && strncmp((char *)buff, "PicoSEXT", 8))
297                 R_ERROR_RETURN("bad header");
298         CHECKED_READ(4, &ver);
299
300         while (!areaEof(file) && found < 4)
301         {
302                 CHECKED_READ(1, buff);
303                 CHECKED_READ(4, &len);
304                 if (len < 0 || len > 1024*512) R_ERROR_RETURN("bad length");
305                 if (buff[0] > CHUNK_FM && buff[0] <= CHUNK_MISC_CD && !(PicoAHW & PAHW_MCD))
306                         R_ERROR_RETURN("cd chunk in non CD state?");
307
308                 switch (buff[0])
309                 {
310                         case CHUNK_VRAM:  CHECKED_READ_BUFF(Pico.vram);  found++; break;
311                         case CHUNK_CRAM:  CHECKED_READ_BUFF(Pico.cram);  found++; break;
312                         case CHUNK_VSRAM: CHECKED_READ_BUFF(Pico.vsram); found++; break;
313                         case CHUNK_VIDEO: CHECKED_READ_BUFF(Pico.video); found++; break;
314                         default:
315                                 areaSeek(file, len, SEEK_CUR);
316                                 break;
317                 }
318         }
319
320 readend:
321         return 0;
322 }
323
324