UIQ3 bugfixes, SVP drc indirect jumps, stuff
[picodrive.git] / Pico / Memory.c
index a9f0927..8975fa2 100644 (file)
@@ -76,8 +76,9 @@ PICO_INTERNAL u32 PicoCheckPc(u32 pc)
   pc&=~1;\r
   if ((pc<<8) == 0)\r
   {\r
-    printf("%i:%03i: game crash detected @ %06x\n", Pico.m.frame_count, Pico.m.scanline, SekPc);\r
-    return (int)Pico.rom + Pico.romsize; // common crash condition, can happen if acc timing is off\r
+    elprintf(EL_STATUS|EL_ANOMALY, "%i:%03i: game crash detected @ %06x\n",\r
+      Pico.m.frame_count, Pico.m.scanline, SekPc);\r
+    return (int)Pico.rom + Pico.romsize; // common crash condition, may happen with bad ROMs\r
   }\r
 \r
   PicoCpuCM68k.membase=PicoMemBase(pc&0x00ffffff);\r
@@ -89,10 +90,9 @@ PICO_INTERNAL u32 PicoCheckPc(u32 pc)
 }\r
 \r
 \r
-PICO_INTERNAL int PicoInitPc(u32 pc)\r
+PICO_INTERNAL void PicoInitPc(u32 pc)\r
 {\r
   PicoCheckPc(pc);\r
-  return 0;\r
 }\r
 \r
 #ifndef _ASM_MEMORY_C\r
