Browse Source

added timer saving to savestates

git-svn-id: file:///home/notaz/opt/svn/PicoDrive@487 be3aeb3a-fb24-0410-a615-afba39da0efa
notaz 16 years ago
parent
commit
e53704e691
5 changed files with 45 additions and 18 deletions
  1. 34 11
      Pico/Memory.c
  2. 2 1
      Pico/Pico.c
  3. 3 2
      Pico/PicoFrameHints.c
  4. 5 3
      Pico/PicoInt.h
  5. 1 1
      platform/linux/port_config.h

+ 34 - 11
Pico/Memory.c

@@ -720,14 +720,14 @@ void ym2612_sync_timers(int z80_cycles, int mode_old, int mode_new)
 
   /* update timer a */
   if (mode_old & 1)
-    while (xcycles >= timer_a_next_oflow)
+    while (xcycles > timer_a_next_oflow)
       timer_a_next_oflow += timer_a_step;
 
   if ((mode_old ^ mode_new) & 1) // turning on/off
   {
     if (mode_old & 1) {
       timer_a_offset = timer_a_next_oflow - xcycles;
-      timer_a_next_oflow = 0x70000000;
+      timer_a_next_oflow = TIMER_NO_OFLOW;
     }
     else
       timer_a_next_oflow = xcycles + timer_a_offset;
@@ -737,14 +737,14 @@ void ym2612_sync_timers(int z80_cycles, int mode_old, int mode_new)
 
   /* update timer b */
   if (mode_old & 2)
-    while (xcycles >= timer_b_next_oflow)
+    while (xcycles > timer_b_next_oflow)
       timer_b_next_oflow += timer_b_step;
 
   if ((mode_old ^ mode_new) & 2)
   {
     if (mode_old & 2) {
       timer_b_offset = timer_b_next_oflow - xcycles;
-      timer_b_next_oflow = 0x70000000;
+      timer_b_next_oflow = TIMER_NO_OFLOW;
     }
     else
       timer_b_next_oflow = xcycles + timer_b_offset;
@@ -796,9 +796,8 @@ int ym2612_write_local(u32 a, u32 d, int is_from_z80)
           {
             //elprintf(EL_STATUS, "timer a set %i", TAnew);
             ym2612.OPN.ST.TA = TAnew;
-            ym2612.OPN.ST.TAC = (1024-TAnew)*18;
+            //ym2612.OPN.ST.TAC = (1024-TAnew)*18;
             //ym2612.OPN.ST.TAT = 0;
-            //
             timer_a_step = timer_a_offset = 16466 * (1024 - TAnew);
             if (ym2612.OPN.ST.mode & 1) {
               int cycles = is_from_z80 ? z80_cyclesDone() : cycles_68k_to_z80(SekCyclesDone());
@@ -812,7 +811,7 @@ int ym2612_write_local(u32 a, u32 d, int is_from_z80)
           if (ym2612.OPN.ST.TB != d) {
             //elprintf(EL_STATUS, "timer b set %i", d);
             ym2612.OPN.ST.TB = d;
-            ym2612.OPN.ST.TBC = (256-d) * 288;
+            //ym2612.OPN.ST.TBC = (256-d) * 288;
             //ym2612.OPN.ST.TBT  = 0;
             timer_b_step = timer_b_offset = 262800 * (256 - d); // 262881
             if (ym2612.OPN.ST.mode & 2) {
@@ -913,18 +912,28 @@ u32 ym2612_read_local_68k(void)
 
 void ym2612_pack_state(void)
 {
-  // TODO timers
+  // timers are saved as tick counts, in 16.16 int format
+  int tac, tat = 0, tbc, tbt = 0;
+  tac = 1024 - ym2612.OPN.ST.TA;
+  tbc = 256  - ym2612.OPN.ST.TB;
+  if (timer_a_next_oflow != TIMER_NO_OFLOW)
+    tat = (int)((double)(timer_a_step - timer_a_next_oflow) / (double)timer_a_step * tac * 65536);
+  if (timer_b_next_oflow != TIMER_NO_OFLOW)
+    tbt = (int)((double)(timer_b_step - timer_b_next_oflow) / (double)timer_b_step * tbc * 65536);
+  elprintf(EL_YMTIMER, "save: timer a %i/%i", tat >> 16, tac);
+  elprintf(EL_YMTIMER, "save: timer b %i/%i", tbt >> 16, tbc);
+
 #ifdef __GP2X__
   if (PicoOpt & POPT_EXT_FM)
-    /*YM2612PicoStateSave2_940(0, 0)*/;
+    /*YM2612PicoStateSave2_940(tat, tbt)*/;
   else
 #endif
-    YM2612PicoStateSave2(0, 0);
+    YM2612PicoStateSave2(tat, tbt);
 }
 
 void ym2612_unpack_state(void)
 {
-  int i, ret, tat, tbt;
+  int i, ret, tac, tat, tbc, tbt;
   YM2612PicoStateLoad();
 
   // feed all the registers and update internal state
@@ -943,6 +952,20 @@ void ym2612_unpack_state(void)
   else
 #endif
     ret = YM2612PicoStateLoad2(&tat, &tbt);
+  if (ret != 0) return; // no saved timers
+
+  tac = (1024 - ym2612.OPN.ST.TA) << 16;
+  tbc = (256  - ym2612.OPN.ST.TB) << 16;
+  if (ym2612.OPN.ST.mode & 1)
+    timer_a_next_oflow = (double)(tac - tat) / (double)tac * timer_a_step;
+  else
+    timer_a_next_oflow = TIMER_NO_OFLOW;
+  if (ym2612.OPN.ST.mode & 2)
+    timer_b_next_oflow = (double)(tbc - tbt) / (double)tbc * timer_b_step;
+  else
+    timer_b_next_oflow = TIMER_NO_OFLOW;
+  elprintf(EL_YMTIMER, "load: %i/%i, timer_a_next_oflow %i", tat>>16, tac>>16, timer_a_next_oflow >> 8);
+  elprintf(EL_YMTIMER, "load: %i/%i, timer_b_next_oflow %i", tbt>>16, tbc>>16, timer_b_next_oflow >> 8);
 }
 
 // -----------------------------------------------------------------

+ 2 - 1
Pico/Pico.c

@@ -378,7 +378,6 @@ static int PicoFrameSimple(void)
 
   SekCyclesReset();
   z80_resetCycles();
-  timers_cycle();
 
   // 6 button pad: let's just say it timed out now
   Pico.m.padTHPhase[0]=Pico.m.padTHPhase[1]=0;
@@ -494,6 +493,8 @@ static int PicoFrameSimple(void)
   if (PsndOut && ym2612.dacen && PsndDacLine <= line_last)
     PsndDoDAC(line_last);
 
+  timers_cycle();
+
   return 0;
 }
 

+ 3 - 2
Pico/PicoFrameHints.c

@@ -63,7 +63,6 @@ static int PicoFrameHints(void)
 #ifdef PICO_CD
   SekCyclesResetS68k();
 #endif
-  timers_cycle();
   PsndDacLine = 0;
 
   pv->status&=~0x88; // clear V-Int, come out of vblank
@@ -241,10 +240,12 @@ static int PicoFrameHints(void)
 
   // sync z80
   if (Pico.m.z80Run && (PicoOpt&POPT_EN_Z80))
-    PicoSyncZ80(Pico.m.pal ? 151809 : 127671); // cycles adjusted for convertor
+    PicoSyncZ80(Pico.m.pal ? 151809 : 127671); // cycles adjusted for converter
   if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1)
     PsndDoDAC(lines-1);
 
+  timers_cycle();
+
   return 0;
 }
 

+ 5 - 3
Pico/PicoInt.h

@@ -479,15 +479,17 @@ void ym2612_sync_timers(int z80_cycles, int mode_old, int mode_new);
 void ym2612_pack_state(void);
 void ym2612_unpack_state(void);
 
+#define TIMER_NO_OFLOW 0x70000000
+
 #define timers_cycle() \
-  if (timer_a_next_oflow > 0 && timer_a_next_oflow < 0x70000000) \
+  if (timer_a_next_oflow > 0 && timer_a_next_oflow < TIMER_NO_OFLOW) \
     timer_a_next_oflow -= Pico.m.pal ? 70938*256 : 59659*256; \
-  if (timer_b_next_oflow > 0 && timer_b_next_oflow < 0x70000000) \
+  if (timer_b_next_oflow > 0 && timer_b_next_oflow < TIMER_NO_OFLOW) \
     timer_b_next_oflow -= Pico.m.pal ? 70938*256 : 59659*256; \
   ym2612_sync_timers(0, ym2612.OPN.ST.mode, ym2612.OPN.ST.mode);
 
 #define timers_reset() \
-  timer_a_next_oflow = timer_b_next_oflow = 0x70000000; \
+  timer_a_next_oflow = timer_b_next_oflow = TIMER_NO_OFLOW; \
   timer_a_step = timer_a_offset = 16495 * 1024; \
   timer_b_step = timer_b_offset = 263912 * 256;
 

+ 1 - 1
platform/linux/port_config.h

@@ -15,7 +15,7 @@
 #define SIMPLE_WRITE_SOUND	0
 #define mix_32_to_16l_stereo_lvl mix_32_to_16l_stereo
 
-#define EL_LOGMASK (EL_ANOMALY|EL_STATUS|EL_SRAMIO|EL_EEPROM|EL_UIO)//|EL_VDPDMA|EL_HVCNT|EL_ASVDP)//|EL_SVP)
+#define EL_LOGMASK (EL_ANOMALY|EL_STATUS|EL_SRAMIO|EL_EEPROM|EL_UIO|EL_YMTIMER)//|EL_VDPDMA|EL_HVCNT|EL_ASVDP)//|EL_SVP)
 // EL_VDPDMA|EL_ASVDP|EL_SR) // |EL_BUSREQ|EL_Z80BNK)
 
 //#define dprintf(f,...) printf("%05i:%03i: " f "\n",Pico.m.frame_count,Pico.m.scanline,##__VA_ARGS__)