\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
+ timer_a_next_oflow = TIMER_NO_OFLOW;\r
}\r
else\r
timer_a_next_oflow = xcycles + timer_a_offset;\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
+ timer_b_next_oflow = TIMER_NO_OFLOW;\r
}\r
else\r
timer_b_next_oflow = xcycles + timer_b_offset;\r
{\r
//elprintf(EL_STATUS, "timer a set %i", TAnew);\r
ym2612.OPN.ST.TA = TAnew;\r
- ym2612.OPN.ST.TAC = (1024-TAnew)*18;\r
+ //ym2612.OPN.ST.TAC = (1024-TAnew)*18;\r
//ym2612.OPN.ST.TAT = 0;\r
- //\r
timer_a_step = timer_a_offset = 16466 * (1024 - TAnew);\r
if (ym2612.OPN.ST.mode & 1) {\r
int cycles = is_from_z80 ? z80_cyclesDone() : cycles_68k_to_z80(SekCyclesDone());\r
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) * 288;\r
+ //ym2612.OPN.ST.TBC = (256-d) * 288;\r
//ym2612.OPN.ST.TBT = 0;\r
timer_b_step = timer_b_offset = 262800 * (256 - d); // 262881\r
if (ym2612.OPN.ST.mode & 2) {\r
\r
void ym2612_pack_state(void)\r
{\r
- // TODO timers\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(0, 0)*/;\r
+ /*YM2612PicoStateSave2_940(tat, tbt)*/;\r
else\r
#endif\r
- YM2612PicoStateSave2(0, 0);\r
+ YM2612PicoStateSave2(tat, tbt);\r
}\r
\r
void ym2612_unpack_state(void)\r
{\r
- int i, ret, tat, tbt;\r
+ int i, ret, tac, tat, tbc, tbt;\r
YM2612PicoStateLoad();\r
\r
// feed all the registers and update internal state\r
else\r
#endif\r
ret = YM2612PicoStateLoad2(&tat, &tbt);\r
+ if (ret != 0) return; // no saved timers\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 = (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 = (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
void ym2612_pack_state(void);\r
void ym2612_unpack_state(void);\r
\r
+#define TIMER_NO_OFLOW 0x70000000\r
+\r
#define timers_cycle() \\r
- if (timer_a_next_oflow > 0 && timer_a_next_oflow < 0x70000000) \\r
+ if (timer_a_next_oflow > 0 && timer_a_next_oflow < TIMER_NO_OFLOW) \\r
timer_a_next_oflow -= Pico.m.pal ? 70938*256 : 59659*256; \\r
- if (timer_b_next_oflow > 0 && timer_b_next_oflow < 0x70000000) \\r
+ if (timer_b_next_oflow > 0 && timer_b_next_oflow < TIMER_NO_OFLOW) \\r
timer_b_next_oflow -= Pico.m.pal ? 70938*256 : 59659*256; \\r
ym2612_sync_timers(0, ym2612.OPN.ST.mode, ym2612.OPN.ST.mode);\r
\r
#define timers_reset() \\r
- timer_a_next_oflow = timer_b_next_oflow = 0x70000000; \\r
+ timer_a_next_oflow = timer_b_next_oflow = TIMER_NO_OFLOW; \\r
timer_a_step = timer_a_offset = 16495 * 1024; \\r
timer_b_step = timer_b_offset = 263912 * 256;\r
\r