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 |
16 | extern int *sn76496_regs;\r |
17 | \r |
18 | \r |
19 | struct 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 |
75736070 |
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 |
cc68a136 |
29 | \r |
30 | \r |
31 | // Scan one variable and callback\r |
32 | static int ScanVar(void *data,int len,char *name,void *PmovFile,int PmovAction)\r |
33 | {\r |
34 | int ret = 0;\r |
35 | if ((PmovAction&3)==1) ret = areaWrite(data,1,len,PmovFile);\r |
36 | if ((PmovAction&3)==2) ret = areaRead (data,1,len,PmovFile);\r |
37 | return (ret != len);\r |
38 | }\r |
39 | \r |
40 | #define SCAN_VAR(x,y) ScanVar(&x,sizeof(x),y,PmovFile,PmovAction);\r |
41 | #define SCANP(x) ScanVar(&Pico.x,sizeof(Pico.x),#x,PmovFile,PmovAction);\r |
42 | \r |
43 | // Pack the cpu into a common format:\r |
51a902ae |
44 | int PicoAreaPackCpu(unsigned char *cpu, int is_sub)\r |
cc68a136 |
45 | {\r |
46 | unsigned int pc=0;\r |
47 | \r |
48 | #ifdef EMU_A68K\r |
49 | memcpy(cpu,M68000_regs.d,0x40);\r |
50 | pc=M68000_regs.pc;\r |
51 | *(unsigned char *)(cpu+0x44)=(unsigned char)M68000_regs.ccr;\r |
52 | *(unsigned char *)(cpu+0x45)=(unsigned char)M68000_regs.srh;\r |
53 | *(unsigned int *)(cpu+0x48)=M68000_regs.isp;\r |
54 | #endif\r |
55 | \r |
56 | #ifdef EMU_C68K\r |
51a902ae |
57 | struct Cyclone *context = is_sub ? &PicoCpuS68k : &PicoCpu;\r |
58 | memcpy(cpu,context->d,0x40);\r |
59 | pc=context->pc-context->membase;\r |
60 | *(unsigned int *)(cpu+0x44)=CycloneGetSr(context);\r |
61 | *(unsigned int *)(cpu+0x48)=context->osp;\r |
cc68a136 |
62 | #endif\r |
63 | \r |
64 | #ifdef EMU_M68K\r |
51a902ae |
65 | m68ki_cpu_core *context = is_sub ? &PicoS68kCPU : &PicoM68kCPU;\r |
66 | memcpy(cpu,context->dar,0x40);\r |
67 | pc=context->pc;\r |
cc68a136 |
68 | *(unsigned int *)(cpu+0x44)=m68k_get_reg(NULL, M68K_REG_SR);\r |
51a902ae |
69 | *(unsigned int *)(cpu+0x48)=context->sp[0];\r |
cc68a136 |
70 | #endif\r |
71 | \r |
72 | *(unsigned int *)(cpu+0x40)=pc;\r |
73 | return 0;\r |
74 | }\r |
75 | \r |
51a902ae |
76 | int PicoAreaUnpackCpu(unsigned char *cpu, int is_sub)\r |
cc68a136 |
77 | {\r |
cc68a136 |
78 | #ifdef EMU_A68K\r |
79 | memcpy(M68000_regs.d,cpu,0x40);\r |
80 | M68000_regs.pc=pc;\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 |
84 | #endif\r |
85 | \r |
86 | #ifdef EMU_C68K\r |
51a902ae |
87 | struct Cyclone *context = is_sub ? &PicoCpuS68k : &PicoCpu;\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 = context->checkpc(*(unsigned int *)(cpu+0x40)); // Base pc\r |
cc68a136 |
93 | #endif\r |
94 | \r |
95 | #ifdef EMU_M68K\r |
51a902ae |
96 | m68ki_cpu_core *context = is_sub ? &PicoS68kCPU : &PicoM68kCPU;\r |
97 | memcpy(context->dar,cpu,0x40);\r |
98 | context->pc=*(unsigned int *)(cpu+0x40);\r |
cc68a136 |
99 | m68k_set_reg(M68K_REG_SR, *(unsigned int *)(cpu+0x44));\r |
51a902ae |
100 | context->sp[0]=*(unsigned int *)(cpu+0x48);\r |
cc68a136 |
101 | #endif\r |
102 | return 0;\r |
103 | }\r |
104 | \r |
105 | // Scan the contents of the virtual machine's memory for saving or loading\r |
106 | static int PicoAreaScan(int PmovAction,unsigned int ver, void *PmovFile)\r |
107 | {\r |
108 | void *ym2612_regs;\r |
109 | unsigned char cpu[0x60];\r |
110 | unsigned char cpu_z80[0x60];\r |
111 | int ret;\r |
112 | \r |
113 | memset(&cpu,0,sizeof(cpu));\r |
114 | memset(&cpu_z80,0,sizeof(cpu_z80));\r |
115 | \r |
116 | ym2612_regs = YM2612GetRegs();\r |
117 | \r |
118 | if (PmovAction&4)\r |
119 | {\r |
120 | Pico.m.scanline=0;\r |
121 | \r |
51a902ae |
122 | // Scan all the memory areas:\r |
cc68a136 |
123 | SCANP(ram) SCANP(vram) SCANP(zram) SCANP(cram) SCANP(vsram)\r |
124 | \r |
125 | // Pack, scan and unpack the cpu data:\r |
51a902ae |
126 | if((PmovAction&3)==1) PicoAreaPackCpu(cpu, 0);\r |
cc68a136 |
127 | //SekInit(); // notaz: do we really have to do this here?\r |
128 | //PicoMemInit();\r |
129 | SCAN_VAR(cpu,"cpu")\r |
51a902ae |
130 | if((PmovAction&3)==2) PicoAreaUnpackCpu(cpu, 0);\r |
cc68a136 |
131 | \r |
132 | SCAN_VAR(Pico.m ,"misc")\r |
133 | SCAN_VAR(Pico.video,"video")\r |
134 | \r |
51a902ae |
135 | // notaz: save/load z80, YM2612, sn76496 states instead of Pico.s (which is unused anyway)\r |
136 | if(PicoOpt&7) {\r |
137 | if((PmovAction&3)==1) z80_pack(cpu_z80);\r |
cc68a136 |
138 | ret = SCAN_VAR(cpu_z80,"cpu_z80")\r |
51a902ae |
139 | // do not unpack if we fail to load z80 state\r |
140 | if((PmovAction&3)==2) {\r |
cc68a136 |
141 | if(ret) z80_reset();\r |
142 | else z80_unpack(cpu_z80);\r |
143 | }\r |
51a902ae |
144 | }\r |
145 | if(PicoOpt&3)\r |
cc68a136 |
146 | ScanVar(sn76496_regs,28*4,"SN76496state", PmovFile, PmovAction); // regs and other stuff\r |
51a902ae |
147 | if(PicoOpt&1) {\r |
cc68a136 |
148 | ScanVar(ym2612_regs, 0x200+4, "YM2612state", PmovFile, PmovAction); // regs + addr line\r |
51a902ae |
149 | if((PmovAction&3)==2) YM2612PicoStateLoad(); // reload YM2612 state from it's regs\r |
150 | }\r |
cc68a136 |
151 | }\r |
152 | \r |
153 | return 0;\r |
154 | }\r |
155 | \r |
156 | // ---------------------------------------------------------------------------\r |
157 | // Helper code to save/load to a file handle\r |
158 | \r |
159 | // Save or load the state from PmovFile:\r |
160 | int PmovState(int PmovAction, void *PmovFile)\r |
161 | {\r |
162 | int minimum=0;\r |
163 | unsigned char head[32];\r |
164 | \r |
75736070 |
165 | if (PicoMCD & 1)\r |
51a902ae |
166 | {\r |
167 | if (PmovAction&1) return PicoCdSaveState(PmovFile);\r |
168 | if (PmovAction&2) return PicoCdLoadState(PmovFile);\r |
169 | }\r |
170 | \r |
cc68a136 |
171 | memset(head,0,sizeof(head));\r |
172 | \r |
173 | // Find out minimal compatible version:\r |
174 | //PicoAreaScan(PmovAction&0xc,&minimum);\r |
175 | minimum = 0x0021;\r |
176 | \r |
177 | memcpy(head,"Pico",4);\r |
178 | *(unsigned int *)(head+0x8)=PicoVer;\r |
179 | *(unsigned int *)(head+0xc)=minimum;\r |
180 | \r |
181 | // Scan header:\r |
182 | if (PmovAction&1) areaWrite(head,1,sizeof(head),PmovFile);\r |
183 | if (PmovAction&2) areaRead (head,1,sizeof(head),PmovFile);\r |
184 | \r |
185 | // Scan memory areas:\r |
186 | PicoAreaScan(PmovAction, *(unsigned int *)(head+0x8), PmovFile);\r |
187 | \r |
188 | return 0;\r |
189 | }\r |
190 | \r |