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