region detection, cd states wip, fixes, stuff
[picodrive.git] / Pico / cd / Area.c
CommitLineData
51a902ae 1// This is part of Pico Library
2
3// (c) Copyright 2006 notaz, All rights reserved.
4// Free for non-commercial use.
5
6// For commercial use, separate licencing terms must be obtained.
7
8
9#include "../PicoInt.h"
10
11// ym2612
12#include "../sound/ym2612.h"
13
14// sn76496
15extern int *sn76496_regs;
16
17
18typedef enum {
19 CHUNK_M68K = 1,
20 CHUNK_RAM,
21 CHUNK_VRAM,
22 CHUNK_ZRAM,
23 CHUNK_CRAM,
24 CHUNK_VSRAM,
25 CHUNK_MISC,
26 CHUNK_VIDEO,
27 CHUNK_Z80,
28 CHUNK_PSG,
29 CHUNK_FM,
30 // CD stuff
31} chunk_name_e;
32
33
34static int write_chunk(chunk_name_e name, int len, void *data, void *file)
35{
36 size_t bwritten = 0;
37 bwritten += areaWrite(&name, 1, 1, file);
38 bwritten += areaWrite(&len, 1, 4, file);
39 bwritten += areaWrite(data, 1, len, file);
40
41 return (bwritten == len + 4 + 1);
42}
43
44
45#define CHECKED_WRITE(name,len,data) \
46 if (!write_chunk(name, len, data, file)) return 1;
47
48#define CHECKED_WRITE_BUFF(name,buff) \
49 if (!write_chunk(name, sizeof(buff), &buff, file)) return 1;
50
51int PicoCdSaveState(void *file)
52{
53 unsigned char buff[0x60];
54 void *ym2612_regs = YM2612GetRegs();
55
56 areaWrite("PicoSMCD", 1, 8, file);
57 areaWrite(&PicoVer, 1, 4, file);
58
59 memset(buff, 0, sizeof(buff));
60 PicoAreaPackCpu(buff, 0);
61 CHECKED_WRITE_BUFF(CHUNK_M68K, buff);
62 CHECKED_WRITE_BUFF(CHUNK_RAM, Pico.ram);
63 CHECKED_WRITE_BUFF(CHUNK_VRAM, Pico.vram);
64 CHECKED_WRITE_BUFF(CHUNK_ZRAM, Pico.zram);
65 CHECKED_WRITE_BUFF(CHUNK_CRAM, Pico.cram);
66 CHECKED_WRITE_BUFF(CHUNK_VSRAM, Pico.vsram);
67 CHECKED_WRITE_BUFF(CHUNK_MISC, Pico.m);
68 CHECKED_WRITE_BUFF(CHUNK_VIDEO, Pico.video);
69 if(PicoOpt&7) {
70 memset(buff, 0, sizeof(buff));
71 z80_pack(buff);
72 CHECKED_WRITE_BUFF(CHUNK_Z80, buff);
73 }
74 if(PicoOpt&3)
75 CHECKED_WRITE(CHUNK_PSG, 28*4, sn76496_regs);
76 if(PicoOpt&1)
77 CHECKED_WRITE(CHUNK_FM, 0x200+4, ym2612_regs);
78
79 // TODO: cd stuff
80
81 return 0;
82}
83
84static int g_read_offs = 0;
85
86#define CHECKED_READ(len,data) \
87 if (areaRead(data, 1, len, file) != len) { \
88 g_read_offs += len; \
89 printf("areaRead: premature EOF\n"); \
90 return 0; \
91 }
92
93#define R_ERROR_RETURN(error) \
94{ \
95 printf("PicoCdLoadState @ %x: " error "\n", g_read_offs); \
96 return 1; \
97}
98
99#define CHECKED_READ2(len2,data) \
100 if (len2 != len) R_ERROR_RETURN("unexpected len, wanted " #len2); \
101 CHECKED_READ(len2, data)
102
103#define CHECKED_READ_BUFF(buff) CHECKED_READ2(sizeof(buff), &buff);
104
105int PicoCdLoadState(void *file)
106{
107 unsigned char buff[0x60];
108 int ver, len;
109 void *ym2612_regs = YM2612GetRegs();
110
111 g_read_offs = 0;
112 CHECKED_READ(8, buff);
113 if (strncmp((char *)buff, "PicoSMCD", 8)) R_ERROR_RETURN("bad header");
114 CHECKED_READ(4, &ver);
115
116 while (!areaEof(file))
117 {
118 CHECKED_READ(1, buff);
119 CHECKED_READ(4, &len);
120 if (len < 0 || len > 1024*256) R_ERROR_RETURN("bad length");
121
122 switch (buff[0])
123 {
124 case CHUNK_M68K:
125 CHECKED_READ_BUFF(buff);
126 PicoAreaUnpackCpu(buff, 0);
127 break;
128
129 case CHUNK_Z80:
130 CHECKED_READ_BUFF(buff);
131 z80_unpack(buff);
132 break;
133
134 case CHUNK_RAM: CHECKED_READ_BUFF(Pico.ram); break;
135 case CHUNK_VRAM: CHECKED_READ_BUFF(Pico.vram); break;
136 case CHUNK_ZRAM: CHECKED_READ_BUFF(Pico.zram); break;
137 case CHUNK_CRAM: CHECKED_READ_BUFF(Pico.cram); break;
138 case CHUNK_VSRAM: CHECKED_READ_BUFF(Pico.vsram); break;
139 case CHUNK_MISC: CHECKED_READ_BUFF(Pico.m); break;
140 case CHUNK_VIDEO: CHECKED_READ_BUFF(Pico.video); break;
141 case CHUNK_PSG: CHECKED_READ2(28*4, sn76496_regs); break;
142 case CHUNK_FM:
143 CHECKED_READ2(0x200+4, ym2612_regs);
144 YM2612PicoStateLoad();
145 break;
146 }
147 }
148
149 return 0;
150}
151