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