cd: rewrite pcm
[picodrive.git] / pico / sound / sound.c
index b1204e2..ec0e205 100644 (file)
@@ -1,19 +1,21 @@
-// This is part of Pico Library\r
-\r
-// (c) Copyright 2004 Dave, All rights reserved.\r
-// (c) Copyright 2006,2007 notaz, All rights reserved.\r
-// Free for non-commercial use.\r
-\r
-// For commercial use, separate licencing terms must be obtained.\r
-\r
+/*\r
+ * PicoDrive\r
+ * (c) Copyright Dave, 2004\r
+ * (C) notaz, 2006-2009\r
+ *\r
+ * This work is licensed under the terms of MAME license.\r
+ * See COPYING file in the top-level directory.\r
+ */\r
 \r
 #include <string.h>\r
 #include "ym2612.h"\r
 #include "sn76496.h"\r
 #include "../pico_int.h"\r
-#include "../cd/pcm.h"\r
+#include "../cd/cue.h"\r
 #include "mix.h"\r
 \r
+#define SIMPLE_WRITE_SOUND 0\r
+\r
 void (*PsndMix_32_to_16l)(short *dest, int *src, int count) = mix_32_to_16l_stereo;\r
 \r
 // master int buffer to mix to\r
@@ -107,14 +109,9 @@ static void dac_recalculate(void)
 \r
 PICO_INTERNAL void PsndReset(void)\r
 {\r
-  void *ym2612_regs;\r
-\r
-  // also clear the internal registers+addr line\r
-  ym2612_regs = YM2612GetRegs();\r
-  memset(ym2612_regs, 0, 0x200+4);\r
-  timers_reset();\r
-\r
+  // PsndRerate calls YM2612Init, which also resets\r
   PsndRerate(0);\r
+  timers_reset();\r
 }\r
 \r
 \r
@@ -163,9 +160,6 @@ void PsndRerate(int preserve_state)
   // recalculate dac info\r
   dac_recalculate();\r
 \r
-  if (PicoAHW & PAHW_MCD)\r
-    pcm_set_rate(PsndRate);\r
-\r
   // clear all buffers\r
   memset32(PsndBuffer, 0, sizeof(PsndBuffer)/4);\r
   memset(cdda_out_buffer, 0, sizeof(cdda_out_buffer));\r
@@ -186,6 +180,9 @@ 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
+\r
   PsndDacLine = line_to + 1;\r
 \r
   pos =dac_info[line_from]>>4;\r
@@ -200,14 +197,6 @@ PICO_INTERNAL void PsndDoDAC(int line_to)
     short *d = PsndOut + pos;\r
     for (; len > 0; len--, d++)  *d = dout;\r
   }\r
-\r
-#if 0\r
-  if (do_pcm) {\r
-    int *d = PsndBuffer;\r
-    d += (PicoOpt&8) ? pos*2 : pos;\r
-    pcm_update(d, len, 1);\r
-  }\r
-#endif\r
 }\r
 \r
 // cdda\r
@@ -252,7 +241,7 @@ PICO_INTERNAL void cdda_start_play(void)
   lba_offset += Pico_mcd->TOC.Tracks[index].Offset;\r
 \r
   // find the actual file for this track\r
-  for (i = index; i >= 0; i--)\r
+  for (i = index; i > 0; i--)\r
     if (Pico_mcd->TOC.Tracks[i].F != NULL) break;\r
 \r
   if (Pico_mcd->TOC.Tracks[i].F == NULL) {\r
@@ -260,7 +249,7 @@ PICO_INTERNAL void cdda_start_play(void)
     return;\r
   }\r
 \r
