From: notaz <notasas@gmail.com>
Date: Mon, 26 May 2008 18:58:21 +0000 (+0000)
Subject: timers implemented for new z80 mode
X-Git-Tag: v1.85~485
X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=43e6eaad0b73b37907be3342e0fd4cf65919e9f6;p=picodrive.git

timers implemented for new z80 mode

git-svn-id: file:///home/notaz/opt/svn/PicoDrive@460 be3aeb3a-fb24-0410-a615-afba39da0efa
---

diff --git a/Pico/Memory.c b/Pico/Memory.c
index 45d9fdf8..fb6b99c4 100644
--- a/Pico/Memory.c
+++ b/Pico/Memory.c
@@ -706,6 +706,53 @@ static int get_scanline(int is_from_z80)
   return vcounts[SekCyclesDone()>>8];
 }
 
+/* probably not should be in this file, but it's near related code here */
+void ym2612_sync_timers(int z80_cycles, int mode_old, int mode_new)
+{
+  int xcycles = z80_cycles << 8;
+
+  /* check for overflows */
+  if ((mode_old & 4) && xcycles > timer_a_next_oflow)
+    ym2612.OPN.ST.status |= 1;
+
+  if ((mode_old & 8) && xcycles > timer_b_next_oflow)
+    ym2612.OPN.ST.status |= 2;
+
+  /* update timer a */
+  if (mode_old & 1)
+    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;
+    }
+    else
+      timer_a_next_oflow = xcycles + timer_a_offset;
+  }
+  if (mode_new & 1)
+    elprintf(EL_YMTIMER, "timer a upd to %i @ %i", timer_a_next_oflow>>8, z80_cycles);
+
+  /* update timer b */
+  if (mode_old & 2)
+    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;
+    }
+    else
+      timer_b_next_oflow = xcycles + timer_b_offset;
+  }
+  if (mode_new & 2)
+    elprintf(EL_YMTIMER, "timer b upd to %i @ %i", timer_b_next_oflow>>8, z80_cycles);
+}
+
 // ym2612 DAC and timer I/O handlers for z80
 int ym2612_write_local(u32 a, u32 d, int is_from_z80)
 {
@@ -752,12 +799,12 @@ int ym2612_write_local(u32 a, u32 d, int is_from_z80)
             //ym2612.OPN.ST.TAC = (1024-TAnew)*18;
             //ym2612.OPN.ST.TAT = 0;
             //
-            timer_a_step = 16495 * (1024 - TAnew);
-            if ((ym2612.OPN.ST.mode & 5) == 5) {
+            timer_a_step = timer_a_offset = 16495 * (1024 - TAnew);
+            if (ym2612.OPN.ST.mode & 1) {
               int cycles = is_from_z80 ? z80_cyclesDone() : cycles_68k_to_z80(SekCyclesDone());
               timer_a_next_oflow = (cycles << 8) + timer_a_step;
-              //elprintf(EL_STATUS, "set to %i @ %i", timer_a_next_oflow>>8, cycles);
             }
+            elprintf(EL_YMTIMER, "timer a set to %i, %i", 1024 - TAnew, timer_a_next_oflow>>8);
           }
           return 0;
         }
@@ -768,36 +815,36 @@ int ym2612_write_local(u32 a, u32 d, int is_from_z80)
             //ym2612.OPN.ST.TBC  = (256-d)<<4;
             //ym2612.OPN.ST.TBC *= 18;
             //ym2612.OPN.ST.TBT  = 0;
