1 // This is part of Pico Library
\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
7 // For commercial use, separate licencing terms must be obtained.
\r
10 #include "PicoInt.h"
\r
13 #include "sound/ym2612.h"
\r
16 extern int *sn76496_regs;
\r
19 struct PicoArea { void *data; int len; char *name; };
\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
25 arearw *areaRead = (arearw *) 0; // fread; // read and write function pointers for
\r
26 arearw *areaWrite = (arearw *) 0; // fwrite; // gzip save state ability
\r
29 // Scan one variable and callback
\r
30 static int ScanVar(void *data,int len,char *name,void *PmovFile,int PmovAction)
\r
33 if ((PmovAction&3)==1) ret = areaWrite(data,1,len,PmovFile);
\r
34 if ((PmovAction&3)==2) ret = areaRead (data,1,len,PmovFile);
\r
35 return (ret != len);
\r
38 #define SCAN_VAR(x,y) ScanVar(&x,sizeof(x),y,PmovFile,PmovAction);
\r
39 #define SCANP(x) ScanVar(&Pico.x,sizeof(Pico.x),#x,PmovFile,PmovAction);
\r
41 // Pack the cpu into a common format:
\r
42 static int PackCpu(unsigned char *cpu)
\r
47 memcpy(cpu,M68000_regs.d,0x40);
\r
49 *(unsigned char *)(cpu+0x44)=(unsigned char)M68000_regs.ccr;
\r
50 *(unsigned char *)(cpu+0x45)=(unsigned char)M68000_regs.srh;
\r
51 *(unsigned int *)(cpu+0x48)=M68000_regs.isp;
\r
55 memcpy(cpu,PicoCpu.d,0x40);
\r
56 pc=PicoCpu.pc-PicoCpu.membase;
\r
57 *(unsigned int *)(cpu+0x44)=CycloneGetSr(&PicoCpu);
\r
58 *(unsigned int *)(cpu+0x48)=PicoCpu.osp;
\r
62 memcpy(cpu,m68ki_cpu.dar,0x40);
\r
64 *(unsigned int *)(cpu+0x44)=m68k_get_reg(NULL, M68K_REG_SR);
\r
65 *(unsigned int *)(cpu+0x48)=m68ki_cpu.sp[0];
\r
68 *(unsigned int *)(cpu+0x40)=pc;
\r
72 static int UnpackCpu(unsigned char *cpu)
\r
76 pc=*(unsigned int *)(cpu+0x40);
\r
79 memcpy(M68000_regs.d,cpu,0x40);
\r
81 M68000_regs.ccr=*(unsigned char *)(cpu+0x44);
\r
82 M68000_regs.srh=*(unsigned char *)(cpu+0x45);
\r
83 M68000_regs.isp=*(unsigned int *)(cpu+0x48);
\r
87 CycloneSetSr(&PicoCpu, *(unsigned int *)(cpu+0x44));
\r
88 PicoCpu.osp=*(unsigned int *)(cpu+0x48);
\r
89 memcpy(PicoCpu.d,cpu,0x40);
\r
91 PicoCpu.pc =PicoCpu.checkpc(pc); // Base pc
\r
95 memcpy(m68ki_cpu.dar,cpu,0x40);
\r
97 m68k_set_reg(M68K_REG_SR, *(unsigned int *)(cpu+0x44));
\r
98 m68ki_cpu.sp[0]=*(unsigned int *)(cpu+0x48);
\r
103 // Scan the contents of the virtual machine's memory for saving or loading
\r
104 static int PicoAreaScan(int PmovAction,unsigned int ver, void *PmovFile)
\r
107 unsigned char cpu[0x60];
\r
108 unsigned char cpu_z80[0x60];
\r
111 memset(&cpu,0,sizeof(cpu));
\r
112 memset(&cpu_z80,0,sizeof(cpu_z80));
\r
114 ym2612_regs = YM2612GetRegs();
\r
120 // Scan all the memory areas:
\r
121 SCANP(ram) SCANP(vram) SCANP(zram) SCANP(cram) SCANP(vsram)
\r
123 // Pack, scan and unpack the cpu data:
\r
124 if((PmovAction&3)==1) PackCpu(cpu);
\r
125 //SekInit(); // notaz: do we really have to do this here?
\r
127 SCAN_VAR(cpu,"cpu")
\r
128 if((PmovAction&3)==2) UnpackCpu(cpu);
\r
130 SCAN_VAR(Pico.m ,"misc")
\r
131 SCAN_VAR(Pico.video,"video")
\r
134 if(ver == 0x0030) { // zram was being saved incorrectly in 0x0030 (byteswaped?)
\r
135 Byteswap(Pico.zram, 0x2000);
\r
136 return 0; // do not try to load sound stuff
\r
139 //SCAN_VAR(Pico.s ,"sound")
\r
140 // notaz: save/load z80, YM2612, sn76496 states instead of Pico.s (which is unused anyway)
\r
142 if((PmovAction&3)==1) z80_pack(cpu_z80);
\r
143 ret = SCAN_VAR(cpu_z80,"cpu_z80")
\r
144 // do not unpack if we fail to load z80 state
\r
145 if((PmovAction&3)==2) {
\r
146 if(ret) z80_reset();
\r
147 else z80_unpack(cpu_z80);
\r
151 ScanVar(sn76496_regs,28*4,"SN76496state", PmovFile, PmovAction); // regs and other stuff
\r
153 ScanVar(ym2612_regs, 0x200+4, "YM2612state", PmovFile, PmovAction); // regs + addr line
\r
154 if((PmovAction&3)==2) YM2612PicoStateLoad(); // reload YM2612 state from it's regs
\r
161 // ---------------------------------------------------------------------------
\r
162 // Helper code to save/load to a file handle
\r
164 // Save or load the state from PmovFile:
\r
165 int PmovState(int PmovAction, void *PmovFile)
\r
168 unsigned char head[32];
\r
170 memset(head,0,sizeof(head));
\r
172 // Find out minimal compatible version:
\r
173 //PicoAreaScan(PmovAction&0xc,&minimum);
\r
176 memcpy(head,"Pico",4);
\r
177 *(unsigned int *)(head+0x8)=PicoVer;
\r
178 *(unsigned int *)(head+0xc)=minimum;
\r
181 if (PmovAction&1) areaWrite(head,1,sizeof(head),PmovFile);
\r
182 if (PmovAction&2) areaRead (head,1,sizeof(head),PmovFile);
\r
184 // Scan memory areas:
\r
185 PicoAreaScan(PmovAction, *(unsigned int *)(head+0x8), PmovFile);
\r