improve 64bit portability
[picodrive.git] / pico / sound / sound.c
index ec0e205..95aac12 100644 (file)
 #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
 static int PsndBuffer[2*(44100+100)/50];\r
 \r
-// dac\r
-static unsigned short dac_info[312+4]; // pppppppp ppppllll, p - pos in buff, l - length to write for this sample\r
+// dac, psg\r
+static unsigned short dac_info[312+4]; // pos in sample buffer\r
 \r
 // cdda output buffer\r
 short cdda_out_buffer[2*1152];\r
 \r
-// for Pico\r
-int PsndRate=0;\r
-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
-short *PsndOut=NULL; // PCM data buffer\r
-\r
-// timers\r
-int timer_a_next_oflow, timer_a_step; // in z80 cycles\r
-int timer_b_next_oflow, timer_b_step;\r
-\r
 // sn76496\r
 extern int *sn76496_regs;\r
 \r
 \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 lines = Pico.m.pal ? 313 : 262;\r
+  int mid = Pico.m.pal ? 68 : 93;\r
+  int i, dac_cnt, pos, len;\r
 \r
-  if (PsndLen <= lines)\r
+  if (Pico.snd.len <= lines)\r
   {\r
     // shrinking algo\r
-    dac_cnt = -PsndLen;\r
+    dac_cnt = -Pico.snd.len;\r
     len=1; pos=0;\r
     dac_info[225] = 1;\r
 \r
     for(i=226; i != 225; i++)\r
     {\r
       if (i >= lines) i = 0;\r
-      len = 0;\r
       if(dac_cnt < 0) {\r
-        len=1;\r
         pos++;\r
         dac_cnt += lines;\r
       }\r
-      dac_cnt -= PsndLen;\r
-      dac_info[i] = (pos<<4)|len;\r
+      dac_cnt -= Pico.snd.len;\r
+      dac_info[i] = pos;\r
     }\r
   }\r
   else\r
   {\r
     // stretching\r
-    dac_cnt = PsndLen;\r
+    dac_cnt = Pico.snd.len;\r
     pos=0;\r
     for(i = 225; i != 224; i++)\r
     {\r
@@ -81,29 +67,17 @@ static void dac_recalculate(void)
         len++;\r
       }\r
       if (i == mid) // midpoint\r
-        while(pos+len < PsndLen/2) {\r
+        while(pos+len < Pico.snd.len/2) {\r
           dac_cnt -= lines;\r
           len++;\r
         }\r
-      dac_cnt += PsndLen;\r
-      dac_info[i] = (pos<<4)|len;\r
-      pos+=len;\r
+      dac_cnt += Pico.snd.len;\r
+      pos += len;\r
+      dac_info[i] = pos;\r
     }\r
-    // last sample\r
-    for(len = 0, i = pos; i < PsndLen; i++) len++;\r
-    if (PsndLen_exc_add) len++;\r
-    dac_info[224] = (pos<<4)|len;\r
   }\r
-  mid = (dac_info[lines-1] & 0xfff0) + ((dac_info[lines-1] & 0xf) << 4);\r
   for (i = lines; i < sizeof(dac_info) / sizeof(dac_info[0]); i++)\r
-    dac_info[i] = mid;\r
-  //for(i=len=0; i < lines; i++) {\r
-  //  printf("%03i : %03i : %i\n", i, dac_info[i]>>4, dac_info[i]&0xf);\r
-  //  len+=dac_info[i]&0xf;\r
-  //}\r
-  //printf("rate is %i, len %f\n", PsndRate, (double)PsndRate/(Pico.m.pal ? 50.0 : 60.0));\r
-  //printf("len total: %i, last pos: %i\n", len, pos);\r
-  //exit(8);\r
+    dac_info[i] = dac_info[0];\r
 }\r
 \r
 \r
@@ -121,41 +95,30 @@ void PsndRerate(int preserve_state)
   void *state = NULL;\r
   int target_fps = Pico.m.pal ? 50 : 60;\r
 \r
