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