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