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