Area.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "PicoInt.h"
  7. // ym2612
  8. #include "sound/ym2612.h"
  9. // sn76496
  10. extern int *sn76496_regs;
  11. struct PicoArea { void *data; int len; char *name; };
  12. // strange observation on Symbian OS 9.1, m600 organizer fw r3a06:
  13. // taking an address of fread or fwrite causes "application could't be started" error
  14. // on startup randomly depending on binary layout of executable file.
  15. arearw *areaRead = (arearw *) 0; // fread; // read and write function pointers for
  16. arearw *areaWrite = (arearw *) 0; // fwrite; // gzip save state ability
  17. areaeof *areaEof = (areaeof *) 0;
  18. areaseek *areaSeek = (areaseek *) 0;
  19. areaclose *areaClose = (areaclose *) 0;
  20. // Scan one variable and callback
  21. static int ScanVar(void *data,int len,char *name,void *PmovFile,int PmovAction)
  22. {
  23. int ret = 0;
  24. if ((PmovAction&3)==1) ret = areaWrite(data,1,len,PmovFile);
  25. if ((PmovAction&3)==2) ret = areaRead (data,1,len,PmovFile);
  26. return (ret != len);
  27. }
  28. #define SCAN_VAR(x,y) ScanVar(&x,sizeof(x),y,PmovFile,PmovAction);
  29. #define SCANP(x) ScanVar(&Pico.x,sizeof(Pico.x),#x,PmovFile,PmovAction);
  30. // Pack the cpu into a common format:
  31. PICO_INTERNAL int PicoAreaPackCpu(unsigned char *cpu, int is_sub)
  32. {
  33. unsigned int pc=0;
  34. #ifdef EMU_A68K
  35. memcpy(cpu,M68000_regs.d,0x40);
  36. pc=M68000_regs.pc;
  37. *(unsigned char *)(cpu+0x44)=(unsigned char)M68000_regs.ccr;
  38. *(unsigned char *)(cpu+0x45)=(unsigned char)M68000_regs.srh;
  39. *(unsigned int *)(cpu+0x48)=M68000_regs.isp;
  40. cpu[0x4c] = M68000_regs.irq;
  41. // stop flag?
  42. #endif
  43. #ifdef EMU_C68K
  44. struct Cyclone *context = is_sub ? &PicoCpuS68k : &PicoCpu;
  45. memcpy(cpu,context->d,0x40);
  46. pc=context->pc-context->membase;
  47. *(unsigned int *)(cpu+0x44)=CycloneGetSr(context);
  48. *(unsigned int *)(cpu+0x48)=context->osp;
  49. cpu[0x4c] = context->irq;
  50. cpu[0x4d] = context->state_flags & 1;
  51. #endif
  52. #ifdef EMU_M68K
  53. void *oldcontext = m68ki_cpu_p;
  54. m68k_set_context(is_sub ? &PicoS68kCPU : &PicoM68kCPU);
  55. memcpy(cpu,m68ki_cpu_p->dar,0x40);
  56. pc=m68ki_cpu_p->pc;
  57. *(unsigned int *)(cpu+0x44)=m68k_get_reg(NULL, M68K_REG_SR);
  58. *(unsigned int *)(cpu+0x48)=m68ki_cpu_p->sp[0];
  59. cpu[0x4c] = CPU_INT_LEVEL>>8;
  60. cpu[0x4d] = CPU_STOPPED;
  61. m68k_set_context(oldcontext);
  62. #endif
  63. *(unsigned int *)(cpu+0x40)=pc;
  64. return 0;
  65. }
  66. PICO_INTERNAL int PicoAreaUnpackCpu(unsigned char *cpu, int is_sub)
  67. {
  68. #ifdef EMU_A68K
  69. memcpy(M68000_regs.d,cpu,0x40);
  70. M68000_regs.pc=pc;
  71. M68000_regs.ccr=*(unsigned char *)(cpu+0x44);
  72. M68000_regs.srh=*(unsigned char *)(cpu+0x45);
  73. M68000_regs.isp=*(unsigned int *)(cpu+0x48);
  74. M68000_regs.irq = cpu[0x4c];
  75. // stop flag?
  76. #endif
  77. #ifdef EMU_C68K
  78. struct Cyclone *context = is_sub ? &PicoCpuS68k : &PicoCpu;
  79. CycloneSetSr(context, *(unsigned int *)(cpu+0x44));
  80. context->osp=*(unsigned int *)(cpu+0x48);
  81. memcpy(context->d,cpu,0x40);
  82. context->membase=0;
  83. context->pc = context->checkpc(*(unsigned int *)(cpu+0x40)); // Base pc
  84. context->irq = cpu[0x4c];
  85. context->state_flags = 0;
  86. if (cpu[0x4d])
  87. context->state_flags |= 1;
  88. #endif
  89. #ifdef EMU_M68K
  90. void *oldcontext = m68ki_cpu_p;
  91. m68k_set_context(is_sub ? &PicoS68kCPU : &PicoM68kCPU);
  92. memcpy(m68ki_cpu_p->dar,cpu,0x40);
  93. m68ki_cpu_p->pc=*(unsigned int *)(cpu+0x40);
  94. m68k_set_reg(M68K_REG_SR, *(unsigned int *)(cpu+0x44));
  95. m68ki_cpu_p->sp[0]=*(unsigned int *)(cpu+0x48);
  96. CPU_INT_LEVEL = cpu[0x4c] << 8;
  97. CPU_STOPPED = cpu[0x4d];
  98. m68k_set_context(oldcontext);
  99. #endif
  100. return 0;
  101. }
  102. // Scan the contents of the virtual machine's memory for saving or loading
  103. static int PicoAreaScan(int PmovAction,unsigned int ver, void *PmovFile)
  104. {
  105. void *ym2612_regs;
  106. unsigned char cpu[0x60];
  107. unsigned char cpu_z80[0x60];
  108. int ret;
  109. memset(&cpu,0,sizeof(cpu));
  110. memset(&cpu_z80,0,sizeof(cpu_z80));
  111. ym2612_regs = YM2612GetRegs();
  112. if (PmovAction&4)
  113. {
  114. Pico.m.scanline=0;
  115. // Scan all the memory areas:
  116. SCANP(ram) SCANP(vram) SCANP(zram) SCANP(cram) SCANP(vsram)
  117. // Pack, scan and unpack the cpu data:
  118. if((PmovAction&3)==1) PicoAreaPackCpu(cpu, 0);
  119. //SekInit(); // notaz: do we really have to do this here?
  120. //PicoMemInit();
  121. SCAN_VAR(cpu,"cpu")
  122. if((PmovAction&3)==2) PicoAreaUnpackCpu(cpu, 0);
  123. SCAN_VAR(Pico.m ,"misc")
  124. SCAN_VAR(Pico.video,"video")
  125. // notaz: save/load z80, YM2612, sn76496 states instead of Pico.s (which is unused anyway)
  126. if(PicoOpt&7) {
  127. if((PmovAction&3)==1) z80_pack(cpu_z80);
  128. ret = SCAN_VAR(cpu_z80,"cpu_z80")
  129. // do not unpack if we fail to load z80 state
  130. if((PmovAction&3)==2) {
  131. if(ret) z80_reset();
  132. else z80_unpack(cpu_z80);
  133. }
  134. }
  135. if(PicoOpt&3)
  136. ScanVar(sn76496_regs,28*4,"SN76496state", PmovFile, PmovAction); // regs and other stuff
  137. if(PicoOpt&1) {
  138. ScanVar(ym2612_regs, 0x200+4, "YM2612state", PmovFile, PmovAction); // regs + addr line
  139. if((PmovAction&3)==2) YM2612PicoStateLoad(); // reload YM2612 state from it's regs
  140. }
  141. }
  142. return 0;
  143. }
  144. // ---------------------------------------------------------------------------
  145. // Helper code to save/load to a file handle
  146. // Save or load the state from PmovFile:
  147. int PmovState(int PmovAction, void *PmovFile)
  148. {
  149. int minimum=0;
  150. unsigned char head[32];
  151. if (PicoMCD & 1)
  152. {
  153. if (PmovAction&1) return PicoCdSaveState(PmovFile);
  154. if (PmovAction&2) return PicoCdLoadState(PmovFile);
  155. }
  156. memset(head,0,sizeof(head));
  157. // Find out minimal compatible version:
  158. //PicoAreaScan(PmovAction&0xc,&minimum);
  159. minimum = 0x0021;
  160. memcpy(head,"Pico",4);
  161. *(unsigned int *)(head+0x8)=PicoVer;
  162. *(unsigned int *)(head+0xc)=minimum;
  163. // Scan header:
  164. if (PmovAction&1) areaWrite(head,1,sizeof(head),PmovFile);
  165. if (PmovAction&2) areaRead (head,1,sizeof(head),PmovFile);
  166. // Scan memory areas:
  167. PicoAreaScan(PmovAction, *(unsigned int *)(head+0x8), PmovFile);
  168. return 0;
  169. }