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