final fixes for UIQ3
[picodrive.git] / Pico / Area.c
CommitLineData
cc68a136 1// This is part of Pico Library\r
2\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
6\r
7// For commercial use, separate licencing terms must be obtained.\r
8\r
9\r
10#include "PicoInt.h"\r
11\r
12// ym2612\r
13#include "sound/ym2612.h"\r
14\r
15// sn76496\r
16extern int *sn76496_regs;\r
17\r
18\r
19struct PicoArea { void *data; int len; char *name; };\r
20\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
24\r
860c6322 25arearw *areaRead = (arearw *) 0; // fread; // read and write function pointers for\r
26arearw *areaWrite = (arearw *) 0; // fwrite; // gzip save state ability\r
27areaeof *areaEof = (areaeof *) 0;\r
28areaseek *areaSeek = (areaseek *) 0;\r
29areaclose *areaClose = (areaclose *) 0;\r
cc68a136 30\r
fad24893 31void (*PicoLoadStateHook)(void) = NULL;\r
32\r
cc68a136 33\r
34// Scan one variable and callback\r
35static int ScanVar(void *data,int len,char *name,void *PmovFile,int PmovAction)\r
36{\r
37 int ret = 0;\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
41}\r
42\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
45\r
46// Pack the cpu into a common format:\r
2aa27095 47PICO_INTERNAL void PicoAreaPackCpu(unsigned char *cpu, int is_sub)\r
cc68a136 48{\r
49 unsigned int pc=0;\r
50\r
3aa1e148 51#if defined(EMU_C68K)\r
52 struct Cyclone *context = is_sub ? &PicoCpuCS68k : &PicoCpuCM68k;\r
51a902ae 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
a4030801 57 cpu[0x4c] = context->irq;\r
0af33fe0 58 cpu[0x4d] = context->state_flags & 1;\r
3aa1e148 59#elif defined(EMU_M68K)\r
83bd0b76 60 void *oldcontext = m68ki_cpu_p;\r
3aa1e148 61 m68k_set_context(is_sub ? &PicoCpuMS68k : &PicoCpuMM68k);\r
83bd0b76 62 memcpy(cpu,m68ki_cpu_p->dar,0x40);\r
63 pc=m68ki_cpu_p->pc;\r
cc68a136 64 *(unsigned int *)(cpu+0x44)=m68k_get_reg(NULL, M68K_REG_SR);\r
58b75cc5 65 *(unsigned int *)(cpu+0x48)=m68ki_cpu_p->sp[m68ki_cpu_p->s_flag^SFLAG_SET];\r
a4030801 66 cpu[0x4c] = CPU_INT_LEVEL>>8;\r
67 cpu[0x4d] = CPU_STOPPED;\r
83bd0b76 68 m68k_set_context(oldcontext);\r
3aa1e148 69#elif defined(EMU_F68K)\r
70 M68K_CONTEXT *context = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k;\r
71 memcpy(cpu,context->dreg,0x40);\r
72 pc=context->pc;\r
73 *(unsigned int *)(cpu+0x44)=context->sr;\r
74 *(unsigned int *)(cpu+0x48)=context->asp;\r
75 cpu[0x4c] = context->interrupts[0];\r
03e4f2a3 76 cpu[0x4d] = (context->execinfo & FM68K_HALTED) ? 1 : 0;\r
cc68a136 77#endif\r
78\r
79 *(unsigned int *)(cpu+0x40)=pc;\r
cc68a136 80}\r
81\r
2aa27095 82PICO_INTERNAL void PicoAreaUnpackCpu(unsigned char *cpu, int is_sub)\r
cc68a136 83{\r
3aa1e148 84#if defined(EMU_C68K)\r
85 struct Cyclone *context = is_sub ? &PicoCpuCS68k : &PicoCpuCM68k;\r
86 CycloneSetSr(context, *(unsigned int *)(cpu+0x44));\r
87 context->osp=*(unsigned int *)(cpu+0x48);\r
51a902ae 88 memcpy(context->d,cpu,0x40);\r
89 context->membase=0;\r
90 context->pc = context->checkpc(*(unsigned int *)(cpu+0x40)); // Base pc\r
a4030801 91 context->irq = cpu[0x4c];\r
0af33fe0 92 context->state_flags = 0;\r
93 if (cpu[0x4d])\r
94 context->state_flags |= 1;\r
3aa1e148 95#elif defined(EMU_M68K)\r
83bd0b76 96 void *oldcontext = m68ki_cpu_p;\r
3aa1e148 97 m68k_set_context(is_sub ? &PicoCpuMS68k : &PicoCpuMM68k);\r
58b75cc5 98 m68k_set_reg(M68K_REG_SR, *(unsigned int *)(cpu+0x44));\r
83bd0b76 99 memcpy(m68ki_cpu_p->dar,cpu,0x40);\r
100 m68ki_cpu_p->pc=*(unsigned int *)(cpu+0x40);\r
58b75cc5 101 m68ki_cpu_p->sp[m68ki_cpu_p->s_flag^SFLAG_SET]=*(unsigned int *)(cpu+0x48);\r
a4030801 102 CPU_INT_LEVEL = cpu[0x4c] << 8;\r
103 CPU_STOPPED = cpu[0x4d];\r
83bd0b76 104 m68k_set_context(oldcontext);\r
3aa1e148 105#elif defined(EMU_F68K)\r
106 M68K_CONTEXT *context = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k;\r
107 memcpy(context->dreg,cpu,0x40);\r
108 context->pc =*(unsigned int *)(cpu+0x40);\r
109 context->sr =*(unsigned int *)(cpu+0x44);\r
110 context->asp=*(unsigned int *)(cpu+0x48);\r
111 context->interrupts[0] = cpu[0x4c];\r
03e4f2a3 112 context->execinfo &= ~FM68K_HALTED;\r
113 if (cpu[0x4d]&1) context->execinfo |= FM68K_HALTED;\r
cc68a136 114#endif\r
cc68a136 115}\r
116\r
117// Scan the contents of the virtual machine's memory for saving or loading\r
118static int PicoAreaScan(int PmovAction,unsigned int ver, void *PmovFile)\r
119{\r
120 void *ym2612_regs;\r
121 unsigned char cpu[0x60];\r
122 unsigned char cpu_z80[0x60];\r
123 int ret;\r
124\r
125 memset(&cpu,0,sizeof(cpu));\r
126 memset(&cpu_z80,0,sizeof(cpu_z80));\r
127\r
128 ym2612_regs = YM2612GetRegs();\r
129\r
130 if (PmovAction&4)\r
131 {\r
132 Pico.m.scanline=0;\r
133\r
51a902ae 134 // Scan all the memory areas:\r
cc68a136 135 SCANP(ram) SCANP(vram) SCANP(zram) SCANP(cram) SCANP(vsram)\r
136\r
137 // Pack, scan and unpack the cpu data:\r
51a902ae 138 if((PmovAction&3)==1) PicoAreaPackCpu(cpu, 0);\r
cc68a136 139 //PicoMemInit();\r
140 SCAN_VAR(cpu,"cpu")\r
51a902ae 141 if((PmovAction&3)==2) PicoAreaUnpackCpu(cpu, 0);\r
cc68a136 142\r
143 SCAN_VAR(Pico.m ,"misc")\r
144 SCAN_VAR(Pico.video,"video")\r
145\r
d2721b08 146 if (PicoOpt&7) {\r
51a902ae 147 if((PmovAction&3)==1) z80_pack(cpu_z80);\r
cc68a136 148 ret = SCAN_VAR(cpu_z80,"cpu_z80")\r
51a902ae 149 // do not unpack if we fail to load z80 state\r
150 if((PmovAction&3)==2) {\r
cc68a136 151 if(ret) z80_reset();\r
152 else z80_unpack(cpu_z80);\r
153 }\r
51a902ae 154 }\r
d2721b08 155 if (PicoOpt&3)\r
cc68a136 156 ScanVar(sn76496_regs,28*4,"SN76496state", PmovFile, PmovAction); // regs and other stuff\r
d2721b08 157 if (PicoOpt&1) {\r
158 if((PmovAction&3)==1) ym2612_pack_state();\r
cc68a136 159 ScanVar(ym2612_regs, 0x200+4, "YM2612state", PmovFile, PmovAction); // regs + addr line\r
453d2a6e 160 if((PmovAction&3)==2) ym2612_unpack_state(); // reload YM2612 state from it's regs\r
51a902ae 161 }\r
cc68a136 162 }\r
163\r
164 return 0;\r
165}\r
166\r
167// ---------------------------------------------------------------------------\r
168// Helper code to save/load to a file handle\r
169\r
170// Save or load the state from PmovFile:\r
171int PmovState(int PmovAction, void *PmovFile)\r
172{\r
173 int minimum=0;\r
174 unsigned char head[32];\r
175\r
602133e1 176 if ((PicoAHW & PAHW_MCD) || carthw_chunks != NULL)\r
51a902ae 177 {\r
178 if (PmovAction&1) return PicoCdSaveState(PmovFile);\r
fad24893 179 if (PmovAction&2) {\r
180 int ret = PicoCdLoadState(PmovFile);\r
181 if (PicoLoadStateHook) PicoLoadStateHook();\r
182 return ret;\r
183 }\r
51a902ae 184 }\r
185\r
cc68a136 186 memset(head,0,sizeof(head));\r
187\r
188 // Find out minimal compatible version:\r
189 //PicoAreaScan(PmovAction&0xc,&minimum);\r
190 minimum = 0x0021;\r
191\r
192 memcpy(head,"Pico",4);\r
193 *(unsigned int *)(head+0x8)=PicoVer;\r
194 *(unsigned int *)(head+0xc)=minimum;\r
195\r
196 // Scan header:\r
197 if (PmovAction&1) areaWrite(head,1,sizeof(head),PmovFile);\r
198 if (PmovAction&2) areaRead (head,1,sizeof(head),PmovFile);\r
199\r
200 // Scan memory areas:\r
201 PicoAreaScan(PmovAction, *(unsigned int *)(head+0x8), PmovFile);\r
202\r
fad24893 203 if ((PmovAction&2) && PicoLoadStateHook) PicoLoadStateHook();\r
204\r
cc68a136 205 return 0;\r
206}\r
207\r