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