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