cff531af |
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 |
cc68a136 |
9 | \r |
10 | #include <string.h>\r |
cc68a136 |
11 | #include "ym2612.h"\r |
12 | #include "sn76496.h"\r |
efcba75f |
13 | #include "../pico_int.h"\r |
4f265db7 |
14 | #include "../cd/pcm.h"\r |
e71fae1f |
15 | #include "../cd/cue.h"\r |
4f265db7 |
16 | #include "mix.h"\r |
cc68a136 |
17 | \r |
9c9cda8c |
18 | #define SIMPLE_WRITE_SOUND 0\r |
19 | \r |
4a32f01f |
20 | void (*PsndMix_32_to_16l)(short *dest, int *src, int count) = mix_32_to_16l_stereo;\r |
21 | \r |
4f265db7 |
22 | // master int buffer to mix to\r |
02da059d |
23 | static int PsndBuffer[2*(44100+100)/50];\r |
cc68a136 |
24 | \r |
cc68a136 |
25 | // dac\r |
4b9c5888 |
26 | static unsigned short dac_info[312+4]; // pppppppp ppppllll, p - pos in buff, l - length to write for this sample\r |
cc68a136 |
27 | \r |
c9e1affc |
28 | // cdda output buffer\r |
29 | short cdda_out_buffer[2*1152];\r |
30 | \r |
cc68a136 |
31 | // for Pico\r |
32 | int PsndRate=0;\r |
33 | int PsndLen=0; // number of mono samples, multiply by 2 for stereo\r |
7a93adeb |
34 | int PsndLen_exc_add=0; // this is for non-integer sample counts per line, eg. 22050/60\r |
35 | int PsndLen_exc_cnt=0;\r |
4b9c5888 |
36 | int PsndDacLine=0;\r |
cc68a136 |
37 | short *PsndOut=NULL; // PCM data buffer\r |
38 | \r |
4b9c5888 |
39 | // timers\r |
48dc74f2 |
40 | int timer_a_next_oflow, timer_a_step; // in z80 cycles\r |
41 | int timer_b_next_oflow, timer_b_step;\r |
4b9c5888 |
42 | \r |
cc68a136 |
43 | // sn76496\r |
44 | extern int *sn76496_regs;\r |
45 | \r |
46 | \r |
eff55556 |
47 | static void dac_recalculate(void)\r |
cc68a136 |
48 | {\r |
49 | int i, dac_cnt, pos, len, lines = Pico.m.pal ? 312 : 262, mid = Pico.m.pal ? 68 : 93;\r |
50 | \r |
43e6eaad |
51 | if (PsndLen <= lines)\r |
52 | {\r |
cc68a136 |
53 | // shrinking algo\r |
7a93adeb |
54 | dac_cnt = -PsndLen;\r |
cc68a136 |
55 | len=1; pos=0;\r |
56 | dac_info[225] = 1;\r |
57 | \r |
43e6eaad |
58 | for(i=226; i != 225; i++)\r |
59 | {\r |
cc68a136 |
60 | if (i >= lines) i = 0;\r |
61 | len = 0;\r |
62 | if(dac_cnt < 0) {\r |
63 | len=1;\r |
64 | pos++;\r |
65 | dac_cnt += lines;\r |
66 | }\r |
67 | dac_cnt -= PsndLen;\r |
68 | dac_info[i] = (pos<<4)|len;\r |
69 | }\r |
43e6eaad |
70 | }\r |
71 | else\r |
72 | {\r |
cc68a136 |
73 | // stretching\r |
7a93adeb |
74 | dac_cnt = PsndLen;\r |
cc68a136 |
75 | pos=0;\r |
43e6eaad |
76 | for(i = 225; i != 224; i++)\r |
77 | {\r |
cc68a136 |
78 | if (i >= lines) i = 0;\r |
79 | len=0;\r |
80 | while(dac_cnt >= 0) {\r |
81 | dac_cnt -= lines;\r |
82 | len++;\r |
83 | }\r |
4f265db7 |
84 | if (i == mid) // midpoint\r |
cc68a136 |
85 | while(pos+len < PsndLen/2) {\r |
86 | dac_cnt -= lines;\r |
87 | len++;\r |
88 | }\r |
89 | dac_cnt += PsndLen;\r |
90 | dac_info[i] = (pos<<4)|len;\r |
91 | pos+=len;\r |
92 | }\r |
93 | // last sample\r |
94 | for(len = 0, i = pos; i < PsndLen; i++) len++;\r |
7a93adeb |
95 | if (PsndLen_exc_add) len++;\r |
cc68a136 |
96 | dac_info[224] = (pos<<4)|len;\r |
97 | }\r |
538a6098 |
98 | mid = (dac_info[lines-1] & 0xfff0) + ((dac_info[lines-1] & 0xf) << 4);\r |
4b9c5888 |
99 | for (i = lines; i < sizeof(dac_info) / sizeof(dac_info[0]); i++)\r |
538a6098 |
100 | dac_info[i] = mid;\r |
7a93adeb |
101 | //for(i=len=0; i < lines; i++) {\r |
102 | // printf("%03i : %03i : %i\n", i, dac_info[i]>>4, dac_info[i]&0xf);\r |
103 | // len+=dac_info[i]&0xf;\r |
104 | //}\r |
105 | //printf("rate is %i, len %f\n", PsndRate, (double)PsndRate/(Pico.m.pal ? 50.0 : 60.0));\r |
106 | //printf("len total: %i, last pos: %i\n", len, pos);\r |
107 | //exit(8);\r |
cc68a136 |
108 | }\r |
109 | \r |
110 | \r |
9d917eea |
111 | PICO_INTERNAL void PsndReset(void)\r |
cc68a136 |
112 | {\r |
af37bca8 |
113 | // PsndRerate calls YM2612Init, which also resets\r |
9d917eea |
114 | PsndRerate(0);\r |
af37bca8 |
115 | timers_reset();\r |
cc68a136 |
116 | }\r |
117 | \r |
118 | \r |
119 | // to be called after changing sound rate or chips\r |
9d917eea |
120 | void PsndRerate(int preserve_state)\r |
cc68a136 |
121 | {\r |
5f8c85be |
122 | void *state = NULL;\r |
7a93adeb |
123 | int target_fps = Pico.m.pal ? 50 : 60;\r |
cc68a136 |
124 | \r |
7a93adeb |
125 | // not all rates are supported in MCD mode due to mp3 decoder limitations\r |
602133e1 |
126 | if (PicoAHW & PAHW_MCD) {\r |
02da059d |
127 | if (!(11025-100 <= PsndRate && PsndRate <= 11025+100) &&\r |
128 | !(22050-100 <= PsndRate && PsndRate <= 22050+100) &&\r |
129 | !(44100-100 <= PsndRate && PsndRate <= 44100+100))\r |
130 | PsndRate = 22050;\r |
602133e1 |
131 | PicoOpt |= POPT_EN_STEREO; // force stereo\r |
4f265db7 |
132 | }\r |
133 | \r |
7a93adeb |
134 | if (preserve_state) {\r |
d8afe7b8 |
135 | state = malloc(0x204);\r |
5f8c85be |
136 | if (state == NULL) return;\r |
d8afe7b8 |
137 | ym2612_pack_state();\r |
138 | memcpy(state, YM2612GetRegs(), 0x204);\r |
7a93adeb |
139 | }\r |
cc68a136 |
140 | YM2612Init(Pico.m.pal ? OSC_PAL/7 : OSC_NTSC/7, PsndRate);\r |
7a93adeb |
141 | if (preserve_state) {\r |
142 | // feed it back it's own registers, just like after loading state\r |
d8afe7b8 |
143 | memcpy(YM2612GetRegs(), state, 0x204);\r |
453d2a6e |
144 | ym2612_unpack_state();\r |
c9e1affc |
145 | if ((PicoAHW & PAHW_MCD) && !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))\r |
146 | cdda_start_play();\r |
7a93adeb |
147 | }\r |
cc68a136 |
148 | \r |
7a93adeb |
149 | if (preserve_state) memcpy(state, sn76496_regs, 28*4); // remember old state\r |
cc68a136 |
150 | SN76496_init(Pico.m.pal ? OSC_PAL/15 : OSC_NTSC/15, PsndRate);\r |
7a93adeb |
151 | if (preserve_state) memcpy(sn76496_regs, state, 28*4); // restore old state\r |
cc68a136 |
152 | \r |
5f8c85be |
153 | if (state)\r |
154 | free(state);\r |
155 | \r |
cc68a136 |
156 | // calculate PsndLen\r |
7a93adeb |
157 | PsndLen=PsndRate / target_fps;\r |
158 | PsndLen_exc_add=((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r |
159 | PsndLen_exc_cnt=0;\r |
cc68a136 |
160 | \r |
161 | // recalculate dac info\r |
162 | dac_recalculate();\r |
4f265db7 |
163 | \r |
602133e1 |
164 | if (PicoAHW & PAHW_MCD)\r |
4f265db7 |
165 | pcm_set_rate(PsndRate);\r |
7a93adeb |
166 | \r |
167 | // clear all buffers\r |
168 | memset32(PsndBuffer, 0, sizeof(PsndBuffer)/4);\r |
c9e1affc |
169 | memset(cdda_out_buffer, 0, sizeof(cdda_out_buffer));\r |
7a93adeb |
170 | if (PsndOut)\r |
9d917eea |
171 | PsndClear();\r |
4a32f01f |
172 | \r |
173 | // set mixer\r |
602133e1 |
174 | PsndMix_32_to_16l = (PicoOpt & POPT_EN_STEREO) ? mix_32_to_16l_stereo : mix_32_to_16_mono;\r |
ed367a3f |
175 | \r |
176 | if (PicoAHW & PAHW_PICO)\r |
177 | PicoReratePico();\r |
cc68a136 |
178 | }\r |
179 | \r |
180 | \r |
4b9c5888 |
181 | PICO_INTERNAL void PsndDoDAC(int line_to)\r |
cc68a136 |
182 | {\r |
4b9c5888 |
183 | int pos, pos1, len;\r |
184 | int dout = ym2612.dacout;\r |
185 | int line_from = PsndDacLine;\r |
4f265db7 |
186 | \r |
9b5713af |
187 | if (line_to >= 312)\r |
188 | line_to = 311;\r |
189 | \r |
4b9c5888 |
190 | PsndDacLine = line_to + 1;\r |
4f265db7 |
191 | \r |
4b9c5888 |
192 | pos =dac_info[line_from]>>4;\r |
193 | pos1=dac_info[line_to];\r |
194 | len = ((pos1>>4)-pos) + (pos1&0xf);\r |
4f265db7 |
195 | if (!len) return;\r |
196 | \r |
4b9c5888 |
197 | if (PicoOpt & POPT_EN_STEREO) {\r |
4f265db7 |
198 | short *d = PsndOut + pos*2;\r |
4b9c5888 |
199 | for (; len > 0; len--, d+=2) *d = dout;\r |
200 | } else {\r |
201 | short *d = PsndOut + pos;\r |
202 | for (; len > 0; len--, d++) *d = dout;\r |
cc68a136 |
203 | }\r |
204 | \r |
4f265db7 |
205 | #if 0\r |
206 | if (do_pcm) {\r |
207 | int *d = PsndBuffer;\r |
208 | d += (PicoOpt&8) ? pos*2 : pos;\r |
209 | pcm_update(d, len, 1);\r |
210 | }\r |
211 | #endif\r |
212 | }\r |
cc68a136 |
213 | \r |
c9e1affc |
214 | // cdda\r |
215 | static pm_file *cdda_stream = NULL;\r |
216 | \r |
217 | static void cdda_raw_update(int *buffer, int length)\r |
218 | {\r |
02da059d |
219 | int ret, cdda_bytes, mult = 1;\r |
220 | if (cdda_stream == NULL)\r |
221 | return;\r |
c9e1affc |
222 | \r |
223 | cdda_bytes = length*4;\r |
02da059d |
224 | if (PsndRate <= 22050 + 100) mult = 2;\r |
225 | if (PsndRate < 22050 - 100) mult = 4;\r |
226 | cdda_bytes *= mult;\r |
c9e1affc |
227 | \r |
228 | ret = pm_read(cdda_out_buffer, cdda_bytes, cdda_stream);\r |
229 | if (ret < cdda_bytes) {\r |
230 | memset((char *)cdda_out_buffer + ret, 0, cdda_bytes - ret);\r |
231 | cdda_stream = NULL;\r |
232 | return;\r |
233 | }\r |
234 | \r |
235 | // now mix\r |
02da059d |
236 | switch (mult) {\r |
237 | case 1: mix_16h_to_32(buffer, cdda_out_buffer, length*2); break;\r |
238 | case 2: mix_16h_to_32_s1(buffer, cdda_out_buffer, length*2); break;\r |
239 | case 4: mix_16h_to_32_s2(buffer, cdda_out_buffer, length*2); break;\r |
c9e1affc |
240 | }\r |
241 | }\r |
242 | \r |
243 | PICO_INTERNAL void cdda_start_play(void)\r |
244 | {\r |
245 | int lba_offset, index, lba_length, i;\r |
246 | \r |
247 | elprintf(EL_STATUS, "cdda play track #%i", Pico_mcd->scd.Cur_Track);\r |
248 | \r |
249 | index = Pico_mcd->scd.Cur_Track - 1;\r |
250 | \r |
251 | lba_offset = Pico_mcd->scd.Cur_LBA - Track_to_LBA(index + 1);\r |
252 | if (lba_offset < 0) lba_offset = 0;\r |
253 | lba_offset += Pico_mcd->TOC.Tracks[index].Offset;\r |
254 | \r |
255 | // find the actual file for this track\r |
98da48e4 |
256 | for (i = index; i > 0; i--)\r |
c9e1affc |
257 | if (Pico_mcd->TOC.Tracks[i].F != NULL) break;\r |
258 | \r |
259 | if (Pico_mcd->TOC.Tracks[i].F == NULL) {\r |
260 | elprintf(EL_STATUS|EL_ANOMALY, "no track?!");\r |
261 | return;\r |
262 | }\r |
263 | \r |
e71fae1f |
264 | if (Pico_mcd->TOC.Tracks[i].ftype == CT_MP3)\r |
c9e1affc |
265 | {\r |
266 | int pos1024 = 0;\r |
267 | \r |
268 | lba_length = Pico_mcd->TOC.Tracks[i].Length;\r |
269 | for (i++; i < Pico_mcd->TOC.Last_Track; i++) {\r |
270 | if (Pico_mcd->TOC.Tracks[i].F != NULL) break;\r |
271 | lba_length += Pico_mcd->TOC.Tracks[i].Length;\r |
272 | }\r |
273 | \r |
274 | if (lba_offset)\r |
275 | pos1024 = lba_offset * 1024 / lba_length;\r |
276 | \r |
277 | mp3_start_play(Pico_mcd->TOC.Tracks[index].F, pos1024);\r |
278 | return;\r |
279 | }\r |
280 | \r |
281 | cdda_stream = Pico_mcd->TOC.Tracks[i].F;\r |
282 | PicoCDBufferFlush(); // buffering relies on fp not being touched\r |
283 | pm_seek(cdda_stream, lba_offset * 2352, SEEK_SET);\r |
e71fae1f |
284 | if (Pico_mcd->TOC.Tracks[i].ftype == CT_WAV)\r |
0bccafeb |
285 | {\r |
286 | // skip headers, assume it's 44kHz stereo uncompressed\r |
287 | pm_seek(cdda_stream, 44, SEEK_CUR);\r |
288 | }\r |
c9e1affc |
289 | }\r |
290 | \r |
cc68a136 |
291 | \r |
9d917eea |
292 | PICO_INTERNAL void PsndClear(void)\r |
4f265db7 |
293 | {\r |
7a93adeb |
294 | int len = PsndLen;\r |
295 | if (PsndLen_exc_add) len++;\r |
602133e1 |
296 | if (PicoOpt & POPT_EN_STEREO)\r |
88b3d7c1 |
297 | memset32((int *) PsndOut, 0, len); // assume PsndOut to be aligned\r |
298 | else {\r |
299 | short *out = PsndOut;\r |
b8a1c09a |
300 | if ((long)out & 2) { *out++ = 0; len--; }\r |
88b3d7c1 |
301 | memset32((int *) out, 0, len/2);\r |
302 | if (len & 1) out[len-1] = 0;\r |
303 | }\r |
cc68a136 |
304 | }\r |
305 | \r |
306 | \r |
7b3f44c6 |
307 | static int PsndRender(int offset, int length)\r |
cc68a136 |
308 | {\r |
85f8e929 |
309 | int buf32_updated = 0;\r |
4f265db7 |
310 | int *buf32 = PsndBuffer+offset;\r |
cc68a136 |
311 | int stereo = (PicoOpt & 8) >> 3;\r |
4f265db7 |
312 | // emulating CD && PCM option enabled && PCM chip on && have enabled channels\r |
602133e1 |
313 | int do_pcm = (PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_PCM) &&\r |
314 | (Pico_mcd->pcm.control & 0x80) && Pico_mcd->pcm.enabled;\r |
cc68a136 |
315 | offset <<= stereo;\r |
316 | \r |
f6c49d38 |
317 | pprof_start(sound);\r |
318 | \r |
03a265e5 |
319 | #if !SIMPLE_WRITE_SOUND\r |
7a93adeb |
320 | if (offset == 0) { // should happen once per frame\r |
321 | // compensate for float part of PsndLen\r |
322 | PsndLen_exc_cnt += PsndLen_exc_add;\r |
323 | if (PsndLen_exc_cnt >= 0x10000) {\r |
324 | PsndLen_exc_cnt -= 0x10000;\r |
325 | length++;\r |
326 | }\r |
327 | }\r |
03a265e5 |
328 | #endif\r |
7a93adeb |
329 | \r |
cc68a136 |
330 | // PSG\r |
602133e1 |
331 | if (PicoOpt & POPT_EN_PSG)\r |
cc68a136 |
332 | SN76496Update(PsndOut+offset, length, stereo);\r |
333 | \r |
ef4eb506 |
334 | if (PicoAHW & PAHW_PICO) {\r |
335 | PicoPicoPCMUpdate(PsndOut+offset, length, stereo);\r |
336 | return length;\r |
337 | }\r |
338 | \r |
cc68a136 |
339 | // Add in the stereo FM buffer\r |
602133e1 |
340 | if (PicoOpt & POPT_EN_FM) {\r |
85f8e929 |
341 | buf32_updated = YM2612UpdateOne(buf32, length, stereo, 1);\r |
3ec29f01 |
342 | } else\r |
fa1e5e29 |
343 | memset32(buf32, 0, length<<stereo);\r |
85f8e929 |
344 | \r |
345 | //printf("active_chs: %02x\n", buf32_updated);\r |
e743be20 |
346 | (void)buf32_updated;\r |
4f265db7 |
347 | \r |
7a93adeb |
348 | // CD: PCM sound\r |
85f8e929 |
349 | if (do_pcm) {\r |
4f265db7 |
350 | pcm_update(buf32, length, stereo);\r |
85f8e929 |
351 | //buf32_updated = 1;\r |
352 | }\r |
4f265db7 |
353 | \r |
7a93adeb |
354 | // CD: CDDA audio\r |
da42200b |
355 | // CD mode, cdda enabled, not data track, CDC is reading\r |
602133e1 |
356 | if ((PicoAHW & PAHW_MCD) && (PicoOpt & POPT_EN_MCD_CDDA) &&\r |
357 | !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))\r |
c9e1affc |
358 | {\r |
359 | // note: only 44, 22 and 11 kHz supported, with forced stereo\r |
360 | int index = Pico_mcd->scd.Cur_Track - 1;\r |
361 | \r |
e71fae1f |
362 | if (Pico_mcd->TOC.Tracks[index].ftype == CT_MP3)\r |
c9e1affc |
363 | mp3_update(buf32, length, stereo);\r |
364 | else\r |
365 | cdda_raw_update(buf32, length);\r |
366 | }\r |
4f265db7 |
367 | \r |
db1d3564 |
368 | if ((PicoAHW & PAHW_32X) && (PicoOpt & POPT_EN_PWM))\r |
369 | p32x_pwm_update(buf32, length, stereo);\r |
370 | \r |
4f265db7 |
371 | // convert + limit to normal 16bit output\r |
4a32f01f |
372 | PsndMix_32_to_16l(PsndOut+offset, buf32, length);\r |
cc68a136 |
373 | \r |
f6c49d38 |
374 | pprof_end(sound);\r |
375 | \r |
7a93adeb |
376 | return length;\r |
cc68a136 |
377 | }\r |
378 | \r |
7b3f44c6 |
379 | // to be called on 224 or line_sample scanlines only\r |
380 | PICO_INTERNAL void PsndGetSamples(int y)\r |
381 | {\r |
382 | #if SIMPLE_WRITE_SOUND\r |
383 | if (y != 224) return;\r |
384 | PsndRender(0, PsndLen);\r |
a4edca53 |
385 | if (PicoWriteSound)\r |
386 | PicoWriteSound(PsndLen * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2));\r |
7b3f44c6 |
387 | PsndClear();\r |
388 | #else\r |
389 | static int curr_pos = 0;\r |
390 | \r |
391 | if (y == 224)\r |
392 | {\r |
393 | if (emustatus & 2)\r |
394 | curr_pos += PsndRender(curr_pos, PsndLen-PsndLen/2);\r |
395 | else curr_pos = PsndRender(0, PsndLen);\r |
a4edca53 |
396 | if (emustatus & 1)\r |
397 | emustatus |= 2;\r |
398 | else emustatus &= ~2;\r |
399 | if (PicoWriteSound)\r |
400 | PicoWriteSound(curr_pos * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2));\r |
7b3f44c6 |
401 | // clear sound buffer\r |
402 | PsndClear();\r |
403 | }\r |
404 | else if (emustatus & 3) {\r |
405 | emustatus|= 2;\r |
406 | emustatus&=~1;\r |
407 | curr_pos = PsndRender(0, PsndLen/2);\r |
408 | }\r |
409 | #endif\r |
410 | }\r |
411 | \r |
2ec9bec5 |
412 | PICO_INTERNAL void PsndGetSamplesMS(void)\r |
413 | {\r |
2ec9bec5 |
414 | int stereo = (PicoOpt & 8) >> 3;\r |
415 | int length = PsndLen;\r |
416 | \r |
417 | #if !SIMPLE_WRITE_SOUND\r |
418 | // compensate for float part of PsndLen\r |
419 | PsndLen_exc_cnt += PsndLen_exc_add;\r |
420 | if (PsndLen_exc_cnt >= 0x10000) {\r |
421 | PsndLen_exc_cnt -= 0x10000;\r |
422 | length++;\r |
423 | }\r |
424 | #endif\r |
425 | \r |
426 | // PSG\r |
427 | if (PicoOpt & POPT_EN_PSG)\r |
428 | SN76496Update(PsndOut, length, stereo);\r |
429 | \r |
19954be1 |
430 | // upmix to "stereo" if needed\r |
431 | if (stereo) {\r |
8a19f430 |
432 | int i, *p;\r |
433 | for (i = length, p = (void *)PsndOut; i > 0; i--, p++)\r |
19954be1 |
434 | *p |= *p << 16;\r |
435 | }\r |
2ec9bec5 |
436 | \r |
437 | if (PicoWriteSound != NULL)\r |
a4edca53 |
438 | PicoWriteSound(length * ((PicoOpt & POPT_EN_STEREO) ? 4 : 2));\r |
2ec9bec5 |
439 | PsndClear();\r |
440 | }\r |
441 | \r |