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