Area.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. 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. #endif
  41. #ifdef EMU_C68K
  42. struct Cyclone *context = is_sub ? &PicoCpuS68k : &PicoCpu;
  43. memcpy(cpu,context->d,0x40);
  44. pc=context->pc-context->membase;
  45. *(unsigned int *)(cpu+0x44)=CycloneGetSr(context);
  46. *(unsigned int *)(cpu+0x48)=context->osp;
  47. #endif
  48. #ifdef EMU_M68K
  49. void *oldcontext = m68ki_cpu_p;
  50. m68k_set_context(is_sub ? &PicoS68kCPU : &PicoM68kCPU);
  51. memcpy(cpu,m68ki_cpu_p->dar,0x40);
  52. pc=m68ki_cpu_p->pc;
  53. *(unsigned int *)(cpu+0x44)=m68k_get_reg(NULL, M68K_REG_SR);
  54. *(unsigned int *)(cpu+0x48)=m68ki_cpu_p->sp[0];
  55. m68k_set_context(oldcontext);
  56. #endif
  57. *(unsigned int *)(cpu+0x40)=pc;
  58. return 0;
  59. }
  60. int PicoAreaUnpackCpu(unsigned char *cpu, int is_sub)
  61. {
  62. #ifdef EMU_A68K
  63. memcpy(M68000_regs.d,cpu,0x40);
  64. M68000_regs.pc=pc;
  65. M68000_regs.ccr=*(unsigned char *)(cpu+0x44);
  66. M68000_regs.srh=*(unsigned char *)(cpu+0x45);
  67. M68000_regs.isp=*(unsigned int *)(cpu+0x48);
  68. #endif
  69. #ifdef EMU_C68K
  70. struct Cyclone *context = is_sub ? &PicoCpuS68k : &PicoCpu;
  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. #endif
  77. #ifdef EMU_M68K
  78. void *oldcontext = m68ki_cpu_p;
  79. m68k_set_context(is_sub ? &PicoS68kCPU : &PicoM68kCPU);
  80. memcpy(m68ki_cpu_p->dar,cpu,0x40);
  81. m68ki_cpu_p->pc=*(unsigned int *)(cpu+0x40);
  82. m68k_set_reg(M68K_REG_SR, *(unsigned int *)(cpu+0x44));
  83. m68ki_cpu_p->sp[0]=*(unsigned int *)(cpu+0x48);
  84. m68k_set_context(oldcontext);
  85. #endif
  86. return 0;
  87. }
  88. // Scan the contents of the virtual machine's memory for saving or loading
  89. static int PicoAreaScan(int PmovAction,unsigned int ver, void *PmovFile)
  90. {
  91. void *ym2612_regs;
  92. unsigned char cpu[0x60];
  93. unsigned char cpu_z80[0x60];
  94. int ret;
  95. memset(&cpu,0,sizeof(cpu));
  96. memset(&cpu_z80,0,sizeof(cpu_z80));
  97. ym2612_regs = YM2612GetRegs();
  98. if (PmovAction&4)
  99. {
  100. Pico.m.scanline=0;
  101. // Scan all the memory areas:
  102. SCANP(ram) SCANP(vram) SCANP(zram) SCANP(cram) SCANP(vsram)
  103. // Pack, scan and unpack the cpu data:
  104. if((PmovAction&3)==1) PicoAreaPackCpu(cpu, 0);
  105. //SekInit(); // notaz: do we really have to do this here?
  106. //PicoMemInit();
  107. SCAN_VAR(cpu,"cpu")
  108. if((PmovAction&3)==2) PicoAreaUnpackCpu(cpu, 0);
  109. SCAN_VAR(Pico.m ,"misc")
  110. SCAN_VAR(Pico.video,"video")
  111. // notaz: save/load z80, YM2612, sn76496 states instead of Pico.s (which is unused anyway)
  112. if(PicoOpt&7) {
  113. if((PmovAction&3)==1) z80_pack(cpu_z80);
  114. ret = SCAN_VAR(cpu_z80,"cpu_z80")
  115. // do not unpack if we fail to load z80 state
  116. if((PmovAction&3)==2) {
  117. if(ret) z80_reset();
  118. else z80_unpack(cpu_z80);
  119. }
  120. }
  121. if(PicoOpt&3)
  122. ScanVar(sn76496_regs,28*4,"SN76496state", PmovFile, PmovAction); // regs and other stuff
  123. if(PicoOpt&1) {
  124. ScanVar(ym2612_regs, 0x200+4, "YM2612state", PmovFile, PmovAction); // regs + addr line
  125. if((PmovAction&3)==2) YM2612PicoStateLoad(); // reload YM2612 state from it's regs
  126. }
  127. }
  128. return 0;
  129. }
  130. // ---------------------------------------------------------------------------
  131. // Helper code to save/load to a file handle
  132. // Save or load the state from PmovFile:
  133. int PmovState(int PmovAction, void *PmovFile)
  134. {
  135. int minimum=0;
  136. unsigned char head[32];
  137. if (PicoMCD & 1)
  138. {
  139. if (PmovAction&1) return PicoCdSaveState(PmovFile);
  140. if (PmovAction&2) return PicoCdLoadState(PmovFile);
  141. }
  142. memset(head,0,sizeof(head));
  143. // Find out minimal compatible version:
  144. //PicoAreaScan(PmovAction&0xc,&minimum);
  145. minimum = 0x0021;
  146. memcpy(head,"Pico",4);
  147. *(unsigned int *)(head+0x8)=PicoVer;
  148. *(unsigned int *)(head+0xc)=minimum;
  149. // Scan header:
  150. if (PmovAction&1) areaWrite(head,1,sizeof(head),PmovFile);
  151. if (PmovAction&2) areaRead (head,1,sizeof(head),PmovFile);
  152. // Scan memory areas:
  153. PicoAreaScan(PmovAction, *(unsigned int *)(head+0x8), PmovFile);
  154. return 0;
  155. }