@@ -106,7 +106,7 @@ PICO_INTERNAL_ASM void PicoMemReset(void)
 int PadRead(int i)\r
 {\r
   int pad,value,data_reg;\r
-  pad=~PicoPad[i]; // Get inverse of pad MXYZ SACB RLDU\r
+  pad=~PicoPadInt[i]; // Get inverse of pad MXYZ SACB RLDU\r
   data_reg=Pico.ioports[i+1];\r
 \r
   // orr the bits, which are set as output\r
@@ -345,9 +345,9 @@ PICO_INTERNAL_ASM u32 PicoRead8(u32 a)
   log_io(a, 8, 0);\r
   if ((a&0xff4000)==0xa00000) { d=z80Read8(a); goto end; } // Z80 Ram\r
 \r
-  if ((a&0xe700e0)==0xc00000) // VDP\r
-       d=PicoVideoRead(a);\r
-  else d=OtherRead16(a&~1, 8);\r
+  if ((a&0xe700e0)==0xc00000) { d=PicoVideoRead8(a); goto end; } // VDP\r
+\r
+  d=OtherRead16(a&~1, 8);\r
   if ((a&1)==0) d>>=8;\r
 \r
 end:\r
@@ -466,7 +466,7 @@ void PicoWrite16(u32 a,u16 d)
 \r
 static void PicoWrite32(u32 a,u32 d)\r
 {\r
-  elprintf(EL_IO, "w32: %06x, %08x", a&0xffffff, d);\r
+  elprintf(EL_IO, "w32: %06x, %08x @%06x", a&0xffffff, d, SekPc);\r
 #ifdef EMU_CORE_DEBUG\r
   lastwrite_cyc_d[lwp_cyc++&15] = d;\r
 #endif\r
@@ -689,8 +689,6 @@ static void m68k_mem_setup(void)
 \r
 // -----------------------------------------------------------------\r
 \r
-extern const unsigned short vcounts[];\r
-\r
 static int get_scanline(int is_from_z80)\r
 {\r
   if (is_from_z80) {\r
@@ -700,13 +698,10 @@ static int get_scanline(int is_from_z80)
     return z80_scanline;\r
   }\r
 \r
-  if (Pico.m.scanline != -1)\r
-    return Pico.m.scanline;\r
-\r
-  return vcounts[SekCyclesDone()>>8];\r
+  return Pico.m.scanline;\r
 }\r
 \r
-/* probably not should be in this file, but it's near related code here */\r
+/* probably should not be in this file, but it's near related code here */\r
 void ym2612_sync_timers(int z80_cycles, int mode_old, int mode_new)\r
 {\r
   int xcycles = z80_cycles << 8;\r
@@ -720,34 +715,30 @@ void ym2612_sync_timers(int z80_cycles, int mode_old, int mode_new)
 \r
   /* update timer a */\r
   if (mode_old & 1)\r
-    while (xcycles >= timer_a_next_oflow)\r
+    while (xcycles > timer_a_next_oflow)\r
       timer_a_next_oflow += timer_a_step;\r
 \r
   if ((mode_old ^ mode_new) & 1) // turning on/off\r
   {\r
-    if (mode_old & 1) {\r
-      timer_a_offset = timer_a_next_oflow - xcycles;\r
-      timer_a_next_oflow = 0x70000000;\r
-    }\r
+    if (mode_old & 1)\r
+      timer_a_next_oflow = TIMER_NO_OFLOW;\r
     else\r
-      timer_a_next_oflow = xcycles + timer_a_offset;\r
+      timer_a_next_oflow = xcycles + timer_a_step;\r
   }\r
   if (mode_new & 1)\r
     elprintf(EL_YMTIMER, "timer a upd to %i @ %i", timer_a_next_oflow>>8, z80_cycles);\r
 \r
   /* update timer b */\r
   if (mode_old & 2)\r
-    while (xcycles >= timer_b_next_oflow)\r
+    while (xcycles > timer_b_next_oflow)\r
       timer_b_next_oflow += timer_b_step;\r
 \r
   if ((mode_old ^ mode_new) & 2)\r
   {\r
-    if (mode_old & 2) {\r
-      timer_b_offset = timer_b_next_oflow - xcycles;\r
-      timer_b_next_oflow = 0x70000000;\r
-    }\r
+    if (mode_old & 2)\r
+      timer_b_next_oflow = TIMER_NO_OFLOW;\r
     else\r
-      timer_b_next_oflow = xcycles + timer_b_offset;\r
+      timer_b_next_oflow = xcycles + timer_b_step;\r
   }\r
   if (mode_new & 2)\r
     elprintf(EL_YMTIMER, "timer b upd to %i @ %i", timer_b_next_oflow>>8, z80_cycles);\r
@@ -798,9 +789,9 @@ int ym2612_write_local(u32 a, u32 d, int is_from_z80)
             ym2612.OPN.ST.TA = TAnew;\r
             //ym2612.OPN.ST.TAC = (1024-TAnew)*18;\r
             //ym2612.OPN.ST.TAT = 0;\r
-            //\r
-            timer_a_step = timer_a_offset = 16495 * (1024 - TAnew);\r
+            timer_a_step = TIMER_A_TICK_ZCYCLES * (1024 - TAnew);\r
             if (ym2612.OPN.ST.mode & 1) {\r
+              // this is not right, should really be done on overflow only\r
               int cycles = is_from_z80 ? z80_cyclesDone() : cycles_68k_to_z80(SekCyclesDone());\r
               timer_a_next_oflow = (cycles << 8) + timer_a_step;\r
             }\r
@@ -812,10 +803,9 @@ int ym2612_write_local(u32 a, u32 d, int is_from_z80)
           if (ym2612.OPN.ST.TB != d) {\r
             //elprintf(EL_STATUS, "timer b set %i", d);\r
             ym2612.OPN.ST.TB = d;\r
-            //ym2612.OPN.ST.TBC  = (256-d)<<4;\r
-            //ym2612.OPN.ST.TBC *= 18;\r
+            //ym2612.OPN.ST.TBC = (256-d) * 288;\r
             //ym2612.OPN.ST.TBT  = 0;\r
-            timer_b_step = timer_b_offset = 263912 * (256 - d);\r
+            timer_b_step = TIMER_B_TICK_ZCYCLES * (256 - d); // 262800\r
             if (ym2612.OPN.ST.mode & 2) {\r
               int cycles = is_from_z80 ? z80_cyclesDone() : cycles_68k_to_z80(SekCyclesDone());\r
               timer_b_next_oflow = (cycles << 8) + timer_b_step;\r
@@ -841,7 +831,7 @@ int ym2612_write_local(u32 a, u32 d, int is_from_z80)
 \r
           if ((d ^ old_mode) & 0xc0) {\r
 #ifdef __GP2X__\r
-            if (PicoOpt & POPT_EXT_FM) YM2612Write_940(a, d, get_scanline(is_from_z80));\r
+            if (PicoOpt & POPT_EXT_FM) return YM2612Write_940(a, d, get_scanline(is_from_z80));\r
 #endif\r
             return 1;\r
           }\r
@@ -912,21 +902,77 @@ u32 ym2612_read_local_68k(void)
   return ym2612.OPN.ST.status;\r
 }\r
 \r
-// TODO: new ym2612 savestates\r
+void ym2612_pack_state(void)\r
+{\r
+  // timers are saved as tick counts, in 16.16 int format\r
+  int tac, tat = 0, tbc, tbt = 0;\r
+  tac = 1024 - ym2612.OPN.ST.TA;\r
+  tbc = 256  - ym2612.OPN.ST.TB;\r
+  if (timer_a_next_oflow != TIMER_NO_OFLOW)\r
+    tat = (int)((double)(timer_a_step - timer_a_next_oflow) / (double)timer_a_step * tac * 65536);\r
+  if (timer_b_next_oflow != TIMER_NO_OFLOW)\r
+    tbt = (int)((double)(timer_b_step - timer_b_next_oflow) / (double)timer_b_step * tbc * 65536);\r
+  elprintf(EL_YMTIMER, "save: timer a %i/%i", tat >> 16, tac);\r
+  elprintf(EL_YMTIMER, "save: timer b %i/%i", tbt >> 16, tbc);\r
+\r
+#ifdef __GP2X__\r
+  if (PicoOpt & POPT_EXT_FM)\r
+    YM2612PicoStateSave2_940(tat, tbt);\r
+  else\r
+#endif\r
+    YM2612PicoStateSave2(tat, tbt);\r
+}\r
+\r
 void ym2612_unpack_state(void)\r
 {\r
-  int i;\r
+  int i, ret, tac, tat, tbc, tbt;\r
   YM2612PicoStateLoad();\r
 \r
   // feed all the registers and update internal state\r
-  for (i = 0x20; i < 0xC0; i++) {\r
+  for (i = 0x20; i < 0xA0; i++) {\r
+    ym2612_write_local(0, i, 0);\r
+    ym2612_write_local(1, ym2612.REGS[i], 0);\r
+  }\r
+  for (i = 0x30; i < 0xA0; i++) {\r
+    ym2612_write_local(2, i, 0);\r
+    ym2612_write_local(3, ym2612.REGS[i|0x100], 0);\r
+  }\r
+  for (i = 0xAF; i >= 0xA0; i--) { // must apply backwards\r
+    ym2612_write_local(2, i, 0);\r
+    ym2612_write_local(3, ym2612.REGS[i|0x100], 0);\r
     ym2612_write_local(0, i, 0);\r
     ym2612_write_local(1, ym2612.REGS[i], 0);\r
   }\r
-  for (i = 0x30; i < 0xC0; i++) {\r
+  for (i = 0xB0; i < 0xB8; i++) {\r
+    ym2612_write_local(0, i, 0);\r
+    ym2612_write_local(1, ym2612.REGS[i], 0);\r
     ym2612_write_local(2, i, 0);\r
     ym2612_write_local(3, ym2612.REGS[i|0x100], 0);\r
   }\r
+\r
+#ifdef __GP2X__\r
+  if (PicoOpt & POPT_EXT_FM)\r
+    ret = YM2612PicoStateLoad2_940(&tat, &tbt);\r
+  else\r
+#endif\r
+    ret = YM2612PicoStateLoad2(&tat, &tbt);\r
+  if (ret != 0) {\r
+    elprintf(EL_STATUS, "old ym2612 state");\r
+    return; // no saved timers\r
+  }\r
+\r
+  tac = (1024 - ym2612.OPN.ST.TA) << 16;\r
+  tbc = (256  - ym2612.OPN.ST.TB) << 16;\r
+  if (ym2612.OPN.ST.mode & 1)\r
+    timer_a_next_oflow = (int)((double)(tac - tat) / (double)tac * timer_a_step);\r
+  else\r
+    timer_a_next_oflow = TIMER_NO_OFLOW;\r
+  if (ym2612.OPN.ST.mode & 2)\r
+    timer_b_next_oflow = (int)((double)(tbc - tbt) / (double)tbc * timer_b_step);\r
+  else\r
+    timer_b_next_oflow = TIMER_NO_OFLOW;\r
+  elprintf(EL_YMTIMER, "load: %i/%i, timer_a_next_oflow %i", tat>>16, tac>>16, timer_a_next_oflow >> 8);\r
+  elprintf(EL_YMTIMER, "load: %i/%i, timer_b_next_oflow %i", tbt>>16, tbc>>16, timer_b_next_oflow >> 8);\r
 }\r
 \r
 // -----------------------------------------------------------------\r
@@ -938,8 +984,7 @@ PICO_INTERNAL unsigned char z80_read(unsigned short a)
 \r
   if ((a>>13)==2) // 0x4000-0x5fff (Charles MacDonald)\r
   {\r
-    if (PicoOpt&POPT_EN_FM) ret = ym2612_read_local_z80();\r
-    return ret;\r
+    return ym2612_read_local_z80();\r
   }\r
 \r
   if (a>=0x8000)\r