+            timer_b_step = timer_b_offset = 263912 * (256 - d);
+            if (ym2612.OPN.ST.mode & 2) {
+              int cycles = is_from_z80 ? z80_cyclesDone() : cycles_68k_to_z80(SekCyclesDone());
+              timer_b_next_oflow = (cycles << 8) + timer_b_step;
+            }
+            elprintf(EL_YMTIMER, "timer b set to %i, %i", 256 - d, timer_b_next_oflow>>8);
           }
           return 0;
         case 0x27: { /* mode, timer control */
           int old_mode = ym2612.OPN.ST.mode;
-          int xcycles = is_from_z80 ? z80_cyclesDone() : cycles_68k_to_z80(SekCyclesDone());
-          xcycles <<= 8;
+          int cycles = is_from_z80 ? z80_cyclesDone() : cycles_68k_to_z80(SekCyclesDone());
+          ym2612.OPN.ST.mode = d;
 
-          //elprintf(EL_STATUS, "st mode %02x", d);
+          elprintf(EL_YMTIMER, "st mode %02x", d);
+          ym2612_sync_timers(cycles, old_mode, d);
 
-          if ((ym2612.OPN.ST.mode & 5) != 5 && (d & 5) == 5) {
-            timer_a_next_oflow = xcycles + timer_a_step;
-            //elprintf(EL_STATUS, "set to %i @ %i st", timer_a_next_oflow>>8, xcycles >> 8);
-          }
+          /* reset Timer a flag */
+          if (d & 0x10)
+            ym2612.OPN.ST.status &= ~1;
 
           /* reset Timer b flag */
           if (d & 0x20)
             ym2612.OPN.ST.status &= ~2;
 
-          /* reset Timer a flag */
-          if (d & 0x10) {
-            if (ym2612.OPN.ST.status & 1)
-              while (xcycles > timer_a_next_oflow)
-                timer_a_next_oflow += timer_a_step;
-            ym2612.OPN.ST.status &= ~1;
-          }
-          if (!(d & 5)) timer_a_next_oflow = 0x80000000;
-          ym2612.OPN.ST.mode = d;
+          if ((d ^ old_mode) & 0xc0) {
 #ifdef __GP2X__
-          if (PicoOpt & POPT_EXT_FM) YM2612Write_940(a, d, get_scanline(is_from_z80));
+            if (PicoOpt & POPT_EXT_FM) YM2612Write_940(a, d, get_scanline(is_from_z80));
 #endif
+            return 1;
+          }
           return 0;
         }
         case 0x2b: { /* DAC Sel  (YM2612) */
@@ -836,16 +883,33 @@ int ym2612_write_local(u32 a, u32 d, int is_from_z80)
   return YM2612Write_(a, d);
 }
 
-// TODO: timer b, 68k side + asm, savestates
+// TODO: savestates
+#define ym2612_read_local() \
+  if (xcycles >= timer_a_next_oflow) \
+    ym2612.OPN.ST.status |= (ym2612.OPN.ST.mode >> 2) & 1; \
+  if (xcycles >= timer_b_next_oflow) \
+    ym2612.OPN.ST.status |= (ym2612.OPN.ST.mode >> 2) & 2
+
+
 u32 ym2612_read_local_z80(void)
 {
   int xcycles = z80_cyclesDone() << 8;
-  if (timer_a_next_oflow != 0x80000000 && xcycles >= timer_a_next_oflow) {
-    ym2612.OPN.ST.status |= 1;
-  }
 
-  //elprintf(EL_STATUS, "timer %i, sched %i, @ %i|%i", ym2612.OPN.ST.status, timer_a_next_oflow>>8,
-  //     xcycles >> 8, (xcycles >> 8) / 228);
+  ym2612_read_local();
+
+  elprintf(EL_YMTIMER, "timer z80 read %i, sched %i, %i @ %i|%i", ym2612.OPN.ST.status,
+      timer_a_next_oflow>>8, timer_b_next_oflow>>8, xcycles >> 8, (xcycles >> 8) / 228);
+  return ym2612.OPN.ST.status;
+}
+
+u32 ym2612_read_local_68k(void)
+{
+  int xcycles = cycles_68k_to_z80(SekCyclesDone()) << 8;
+
+  ym2612_read_local();
+
+  elprintf(EL_YMTIMER, "timer 68k read %i, sched %i, %i @ %i|%i", ym2612.OPN.ST.status,
+      timer_a_next_oflow>>8, timer_b_next_oflow>>8, xcycles >> 8, (xcycles >> 8) / 228);
   return ym2612.OPN.ST.status;
 }
 
@@ -869,13 +933,13 @@ PICO_INTERNAL unsigned char z80_read(unsigned short a)
     addr68k=Pico.m.z80_bank68k<<15;
     addr68k+=a&0x7fff;
 
