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
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 PmovAction)
\r
38 if ((PmovAction&3)==1) ret = areaWrite(data,1,len,PmovFile);
\r
39 if ((PmovAction&3)==2) ret = areaRead (data,1,len,PmovFile);
\r
40 return (ret != len);
\r
43 #define SCAN_VAR(x,y) ScanVar(&x,sizeof(x),y,PmovFile,PmovAction);
\r
44 #define SCANP(x) ScanVar(&Pico.x,sizeof(Pico.x),#x,PmovFile,PmovAction);
\r
46 // Pack the cpu into a common format:
\r
47 PICO_INTERNAL int PicoAreaPackCpu(unsigned char *cpu, int is_sub)
\r
51 #if defined(EMU_C68K)
\r
52 struct Cyclone *context = is_sub ? &PicoCpuCS68k : &PicoCpuCM68k;
\r
53 memcpy(cpu,context->d,0x40);
\r
54 pc=context->pc-context->membase;
\r
55 *(unsigned int *)(cpu+0x44)=CycloneGetSr(context);
\r
56 *(unsigned int *)(cpu+0x48)=context->osp;
\r
57 cpu[0x4c] = context->irq;
\r
58 cpu[0x4d] = context->state_flags & 1;
\r
59 #elif defined(EMU_M68K)
\r
60 void *oldcontext = m68ki_cpu_p;
\r
61 m68k_set_context(is_sub ? &PicoCpuMS68k : &PicoCpuMM68k);
\r
62 memcpy(cpu,m68ki_cpu_p->dar,0x40);
\r
64 *(unsigned int *)(cpu+0x44)=m68k_get_reg(NULL, M68K_REG_SR);
\r
65 *(unsigned int *)(cpu+0x48)=m68ki_cpu_p->sp[0];
\r
66 cpu[0x4c] = CPU_INT_LEVEL>>8;
\r
67 cpu[0x4d] = CPU_STOPPED;
\r
68 m68k_set_context(oldcontext);
\r
69 #elif defined(EMU_F68K)
\r
70 M68K_CONTEXT *context = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k;
\r
71 memcpy(cpu,context->dreg,0x40);
\r
73 *(unsigned int *)(cpu+0x44)=context->sr;
\r
74 *(unsigned int *)(cpu+0x48)=context->asp;
\r
75 cpu[0x4c] = context->interrupts[0];
\r
76 cpu[0x4d] = (context->execinfo & FM68K_HALTED) ? 1 : 0;
\r
79 *(unsigned int *)(cpu+0x40)=pc;
\r
83 PICO_INTERNAL int PicoAreaUnpackCpu(unsigned char *cpu, int is_sub)
\r
85 #if defined(EMU_C68K)
\r
86 struct Cyclone *context = is_sub ? &PicoCpuCS68k : &PicoCpuCM68k;
\r
87 CycloneSetSr(context, *(unsigned int *)(cpu+0x44));
\r
88 context->osp=*(unsigned int *)(cpu+0x48);
\r
89 memcpy(context->d,cpu,0x40);
\r
91 context->pc = context->checkpc(*(unsigned int *)(cpu+0x40)); // Base pc
\r
92 context->irq = cpu[0x4c];
\r
93 context->state_flags = 0;
\r
95 context->state_flags |= 1;
\r
96 #elif defined(EMU_M68K)
\r
97 void *oldcontext = m68ki_cpu_p;
\r
98 m68k_set_context(is_sub ? &PicoCpuMS68k : &PicoCpuMM68k);
\r
99 memcpy(m68ki_cpu_p->dar,cpu,0x40);
\r
100 m68ki_cpu_p->pc=*(unsigned int *)(cpu+0x40);
\r
101 m68k_set_reg(M68K_REG_SR, *(unsigned int *)(cpu+0x44));
\r
102 m68ki_cpu_p->sp[0]=*(unsigned int *)(cpu+0x48);
\r
103 CPU_INT_LEVEL = cpu[0x4c] << 8;
\r
104 CPU_STOPPED = cpu[0x4d];
\r
105 m68k_set_context(oldcontext);
\r
106 #elif defined(EMU_F68K)
\r
107 M68K_CONTEXT *context = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k;
\r
108 memcpy(context->dreg,cpu,0x40);
\r
109 context->pc =*(unsigned int *)(cpu+0x40);
\r
110 context->sr =*(unsigned int *)(cpu+0x44);
\r
111 context->asp=*(unsigned int *)(cpu+0x48);
\r
112 context->interrupts[0] = cpu[0x4c];
\r
113 context->execinfo &= ~FM68K_HALTED;
\r
114 if (cpu[0x4d]&1) context->execinfo |= FM68K_HALTED;
\r
119 // Scan the contents of the virtual machine's memory for saving or loading
\r
120 static int PicoAreaScan(int PmovAction,unsigned int ver, void *PmovFile)
\r
123 unsigned char cpu[0x60];
\r
124 unsigned char cpu_z80[0x60];
\r
127 memset(&cpu,0,sizeof(cpu));
\r
128 memset(&cpu_z80,0,sizeof(cpu_z80));
\r
130 ym2612_regs = YM2612GetRegs();
\r
136 // Scan all the memory areas:
\r
137 SCANP(ram) SCANP(vram) SCANP(zram) SCANP(cram) SCANP(vsram)
\r
139 // Pack, scan and unpack the cpu data:
\r
140 if((PmovAction&3)==1) PicoAreaPackCpu(cpu, 0);
\r
142 SCAN_VAR(cpu,"cpu")
\r
143 if((PmovAction&3)==2) PicoAreaUnpackCpu(cpu, 0);
\r
145 SCAN_VAR(Pico.m ,"misc")
\r
146 SCAN_VAR(Pico.video,"video")
\r
149 if((PmovAction&3)==1) z80_pack(cpu_z80);
\r
150 ret = SCAN_VAR(cpu_z80,"cpu_z80")
\r
151 // do not unpack if we fail to load z80 state
\r
152 if((PmovAction&3)==2) {
\r
153 if(ret) z80_reset();
\r
154 else z80_unpack(cpu_z80);
\r
158 ScanVar(sn76496_regs,28*4,"SN76496state", PmovFile, PmovAction); // regs and other stuff
\r
160 if((PmovAction&3)==1) ym2612_pack_state();
\r
161 ScanVar(ym2612_regs, 0x200+4, "YM2612state", PmovFile, PmovAction); // regs + addr line
\r
162 if((PmovAction&3)==2) ym2612_unpack_state(); // reload YM2612 state from it's regs
\r
169 // ---------------------------------------------------------------------------
\r
170 // Helper code to save/load to a file handle
\r
172 // Save or load the state from PmovFile:
\r
173 int PmovState(int PmovAction, void *PmovFile)
\r
176 unsigned char head[32];
\r
178 if ((PicoAHW & PAHW_MCD) || carthw_chunks != NULL)
\r
180 if (PmovAction&1) return PicoCdSaveState(PmovFile);
\r
181 if (PmovAction&2) {
\r
182 int ret = PicoCdLoadState(PmovFile);
\r
183 if (PicoLoadStateHook) PicoLoadStateHook();
\r
188 memset(head,0,sizeof(head));
\r
190 // Find out minimal compatible version:
\r
191 //PicoAreaScan(PmovAction&0xc,&minimum);
\r
194 memcpy(head,"Pico",4);
\r
195 *(unsigned int *)(head+0x8)=PicoVer;
\r
196 *(unsigned int *)(head+0xc)=minimum;
\r
199 if (PmovAction&1) areaWrite(head,1,sizeof(head),PmovFile);
\r
200 if (PmovAction&2) areaRead (head,1,sizeof(head),PmovFile);
\r
202 // Scan memory areas:
\r
203 PicoAreaScan(PmovAction, *(unsigned int *)(head+0x8), PmovFile);
\r
205 if ((PmovAction&2) && PicoLoadStateHook) PicoLoadStateHook();
\r