Commit | Line | Data |
---|---|---|
ef79bbde P |
1 | /*************************************************************************** |
2 | spu.c - description | |
3 | ------------------- | |
4 | begin : Wed May 15 2002 | |
5 | copyright : (C) 2002 by Pete Bernert | |
6 | email : BlackDove@addcom.de | |
07a6dd2c | 7 | |
63a4f6b6 | 8 | Portions (C) GraÅžvydas "notaz" Ignotas, 2010-2012,2014,2015 |
07a6dd2c | 9 | |
ef79bbde P |
10 | ***************************************************************************/ |
11 | /*************************************************************************** | |
12 | * * | |
13 | * This program is free software; you can redistribute it and/or modify * | |
14 | * it under the terms of the GNU General Public License as published by * | |
15 | * the Free Software Foundation; either version 2 of the License, or * | |
16 | * (at your option) any later version. See also the license.txt file for * | |
17 | * additional informations. * | |
18 | * * | |
19 | ***************************************************************************/ | |
20 | ||
21 | #include "stdafx.h" | |
22 | ||
23 | #define _IN_SPU | |
24 | ||
25 | #include "externals.h" | |
3fc2a4c2 | 26 | #include "registers.h" |
07c13dfd | 27 | #include "out.h" |
3154bfab | 28 | #include "spu_config.h" |
ef79bbde | 29 | |
de4a0279 | 30 | #ifdef __arm__ |
31 | #include "arm_features.h" | |
32 | #endif | |
33 | ||
8f2bb0cb | 34 | #ifdef HAVE_ARMV7 |
1775933a | 35 | #define ssat32_to_16(v) \ |
36 | asm("ssat %0,#16,%1" : "=r" (v) : "r" (v)) | |
37 | #else | |
38 | #define ssat32_to_16(v) do { \ | |
39 | if (v < -32768) v = -32768; \ | |
40 | else if (v > 32767) v = 32767; \ | |
41 | } while (0) | |
42 | #endif | |
43 | ||
fb552464 | 44 | #define PSXCLK 33868800 /* 33.8688 MHz */ |
45 | ||
650adfd2 | 46 | // intended to be ~1 frame |
47 | #define IRQ_NEAR_BLOCKS 32 | |
48 | ||
6d866bb7 | 49 | /* |
ef79bbde P |
50 | #if defined (USEMACOSX) |
51 | static char * libraryName = N_("Mac OS X Sound"); | |
52 | #elif defined (USEALSA) | |
53 | static char * libraryName = N_("ALSA Sound"); | |
54 | #elif defined (USEOSS) | |
55 | static char * libraryName = N_("OSS Sound"); | |
56 | #elif defined (USESDL) | |
57 | static char * libraryName = N_("SDL Sound"); | |
58 | #elif defined (USEPULSEAUDIO) | |
59 | static char * libraryName = N_("PulseAudio Sound"); | |
60 | #else | |
61 | static char * libraryName = N_("NULL Sound"); | |
62 | #endif | |
63 | ||
64 | static char * libraryInfo = N_("P.E.Op.S. Sound Driver V1.7\nCoded by Pete Bernert and the P.E.Op.S. team\n"); | |
6d866bb7 | 65 | */ |
ef79bbde P |
66 | |
67 | // globals | |
68 | ||
3154bfab | 69 | SPUInfo spu; |
70 | SPUConfig spu_config; | |
ef79bbde | 71 | |
5514a050 | 72 | static int iFMod[NSSIZE]; |
05c7cec7 | 73 | static int RVB[NSSIZE * 2]; |
3154bfab | 74 | int ChanBuf[NSSIZE]; |
ef79bbde | 75 | |
983a7cfd | 76 | #define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2 |
77 | ||
ef79bbde P |
78 | //////////////////////////////////////////////////////////////////////// |
79 | // CODE AREA | |
80 | //////////////////////////////////////////////////////////////////////// | |
81 | ||
82 | // dirty inline func includes | |
83 | ||
84 | #include "reverb.c" | |
85 | #include "adsr.c" | |
86 | ||
87 | //////////////////////////////////////////////////////////////////////// | |
88 | // helpers for simple interpolation | |
89 | ||
90 | // | |
91 | // easy interpolation on upsampling, no special filter, just "Pete's common sense" tm | |
92 | // | |
93 | // instead of having n equal sample values in a row like: | |
94 | // ____ | |
95 | // |____ | |
96 | // | |
97 | // we compare the current delta change with the next delta change. | |
98 | // | |
99 | // if curr_delta is positive, | |
100 | // | |
101 | // - and next delta is smaller (or changing direction): | |
102 | // \. | |
103 | // -__ | |
104 | // | |
105 | // - and next delta significant (at least twice) bigger: | |
106 | // --_ | |
107 | // \. | |
108 | // | |
109 | // - and next delta is nearly same: | |
110 | // \. | |
111 | // \. | |
112 | // | |
113 | // | |
114 | // if curr_delta is negative, | |
115 | // | |
116 | // - and next delta is smaller (or changing direction): | |
117 | // _-- | |
118 | // / | |
119 | // | |
120 | // - and next delta significant (at least twice) bigger: | |
121 | // / | |
122 | // __- | |
123 | // | |
124 | // - and next delta is nearly same: | |
125 | // / | |
126 | // / | |
127 | // | |
128 | ||
011755d7 | 129 | static void InterpolateUp(int *SB, int sinc) |
ef79bbde | 130 | { |
011755d7 | 131 | if(SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass |
ef79bbde | 132 | { |
011755d7 | 133 | const int id1=SB[30]-SB[29]; // curr delta to next val |
134 | const int id2=SB[31]-SB[30]; // and next delta to next-next val :) | |
ef79bbde | 135 | |
011755d7 | 136 | SB[32]=0; |
ef79bbde P |
137 | |
138 | if(id1>0) // curr delta positive | |
139 | { | |
140 | if(id2<id1) | |
011755d7 | 141 | {SB[28]=id1;SB[32]=2;} |
ef79bbde P |
142 | else |
143 | if(id2<(id1<<1)) | |
011755d7 | 144 | SB[28]=(id1*sinc)>>16; |
ef79bbde | 145 | else |
011755d7 | 146 | SB[28]=(id1*sinc)>>17; |
ef79bbde P |
147 | } |
148 | else // curr delta negative | |
149 | { | |
150 | if(id2>id1) | |
011755d7 | 151 | {SB[28]=id1;SB[32]=2;} |
ef79bbde P |
152 | else |
153 | if(id2>(id1<<1)) | |
011755d7 | 154 | SB[28]=(id1*sinc)>>16; |
ef79bbde | 155 | else |
011755d7 | 156 | SB[28]=(id1*sinc)>>17; |
ef79bbde P |
157 | } |
158 | } | |
159 | else | |
011755d7 | 160 | if(SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass |
ef79bbde | 161 | { |
011755d7 | 162 | SB[32]=0; |
ef79bbde | 163 | |
011755d7 | 164 | SB[28]=(SB[28]*sinc)>>17; |
165 | //if(sinc<=0x8000) | |
166 | // SB[29]=SB[30]-(SB[28]*((0x10000/sinc)-1)); | |
76d1d09c | 167 | //else |
011755d7 | 168 | SB[29]+=SB[28]; |
ef79bbde P |
169 | } |
170 | else // no flags? add bigger val (if possible), calc smaller step, set flag1 | |
011755d7 | 171 | SB[29]+=SB[28]; |
ef79bbde P |
172 | } |
173 | ||
174 | // | |
175 | // even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm | |
176 | // | |
177 | ||
011755d7 | 178 | static void InterpolateDown(int *SB, int sinc) |
ef79bbde | 179 | { |
011755d7 | 180 | if(sinc>=0x20000L) // we would skip at least one val? |
ef79bbde | 181 | { |
011755d7 | 182 | SB[29]+=(SB[30]-SB[29])/2; // add easy weight |
183 | if(sinc>=0x30000L) // we would skip even more vals? | |
184 | SB[29]+=(SB[31]-SB[30])/2; // add additional next weight | |
ef79bbde P |
185 | } |
186 | } | |
187 | ||
188 | //////////////////////////////////////////////////////////////////////// | |
189 | // helpers for gauss interpolation | |
190 | ||
011755d7 | 191 | #define gval0 (((short*)(&SB[29]))[gpos&3]) |
192 | #define gval(x) ((int)((short*)(&SB[29]))[(gpos+x)&3]) | |
ef79bbde P |
193 | |
194 | #include "gauss_i.h" | |
195 | ||
196 | //////////////////////////////////////////////////////////////////////// | |
197 | ||
198 | #include "xa.c" | |
199 | ||
e4f075af | 200 | static void do_irq(void) |
201 | { | |
3154bfab | 202 | //if(!(spu.spuStat & STAT_IRQ)) |
e4f075af | 203 | { |
3154bfab | 204 | spu.spuStat |= STAT_IRQ; // asserted status? |
205 | if(spu.irqCallback) spu.irqCallback(); | |
e4f075af | 206 | } |
207 | } | |
208 | ||
209 | static int check_irq(int ch, unsigned char *pos) | |
210 | { | |
54604e03 | 211 | if((spu.spuCtrl & (CTRL_ON|CTRL_IRQ)) == (CTRL_ON|CTRL_IRQ) && pos == spu.pSpuIrq) |
e4f075af | 212 | { |
3154bfab | 213 | //printf("ch%d irq %04x\n", ch, pos - spu.spuMemC); |
e4f075af | 214 | do_irq(); |
215 | return 1; | |
216 | } | |
217 | return 0; | |
218 | } | |
219 | ||
54604e03 | 220 | void check_irq_io(unsigned int addr) |
221 | { | |
222 | unsigned int irq_addr = regAreaGet(H_SPUirqAddr) << 3; | |
223 | //addr &= ~7; // ? | |
224 | if((spu.spuCtrl & (CTRL_ON|CTRL_IRQ)) == (CTRL_ON|CTRL_IRQ) && addr == irq_addr) | |
225 | { | |
226 | //printf("io irq %04x\n", irq_addr); | |
227 | do_irq(); | |
228 | } | |
229 | } | |
230 | ||
ef79bbde P |
231 | //////////////////////////////////////////////////////////////////////// |
232 | // START SOUND... called by main thread to setup a new sound on a channel | |
233 | //////////////////////////////////////////////////////////////////////// | |
234 | ||
3bd31caf | 235 | static void StartSoundSB(int *SB) |
ef79bbde | 236 | { |
de4a0279 | 237 | SB[26]=0; // init mixing vars |
238 | SB[27]=0; | |
ef79bbde | 239 | |
de4a0279 | 240 | SB[28]=0; |
241 | SB[29]=0; // init our interpolation helpers | |
242 | SB[30]=0; | |
243 | SB[31]=0; | |
3bd31caf | 244 | } |
245 | ||
246 | static void StartSoundMain(int ch) | |
247 | { | |
248 | SPUCHAN *s_chan = &spu.s_chan[ch]; | |
249 | ||
250 | StartADSR(ch); | |
251 | StartREVERB(ch); | |
252 | ||
6183ddf9 | 253 | s_chan->prevflags = 2; |
254 | s_chan->iSBPos = 27; | |
255 | s_chan->spos = 0; | |
256 | s_chan->bStarting = 1; | |
ef79bbde | 257 | |
a0d4a8f5 | 258 | s_chan->pCurr = spu.spuMemC + ((regAreaGetCh(ch, 6) & ~1) << 3); |
9601ca6d | 259 | |
3154bfab | 260 | spu.dwNewChannel&=~(1<<ch); // clear new channel bit |
4ccd0fb2 | 261 | spu.dwChannelDead&=~(1<<ch); |
df68316d | 262 | spu.dwChannelsAudible|=1<<ch; |
ef79bbde P |
263 | } |
264 | ||
3bd31caf | 265 | static void StartSound(int ch) |
266 | { | |
267 | StartSoundMain(ch); | |
268 | StartSoundSB(spu.SB + ch * SB_SIZE); | |
269 | } | |
270 | ||
ef79bbde P |
271 | //////////////////////////////////////////////////////////////////////// |
272 | // ALL KIND OF HELPERS | |
273 | //////////////////////////////////////////////////////////////////////// | |
274 | ||
011755d7 | 275 | INLINE int FModChangeFrequency(int *SB, int pitch, int ns) |
ef79bbde | 276 | { |
f63561cb | 277 | pitch = (signed short)pitch; |
278 | pitch = ((32768 + iFMod[ns]) * pitch) >> 15; | |
279 | pitch &= 0xffff; | |
280 | if (pitch > 0x3fff) | |
281 | pitch = 0x3fff; | |
ef79bbde | 282 | |
f63561cb | 283 | iFMod[ns] = 0; |
284 | SB[32] = 1; // reset interpolation | |
ef79bbde | 285 | |
f63561cb | 286 | return pitch << 4; |
ef79bbde P |
287 | } |
288 | ||
289 | //////////////////////////////////////////////////////////////////////// | |
290 | ||
011755d7 | 291 | INLINE void StoreInterpolationVal(int *SB, int sinc, int fa, int fmod_freq) |
ef79bbde | 292 | { |
011755d7 | 293 | if(fmod_freq) // fmod freq channel |
294 | SB[29]=fa; | |
ef79bbde P |
295 | else |
296 | { | |
381ea103 | 297 | ssat32_to_16(fa); |
ef79bbde | 298 | |
3154bfab | 299 | if(spu_config.iUseInterpolation>=2) // gauss/cubic interpolation |
011755d7 | 300 | { |
301 | int gpos = SB[28]; | |
302 | gval0 = fa; | |
ef79bbde | 303 | gpos = (gpos+1) & 3; |
011755d7 | 304 | SB[28] = gpos; |
ef79bbde P |
305 | } |
306 | else | |
3154bfab | 307 | if(spu_config.iUseInterpolation==1) // simple interpolation |
ef79bbde | 308 | { |
011755d7 | 309 | SB[28] = 0; |
310 | SB[29] = SB[30]; // -> helpers for simple linear interpolation: delay real val for two slots, and calc the two deltas, for a 'look at the future behaviour' | |
311 | SB[30] = SB[31]; | |
312 | SB[31] = fa; | |
313 | SB[32] = 1; // -> flag: calc new interolation | |
ef79bbde | 314 | } |
011755d7 | 315 | else SB[29]=fa; // no interpolation |
ef79bbde P |
316 | } |
317 | } | |
318 | ||
319 | //////////////////////////////////////////////////////////////////////// | |
320 | ||
011755d7 | 321 | INLINE int iGetInterpolationVal(int *SB, int sinc, int spos, int fmod_freq) |
ef79bbde P |
322 | { |
323 | int fa; | |
324 | ||
011755d7 | 325 | if(fmod_freq) return SB[29]; |
ef79bbde | 326 | |
3154bfab | 327 | switch(spu_config.iUseInterpolation) |
011755d7 | 328 | { |
ef79bbde P |
329 | //--------------------------------------------------// |
330 | case 3: // cubic interpolation | |
331 | { | |
332 | long xd;int gpos; | |
8cd23d6d | 333 | xd = (spos >> 1)+1; |
011755d7 | 334 | gpos = SB[28]; |
ef79bbde P |
335 | |
336 | fa = gval(3) - 3*gval(2) + 3*gval(1) - gval0; | |
337 | fa *= (xd - (2<<15)) / 6; | |
338 | fa >>= 15; | |
339 | fa += gval(2) - gval(1) - gval(1) + gval0; | |
340 | fa *= (xd - (1<<15)) >> 1; | |
341 | fa >>= 15; | |
342 | fa += gval(1) - gval0; | |
343 | fa *= xd; | |
344 | fa >>= 15; | |
345 | fa = fa + gval0; | |
346 | ||
347 | } break; | |
348 | //--------------------------------------------------// | |
349 | case 2: // gauss interpolation | |
350 | { | |
351 | int vl, vr;int gpos; | |
8cd23d6d | 352 | vl = (spos >> 6) & ~3; |
011755d7 | 353 | gpos = SB[28]; |
63646208 S |
354 | vr=(gauss[vl]*(int)gval0) >> 15; |
355 | vr+=(gauss[vl+1]*gval(1)) >> 15; | |
356 | vr+=(gauss[vl+2]*gval(2)) >> 15; | |
357 | vr+=(gauss[vl+3]*gval(3)) >> 15; | |
358 | fa = vr; | |
ef79bbde P |
359 | } break; |
360 | //--------------------------------------------------// | |
361 | case 1: // simple interpolation | |
362 | { | |
011755d7 | 363 | if(sinc<0x10000L) // -> upsampling? |
364 | InterpolateUp(SB, sinc); // --> interpolate up | |
365 | else InterpolateDown(SB, sinc); // --> else down | |
366 | fa=SB[29]; | |
ef79bbde P |
367 | } break; |
368 | //--------------------------------------------------// | |
369 | default: // no interpolation | |
370 | { | |
011755d7 | 371 | fa=SB[29]; |
ef79bbde P |
372 | } break; |
373 | //--------------------------------------------------// | |
374 | } | |
375 | ||
376 | return fa; | |
377 | } | |
378 | ||
381ea103 | 379 | static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor) |
380 | { | |
5514a050 | 381 | static const int f[16][2] = { |
382 | { 0, 0 }, | |
383 | { 60, 0 }, | |
384 | { 115, -52 }, | |
385 | { 98, -55 }, | |
386 | { 122, -60 } | |
387 | }; | |
381ea103 | 388 | int nSample; |
389 | int fa, s_1, s_2, d, s; | |
390 | ||
391 | s_1 = dest[27]; | |
392 | s_2 = dest[26]; | |
393 | ||
394 | for (nSample = 0; nSample < 28; src++) | |
395 | { | |
396 | d = (int)*src; | |
397 | s = (int)(signed short)((d & 0x0f) << 12); | |
398 | ||
f63561cb | 399 | fa = s >> shift_factor; |
381ea103 | 400 | fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6); |
f63561cb | 401 | ssat32_to_16(fa); |
402 | s_2 = s_1; s_1 = fa; | |
381ea103 | 403 | |
404 | dest[nSample++] = fa; | |
405 | ||
406 | s = (int)(signed short)((d & 0xf0) << 8); | |
f63561cb | 407 | fa = s >> shift_factor; |
381ea103 | 408 | fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6); |
f63561cb | 409 | ssat32_to_16(fa); |
410 | s_2 = s_1; s_1 = fa; | |
381ea103 | 411 | |
412 | dest[nSample++] = fa; | |
413 | } | |
414 | } | |
415 | ||
3bd31caf | 416 | static int decode_block(void *unused, int ch, int *SB) |
e11ae5c5 | 417 | { |
5514a050 | 418 | SPUCHAN *s_chan = &spu.s_chan[ch]; |
e11ae5c5 | 419 | unsigned char *start; |
215ff9e6 | 420 | int predict_nr, shift_factor, flags; |
e11ae5c5 | 421 | int ret = 0; |
422 | ||
5514a050 | 423 | start = s_chan->pCurr; // set up the current pos |
6183ddf9 | 424 | if (start - spu.spuMemC < 0x1000) { // ? |
425 | //log_unhandled("ch%02d plays decode bufs @%05lx\n", | |
426 | // ch, (long)(start - spu.spuMemC)); | |
215ff9e6 | 427 | ret = 1; |
6183ddf9 | 428 | } |
e11ae5c5 | 429 | |
5514a050 | 430 | if (s_chan->prevflags & 1) // 1: stop/loop |
3fc2a4c2 | 431 | { |
5514a050 | 432 | if (!(s_chan->prevflags & 2)) |
215ff9e6 | 433 | ret = 1; |
e4f075af | 434 | |
5514a050 | 435 | start = s_chan->pLoop; |
3fc2a4c2 | 436 | } |
4622c0a1 | 437 | |
438 | check_irq(ch, start); | |
e11ae5c5 | 439 | |
5514a050 | 440 | predict_nr = start[0]; |
215ff9e6 | 441 | shift_factor = predict_nr & 0xf; |
e11ae5c5 | 442 | predict_nr >>= 4; |
e11ae5c5 | 443 | |
011755d7 | 444 | decode_block_data(SB, start + 2, predict_nr, shift_factor); |
e11ae5c5 | 445 | |
215ff9e6 | 446 | flags = start[1]; |
4622c0a1 | 447 | if (flags & 4 && !s_chan->bIgnoreLoop) |
5514a050 | 448 | s_chan->pLoop = start; // loop adress |
e11ae5c5 | 449 | |
215ff9e6 | 450 | start += 16; |
3fc2a4c2 | 451 | |
5514a050 | 452 | s_chan->pCurr = start; // store values for next cycle |
453 | s_chan->prevflags = flags; | |
6183ddf9 | 454 | s_chan->bStarting = 0; |
e11ae5c5 | 455 | |
456 | return ret; | |
457 | } | |
458 | ||
07a6dd2c | 459 | // do block, but ignore sample data |
460 | static int skip_block(int ch) | |
461 | { | |
5514a050 | 462 | SPUCHAN *s_chan = &spu.s_chan[ch]; |
463 | unsigned char *start = s_chan->pCurr; | |
215ff9e6 | 464 | int flags; |
465 | int ret = 0; | |
466 | ||
5514a050 | 467 | if (s_chan->prevflags & 1) { |
468 | if (!(s_chan->prevflags & 2)) | |
215ff9e6 | 469 | ret = 1; |
07a6dd2c | 470 | |
5514a050 | 471 | start = s_chan->pLoop; |
215ff9e6 | 472 | } |
4622c0a1 | 473 | |
474 | check_irq(ch, start); | |
07a6dd2c | 475 | |
215ff9e6 | 476 | flags = start[1]; |
4622c0a1 | 477 | if (flags & 4 && !s_chan->bIgnoreLoop) |
5514a050 | 478 | s_chan->pLoop = start; |
07a6dd2c | 479 | |
e4f075af | 480 | start += 16; |
07a6dd2c | 481 | |
5514a050 | 482 | s_chan->pCurr = start; |
483 | s_chan->prevflags = flags; | |
6183ddf9 | 484 | s_chan->bStarting = 0; |
e4f075af | 485 | |
89cb2058 | 486 | return ret; |
07a6dd2c | 487 | } |
488 | ||
650adfd2 | 489 | // if irq is going to trigger sooner than in upd_samples, set upd_samples |
490 | static void scan_for_irq(int ch, unsigned int *upd_samples) | |
491 | { | |
5514a050 | 492 | SPUCHAN *s_chan = &spu.s_chan[ch]; |
650adfd2 | 493 | int pos, sinc, sinc_inv, end; |
494 | unsigned char *block; | |
495 | int flags; | |
496 | ||
5514a050 | 497 | block = s_chan->pCurr; |
498 | pos = s_chan->spos; | |
499 | sinc = s_chan->sinc; | |
650adfd2 | 500 | end = pos + *upd_samples * sinc; |
b49db14f | 501 | if (s_chan->prevflags & 1) // 1: stop/loop |
502 | block = s_chan->pLoop; | |
650adfd2 | 503 | |
5514a050 | 504 | pos += (28 - s_chan->iSBPos) << 16; |
650adfd2 | 505 | while (pos < end) |
506 | { | |
3154bfab | 507 | if (block == spu.pSpuIrq) |
650adfd2 | 508 | break; |
509 | flags = block[1]; | |
510 | block += 16; | |
511 | if (flags & 1) { // 1: stop/loop | |
5514a050 | 512 | block = s_chan->pLoop; |
650adfd2 | 513 | } |
514 | pos += 28 << 16; | |
515 | } | |
516 | ||
517 | if (pos < end) | |
518 | { | |
5514a050 | 519 | sinc_inv = s_chan->sinc_inv; |
650adfd2 | 520 | if (sinc_inv == 0) |
5514a050 | 521 | sinc_inv = s_chan->sinc_inv = (0x80000000u / (uint32_t)sinc) << 1; |
650adfd2 | 522 | |
5514a050 | 523 | pos -= s_chan->spos; |
650adfd2 | 524 | *upd_samples = (((uint64_t)pos * sinc_inv) >> 32) + 1; |
525 | //xprintf("ch%02d: irq sched: %3d %03d\n", | |
526 | // ch, *upd_samples, *upd_samples * 60 * 263 / 44100); | |
527 | } | |
528 | } | |
529 | ||
07a6dd2c | 530 | #define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \ |
3bd31caf | 531 | static noinline int do_samples_##name( \ |
532 | int (*decode_f)(void *context, int ch, int *SB), void *ctx, \ | |
533 | int ch, int ns_to, int *SB, int sinc, int *spos, int *sbpos) \ | |
07a6dd2c | 534 | { \ |
215ff9e6 | 535 | int ns, d, fa; \ |
536 | int ret = ns_to; \ | |
07a6dd2c | 537 | interp_start; \ |
538 | \ | |
215ff9e6 | 539 | for (ns = 0; ns < ns_to; ns++) \ |
07a6dd2c | 540 | { \ |
541 | fmod_code; \ | |
542 | \ | |
63a4f6b6 | 543 | *spos += sinc; \ |
544 | while (*spos >= 0x10000) \ | |
07a6dd2c | 545 | { \ |
63a4f6b6 | 546 | fa = SB[(*sbpos)++]; \ |
547 | if (*sbpos >= 28) \ | |
07a6dd2c | 548 | { \ |
63a4f6b6 | 549 | *sbpos = 0; \ |
3bd31caf | 550 | d = decode_f(ctx, ch, SB); \ |
215ff9e6 | 551 | if (d && ns < ret) \ |
552 | ret = ns; \ | |
07a6dd2c | 553 | } \ |
554 | \ | |
07a6dd2c | 555 | interp1_code; \ |
63a4f6b6 | 556 | *spos -= 0x10000; \ |
07a6dd2c | 557 | } \ |
558 | \ | |
559 | interp2_code; \ | |
07a6dd2c | 560 | } \ |
561 | \ | |
07a6dd2c | 562 | interp_end; \ |
563 | \ | |
564 | return ret; \ | |
565 | } | |
566 | ||
567 | #define fmod_recv_check \ | |
5514a050 | 568 | if(spu.s_chan[ch].bFMod==1 && iFMod[ns]) \ |
569 | sinc = FModChangeFrequency(SB, spu.s_chan[ch].iRawPitch, ns) | |
07a6dd2c | 570 | |
571 | make_do_samples(default, fmod_recv_check, , | |
5514a050 | 572 | StoreInterpolationVal(SB, sinc, fa, spu.s_chan[ch].bFMod==2), |
573 | ChanBuf[ns] = iGetInterpolationVal(SB, sinc, *spos, spu.s_chan[ch].bFMod==2), ) | |
011755d7 | 574 | make_do_samples(noint, , fa = SB[29], , ChanBuf[ns] = fa, SB[29] = fa) |
07a6dd2c | 575 | |
576 | #define simple_interp_store \ | |
215ff9e6 | 577 | SB[28] = 0; \ |
578 | SB[29] = SB[30]; \ | |
579 | SB[30] = SB[31]; \ | |
580 | SB[31] = fa; \ | |
581 | SB[32] = 1 | |
07a6dd2c | 582 | |
583 | #define simple_interp_get \ | |
011755d7 | 584 | if(sinc<0x10000) /* -> upsampling? */ \ |
585 | InterpolateUp(SB, sinc); /* --> interpolate up */ \ | |
586 | else InterpolateDown(SB, sinc); /* --> else down */ \ | |
587 | ChanBuf[ns] = SB[29] | |
07a6dd2c | 588 | |
589 | make_do_samples(simple, , , | |
590 | simple_interp_store, simple_interp_get, ) | |
591 | ||
215ff9e6 | 592 | static int do_samples_skip(int ch, int ns_to) |
07a6dd2c | 593 | { |
5514a050 | 594 | SPUCHAN *s_chan = &spu.s_chan[ch]; |
8f5f2dd5 | 595 | int spos = s_chan->spos; |
596 | int sinc = s_chan->sinc; | |
215ff9e6 | 597 | int ret = ns_to, ns, d; |
b1094d0e | 598 | |
8f5f2dd5 | 599 | spos += s_chan->iSBPos << 16; |
215ff9e6 | 600 | |
601 | for (ns = 0; ns < ns_to; ns++) | |
07a6dd2c | 602 | { |
8f5f2dd5 | 603 | spos += sinc; |
604 | while (spos >= 28*0x10000) | |
215ff9e6 | 605 | { |
606 | d = skip_block(ch); | |
607 | if (d && ns < ret) | |
608 | ret = ns; | |
8f5f2dd5 | 609 | spos -= 28*0x10000; |
215ff9e6 | 610 | } |
07a6dd2c | 611 | } |
612 | ||
8f5f2dd5 | 613 | s_chan->iSBPos = spos >> 16; |
614 | s_chan->spos = spos & 0xffff; | |
215ff9e6 | 615 | |
616 | return ret; | |
617 | } | |
618 | ||
619 | static void do_lsfr_samples(int ns_to, int ctrl, | |
620 | unsigned int *dwNoiseCount, unsigned int *dwNoiseVal) | |
621 | { | |
622 | unsigned int counter = *dwNoiseCount; | |
623 | unsigned int val = *dwNoiseVal; | |
624 | unsigned int level, shift, bit; | |
625 | int ns; | |
626 | ||
b1094d0e | 627 | // modified from DrHell/shalma, no fraction |
215ff9e6 | 628 | level = (ctrl >> 10) & 0x0f; |
b1094d0e | 629 | level = 0x8000 >> level; |
630 | ||
215ff9e6 | 631 | for (ns = 0; ns < ns_to; ns++) |
b1094d0e | 632 | { |
215ff9e6 | 633 | counter += 2; |
634 | if (counter >= level) | |
b1094d0e | 635 | { |
215ff9e6 | 636 | counter -= level; |
637 | shift = (val >> 10) & 0x1f; | |
b1094d0e | 638 | bit = (0x69696969 >> shift) & 1; |
215ff9e6 | 639 | bit ^= (val >> 15) & 1; |
640 | val = (val << 1) | bit; | |
b1094d0e | 641 | } |
642 | ||
215ff9e6 | 643 | ChanBuf[ns] = (signed short)val; |
b1094d0e | 644 | } |
07a6dd2c | 645 | |
215ff9e6 | 646 | *dwNoiseCount = counter; |
647 | *dwNoiseVal = val; | |
648 | } | |
649 | ||
650 | static int do_samples_noise(int ch, int ns_to) | |
651 | { | |
652 | int ret; | |
653 | ||
654 | ret = do_samples_skip(ch, ns_to); | |
655 | ||
656 | do_lsfr_samples(ns_to, spu.spuCtrl, &spu.dwNoiseCount, &spu.dwNoiseVal); | |
657 | ||
89cb2058 | 658 | return ret; |
07a6dd2c | 659 | } |
660 | ||
665f33e1 | 661 | #ifdef HAVE_ARMV5 |
3a721c1f | 662 | // asm code; lv and rv must be 0-3fff |
3bd31caf | 663 | extern void mix_chan(int *SSumLR, int count, int lv, int rv); |
664 | extern void mix_chan_rvb(int *SSumLR, int count, int lv, int rv, int *rvb); | |
b17618c0 | 665 | #else |
3bd31caf | 666 | static void mix_chan(int *SSumLR, int count, int lv, int rv) |
b17618c0 | 667 | { |
3bd31caf | 668 | const int *src = ChanBuf; |
b17618c0 | 669 | int l, r; |
670 | ||
671 | while (count--) | |
672 | { | |
673 | int sval = *src++; | |
674 | ||
675 | l = (sval * lv) >> 14; | |
676 | r = (sval * rv) >> 14; | |
3bd31caf | 677 | *SSumLR++ += l; |
678 | *SSumLR++ += r; | |
b17618c0 | 679 | } |
680 | } | |
681 | ||
3bd31caf | 682 | static void mix_chan_rvb(int *SSumLR, int count, int lv, int rv, int *rvb) |
b17618c0 | 683 | { |
3bd31caf | 684 | const int *src = ChanBuf; |
685 | int *dst = SSumLR; | |
686 | int *drvb = rvb; | |
b17618c0 | 687 | int l, r; |
688 | ||
689 | while (count--) | |
690 | { | |
691 | int sval = *src++; | |
692 | ||
693 | l = (sval * lv) >> 14; | |
694 | r = (sval * rv) >> 14; | |
695 | *dst++ += l; | |
696 | *dst++ += r; | |
697 | *drvb++ += l; | |
698 | *drvb++ += r; | |
699 | } | |
700 | } | |
701 | #endif | |
702 | ||
b72f17a1 | 703 | // 0x0800-0x0bff Voice 1 |
704 | // 0x0c00-0x0fff Voice 3 | |
215ff9e6 | 705 | static noinline void do_decode_bufs(unsigned short *mem, int which, |
706 | int count, int decode_pos) | |
b72f17a1 | 707 | { |
215ff9e6 | 708 | unsigned short *dst = &mem[0x800/2 + which*0x400/2]; |
709 | const int *src = ChanBuf; | |
710 | int cursor = decode_pos; | |
b72f17a1 | 711 | |
712 | while (count-- > 0) | |
713 | { | |
ee9afdbd | 714 | cursor &= 0x1ff; |
b72f17a1 | 715 | dst[cursor] = *src++; |
ee9afdbd | 716 | cursor++; |
b72f17a1 | 717 | } |
718 | ||
719 | // decode_pos is updated and irqs are checked later, after voice loop | |
720 | } | |
721 | ||
215ff9e6 | 722 | static void do_silent_chans(int ns_to, int silentch) |
723 | { | |
5514a050 | 724 | unsigned int mask; |
725 | SPUCHAN *s_chan; | |
215ff9e6 | 726 | int ch; |
727 | ||
5514a050 | 728 | mask = silentch & 0xffffff; |
729 | for (ch = 0; mask != 0; ch++, mask >>= 1) | |
215ff9e6 | 730 | { |
5514a050 | 731 | if (!(mask & 1)) continue; |
215ff9e6 | 732 | if (spu.dwChannelDead & (1<<ch)) continue; |
5514a050 | 733 | |
734 | s_chan = &spu.s_chan[ch]; | |
735 | if (s_chan->pCurr > spu.pSpuIrq && s_chan->pLoop > spu.pSpuIrq) | |
215ff9e6 | 736 | continue; |
737 | ||
5514a050 | 738 | s_chan->spos += s_chan->iSBPos << 16; |
739 | s_chan->iSBPos = 0; | |
215ff9e6 | 740 | |
5514a050 | 741 | s_chan->spos += s_chan->sinc * ns_to; |
742 | while (s_chan->spos >= 28 * 0x10000) | |
215ff9e6 | 743 | { |
5514a050 | 744 | unsigned char *start = s_chan->pCurr; |
215ff9e6 | 745 | |
746 | skip_block(ch); | |
5514a050 | 747 | if (start == s_chan->pCurr || start - spu.spuMemC < 0x1000) |
215ff9e6 | 748 | { |
749 | // looping on self or stopped(?) | |
750 | spu.dwChannelDead |= 1<<ch; | |
5514a050 | 751 | s_chan->spos = 0; |
215ff9e6 | 752 | break; |
753 | } | |
754 | ||
5514a050 | 755 | s_chan->spos -= 28 * 0x10000; |
215ff9e6 | 756 | } |
757 | } | |
758 | } | |
759 | ||
760 | static void do_channels(int ns_to) | |
761 | { | |
762 | unsigned int mask; | |
05c7cec7 | 763 | int do_rvb, ch, d; |
5514a050 | 764 | SPUCHAN *s_chan; |
63a4f6b6 | 765 | int *SB, sinc; |
215ff9e6 | 766 | |
05c7cec7 | 767 | do_rvb = spu.rvb->StartAddr && spu_config.iUseReverb; |
768 | if (do_rvb) | |
769 | memset(RVB, 0, ns_to * sizeof(RVB[0]) * 2); | |
3bd31caf | 770 | |
771 | mask = spu.dwNewChannel & 0xffffff; | |
772 | for (ch = 0; mask != 0; ch++, mask >>= 1) { | |
773 | if (mask & 1) | |
774 | StartSound(ch); | |
775 | } | |
215ff9e6 | 776 | |
2daa7c1d | 777 | mask = spu.dwChannelsAudible & 0xffffff; |
215ff9e6 | 778 | for (ch = 0; mask != 0; ch++, mask >>= 1) // loop em all... |
779 | { | |
780 | if (!(mask & 1)) continue; // channel not playing? next | |
781 | ||
5514a050 | 782 | s_chan = &spu.s_chan[ch]; |
de4a0279 | 783 | SB = spu.SB + ch * SB_SIZE; |
5514a050 | 784 | sinc = s_chan->sinc; |
4ecb720e | 785 | if (spu.s_chan[ch].bNewPitch) |
786 | SB[32] = 1; // reset interpolation | |
787 | spu.s_chan[ch].bNewPitch = 0; | |
63a4f6b6 | 788 | |
5514a050 | 789 | if (s_chan->bNoise) |
215ff9e6 | 790 | d = do_samples_noise(ch, ns_to); |
5514a050 | 791 | else if (s_chan->bFMod == 2 |
792 | || (s_chan->bFMod == 0 && spu_config.iUseInterpolation == 0)) | |
3bd31caf | 793 | d = do_samples_noint(decode_block, NULL, ch, ns_to, |
5514a050 | 794 | SB, sinc, &s_chan->spos, &s_chan->iSBPos); |
795 | else if (s_chan->bFMod == 0 && spu_config.iUseInterpolation == 1) | |
3bd31caf | 796 | d = do_samples_simple(decode_block, NULL, ch, ns_to, |
5514a050 | 797 | SB, sinc, &s_chan->spos, &s_chan->iSBPos); |
215ff9e6 | 798 | else |
3bd31caf | 799 | d = do_samples_default(decode_block, NULL, ch, ns_to, |
5514a050 | 800 | SB, sinc, &s_chan->spos, &s_chan->iSBPos); |
215ff9e6 | 801 | |
6183ddf9 | 802 | if (!s_chan->bStarting) { |
803 | d = MixADSR(&s_chan->ADSRX, d); | |
804 | if (d < ns_to) { | |
805 | spu.dwChannelsAudible &= ~(1 << ch); | |
806 | s_chan->ADSRX.State = ADSR_RELEASE; | |
807 | s_chan->ADSRX.EnvelopeVol = 0; | |
808 | memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0])); | |
809 | } | |
215ff9e6 | 810 | } |
811 | ||
812 | if (ch == 1 || ch == 3) | |
813 | { | |
814 | do_decode_bufs(spu.spuMem, ch/2, ns_to, spu.decode_pos); | |
815 | spu.decode_dirty_ch |= 1 << ch; | |
816 | } | |
817 | ||
5514a050 | 818 | if (s_chan->bFMod == 2) // fmod freq channel |
215ff9e6 | 819 | memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0])); |
05c7cec7 | 820 | if (s_chan->bRVBActive && do_rvb) |
821 | mix_chan_rvb(spu.SSumLR, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume, RVB); | |
215ff9e6 | 822 | else |
3bd31caf | 823 | mix_chan(spu.SSumLR, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume); |
215ff9e6 | 824 | } |
05c7cec7 | 825 | |
e81f182d | 826 | MixXA(spu.SSumLR, RVB, ns_to, spu.decode_pos); |
827 | ||
05c7cec7 | 828 | if (spu.rvb->StartAddr) { |
7b2c4897 | 829 | if (do_rvb) |
05c7cec7 | 830 | REVERBDo(spu.SSumLR, RVB, ns_to, spu.rvb->CurrAddr); |
05c7cec7 | 831 | |
832 | spu.rvb->CurrAddr += ns_to / 2; | |
833 | while (spu.rvb->CurrAddr >= 0x40000) | |
834 | spu.rvb->CurrAddr -= 0x40000 - spu.rvb->StartAddr; | |
835 | } | |
215ff9e6 | 836 | } |
837 | ||
05c7cec7 | 838 | static void do_samples_finish(int *SSumLR, int ns_to, |
3bd31caf | 839 | int silentch, int decode_pos); |
63a4f6b6 | 840 | |
841 | // optional worker thread handling | |
842 | ||
94f0c7c5 | 843 | #if P_HAVE_PTHREAD || defined(WANT_THREAD_CODE) |
63a4f6b6 | 844 | |
3bd31caf | 845 | // worker thread state |
846 | static struct spu_worker { | |
847 | union { | |
848 | struct { | |
849 | unsigned int exit_thread; | |
850 | unsigned int i_ready; | |
851 | unsigned int i_reaped; | |
05c7cec7 | 852 | unsigned int last_boot_cnt; // dsp |
0c1151fe | 853 | unsigned int ram_dirty; |
3bd31caf | 854 | }; |
855 | // aligning for C64X_DSP | |
856 | unsigned int _pad0[128/4]; | |
857 | }; | |
858 | union { | |
859 | struct { | |
860 | unsigned int i_done; | |
861 | unsigned int active; // dsp | |
862 | unsigned int boot_cnt; | |
863 | }; | |
864 | unsigned int _pad1[128/4]; | |
865 | }; | |
866 | struct work_item { | |
867 | int ns_to; | |
868 | int ctrl; | |
869 | int decode_pos; | |
05c7cec7 | 870 | int rvb_addr; |
3bd31caf | 871 | unsigned int channels_new; |
872 | unsigned int channels_on; | |
873 | unsigned int channels_silent; | |
874 | struct { | |
875 | int spos; | |
876 | int sbpos; | |
877 | int sinc; | |
878 | int start; | |
879 | int loop; | |
05c7cec7 | 880 | short vol_l; |
881 | short vol_r; | |
4ecb720e | 882 | unsigned short ns_to; |
883 | unsigned short bNoise:1; | |
884 | unsigned short bFMod:2; | |
885 | unsigned short bRVBActive:1; | |
886 | unsigned short bNewPitch:1; | |
3bd31caf | 887 | ADSRInfoEx adsr; |
3bd31caf | 888 | } ch[24]; |
3bd31caf | 889 | int SSumLR[NSSIZE * 2]; |
890 | } i[4]; | |
891 | } *worker; | |
892 | ||
893 | #define WORK_MAXCNT (sizeof(worker->i) / sizeof(worker->i[0])) | |
894 | #define WORK_I_MASK (WORK_MAXCNT - 1) | |
895 | ||
5514a050 | 896 | static void thread_work_start(void); |
3bd31caf | 897 | static void thread_work_wait_sync(struct work_item *work, int force); |
05c7cec7 | 898 | static void thread_sync_caches(void); |
3bd31caf | 899 | static int thread_get_i_done(void); |
900 | ||
901 | static int decode_block_work(void *context, int ch, int *SB) | |
902 | { | |
903 | const unsigned char *ram = spu.spuMemC; | |
904 | int predict_nr, shift_factor, flags; | |
905 | struct work_item *work = context; | |
906 | int start = work->ch[ch].start; | |
907 | int loop = work->ch[ch].loop; | |
908 | ||
909 | predict_nr = ram[start]; | |
910 | shift_factor = predict_nr & 0xf; | |
911 | predict_nr >>= 4; | |
912 | ||
913 | decode_block_data(SB, ram + start + 2, predict_nr, shift_factor); | |
914 | ||
915 | flags = ram[start + 1]; | |
916 | if (flags & 4) | |
917 | loop = start; // loop adress | |
918 | ||
919 | start += 16; | |
920 | ||
921 | if (flags & 1) // 1: stop/loop | |
922 | start = loop; | |
923 | ||
924 | work->ch[ch].start = start & 0x7ffff; | |
925 | work->ch[ch].loop = loop; | |
926 | ||
927 | return 0; | |
928 | } | |
5514a050 | 929 | |
3bd31caf | 930 | static void queue_channel_work(int ns_to, unsigned int silentch) |
63a4f6b6 | 931 | { |
3bd31caf | 932 | struct work_item *work; |
933 | SPUCHAN *s_chan; | |
63a4f6b6 | 934 | unsigned int mask; |
3bd31caf | 935 | int ch, d; |
63a4f6b6 | 936 | |
3bd31caf | 937 | work = &worker->i[worker->i_ready & WORK_I_MASK]; |
938 | work->ns_to = ns_to; | |
939 | work->ctrl = spu.spuCtrl; | |
940 | work->decode_pos = spu.decode_pos; | |
941 | work->channels_silent = silentch; | |
942 | ||
943 | mask = work->channels_new = spu.dwNewChannel & 0xffffff; | |
944 | for (ch = 0; mask != 0; ch++, mask >>= 1) { | |
945 | if (mask & 1) | |
946 | StartSoundMain(ch); | |
947 | } | |
948 | ||
2daa7c1d | 949 | mask = work->channels_on = spu.dwChannelsAudible & 0xffffff; |
3bd31caf | 950 | spu.decode_dirty_ch |= mask & 0x0a; |
63a4f6b6 | 951 | |
63a4f6b6 | 952 | for (ch = 0; mask != 0; ch++, mask >>= 1) |
953 | { | |
954 | if (!(mask & 1)) continue; | |
955 | ||
5514a050 | 956 | s_chan = &spu.s_chan[ch]; |
3bd31caf | 957 | work->ch[ch].spos = s_chan->spos; |
958 | work->ch[ch].sbpos = s_chan->iSBPos; | |
959 | work->ch[ch].sinc = s_chan->sinc; | |
960 | work->ch[ch].adsr = s_chan->ADSRX; | |
05c7cec7 | 961 | work->ch[ch].vol_l = s_chan->iLeftVolume; |
962 | work->ch[ch].vol_r = s_chan->iRightVolume; | |
3bd31caf | 963 | work->ch[ch].start = s_chan->pCurr - spu.spuMemC; |
964 | work->ch[ch].loop = s_chan->pLoop - spu.spuMemC; | |
4ecb720e | 965 | work->ch[ch].bNoise = s_chan->bNoise; |
966 | work->ch[ch].bFMod = s_chan->bFMod; | |
967 | work->ch[ch].bRVBActive = s_chan->bRVBActive; | |
968 | work->ch[ch].bNewPitch = s_chan->bNewPitch; | |
5514a050 | 969 | if (s_chan->prevflags & 1) |
3bd31caf | 970 | work->ch[ch].start = work->ch[ch].loop; |
63a4f6b6 | 971 | |
3bd31caf | 972 | d = do_samples_skip(ch, ns_to); |
973 | work->ch[ch].ns_to = d; | |
974 | ||
6183ddf9 | 975 | if (!s_chan->bStarting) { |
976 | // note: d is not accurate on skip | |
977 | d = SkipADSR(&s_chan->ADSRX, d); | |
978 | if (d < ns_to) { | |
979 | spu.dwChannelsAudible &= ~(1 << ch); | |
980 | s_chan->ADSRX.State = ADSR_RELEASE; | |
981 | s_chan->ADSRX.EnvelopeVol = 0; | |
982 | } | |
3bd31caf | 983 | } |
4ecb720e | 984 | s_chan->bNewPitch = 0; |
63a4f6b6 | 985 | } |
986 | ||
05c7cec7 | 987 | work->rvb_addr = 0; |
988 | if (spu.rvb->StartAddr) { | |
7b2c4897 | 989 | if (spu_config.iUseReverb) |
05c7cec7 | 990 | work->rvb_addr = spu.rvb->CurrAddr; |
05c7cec7 | 991 | |
992 | spu.rvb->CurrAddr += ns_to / 2; | |
993 | while (spu.rvb->CurrAddr >= 0x40000) | |
994 | spu.rvb->CurrAddr -= 0x40000 - spu.rvb->StartAddr; | |
995 | } | |
996 | ||
3bd31caf | 997 | worker->i_ready++; |
5514a050 | 998 | thread_work_start(); |
63a4f6b6 | 999 | } |
1000 | ||
3bd31caf | 1001 | static void do_channel_work(struct work_item *work) |
63a4f6b6 | 1002 | { |
3bd31caf | 1003 | unsigned int mask; |
63a4f6b6 | 1004 | int *SB, sinc, spos, sbpos; |
1005 | int d, ch, ns_to; | |
1006 | ||
3bd31caf | 1007 | ns_to = work->ns_to; |
05c7cec7 | 1008 | |
1009 | if (work->rvb_addr) | |
1010 | memset(RVB, 0, ns_to * sizeof(RVB[0]) * 2); | |
63a4f6b6 | 1011 | |
3bd31caf | 1012 | mask = work->channels_new; |
1013 | for (ch = 0; mask != 0; ch++, mask >>= 1) { | |
1014 | if (mask & 1) | |
1015 | StartSoundSB(spu.SB + ch * SB_SIZE); | |
1016 | } | |
1017 | ||
1018 | mask = work->channels_on; | |
63a4f6b6 | 1019 | for (ch = 0; mask != 0; ch++, mask >>= 1) |
1020 | { | |
1021 | if (!(mask & 1)) continue; | |
1022 | ||
3bd31caf | 1023 | d = work->ch[ch].ns_to; |
1024 | spos = work->ch[ch].spos; | |
1025 | sbpos = work->ch[ch].sbpos; | |
1026 | sinc = work->ch[ch].sinc; | |
63a4f6b6 | 1027 | |
de4a0279 | 1028 | SB = spu.SB + ch * SB_SIZE; |
4ecb720e | 1029 | if (work->ch[ch].bNewPitch) |
1030 | SB[32] = 1; // reset interpolation | |
5514a050 | 1031 | |
4ecb720e | 1032 | if (work->ch[ch].bNoise) |
3bd31caf | 1033 | do_lsfr_samples(d, work->ctrl, &spu.dwNoiseCount, &spu.dwNoiseVal); |
4ecb720e | 1034 | else if (work->ch[ch].bFMod == 2 |
1035 | || (work->ch[ch].bFMod == 0 && spu_config.iUseInterpolation == 0)) | |
3bd31caf | 1036 | do_samples_noint(decode_block_work, work, ch, d, SB, sinc, &spos, &sbpos); |
4ecb720e | 1037 | else if (work->ch[ch].bFMod == 0 && spu_config.iUseInterpolation == 1) |
3bd31caf | 1038 | do_samples_simple(decode_block_work, work, ch, d, SB, sinc, &spos, &sbpos); |
63a4f6b6 | 1039 | else |
3bd31caf | 1040 | do_samples_default(decode_block_work, work, ch, d, SB, sinc, &spos, &sbpos); |
63a4f6b6 | 1041 | |
3bd31caf | 1042 | d = MixADSR(&work->ch[ch].adsr, d); |
63a4f6b6 | 1043 | if (d < ns_to) { |
3bd31caf | 1044 | work->ch[ch].adsr.EnvelopeVol = 0; |
63a4f6b6 | 1045 | memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0])); |
1046 | } | |
1047 | ||
1048 | if (ch == 1 || ch == 3) | |
4ecb720e | 1049 | do_decode_bufs(spu.spuMem, ch/2, ns_to, work->decode_pos); |
63a4f6b6 | 1050 | |
4ecb720e | 1051 | if (work->ch[ch].bFMod == 2) // fmod freq channel |
63a4f6b6 | 1052 | memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0])); |
4ecb720e | 1053 | if (work->ch[ch].bRVBActive && work->rvb_addr) |
3bd31caf | 1054 | mix_chan_rvb(work->SSumLR, ns_to, |
05c7cec7 | 1055 | work->ch[ch].vol_l, work->ch[ch].vol_r, RVB); |
63a4f6b6 | 1056 | else |
05c7cec7 | 1057 | mix_chan(work->SSumLR, ns_to, work->ch[ch].vol_l, work->ch[ch].vol_r); |
63a4f6b6 | 1058 | } |
05c7cec7 | 1059 | |
1060 | if (work->rvb_addr) | |
1061 | REVERBDo(work->SSumLR, RVB, ns_to, work->rvb_addr); | |
63a4f6b6 | 1062 | } |
1063 | ||
3bd31caf | 1064 | static void sync_worker_thread(int force) |
63a4f6b6 | 1065 | { |
3bd31caf | 1066 | struct work_item *work; |
1067 | int done, used_space; | |
63a4f6b6 | 1068 | |
7b2c4897 | 1069 | // rvb offsets will change, thread may be using them |
1070 | force |= spu.rvb->dirty && spu.rvb->StartAddr; | |
1071 | ||
3bd31caf | 1072 | done = thread_get_i_done() - worker->i_reaped; |
1073 | used_space = worker->i_ready - worker->i_reaped; | |
0c1151fe | 1074 | |
3bd31caf | 1075 | //printf("done: %d use: %d dsp: %u/%u\n", done, used_space, |
1076 | // worker->boot_cnt, worker->i_done); | |
63a4f6b6 | 1077 | |
3bd31caf | 1078 | while ((force && used_space > 0) || used_space >= WORK_MAXCNT || done > 0) { |
1079 | work = &worker->i[worker->i_reaped & WORK_I_MASK]; | |
1080 | thread_work_wait_sync(work, force); | |
63a4f6b6 | 1081 | |
e81f182d | 1082 | MixXA(work->SSumLR, RVB, work->ns_to, work->decode_pos); |
05c7cec7 | 1083 | do_samples_finish(work->SSumLR, work->ns_to, |
3bd31caf | 1084 | work->channels_silent, work->decode_pos); |
63a4f6b6 | 1085 | |
3bd31caf | 1086 | worker->i_reaped++; |
1087 | done = thread_get_i_done() - worker->i_reaped; | |
1088 | used_space = worker->i_ready - worker->i_reaped; | |
63a4f6b6 | 1089 | } |
05c7cec7 | 1090 | if (force) |
1091 | thread_sync_caches(); | |
63a4f6b6 | 1092 | } |
1093 | ||
1094 | #else | |
1095 | ||
1096 | static void queue_channel_work(int ns_to, int silentch) {} | |
3bd31caf | 1097 | static void sync_worker_thread(int force) {} |
1098 | ||
1099 | static const void * const worker = NULL; | |
63a4f6b6 | 1100 | |
94f0c7c5 | 1101 | #endif // P_HAVE_PTHREAD || defined(WANT_THREAD_CODE) |
63a4f6b6 | 1102 | |
ef79bbde P |
1103 | //////////////////////////////////////////////////////////////////////// |
1104 | // MAIN SPU FUNCTION | |
6d75977b | 1105 | // here is the main job handler... |
ef79bbde P |
1106 | //////////////////////////////////////////////////////////////////////// |
1107 | ||
de4a0279 | 1108 | void do_samples(unsigned int cycles_to, int do_direct) |
ef79bbde | 1109 | { |
3bd31caf | 1110 | unsigned int silentch; |
650adfd2 | 1111 | int cycle_diff; |
3bd31caf | 1112 | int ns_to; |
ef79bbde | 1113 | |
3154bfab | 1114 | cycle_diff = cycles_to - spu.cycles_played; |
650adfd2 | 1115 | if (cycle_diff < -2*1048576 || cycle_diff > 2*1048576) |
1116 | { | |
1117 | //xprintf("desync %u %d\n", cycles_to, cycle_diff); | |
3154bfab | 1118 | spu.cycles_played = cycles_to; |
650adfd2 | 1119 | return; |
1120 | } | |
e34a4bd3 | 1121 | |
2daa7c1d | 1122 | silentch = ~(spu.dwChannelsAudible | spu.dwNewChannel) & 0xffffff; |
3bd31caf | 1123 | |
1124 | do_direct |= (silentch == 0xffffff); | |
de4a0279 | 1125 | if (worker != NULL) |
1126 | sync_worker_thread(do_direct); | |
1127 | ||
650adfd2 | 1128 | if (cycle_diff < 2 * 768) |
1129 | return; | |
e34a4bd3 | 1130 | |
650adfd2 | 1131 | ns_to = (cycle_diff / 768 + 1) & ~1; |
1132 | if (ns_to > NSSIZE) { | |
1133 | // should never happen | |
1134 | //xprintf("ns_to oflow %d %d\n", ns_to, NSSIZE); | |
1135 | ns_to = NSSIZE; | |
1136 | } | |
1137 | ||
1138 | ////////////////////////////////////////////////////// | |
1139 | // special irq handling in the decode buffers (0x0000-0x1000) | |
1140 | // we know: | |
1141 | // the decode buffers are located in spu memory in the following way: | |
1142 | // 0x0000-0x03ff CD audio left | |
1143 | // 0x0400-0x07ff CD audio right | |
1144 | // 0x0800-0x0bff Voice 1 | |
1145 | // 0x0c00-0x0fff Voice 3 | |
1146 | // and decoded data is 16 bit for one sample | |
1147 | // we assume: | |
1148 | // even if voices 1/3 are off or no cd audio is playing, the internal | |
1149 | // play positions will move on and wrap after 0x400 bytes. | |
1150 | // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and | |
1151 | // increase this pointer on each sample by 2 bytes. If this pointer | |
1152 | // (or 0x400 offsets of this pointer) hits the spuirq address, we generate | |
1153 | // an IRQ. | |
1154 | ||
3154bfab | 1155 | if (unlikely((spu.spuCtrl & CTRL_IRQ) |
c4c66b22 | 1156 | && spu.pSpuIrq < spu.spuMemC+0x1000)) |
650adfd2 | 1157 | { |
3154bfab | 1158 | int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff; |
1159 | int left = (irq_pos - spu.decode_pos) & 0x1ff; | |
650adfd2 | 1160 | if (0 < left && left <= ns_to) |
1161 | { | |
3154bfab | 1162 | //xprintf("decoder irq %x\n", spu.decode_pos); |
650adfd2 | 1163 | do_irq(); |
650adfd2 | 1164 | } |
1165 | } | |
56e500f3 | 1166 | if (!spu.cycles_dma_end || (int)(spu.cycles_dma_end - cycles_to) < 0) { |
1167 | spu.cycles_dma_end = 0; | |
1168 | check_irq_io(spu.spuAddr); | |
1169 | } | |
650adfd2 | 1170 | |
7b2c4897 | 1171 | if (unlikely(spu.rvb->dirty)) |
1172 | REVERBPrep(); | |
1173 | ||
3bd31caf | 1174 | if (do_direct || worker == NULL || !spu_config.iUseThread) { |
1175 | do_channels(ns_to); | |
05c7cec7 | 1176 | do_samples_finish(spu.SSumLR, ns_to, silentch, spu.decode_pos); |
63a4f6b6 | 1177 | } |
215ff9e6 | 1178 | else { |
3bd31caf | 1179 | queue_channel_work(ns_to, silentch); |
4ecb720e | 1180 | //sync_worker_thread(1); // uncomment for debug |
215ff9e6 | 1181 | } |
ef79bbde | 1182 | |
215ff9e6 | 1183 | // advance "stopped" channels that can cause irqs |
1184 | // (all chans are always playing on the real thing..) | |
1185 | if (spu.spuCtrl & CTRL_IRQ) | |
1186 | do_silent_chans(ns_to, silentch); | |
1187 | ||
1188 | spu.cycles_played += ns_to * 768; | |
1189 | spu.decode_pos = (spu.decode_pos + ns_to) & 0x1ff; | |
6183ddf9 | 1190 | #if 0 |
1191 | static int ccount; static time_t ctime; ccount++; | |
1192 | if (time(NULL) != ctime) | |
1193 | { printf("%d\n", ccount); ccount = 0; ctime = time(NULL); } | |
1194 | #endif | |
215ff9e6 | 1195 | } |
1196 | ||
05c7cec7 | 1197 | static void do_samples_finish(int *SSumLR, int ns_to, |
3bd31caf | 1198 | int silentch, int decode_pos) |
215ff9e6 | 1199 | { |
a0d4a8f5 | 1200 | int vol_l = ((int)regAreaGet(H_SPUmvolL) << 17) >> 17; |
1201 | int vol_r = ((int)regAreaGet(H_SPUmvolR) << 17) >> 17; | |
215ff9e6 | 1202 | int ns; |
1203 | int d; | |
78c60846 | 1204 | |
3bd31caf | 1205 | // must clear silent channel decode buffers |
1206 | if(unlikely(silentch & spu.decode_dirty_ch & (1<<1))) | |
b72f17a1 | 1207 | { |
3154bfab | 1208 | memset(&spu.spuMem[0x800/2], 0, 0x400); |
1209 | spu.decode_dirty_ch &= ~(1<<1); | |
b72f17a1 | 1210 | } |
3154bfab | 1211 | if(unlikely(silentch & spu.decode_dirty_ch & (1<<3))) |
b72f17a1 | 1212 | { |
3154bfab | 1213 | memset(&spu.spuMem[0xc00/2], 0, 0x400); |
1214 | spu.decode_dirty_ch &= ~(1<<3); | |
b72f17a1 | 1215 | } |
8680e822 | 1216 | |
a0d4a8f5 | 1217 | vol_l = vol_l * spu_config.iVolume >> 10; |
1218 | vol_r = vol_r * spu_config.iVolume >> 10; | |
1219 | ||
e81f182d | 1220 | if (!(spu.spuCtrl & CTRL_MUTE) || !(vol_l | vol_r)) |
1775933a | 1221 | { |
a0d4a8f5 | 1222 | // muted? (rare) |
3154bfab | 1223 | memset(spu.pS, 0, ns_to * 2 * sizeof(spu.pS[0])); |
a0d4a8f5 | 1224 | memset(SSumLR, 0, ns_to * 2 * sizeof(SSumLR[0])); |
3154bfab | 1225 | spu.pS += ns_to * 2; |
1775933a | 1226 | } |
1227 | else | |
650adfd2 | 1228 | for (ns = 0; ns < ns_to * 2; ) |
ef79bbde | 1229 | { |
9e7a7352 | 1230 | d = SSumLR[ns]; SSumLR[ns] = 0; |
e29eaeb9 | 1231 | d = d * vol_l >> 14; |
1775933a | 1232 | ssat32_to_16(d); |
3154bfab | 1233 | *spu.pS++ = d; |
97ea4077 | 1234 | ns++; |
ef79bbde | 1235 | |
9e7a7352 | 1236 | d = SSumLR[ns]; SSumLR[ns] = 0; |
e29eaeb9 | 1237 | d = d * vol_r >> 14; |
1775933a | 1238 | ssat32_to_16(d); |
3154bfab | 1239 | *spu.pS++ = d; |
97ea4077 | 1240 | ns++; |
ef79bbde | 1241 | } |
650adfd2 | 1242 | } |
ef79bbde | 1243 | |
650adfd2 | 1244 | void schedule_next_irq(void) |
1245 | { | |
1246 | unsigned int upd_samples; | |
1247 | int ch; | |
ef79bbde | 1248 | |
3154bfab | 1249 | if (spu.scheduleCallback == NULL) |
650adfd2 | 1250 | return; |
e34a4bd3 | 1251 | |
650adfd2 | 1252 | upd_samples = 44100 / 50; |
e34a4bd3 | 1253 | |
650adfd2 | 1254 | for (ch = 0; ch < MAXCHAN; ch++) |
1255 | { | |
3154bfab | 1256 | if (spu.dwChannelDead & (1 << ch)) |
650adfd2 | 1257 | continue; |
5514a050 | 1258 | if ((unsigned long)(spu.pSpuIrq - spu.s_chan[ch].pCurr) > IRQ_NEAR_BLOCKS * 16 |
1259 | && (unsigned long)(spu.pSpuIrq - spu.s_chan[ch].pLoop) > IRQ_NEAR_BLOCKS * 16) | |
650adfd2 | 1260 | continue; |
4f9efe54 | 1261 | if (spu.s_chan[ch].sinc == 0) |
1262 | continue; | |
16187bfb | 1263 | |
650adfd2 | 1264 | scan_for_irq(ch, &upd_samples); |
ef79bbde P |
1265 | } |
1266 | ||
3154bfab | 1267 | if (unlikely(spu.pSpuIrq < spu.spuMemC + 0x1000)) |
650adfd2 | 1268 | { |
3154bfab | 1269 | int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff; |
1270 | int left = (irq_pos - spu.decode_pos) & 0x1ff; | |
650adfd2 | 1271 | if (0 < left && left < upd_samples) { |
3154bfab | 1272 | //xprintf("decode: %3d (%3d/%3d)\n", left, spu.decode_pos, irq_pos); |
650adfd2 | 1273 | upd_samples = left; |
1274 | } | |
1275 | } | |
16187bfb | 1276 | |
650adfd2 | 1277 | if (upd_samples < 44100 / 50) |
3154bfab | 1278 | spu.scheduleCallback(upd_samples * 768); |
ef79bbde P |
1279 | } |
1280 | ||
1281 | // SPU ASYNC... even newer epsxe func | |
1282 | // 1 time every 'cycle' cycles... harhar | |
1283 | ||
650adfd2 | 1284 | // rearmed: called dynamically now |
554a2220 | 1285 | |
650adfd2 | 1286 | void CALLBACK SPUasync(unsigned int cycle, unsigned int flags) |
ef79bbde | 1287 | { |
403a6290 | 1288 | do_samples(cycle, 0); |
fb552464 | 1289 | |
3154bfab | 1290 | if (spu.spuCtrl & CTRL_IRQ) |
650adfd2 | 1291 | schedule_next_irq(); |
f8edb5bc | 1292 | |
650adfd2 | 1293 | if (flags & 1) { |
3154bfab | 1294 | out_current->feed(spu.pSpuBuffer, (unsigned char *)spu.pS - spu.pSpuBuffer); |
1295 | spu.pS = (short *)spu.pSpuBuffer; | |
fb552464 | 1296 | |
3154bfab | 1297 | if (spu_config.iTempo) { |
650adfd2 | 1298 | if (!out_current->busy()) |
1299 | // cause more samples to be generated | |
1300 | // (and break some games because of bad sync) | |
3154bfab | 1301 | spu.cycles_played -= 44100 / 60 / 2 * 768; |
ef79bbde | 1302 | } |
650adfd2 | 1303 | } |
ef79bbde P |
1304 | } |
1305 | ||
1306 | // SPU UPDATE... new epsxe func | |
1307 | // 1 time every 32 hsync lines | |
1308 | // (312/32)x50 in pal | |
1309 | // (262/32)x60 in ntsc | |
1310 | ||
1311 | // since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will | |
1312 | // leave that func in the linux port, until epsxe linux is using | |
1313 | // the async function as well | |
1314 | ||
1315 | void CALLBACK SPUupdate(void) | |
1316 | { | |
ef79bbde P |
1317 | } |
1318 | ||
1319 | // XA AUDIO | |
1320 | ||
cdaef86c | 1321 | void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap, unsigned int cycle, int is_start) |
ef79bbde P |
1322 | { |
1323 | if(!xap) return; | |
cdaef86c | 1324 | if(!xap->freq) return; // no xa freq ? bye |
ef79bbde | 1325 | |
cdaef86c | 1326 | if (is_start) |
1327 | do_samples(cycle, 1); // catch up to prevent source underflows later | |
1328 | ||
1329 | FeedXA(xap); // call main XA feeder | |
403a6290 | 1330 | spu.xapGlobal = xap; // store info for save states |
ef79bbde P |
1331 | } |
1332 | ||
1333 | // CDDA AUDIO | |
cdaef86c | 1334 | int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes, unsigned int cycle, int is_start) |
ef79bbde | 1335 | { |
983a7cfd | 1336 | if (!pcm) return -1; |
1337 | if (nbytes<=0) return -1; | |
ef79bbde | 1338 | |
cdaef86c | 1339 | if (is_start) |
1340 | do_samples(cycle, 1); // catch up to prevent source underflows later | |
1341 | ||
403a6290 | 1342 | FeedCDDA((unsigned char *)pcm, nbytes); |
1343 | return 0; | |
ef79bbde P |
1344 | } |
1345 | ||
6d75977b | 1346 | // to be called after state load |
1347 | void ClearWorkingState(void) | |
ef79bbde | 1348 | { |
5514a050 | 1349 | memset(iFMod, 0, sizeof(iFMod)); |
3154bfab | 1350 | spu.pS=(short *)spu.pSpuBuffer; // setup soundbuffer pointer |
ef79bbde P |
1351 | } |
1352 | ||
1353 | // SETUPSTREAMS: init most of the spu buffers | |
05c7cec7 | 1354 | static void SetupStreams(void) |
ef79bbde | 1355 | { |
3154bfab | 1356 | spu.pSpuBuffer = (unsigned char *)malloc(32768); // alloc mixing buffer |
3bd31caf | 1357 | spu.SSumLR = calloc(NSSIZE * 2, sizeof(spu.SSumLR[0])); |
ef79bbde | 1358 | |
2cb5735e | 1359 | spu.XAStart = malloc(44100 * sizeof(uint32_t)); // alloc xa buffer |
3154bfab | 1360 | spu.XAEnd = spu.XAStart + 44100; |
1361 | spu.XAPlay = spu.XAStart; | |
1362 | spu.XAFeed = spu.XAStart; | |
ef79bbde | 1363 | |
2cb5735e | 1364 | spu.CDDAStart = malloc(CDDA_BUFFER_SIZE); // alloc cdda buffer |
403a6290 | 1365 | spu.CDDAEnd = spu.CDDAStart + CDDA_BUFFER_SIZE / sizeof(uint32_t); |
3154bfab | 1366 | spu.CDDAPlay = spu.CDDAStart; |
1367 | spu.CDDAFeed = spu.CDDAStart; | |
ef79bbde | 1368 | |
6d75977b | 1369 | ClearWorkingState(); |
ef79bbde P |
1370 | } |
1371 | ||
1372 | // REMOVESTREAMS: free most buffer | |
05c7cec7 | 1373 | static void RemoveStreams(void) |
ef79bbde | 1374 | { |
3154bfab | 1375 | free(spu.pSpuBuffer); // free mixing buffer |
1376 | spu.pSpuBuffer = NULL; | |
3bd31caf | 1377 | free(spu.SSumLR); |
1378 | spu.SSumLR = NULL; | |
3154bfab | 1379 | free(spu.XAStart); // free XA buffer |
1380 | spu.XAStart = NULL; | |
1381 | free(spu.CDDAStart); // free CDDA buffer | |
1382 | spu.CDDAStart = NULL; | |
ef79bbde P |
1383 | } |
1384 | ||
5514a050 | 1385 | #if defined(C64X_DSP) |
1386 | ||
1387 | /* special code for TI C64x DSP */ | |
1388 | #include "spu_c64x.c" | |
1389 | ||
94f0c7c5 | 1390 | #elif P_HAVE_PTHREAD |
5514a050 | 1391 | |
1392 | #include <pthread.h> | |
1393 | #include <semaphore.h> | |
1394 | #include <unistd.h> | |
1395 | ||
1396 | static struct { | |
1397 | pthread_t thread; | |
1398 | sem_t sem_avail; | |
1399 | sem_t sem_done; | |
1400 | } t; | |
1401 | ||
1402 | /* generic pthread implementation */ | |
1403 | ||
1404 | static void thread_work_start(void) | |
1405 | { | |
1406 | sem_post(&t.sem_avail); | |
1407 | } | |
1408 | ||
3bd31caf | 1409 | static void thread_work_wait_sync(struct work_item *work, int force) |
5514a050 | 1410 | { |
1411 | sem_wait(&t.sem_done); | |
1412 | } | |
63a4f6b6 | 1413 | |
3bd31caf | 1414 | static int thread_get_i_done(void) |
de4a0279 | 1415 | { |
3bd31caf | 1416 | return worker->i_done; |
de4a0279 | 1417 | } |
1418 | ||
05c7cec7 | 1419 | static void thread_sync_caches(void) |
1420 | { | |
1421 | } | |
1422 | ||
63a4f6b6 | 1423 | static void *spu_worker_thread(void *unused) |
1424 | { | |
3bd31caf | 1425 | struct work_item *work; |
1426 | ||
63a4f6b6 | 1427 | while (1) { |
5514a050 | 1428 | sem_wait(&t.sem_avail); |
63a4f6b6 | 1429 | if (worker->exit_thread) |
1430 | break; | |
1431 | ||
3bd31caf | 1432 | work = &worker->i[worker->i_done & WORK_I_MASK]; |
1433 | do_channel_work(work); | |
1434 | worker->i_done++; | |
63a4f6b6 | 1435 | |
5514a050 | 1436 | sem_post(&t.sem_done); |
63a4f6b6 | 1437 | } |
1438 | ||
1439 | return NULL; | |
1440 | } | |
1441 | ||
1442 | static void init_spu_thread(void) | |
1443 | { | |
1444 | int ret; | |
1445 | ||
1446 | if (sysconf(_SC_NPROCESSORS_ONLN) <= 1) | |
1447 | return; | |
1448 | ||
1449 | worker = calloc(1, sizeof(*worker)); | |
1450 | if (worker == NULL) | |
1451 | return; | |
5514a050 | 1452 | ret = sem_init(&t.sem_avail, 0, 0); |
63a4f6b6 | 1453 | if (ret != 0) |
1454 | goto fail_sem_avail; | |
5514a050 | 1455 | ret = sem_init(&t.sem_done, 0, 0); |
63a4f6b6 | 1456 | if (ret != 0) |
1457 | goto fail_sem_done; | |
1458 | ||
5514a050 | 1459 | ret = pthread_create(&t.thread, NULL, spu_worker_thread, NULL); |
63a4f6b6 | 1460 | if (ret != 0) |
1461 | goto fail_thread; | |
1462 | ||
3bd31caf | 1463 | spu_config.iThreadAvail = 1; |
63a4f6b6 | 1464 | return; |
1465 | ||
1466 | fail_thread: | |
5514a050 | 1467 | sem_destroy(&t.sem_done); |
63a4f6b6 | 1468 | fail_sem_done: |
5514a050 | 1469 | sem_destroy(&t.sem_avail); |
63a4f6b6 | 1470 | fail_sem_avail: |
1471 | free(worker); | |
1472 | worker = NULL; | |
3bd31caf | 1473 | spu_config.iThreadAvail = 0; |
63a4f6b6 | 1474 | } |
1475 | ||
1476 | static void exit_spu_thread(void) | |
1477 | { | |
1478 | if (worker == NULL) | |
1479 | return; | |
1480 | worker->exit_thread = 1; | |
5514a050 | 1481 | sem_post(&t.sem_avail); |
1482 | pthread_join(t.thread, NULL); | |
1483 | sem_destroy(&t.sem_done); | |
1484 | sem_destroy(&t.sem_avail); | |
63a4f6b6 | 1485 | free(worker); |
1486 | worker = NULL; | |
1487 | } | |
1488 | ||
94f0c7c5 | 1489 | #else // if !P_HAVE_PTHREAD |
63a4f6b6 | 1490 | |
1491 | static void init_spu_thread(void) | |
1492 | { | |
1493 | } | |
1494 | ||
1495 | static void exit_spu_thread(void) | |
1496 | { | |
1497 | } | |
1498 | ||
1499 | #endif | |
ef79bbde P |
1500 | |
1501 | // SPUINIT: this func will be called first by the main emu | |
1502 | long CALLBACK SPUinit(void) | |
1503 | { | |
05c7cec7 | 1504 | int i; |
1505 | ||
9bb93b77 | 1506 | memset(&spu, 0, sizeof(spu)); |
5514a050 | 1507 | spu.spuMemC = calloc(1, 512 * 1024); |
ef79bbde P |
1508 | InitADSR(); |
1509 | ||
5514a050 | 1510 | spu.s_chan = calloc(MAXCHAN+1, sizeof(spu.s_chan[0])); // channel + 1 infos (1 is security for fmod handling) |
05c7cec7 | 1511 | spu.rvb = calloc(1, sizeof(REVERBInfo)); |
de4a0279 | 1512 | spu.SB = calloc(MAXCHAN, sizeof(spu.SB[0]) * SB_SIZE); |
5514a050 | 1513 | |
1514 | spu.spuAddr = 0; | |
3154bfab | 1515 | spu.decode_pos = 0; |
c4c66b22 | 1516 | spu.pSpuIrq = spu.spuMemC; |
ef79bbde | 1517 | |
ef79bbde P |
1518 | SetupStreams(); // prepare streaming |
1519 | ||
3154bfab | 1520 | if (spu_config.iVolume == 0) |
1521 | spu_config.iVolume = 768; // 1024 is 1.0 | |
1522 | ||
63a4f6b6 | 1523 | init_spu_thread(); |
1524 | ||
05c7cec7 | 1525 | for (i = 0; i < MAXCHAN; i++) // loop sound channels |
1526 | { | |
1527 | spu.s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain | |
1528 | spu.s_chan[i].ADSRX.SustainIncrease = 1; | |
1529 | spu.s_chan[i].pLoop = spu.spuMemC; | |
1530 | spu.s_chan[i].pCurr = spu.spuMemC; | |
9601ca6d | 1531 | spu.s_chan[i].bIgnoreLoop = 0; |
05c7cec7 | 1532 | } |
1533 | ||
1534 | spu.bSpuInit=1; // flag: we are inited | |
1535 | ||
ef79bbde P |
1536 | return 0; |
1537 | } | |
1538 | ||
1539 | // SPUOPEN: called by main emu after init | |
1540 | long CALLBACK SPUopen(void) | |
1541 | { | |
3154bfab | 1542 | if (spu.bSPUIsOpen) return 0; // security for some stupid main emus |
ef79bbde P |
1543 | |
1544 | SetupSound(); // setup sound (before init!) | |
ef79bbde | 1545 | |
3154bfab | 1546 | spu.bSPUIsOpen = 1; |
ef79bbde P |
1547 | |
1548 | return PSE_SPU_ERR_SUCCESS; | |
1549 | } | |
1550 | ||
1551 | // SPUCLOSE: called before shutdown | |
1552 | long CALLBACK SPUclose(void) | |
1553 | { | |
3154bfab | 1554 | if (!spu.bSPUIsOpen) return 0; // some security |
ef79bbde | 1555 | |
3154bfab | 1556 | spu.bSPUIsOpen = 0; // no more open |
ef79bbde | 1557 | |
07c13dfd | 1558 | out_current->finish(); // no more sound handling |
ef79bbde P |
1559 | |
1560 | return 0; | |
1561 | } | |
1562 | ||
1563 | // SPUSHUTDOWN: called by main emu on final exit | |
1564 | long CALLBACK SPUshutdown(void) | |
1565 | { | |
1566 | SPUclose(); | |
ef79bbde | 1567 | |
63a4f6b6 | 1568 | exit_spu_thread(); |
1569 | ||
5514a050 | 1570 | free(spu.spuMemC); |
1571 | spu.spuMemC = NULL; | |
de4a0279 | 1572 | free(spu.SB); |
1573 | spu.SB = NULL; | |
5514a050 | 1574 | free(spu.s_chan); |
1575 | spu.s_chan = NULL; | |
05c7cec7 | 1576 | free(spu.rvb); |
1577 | spu.rvb = NULL; | |
5514a050 | 1578 | |
1579 | RemoveStreams(); // no more streaming | |
1580 | spu.bSpuInit=0; | |
1581 | ||
ef79bbde P |
1582 | return 0; |
1583 | } | |
1584 | ||
ef79bbde P |
1585 | // SETUP CALLBACKS |
1586 | // this functions will be called once, | |
1587 | // passes a callback that should be called on SPU-IRQ/cdda volume change | |
1588 | void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void)) | |
1589 | { | |
3154bfab | 1590 | spu.irqCallback = callback; |
ef79bbde P |
1591 | } |
1592 | ||
7d6997de | 1593 | void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(short, short)) |
ef79bbde | 1594 | { |
3154bfab | 1595 | spu.cddavCallback = CDDAVcallback; |
ef79bbde P |
1596 | } |
1597 | ||
2b30c129 | 1598 | void CALLBACK SPUregisterScheduleCb(void (CALLBACK *callback)(unsigned int)) |
1599 | { | |
3154bfab | 1600 | spu.scheduleCallback = callback; |
2b30c129 | 1601 | } |
1602 | ||
ef79bbde | 1603 | // COMMON PLUGIN INFO FUNCS |
e906c010 | 1604 | /* |
ef79bbde P |
1605 | char * CALLBACK PSEgetLibName(void) |
1606 | { | |
1607 | return _(libraryName); | |
1608 | } | |
1609 | ||
1610 | unsigned long CALLBACK PSEgetLibType(void) | |
1611 | { | |
1612 | return PSE_LT_SPU; | |
1613 | } | |
1614 | ||
1615 | unsigned long CALLBACK PSEgetLibVersion(void) | |
1616 | { | |
1617 | return (1 << 16) | (6 << 8); | |
1618 | } | |
1619 | ||
1620 | char * SPUgetLibInfos(void) | |
1621 | { | |
1622 | return _(libraryInfo); | |
1623 | } | |
e906c010 | 1624 | */ |
6d866bb7 | 1625 | |
90f1d26c | 1626 | // debug |
174c454a | 1627 | void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out) |
90f1d26c | 1628 | { |
174c454a | 1629 | int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0; |
90f1d26c | 1630 | |
5514a050 | 1631 | if (spu.s_chan == NULL) |
1632 | return; | |
1633 | ||
90f1d26c | 1634 | for(;ch<MAXCHAN;ch++) |
1635 | { | |
2daa7c1d | 1636 | if (!(spu.dwChannelsAudible & (1<<ch))) |
90f1d26c | 1637 | continue; |
5514a050 | 1638 | if (spu.s_chan[ch].bFMod == 2) |
90f1d26c | 1639 | fmod_chans |= 1 << ch; |
5514a050 | 1640 | if (spu.s_chan[ch].bNoise) |
90f1d26c | 1641 | noise_chans |= 1 << ch; |
5514a050 | 1642 | if((spu.spuCtrl&CTRL_IRQ) && spu.s_chan[ch].pCurr <= spu.pSpuIrq && spu.s_chan[ch].pLoop <= spu.pSpuIrq) |
174c454a | 1643 | irq_chans |= 1 << ch; |
90f1d26c | 1644 | } |
1645 | ||
2daa7c1d | 1646 | *chans_out = spu.dwChannelsAudible; |
1647 | *run_chans = ~spu.dwChannelsAudible & ~spu.dwChannelDead & irq_chans; | |
90f1d26c | 1648 | *fmod_chans_out = fmod_chans; |
1649 | *noise_chans_out = noise_chans; | |
1650 | } | |
1651 | ||
6d866bb7 | 1652 | // vim:shiftwidth=1:expandtab |