From 6f7beab435cf3224780701f7c79b199440665718 Mon Sep 17 00:00:00 2001
From: kub <derkub@gmail.com>
Date: Thu, 16 Jul 2020 19:05:46 +0200
Subject: [PATCH] audio, fix sound issues in some intros

---
 pico/memory.c      | 6 +++++-
 pico/sound/sound.c | 4 ++--
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/pico/memory.c b/pico/memory.c
index c0ba9ffe..ff41ac96 100644
--- a/pico/memory.c
+++ b/pico/memory.c
@@ -946,9 +946,9 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80)
   {
     int cycles = is_from_z80 ? z80_cyclesDone() : z80_cycles_from_68k();
     //elprintf(EL_STATUS, "%03i dac w %08x z80 %i", cycles, d, is_from_z80);
-    ym2612.dacout = ((int)d - 0x80) << 6;
     if (ym2612.dacen)
       PsndDoDAC(cycles);
+    ym2612.dacout = ((int)d - 0x80) << 6;
     return 0;
   }
 
@@ -1008,6 +1008,9 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80)
         case 0x27: { /* mode, timer control */
           int old_mode = ym2612.OPN.ST.mode;
           int cycles = is_from_z80 ? z80_cyclesDone() : z80_cycles_from_68k();
+
+          if (ym2612.OPN.ST.mode != d)
+            PsndDoFM(cycles);
           ym2612.OPN.ST.mode = d;
 
           elprintf(EL_YMTIMER, "st mode %02x", d);
@@ -1066,6 +1069,7 @@ static int ym2612_write_local(u32 a, u32 d, int is_from_z80)
 
 
 #define ym2612_read_local() \
+  PsndDoFM(xcycles>>8); \
   if (xcycles >= Pico.t.timer_a_next_oflow) \
     ym2612.OPN.ST.status |= (ym2612.OPN.ST.mode >> 2) & 1; \
   if (xcycles >= Pico.t.timer_b_next_oflow) \
diff --git a/pico/sound/sound.c b/pico/sound/sound.c
index a6d55df2..0b371f25 100644
--- a/pico/sound/sound.c
+++ b/pico/sound/sound.c
@@ -169,8 +169,8 @@ PICO_INTERNAL void PsndDoFM(int cyc_to)
   // Q16, number of samples since last call
   len = (cyc_to * Pico.snd.clkl_mult) - Pico.snd.fm_pos;
 
-  // don't do this too often (about every 4th scanline)
-  if (len >> 20 <= PicoIn.sndRate >> 12)
+  // don't do this too often (about once every canline)
+  if (len >> 16 <= PicoIn.sndRate >> 10)
     return;
 
   // update position and calculate buffer offset and length
-- 
2.39.5