2 // This file is part of the PicoDrive Megadrive Emulator
\r
4 // This code is licensed under the GNU General Public License version 2.0 and the MAME License.
\r
5 // You can choose the license that has the most advantages for you.
\r
7 // SVN repository can be found at http://code.google.com/p/cyclone68000/
\r
11 int (*PicoAcb)(struct PicoArea *)=NULL; // Area callback for each block of memory
\r
12 FILE *PmovFile=NULL;
\r
15 // Scan one variable and callback
\r
16 static void ScanVar(void *data,int len,char *name)
\r
18 struct PicoArea pa={NULL,0,NULL};
\r
19 pa.data=data; pa.len=len; pa.name=name;
\r
20 if (PicoAcb) PicoAcb(&pa);
\r
23 #define SCAN_VAR(x,y) ScanVar(&x,sizeof(x),y);
\r
24 #define SCANP(x) ScanVar(&Pico.x,sizeof(Pico.x),#x);
\r
26 // Pack the cpu into a common format:
\r
27 static int PackCpu(unsigned char *cpu)
\r
32 memcpy(cpu,M68000_regs.d,0x40);
\r
34 *(unsigned char *)(cpu+0x44)=(unsigned char)M68000_regs.ccr;
\r
35 *(unsigned char *)(cpu+0x45)=(unsigned char)M68000_regs.srh;
\r
36 *(unsigned int *)(cpu+0x48)=M68000_regs.isp;
\r
40 memcpy(cpu,PicoCpu.d,0x40);
\r
41 pc=PicoCpu.pc-PicoCpu.membase;
\r
42 *(unsigned char *)(cpu+0x44)=PicoCpu.ccr;
\r
43 *(unsigned char *)(cpu+0x45)=PicoCpu.srh;
\r
44 *(unsigned int *)(cpu+0x48)=PicoCpu.osp;
\r
47 *(unsigned int *)(cpu+0x40)=pc;
\r
51 static int UnpackCpu(unsigned char *cpu)
\r
55 pc=*(unsigned int *)(cpu+0x40);
\r
58 memcpy(M68000_regs.d,cpu,0x40);
\r
60 M68000_regs.ccr=*(unsigned char *)(cpu+0x44);
\r
61 M68000_regs.srh=*(unsigned char *)(cpu+0x45);
\r
62 M68000_regs.isp=*(unsigned int *)(cpu+0x48);
\r
66 memcpy(PicoCpu.d,cpu,0x40);
\r
68 PicoCpu.pc =PicoCpu.checkpc(pc); // Base pc
\r
69 PicoCpu.ccr=*(unsigned char *)(cpu+0x44);
\r
70 PicoCpu.srh=*(unsigned char *)(cpu+0x45);
\r
71 PicoCpu.osp=*(unsigned int *)(cpu+0x48);
\r
76 // Scan the contents of the virtual machine's memory for saving or loading
\r
77 int PicoAreaScan(int action,int *pmin)
\r
79 unsigned char cpu[0x60];
\r
81 memset(&cpu,0,sizeof(cpu));
\r
85 // Scan all the memory areas:
\r
86 SCANP(ram) SCANP(vram) SCANP(zram) SCANP(cram) SCANP(vsram)
\r
88 // Pack, scan and unpack the cpu data:
\r
95 SCAN_VAR(Pico.m ,"misc")
\r
96 SCAN_VAR(Pico.video,"video")
\r
97 SCAN_VAR(Pico.s ,"sound")
\r
102 memset(Pico.highpal,0,sizeof(Pico.highpal));
\r
103 Pico.m.dirtyPal=1; // Recalculate palette
\r
107 if (pmin) *pmin=0x0021;
\r
112 // ---------------------------------------------------------------------------
\r
113 // Helper code to save/load to a file handle
\r
115 // Area callback for each piece of Megadrive memory, read or write to the file:
\r
116 static int StateAcb(struct PicoArea *pa)
\r
118 if (PmovFile==NULL) return 1;
\r
120 if ((PmovAction&3)==1) fwrite(pa->data,1,pa->len,PmovFile);
\r
121 if ((PmovAction&3)==2) fread (pa->data,1,pa->len,PmovFile);
\r
125 // Save or load the state from PmovFile:
\r
129 unsigned char head[32];
\r
131 memset(head,0,sizeof(head));
\r
133 // Find out minial compatible version:
\r
134 PicoAreaScan(PmovAction&0xc,&minimum);
\r
136 memcpy(head,"Pico",4);
\r
137 *(unsigned int *)(head+0x8)=PicoVer;
\r
138 *(unsigned int *)(head+0xc)=minimum;
\r
141 if (PmovAction&1) fwrite(head,1,sizeof(head),PmovFile);
\r
142 if (PmovAction&2) fread (head,1,sizeof(head),PmovFile);
\r
144 // Scan memory areas:
\r
146 PicoAreaScan(PmovAction,NULL);
\r
154 if (PmovFile==NULL) return 1;
\r
156 if ((PmovAction&3)==0) return 0;
\r
158 PicoPad[1]=0; // Make sure pad #2 is blank
\r
159 if (PmovAction&1) ret=fwrite(PicoPad,1,2,PmovFile);
\r
160 if (PmovAction&2) ret=fread (PicoPad,1,2,PmovFile);
\r
165 fclose(PmovFile); PmovFile=NULL;
\r