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