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