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