moved writelrom to use global path
[picodrive.git] / pico / cd / area.c
... / ...
CommitLineData
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
11extern int *sn76496_regs;
12
13carthw_state_chunk *carthw_chunks;
14void (*PicoStateProgressCB)(const char *str) = 0;
15
16
17typedef 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
47static 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
76static 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
99PICO_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
166static 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
193PICO_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
272readend:
273 if (PicoAHW & PAHW_MCD)
274 {
275 /* after load events */
276 if (Pico_mcd->s68k_regs[3]&4) // 1M mode?
277 wram_2M_to_1M(Pico_mcd->word_ram2M);
278 PicoMemResetCD(Pico_mcd->s68k_regs[3]);
279#ifdef _ASM_CD_MEMORY_C
280 if (Pico_mcd->s68k_regs[3]&4)
281 PicoMemResetCDdecode(Pico_mcd->s68k_regs[3]);
282#endif
283 if (!(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))
284 cdda_start_play();
285 // restore hint vector
286 *(unsigned short *)(Pico_mcd->bios + 0x72) = Pico_mcd->m.hint_vector;
287
288 // must unpack after other CD stuff is loaded
289 PicoAreaUnpackCpu(buff_s68k, 1);
290 }
291 PicoAreaUnpackCpu(buff_m68k, 0);
292
293 return 0;
294}
295
296
297int PicoCdLoadStateGfx(void *file)
298{
299 int ver, len, found = 0;
300 char buff[8];
301
302 g_read_offs = 0;
303 CHECKED_READ(8, buff);
304 if (strncmp((char *)buff, "PicoSMCD", 8) && strncmp((char *)buff, "PicoSEXT", 8))
305 R_ERROR_RETURN("bad header");
306 CHECKED_READ(4, &ver);
307
308 while (!areaEof(file) && found < 4)
309 {
310 CHECKED_READ(1, buff);
311 CHECKED_READ(4, &len);
312 if (len < 0 || len > 1024*512) R_ERROR_RETURN("bad length");
313 if (buff[0] > CHUNK_FM && buff[0] <= CHUNK_MISC_CD && !(PicoAHW & PAHW_MCD))
314 R_ERROR_RETURN("cd chunk in non CD state?");
315
316 switch (buff[0])
317 {
318 case CHUNK_VRAM: CHECKED_READ_BUFF(Pico.vram); found++; break;
319 case CHUNK_CRAM: CHECKED_READ_BUFF(Pico.cram); found++; break;
320 case CHUNK_VSRAM: CHECKED_READ_BUFF(Pico.vsram); found++; break;
321 case CHUNK_VIDEO: CHECKED_READ_BUFF(Pico.video); found++; break;
322 default:
323 areaSeek(file, len, SEEK_CUR);
324 break;
325 }
326 }
327
328readend:
329 return 0;
330}
331
332