+    if (addr68k < Pico.romsize) { ret = Pico.rom[addr68k^1]; goto bnkend; }
+    elprintf(EL_ANOMALY, "z80->68k upper read [%06x] %02x", addr68k, ret);
     if (PicoAHW & PAHW_MCD)
          ret = PicoReadM68k8(addr68k);
     else ret = PicoRead8(addr68k);
-    if (addr68k >= 0x400000) // not many games do this
-      { elprintf(EL_ANOMALY, "z80->68k upper read [%06x] %02x", addr68k, ret); }
-    else
-      { elprintf(EL_Z80BNK, "z80->68k r8 [%06x] %02x", addr68k, ret); }
+bnkend:
+    elprintf(EL_Z80BNK, "z80->68k r8 [%06x] %02x", addr68k, ret);
     return ret;
   }
 
diff --git a/Pico/Memory.s b/Pico/Memory.s
index dbe76ac5..04d062f0 100644
--- a/Pico/Memory.s
+++ b/Pico/Memory.s
@@ -389,11 +389,7 @@ m_read8_misc2:
     ldr     r1, =PicoOpt
     ldr     r1, [r1]
     tst     r1, #1
-
-    ldrne   r1, =ym2612_st
-    ldrne   r1, [r1]
-    ldrneb  r0, [r1, #0x11]   @ ym2612_st->status
-    bxne    lr
+    bne     ym2612_read_local_68k
 
 m_read8_fake_ym2612:
     ldr     r3, =(Pico+0x22200)
diff --git a/Pico/MemoryCmn.c b/Pico/MemoryCmn.c
index 2cf8e8d3..4b5620da 100644
--- a/Pico/MemoryCmn.c
+++ b/Pico/MemoryCmn.c
@@ -114,9 +114,8 @@ u32 OtherRead16(u32 a, int realsize)
       elprintf(EL_ANOMALY, "68k z80 read with no bus! [%06x] @ %06x", a, SekPc);
     if ((a&0x4000)==0x0000) { d=z80Read8(a); d|=d<<8; goto end; } // Z80 ram (not byteswaped)
     if ((a&0x6000)==0x4000) { // 0x4000-0x5fff, Fudge if disabled
-      if(PicoOpt&POPT_EN_FM) d=YM2612Read();
+      if (PicoOpt&POPT_EN_FM) d=ym2612_read_local_68k();
       else d=Pico.m.rotate++&3;
-      elprintf(EL_YM2612R, "read ym2612: %02x", d);
       goto end;
     }
     elprintf(EL_ANOMALY, "68k bad read [%06x]", a);
diff --git a/Pico/Memory_amips.s b/Pico/Memory_amips.s
index 4ad7cd30..5a6b7681 100644
--- a/Pico/Memory_amips.s
+++ b/Pico/Memory_amips.s
@@ -421,12 +421,8 @@ m_read8_z80_misc:
     lui     $t0, %hi(PicoOpt)
     lw      $t0, %lo(PicoOpt)($t0)
     andi    $t0, 1
-    beqz    $t0, m_read8_fake_ym2612
-    lui     $t0, %hi(Pico+0x22208)
-    lui     $t0, %hi(ym2612_st)
-    lw      $t0, %lo(ym2612_st)($t0)
-    jr      $ra
-    lb      $v0, 0x11($t0)
+    bnez    $t0, ym2612_read_local_68k
+    nop
 
 m_read8_fake_ym2612:
     lb      $v0, %lo(Pico+0x22208)($t0) # Pico.m.rotate
diff --git a/Pico/PicoInt.h b/Pico/PicoInt.h
index 8525198f..bf5c46d9 100644
--- a/Pico/PicoInt.h
+++ b/Pico/PicoInt.h
@@ -431,6 +431,7 @@ PICO_INTERNAL_ASM void PicoMemResetCDdecode(int r3);
 
 // Pico/Memory.c
 PICO_INTERNAL void PicoMemSetupPico(void);
+PICO_INTERNAL unsigned int ym2612_read_local_68k(void);
 
 // Pico.c
 extern struct Pico Pico;
@@ -474,13 +475,23 @@ PICO_INTERNAL void cdda_start_play();
 extern short cdda_out_buffer[2*1152];
 extern int PsndLen_exc_cnt;
 extern int PsndLen_exc_add;
-extern int timer_a_next_oflow, timer_a_step; // in z80 cycles
+extern int timer_a_next_oflow, timer_a_step, timer_a_offset; // in z80 cycles
+extern int timer_b_next_oflow, timer_b_step, timer_b_offset;
+
+void ym2612_sync_timers(int z80_cycles, int mode_old, int mode_new);
 
 #define timers_cycle() \
-  if (timer_a_next_oflow > 0) timer_a_next_oflow -= Pico.m.pal ? 70938*256 : 59659*256
+  if (timer_a_next_oflow > 0 && timer_a_next_oflow < 0x70000000) \
+    timer_a_next_oflow -= Pico.m.pal ? 70938*256 : 59659*256; \
+  if (timer_b_next_oflow > 0 && timer_b_next_oflow < 0x70000000) \
+    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 = 0x80000000
+  timer_a_next_oflow = timer_b_next_oflow = 0x70000000; \
+  timer_a_step = timer_a_offset = 16495 * 1024; \
+  timer_b_step = timer_b_offset = 263912 * 256;
+
 
 // VideoPort.c
 PICO_INTERNAL_ASM void PicoVideoWrite(unsigned int a,unsigned short d);
@@ -528,7 +539,7 @@ extern int PsndDacLine;
 #define EL_HVCNT   0x00000001 /* hv counter reads */
 #define EL_SR      0x00000002 /* SR reads */
 #define EL_INTS    0x00000004 /* ints and acks */
-#define EL_YM2612R 0x00000008 /* 68k ym2612 reads */
+#define EL_YMTIMER 0x00000008 /* ym2612 timer stuff */
 #define EL_INTSW   0x00000010 /* log irq switching on/off */
 #define EL_ASVDP   0x00000020 /* VDP accesses during active scan */
 #define EL_VDPDMA  0x00000040 /* VDP DMA transfers and their timing */
diff --git a/Pico/carthw/svp/compiler.c b/Pico/carthw/svp/compiler.c
index 551794e7..2db22b1f 100644
--- a/Pico/carthw/svp/compiler.c
+++ b/Pico/carthw/svp/compiler.c
@@ -1763,6 +1763,7 @@ void *ssp_translate_block(int pc)
 		fwrite(tcache, 1, (tcache_ptr - tcache)*4, f);
 		fclose(f);
 	}
