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