--- /dev/null
+\r
+#include "PicoInt.h"\r
+\r
+int (*PicoAcb)(struct PicoArea *)=NULL; // Area callback for each block of memory\r
+FILE *PmovFile=NULL;\r
+int PmovAction=0;\r
+\r
+// Scan one variable and callback\r
+static void ScanVar(void *data,int len,char *name)\r
+{\r
+ struct PicoArea pa={NULL,0,NULL};\r
+ pa.data=data; pa.len=len; pa.name=name;\r
+ if (PicoAcb) PicoAcb(&pa);\r
+}\r
+\r
+#define SCAN_VAR(x,y) ScanVar(&x,sizeof(x),y);\r
+#define SCANP(x) ScanVar(&Pico.x,sizeof(Pico.x),#x);\r
+\r
+// Pack the cpu into a common format:\r
+static int PackCpu(unsigned char *cpu)\r
+{\r
+ unsigned int pc=0;\r
+\r
+#ifdef EMU_A68K\r
+ memcpy(cpu,M68000_regs.d,0x40);\r
+ pc=M68000_regs.pc;\r
+ *(unsigned char *)(cpu+0x44)=(unsigned char)M68000_regs.ccr;\r
+ *(unsigned char *)(cpu+0x45)=(unsigned char)M68000_regs.srh;\r
+ *(unsigned int *)(cpu+0x48)=M68000_regs.isp;\r
+#endif\r
+\r
+#ifdef EMU_C68K\r
+ memcpy(cpu,PicoCpu.d,0x40);\r
+ pc=PicoCpu.pc-PicoCpu.membase;\r
+ *(unsigned char *)(cpu+0x44)=PicoCpu.ccr;\r
+ *(unsigned char *)(cpu+0x45)=PicoCpu.srh;\r
+ *(unsigned int *)(cpu+0x48)=PicoCpu.osp;\r
+#endif\r
+\r
+ *(unsigned int *)(cpu+0x40)=pc;\r
+ return 0;\r
+}\r
+\r
+static int UnpackCpu(unsigned char *cpu)\r
+{\r
+ unsigned int pc=0;\r
+\r
+ pc=*(unsigned int *)(cpu+0x40);\r
+\r
+#ifdef EMU_A68K\r
+ memcpy(M68000_regs.d,cpu,0x40);\r
+ M68000_regs.pc=pc;\r
+ M68000_regs.ccr=*(unsigned char *)(cpu+0x44);\r
+ M68000_regs.srh=*(unsigned char *)(cpu+0x45);\r
+ M68000_regs.isp=*(unsigned int *)(cpu+0x48);\r
+#endif\r
+\r
+#ifdef EMU_C68K\r
+ memcpy(PicoCpu.d,cpu,0x40);\r
+ PicoCpu.membase=0;\r
+ PicoCpu.pc =PicoCpu.checkpc(pc); // Base pc\r
+ PicoCpu.ccr=*(unsigned char *)(cpu+0x44);\r
+ PicoCpu.srh=*(unsigned char *)(cpu+0x45);\r
+ PicoCpu.osp=*(unsigned int *)(cpu+0x48);\r
+#endif\r
+ return 0;\r
+}\r
+\r
+// Scan the contents of the virtual machine's memory for saving or loading\r
+int PicoAreaScan(int action,int *pmin)\r
+{\r
+ unsigned char cpu[0x60];\r
+\r
+ memset(&cpu,0,sizeof(cpu));\r
+\r
+ if (action&4)\r
+ {\r
+ // Scan all the memory areas:\r
+ SCANP(ram) SCANP(vram) SCANP(zram) SCANP(cram) SCANP(vsram)\r
+\r
+ // Pack, scan and unpack the cpu data:\r
+ PackCpu(cpu);\r
+ SekInit();\r
+ PicoMemInit();\r
+ SCAN_VAR(cpu,"cpu")\r
+ UnpackCpu(cpu);\r
+\r
+ SCAN_VAR(Pico.m ,"misc")\r
+ SCAN_VAR(Pico.video,"video")\r
+ SCAN_VAR(Pico.s ,"sound")\r
+\r
+ Pico.m.scanline=0;\r
+ if (action&2)\r
+ {\r
+ memset(Pico.highpal,0,sizeof(Pico.highpal));\r
+ Pico.m.dirtyPal=1; // Recalculate palette\r
+ }\r
+ }\r
+\r
+ if (pmin) *pmin=0x0021;\r
+\r
+ return 0;\r
+}\r
+\r
+// ---------------------------------------------------------------------------\r
+// Helper code to save/load to a file handle\r
+\r
+// Area callback for each piece of Megadrive memory, read or write to the file:\r
+static int StateAcb(struct PicoArea *pa)\r
+{\r
+ if (PmovFile==NULL) return 1;\r
+\r
+ if ((PmovAction&3)==1) fwrite(pa->data,1,pa->len,PmovFile);\r
+ if ((PmovAction&3)==2) fread (pa->data,1,pa->len,PmovFile);\r
+ return 0;\r
+}\r
+\r
+// Save or load the state from PmovFile:\r
+int PmovState()\r
+{\r
+ int minimum=0;\r
+ unsigned char head[32];\r
+\r
+ memset(head,0,sizeof(head));\r
+\r
+ // Find out minial compatible version:\r
+ PicoAreaScan(PmovAction&0xc,&minimum); \r
+\r
+ memcpy(head,"Pico",4);\r
+ *(unsigned int *)(head+0x8)=PicoVer;\r
+ *(unsigned int *)(head+0xc)=minimum;\r
+\r
+ // Scan header:\r
+ if (PmovAction&1) fwrite(head,1,sizeof(head),PmovFile);\r
+ if (PmovAction&2) fread (head,1,sizeof(head),PmovFile);\r
+\r
+ // Scan memory areas:\r
+ PicoAcb=StateAcb;\r
+ PicoAreaScan(PmovAction,NULL);\r
+ PicoAcb=NULL;\r
+ return 0;\r
+}\r
+\r
+int PmovUpdate()\r
+{\r
+ int ret=0;\r
+ if (PmovFile==NULL) return 1;\r
+ \r
+ if ((PmovAction&3)==0) return 0;\r
+\r
+ PicoPad[1]=0; // Make sure pad #2 is blank\r
+ if (PmovAction&1) ret=fwrite(PicoPad,1,2,PmovFile);\r
+ if (PmovAction&2) ret=fread (PicoPad,1,2,PmovFile);\r
+\r
+ if (ret!=2)\r
+ {\r
+ // End of file\r
+ fclose(PmovFile); PmovFile=NULL;\r
+ PmovAction=0;\r
+ }\r
+\r
+ return 0;\r
+}\r