lowercasing filenames, part3
[picodrive.git] / pico / area.c
diff --git a/pico/area.c b/pico/area.c
new file mode 100644 (file)
index 0000000..876320d
--- /dev/null
@@ -0,0 +1,207 @@
+// This is part of Pico Library\r
+\r
+// (c) Copyright 2004 Dave, All rights reserved.\r
+// (c) Copyright 2006 notaz, All rights reserved.\r
+// Free for non-commercial use.\r
+\r
+// For commercial use, separate licencing terms must be obtained.\r
+\r
+\r
+#include "pico_int.h"\r
+\r
+// ym2612\r
+#include "sound/ym2612.h"\r
+\r
+// sn76496\r
+extern int *sn76496_regs;\r
+\r
+\r
+struct PicoArea { void *data; int len; char *name; };\r
+\r
+// strange observation on Symbian OS 9.1, m600 organizer fw r3a06:\r
+// taking an address of fread or fwrite causes "application could't be started" error\r
+// on startup randomly depending on binary layout of executable file.\r
+\r
+arearw    *areaRead  = (arearw *) 0; // fread;  // read and write function pointers for\r
+arearw    *areaWrite = (arearw *) 0; // fwrite; // gzip save state ability\r
+areaeof   *areaEof   = (areaeof *) 0;\r
+areaseek  *areaSeek  = (areaseek *) 0;\r
+areaclose *areaClose = (areaclose *) 0;\r
+\r
+void (*PicoLoadStateHook)(void) = NULL;\r
+\r
+\r
+// Scan one variable and callback\r
+static int ScanVar(void *data,int len,char *name,void *PmovFile,int PmovAction)\r
+{\r
+  int ret = 0;\r
+  if ((PmovAction&3)==1) ret = areaWrite(data,1,len,PmovFile);\r
+  if ((PmovAction&3)==2) ret = areaRead (data,1,len,PmovFile);\r
+  return (ret != len);\r
+}\r
+\r
+#define SCAN_VAR(x,y) ScanVar(&x,sizeof(x),y,PmovFile,PmovAction);\r
+#define SCANP(x)      ScanVar(&Pico.x,sizeof(Pico.x),#x,PmovFile,PmovAction);\r
+\r
+// Pack the cpu into a common format:\r
+PICO_INTERNAL void PicoAreaPackCpu(unsigned char *cpu, int is_sub)\r
+{\r
+  unsigned int pc=0;\r
+\r
+#if defined(EMU_C68K)\r
+  struct Cyclone *context = is_sub ? &PicoCpuCS68k : &PicoCpuCM68k;\r
+  memcpy(cpu,context->d,0x40);\r
+  pc=context->pc-context->membase;\r
+  *(unsigned int *)(cpu+0x44)=CycloneGetSr(context);\r
+  *(unsigned int *)(cpu+0x48)=context->osp;\r
+  cpu[0x4c] = context->irq;\r
+  cpu[0x4d] = context->state_flags & 1;\r
+#elif defined(EMU_M68K)\r
+  void *oldcontext = m68ki_cpu_p;\r
+  m68k_set_context(is_sub ? &PicoCpuMS68k : &PicoCpuMM68k);\r
+  memcpy(cpu,m68ki_cpu_p->dar,0x40);\r
+  pc=m68ki_cpu_p->pc;\r
+  *(unsigned int  *)(cpu+0x44)=m68k_get_reg(NULL, M68K_REG_SR);\r
+  *(unsigned int  *)(cpu+0x48)=m68ki_cpu_p->sp[m68ki_cpu_p->s_flag^SFLAG_SET];\r
+  cpu[0x4c] = CPU_INT_LEVEL>>8;\r
+  cpu[0x4d] = CPU_STOPPED;\r
+  m68k_set_context(oldcontext);\r
+#elif defined(EMU_F68K)\r
+  M68K_CONTEXT *context = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k;\r
+  memcpy(cpu,context->dreg,0x40);\r
+  pc=context->pc;\r
+  *(unsigned int  *)(cpu+0x44)=context->sr;\r
+  *(unsigned int  *)(cpu+0x48)=context->asp;\r
+  cpu[0x4c] = context->interrupts[0];\r
+  cpu[0x4d] = (context->execinfo & FM68K_HALTED) ? 1 : 0;\r
+#endif\r
+\r
+  *(unsigned int *)(cpu+0x40)=pc;\r
+}\r
+\r
+PICO_INTERNAL void PicoAreaUnpackCpu(unsigned char *cpu, int is_sub)\r
+{\r
+#if defined(EMU_C68K)\r
+  struct Cyclone *context = is_sub ? &PicoCpuCS68k : &PicoCpuCM68k;\r
+  CycloneSetSr(context, *(unsigned int *)(cpu+0x44));\r
+  context->osp=*(unsigned int *)(cpu+0x48);\r
+  memcpy(context->d,cpu,0x40);\r
+  context->membase=0;\r
+  context->pc = context->checkpc(*(unsigned int *)(cpu+0x40)); // Base pc\r
+  context->irq = cpu[0x4c];\r
+  context->state_flags = 0;\r
+  if (cpu[0x4d])\r
+    context->state_flags |= 1;\r
+#elif defined(EMU_M68K)\r
+  void *oldcontext = m68ki_cpu_p;\r
+  m68k_set_context(is_sub ? &PicoCpuMS68k : &PicoCpuMM68k);\r
+  m68k_set_reg(M68K_REG_SR, *(unsigned int *)(cpu+0x44));\r
+  memcpy(m68ki_cpu_p->dar,cpu,0x40);\r
+  m68ki_cpu_p->pc=*(unsigned int *)(cpu+0x40);\r
+  m68ki_cpu_p->sp[m68ki_cpu_p->s_flag^SFLAG_SET]=*(unsigned int *)(cpu+0x48);\r
+  CPU_INT_LEVEL = cpu[0x4c] << 8;\r
+  CPU_STOPPED = cpu[0x4d];\r
+  m68k_set_context(oldcontext);\r
+#elif defined(EMU_F68K)\r
+  M68K_CONTEXT *context = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k;\r
+  memcpy(context->dreg,cpu,0x40);\r
+  context->pc =*(unsigned int *)(cpu+0x40);\r
+  context->sr =*(unsigned int *)(cpu+0x44);\r
+  context->asp=*(unsigned int *)(cpu+0x48);\r
+  context->interrupts[0] = cpu[0x4c];\r
+  context->execinfo &= ~FM68K_HALTED;\r
+  if (cpu[0x4d]&1) context->execinfo |= FM68K_HALTED;\r
+#endif\r
+}\r
+\r
+// Scan the contents of the virtual machine's memory for saving or loading\r
+static int PicoAreaScan(int PmovAction,unsigned int ver, void *PmovFile)\r
+{\r
+  void *ym2612_regs;\r
+  unsigned char cpu[0x60];\r
+  unsigned char cpu_z80[0x60];\r
+  int ret;\r
+\r
+  memset(&cpu,0,sizeof(cpu));\r
+  memset(&cpu_z80,0,sizeof(cpu_z80));\r
+\r
+  ym2612_regs = YM2612GetRegs();\r
+\r
+  if (PmovAction&4)\r
+  {\r
+    Pico.m.scanline=0;\r
+\r
+    // Scan all the memory areas:\r
+    SCANP(ram) SCANP(vram) SCANP(zram) SCANP(cram) SCANP(vsram)\r
+\r
+    // Pack, scan and unpack the cpu data:\r
+    if((PmovAction&3)==1) PicoAreaPackCpu(cpu, 0);\r
+    //PicoMemInit();\r
+    SCAN_VAR(cpu,"cpu")\r
+    if((PmovAction&3)==2) PicoAreaUnpackCpu(cpu, 0);\r
+\r
+    SCAN_VAR(Pico.m    ,"misc")\r
+    SCAN_VAR(Pico.video,"video")\r
+\r
+    if (PicoOpt&7) {\r
+      if((PmovAction&3)==1) z80_pack(cpu_z80);\r
+      ret = SCAN_VAR(cpu_z80,"cpu_z80")\r
+      // do not unpack if we fail to load z80 state\r
+      if((PmovAction&3)==2) {\r
+        if(ret) z80_reset();\r
+        else    z80_unpack(cpu_z80);\r
+      }\r
+    }\r
+    if (PicoOpt&3)\r
+      ScanVar(sn76496_regs,28*4,"SN76496state", PmovFile, PmovAction); // regs and other stuff\r
+    if (PicoOpt&1) {\r
+      if((PmovAction&3)==1) ym2612_pack_state();\r
+      ScanVar(ym2612_regs, 0x200+4, "YM2612state", PmovFile, PmovAction); // regs + addr line\r
+      if((PmovAction&3)==2) ym2612_unpack_state(); // reload YM2612 state from it's regs\r
+    }\r
+  }\r
+\r
+  return 0;\r
+}\r
+\r
+// ---------------------------------------------------------------------------\r
+// Helper code to save/load to a file handle\r
+\r
+// Save or load the state from PmovFile:\r
+int PmovState(int PmovAction, void *PmovFile)\r
+{\r
+  int minimum=0;\r
+  unsigned char head[32];\r
+\r
+  if ((PicoAHW & PAHW_MCD) || carthw_chunks != NULL)\r
+  {\r
+    if (PmovAction&1) return PicoCdSaveState(PmovFile);\r
+    if (PmovAction&2) {\r
+      int ret = PicoCdLoadState(PmovFile);\r
+      if (PicoLoadStateHook) PicoLoadStateHook();\r
+      return ret;\r
+    }\r
+  }\r
+\r
+  memset(head,0,sizeof(head));\r
+\r
+  // Find out minimal compatible version:\r
+  //PicoAreaScan(PmovAction&0xc,&minimum);\r
+  minimum = 0x0021;\r
+\r
+  memcpy(head,"Pico",4);\r
+  *(unsigned int *)(head+0x8)=PicoVer;\r
+  *(unsigned int *)(head+0xc)=minimum;\r
+\r
+  // Scan header:\r
+  if (PmovAction&1) areaWrite(head,1,sizeof(head),PmovFile);\r
+  if (PmovAction&2) areaRead (head,1,sizeof(head),PmovFile);\r
+\r
+  // Scan memory areas:\r
+  PicoAreaScan(PmovAction, *(unsigned int *)(head+0x8), PmovFile);\r
+\r
+  if ((PmovAction&2) && PicoLoadStateHook) PicoLoadStateHook();\r
+\r
+  return 0;\r
+}\r
+\r