-  // not all rates are supported in MCD mode due to mp3 decoder limitations\r
-  if (PicoAHW & PAHW_MCD) {\r
-    if (!(11025-100 <= PsndRate && PsndRate <= 11025+100) &&\r
-        !(22050-100 <= PsndRate && PsndRate <= 22050+100) &&\r
-        !(44100-100 <= PsndRate && PsndRate <= 44100+100))\r
-      PsndRate = 22050;\r
-    PicoOpt |= POPT_EN_STEREO; // force stereo\r
-  }\r
-\r
   if (preserve_state) {\r
     state = malloc(0x204);\r
     if (state == NULL) return;\r
     ym2612_pack_state();\r
     memcpy(state, YM2612GetRegs(), 0x204);\r
   }\r
-  YM2612Init(Pico.m.pal ? OSC_PAL/7 : OSC_NTSC/7, PsndRate);\r
+  YM2612Init(Pico.m.pal ? OSC_PAL/7 : OSC_NTSC/7, PicoIn.sndRate);\r
   if (preserve_state) {\r
     // feed it back it's own registers, just like after loading state\r
     memcpy(YM2612GetRegs(), state, 0x204);\r
     ym2612_unpack_state();\r
-    if ((PicoAHW & PAHW_MCD) && !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))\r
-      cdda_start_play();\r
   }\r
 \r
   if (preserve_state) memcpy(state, sn76496_regs, 28*4); // remember old state\r
-  SN76496_init(Pico.m.pal ? OSC_PAL/15 : OSC_NTSC/15, PsndRate);\r
+  SN76496_init(Pico.m.pal ? OSC_PAL/15 : OSC_NTSC/15, PicoIn.sndRate);\r
   if (preserve_state) memcpy(sn76496_regs, state, 28*4); // restore old state\r
 \r
   if (state)\r
     free(state);\r
 \r
-  // calculate PsndLen\r
-  PsndLen=PsndRate / target_fps;\r
-  PsndLen_exc_add=((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r
-  PsndLen_exc_cnt=0;\r
+  // calculate Pico.snd.len\r
+  Pico.snd.len = PicoIn.sndRate / target_fps;\r
+  Pico.snd.len_e_add = ((PicoIn.sndRate - Pico.snd.len * target_fps) << 16) / target_fps;\r
+  Pico.snd.len_e_cnt = 0;\r
 \r
   // recalculate dac info\r
   dac_recalculate();\r
@@ -163,60 +126,104 @@ void PsndRerate(int preserve_state)
   // clear all buffers\r
   memset32(PsndBuffer, 0, sizeof(PsndBuffer)/4);\r
   memset(cdda_out_buffer, 0, sizeof(cdda_out_buffer));\r
-  if (PsndOut)\r
+  if (PicoIn.sndOut)\r
     PsndClear();\r
 \r
   // set mixer\r
-  PsndMix_32_to_16l = (PicoOpt & POPT_EN_STEREO) ? mix_32_to_16l_stereo : mix_32_to_16_mono;\r
+  PsndMix_32_to_16l = (PicoIn.opt & POPT_EN_STEREO) ? mix_32_to_16l_stereo : mix_32_to_16_mono;\r
 \r
-  if (PicoAHW & PAHW_PICO)\r
+  if (PicoIn.AHW & PAHW_PICO)\r
     PicoReratePico();\r
 }\r
 \r
 \r
+PICO_INTERNAL void PsndStartFrame(void)\r
+{\r
+  // compensate for float part of Pico.snd.len\r
+  Pico.snd.len_use = Pico.snd.len;\r
+  Pico.snd.len_e_cnt += Pico.snd.len_e_add;\r
+  if (Pico.snd.len_e_cnt >= 0x10000) {\r
+    Pico.snd.len_e_cnt -= 0x10000;\r
+    Pico.snd.len_use++;\r
+  }\r
+\r
+  Pico.snd.dac_line = Pico.snd.psg_line = 0;\r
+  Pico.m.status &= ~1;\r
+  dac_info[224] = Pico.snd.len_use;\r
+}\r
+\r
 PICO_INTERNAL void PsndDoDAC(int line_to)\r
 {\r
   int pos, pos1, len;\r
   int dout = ym2612.dacout;\r
-  int line_from = PsndDacLine;\r
+  int line_from = Pico.snd.dac_line;\r
 \r
-  if (line_to >= 312)\r
-    line_to = 311;\r
+  if (line_to >= 313)\r
+    line_to = 312;\r
 \r
-  PsndDacLine = line_to + 1;\r
+  pos  = dac_info[line_from];\r
+  pos1 = dac_info[line_to + 1];\r
+  len = pos1 - pos;\r
+  if (len <= 0)\r
+    return;\r
 \r
-  pos =dac_info[line_from]>>4;\r
-  pos1=dac_info[line_to];\r
-  len = ((pos1>>4)-pos) + (pos1&0xf);\r
-  if (!len) return;\r
+  Pico.snd.dac_line = line_to + 1;\r
 \r
-  if (PicoOpt & POPT_EN_STEREO) {\r
-    short *d = PsndOut + pos*2;\r
-    for (; len > 0; len--, d+=2) *d = dout;\r
+  if (!PicoIn.sndOut)\r
+    return;\r
+\r
+  if (PicoIn.opt & POPT_EN_STEREO) {\r
+    short *d = PicoIn.sndOut + pos*2;\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
+    short *d = PicoIn.sndOut + pos;\r
+    for (; len > 0; len--, d++)  *d += dout;\r
   }\r
 }\r
 \r
-// cdda\r
-static pm_file *cdda_stream = NULL;\r
+PICO_INTERNAL void PsndDoPSG(int line_to)\r
+{\r
+  int line_from = Pico.snd.psg_line;\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
+  Pico.snd.psg_line = line_to + 1;\r
+\r
+  if (!PicoIn.sndOut || !(PicoIn.opt & POPT_EN_PSG))\r
+    return;\r
+\r
+  if (PicoIn.opt & POPT_EN_STEREO) {\r
+    stereo = 1;\r
+    pos <<= 1;\r
+  }\r
+  SN76496Update(PicoIn.sndOut + pos, len, stereo);\r
+}\r
 \r
+// cdda\r
 static void cdda_raw_update(int *buffer, int length)\r
 {\r
   int ret, cdda_bytes, mult = 1;\r
-  if (cdda_stream == NULL)\r
-    return;\r
 \r
   cdda_bytes = length*4;\r
-  if (PsndRate <= 22050 + 100) mult = 2;\r
-  if (PsndRate <  22050 - 100) mult = 4;\r
+  if (PicoIn.sndRate <= 22050 + 100) mult = 2;\r
+  if (PicoIn.sndRate <  22050 - 100) mult = 4;\r
   cdda_bytes *= mult;\r
 \r
-  ret = pm_read(cdda_out_buffer, cdda_bytes, cdda_stream);\r
+  ret = pm_read(cdda_out_buffer, cdda_bytes, Pico_mcd->cdda_stream);\r
   if (ret < cdda_bytes) {\r
     memset((char *)cdda_out_buffer + ret, 0, cdda_bytes - ret);\r
-    cdda_stream = NULL;\r
+    Pico_mcd->cdda_stream = NULL;\r
     return;\r
   }\r
 \r
@@ -228,64 +235,37 @@ static void cdda_raw_update(int *buffer, int length)
   }\r
 }\r
 \r
-PICO_INTERNAL void cdda_start_play(void)\r
+void cdda_start_play(int lba_base, int lba_offset, int lb_len)\r
 {\r
-  int lba_offset, index, lba_length, i;\r
-\r
-  elprintf(EL_STATUS, "cdda play track #%i", Pico_mcd->scd.Cur_Track);\r
-\r
-  index = Pico_mcd->scd.Cur_Track - 1;\r
-\r
-  lba_offset = Pico_mcd->scd.Cur_LBA - Track_to_LBA(index + 1);\r
-  if (lba_offset < 0) lba_offset = 0;\r
-  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
-    if (Pico_mcd->TOC.Tracks[i].F != NULL) break;\r
-\r
-  if (Pico_mcd->TOC.Tracks[i].F == NULL) {\r
-    elprintf(EL_STATUS|EL_ANOMALY, "no track?!");\r
-    return;\r
-  }\r
-\r
-  if (Pico_mcd->TOC.Tracks[i].ftype == CT_MP3)\r
+  if (Pico_mcd->cdda_type == CT_MP3)\r
   {\r
     int pos1024 = 0;\r
 \r
-    lba_length = Pico_mcd->TOC.Tracks[i].Length;\r
-    for (i++; i < Pico_mcd->TOC.Last_Track; i++) {\r
-      if (Pico_mcd->TOC.Tracks[i].F != NULL) break;\r
-      lba_length += Pico_mcd->TOC.Tracks[i].Length;\r
-    }\r
-\r
     if (lba_offset)\r
-      pos1024 = lba_offset * 1024 / lba_length;\r
+      pos1024 = lba_offset * 1024 / lb_len;\r
 \r
-    mp3_start_play(Pico_mcd->TOC.Tracks[index].F, pos1024);\r
+    mp3_start_play(Pico_mcd->cdda_stream, pos1024);\r
     return;\r
   }\r
 \r
-  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 == CT_WAV)\r
+  pm_seek(Pico_mcd->cdda_stream, (lba_base + lba_offset) * 2352, SEEK_SET);\r
+  if (Pico_mcd->cdda_type == CT_WAV)\r
   {\r
     // skip headers, assume it's 44kHz stereo uncompressed\r
-    pm_seek(cdda_stream, 44, SEEK_CUR);\r
+    pm_seek(Pico_mcd->cdda_stream, 44, SEEK_CUR);\r
   }\r
 }\r
 \r
 \r
 PICO_INTERNAL void PsndClear(void)\r
 {\r
-  int len = PsndLen;\r
-  if (PsndLen_exc_add) len++;\r
-  if (PicoOpt & POPT_EN_STEREO)\r
-    memset32((int *) PsndOut, 0, len); // assume PsndOut to be aligned\r
+  int len = Pico.snd.len;\r
+  if (Pico.snd.len_e_add) len++;\r
+  if (PicoIn.opt & POPT_EN_STEREO)\r
+    memset32((int *) PicoIn.sndOut, 0, len); // assume PicoIn.sndOut to be aligned\r
   else {\r
-    short *out = PsndOut;\r
-    if ((long)out & 2) { *out++ = 0; len--; }\r
+    short *out = PicoIn.sndOut;\r
+    if ((uintptr_t)out & 2) { *out++ = 0; len--; }\r
     memset32((int *) out, 0, len/2);\r
     if (len & 1) out[len-1] = 0;\r
   }\r
@@ -296,34 +276,19 @@ static int PsndRender(int offset, int length)
 {\r
   int  buf32_updated = 0;\r
   int *buf32 = PsndBuffer+offset;\r
-  int stereo = (PicoOpt & 8) >> 3;\r
+  int stereo = (PicoIn.opt & 8) >> 3;\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
-    PsndLen_exc_cnt += PsndLen_exc_add;\r
-    if (PsndLen_exc_cnt >= 0x10000) {\r
-      PsndLen_exc_cnt -= 0x10000;\r
-      length++;\r
-    }\r
-  }\r
-#endif\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
+  if (PicoIn.AHW & PAHW_PICO) {\r
+    PicoPicoPCMUpdate(PicoIn.sndOut+offset, length, stereo);\r
     return length;\r
   }\r
 \r
   // Add in the stereo FM buffer\r
-  if (PicoOpt & POPT_EN_FM) {\r
+  if (PicoIn.opt & POPT_EN_FM) {\r
     buf32_updated = YM2612UpdateOne(buf32, length, stereo, 1);\r
   } else\r
     memset32(buf32, 0, length<<stereo);\r
@@ -332,30 +297,29 @@ static int PsndRender(int offset, int length)
   (void)buf32_updated;\r
 \r
   // CD: PCM sound\r
-  if (PicoAHW & PAHW_MCD) {\r
+  if (PicoIn.AHW & PAHW_MCD) {\r
     pcd_pcm_update(buf32, length, stereo);\r
     //buf32_updated = 1;\r
   }\r
 \r
   // CD: CDDA audio\r
   // CD mode, cdda enabled, not data track, CDC is reading\r
-  if ((PicoAHW & PAHW_MCD) && (PicoOpt & POPT_EN_MCD_CDDA) &&\r
-               !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))\r
+  if ((PicoIn.AHW & PAHW_MCD) && (PicoIn.opt & POPT_EN_MCD_CDDA)\r
+      && Pico_mcd->cdda_stream != NULL\r
+      && !(Pico_mcd->s68k_regs[0x36] & 1))\r
   {\r
     // 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 == CT_MP3)\r
+    if (Pico_mcd->cdda_type == 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
+  if ((PicoIn.AHW & PAHW_32X) && (PicoIn.opt & 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
+  PsndMix_32_to_16l(PicoIn.sndOut+offset, buf32, length);\r
 \r
   pprof_end(sound);\r
 \r
@@ -365,63 +329,52 @@ static int PsndRender(int offset, int length)
 // to be called on 224 or line_sample scanlines only\r
 PICO_INTERNAL void PsndGetSamples(int y)\r
 {\r
-#if SIMPLE_WRITE_SOUND\r
-  if (y != 224) return;\r
-  PsndRender(0, PsndLen);\r
-  if (PicoWriteSound)\r
-    PicoWriteSound(PsndLen * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2));\r
-  PsndClear();\r
-#else\r
   static int curr_pos = 0;\r
 \r
+  if (ym2612.dacen && Pico.snd.dac_line < y)\r
+    PsndDoDAC(y - 1);\r
+  PsndDoPSG(y - 1);\r
+\r
   if (y == 224)\r
   {\r
-    if (emustatus & 2)\r
-         curr_pos += PsndRender(curr_pos, PsndLen-PsndLen/2);\r
-    else curr_pos  = PsndRender(0, PsndLen);\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
+    if (Pico.m.status & 2)\r
+         curr_pos += PsndRender(curr_pos, Pico.snd.len-Pico.snd.len/2);\r
+    else curr_pos  = PsndRender(0, Pico.snd.len_use);\r
+    if (Pico.m.status & 1)\r
+         Pico.m.status |=  2;\r
+    else Pico.m.status &= ~2;\r
+    if (PicoIn.writeSound)\r
+      PicoIn.writeSound(curr_pos * ((PicoIn.opt & POPT_EN_STEREO) ? 4 : 2));\r
     // clear sound buffer\r
     PsndClear();\r
+    Pico.snd.dac_line = 224;\r
+    dac_info[224] = 0;\r
   }\r
-  else if (emustatus & 3) {\r
-    emustatus|= 2;\r
-    emustatus&=~1;\r
-    curr_pos = PsndRender(0, PsndLen/2);\r
+  else if (Pico.m.status & 3) {\r
+    Pico.m.status |=  2;\r
+    Pico.m.status &= ~1;\r
+    curr_pos = PsndRender(0, Pico.snd.len/2);\r
   }\r
-#endif\r
 }\r
 \r
 PICO_INTERNAL void PsndGetSamplesMS(void)\r
 {\r
-  int stereo = (PicoOpt & 8) >> 3;\r
-  int length = PsndLen;\r
-\r
-#if !SIMPLE_WRITE_SOUND\r
-  // compensate for float part of PsndLen\r
-  PsndLen_exc_cnt += PsndLen_exc_add;\r
-  if (PsndLen_exc_cnt >= 0x10000) {\r
-    PsndLen_exc_cnt -= 0x10000;\r
-    length++;\r
-  }\r
-#endif\r
+  int length = Pico.snd.len_use;\r
 \r
-  // PSG\r
-  if (PicoOpt & POPT_EN_PSG)\r
-    SN76496Update(PsndOut, length, stereo);\r
+  PsndDoPSG(223);\r
 \r
   // upmix to "stereo" if needed\r
-  if (stereo) {\r
+  if (PicoIn.opt & POPT_EN_STEREO) {\r
     int i, *p;\r
-    for (i = length, p = (void *)PsndOut; i > 0; i--, p++)\r
+    for (i = length, p = (void *)PicoIn.sndOut; i > 0; i--, p++)\r
       *p |= *p << 16;\r
   }\r
 \r
-  if (PicoWriteSound != NULL)\r
-    PicoWriteSound(length * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2));\r
+  if (PicoIn.writeSound != NULL)\r
+    PicoIn.writeSound(length * ((PicoIn.opt & POPT_EN_STEREO) ? 4 : 2));\r
   PsndClear();\r
+\r
+  dac_info[224] = 0;\r
 }\r
 \r
+// vim:shiftwidth=2:ts=2:expandtab\r