some more risky timing changes
[picodrive.git] / pico / sound / sound.c
index 8f88dd7..a67ebcc 100644 (file)
@@ -19,7 +19,7 @@ void (*PsndMix_32_to_16l)(short *dest, int *src, int count) = mix_32_to_16l_ster
 // master int buffer to mix to\r
 static int PsndBuffer[2*(44100+100)/50];\r
 \r
-// dac\r
+// dac, psg\r
 static unsigned short dac_info[312+4]; // pos in sample buffer\r
 \r
 // cdda output buffer\r
@@ -30,7 +30,7 @@ int PsndRate=0;
 int PsndLen=0; // number of mono samples, multiply by 2 for stereo\r
 int PsndLen_exc_add=0; // this is for non-integer sample counts per line, eg. 22050/60\r
 int PsndLen_exc_cnt=0;\r
-int PsndDacLine=0;\r
+int PsndDacLine, PsndPsgLine;\r
 short *PsndOut=NULL; // PCM data buffer\r
 static int PsndLen_use;\r
 \r
@@ -44,7 +44,7 @@ extern int *sn76496_regs;
 \r
 static void dac_recalculate(void)\r
 {\r
-  int i, dac_cnt, pos, len, lines = Pico.m.pal ? 312 : 262, mid = Pico.m.pal ? 68 : 93;\r
+  int i, dac_cnt, pos, len, lines = Pico.m.pal ? 313 : 262, mid = Pico.m.pal ? 68 : 93;\r
 \r
   if (PsndLen <= lines)\r
   {\r
@@ -158,7 +158,7 @@ PICO_INTERNAL void PsndStartFrame(void)
     PsndLen_use++;\r
   }\r
 \r
-  PsndDacLine = 0;\r
+  PsndDacLine = PsndPsgLine = 0;\r
   emustatus &= ~1;\r
   dac_info[224] = PsndLen_use;\r
 }\r
@@ -169,8 +169,8 @@ PICO_INTERNAL void PsndDoDAC(int line_to)
   int dout = ym2612.dacout;\r
   int line_from = PsndDacLine;\r
 \r
-  if (line_to >= 312)\r
-    line_to = 311;\r
+  if (line_to >= 313)\r
+    line_to = 312;\r
 \r
   pos  = dac_info[line_from];\r
   pos1 = dac_info[line_to + 1];\r
@@ -185,13 +185,42 @@ PICO_INTERNAL void PsndDoDAC(int line_to)
 \r
   if (PicoOpt & POPT_EN_STEREO) {\r
     short *d = PsndOut + pos*2;\r
-    for (; len > 0; len--, d+=2) *d = dout;\r
+    for (; len > 0; len--, d+=2) *d += dout;\r
   } else {\r
     short *d = PsndOut + pos;\r
-    for (; len > 0; len--, d++)  *d = dout;\r
+    for (; len > 0; len--, d++)  *d += dout;\r
   }\r
 }\r
 \r
+PICO_INTERNAL void PsndDoPSG(int line_to)\r
+{\r
+  int line_from = PsndPsgLine;\r
+  int pos, pos1, len;\r
+  int stereo = 0;\r
+\r
+  if (line_to >= 313)\r
+    line_to = 312;\r
+\r
+  pos  = dac_info[line_from];\r
+  pos1 = dac_info[line_to + 1];\r
+  len = pos1 - pos;\r
+  //elprintf(EL_STATUS, "%3d %3d %3d %3d %3d",\r
+  //  pos, pos1, len, line_from, line_to);\r
+  if (len <= 0)\r
+    return;\r
+\r
+  PsndPsgLine = line_to + 1;\r
+\r
+  if (!PsndOut || !(PicoOpt & POPT_EN_PSG))\r
+    return;\r
+\r
+  if (PicoOpt & POPT_EN_STEREO) {\r
+    stereo = 1;\r
+    pos <<= 1;\r
+  }\r
+  SN76496Update(PsndOut + pos, len, stereo);\r
+}\r
+\r
 // cdda\r
 static void cdda_raw_update(int *buffer, int length)\r
 {\r
@@ -264,10 +293,6 @@ static int PsndRender(int offset, int length)
 \r
   pprof_start(sound);\r
 \r
-  // PSG\r
-  if (PicoOpt & POPT_EN_PSG)\r
-    SN76496Update(PsndOut+offset, length, stereo);\r
-\r
   if (PicoAHW & PAHW_PICO) {\r
     PicoPicoPCMUpdate(PsndOut+offset, length, stereo);\r
     return length;\r
@@ -319,6 +344,7 @@ PICO_INTERNAL void PsndGetSamples(int y)
 \r
   if (ym2612.dacen && PsndDacLine < y)\r
     PsndDoDAC(y - 1);\r
+  PsndDoPSG(y - 1);\r
 \r
   if (y == 224)\r
   {\r