| 1 | // Savestate handling for emulated Sega/Mega CD machine. |
| 2 | // (c) Copyright 2007, Grazvydas "notaz" Ignotas |
| 3 | |
| 4 | |
| 5 | #include "../PicoInt.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("PicoSMCD", 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 | CHECKED_WRITE(CHUNK_FM, 0x200+4, ym2612_regs); |
| 126 | |
| 127 | if (PicoMCD & 1) |
| 128 | { |
| 129 | Pico_mcd->m.audio_offset = mp3_get_offset(); |
| 130 | memset(buff, 0, sizeof(buff)); |
| 131 | PicoAreaPackCpu(buff, 1); |
| 132 | if (Pico_mcd->s68k_regs[3]&4) // 1M mode? |
| 133 | wram_1M_to_2M(Pico_mcd->word_ram2M); |
| 134 | Pico_mcd->m.hint_vector = *(unsigned short *)(Pico_mcd->bios + 0x72); |
| 135 | |
| 136 | CHECKED_WRITE_BUFF(CHUNK_S68K, buff); |
| 137 | CHECKED_WRITE_BUFF(CHUNK_PRG_RAM, Pico_mcd->prg_ram); |
| 138 | CHECKED_WRITE_BUFF(CHUNK_WORD_RAM, Pico_mcd->word_ram2M); // in 2M format |
| 139 | CHECKED_WRITE_BUFF(CHUNK_PCM_RAM, Pico_mcd->pcm_ram); |
| 140 | CHECKED_WRITE_BUFF(CHUNK_BRAM, Pico_mcd->bram); |
| 141 | CHECKED_WRITE_BUFF(CHUNK_GA_REGS, Pico_mcd->s68k_regs); // GA regs, not CPU regs |
| 142 | CHECKED_WRITE_BUFF(CHUNK_PCM, Pico_mcd->pcm); |
| 143 | CHECKED_WRITE_BUFF(CHUNK_CDD, Pico_mcd->cdd); |
| 144 | CHECKED_WRITE_BUFF(CHUNK_CDC, Pico_mcd->cdc); |
| 145 | CHECKED_WRITE_BUFF(CHUNK_SCD, Pico_mcd->scd); |
| 146 | CHECKED_WRITE_BUFF(CHUNK_RC, Pico_mcd->rot_comp); |
| 147 | CHECKED_WRITE_BUFF(CHUNK_MISC_CD, Pico_mcd->m); |
| 148 | |
| 149 | if (Pico_mcd->s68k_regs[3]&4) // convert back |
| 150 | wram_2M_to_1M(Pico_mcd->word_ram2M); |
| 151 | } |
| 152 | |
| 153 | if (carthw_chunks != NULL) |
| 154 | { |
| 155 | carthw_state_chunk *chwc; |
| 156 | if (PicoStateProgressCB) |
| 157 | PicoStateProgressCB("Saving.. cart hw state"); |
| 158 | for (chwc = carthw_chunks; chwc->ptr != NULL; chwc++) |
| 159 | CHECKED_WRITE(chwc->chunk, chwc->size, chwc->ptr); |
| 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static int g_read_offs = 0; |
| 166 | |
| 167 | #define R_ERROR_RETURN(error) \ |
| 168 | { \ |
| 169 | elprintf(EL_STATUS, "PicoCdLoadState @ %x: " error, g_read_offs); \ |
| 170 | return 1; \ |
| 171 | } |
| 172 | |
| 173 | // when is eof really set? |
| 174 | #define CHECKED_READ(len,data) \ |
| 175 | if (areaRead(data, 1, len, file) != len) { \ |
| 176 | if (len == 1 && areaEof(file)) return 0; \ |
| 177 | R_ERROR_RETURN("areaRead: premature EOF\n"); \ |
| 178 | return 1; \ |
| 179 | } \ |
| 180 | g_read_offs += len; |
| 181 | |
| 182 | #define CHECKED_READ2(len2,data) \ |
| 183 | if (len2 != len) { \ |
| 184 | elprintf(EL_STATUS, "unexpected len %i, wanted %i (%s)", len, len2, #len2); \ |
| 185 | if (len > len2) R_ERROR_RETURN("failed."); \ |
| 186 | /* else read anyway and hope for the best.. */ \ |
| 187 | } \ |
| 188 | CHECKED_READ(len, data) |
| 189 | |
| 190 | #define CHECKED_READ_BUFF(buff) CHECKED_READ2(sizeof(buff), &buff); |
| 191 | |
| 192 | PICO_INTERNAL int PicoCdLoadState(void *file) |
| 193 | { |
| 194 | unsigned char buff[0x60]; |
| 195 | int ver, len; |
| 196 | void *ym2612_regs = YM2612GetRegs(); |
| 197 | |
| 198 | g_read_offs = 0; |
| 199 | CHECKED_READ(8, buff); |
| 200 | if (strncmp((char *)buff, "PicoSMCD", 8)) R_ERROR_RETURN("bad header"); |
| 201 | CHECKED_READ(4, &ver); |
| 202 | |
| 203 | while (!areaEof(file)) |
| 204 | { |
| 205 | CHECKED_READ(1, buff); |
| 206 | CHECKED_READ(4, &len); |
| 207 | if (len < 0 || len > 1024*512) R_ERROR_RETURN("bad length"); |
| 208 | if (buff[0] > CHUNK_FM && buff[0] <= CHUNK_MISC_CD && !(PicoMCD & 1)) |
| 209 | R_ERROR_RETURN("cd chunk in non CD state?"); |
| 210 | |
| 211 | switch (buff[0]) |
| 212 | { |
| 213 | case CHUNK_M68K: |
| 214 | CHECKED_READ_BUFF(buff); |
| 215 | PicoAreaUnpackCpu(buff, 0); |
| 216 | break; |
| 217 | |
| 218 | case CHUNK_Z80: |
| 219 | CHECKED_READ_BUFF(buff); |
| 220 | z80_unpack(buff); |
| 221 | break; |
| 222 | |
| 223 | case CHUNK_RAM: CHECKED_READ_BUFF(Pico.ram); break; |
| 224 | case CHUNK_VRAM: CHECKED_READ_BUFF(Pico.vram); break; |
| 225 | case CHUNK_ZRAM: CHECKED_READ_BUFF(Pico.zram); break; |
| 226 | case CHUNK_CRAM: CHECKED_READ_BUFF(Pico.cram); break; |
| 227 | case CHUNK_VSRAM: CHECKED_READ_BUFF(Pico.vsram); break; |
| 228 | case CHUNK_MISC: CHECKED_READ_BUFF(Pico.m); break; |
| 229 | case CHUNK_VIDEO: CHECKED_READ_BUFF(Pico.video); break; |
| 230 | case CHUNK_PSG: CHECKED_READ2(28*4, sn76496_regs); break; |
| 231 | case CHUNK_FM: |
| 232 | CHECKED_READ2(0x200+4, ym2612_regs); |
| 233 | YM2612PicoStateLoad(); |
| 234 | break; |
| 235 | |
| 236 | // cd stuff |
| 237 | case CHUNK_S68K: |
| 238 | CHECKED_READ_BUFF(buff); |
| 239 | PicoAreaUnpackCpu(buff, 1); |
| 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 | if (PicoMCD & 1) |
| 273 | { |
| 274 | /* after load events */ |
| 275 | if (Pico_mcd->s68k_regs[3]&4) // 1M mode? |
| 276 | wram_2M_to_1M(Pico_mcd->word_ram2M); |
| 277 | PicoMemResetCD(Pico_mcd->s68k_regs[3]); |
| 278 | #ifdef _ASM_CD_MEMORY_C |
| 279 | if (Pico_mcd->s68k_regs[3]&4) |
| 280 | PicoMemResetCDdecode(Pico_mcd->s68k_regs[3]); |
| 281 | #endif |
| 282 | if (Pico_mcd->m.audio_track > 0 && Pico_mcd->m.audio_track < Pico_mcd->TOC.Last_Track) |
| 283 | mp3_start_play(Pico_mcd->TOC.Tracks[Pico_mcd->m.audio_track].F, Pico_mcd->m.audio_offset); |
| 284 | // restore hint vector |
| 285 | *(unsigned short *)(Pico_mcd->bios + 0x72) = Pico_mcd->m.hint_vector; |
| 286 | } |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | |
| 292 | int PicoCdLoadStateGfx(void *file) |
| 293 | { |
| 294 | int ver, len, found = 0; |
| 295 | char buff[8]; |
| 296 | |
| 297 | g_read_offs = 0; |
| 298 | CHECKED_READ(8, buff); |
| 299 | if (strncmp(buff, "PicoSMCD", 8)) R_ERROR_RETURN("bad header"); |
| 300 | CHECKED_READ(4, &ver); |
| 301 | |
| 302 | while (!areaEof(file) && found < 4) |
| 303 | { |
| 304 | CHECKED_READ(1, buff); |
| 305 | CHECKED_READ(4, &len); |
| 306 | if (len < 0 || len > 1024*512) R_ERROR_RETURN("bad length"); |
| 307 | if (buff[0] > CHUNK_FM && buff[0] <= CHUNK_MISC_CD && !(PicoMCD & 1)) |
| 308 | R_ERROR_RETURN("cd chunk in non CD state?"); |
| 309 | |
| 310 | switch (buff[0]) |
| 311 | { |
| 312 | case CHUNK_VRAM: CHECKED_READ_BUFF(Pico.vram); found++; break; |
| 313 | case CHUNK_CRAM: CHECKED_READ_BUFF(Pico.cram); found++; break; |
| 314 | case CHUNK_VSRAM: CHECKED_READ_BUFF(Pico.vsram); found++; break; |
| 315 | case CHUNK_VIDEO: CHECKED_READ_BUFF(Pico.video); found++; break; |
| 316 | default: |
| 317 | areaSeek(file, len, SEEK_CUR); |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | |