some more risky timing changes
[picodrive.git] / pico / sound / sound.c
1 /*\r
2  * PicoDrive\r
3  * (c) Copyright Dave, 2004\r
4  * (C) notaz, 2006-2009\r
5  *\r
6  * This work is licensed under the terms of MAME license.\r
7  * See COPYING file in the top-level directory.\r
8  */\r
9 \r
10 #include <string.h>\r
11 #include "ym2612.h"\r
12 #include "sn76496.h"\r
13 #include "../pico_int.h"\r
14 #include "../cd/cue.h"\r
15 #include "mix.h"\r
16 \r
17 void (*PsndMix_32_to_16l)(short *dest, int *src, int count) = mix_32_to_16l_stereo;\r
18 \r
19 // master int buffer to mix to\r
20 static int PsndBuffer[2*(44100+100)/50];\r
21 \r
22 // dac, psg\r
23 static unsigned short dac_info[312+4]; // pos in sample buffer\r
24 \r
25 // cdda output buffer\r
26 short cdda_out_buffer[2*1152];\r
27 \r
28 // for Pico\r
29 int PsndRate=0;\r
30 int PsndLen=0; // number of mono samples, multiply by 2 for stereo\r
31 int PsndLen_exc_add=0; // this is for non-integer sample counts per line, eg. 22050/60\r
32 int PsndLen_exc_cnt=0;\r
33 int PsndDacLine, PsndPsgLine;\r
34 short *PsndOut=NULL; // PCM data buffer\r
35 static int PsndLen_use;\r
36 \r
37 // timers\r
38 int timer_a_next_oflow, timer_a_step; // in z80 cycles\r
39 int timer_b_next_oflow, timer_b_step;\r
40 \r
41 // sn76496\r
42 extern int *sn76496_regs;\r
43 \r
44 \r
45 static void dac_recalculate(void)\r
46 {\r
47   int i, dac_cnt, pos, len, lines = Pico.m.pal ? 313 : 262, mid = Pico.m.pal ? 68 : 93;\r
48 \r
49   if (PsndLen <= lines)\r
50   {\r
51     // shrinking algo\r
52     dac_cnt = -PsndLen;\r
53     len=1; pos=0;\r
54     dac_info[225] = 1;\r
55 \r
56     for(i=226; i != 225; i++)\r
57     {\r
58       if (i >= lines) i = 0;\r
59       if(dac_cnt < 0) {\r
60         pos++;\r
61         dac_cnt += lines;\r
62       }\r
63       dac_cnt -= PsndLen;\r
64       dac_info[i] = pos;\r
65     }\r
66   }\r
67   else\r
68   {\r
69     // stretching\r
70     dac_cnt = PsndLen;\r
71     pos=0;\r
72     for(i = 225; i != 224; i++)\r
73     {\r
74       if (i >= lines) i = 0;\r
75       len=0;\r
76       while(dac_cnt >= 0) {\r
77         dac_cnt -= lines;\r
78         len++;\r
79       }\r
80       if (i == mid) // midpoint\r
81         while(pos+len < PsndLen/2) {\r
82           dac_cnt -= lines;\r
83           len++;\r
84         }\r
85       dac_cnt += PsndLen;\r
86       pos += len;\r
87       dac_info[i] = pos;\r
88     }\r
89   }\r
90   for (i = lines; i < sizeof(dac_info) / sizeof(dac_info[0]); i++)\r
91     dac_info[i] = dac_info[0];\r
92 }\r
93 \r
94 \r
95 PICO_INTERNAL void PsndReset(void)\r
96 {\r
97   // PsndRerate calls YM2612Init, which also resets\r
98   PsndRerate(0);\r
99   timers_reset();\r
100 }\r
101 \r
102 \r
103 // to be called after changing sound rate or chips\r
104 void PsndRerate(int preserve_state)\r
105 {\r
106   void *state = NULL;\r
107   int target_fps = Pico.m.pal ? 50 : 60;\r
108 \r
109   if (preserve_state) {\r
110     state = malloc(0x204);\r
111     if (state == NULL) return;\r
112     ym2612_pack_state();\r
113     memcpy(state, YM2612GetRegs(), 0x204);\r
114   }\r
115   YM2612Init(Pico.m.pal ? OSC_PAL/7 : OSC_NTSC/7, PsndRate);\r
116   if (preserve_state) {\r
117     // feed it back it's own registers, just like after loading state\r
118     memcpy(YM2612GetRegs(), state, 0x204);\r
119     ym2612_unpack_state();\r
120   }\r
121 \r
122   if (preserve_state) memcpy(state, sn76496_regs, 28*4); // remember old state\r
123   SN76496_init(Pico.m.pal ? OSC_PAL/15 : OSC_NTSC/15, PsndRate);\r
124   if (preserve_state) memcpy(sn76496_regs, state, 28*4); // restore old state\r
125 \r
126   if (state)\r
127     free(state);\r
128 \r
129   // calculate PsndLen\r
130   PsndLen=PsndRate / target_fps;\r
131   PsndLen_exc_add=((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r
132   PsndLen_exc_cnt=0;\r
133 \r
134   // recalculate dac info\r
135   dac_recalculate();\r
136 \r
137   // clear all buffers\r
138   memset32(PsndBuffer, 0, sizeof(PsndBuffer)/4);\r
139   memset(cdda_out_buffer, 0, sizeof(cdda_out_buffer));\r
140   if (PsndOut)\r
141     PsndClear();\r
142 \r
143   // set mixer\r
144   PsndMix_32_to_16l = (PicoOpt & POPT_EN_STEREO) ? mix_32_to_16l_stereo : mix_32_to_16_mono;\r
145 \r
146   if (PicoAHW & PAHW_PICO)\r
147     PicoReratePico();\r
148 }\r
149 \r
150 \r
151 PICO_INTERNAL void PsndStartFrame(void)\r
152 {\r
153   // compensate for float part of PsndLen\r
154   PsndLen_use = PsndLen;\r
155   PsndLen_exc_cnt += PsndLen_exc_add;\r
156   if (PsndLen_exc_cnt >= 0x10000) {\r
157     PsndLen_exc_cnt -= 0x10000;\r
158     PsndLen_use++;\r
159   }\r
160 \r
161   PsndDacLine = PsndPsgLine = 0;\r
162   emustatus &= ~1;\r
163   dac_info[224] = PsndLen_use;\r
164 }\r
165 \r
166 PICO_INTERNAL void PsndDoDAC(int line_to)\r
167 {\r
168   int pos, pos1, len;\r
169   int dout = ym2612.dacout;\r
170   int line_from = PsndDacLine;\r
171 \r
172   if (line_to >= 313)\r
173     line_to = 312;\r
174 \r
175   pos  = dac_info[line_from];\r
176   pos1 = dac_info[line_to + 1];\r
177   len = pos1 - pos;\r
178   if (len <= 0)\r
179     return;\r
180 \r
181   PsndDacLine = line_to + 1;\r
182 \r
183   if (!PsndOut)\r
184     return;\r
185 \r
186   if (PicoOpt & POPT_EN_STEREO) {\r
187     short *d = PsndOut + pos*2;\r
188     for (; len > 0; len--, d+=2) *d += dout;\r
189   } else {\r
190     short *d = PsndOut + pos;\r
191     for (; len > 0; len--, d++)  *d += dout;\r
192   }\r
193 }\r
194 \r
195 PICO_INTERNAL void PsndDoPSG(int line_to)\r
196 {\r
197   int line_from = PsndPsgLine;\r
198   int pos, pos1, len;\r
199   int stereo = 0;\r
200 \r
201   if (line_to >= 313)\r
202     line_to = 312;\r
203 \r
204   pos  = dac_info[line_from];\r
205   pos1 = dac_info[line_to + 1];\r
206   len = pos1 - pos;\r
207   //elprintf(EL_STATUS, "%3d %3d %3d %3d %3d",\r
208   //  pos, pos1, len, line_from, line_to);\r
209   if (len <= 0)\r
210     return;\r
211 \r
212   PsndPsgLine = line_to + 1;\r
213 \r
214   if (!PsndOut || !(PicoOpt & POPT_EN_PSG))\r
215     return;\r
216 \r
217   if (PicoOpt & POPT_EN_STEREO) {\r
218     stereo = 1;\r
219     pos <<= 1;\r
220   }\r
221   SN76496Update(PsndOut + pos, len, stereo);\r
222 }\r
223 \r
224 // cdda\r
225 static void cdda_raw_update(int *buffer, int length)\r
226 {\r
227   int ret, cdda_bytes, mult = 1;\r
228 \r
229   cdda_bytes = length*4;\r
230   if (PsndRate <= 22050 + 100) mult = 2;\r
231   if (PsndRate <  22050 - 100) mult = 4;\r
232   cdda_bytes *= mult;\r
233 \r
234   ret = pm_read(cdda_out_buffer, cdda_bytes, Pico_mcd->cdda_stream);\r
235   if (ret < cdda_bytes) {\r
236     memset((char *)cdda_out_buffer + ret, 0, cdda_bytes - ret);\r
237     Pico_mcd->cdda_stream = NULL;\r
238     return;\r
239   }\r
240 \r
241   // now mix\r
242   switch (mult) {\r
243     case 1: mix_16h_to_32(buffer, cdda_out_buffer, length*2); break;\r
244     case 2: mix_16h_to_32_s1(buffer, cdda_out_buffer, length*2); break;\r
245     case 4: mix_16h_to_32_s2(buffer, cdda_out_buffer, length*2); break;\r
246   }\r
247 }\r
248 \r
249 void cdda_start_play(int lba_base, int lba_offset, int lb_len)\r
250 {\r
251   if (Pico_mcd->cdda_type == CT_MP3)\r
252   {\r
253     int pos1024 = 0;\r
254 \r
255     if (lba_offset)\r
256       pos1024 = lba_offset * 1024 / lb_len;\r
257 \r
258     mp3_start_play(Pico_mcd->cdda_stream, pos1024);\r
259     return;\r
260   }\r
261 \r
262   pm_seek(Pico_mcd->cdda_stream, (lba_base + lba_offset) * 2352, SEEK_SET);\r
263   if (Pico_mcd->cdda_type == CT_WAV)\r
264   {\r
265     // skip headers, assume it's 44kHz stereo uncompressed\r
266     pm_seek(Pico_mcd->cdda_stream, 44, SEEK_CUR);\r
267   }\r
268 }\r
269 \r
270 \r
271 PICO_INTERNAL void PsndClear(void)\r
272 {\r
273   int len = PsndLen;\r
274   if (PsndLen_exc_add) len++;\r
275   if (PicoOpt & POPT_EN_STEREO)\r
276     memset32((int *) PsndOut, 0, len); // assume PsndOut to be aligned\r
277   else {\r
278     short *out = PsndOut;\r
279     if ((long)out & 2) { *out++ = 0; len--; }\r
280     memset32((int *) out, 0, len/2);\r
281     if (len & 1) out[len-1] = 0;\r
282   }\r
283 }\r
284 \r
285 \r
286 static int PsndRender(int offset, int length)\r
287 {\r
288   int  buf32_updated = 0;\r
289   int *buf32 = PsndBuffer+offset;\r
290   int stereo = (PicoOpt & 8) >> 3;\r
291 \r
292   offset <<= stereo;\r
293 \r
294   pprof_start(sound);\r
295 \r
296   if (PicoAHW & PAHW_PICO) {\r
297     PicoPicoPCMUpdate(PsndOut+offset, length, stereo);\r
298     return length;\r
299   }\r
300 \r
301   // Add in the stereo FM buffer\r
302   if (PicoOpt & POPT_EN_FM) {\r
303     buf32_updated = YM2612UpdateOne(buf32, length, stereo, 1);\r
304   } else\r
305     memset32(buf32, 0, length<<stereo);\r
306 \r
307 //printf("active_chs: %02x\n", buf32_updated);\r
308   (void)buf32_updated;\r
309 \r
310   // CD: PCM sound\r
311   if (PicoAHW & PAHW_MCD) {\r
312     pcd_pcm_update(buf32, length, stereo);\r
313     //buf32_updated = 1;\r
314   }\r
315 \r
316   // CD: CDDA audio\r
317   // CD mode, cdda enabled, not data track, CDC is reading\r
318   if ((PicoAHW & PAHW_MCD) && (PicoOpt & POPT_EN_MCD_CDDA)\r
319       && Pico_mcd->cdda_stream != NULL\r
320       && !(Pico_mcd->s68k_regs[0x36] & 1))\r
321   {\r
322     // note: only 44, 22 and 11 kHz supported, with forced stereo\r
323     if (Pico_mcd->cdda_type == CT_MP3)\r
324       mp3_update(buf32, length, stereo);\r
325     else\r
326       cdda_raw_update(buf32, length);\r
327   }\r
328 \r
329   if ((PicoAHW & PAHW_32X) && (PicoOpt & POPT_EN_PWM))\r
330     p32x_pwm_update(buf32, length, stereo);\r
331 \r
332   // convert + limit to normal 16bit output\r
333   PsndMix_32_to_16l(PsndOut+offset, buf32, length);\r
334 \r
335   pprof_end(sound);\r
336 \r
337   return length;\r
338 }\r
339 \r
340 // to be called on 224 or line_sample scanlines only\r
341 PICO_INTERNAL void PsndGetSamples(int y)\r
342 {\r
343   static int curr_pos = 0;\r
344 \r
345   if (ym2612.dacen && PsndDacLine < y)\r
346     PsndDoDAC(y - 1);\r
347   PsndDoPSG(y - 1);\r
348 \r
349   if (y == 224)\r
350   {\r
351     if (emustatus & 2)\r
352          curr_pos += PsndRender(curr_pos, PsndLen-PsndLen/2);\r
353     else curr_pos  = PsndRender(0, PsndLen_use);\r
354     if (emustatus & 1)\r
355          emustatus |=  2;\r
356     else emustatus &= ~2;\r
357     if (PicoWriteSound)\r
358       PicoWriteSound(curr_pos * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2));\r
359     // clear sound buffer\r
360     PsndClear();\r
361     PsndDacLine = 224;\r
362     dac_info[224] = 0;\r
363   }\r
364   else if (emustatus & 3) {\r
365     emustatus|= 2;\r
366     emustatus&=~1;\r
367     curr_pos = PsndRender(0, PsndLen/2);\r
368   }\r
369 }\r
370 \r
371 PICO_INTERNAL void PsndGetSamplesMS(void)\r
372 {\r
373   int stereo = (PicoOpt & 8) >> 3;\r
374   int length = PsndLen_use;\r
375 \r
376   // PSG\r
377   if (PicoOpt & POPT_EN_PSG)\r
378     SN76496Update(PsndOut, length, stereo);\r
379 \r
380   // upmix to "stereo" if needed\r
381   if (stereo) {\r
382     int i, *p;\r
383     for (i = length, p = (void *)PsndOut; i > 0; i--, p++)\r
384       *p |= *p << 16;\r
385   }\r
386 \r
387   if (PicoWriteSound != NULL)\r
388     PicoWriteSound(length * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2));\r
389   PsndClear();\r
390 }\r
391 \r
392 // vim:shiftwidth=2:ts=2:expandtab\r