spu: get rid of iSpuAsyncWait hack
[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
8 Portions (C) GraÅžvydas "notaz" Ignotas, 2010-2011
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"
ef79bbde 27#include "dsoundoss.h"
ef79bbde
P
28
29#ifdef ENABLE_NLS
30#include <libintl.h>
31#include <locale.h>
32#define _(x) gettext(x)
33#define N_(x) (x)
34#else
35#define _(x) (x)
36#define N_(x) (x)
37#endif
38
a80ae4a0 39#ifdef __ARM_ARCH_7A__
1775933a 40 #define ssat32_to_16(v) \
41 asm("ssat %0,#16,%1" : "=r" (v) : "r" (v))
42#else
43 #define ssat32_to_16(v) do { \
44 if (v < -32768) v = -32768; \
45 else if (v > 32767) v = 32767; \
46 } while (0)
47#endif
48
fb552464 49#define PSXCLK 33868800 /* 33.8688 MHz */
50
6d866bb7 51/*
ef79bbde
P
52#if defined (USEMACOSX)
53static char * libraryName = N_("Mac OS X Sound");
54#elif defined (USEALSA)
55static char * libraryName = N_("ALSA Sound");
56#elif defined (USEOSS)
57static char * libraryName = N_("OSS Sound");
58#elif defined (USESDL)
59static char * libraryName = N_("SDL Sound");
60#elif defined (USEPULSEAUDIO)
61static char * libraryName = N_("PulseAudio Sound");
62#else
63static char * libraryName = N_("NULL Sound");
64#endif
65
66static char * libraryInfo = N_("P.E.Op.S. Sound Driver V1.7\nCoded by Pete Bernert and the P.E.Op.S. team\n");
6d866bb7 67*/
ef79bbde
P
68
69// globals
70
71// psx buffer / addresses
72
73unsigned short regArea[10000];
74unsigned short spuMem[256*1024];
75unsigned char * spuMemC;
76unsigned char * pSpuIrq=0;
77unsigned char * pSpuBuffer;
78unsigned char * pMixIrq=0;
79
80// user settings
81
9e7a7352 82int iVolume=768; // 1024 is 1.0
ef79bbde 83int iXAPitch=1;
ef79bbde
P
84int iSPUIRQWait=1;
85int iDebugMode=0;
86int iRecordMode=0;
87int iUseReverb=2;
88int iUseInterpolation=2;
ef79bbde
P
89
90// MAIN infos struct for each channel
91
92SPUCHAN s_chan[MAXCHAN+1]; // channel + 1 infos (1 is security for fmod handling)
93REVERBInfo rvb;
94
b1094d0e 95unsigned int dwNoiseVal; // global noise generator
96unsigned int dwNoiseCount;
ef79bbde
P
97
98unsigned short spuCtrl=0; // some vars to store psx reg infos
99unsigned short spuStat=0;
100unsigned short spuIrq=0;
101unsigned long spuAddr=0xffffffff; // address into spu mem
ef79bbde
P
102int bSpuInit=0;
103int bSPUIsOpen=0;
104
174c454a 105unsigned int dwNewChannel=0; // flags for faster testing, if new channel starts
106unsigned int dwChannelOn=0; // not silent channels
107unsigned int dwPendingChanOff=0;
108unsigned int dwChannelDead=0; // silent+not useful channels
ef79bbde
P
109
110void (CALLBACK *irqCallback)(void)=0; // func of main emu, called on spu irq
111void (CALLBACK *cddavCallback)(unsigned short,unsigned short)=0;
112
113// certain globals (were local before, but with the new timeproc I need em global)
114
6d866bb7 115static const int f[8][2] = { { 0, 0 },
ef79bbde
P
116 { 60, 0 },
117 { 115, -52 },
118 { 98, -55 },
119 { 122, -60 } };
b17618c0 120int ChanBuf[NSSIZE+3];
121int SSumLR[(NSSIZE+3)*2];
ef79bbde
P
122int iFMod[NSSIZE];
123int iCycle = 0;
124short * pS;
125
fb552464 126int had_dma;
ef79bbde
P
127int lastch=-1; // last channel processed on spu irq in timer mode
128static int lastns=0; // last ns pos
fb552464 129static int cycles_since_update;
ef79bbde 130
983a7cfd 131#define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2
132
ef79bbde
P
133////////////////////////////////////////////////////////////////////////
134// CODE AREA
135////////////////////////////////////////////////////////////////////////
136
137// dirty inline func includes
138
139#include "reverb.c"
140#include "adsr.c"
141
142////////////////////////////////////////////////////////////////////////
143// helpers for simple interpolation
144
145//
146// easy interpolation on upsampling, no special filter, just "Pete's common sense" tm
147//
148// instead of having n equal sample values in a row like:
149// ____
150// |____
151//
152// we compare the current delta change with the next delta change.
153//
154// if curr_delta is positive,
155//
156// - and next delta is smaller (or changing direction):
157// \.
158// -__
159//
160// - and next delta significant (at least twice) bigger:
161// --_
162// \.
163//
164// - and next delta is nearly same:
165// \.
166// \.
167//
168//
169// if curr_delta is negative,
170//
171// - and next delta is smaller (or changing direction):
172// _--
173// /
174//
175// - and next delta significant (at least twice) bigger:
176// /
177// __-
178//
179// - and next delta is nearly same:
180// /
181// /
182//
183
184
185INLINE void InterpolateUp(int ch)
186{
187 if(s_chan[ch].SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass
188 {
189 const int id1=s_chan[ch].SB[30]-s_chan[ch].SB[29]; // curr delta to next val
190 const int id2=s_chan[ch].SB[31]-s_chan[ch].SB[30]; // and next delta to next-next val :)
191
192 s_chan[ch].SB[32]=0;
193
194 if(id1>0) // curr delta positive
195 {
196 if(id2<id1)
197 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
198 else
199 if(id2<(id1<<1))
200 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
201 else
202 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
203 }
204 else // curr delta negative
205 {
206 if(id2>id1)
207 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
208 else
209 if(id2>(id1<<1))
210 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
211 else
212 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
213 }
214 }
215 else
216 if(s_chan[ch].SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass
217 {
218 s_chan[ch].SB[32]=0;
219
220 s_chan[ch].SB[28]=(s_chan[ch].SB[28]*s_chan[ch].sinc)/0x20000L;
76d1d09c 221 //if(s_chan[ch].sinc<=0x8000)
222 // s_chan[ch].SB[29]=s_chan[ch].SB[30]-(s_chan[ch].SB[28]*((0x10000/s_chan[ch].sinc)-1));
223 //else
224 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
ef79bbde
P
225 }
226 else // no flags? add bigger val (if possible), calc smaller step, set flag1
227 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
228}
229
230//
231// even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm
232//
233
234INLINE void InterpolateDown(int ch)
235{
236 if(s_chan[ch].sinc>=0x20000L) // we would skip at least one val?
237 {
238 s_chan[ch].SB[29]+=(s_chan[ch].SB[30]-s_chan[ch].SB[29])/2; // add easy weight
239 if(s_chan[ch].sinc>=0x30000L) // we would skip even more vals?
240 s_chan[ch].SB[29]+=(s_chan[ch].SB[31]-s_chan[ch].SB[30])/2;// add additional next weight
241 }
242}
243
244////////////////////////////////////////////////////////////////////////
245// helpers for gauss interpolation
246
247#define gval0 (((short*)(&s_chan[ch].SB[29]))[gpos])
248#define gval(x) (((short*)(&s_chan[ch].SB[29]))[(gpos+x)&3])
249
250#include "gauss_i.h"
251
252////////////////////////////////////////////////////////////////////////
253
254#include "xa.c"
255
256////////////////////////////////////////////////////////////////////////
257// START SOUND... called by main thread to setup a new sound on a channel
258////////////////////////////////////////////////////////////////////////
259
260INLINE void StartSound(int ch)
261{
262 StartADSR(ch);
263 StartREVERB(ch);
264
b00afb77 265 // fussy timing issues - do in VoiceOn
266 //s_chan[ch].pCurr=s_chan[ch].pStart; // set sample start
267 //s_chan[ch].bStop=0;
268 //s_chan[ch].bOn=1;
ef79bbde 269
381ea103 270 s_chan[ch].SB[26]=0; // init mixing vars
271 s_chan[ch].SB[27]=0;
ef79bbde
P
272 s_chan[ch].iSBPos=28;
273
ef79bbde
P
274 s_chan[ch].SB[29]=0; // init our interpolation helpers
275 s_chan[ch].SB[30]=0;
276
277 if(iUseInterpolation>=2) // gauss interpolation?
278 {s_chan[ch].spos=0x30000L;s_chan[ch].SB[28]=0;} // -> start with more decoding
279 else {s_chan[ch].spos=0x10000L;s_chan[ch].SB[31]=0;} // -> no/simple interpolation starts with one 44100 decoding
280
281 dwNewChannel&=~(1<<ch); // clear new channel bit
282}
283
284////////////////////////////////////////////////////////////////////////
285// ALL KIND OF HELPERS
286////////////////////////////////////////////////////////////////////////
287
07a6dd2c 288INLINE int FModChangeFrequency(int ch,int ns)
ef79bbde 289{
7e44d49d 290 unsigned int NP=s_chan[ch].iRawPitch;
07a6dd2c 291 int sinc;
ef79bbde
P
292
293 NP=((32768L+iFMod[ns])*NP)/32768L;
294
295 if(NP>0x3fff) NP=0x3fff;
296 if(NP<0x1) NP=0x1;
297
7e44d49d 298 sinc=NP<<4; // calc frequency
ef79bbde 299 if(iUseInterpolation==1) // freq change in simple interpolation mode
07a6dd2c 300 s_chan[ch].SB[32]=1;
ef79bbde 301 iFMod[ns]=0;
07a6dd2c 302
303 return sinc;
ef79bbde
P
304}
305
306////////////////////////////////////////////////////////////////////////
307
ef79bbde
P
308INLINE void StoreInterpolationVal(int ch,int fa)
309{
310 if(s_chan[ch].bFMod==2) // fmod freq channel
311 s_chan[ch].SB[29]=fa;
312 else
313 {
381ea103 314 ssat32_to_16(fa);
ef79bbde
P
315
316 if(iUseInterpolation>=2) // gauss/cubic interpolation
317 {
318 int gpos = s_chan[ch].SB[28];
319 gval0 = fa;
320 gpos = (gpos+1) & 3;
321 s_chan[ch].SB[28] = gpos;
322 }
323 else
324 if(iUseInterpolation==1) // simple interpolation
325 {
326 s_chan[ch].SB[28] = 0;
327 s_chan[ch].SB[29] = s_chan[ch].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 s_chan[ch].SB[30] = s_chan[ch].SB[31];
329 s_chan[ch].SB[31] = fa;
330 s_chan[ch].SB[32] = 1; // -> flag: calc new interolation
331 }
332 else s_chan[ch].SB[29]=fa; // no interpolation
333 }
334}
335
336////////////////////////////////////////////////////////////////////////
337
8cd23d6d 338INLINE int iGetInterpolationVal(int ch, int spos)
ef79bbde
P
339{
340 int fa;
341
342 if(s_chan[ch].bFMod==2) return s_chan[ch].SB[29];
343
344 switch(iUseInterpolation)
345 {
346 //--------------------------------------------------//
347 case 3: // cubic interpolation
348 {
349 long xd;int gpos;
8cd23d6d 350 xd = (spos >> 1)+1;
ef79bbde
P
351 gpos = s_chan[ch].SB[28];
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;
ef79bbde
P
370 gpos = s_chan[ch].SB[28];
371 vr=(gauss[vl]*gval0)&~2047;
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 {
380 if(s_chan[ch].sinc<0x10000L) // -> upsampling?
381 InterpolateUp(ch); // --> interpolate up
382 else InterpolateDown(ch); // --> else down
383 fa=s_chan[ch].SB[29];
384 } break;
385 //--------------------------------------------------//
386 default: // no interpolation
387 {
388 fa=s_chan[ch].SB[29];
389 } break;
390 //--------------------------------------------------//
391 }
392
393 return fa;
394}
395
3fc2a4c2 396static void do_irq(void)
397{
8048a6d7 398 //if(!(spuStat & STAT_IRQ))
3fc2a4c2 399 {
8048a6d7 400 spuStat |= STAT_IRQ; // asserted status?
3fc2a4c2 401 if(irqCallback) irqCallback();
402 }
403}
404
381ea103 405static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor)
406{
407 int nSample;
408 int fa, s_1, s_2, d, s;
409
410 s_1 = dest[27];
411 s_2 = dest[26];
412
413 for (nSample = 0; nSample < 28; src++)
414 {
415 d = (int)*src;
416 s = (int)(signed short)((d & 0x0f) << 12);
417
418 fa = s >> shift_factor;
419 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
420 s_2=s_1;s_1=fa;
421
422 dest[nSample++] = fa;
423
424 s = (int)(signed short)((d & 0xf0) << 8);
425 fa = s >> shift_factor;
426 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
427 s_2=s_1;s_1=fa;
428
429 dest[nSample++] = fa;
430 }
431}
432
e11ae5c5 433static int decode_block(int ch)
434{
435 unsigned char *start;
381ea103 436 int predict_nr,shift_factor,flags;
e11ae5c5 437 int ret = 0;
438
e11ae5c5 439 start=s_chan[ch].pCurr; // set up the current pos
174c454a 440 if(dwPendingChanOff&(1<<ch))
e11ae5c5 441 {
442 dwChannelOn&=~(1<<ch); // -> turn everything off
3fc2a4c2 443 dwPendingChanOff&=~(1<<ch);
e11ae5c5 444 s_chan[ch].bStop=1;
445 s_chan[ch].ADSRX.EnvelopeVol=0;
e11ae5c5 446 }
447
3fc2a4c2 448 //////////////////////////////////////////// irq check
449
450 if(spuCtrl&CTRL_IRQ)
451 {
452 if(pSpuIrq == start) // irq address reached?
453 {
454 do_irq(); // -> call main emu
455 ret = 1;
456 }
457 }
e11ae5c5 458
381ea103 459 predict_nr=(int)start[0];
e11ae5c5 460 shift_factor=predict_nr&0xf;
461 predict_nr >>= 4;
e11ae5c5 462
381ea103 463 decode_block_data(s_chan[ch].SB, start + 2, predict_nr, shift_factor);
e11ae5c5 464
e11ae5c5 465 //////////////////////////////////////////// flag handler
466
381ea103 467 flags=(int)start[1];
468 if(flags&4)
469 s_chan[ch].pLoop=start; // loop adress
e11ae5c5 470
381ea103 471 start+=16;
e11ae5c5 472 if(flags&1) // 1: stop/loop
473 {
3fc2a4c2 474 if(!(flags&2))
475 dwPendingChanOff|=1<<ch;
476
477 start = s_chan[ch].pLoop;
e11ae5c5 478 }
479
174c454a 480 if (start - spuMemC >= 0x80000) {
481 // most likely wrong
482 start = spuMemC;
483 printf("ch%d oflow\n", ch);
484 }
e11ae5c5 485
381ea103 486 s_chan[ch].pCurr = start; // store values for next cycle
be1cb678 487 s_chan[ch].bJump = flags & 1;
e11ae5c5 488
489 return ret;
490}
491
07a6dd2c 492// do block, but ignore sample data
493static int skip_block(int ch)
494{
495 unsigned char *start = s_chan[ch].pCurr;
496 int flags = start[1];
497 int ret = 0;
498
07a6dd2c 499 if(start == pSpuIrq)
500 {
501 do_irq();
502 ret = 1;
503 }
504
381ea103 505 if(flags & 4)
be1cb678 506 s_chan[ch].pLoop = start;
07a6dd2c 507
508 s_chan[ch].pCurr += 16;
509
510 if(flags & 1)
511 s_chan[ch].pCurr = s_chan[ch].pLoop;
512
be1cb678 513 s_chan[ch].bJump = flags & 1;
07a6dd2c 514 return ret;
515}
516
517#define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
518static int do_samples_##name(int ch, int ns, int ns_to) \
519{ \
520 int sinc = s_chan[ch].sinc; \
521 int spos = s_chan[ch].spos; \
381ea103 522 int sbpos = s_chan[ch].iSBPos; \
523 int *SB = s_chan[ch].SB; \
07a6dd2c 524 int ret = -1; \
525 int d, fa; \
526 interp_start; \
527 \
528 for (; ns < ns_to; ns++) \
529 { \
530 fmod_code; \
531 \
532 while (spos >= 0x10000) \
533 { \
381ea103 534 if(sbpos == 28) \
07a6dd2c 535 { \
381ea103 536 sbpos = 0; \
07a6dd2c 537 d = decode_block(ch); \
538 if(d && iSPUIRQWait) \
539 { \
540 ret = ns; \
541 goto out; \
542 } \
543 } \
544 \
381ea103 545 fa = SB[sbpos++]; \
07a6dd2c 546 interp1_code; \
547 spos -= 0x10000; \
548 } \
549 \
550 interp2_code; \
551 spos += sinc; \
552 } \
553 \
554out: \
555 s_chan[ch].sinc = sinc; \
556 s_chan[ch].spos = spos; \
381ea103 557 s_chan[ch].iSBPos = sbpos; \
07a6dd2c 558 interp_end; \
559 \
560 return ret; \
561}
562
563#define fmod_recv_check \
564 if(s_chan[ch].bFMod==1 && iFMod[ns]) \
565 sinc = FModChangeFrequency(ch,ns)
566
567make_do_samples(default, fmod_recv_check, ,
568 StoreInterpolationVal(ch, fa),
8cd23d6d 569 ChanBuf[ns] = iGetInterpolationVal(ch, spos), )
07a6dd2c 570make_do_samples(noint, , fa = s_chan[ch].SB[29], , ChanBuf[ns] = fa, s_chan[ch].SB[29] = fa)
571
572#define simple_interp_store \
573 s_chan[ch].SB[28] = 0; \
574 s_chan[ch].SB[29] = s_chan[ch].SB[30]; \
575 s_chan[ch].SB[30] = s_chan[ch].SB[31]; \
576 s_chan[ch].SB[31] = fa; \
577 s_chan[ch].SB[32] = 1
578
579#define simple_interp_get \
580 if(sinc<0x10000) /* -> upsampling? */ \
581 InterpolateUp(ch); /* --> interpolate up */ \
582 else InterpolateDown(ch); /* --> else down */ \
583 ChanBuf[ns] = s_chan[ch].SB[29]
584
585make_do_samples(simple, , ,
586 simple_interp_store, simple_interp_get, )
587
588static int do_samples_noise(int ch, int ns, int ns_to)
589{
b1094d0e 590 int level, shift, bit;
591
07a6dd2c 592 s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns);
593 while (s_chan[ch].spos >= 28*0x10000)
594 {
595 skip_block(ch);
596 s_chan[ch].spos -= 28*0x10000;
597 }
598
b1094d0e 599 // modified from DrHell/shalma, no fraction
600 level = (spuCtrl >> 10) & 0x0f;
601 level = 0x8000 >> level;
602
07a6dd2c 603 for (; ns < ns_to; ns++)
b1094d0e 604 {
605 dwNoiseCount += 2;
606 if (dwNoiseCount >= level)
607 {
608 dwNoiseCount -= level;
609 shift = (dwNoiseVal >> 10) & 0x1f;
610 bit = (0x69696969 >> shift) & 1;
611 if (dwNoiseVal & 0x8000)
612 bit ^= 1;
613 dwNoiseVal = (dwNoiseVal << 1) | bit;
614 }
615
616 ChanBuf[ns] = (signed short)dwNoiseVal;
617 }
07a6dd2c 618
619 return -1;
620}
621
3a721c1f 622#ifdef __arm__
623// asm code; lv and rv must be 0-3fff
b17618c0 624extern void mix_chan(int start, int count, int lv, int rv);
625extern void mix_chan_rvb(int start, int count, int lv, int rv);
626#else
627static void mix_chan(int start, int count, int lv, int rv)
628{
629 int *dst = SSumLR + start * 2;
630 const int *src = ChanBuf + start;
631 int l, r;
632
633 while (count--)
634 {
635 int sval = *src++;
636
637 l = (sval * lv) >> 14;
638 r = (sval * rv) >> 14;
639 *dst++ += l;
640 *dst++ += r;
641 }
642}
643
644static void mix_chan_rvb(int start, int count, int lv, int rv)
645{
646 int *dst = SSumLR + start * 2;
647 int *drvb = sRVBStart + start * 2;
648 const int *src = ChanBuf + start;
649 int l, r;
650
651 while (count--)
652 {
653 int sval = *src++;
654
655 l = (sval * lv) >> 14;
656 r = (sval * rv) >> 14;
657 *dst++ += l;
658 *dst++ += r;
659 *drvb++ += l;
660 *drvb++ += r;
661 }
662}
663#endif
664
ef79bbde
P
665////////////////////////////////////////////////////////////////////////
666// MAIN SPU FUNCTION
6d75977b 667// here is the main job handler...
ef79bbde
P
668// basically the whole sound processing is done in this fat func!
669////////////////////////////////////////////////////////////////////////
670
fb552464 671static int do_samples(int forced_updates)
ef79bbde 672{
9e7a7352 673 int volmult = iVolume;
07a6dd2c 674 int ns,ns_from,ns_to;
174c454a 675 int ch,d,silentch;
ef79bbde
P
676 int bIRQReturn=0;
677
6d75977b 678 while(1)
ef79bbde
P
679 {
680 // ok, at the beginning we are looking if there is
681 // enuff free place in the dsound/oss buffer to
682 // fill in new data, or if there is a new channel to start.
683 // if not, we wait (thread) or return (timer/spuasync)
684 // until enuff free place is available/a new channel gets
685 // started
686
fb552464 687 if(!forced_updates && SoundGetBytesBuffered()) // still enuff data in sound buffer?
ef79bbde 688 {
6d75977b 689 return 0;
ef79bbde
P
690 }
691
fb552464 692 cycles_since_update = 0;
693 if(forced_updates > 0)
694 forced_updates--;
695
ef79bbde
P
696 //--------------------------------------------------// continue from irq handling in timer mode?
697
8680e822 698 ns_from=0;
699 ns_to=NSSIZE;
700 ch=0;
ef79bbde
P
701 if(lastch>=0) // will be -1 if no continue is pending
702 {
e11ae5c5 703 ch=lastch; ns_from=lastns; lastch=-1; // -> setup all kind of vars to continue
ef79bbde
P
704 }
705
174c454a 706 silentch=~(dwChannelOn|dwNewChannel);
707
ef79bbde
P
708 //--------------------------------------------------//
709 //- main channel loop -//
710 //--------------------------------------------------//
711 {
8680e822 712 for(;ch<MAXCHAN;ch++) // loop em all... we will collect 1 ms of sound of each playing channel
ef79bbde 713 {
6d866bb7 714 if(dwNewChannel&(1<<ch)) StartSound(ch); // start new sound
715 if(!(dwChannelOn&(1<<ch))) continue; // channel not playing? next
ef79bbde 716
07a6dd2c 717 if(s_chan[ch].bNoise)
718 d=do_samples_noise(ch, ns_from, ns_to);
719 else if(s_chan[ch].bFMod==2 || (s_chan[ch].bFMod==0 && iUseInterpolation==0))
720 d=do_samples_noint(ch, ns_from, ns_to);
721 else if(s_chan[ch].bFMod==0 && iUseInterpolation==1)
722 d=do_samples_simple(ch, ns_from, ns_to);
723 else
724 d=do_samples_default(ch, ns_from, ns_to);
725 if(d>=0)
ef79bbde 726 {
07a6dd2c 727 bIRQReturn=1;
728 lastch=ch;
729 lastns=ns_to=d;
587fa7de 730 if(d==0)
731 break;
1ab7621a 732 }
ef79bbde 733
1ab7621a 734 MixADSR(ch, ns_from, ns_to);
735
736 if(s_chan[ch].bFMod==2) // fmod freq channel
737 memcpy(iFMod, ChanBuf, sizeof(iFMod));
b17618c0 738 else if(s_chan[ch].bRVBActive)
739 mix_chan_rvb(ns_from,ns_to-ns_from,s_chan[ch].iLeftVolume,s_chan[ch].iRightVolume);
1775933a 740 else
b17618c0 741 mix_chan(ns_from,ns_to-ns_from,s_chan[ch].iLeftVolume,s_chan[ch].iRightVolume);
ef79bbde
P
742 }
743 }
744
78c60846 745 // advance "stopped" channels that can cause irqs
746 // (all chans are always playing on the real thing..)
747 if(!bIRQReturn && (spuCtrl&CTRL_IRQ))
748 for(ch=0;ch<MAXCHAN;ch++)
749 {
174c454a 750 if(!(silentch&(1<<ch))) continue; // already handled
751 if(dwChannelDead&(1<<ch)) continue;
78c60846 752 if(s_chan[ch].pCurr > pSpuIrq && s_chan[ch].pLoop > pSpuIrq)
753 continue;
754
78c60846 755 s_chan[ch].spos += s_chan[ch].sinc * NSSIZE;
756 while(s_chan[ch].spos >= 28 * 0x10000)
757 {
7e44d49d 758 unsigned char *start = s_chan[ch].pCurr;
78c60846 759
a3ed1915 760 // no need for bIRQReturn since the channel is silent
fb552464 761 skip_block(ch);
07a6dd2c 762 if(start == s_chan[ch].pCurr)
78c60846 763 {
764 // looping on self
7e44d49d 765 dwChannelDead |= 1<<ch;
766 s_chan[ch].spos = 0;
78c60846 767 break;
768 }
769
78c60846 770 s_chan[ch].spos -= 28 * 0x10000;
771 }
772 }
773
e11ae5c5 774 if(bIRQReturn && iSPUIRQWait) // special return for "spu irq - wait for cpu action"
6d75977b 775 return 0;
8680e822 776
777
ef79bbde
P
778 //---------------------------------------------------//
779 //- here we have another 1 ms of sound data
780 //---------------------------------------------------//
781 // mix XA infos (if any)
782
783 MixXA();
784
785 ///////////////////////////////////////////////////////
786 // mix all channels (including reverb) into one buffer
787
1775933a 788 if(iUseReverb)
789 REVERBDo();
790
791 if((spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this)
792 {
793 memset(pS, 0, NSSIZE * 2 * sizeof(pS[0]));
794 pS += NSSIZE*2;
795 }
796 else
97ea4077 797 for (ns = 0; ns < NSSIZE*2; )
ef79bbde 798 {
9e7a7352 799 d = SSumLR[ns]; SSumLR[ns] = 0;
800 d = d * volmult >> 10;
1775933a 801 ssat32_to_16(d);
ef79bbde 802 *pS++ = d;
97ea4077 803 ns++;
ef79bbde 804
9e7a7352 805 d = SSumLR[ns]; SSumLR[ns] = 0;
806 d = d * volmult >> 10;
1775933a 807 ssat32_to_16(d);
ef79bbde 808 *pS++ = d;
97ea4077 809 ns++;
ef79bbde
P
810 }
811
812 //////////////////////////////////////////////////////
813 // special irq handling in the decode buffers (0x0000-0x1000)
814 // we know:
815 // the decode buffers are located in spu memory in the following way:
816 // 0x0000-0x03ff CD audio left
817 // 0x0400-0x07ff CD audio right
818 // 0x0800-0x0bff Voice 1
819 // 0x0c00-0x0fff Voice 3
820 // and decoded data is 16 bit for one sample
821 // we assume:
822 // even if voices 1/3 are off or no cd audio is playing, the internal
823 // play positions will move on and wrap after 0x400 bytes.
824 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
825 // increase this pointer on each sample by 2 bytes. If this pointer
826 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
827 // an IRQ. Only problem: the "wait for cpu" option is kinda hard to do here
828 // in some of Peops timer modes. So: we ignore this option here (for now).
829
3fc2a4c2 830 if(pMixIrq)
ef79bbde
P
831 {
832 for(ns=0;ns<NSSIZE;ns++)
833 {
834 if((spuCtrl&0x40) && pSpuIrq && pSpuIrq<spuMemC+0x1000)
835 {
836 for(ch=0;ch<4;ch++)
837 {
838 if(pSpuIrq>=pMixIrq+(ch*0x400) && pSpuIrq<pMixIrq+(ch*0x400)+2)
3fc2a4c2 839 do_irq();
ef79bbde
P
840 }
841 }
842 pMixIrq+=2;if(pMixIrq>spuMemC+0x3ff) pMixIrq=spuMemC;
843 }
844 }
845
846 InitREVERB();
847
848 // feed the sound
849 // wanna have around 1/60 sec (16.666 ms) updates
381ea103 850 if (iCycle++ > 16/FRAG_MSECS)
ef79bbde
P
851 {
852 SoundFeedStreamData((unsigned char *)pSpuBuffer,
853 ((unsigned char *)pS) - ((unsigned char *)pSpuBuffer));
854 pS = (short *)pSpuBuffer;
855 iCycle = 0;
856 }
857 }
858
ef79bbde
P
859 return 0;
860}
861
862// SPU ASYNC... even newer epsxe func
863// 1 time every 'cycle' cycles... harhar
864
554a2220 865// rearmed: called every 2ms now
866
ef79bbde
P
867void CALLBACK SPUasync(unsigned long cycle)
868{
fb552464 869 int forced_updates = 0;
870 int do_update = 0;
871
f8edb5bc 872 if(!bSpuInit) return; // -> no init, no call
873
fb552464 874 cycles_since_update += cycle;
875
876 if(dwNewChannel || had_dma)
ef79bbde 877 {
fb552464 878 forced_updates = 1;
879 do_update = 1;
880 had_dma = 0;
ef79bbde
P
881 }
882
fb552464 883 // once per frame should be fine (using a bit more because of BIAS)
884 if(cycles_since_update > PSXCLK/60 * 5/4)
885 do_update = 1;
886
887 if(do_update)
888 do_samples(forced_updates);
ef79bbde
P
889}
890
891// SPU UPDATE... new epsxe func
892// 1 time every 32 hsync lines
893// (312/32)x50 in pal
894// (262/32)x60 in ntsc
895
896// since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
897// leave that func in the linux port, until epsxe linux is using
898// the async function as well
899
900void CALLBACK SPUupdate(void)
901{
902 SPUasync(0);
903}
904
905// XA AUDIO
906
907void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap)
908{
909 if(!xap) return;
910 if(!xap->freq) return; // no xa freq ? bye
911
912 FeedXA(xap); // call main XA feeder
913}
914
915// CDDA AUDIO
983a7cfd 916int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
ef79bbde 917{
983a7cfd 918 if (!pcm) return -1;
919 if (nbytes<=0) return -1;
ef79bbde 920
983a7cfd 921 return FeedCDDA((unsigned char *)pcm, nbytes);
ef79bbde
P
922}
923
6d75977b 924// to be called after state load
925void ClearWorkingState(void)
ef79bbde 926{
97ea4077 927 memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers
6d75977b 928 memset(iFMod,0,sizeof(iFMod));
ef79bbde 929 pS=(short *)pSpuBuffer; // setup soundbuffer pointer
ef79bbde
P
930}
931
932// SETUPSTREAMS: init most of the spu buffers
933void SetupStreams(void)
934{
935 int i;
936
937 pSpuBuffer=(unsigned char *)malloc(32768); // alloc mixing buffer
938
939 if(iUseReverb==1) i=88200*2;
940 else i=NSSIZE*2;
941
942 sRVBStart = (int *)malloc(i*4); // alloc reverb buffer
943 memset(sRVBStart,0,i*4);
944 sRVBEnd = sRVBStart + i;
945 sRVBPlay = sRVBStart;
946
947 XAStart = // alloc xa buffer
948 (uint32_t *)malloc(44100 * sizeof(uint32_t));
949 XAEnd = XAStart + 44100;
950 XAPlay = XAStart;
951 XAFeed = XAStart;
952
953 CDDAStart = // alloc cdda buffer
983a7cfd 954 (uint32_t *)malloc(CDDA_BUFFER_SIZE);
ef79bbde
P
955 CDDAEnd = CDDAStart + 16384;
956 CDDAPlay = CDDAStart;
b8e744ce 957 CDDAFeed = CDDAStart;
ef79bbde
P
958
959 for(i=0;i<MAXCHAN;i++) // loop sound channels
960 {
961// we don't use mutex sync... not needed, would only
962// slow us down:
963// s_chan[i].hMutex=CreateMutex(NULL,FALSE,NULL);
6d866bb7 964 s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
ef79bbde 965 s_chan[i].pLoop=spuMemC;
ef79bbde
P
966 s_chan[i].pCurr=spuMemC;
967 }
968
6d75977b 969 pMixIrq=spuMemC; // enable decoded buffer irqs by setting the address
970
971 ClearWorkingState();
972
973 bSpuInit=1; // flag: we are inited
ef79bbde
P
974}
975
976// REMOVESTREAMS: free most buffer
977void RemoveStreams(void)
978{
979 free(pSpuBuffer); // free mixing buffer
980 pSpuBuffer = NULL;
981 free(sRVBStart); // free reverb buffer
982 sRVBStart = NULL;
983 free(XAStart); // free XA buffer
984 XAStart = NULL;
985 free(CDDAStart); // free CDDA buffer
986 CDDAStart = NULL;
987}
988
989// INIT/EXIT STUFF
990
991// SPUINIT: this func will be called first by the main emu
992long CALLBACK SPUinit(void)
993{
994 spuMemC = (unsigned char *)spuMem; // just small setup
995 memset((void *)&rvb, 0, sizeof(REVERBInfo));
996 InitADSR();
997
ef79bbde
P
998 spuIrq = 0;
999 spuAddr = 0xffffffff;
ef79bbde
P
1000 spuMemC = (unsigned char *)spuMem;
1001 pMixIrq = 0;
1002 memset((void *)s_chan, 0, (MAXCHAN + 1) * sizeof(SPUCHAN));
1003 pSpuIrq = 0;
cdb31c95 1004 //iSPUIRQWait = 0;
ef79bbde
P
1005 lastch = -1;
1006
ef79bbde
P
1007 SetupStreams(); // prepare streaming
1008
1009 return 0;
1010}
1011
1012// SPUOPEN: called by main emu after init
1013long CALLBACK SPUopen(void)
1014{
1015 if (bSPUIsOpen) return 0; // security for some stupid main emus
1016
1017 SetupSound(); // setup sound (before init!)
ef79bbde
P
1018
1019 bSPUIsOpen = 1;
1020
1021 return PSE_SPU_ERR_SUCCESS;
1022}
1023
1024// SPUCLOSE: called before shutdown
1025long CALLBACK SPUclose(void)
1026{
1027 if (!bSPUIsOpen) return 0; // some security
1028
1029 bSPUIsOpen = 0; // no more open
1030
ef79bbde
P
1031 RemoveSound(); // no more sound handling
1032
1033 return 0;
1034}
1035
1036// SPUSHUTDOWN: called by main emu on final exit
1037long CALLBACK SPUshutdown(void)
1038{
1039 SPUclose();
1040 RemoveStreams(); // no more streaming
6d75977b 1041 bSpuInit=0;
ef79bbde
P
1042
1043 return 0;
1044}
1045
1046// SPUTEST: we don't test, we are always fine ;)
1047long CALLBACK SPUtest(void)
1048{
1049 return 0;
1050}
1051
1052// SPUCONFIGURE: call config dialog
1053long CALLBACK SPUconfigure(void)
1054{
1055#ifdef _MACOSX
1056 DoConfiguration();
1057#else
ee849648 1058// StartCfgTool("CFG");
ef79bbde
P
1059#endif
1060 return 0;
1061}
1062
1063// SPUABOUT: show about window
1064void CALLBACK SPUabout(void)
1065{
1066#ifdef _MACOSX
1067 DoAbout();
1068#else
ee849648 1069// StartCfgTool("ABOUT");
ef79bbde
P
1070#endif
1071}
1072
1073// SETUP CALLBACKS
1074// this functions will be called once,
1075// passes a callback that should be called on SPU-IRQ/cdda volume change
1076void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1077{
1078 irqCallback = callback;
1079}
1080
1081void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1082{
1083 cddavCallback = CDDAVcallback;
1084}
1085
1086// COMMON PLUGIN INFO FUNCS
e906c010 1087/*
ef79bbde
P
1088char * CALLBACK PSEgetLibName(void)
1089{
1090 return _(libraryName);
1091}
1092
1093unsigned long CALLBACK PSEgetLibType(void)
1094{
1095 return PSE_LT_SPU;
1096}
1097
1098unsigned long CALLBACK PSEgetLibVersion(void)
1099{
1100 return (1 << 16) | (6 << 8);
1101}
1102
1103char * SPUgetLibInfos(void)
1104{
1105 return _(libraryInfo);
1106}
e906c010 1107*/
6d866bb7 1108
90f1d26c 1109// debug
174c454a 1110void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
90f1d26c 1111{
174c454a 1112 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
90f1d26c 1113
1114 for(;ch<MAXCHAN;ch++)
1115 {
1116 if (!(dwChannelOn & (1<<ch)))
1117 continue;
1118 if (s_chan[ch].bFMod == 2)
1119 fmod_chans |= 1 << ch;
1120 if (s_chan[ch].bNoise)
1121 noise_chans |= 1 << ch;
174c454a 1122 if((spuCtrl&CTRL_IRQ) && s_chan[ch].pCurr <= pSpuIrq && s_chan[ch].pLoop <= pSpuIrq)
1123 irq_chans |= 1 << ch;
90f1d26c 1124 }
1125
1126 *chans_out = dwChannelOn;
174c454a 1127 *run_chans = ~dwChannelOn & ~dwChannelDead & irq_chans;
90f1d26c 1128 *fmod_chans_out = fmod_chans;
1129 *noise_chans_out = noise_chans;
1130}
1131
6d866bb7 1132// vim:shiftwidth=1:expandtab