+	printf("dumped tcache.bin\n");
 	exit(0);
 #endif
 
diff --git a/Pico/cd/Pico.c b/Pico/cd/Pico.c
index bd61c2b7..0bb61161 100644
--- a/Pico/cd/Pico.c
+++ b/Pico/cd/Pico.c
@@ -2,7 +2,7 @@
 
 
 #include "../PicoInt.h"
-
+#include "../sound/ym2612.h"
 
 extern unsigned char formatted_bram[4*0x10];
 extern unsigned int s68k_poll_adclk;
diff --git a/Pico/sound/sound.c b/Pico/sound/sound.c
index 6c7c6d87..4bc92922 100644
--- a/Pico/sound/sound.c
+++ b/Pico/sound/sound.c
@@ -35,8 +35,8 @@ int PsndDacLine=0;
 short *PsndOut=NULL; // PCM data buffer
 
 // timers
-int timer_a_next_oflow, timer_a_step; // in z80 cycles
-//int
+int timer_a_next_oflow, timer_a_step, timer_a_offset; // in z80 cycles
+int timer_b_next_oflow, timer_b_step, timer_b_offset;
 
 // sn76496
 extern int *sn76496_regs;
@@ -46,13 +46,15 @@ static void dac_recalculate(void)
 {
   int i, dac_cnt, pos, len, lines = Pico.m.pal ? 312 : 262, mid = Pico.m.pal ? 68 : 93;
 
-  if(PsndLen <= lines) {
+  if (PsndLen <= lines)
+  {
     // shrinking algo
     dac_cnt = -PsndLen;
     len=1; pos=0;
     dac_info[225] = 1;
 
-    for(i=226; i != 225; i++) {
+    for(i=226; i != 225; i++)
+    {
       if (i >= lines) i = 0;
       len = 0;
       if(dac_cnt < 0) {
@@ -63,11 +65,14 @@ static void dac_recalculate(void)
       dac_cnt -= PsndLen;
       dac_info[i] = (pos<<4)|len;
     }
-  } else {
+  }
+  else
+  {
     // stretching
     dac_cnt = PsndLen;
     pos=0;
-    for(i = 225; i != 224; i++) {
+    for(i = 225; i != 224; i++)
+    {
       if (i >= lines) i = 0;
       len=0;
       while(dac_cnt >= 0) {
diff --git a/Pico/sound/ym2612.c b/Pico/sound/ym2612.c
index b9749f24..34dabecd 100644
--- a/Pico/sound/ym2612.c
+++ b/Pico/sound/ym2612.c
@@ -1584,8 +1584,6 @@ static int OPNWriteReg(int r, int v)
 /*      YM2612 local section                                                   */
 /*******************************************************************************/
 
-FM_ST *ym2612_st;
-
 /* Generate samples for YM2612 */
 int YM2612UpdateOne_(int *buffer, int length, int stereo, int is_buf_empty)
 {
@@ -1650,9 +1648,6 @@ int YM2612UpdateOne_(int *buffer, int length, int stereo, int is_buf_empty)
 /* initialize YM2612 emulator */
 void YM2612Init_(int clock, int rate)
 {
-	// notaz
-	ym2612_st = &ym2612.OPN.ST;
-
 	memset(&ym2612, 0, sizeof(ym2612));
 	init_tables();
 
diff --git a/Pico/sound/ym2612.h b/Pico/sound/ym2612.h
index 39c3cf66..a1124de4 100644
--- a/Pico/sound/ym2612.h
+++ b/Pico/sound/ym2612.h
@@ -141,35 +141,16 @@ typedef struct
 } YM2612;
 #endif
 
-extern FM_ST *ym2612_st;
 #ifndef EXTERNAL_YM2612
 extern YM2612 ym2612;
 #endif
 
-#define YM2612Read() ym2612_st->status
-
-#define YM2612PicoTick(n) \
-{ \
-	/* timer A */ \
-	if(ym2612_st->mode & 0x01 && (ym2612_st->TAT+=64*n) >= ym2612_st->TAC) { \
-		ym2612_st->TAT -= ym2612_st->TAC; \
-		if(ym2612_st->mode & 0x04) ym2612_st->status |= 1; \
-	} \
- \
-	/* timer B */ \
-	if(ym2612_st->mode & 0x02 && (ym2612_st->TBT+=64*n) >= ym2612_st->TBC) { \
-		ym2612_st->TBT -= ym2612_st->TBC; \
-		if(ym2612_st->mode & 0x08) ym2612_st->status |= 2; \
-	} \
-}
-
-
 void YM2612Init_(int baseclock, int rate);
 void YM2612ResetChip_(void);
 int  YM2612UpdateOne_(int *buffer, int length, int stereo, int is_buf_empty);
 
 int  YM2612Write_(unsigned int a, unsigned int v);
-unsigned char YM2612Read_(void);
+//unsigned char YM2612Read_(void);
 
 int  YM2612PicoTick_(int n);
 void YM2612PicoStateLoad_(void);
diff --git a/cpu/DrZ80/drz80.s b/cpu/DrZ80/drz80.s
index 9b07a133..283cd8d0 100644
--- a/cpu/DrZ80/drz80.s
+++ b/cpu/DrZ80/drz80.s
@@ -24,7 +24,7 @@
       .extern PicoRead8
       .extern Pico
       .extern z80_write
-      .extern ym2612_st
+      .extern ym2612_read_local_z80
 .endif
 
 DrZ80Ver: .long 0x0001
diff --git a/platform/gp2x/940ctl.h b/platform/gp2x/940ctl.h
index 0318c1c5..32512820 100644
--- a/platform/gp2x/940ctl.h
+++ b/platform/gp2x/940ctl.h
@@ -6,7 +6,6 @@ void YM2612ResetChip_940(void);
 int  YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty);
 
 int  YM2612Write_940(unsigned int a, unsigned int v, int scanline);
-unsigned char YM2612Read_940(void);
 
 int  YM2612PicoTick_940(int n);
 void YM2612PicoStateLoad_940(void);