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 "pico_int.h"
\r
11 #include <zlib/zlib.h>
\r
14 #include "sound/ym2612.h"
\r
17 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
27 areaeof *areaEof = (areaeof *) 0;
\r
28 areaseek *areaSeek = (areaseek *) 0;
\r
29 areaclose *areaClose = (areaclose *) 0;
\r
31 void (*PicoLoadStateHook)(void) = NULL;
\r
34 // Scan one variable and callback
\r
35 static int ScanVar(void *data,int len,char *name,void *PmovFile,int is_write)
\r
39 ret = areaWrite(data,1,len,PmovFile);
\r
41 ret = areaRead (data,1,len,PmovFile);
\r
42 return (ret != len);
\r
45 #define SCAN_VAR(x,y) ScanVar(&x,sizeof(x),y,PmovFile,is_write);
\r
46 #define SCANP(x) ScanVar(&Pico.x,sizeof(Pico.x),#x,PmovFile,is_write);
\r
48 // Pack the cpu into a common format:
\r
49 PICO_INTERNAL void PicoAreaPackCpu(unsigned char *cpu, int is_sub)
\r
53 #if defined(EMU_C68K)
\r
54 struct Cyclone *context = is_sub ? &PicoCpuCS68k : &PicoCpuCM68k;
\r
55 memcpy(cpu,context->d,0x40);
\r
56 pc=context->pc-context->membase;
\r
57 *(unsigned int *)(cpu+0x44)=CycloneGetSr(context);
\r
58 *(unsigned int *)(cpu+0x48)=context->osp;
\r
59 cpu[0x4c] = context->irq;
\r
60 cpu[0x4d] = context->state_flags & 1;
\r
61 #elif defined(EMU_M68K)
\r
62 void *oldcontext = m68ki_cpu_p;
\r
63 m68k_set_context(is_sub ? &PicoCpuMS68k : &PicoCpuMM68k);
\r
64 memcpy(cpu,m68ki_cpu_p->dar,0x40);
\r
66 *(unsigned int *)(cpu+0x44)=m68k_get_reg(NULL, M68K_REG_SR);
\r
67 *(unsigned int *)(cpu+0x48)=m68ki_cpu_p->sp[m68ki_cpu_p->s_flag^SFLAG_SET];
\r
68 cpu[0x4c] = CPU_INT_LEVEL>>8;
\r
69 cpu[0x4d] = CPU_STOPPED;
\r
70 m68k_set_context(oldcontext);
\r
71 #elif defined(EMU_F68K)
\r
72 M68K_CONTEXT *context = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k;
\r
73 memcpy(cpu,context->dreg,0x40);
\r
75 *(unsigned int *)(cpu+0x44)=context->sr;
\r
76 *(unsigned int *)(cpu+0x48)=context->asp;
\r
77 cpu[0x4c] = context->interrupts[0];
\r
78 cpu[0x4d] = (context->execinfo & FM68K_HALTED) ? 1 : 0;
\r
81 *(unsigned int *)(cpu+0x40)=pc;
\r
84 PICO_INTERNAL void PicoAreaUnpackCpu(unsigned char *cpu, int is_sub)
\r
86 #if defined(EMU_C68K)
\r
87 struct Cyclone *context = is_sub ? &PicoCpuCS68k : &PicoCpuCM68k;
\r
88 CycloneSetSr(context, *(unsigned int *)(cpu+0x44));
\r
89 context->osp=*(unsigned int *)(cpu+0x48);
\r
90 memcpy(context->d,cpu,0x40);
\r
91 context->membase = 0;
\r
92 context->pc = *(unsigned int *)(cpu+0x40);
\r
93 CycloneUnpack(context, NULL); // rebase PC
\r
94 context->irq = cpu[0x4c];
\r
95 context->state_flags = 0;
\r
97 context->state_flags |= 1;
\r
98 #elif defined(EMU_M68K)
\r
99 void *oldcontext = m68ki_cpu_p;
\r
100 m68k_set_context(is_sub ? &PicoCpuMS68k : &PicoCpuMM68k);
\r
101 m68k_set_reg(M68K_REG_SR, *(unsigned int *)(cpu+0x44));
\r
102 memcpy(m68ki_cpu_p->dar,cpu,0x40);
\r
103 m68ki_cpu_p->pc=*(unsigned int *)(cpu+0x40);
\r
104 m68ki_cpu_p->sp[m68ki_cpu_p->s_flag^SFLAG_SET]=*(unsigned int *)(cpu+0x48);
\r
105 CPU_INT_LEVEL = cpu[0x4c] << 8;
\r
106 CPU_STOPPED = cpu[0x4d];
\r
107 m68k_set_context(oldcontext);
\r
108 #elif defined(EMU_F68K)
\r
109 M68K_CONTEXT *context = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k;
\r
110 memcpy(context->dreg,cpu,0x40);
\r
111 context->pc =*(unsigned int *)(cpu+0x40);
\r
112 context->sr =*(unsigned int *)(cpu+0x44);
\r
113 context->asp=*(unsigned int *)(cpu+0x48);
\r
114 context->interrupts[0] = cpu[0x4c];
\r
115 context->execinfo &= ~FM68K_HALTED;
\r
116 if (cpu[0x4d]&1) context->execinfo |= FM68K_HALTED;
\r
120 // Scan the contents of the virtual machine's memory for saving or loading
\r
121 static int PicoAreaScan(int is_write, unsigned int ver, void *PmovFile)
\r
124 unsigned char cpu[0x60];
\r
125 unsigned char cpu_z80[0x60];
\r
128 memset(&cpu,0,sizeof(cpu));
\r
129 memset(&cpu_z80,0,sizeof(cpu_z80));
\r
132 ym2612_regs = YM2612GetRegs();
\r
134 // Scan all the memory areas:
\r
135 SCANP(ram) SCANP(vram) SCANP(zram) SCANP(cram) SCANP(vsram)
\r
137 // Pack, scan and unpack the cpu data:
\r
139 PicoAreaPackCpu(cpu, 0);
\r
140 SCAN_VAR(cpu,"cpu")
\r
142 PicoAreaUnpackCpu(cpu, 0);
\r
144 SCAN_VAR(Pico.m ,"misc")
\r
145 SCAN_VAR(Pico.video,"video")
\r
147 // no longer keeping eeprom data in sram_reg
\r
148 if (!is_write && (Pico.m.sram_reg & 4))
\r
149 Pico.m.sram_reg = SRR_MAPPED;
\r
153 ret = SCAN_VAR(cpu_z80,"cpu_z80")
\r
154 // do not unpack if we fail to load z80 state
\r
156 if (ret) z80_reset();
\r
157 else z80_unpack(cpu_z80);
\r
160 ScanVar(sn76496_regs, 28*4, "SN76496state", PmovFile, is_write);
\r
162 ym2612_pack_state();
\r
163 ret = ScanVar(ym2612_regs, 0x200+4, "YM2612state", PmovFile, is_write); // regs + addr line
\r
164 if (!is_write && !ret)
\r
165 ym2612_unpack_state();
\r
170 // ---------------------------------------------------------------------------
\r
171 // Helper code to save/load to a file handle
\r
173 // XXX: error checking
\r
174 // Save or load the state from PmovFile:
\r
175 static int PmovState(int is_write, void *PmovFile)
\r
177 unsigned char head[32];
\r
179 if ((PicoAHW & PAHW_MCD) || carthw_chunks != NULL)
\r
182 return PicoCdSaveState(PmovFile);
\r
184 int ret = PicoCdLoadState(PmovFile);
\r
185 if (PicoLoadStateHook) PicoLoadStateHook();
\r
190 memset(head,0,sizeof(head));
\r
192 // not really used..
\r
193 memcpy(head,"Pico",4);
\r
194 *(unsigned int *)(head+0x8)=0x0133;
\r
195 *(unsigned int *)(head+0xc)=0x0021;
\r
199 areaWrite(head,1,sizeof(head),PmovFile);
\r
201 areaRead (head,1,sizeof(head),PmovFile);
\r
203 // Scan memory areas:
\r
204 PicoAreaScan(is_write, *(unsigned int *)(head+0x8), PmovFile);
\r
206 if (!is_write && PicoLoadStateHook)
\r
207 PicoLoadStateHook();
\r
212 static size_t gzRead2(void *p, size_t _size, size_t _n, void *file)
\r
214 return gzread(file, p, _n);
\r
217 static size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)
\r
219 return gzwrite(file, p, _n);
\r
222 static void set_cbs(int gz)
\r
225 areaRead = gzRead2;
\r
226 areaWrite = gzWrite2;
\r
227 areaEof = (areaeof *) gzeof;
\r
228 areaSeek = (areaseek *) gzseek;
\r
229 areaClose = (areaclose *) gzclose;
\r
231 areaRead = (arearw *) fread;
\r
232 areaWrite = (arearw *) fwrite;
\r
233 areaEof = (areaeof *) feof;
\r
234 areaSeek = (areaseek *) fseek;
\r
235 areaClose = (areaclose *) fclose;
\r
239 int PicoState(const char *fname, int is_save)
\r
241 void *afile = NULL;
\r
244 if (strcmp(fname + strlen(fname) - 3, ".gz") == 0)
\r
246 if ( (afile = gzopen(fname, is_save ? "wb" : "rb")) ) {
\r
249 gzsetparams(afile, 9, Z_DEFAULT_STRATEGY);
\r
254 if ( (afile = fopen(fname, is_save ? "wb" : "rb")) ) {
\r
262 ret = PmovState(is_save, afile);
\r
270 int PicoStateLoadVDP(const char *fname)
\r
272 void *afile = NULL;
\r
273 if (strcmp(fname + strlen(fname) - 3, ".gz") == 0)
\r
275 if ( (afile = gzopen(fname, "rb")) )
\r
280 if ( (afile = fopen(fname, "rb")) )
\r
286 if ((PicoAHW & PAHW_MCD) || carthw_chunks != NULL) {
\r
287 PicoCdLoadStateGfx(afile);
\r
289 areaSeek(afile, 0x10020, SEEK_SET); // skip header and RAM in state file
\r
290 areaRead(Pico.vram, 1, sizeof(Pico.vram), afile);
\r
291 areaSeek(afile, 0x2000, SEEK_CUR);
\r
292 areaRead(Pico.cram, 1, sizeof(Pico.cram), afile);
\r
293 areaRead(Pico.vsram, 1, sizeof(Pico.vsram), afile);
\r
294 areaSeek(afile, 0x221a0, SEEK_SET);
\r
295 areaRead(&Pico.video, 1, sizeof(Pico.video), afile);
\r