1 // Savestate handling for emulated Sega/Mega CD machine.
2 // (c) Copyright 2007, Grazvydas "notaz" Ignotas
5 #include "../pico_int.h"
8 #include "../sound/ym2612.h"
11 extern int *sn76496_regs;
13 carthw_state_chunk *carthw_chunks;
14 void (*PicoStateProgressCB)(const char *str) = 0;
43 // CHUNK_CARTHW = 64, // defined in PicoInt
47 static char *chunk_names[] = {
49 "Saving.. M68K state",
61 "Saving.. S68K state",
64 "Saving.. PCM_RAM", // 15
66 "Saving.. GATE ARRAY regs",
76 static int write_chunk(chunk_name_e name, int len, void *data, void *file)
79 bwritten += areaWrite(&name, 1, 1, file);
80 bwritten += areaWrite(&len, 1, 4, file);
81 bwritten += areaWrite(data, 1, len, file);
83 return (bwritten == len + 4 + 1);
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; \
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; \
99 PICO_INTERNAL int PicoCdSaveState(void *file)
101 unsigned char buff[0x60];
102 void *ym2612_regs = YM2612GetRegs();
104 areaWrite("PicoSEXT", 1, 8, file);
105 areaWrite(&PicoVer, 1, 4, file);
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);
118 memset(buff, 0, sizeof(buff));
120 CHECKED_WRITE_BUFF(CHUNK_Z80, buff);
123 CHECKED_WRITE(CHUNK_PSG, 28*4, sn76496_regs);
126 CHECKED_WRITE(CHUNK_FM, 0x200+4, ym2612_regs);
129 if (PicoAHW & PAHW_MCD)
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);
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);
150 if (Pico_mcd->s68k_regs[3]&4) // convert back
151 wram_2M_to_1M(Pico_mcd->word_ram2M);
154 if (carthw_chunks != NULL)
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);
166 static int g_read_offs = 0;
168 #define R_ERROR_RETURN(error) \
170 elprintf(EL_STATUS, "PicoCdLoadState @ %x: " error, g_read_offs); \
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"); \
183 #define CHECKED_READ2(len2,data) \
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.. */ \
189 CHECKED_READ(len, data)
191 #define CHECKED_READ_BUFF(buff) CHECKED_READ2(sizeof(buff), &buff);
193 PICO_INTERNAL int PicoCdLoadState(void *file)
195 unsigned char buff[0x60], buff_m68k[0x60], buff_s68k[0x60];
197 void *ym2612_regs = YM2612GetRegs();
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);
205 while (!areaEof(file))
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?");
216 CHECKED_READ_BUFF(buff_m68k);
220 CHECKED_READ_BUFF(buff);
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;
233 CHECKED_READ2(0x200+4, ym2612_regs);
234 ym2612_unpack_state();
239 CHECKED_READ_BUFF(buff_s68k);
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;
255 if (carthw_chunks != NULL)
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);
265 elprintf(EL_STATUS, "PicoCdLoadState: skipping unknown chunk %i of size %i", buff[0], len);
266 areaSeek(file, len, SEEK_CUR);
273 if (PicoAHW & PAHW_MCD)
275 PicoMemStateLoaded();
277 if (!(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))
280 // must unpack after other CD stuff is loaded
281 PicoAreaUnpackCpu(buff_s68k, 1);
283 PicoAreaUnpackCpu(buff_m68k, 0);
289 int PicoCdLoadStateGfx(void *file)
291 int ver, len, found = 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);
300 while (!areaEof(file) && found < 4)
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?");
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;
315 areaSeek(file, len, SEEK_CUR);