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