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