spu: get rid of iSpuAsyncWait hack
[pcsx_rearmed.git] / plugins / dfsound / spu.c
... / ...
CommitLineData
1/***************************************************************************
2 spu.c - description
3 -------------------
4 begin : Wed May 15 2002
5 copyright : (C) 2002 by Pete Bernert
6 email : BlackDove@addcom.de
7
8 Portions (C) GraÅžvydas "notaz" Ignotas, 2010-2011
9
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"
26#include "registers.h"
27#include "dsoundoss.h"
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
39#ifdef __ARM_ARCH_7A__
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
49#define PSXCLK 33868800 /* 33.8688 MHz */
50
51/*
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");
67*/
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
82int iVolume=768; // 1024 is 1.0
83int iXAPitch=1;
84int iSPUIRQWait=1;
85int iDebugMode=0;
86int iRecordMode=0;
87int iUseReverb=2;
88int iUseInterpolation=2;
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
95unsigned int dwNoiseVal; // global noise generator
96unsigned int dwNoiseCount;
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
102int bSpuInit=0;
103int bSPUIsOpen=0;
104
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
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
115static const int f[8][2] = { { 0, 0 },
116 { 60, 0 },
117 { 115, -52 },
118 { 98, -55 },
119 { 122, -60 } };
120int ChanBuf[NSSIZE+3];
121int SSumLR[(NSSIZE+3)*2];
122int iFMod[NSSIZE];
123int iCycle = 0;
124short * pS;
125
126int had_dma;
127int lastch=-1; // last channel processed on spu irq in timer mode
128static int lastns=0; // last ns pos
129static int cycles_since_update;
130
131#define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2
132
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;
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];
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
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;
269
270 s_chan[ch].SB[26]=0; // init mixing vars
271 s_chan[ch].SB[27]=0;
272 s_chan[ch].iSBPos=28;
273
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
288INLINE int FModChangeFrequency(int ch,int ns)
289{
290 unsigned int NP=s_chan[ch].iRawPitch;
291 int sinc;
292
293 NP=((32768L+iFMod[ns])*NP)/32768L;
294
295 if(NP>0x3fff) NP=0x3fff;
296 if(NP<0x1) NP=0x1;
297
298 sinc=NP<<4; // calc frequency
299 if(iUseInterpolation==1) // freq change in simple interpolation mode
300 s_chan[ch].SB[32]=1;
301 iFMod[ns]=0;
302
303 return sinc;
304}
305
306////////////////////////////////////////////////////////////////////////
307
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 {
314 ssat32_to_16(fa);
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
338INLINE int iGetInterpolationVal(int ch, int spos)
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;
350 xd = (spos >> 1)+1;
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;
369 vl = (spos >> 6) & ~3;
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
396static void do_irq(void)
397{
398 //if(!(spuStat & STAT_IRQ))
399 {
400 spuStat |= STAT_IRQ; // asserted status?
401 if(irqCallback) irqCallback();
402 }
403}
404
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
433static int decode_block(int ch)
434{
435 unsigned char *start;
436 int predict_nr,shift_factor,flags;
437 int ret = 0;
438
439 start=s_chan[ch].pCurr; // set up the current pos
440 if(dwPendingChanOff&(1<<ch))
441 {
442 dwChannelOn&=~(1<<ch); // -> turn everything off
443 dwPendingChanOff&=~(1<<ch);
444 s_chan[ch].bStop=1;
445 s_chan[ch].ADSRX.EnvelopeVol=0;
446 }
447
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 }
458
459 predict_nr=(int)start[0];
460 shift_factor=predict_nr&0xf;
461 predict_nr >>= 4;
462
463 decode_block_data(s_chan[ch].SB, start + 2, predict_nr, shift_factor);
464
465 //////////////////////////////////////////// flag handler
466
467 flags=(int)start[1];
468 if(flags&4)
469 s_chan[ch].pLoop=start; // loop adress
470
471 start+=16;
472 if(flags&1) // 1: stop/loop
473 {
474 if(!(flags&2))
475 dwPendingChanOff|=1<<ch;
476
477 start = s_chan[ch].pLoop;
478 }
479
480 if (start - spuMemC >= 0x80000) {
481 // most likely wrong
482 start = spuMemC;
483 printf("ch%d oflow\n", ch);
484 }
485
486 s_chan[ch].pCurr = start; // store values for next cycle
487 s_chan[ch].bJump = flags & 1;
488
489 return ret;
490}
491
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
499 if(start == pSpuIrq)
500 {
501 do_irq();
502 ret = 1;
503 }
504
505 if(flags & 4)
506 s_chan[ch].pLoop = start;
507
508 s_chan[ch].pCurr += 16;
509
510 if(flags & 1)
511 s_chan[ch].pCurr = s_chan[ch].pLoop;
512
513 s_chan[ch].bJump = flags & 1;
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; \
522 int sbpos = s_chan[ch].iSBPos; \
523 int *SB = s_chan[ch].SB; \
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 { \
534 if(sbpos == 28) \
535 { \
536 sbpos = 0; \
537 d = decode_block(ch); \
538 if(d && iSPUIRQWait) \
539 { \
540 ret = ns; \
541 goto out; \
542 } \
543 } \
544 \
545 fa = SB[sbpos++]; \
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; \
557 s_chan[ch].iSBPos = sbpos; \
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),
569 ChanBuf[ns] = iGetInterpolationVal(ch, spos), )
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{
590 int level, shift, bit;
591
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
599 // modified from DrHell/shalma, no fraction
600 level = (spuCtrl >> 10) & 0x0f;
601 level = 0x8000 >> level;
602
603 for (; ns < ns_to; ns++)
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 }
618
619 return -1;
620}
621
622#ifdef __arm__
623// asm code; lv and rv must be 0-3fff
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
665////////////////////////////////////////////////////////////////////////
666// MAIN SPU FUNCTION
667// here is the main job handler...
668// basically the whole sound processing is done in this fat func!
669////////////////////////////////////////////////////////////////////////
670
671static int do_samples(int forced_updates)
672{
673 int volmult = iVolume;
674 int ns,ns_from,ns_to;
675 int ch,d,silentch;
676 int bIRQReturn=0;
677
678 while(1)
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
687 if(!forced_updates && SoundGetBytesBuffered()) // still enuff data in sound buffer?
688 {
689 return 0;
690 }
691
692 cycles_since_update = 0;
693 if(forced_updates > 0)
694 forced_updates--;
695
696 //--------------------------------------------------// continue from irq handling in timer mode?
697
698 ns_from=0;
699 ns_to=NSSIZE;
700 ch=0;
701 if(lastch>=0) // will be -1 if no continue is pending
702 {
703 ch=lastch; ns_from=lastns; lastch=-1; // -> setup all kind of vars to continue
704 }
705
706 silentch=~(dwChannelOn|dwNewChannel);
707
708 //--------------------------------------------------//
709 //- main channel loop -//
710 //--------------------------------------------------//
711 {
712 for(;ch<MAXCHAN;ch++) // loop em all... we will collect 1 ms of sound of each playing channel
713 {
714 if(dwNewChannel&(1<<ch)) StartSound(ch); // start new sound
715 if(!(dwChannelOn&(1<<ch))) continue; // channel not playing? next
716
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)
726 {
727 bIRQReturn=1;
728 lastch=ch;
729 lastns=ns_to=d;
730 if(d==0)
731 break;
732 }
733
734 MixADSR(ch, ns_from, ns_to);
735
736 if(s_chan[ch].bFMod==2) // fmod freq channel
737 memcpy(iFMod, ChanBuf, sizeof(iFMod));
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);
740 else
741 mix_chan(ns_from,ns_to-ns_from,s_chan[ch].iLeftVolume,s_chan[ch].iRightVolume);
742 }
743 }
744
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 {
750 if(!(silentch&(1<<ch))) continue; // already handled
751 if(dwChannelDead&(1<<ch)) continue;
752 if(s_chan[ch].pCurr > pSpuIrq && s_chan[ch].pLoop > pSpuIrq)
753 continue;
754
755 s_chan[ch].spos += s_chan[ch].sinc * NSSIZE;
756 while(s_chan[ch].spos >= 28 * 0x10000)
757 {
758 unsigned char *start = s_chan[ch].pCurr;
759
760 // no need for bIRQReturn since the channel is silent
761 skip_block(ch);
762 if(start == s_chan[ch].pCurr)
763 {
764 // looping on self
765 dwChannelDead |= 1<<ch;
766 s_chan[ch].spos = 0;
767 break;
768 }
769
770 s_chan[ch].spos -= 28 * 0x10000;
771 }
772 }
773
774 if(bIRQReturn && iSPUIRQWait) // special return for "spu irq - wait for cpu action"
775 return 0;
776
777
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
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
797 for (ns = 0; ns < NSSIZE*2; )
798 {
799 d = SSumLR[ns]; SSumLR[ns] = 0;
800 d = d * volmult >> 10;
801 ssat32_to_16(d);
802 *pS++ = d;
803 ns++;
804
805 d = SSumLR[ns]; SSumLR[ns] = 0;
806 d = d * volmult >> 10;
807 ssat32_to_16(d);
808 *pS++ = d;
809 ns++;
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
830 if(pMixIrq)
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)
839 do_irq();
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
850 if (iCycle++ > 16/FRAG_MSECS)
851 {
852 SoundFeedStreamData((unsigned char *)pSpuBuffer,
853 ((unsigned char *)pS) - ((unsigned char *)pSpuBuffer));
854 pS = (short *)pSpuBuffer;
855 iCycle = 0;
856 }
857 }
858
859 return 0;
860}
861
862// SPU ASYNC... even newer epsxe func
863// 1 time every 'cycle' cycles... harhar
864
865// rearmed: called every 2ms now
866
867void CALLBACK SPUasync(unsigned long cycle)
868{
869 int forced_updates = 0;
870 int do_update = 0;
871
872 if(!bSpuInit) return; // -> no init, no call
873
874 cycles_since_update += cycle;
875
876 if(dwNewChannel || had_dma)
877 {
878 forced_updates = 1;
879 do_update = 1;
880 had_dma = 0;
881 }
882
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);
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
916int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
917{
918 if (!pcm) return -1;
919 if (nbytes<=0) return -1;
920
921 return FeedCDDA((unsigned char *)pcm, nbytes);
922}
923
924// to be called after state load
925void ClearWorkingState(void)
926{
927 memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers
928 memset(iFMod,0,sizeof(iFMod));
929 pS=(short *)pSpuBuffer; // setup soundbuffer pointer
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
954 (uint32_t *)malloc(CDDA_BUFFER_SIZE);
955 CDDAEnd = CDDAStart + 16384;
956 CDDAPlay = CDDAStart;
957 CDDAFeed = CDDAStart;
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);
964 s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
965 s_chan[i].pLoop=spuMemC;
966 s_chan[i].pCurr=spuMemC;
967 }
968
969 pMixIrq=spuMemC; // enable decoded buffer irqs by setting the address
970
971 ClearWorkingState();
972
973 bSpuInit=1; // flag: we are inited
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
998 spuIrq = 0;
999 spuAddr = 0xffffffff;
1000 spuMemC = (unsigned char *)spuMem;
1001 pMixIrq = 0;
1002 memset((void *)s_chan, 0, (MAXCHAN + 1) * sizeof(SPUCHAN));
1003 pSpuIrq = 0;
1004 //iSPUIRQWait = 0;
1005 lastch = -1;
1006
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!)
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
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
1041 bSpuInit=0;
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
1058// StartCfgTool("CFG");
1059#endif
1060 return 0;
1061}
1062
1063// SPUABOUT: show about window
1064void CALLBACK SPUabout(void)
1065{
1066#ifdef _MACOSX
1067 DoAbout();
1068#else
1069// StartCfgTool("ABOUT");
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
1087/*
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}
1107*/
1108
1109// debug
1110void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
1111{
1112 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
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;
1122 if((spuCtrl&CTRL_IRQ) && s_chan[ch].pCurr <= pSpuIrq && s_chan[ch].pLoop <= pSpuIrq)
1123 irq_chans |= 1 << ch;
1124 }
1125
1126 *chans_out = dwChannelOn;
1127 *run_chans = ~dwChannelOn & ~dwChannelDead & irq_chans;
1128 *fmod_chans_out = fmod_chans;
1129 *noise_chans_out = noise_chans;
1130}
1131
1132// vim:shiftwidth=1:expandtab