-  if (Pico_mcd->TOC.Tracks[i].ftype == TYPE_MP3)\r
+  if (Pico_mcd->TOC.Tracks[i].ftype == CT_MP3)\r
   {\r
     int pos1024 = 0;\r
 \r
@@ -280,7 +269,7 @@ PICO_INTERNAL void cdda_start_play(void)
   cdda_stream = Pico_mcd->TOC.Tracks[i].F;\r
   PicoCDBufferFlush(); // buffering relies on fp not being touched\r
   pm_seek(cdda_stream, lba_offset * 2352, SEEK_SET);\r
-  if (Pico_mcd->TOC.Tracks[i].ftype == TYPE_WAV)\r
+  if (Pico_mcd->TOC.Tracks[i].ftype == CT_WAV)\r
   {\r
     // skip headers, assume it's 44kHz stereo uncompressed\r
     pm_seek(cdda_stream, 44, SEEK_CUR);\r
@@ -296,7 +285,7 @@ PICO_INTERNAL void PsndClear(void)
     memset32((int *) PsndOut, 0, len); // assume PsndOut to be aligned\r
   else {\r
     short *out = PsndOut;\r
-    if ((int)out & 2) { *out++ = 0; len--; }\r
+    if ((long)out & 2) { *out++ = 0; len--; }\r
     memset32((int *) out, 0, len/2);\r
     if (len & 1) out[len-1] = 0;\r
   }\r
@@ -308,11 +297,11 @@ static int PsndRender(int offset, int length)
   int  buf32_updated = 0;\r
   int *buf32 = PsndBuffer+offset;\r
   int stereo = (PicoOpt & 8) >> 3;\r
-  // emulating CD && PCM option enabled && PCM chip on && have enabled channels\r
-  int do_pcm = (PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_PCM) &&\r
-               (Pico_mcd->pcm.control & 0x80) && Pico_mcd->pcm.enabled;\r
+\r
   offset <<= stereo;\r
 \r
+  pprof_start(sound);\r
+\r
 #if !SIMPLE_WRITE_SOUND\r
   if (offset == 0) { // should happen once per frame\r
     // compensate for float part of PsndLen\r
@@ -340,10 +329,11 @@ static int PsndRender(int offset, int length)
     memset32(buf32, 0, length<<stereo);\r
 \r
 //printf("active_chs: %02x\n", buf32_updated);\r
+  (void)buf32_updated;\r
 \r
   // CD: PCM sound\r
-  if (do_pcm) {\r
-    pcm_update(buf32, length, stereo);\r
+  if (PicoAHW & PAHW_MCD) {\r
+    pcd_pcm_update(buf32, length, stereo);\r
     //buf32_updated = 1;\r
   }\r
 \r
@@ -355,15 +345,20 @@ static int PsndRender(int offset, int length)
     // note: only 44, 22 and 11 kHz supported, with forced stereo\r
     int index = Pico_mcd->scd.Cur_Track - 1;\r
 \r
-    if (Pico_mcd->TOC.Tracks[index].ftype == TYPE_MP3)\r
+    if (Pico_mcd->TOC.Tracks[index].ftype == CT_MP3)\r
       mp3_update(buf32, length, stereo);\r
     else\r
       cdda_raw_update(buf32, length);\r
   }\r
 \r
+  if ((PicoAHW & PAHW_32X) && (PicoOpt & POPT_EN_PWM))\r
+    p32x_pwm_update(buf32, length, stereo);\r
+\r
   // convert + limit to normal 16bit output\r
   PsndMix_32_to_16l(PsndOut+offset, buf32, length);\r
 \r
+  pprof_end(sound);\r
+\r
   return length;\r
 }\r
 \r
@@ -373,7 +368,8 @@ PICO_INTERNAL void PsndGetSamples(int y)
 #if SIMPLE_WRITE_SOUND\r
   if (y != 224) return;\r
   PsndRender(0, PsndLen);\r
-  if (PicoWriteSound) PicoWriteSound(PsndLen);\r
+  if (PicoWriteSound)\r
+    PicoWriteSound(PsndLen * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2));\r
   PsndClear();\r
 #else\r
   static int curr_pos = 0;\r
@@ -383,8 +379,11 @@ PICO_INTERNAL void PsndGetSamples(int y)
     if (emustatus & 2)\r
          curr_pos += PsndRender(curr_pos, PsndLen-PsndLen/2);\r
     else curr_pos  = PsndRender(0, PsndLen);\r
-    if (emustatus&1) emustatus|=2; else emustatus&=~2;\r
-    if (PicoWriteSound) PicoWriteSound(curr_pos);\r
+    if (emustatus & 1)\r
+         emustatus |=  2;\r
+    else emustatus &= ~2;\r
+    if (PicoWriteSound)\r
+      PicoWriteSound(curr_pos * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2));\r
     // clear sound buffer\r
     PsndClear();\r
   }\r
@@ -422,7 +421,7 @@ PICO_INTERNAL void PsndGetSamplesMS(void)
   }\r
 \r
   if (PicoWriteSound != NULL)\r
-    PicoWriteSound(length);\r
+    PicoWriteSound(length * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2));\r
   PsndClear();\r
 }\r
 \r