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