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