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