Area.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. void (*PicoLoadStateHook)(void) = NULL;
  21. // Scan one variable and callback
  22. static int ScanVar(void *data,int len,char *name,void *PmovFile,int PmovAction)
  23. {
  24. int ret = 0;
  25. if ((PmovAction&3)==1) ret = areaWrite(data,1,len,PmovFile);
  26. if ((PmovAction&3)==2) ret = areaRead (data,1,len,PmovFile);
  27. return (ret != len);
  28. }
  29. #define SCAN_VAR(x,y) ScanVar(&x,sizeof(x),y,PmovFile,PmovAction);
  30. #define SCANP(x) ScanVar(&Pico.x,sizeof(Pico.x),#x,PmovFile,PmovAction);
  31. // Pack the cpu into a common format:
  32. PICO_INTERNAL void PicoAreaPackCpu(unsigned char *cpu, int is_sub)
  33. {
  34. unsigned int pc=0;
  35. #if defined(EMU_C68K)
  36. struct Cyclone *context = is_sub ? &PicoCpuCS68k : &PicoCpuCM68k;
  37. memcpy(cpu,context->d,0x40);
  38. pc=context->pc-context->membase;
  39. *(unsigned int *)(cpu+0x44)=CycloneGetSr(context);
  40. *(unsigned int *)(cpu+0x48)=context->osp;
  41. cpu[0x4c] = context->irq;
  42. cpu[0x4d] = context->state_flags & 1;
  43. #elif defined(EMU_M68K)
  44. void *oldcontext = m68ki_cpu_p;
  45. m68k_set_context(is_sub ? &PicoCpuMS68k : &PicoCpuMM68k);
  46. memcpy(cpu,m68ki_cpu_p->dar,0x40);
  47. pc=m68ki_cpu_p->pc;
  48. *(unsigned int *)(cpu+0x44)=m68k_get_reg(NULL, M68K_REG_SR);
  49. *(unsigned int *)(cpu+0x48)=m68ki_cpu_p->sp[m68ki_cpu_p->s_flag^SFLAG_SET];
  50. cpu[0x4c] = CPU_INT_LEVEL>>8;
  51. cpu[0x4d] = CPU_STOPPED;
  52. m68k_set_context(oldcontext);
  53. #elif defined(EMU_F68K)
  54. M68K_CONTEXT *context = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k;
  55. memcpy(cpu,context->dreg,0x40);
  56. pc=context->pc;
  57. *(unsigned int *)(cpu+0x44)=context->sr;
  58. *(unsigned int *)(cpu+0x48)=context->asp;
  59. cpu[0x4c] = context->interrupts[0];
  60. cpu[0x4d] = (context->execinfo & FM68K_HALTED) ? 1 : 0;
  61. #endif
  62. *(unsigned int *)(cpu+0x40)=pc;
  63. }
  64. PICO_INTERNAL void PicoAreaUnpackCpu(unsigned char *cpu, int is_sub)
  65. {
  66. #if defined(EMU_C68K)
  67. struct Cyclone *context = is_sub ? &PicoCpuCS68k : &PicoCpuCM68k;
  68. CycloneSetSr(context, *(unsigned int *)(cpu+0x44));
  69. context->osp=*(unsigned int *)(cpu+0x48);
  70. memcpy(context->d,cpu,0x40);
  71. context->membase=0;
  72. context->pc = context->checkpc(*(unsigned int *)(cpu+0x40)); // Base pc
  73. context->irq = cpu[0x4c];
  74. context->state_flags = 0;
  75. if (cpu[0x4d])
  76. context->state_flags |= 1;
  77. #elif defined(EMU_M68K)
  78. void *oldcontext = m68ki_cpu_p;
  79. m68k_set_context(is_sub ? &PicoCpuMS68k : &PicoCpuMM68k);
  80. m68k_set_reg(M68K_REG_SR, *(unsigned int *)(cpu+0x44));
  81. memcpy(m68ki_cpu_p->dar,cpu,0x40);
  82. m68ki_cpu_p->pc=*(unsigned int *)(cpu+0x40);
  83. m68ki_cpu_p->sp[m68ki_cpu_p->s_flag^SFLAG_SET]=*(unsigned int *)(cpu+0x48);
  84. CPU_INT_LEVEL = cpu[0x4c] << 8;
  85. CPU_STOPPED = cpu[0x4d];
  86. m68k_set_context(oldcontext);
  87. #elif defined(EMU_F68K)
  88. M68K_CONTEXT *context = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k;
  89. memcpy(context->dreg,cpu,0x40);
  90. context->pc =*(unsigned int *)(cpu+0x40);
  91. context->sr =*(unsigned int *)(cpu+0x44);
  92. context->asp=*(unsigned int *)(cpu+0x48);
  93. context->interrupts[0] = cpu[0x4c];
  94. context->execinfo &= ~FM68K_HALTED;
  95. if (cpu[0x4d]&1) context->execinfo |= FM68K_HALTED;
  96. #endif
  97. }
  98. // Scan the contents of the virtual machine's memory for saving or loading
  99. static int PicoAreaScan(int PmovAction,unsigned int ver, void *PmovFile)
  100. {
  101. void *ym2612_regs;
  102. unsigned char cpu[0x60];
  103. unsigned char cpu_z80[0x60];
  104. int ret;
  105. memset(&cpu,0,sizeof(cpu));
  106. memset(&cpu_z80,0,sizeof(cpu_z80));
  107. ym2612_regs = YM2612GetRegs();
  108. if (PmovAction&4)
  109. {
  110. Pico.m.scanline=0;
  111. // Scan all the memory areas:
  112. SCANP(ram) SCANP(vram) SCANP(zram) SCANP(cram) SCANP(vsram)
  113. // Pack, scan and unpack the cpu data:
  114. if((PmovAction&3)==1) PicoAreaPackCpu(cpu, 0);
  115. //PicoMemInit();
  116. SCAN_VAR(cpu,"cpu")
  117. if((PmovAction&3)==2) PicoAreaUnpackCpu(cpu, 0);
  118. SCAN_VAR(Pico.m ,"misc")
  119. SCAN_VAR(Pico.video,"video")
  120. if (PicoOpt&7) {
  121. if((PmovAction&3)==1) z80_pack(cpu_z80);
  122. ret = SCAN_VAR(cpu_z80,"cpu_z80")
  123. // do not unpack if we fail to load z80 state
  124. if((PmovAction&3)==2) {
  125. if(ret) z80_reset();
  126. else z80_unpack(cpu_z80);
  127. }
  128. }
  129. if (PicoOpt&3)
  130. ScanVar(sn76496_regs,28*4,"SN76496state", PmovFile, PmovAction); // regs and other stuff
  131. if (PicoOpt&1) {
  132. if((PmovAction&3)==1) ym2612_pack_state();
  133. ScanVar(ym2612_regs, 0x200+4, "YM2612state", PmovFile, PmovAction); // regs + addr line
  134. if((PmovAction&3)==2) ym2612_unpack_state(); // reload YM2612 state from it's regs
  135. }
  136. }
  137. return 0;
  138. }
  139. // ---------------------------------------------------------------------------
  140. // Helper code to save/load to a file handle
  141. // Save or load the state from PmovFile:
  142. int PmovState(int PmovAction, void *PmovFile)
  143. {
  144. int minimum=0;
  145. unsigned char head[32];
  146. if ((PicoAHW & PAHW_MCD) || carthw_chunks != NULL)
  147. {
  148. if (PmovAction&1) return PicoCdSaveState(PmovFile);
  149. if (PmovAction&2) {
  150. int ret = PicoCdLoadState(PmovFile);
  151. if (PicoLoadStateHook) PicoLoadStateHook();
  152. return ret;
  153. }
  154. }
  155. memset(head,0,sizeof(head));
  156. // Find out minimal compatible version:
  157. //PicoAreaScan(PmovAction&0xc,&minimum);
  158. minimum = 0x0021;
  159. memcpy(head,"Pico",4);
  160. *(unsigned int *)(head+0x8)=PicoVer;
  161. *(unsigned int *)(head+0xc)=minimum;
  162. // Scan header:
  163. if (PmovAction&1) areaWrite(head,1,sizeof(head),PmovFile);
  164. if (PmovAction&2) areaRead (head,1,sizeof(head),PmovFile);
  165. // Scan memory areas:
  166. PicoAreaScan(PmovAction, *(unsigned int *)(head+0x8), PmovFile);
  167. if ((PmovAction&2) && PicoLoadStateHook) PicoLoadStateHook();
  168. return 0;
  169. }