amalgamation
[picodrive.git] / Pico / Area.c
1 // This is part of Pico Library\r
2 \r
3 // (c) Copyright 2004 Dave, All rights reserved.\r
4 // (c) Copyright 2006 notaz, All rights reserved.\r
5 // Free for non-commercial use.\r
6 \r
7 // For commercial use, separate licencing terms must be obtained.\r
8 \r
9 \r
10 #include "PicoInt.h"\r
11 \r
12 // ym2612\r
13 #include "sound/ym2612.h"\r
14 \r
15 // sn76496\r
16 extern int *sn76496_regs;\r
17 \r
18 \r
19 struct PicoArea { void *data; int len; char *name; };\r
20 \r
21 // strange observation on Symbian OS 9.1, m600 organizer fw r3a06:\r
22 // taking an address of fread or fwrite causes "application could't be started" error\r
23 // on startup randomly depending on binary layout of executable file.\r
24 \r
25 arearw    *areaRead  = (arearw *) 0; // fread;  // read and write function pointers for\r
26 arearw    *areaWrite = (arearw *) 0; // fwrite; // gzip save state ability\r
27 areaeof   *areaEof   = (areaeof *) 0;\r
28 areaseek  *areaSeek  = (areaseek *) 0;\r
29 areaclose *areaClose = (areaclose *) 0;\r
30 \r
31 \r
32 // Scan one variable and callback\r
33 static int ScanVar(void *data,int len,char *name,void *PmovFile,int PmovAction)\r
34 {\r
35   int ret = 0;\r
36   if ((PmovAction&3)==1) ret = areaWrite(data,1,len,PmovFile);\r
37   if ((PmovAction&3)==2) ret = areaRead (data,1,len,PmovFile);\r
38   return (ret != len);\r
39 }\r
40 \r
41 #define SCAN_VAR(x,y) ScanVar(&x,sizeof(x),y,PmovFile,PmovAction);\r
42 #define SCANP(x)      ScanVar(&Pico.x,sizeof(Pico.x),#x,PmovFile,PmovAction);\r
43 \r
44 // Pack the cpu into a common format:\r
45 PICO_INTERNAL int PicoAreaPackCpu(unsigned char *cpu, int is_sub)\r
46 {\r
47   unsigned int pc=0;\r
48 \r
49 #ifdef EMU_A68K\r
50   memcpy(cpu,M68000_regs.d,0x40);\r
51   pc=M68000_regs.pc;\r
52   *(unsigned char *)(cpu+0x44)=(unsigned char)M68000_regs.ccr;\r
53   *(unsigned char *)(cpu+0x45)=(unsigned char)M68000_regs.srh;\r
54   *(unsigned int  *)(cpu+0x48)=M68000_regs.isp;\r
55   cpu[0x4c] = M68000_regs.irq;\r
56   // stop flag?\r
57 #endif\r
58 \r
59 #ifdef EMU_C68K\r
60   struct Cyclone *context = is_sub ? &PicoCpuS68k : &PicoCpu;\r
61   memcpy(cpu,context->d,0x40);\r
62   pc=context->pc-context->membase;\r
63   *(unsigned int *)(cpu+0x44)=CycloneGetSr(context);\r
64   *(unsigned int *)(cpu+0x48)=context->osp;\r
65   cpu[0x4c] = context->irq;\r
66   cpu[0x4d] = context->state_flags & 1;\r
67 #endif\r
68 \r
69 #ifdef EMU_M68K\r
70   void *oldcontext = m68ki_cpu_p;\r
71   m68k_set_context(is_sub ? &PicoS68kCPU : &PicoM68kCPU);\r
72   memcpy(cpu,m68ki_cpu_p->dar,0x40);\r
73   pc=m68ki_cpu_p->pc;\r
74   *(unsigned int  *)(cpu+0x44)=m68k_get_reg(NULL, M68K_REG_SR);\r
75   *(unsigned int  *)(cpu+0x48)=m68ki_cpu_p->sp[0];\r
76   cpu[0x4c] = CPU_INT_LEVEL>>8;\r
77   cpu[0x4d] = CPU_STOPPED;\r
78   m68k_set_context(oldcontext);\r
79 #endif\r
80 \r
81   *(unsigned int *)(cpu+0x40)=pc;\r
82   return 0;\r
83 }\r
84 \r
85 PICO_INTERNAL int PicoAreaUnpackCpu(unsigned char *cpu, int is_sub)\r
86 {\r
87 #ifdef EMU_A68K\r
88   memcpy(M68000_regs.d,cpu,0x40);\r
89   M68000_regs.pc=pc;\r
90   M68000_regs.ccr=*(unsigned char *)(cpu+0x44);\r
91   M68000_regs.srh=*(unsigned char *)(cpu+0x45);\r
92   M68000_regs.isp=*(unsigned int  *)(cpu+0x48);\r
93   M68000_regs.irq = cpu[0x4c];\r
94   // stop flag?\r
95 #endif\r
96 \r
97 #ifdef EMU_C68K\r
98   struct Cyclone *context = is_sub ? &PicoCpuS68k : &PicoCpu;\r
99   CycloneSetSr(context, *(unsigned int  *)(cpu+0x44));\r
100   context->osp=*(unsigned int  *)(cpu+0x48);\r
101   memcpy(context->d,cpu,0x40);\r
102   context->membase=0;\r
103   context->pc = context->checkpc(*(unsigned int *)(cpu+0x40)); // Base pc\r
104   context->irq = cpu[0x4c];\r
105   context->state_flags = 0;\r
106   if (cpu[0x4d])\r
107     context->state_flags |= 1;\r
108 #endif\r
109 \r
110 #ifdef EMU_M68K\r
111   void *oldcontext = m68ki_cpu_p;\r
112   m68k_set_context(is_sub ? &PicoS68kCPU : &PicoM68kCPU);\r
113   memcpy(m68ki_cpu_p->dar,cpu,0x40);\r
114   m68ki_cpu_p->pc=*(unsigned int *)(cpu+0x40);\r
115   m68k_set_reg(M68K_REG_SR, *(unsigned int *)(cpu+0x44));\r
116   m68ki_cpu_p->sp[0]=*(unsigned int *)(cpu+0x48);\r
117   CPU_INT_LEVEL = cpu[0x4c] << 8;\r
118   CPU_STOPPED = cpu[0x4d];\r
119   m68k_set_context(oldcontext);\r
120 #endif\r
121   return 0;\r
122 }\r
123 \r
124 // Scan the contents of the virtual machine's memory for saving or loading\r
125 static int PicoAreaScan(int PmovAction,unsigned int ver, void *PmovFile)\r
126 {\r
127   void *ym2612_regs;\r
128   unsigned char cpu[0x60];\r
129   unsigned char cpu_z80[0x60];\r
130   int ret;\r
131 \r
132   memset(&cpu,0,sizeof(cpu));\r
133   memset(&cpu_z80,0,sizeof(cpu_z80));\r
134 \r
135   ym2612_regs = YM2612GetRegs();\r
136 \r
137   if (PmovAction&4)\r
138   {\r
139     Pico.m.scanline=0;\r
140 \r
141     // Scan all the memory areas:\r
142     SCANP(ram) SCANP(vram) SCANP(zram) SCANP(cram) SCANP(vsram)\r
143 \r
144     // Pack, scan and unpack the cpu data:\r
145     if((PmovAction&3)==1) PicoAreaPackCpu(cpu, 0);\r
146     //SekInit();     // notaz: do we really have to do this here?\r
147     //PicoMemInit();\r
148     SCAN_VAR(cpu,"cpu")\r
149     if((PmovAction&3)==2) PicoAreaUnpackCpu(cpu, 0);\r
150 \r
151     SCAN_VAR(Pico.m    ,"misc")\r
152     SCAN_VAR(Pico.video,"video")\r
153 \r
154     // notaz: save/load z80, YM2612, sn76496 states instead of Pico.s (which is unused anyway)\r
155     if(PicoOpt&7) {\r
156       if((PmovAction&3)==1) z80_pack(cpu_z80);\r
157       ret = SCAN_VAR(cpu_z80,"cpu_z80")\r
158       // do not unpack if we fail to load z80 state\r
159       if((PmovAction&3)==2) {\r
160         if(ret) z80_reset();\r
161         else    z80_unpack(cpu_z80);\r
162       }\r
163     }\r
164     if(PicoOpt&3)\r
165       ScanVar(sn76496_regs,28*4,"SN76496state", PmovFile, PmovAction); // regs and other stuff\r
166     if(PicoOpt&1) {\r
167       ScanVar(ym2612_regs, 0x200+4, "YM2612state", PmovFile, PmovAction); // regs + addr line\r
168       if((PmovAction&3)==2) YM2612PicoStateLoad(); // reload YM2612 state from it's regs\r
169     }\r
170   }\r
171 \r
172   return 0;\r
173 }\r
174 \r
175 // ---------------------------------------------------------------------------\r
176 // Helper code to save/load to a file handle\r
177 \r
178 // Save or load the state from PmovFile:\r
179 int PmovState(int PmovAction, void *PmovFile)\r
180 {\r
181   int minimum=0;\r
182   unsigned char head[32];\r
183 \r
184   if (PicoMCD & 1)\r
185   {\r
186     if (PmovAction&1) return PicoCdSaveState(PmovFile);\r
187     if (PmovAction&2) return PicoCdLoadState(PmovFile);\r
188   }\r
189 \r
190   memset(head,0,sizeof(head));\r
191 \r
192   // Find out minimal compatible version:\r
193   //PicoAreaScan(PmovAction&0xc,&minimum);\r
194   minimum = 0x0021;\r
195 \r
196   memcpy(head,"Pico",4);\r
197   *(unsigned int *)(head+0x8)=PicoVer;\r
198   *(unsigned int *)(head+0xc)=minimum;\r
199 \r
200   // Scan header:\r
201   if (PmovAction&1) areaWrite(head,1,sizeof(head),PmovFile);\r
202   if (PmovAction&2) areaRead (head,1,sizeof(head),PmovFile);\r
203 \r
204   // Scan memory areas:\r
205   PicoAreaScan(PmovAction, *(unsigned int *)(head+0x8), PmovFile);\r
206 \r
207   return 0;\r
208 }\r
209 \r