area.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // This is part of Pico Library
  2. // (c) Copyright 2004 Dave, All rights reserved.
  3. // (c) Copyright 2006 notaz, All rights reserved.
  4. // Free for non-commercial use.
  5. // For commercial use, separate licencing terms must be obtained.
  6. #include "pico_int.h"
  7. #include <zlib/zlib.h>
  8. // ym2612
  9. #include "sound/ym2612.h"
  10. // sn76496
  11. extern int *sn76496_regs;
  12. struct PicoArea { void *data; int len; char *name; };
  13. // strange observation on Symbian OS 9.1, m600 organizer fw r3a06:
  14. // taking an address of fread or fwrite causes "application could't be started" error
  15. // on startup randomly depending on binary layout of executable file.
  16. arearw *areaRead = (arearw *) 0; // fread; // read and write function pointers for
  17. arearw *areaWrite = (arearw *) 0; // fwrite; // gzip save state ability
  18. areaeof *areaEof = (areaeof *) 0;
  19. areaseek *areaSeek = (areaseek *) 0;
  20. areaclose *areaClose = (areaclose *) 0;
  21. void (*PicoLoadStateHook)(void) = NULL;
  22. // Scan one variable and callback
  23. static int ScanVar(void *data,int len,char *name,void *PmovFile,int is_write)
  24. {
  25. int ret = 0;
  26. if (is_write)
  27. ret = areaWrite(data,1,len,PmovFile);
  28. else
  29. ret = areaRead (data,1,len,PmovFile);
  30. return (ret != len);
  31. }
  32. #define SCAN_VAR(x,y) ScanVar(&x,sizeof(x),y,PmovFile,is_write);
  33. #define SCANP(x) ScanVar(&Pico.x,sizeof(Pico.x),#x,PmovFile,is_write);
  34. // Pack the cpu into a common format:
  35. PICO_INTERNAL void PicoAreaPackCpu(unsigned char *cpu, int is_sub)
  36. {
  37. unsigned int pc=0;
  38. #if defined(EMU_C68K)
  39. struct Cyclone *context = is_sub ? &PicoCpuCS68k : &PicoCpuCM68k;
  40. memcpy(cpu,context->d,0x40);
  41. pc=context->pc-context->membase;
  42. *(unsigned int *)(cpu+0x44)=CycloneGetSr(context);
  43. *(unsigned int *)(cpu+0x48)=context->osp;
  44. cpu[0x4c] = context->irq;
  45. cpu[0x4d] = context->state_flags & 1;
  46. #elif defined(EMU_M68K)
  47. void *oldcontext = m68ki_cpu_p;
  48. m68k_set_context(is_sub ? &PicoCpuMS68k : &PicoCpuMM68k);
  49. memcpy(cpu,m68ki_cpu_p->dar,0x40);
  50. pc=m68ki_cpu_p->pc;
  51. *(unsigned int *)(cpu+0x44)=m68k_get_reg(NULL, M68K_REG_SR);
  52. *(unsigned int *)(cpu+0x48)=m68ki_cpu_p->sp[m68ki_cpu_p->s_flag^SFLAG_SET];
  53. cpu[0x4c] = CPU_INT_LEVEL>>8;
  54. cpu[0x4d] = CPU_STOPPED;
  55. m68k_set_context(oldcontext);
  56. #elif defined(EMU_F68K)
  57. M68K_CONTEXT *context = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k;
  58. memcpy(cpu,context->dreg,0x40);
  59. pc=context->pc;
  60. *(unsigned int *)(cpu+0x44)=context->sr;
  61. *(unsigned int *)(cpu+0x48)=context->asp;
  62. cpu[0x4c] = context->interrupts[0];
  63. cpu[0x4d] = (context->execinfo & FM68K_HALTED) ? 1 : 0;
  64. #endif
  65. *(unsigned int *)(cpu+0x40)=pc;
  66. }
  67. PICO_INTERNAL void PicoAreaUnpackCpu(unsigned char *cpu, int is_sub)
  68. {
  69. #if defined(EMU_C68K)
  70. struct Cyclone *context = is_sub ? &PicoCpuCS68k : &PicoCpuCM68k;
  71. CycloneSetSr(context, *(unsigned int *)(cpu+0x44));
  72. context->osp=*(unsigned int *)(cpu+0x48);
  73. memcpy(context->d,cpu,0x40);
  74. context->membase=0;
  75. context->pc = context->checkpc(*(unsigned int *)(cpu+0x40)); // Base pc
  76. context->irq = cpu[0x4c];
  77. context->state_flags = 0;
  78. if (cpu[0x4d])
  79. context->state_flags |= 1;
  80. #elif defined(EMU_M68K)
  81. void *oldcontext = m68ki_cpu_p;
  82. m68k_set_context(is_sub ? &PicoCpuMS68k : &PicoCpuMM68k);
  83. m68k_set_reg(M68K_REG_SR, *(unsigned int *)(cpu+0x44));
  84. memcpy(m68ki_cpu_p->dar,cpu,0x40);
  85. m68ki_cpu_p->pc=*(unsigned int *)(cpu+0x40);
  86. m68ki_cpu_p->sp[m68ki_cpu_p->s_flag^SFLAG_SET]=*(unsigned int *)(cpu+0x48);
  87. CPU_INT_LEVEL = cpu[0x4c] << 8;
  88. CPU_STOPPED = cpu[0x4d];
  89. m68k_set_context(oldcontext);
  90. #elif defined(EMU_F68K)
  91. M68K_CONTEXT *context = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k;
  92. memcpy(context->dreg,cpu,0x40);
  93. context->pc =*(unsigned int *)(cpu+0x40);
  94. context->sr =*(unsigned int *)(cpu+0x44);
  95. context->asp=*(unsigned int *)(cpu+0x48);
  96. context->interrupts[0] = cpu[0x4c];
  97. context->execinfo &= ~FM68K_HALTED;
  98. if (cpu[0x4d]&1) context->execinfo |= FM68K_HALTED;
  99. #endif
  100. }
  101. // Scan the contents of the virtual machine's memory for saving or loading
  102. static int PicoAreaScan(int is_write, unsigned int ver, void *PmovFile)
  103. {
  104. void *ym2612_regs;
  105. unsigned char cpu[0x60];
  106. unsigned char cpu_z80[0x60];
  107. int ret;
  108. memset(&cpu,0,sizeof(cpu));
  109. memset(&cpu_z80,0,sizeof(cpu_z80));
  110. Pico.m.scanline=0;
  111. ym2612_regs = YM2612GetRegs();
  112. // Scan all the memory areas:
  113. SCANP(ram) SCANP(vram) SCANP(zram) SCANP(cram) SCANP(vsram)
  114. // Pack, scan and unpack the cpu data:
  115. if (is_write)
  116. PicoAreaPackCpu(cpu, 0);
  117. SCAN_VAR(cpu,"cpu")
  118. if (!is_write)
  119. PicoAreaUnpackCpu(cpu, 0);
  120. SCAN_VAR(Pico.m ,"misc")
  121. SCAN_VAR(Pico.video,"video")
  122. // no longer keeping eeprom data in sram_reg
  123. if (!is_write && (Pico.m.sram_reg & 4))
  124. Pico.m.sram_reg = SRR_MAPPED;
  125. if (is_write)
  126. z80_pack(cpu_z80);
  127. ret = SCAN_VAR(cpu_z80,"cpu_z80")
  128. // do not unpack if we fail to load z80 state
  129. if (!is_write) {
  130. if (ret) z80_reset();
  131. else z80_unpack(cpu_z80);
  132. }
  133. ScanVar(sn76496_regs, 28*4, "SN76496state", PmovFile, is_write);
  134. if (is_write)
  135. ym2612_pack_state();
  136. ret = ScanVar(ym2612_regs, 0x200+4, "YM2612state", PmovFile, is_write); // regs + addr line
  137. if (!is_write && !ret)
  138. ym2612_unpack_state();
  139. return 0;
  140. }
  141. // ---------------------------------------------------------------------------
  142. // Helper code to save/load to a file handle
  143. // XXX: error checking
  144. // Save or load the state from PmovFile:
  145. static int PmovState(int is_write, void *PmovFile)
  146. {
  147. int minimum=0;
  148. unsigned char head[32];
  149. if ((PicoAHW & PAHW_MCD) || carthw_chunks != NULL)
  150. {
  151. if (is_write)
  152. return PicoCdSaveState(PmovFile);
  153. else {
  154. int ret = PicoCdLoadState(PmovFile);
  155. if (PicoLoadStateHook) PicoLoadStateHook();
  156. return ret;
  157. }
  158. }
  159. memset(head,0,sizeof(head));
  160. // Find out minimal compatible version:
  161. minimum = 0x0021;
  162. memcpy(head,"Pico",4);
  163. *(unsigned int *)(head+0x8)=PicoVer;
  164. *(unsigned int *)(head+0xc)=minimum;
  165. // Scan header:
  166. if (is_write)
  167. areaWrite(head,1,sizeof(head),PmovFile);
  168. else
  169. areaRead (head,1,sizeof(head),PmovFile);
  170. // Scan memory areas:
  171. PicoAreaScan(is_write, *(unsigned int *)(head+0x8), PmovFile);
  172. if (!is_write && PicoLoadStateHook)
  173. PicoLoadStateHook();
  174. return 0;
  175. }
  176. static size_t gzRead2(void *p, size_t _size, size_t _n, void *file)
  177. {
  178. return gzread(file, p, _n);
  179. }
  180. static size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)
  181. {
  182. return gzwrite(file, p, _n);
  183. }
  184. static void set_cbs(int gz)
  185. {
  186. if (gz) {
  187. areaRead = gzRead2;
  188. areaWrite = gzWrite2;
  189. areaEof = (areaeof *) gzeof;
  190. areaSeek = (areaseek *) gzseek;
  191. areaClose = (areaclose *) gzclose;
  192. } else {
  193. areaRead = (arearw *) fread;
  194. areaWrite = (arearw *) fwrite;
  195. areaEof = (areaeof *) feof;
  196. areaSeek = (areaseek *) fseek;
  197. areaClose = (areaclose *) fclose;
  198. }
  199. }
  200. int PicoState(const char *fname, int is_save)
  201. {
  202. void *afile = NULL;
  203. int ret;
  204. if (strcmp(fname + strlen(fname) - 3, ".gz") == 0)
  205. {
  206. if ( (afile = gzopen(fname, is_save ? "wb" : "rb")) ) {
  207. set_cbs(1);
  208. if (is_save)
  209. gzsetparams(afile, 9, Z_DEFAULT_STRATEGY);
  210. }
  211. }
  212. else
  213. {
  214. if ( (afile = fopen(fname, is_save ? "wb" : "rb")) ) {
  215. set_cbs(0);
  216. }
  217. }
  218. if (afile == NULL)
  219. return -1;
  220. ret = PmovState(is_save, afile);
  221. areaClose(afile);
  222. if (!is_save)
  223. Pico.m.dirtyPal=1;
  224. return ret;
  225. }
  226. int PicoStateLoadVDP(const char *fname)
  227. {
  228. void *afile = NULL;
  229. if (strcmp(fname + strlen(fname) - 3, ".gz") == 0)
  230. {
  231. if ( (afile = gzopen(fname, "rb")) )
  232. set_cbs(1);
  233. }
  234. else
  235. {
  236. if ( (afile = fopen(fname, "rb")) )
  237. set_cbs(0);
  238. }
  239. if (afile == NULL)
  240. return -1;
  241. if ((PicoAHW & PAHW_MCD) || carthw_chunks != NULL) {
  242. PicoCdLoadStateGfx(afile);
  243. } else {
  244. areaSeek(afile, 0x10020, SEEK_SET); // skip header and RAM in state file
  245. areaRead(Pico.vram, 1, sizeof(Pico.vram), afile);
  246. areaSeek(afile, 0x2000, SEEK_CUR);
  247. areaRead(Pico.cram, 1, sizeof(Pico.cram), afile);
  248. areaRead(Pico.vsram, 1, sizeof(Pico.vsram), afile);
  249. areaSeek(afile, 0x221a0, SEEK_SET);
  250. areaRead(&Pico.video, 1, sizeof(Pico.video), afile);
  251. }
  252. areaClose(afile);
  253. return 0;
  254. }