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