spu: remove some hacks
[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
650adfd2 52// intended to be ~1 frame
53#define IRQ_NEAR_BLOCKS 32
54
6d866bb7 55/*
ef79bbde
P
56#if defined (USEMACOSX)
57static char * libraryName = N_("Mac OS X Sound");
58#elif defined (USEALSA)
59static char * libraryName = N_("ALSA Sound");
60#elif defined (USEOSS)
61static char * libraryName = N_("OSS Sound");
62#elif defined (USESDL)
63static char * libraryName = N_("SDL Sound");
64#elif defined (USEPULSEAUDIO)
65static char * libraryName = N_("PulseAudio Sound");
66#else
67static char * libraryName = N_("NULL Sound");
68#endif
69
70static char * libraryInfo = N_("P.E.Op.S. Sound Driver V1.7\nCoded by Pete Bernert and the P.E.Op.S. team\n");
6d866bb7 71*/
ef79bbde
P
72
73// globals
74
75// psx buffer / addresses
76
77unsigned short regArea[10000];
78unsigned short spuMem[256*1024];
79unsigned char * spuMemC;
80unsigned char * pSpuIrq=0;
81unsigned char * pSpuBuffer;
ef79bbde
P
82
83// user settings
84
9e7a7352 85int iVolume=768; // 1024 is 1.0
ef79bbde 86int iXAPitch=1;
ef79bbde
P
87int iUseReverb=2;
88int iUseInterpolation=2;
ef79bbde
P
89
90// MAIN infos struct for each channel
91
92SPUCHAN s_chan[MAXCHAN+1]; // channel + 1 infos (1 is security for fmod handling)
93REVERBInfo rvb;
94
b1094d0e 95unsigned int dwNoiseVal; // global noise generator
96unsigned int dwNoiseCount;
ef79bbde
P
97
98unsigned short spuCtrl=0; // some vars to store psx reg infos
99unsigned short spuStat=0;
100unsigned short spuIrq=0;
101unsigned long spuAddr=0xffffffff; // address into spu mem
ef79bbde
P
102int bSpuInit=0;
103int bSPUIsOpen=0;
104
174c454a 105unsigned int dwNewChannel=0; // flags for faster testing, if new channel starts
106unsigned int dwChannelOn=0; // not silent channels
107unsigned int dwPendingChanOff=0;
108unsigned int dwChannelDead=0; // silent+not useful channels
ef79bbde
P
109
110void (CALLBACK *irqCallback)(void)=0; // func of main emu, called on spu irq
111void (CALLBACK *cddavCallback)(unsigned short,unsigned short)=0;
650adfd2 112void (CALLBACK *scheduleCallback)(unsigned int)=0;
ef79bbde
P
113
114// certain globals (were local before, but with the new timeproc I need em global)
115
6d866bb7 116static const int f[8][2] = { { 0, 0 },
ef79bbde
P
117 { 60, 0 },
118 { 115, -52 },
119 { 98, -55 },
120 { 122, -60 } };
b17618c0 121int ChanBuf[NSSIZE+3];
122int SSumLR[(NSSIZE+3)*2];
ef79bbde 123int iFMod[NSSIZE];
ef79bbde
P
124short * pS;
125
b72f17a1 126static int decode_dirty_ch;
127int decode_pos;
650adfd2 128unsigned int cycles_played;
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
650adfd2 521// if irq is going to trigger sooner than in upd_samples, set upd_samples
522static void scan_for_irq(int ch, unsigned int *upd_samples)
523{
524 int pos, sinc, sinc_inv, end;
525 unsigned char *block;
526 int flags;
527
528 block = s_chan[ch].pCurr;
529 pos = s_chan[ch].spos;
530 sinc = s_chan[ch].sinc;
531 end = pos + *upd_samples * sinc;
532
533 pos += (28 - s_chan[ch].iSBPos) << 16;
534 while (pos < end)
535 {
536 if (block == pSpuIrq)
537 break;
538 flags = block[1];
539 block += 16;
540 if (flags & 1) { // 1: stop/loop
541 block = s_chan[ch].pLoop;
542 if (block == pSpuIrq) // hack.. (see decode_block)
543 break;
544 }
545 pos += 28 << 16;
546 }
547
548 if (pos < end)
549 {
550 sinc_inv = s_chan[ch].sinc_inv;
551 if (sinc_inv == 0)
552 sinc_inv = s_chan[ch].sinc_inv = (0x80000000u / (uint32_t)sinc) << 1;
553
554 pos -= s_chan[ch].spos;
555 *upd_samples = (((uint64_t)pos * sinc_inv) >> 32) + 1;
556 //xprintf("ch%02d: irq sched: %3d %03d\n",
557 // ch, *upd_samples, *upd_samples * 60 * 263 / 44100);
558 }
559}
560
07a6dd2c 561#define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
650adfd2 562static noinline int do_samples_##name(int ch, int ns, int ns_to) \
07a6dd2c 563{ \
564 int sinc = s_chan[ch].sinc; \
565 int spos = s_chan[ch].spos; \
381ea103 566 int sbpos = s_chan[ch].iSBPos; \
567 int *SB = s_chan[ch].SB; \
07a6dd2c 568 int ret = -1; \
569 int d, fa; \
570 interp_start; \
571 \
572 for (; ns < ns_to; ns++) \
573 { \
574 fmod_code; \
575 \
650adfd2 576 spos += sinc; \
07a6dd2c 577 while (spos >= 0x10000) \
578 { \
650adfd2 579 fa = SB[sbpos++]; \
580 if(sbpos >= 28) \
07a6dd2c 581 { \
381ea103 582 sbpos = 0; \
07a6dd2c 583 d = decode_block(ch); \
f2aec10a 584 if(d) \
650adfd2 585 ret = /*ns_to =*/ ns + 1; \
07a6dd2c 586 } \
587 \
07a6dd2c 588 interp1_code; \
589 spos -= 0x10000; \
590 } \
591 \
592 interp2_code; \
07a6dd2c 593 } \
594 \
07a6dd2c 595 s_chan[ch].sinc = sinc; \
596 s_chan[ch].spos = spos; \
381ea103 597 s_chan[ch].iSBPos = sbpos; \
07a6dd2c 598 interp_end; \
599 \
600 return ret; \
601}
602
603#define fmod_recv_check \
604 if(s_chan[ch].bFMod==1 && iFMod[ns]) \
605 sinc = FModChangeFrequency(ch,ns)
606
607make_do_samples(default, fmod_recv_check, ,
608 StoreInterpolationVal(ch, fa),
8cd23d6d 609 ChanBuf[ns] = iGetInterpolationVal(ch, spos), )
07a6dd2c 610make_do_samples(noint, , fa = s_chan[ch].SB[29], , ChanBuf[ns] = fa, s_chan[ch].SB[29] = fa)
611
612#define simple_interp_store \
613 s_chan[ch].SB[28] = 0; \
614 s_chan[ch].SB[29] = s_chan[ch].SB[30]; \
615 s_chan[ch].SB[30] = s_chan[ch].SB[31]; \
616 s_chan[ch].SB[31] = fa; \
617 s_chan[ch].SB[32] = 1
618
619#define simple_interp_get \
620 if(sinc<0x10000) /* -> upsampling? */ \
621 InterpolateUp(ch); /* --> interpolate up */ \
622 else InterpolateDown(ch); /* --> else down */ \
623 ChanBuf[ns] = s_chan[ch].SB[29]
624
625make_do_samples(simple, , ,
626 simple_interp_store, simple_interp_get, )
627
650adfd2 628static noinline int do_samples_noise(int ch, int ns, int ns_to)
07a6dd2c 629{
b1094d0e 630 int level, shift, bit;
89cb2058 631 int ret = -1, d;
b1094d0e 632
07a6dd2c 633 s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns);
634 while (s_chan[ch].spos >= 28*0x10000)
635 {
89cb2058 636 d = skip_block(ch);
637 if (d)
638 ret = ns_to;
07a6dd2c 639 s_chan[ch].spos -= 28*0x10000;
640 }
641
b1094d0e 642 // modified from DrHell/shalma, no fraction
643 level = (spuCtrl >> 10) & 0x0f;
644 level = 0x8000 >> level;
645
07a6dd2c 646 for (; ns < ns_to; ns++)
b1094d0e 647 {
648 dwNoiseCount += 2;
649 if (dwNoiseCount >= level)
650 {
651 dwNoiseCount -= level;
652 shift = (dwNoiseVal >> 10) & 0x1f;
653 bit = (0x69696969 >> shift) & 1;
654 if (dwNoiseVal & 0x8000)
655 bit ^= 1;
656 dwNoiseVal = (dwNoiseVal << 1) | bit;
657 }
658
659 ChanBuf[ns] = (signed short)dwNoiseVal;
660 }
07a6dd2c 661
89cb2058 662 return ret;
07a6dd2c 663}
664
665f33e1 665#ifdef HAVE_ARMV5
3a721c1f 666// asm code; lv and rv must be 0-3fff
b17618c0 667extern void mix_chan(int start, int count, int lv, int rv);
668extern void mix_chan_rvb(int start, int count, int lv, int rv);
669#else
670static void mix_chan(int start, int count, int lv, int rv)
671{
672 int *dst = SSumLR + start * 2;
673 const int *src = ChanBuf + start;
674 int l, r;
675
676 while (count--)
677 {
678 int sval = *src++;
679
680 l = (sval * lv) >> 14;
681 r = (sval * rv) >> 14;
682 *dst++ += l;
683 *dst++ += r;
684 }
685}
686
687static void mix_chan_rvb(int start, int count, int lv, int rv)
688{
689 int *dst = SSumLR + start * 2;
690 int *drvb = sRVBStart + start * 2;
691 const int *src = ChanBuf + start;
692 int l, r;
693
694 while (count--)
695 {
696 int sval = *src++;
697
698 l = (sval * lv) >> 14;
699 r = (sval * rv) >> 14;
700 *dst++ += l;
701 *dst++ += r;
702 *drvb++ += l;
703 *drvb++ += r;
704 }
705}
706#endif
707
b72f17a1 708// 0x0800-0x0bff Voice 1
709// 0x0c00-0x0fff Voice 3
650adfd2 710static noinline void do_decode_bufs(int which, int start, int count)
b72f17a1 711{
712 const int *src = ChanBuf + start;
713 unsigned short *dst = &spuMem[0x800/2 + which*0x400/2];
ee9afdbd 714 int cursor = decode_pos + start;
b72f17a1 715
716 while (count-- > 0)
717 {
ee9afdbd 718 cursor &= 0x1ff;
b72f17a1 719 dst[cursor] = *src++;
ee9afdbd 720 cursor++;
b72f17a1 721 }
722
723 // decode_pos is updated and irqs are checked later, after voice loop
724}
725
ef79bbde
P
726////////////////////////////////////////////////////////////////////////
727// MAIN SPU FUNCTION
6d75977b 728// here is the main job handler...
ef79bbde
P
729// basically the whole sound processing is done in this fat func!
730////////////////////////////////////////////////////////////////////////
731
650adfd2 732void do_samples(unsigned int cycles_to)
ef79bbde 733{
650adfd2 734 const int ns_from = 0;
735 int ns,ns_to,ns_len;
9e7a7352 736 int volmult = iVolume;
174c454a 737 int ch,d,silentch;
650adfd2 738 int cycle_diff;
ef79bbde 739
650adfd2 740 cycle_diff = cycles_to - cycles_played;
741 if (cycle_diff < -2*1048576 || cycle_diff > 2*1048576)
742 {
743 //xprintf("desync %u %d\n", cycles_to, cycle_diff);
744 cycles_played = cycles_to;
745 return;
746 }
e34a4bd3 747
650adfd2 748 if (cycle_diff < 2 * 768)
749 return;
e34a4bd3 750
650adfd2 751 ns_to = (cycle_diff / 768 + 1) & ~1;
752 if (ns_to > NSSIZE) {
753 // should never happen
754 //xprintf("ns_to oflow %d %d\n", ns_to, NSSIZE);
755 ns_to = NSSIZE;
756 }
757
758 //////////////////////////////////////////////////////
759 // special irq handling in the decode buffers (0x0000-0x1000)
760 // we know:
761 // the decode buffers are located in spu memory in the following way:
762 // 0x0000-0x03ff CD audio left
763 // 0x0400-0x07ff CD audio right
764 // 0x0800-0x0bff Voice 1
765 // 0x0c00-0x0fff Voice 3
766 // and decoded data is 16 bit for one sample
767 // we assume:
768 // even if voices 1/3 are off or no cd audio is playing, the internal
769 // play positions will move on and wrap after 0x400 bytes.
770 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
771 // increase this pointer on each sample by 2 bytes. If this pointer
772 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
773 // an IRQ.
774
775 if (unlikely((spuCtrl & CTRL_IRQ) && pSpuIrq && pSpuIrq < spuMemC+0x1000))
776 {
777 int irq_pos = (pSpuIrq - spuMemC) / 2 & 0x1ff;
778 int left = (irq_pos - decode_pos) & 0x1ff;
779 if (0 < left && left <= ns_to)
780 {
781 //xprintf("decoder irq %x\n", decode_pos);
782 do_irq();
650adfd2 783 }
784 }
785
786 InitREVERB(ns_to);
ef79bbde 787
650adfd2 788 {
174c454a 789 silentch=~(dwChannelOn|dwNewChannel);
790
ef79bbde
P
791 //--------------------------------------------------//
792 //- main channel loop -//
793 //--------------------------------------------------//
794 {
650adfd2 795 for(ch=0;ch<MAXCHAN;ch++) // loop em all...
ef79bbde 796 {
6d866bb7 797 if(dwNewChannel&(1<<ch)) StartSound(ch); // start new sound
798 if(!(dwChannelOn&(1<<ch))) continue; // channel not playing? next
ef79bbde 799
07a6dd2c 800 if(s_chan[ch].bNoise)
650adfd2 801 do_samples_noise(ch, ns_from, ns_to);
07a6dd2c 802 else if(s_chan[ch].bFMod==2 || (s_chan[ch].bFMod==0 && iUseInterpolation==0))
650adfd2 803 do_samples_noint(ch, ns_from, ns_to);
07a6dd2c 804 else if(s_chan[ch].bFMod==0 && iUseInterpolation==1)
650adfd2 805 do_samples_simple(ch, ns_from, ns_to);
07a6dd2c 806 else
650adfd2 807 do_samples_default(ch, ns_from, ns_to);
808
e391d21c 809 ns_len = ns_to - ns_from;
ef79bbde 810
1ab7621a 811 MixADSR(ch, ns_from, ns_to);
812
b72f17a1 813 if(ch==1 || ch==3)
814 {
e391d21c 815 do_decode_bufs(ch/2, ns_from, ns_len);
b72f17a1 816 decode_dirty_ch |= 1<<ch;
817 }
818
1ab7621a 819 if(s_chan[ch].bFMod==2) // fmod freq channel
e391d21c 820 memcpy(&iFMod[ns_from], &ChanBuf[ns_from], ns_len * sizeof(iFMod[0]));
821 if(s_chan[ch].bRVBActive)
822 mix_chan_rvb(ns_from, ns_len, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume);
1775933a 823 else
e391d21c 824 mix_chan(ns_from, ns_len, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume);
ef79bbde
P
825 }
826 }
827
78c60846 828 // advance "stopped" channels that can cause irqs
829 // (all chans are always playing on the real thing..)
f2aec10a 830 if(spuCtrl&CTRL_IRQ)
78c60846 831 for(ch=0;ch<MAXCHAN;ch++)
832 {
174c454a 833 if(!(silentch&(1<<ch))) continue; // already handled
834 if(dwChannelDead&(1<<ch)) continue;
78c60846 835 if(s_chan[ch].pCurr > pSpuIrq && s_chan[ch].pLoop > pSpuIrq)
836 continue;
837
f2aec10a 838 s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns_from);
78c60846 839 while(s_chan[ch].spos >= 28 * 0x10000)
840 {
7e44d49d 841 unsigned char *start = s_chan[ch].pCurr;
78c60846 842
fb552464 843 skip_block(ch);
f9fe4923 844 if(start == s_chan[ch].pCurr || start - spuMemC < 0x1000)
78c60846 845 {
f9fe4923 846 // looping on self or stopped(?)
7e44d49d 847 dwChannelDead |= 1<<ch;
848 s_chan[ch].spos = 0;
78c60846 849 break;
850 }
851
78c60846 852 s_chan[ch].spos -= 28 * 0x10000;
853 }
854 }
855
b72f17a1 856 if(unlikely(silentch & decode_dirty_ch & (1<<1))) // must clear silent channel decode buffers
857 {
858 memset(&spuMem[0x800/2], 0, 0x400);
859 decode_dirty_ch &= ~(1<<1);
860 }
861 if(unlikely(silentch & decode_dirty_ch & (1<<3)))
862 {
863 memset(&spuMem[0xc00/2], 0, 0x400);
864 decode_dirty_ch &= ~(1<<3);
865 }
8680e822 866
ef79bbde
P
867 //---------------------------------------------------//
868 //- here we have another 1 ms of sound data
869 //---------------------------------------------------//
870 // mix XA infos (if any)
871
650adfd2 872 MixXA(ns_to);
ef79bbde
P
873
874 ///////////////////////////////////////////////////////
875 // mix all channels (including reverb) into one buffer
876
1775933a 877 if(iUseReverb)
650adfd2 878 REVERBDo(ns_to);
1775933a 879
880 if((spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this)
881 {
650adfd2 882 memset(pS, 0, ns_to * 2 * sizeof(pS[0]));
883 pS += ns_to * 2;
1775933a 884 }
885 else
650adfd2 886 for (ns = 0; ns < ns_to * 2; )
ef79bbde 887 {
9e7a7352 888 d = SSumLR[ns]; SSumLR[ns] = 0;
889 d = d * volmult >> 10;
1775933a 890 ssat32_to_16(d);
ef79bbde 891 *pS++ = d;
97ea4077 892 ns++;
ef79bbde 893
9e7a7352 894 d = SSumLR[ns]; SSumLR[ns] = 0;
895 d = d * volmult >> 10;
1775933a 896 ssat32_to_16(d);
ef79bbde 897 *pS++ = d;
97ea4077 898 ns++;
ef79bbde
P
899 }
900
650adfd2 901 cycles_played += ns_to * 768;
16187bfb 902
650adfd2 903 decode_pos = (decode_pos + ns_to) & 0x1ff;
904 }
905}
ef79bbde 906
650adfd2 907void schedule_next_irq(void)
908{
909 unsigned int upd_samples;
910 int ch;
ef79bbde 911
650adfd2 912 if (scheduleCallback == NULL)
913 return;
e34a4bd3 914
650adfd2 915 upd_samples = 44100 / 50;
e34a4bd3 916
650adfd2 917 for (ch = 0; ch < MAXCHAN; ch++)
918 {
919 if (dwChannelDead & (1 << ch))
920 continue;
921 if ((unsigned long)(pSpuIrq - s_chan[ch].pCurr) > IRQ_NEAR_BLOCKS * 16
922 && (unsigned long)(pSpuIrq - s_chan[ch].pLoop) > IRQ_NEAR_BLOCKS * 16)
923 continue;
16187bfb 924
650adfd2 925 scan_for_irq(ch, &upd_samples);
ef79bbde
P
926 }
927
650adfd2 928 if (unlikely(pSpuIrq < spuMemC + 0x1000))
929 {
930 int irq_pos = (pSpuIrq - spuMemC) / 2 & 0x1ff;
931 int left = (irq_pos - decode_pos) & 0x1ff;
932 if (0 < left && left < upd_samples) {
933 //xprintf("decode: %3d (%3d/%3d)\n", left, decode_pos, irq_pos);
934 upd_samples = left;
935 }
936 }
16187bfb 937
650adfd2 938 if (upd_samples < 44100 / 50)
939 scheduleCallback(upd_samples * 768);
ef79bbde
P
940}
941
942// SPU ASYNC... even newer epsxe func
943// 1 time every 'cycle' cycles... harhar
944
650adfd2 945// rearmed: called dynamically now
554a2220 946
650adfd2 947void CALLBACK SPUasync(unsigned int cycle, unsigned int flags)
ef79bbde 948{
650adfd2 949 do_samples(cycle);
fb552464 950
650adfd2 951 if (spuCtrl & CTRL_IRQ)
952 schedule_next_irq();
f8edb5bc 953
650adfd2 954 if (flags & 1) {
955 out_current->feed(pSpuBuffer, (unsigned char *)pS - pSpuBuffer);
956 pS = (short *)pSpuBuffer;
fb552464 957
650adfd2 958 if (0) {
959 if (!out_current->busy())
960 // cause more samples to be generated
961 // (and break some games because of bad sync)
962 cycles_played -= 44100 / 60 / 2 * 768;
ef79bbde 963 }
650adfd2 964 }
ef79bbde
P
965}
966
967// SPU UPDATE... new epsxe func
968// 1 time every 32 hsync lines
969// (312/32)x50 in pal
970// (262/32)x60 in ntsc
971
972// since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
973// leave that func in the linux port, until epsxe linux is using
974// the async function as well
975
976void CALLBACK SPUupdate(void)
977{
ef79bbde
P
978}
979
980// XA AUDIO
981
982void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap)
983{
984 if(!xap) return;
985 if(!xap->freq) return; // no xa freq ? bye
986
987 FeedXA(xap); // call main XA feeder
988}
989
990// CDDA AUDIO
983a7cfd 991int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
ef79bbde 992{
983a7cfd 993 if (!pcm) return -1;
994 if (nbytes<=0) return -1;
ef79bbde 995
983a7cfd 996 return FeedCDDA((unsigned char *)pcm, nbytes);
ef79bbde
P
997}
998
6d75977b 999// to be called after state load
1000void ClearWorkingState(void)
ef79bbde 1001{
97ea4077 1002 memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers
6d75977b 1003 memset(iFMod,0,sizeof(iFMod));
ef79bbde 1004 pS=(short *)pSpuBuffer; // setup soundbuffer pointer
ef79bbde
P
1005}
1006
1007// SETUPSTREAMS: init most of the spu buffers
1008void SetupStreams(void)
1009{
1010 int i;
1011
1012 pSpuBuffer=(unsigned char *)malloc(32768); // alloc mixing buffer
1013
1014 if(iUseReverb==1) i=88200*2;
1015 else i=NSSIZE*2;
1016
1017 sRVBStart = (int *)malloc(i*4); // alloc reverb buffer
1018 memset(sRVBStart,0,i*4);
1019 sRVBEnd = sRVBStart + i;
1020 sRVBPlay = sRVBStart;
1021
1022 XAStart = // alloc xa buffer
1023 (uint32_t *)malloc(44100 * sizeof(uint32_t));
1024 XAEnd = XAStart + 44100;
1025 XAPlay = XAStart;
1026 XAFeed = XAStart;
1027
1028 CDDAStart = // alloc cdda buffer
983a7cfd 1029 (uint32_t *)malloc(CDDA_BUFFER_SIZE);
ef79bbde
P
1030 CDDAEnd = CDDAStart + 16384;
1031 CDDAPlay = CDDAStart;
b8e744ce 1032 CDDAFeed = CDDAStart;
ef79bbde
P
1033
1034 for(i=0;i<MAXCHAN;i++) // loop sound channels
1035 {
1036// we don't use mutex sync... not needed, would only
1037// slow us down:
1038// s_chan[i].hMutex=CreateMutex(NULL,FALSE,NULL);
6d866bb7 1039 s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
650adfd2 1040 s_chan[i].ADSRX.SustainIncrease = 1;
ef79bbde 1041 s_chan[i].pLoop=spuMemC;
ef79bbde
P
1042 s_chan[i].pCurr=spuMemC;
1043 }
1044
6d75977b 1045 ClearWorkingState();
1046
1047 bSpuInit=1; // flag: we are inited
ef79bbde
P
1048}
1049
1050// REMOVESTREAMS: free most buffer
1051void RemoveStreams(void)
1052{
1053 free(pSpuBuffer); // free mixing buffer
1054 pSpuBuffer = NULL;
1055 free(sRVBStart); // free reverb buffer
1056 sRVBStart = NULL;
1057 free(XAStart); // free XA buffer
1058 XAStart = NULL;
1059 free(CDDAStart); // free CDDA buffer
1060 CDDAStart = NULL;
1061}
1062
1063// INIT/EXIT STUFF
1064
1065// SPUINIT: this func will be called first by the main emu
1066long CALLBACK SPUinit(void)
1067{
1068 spuMemC = (unsigned char *)spuMem; // just small setup
1069 memset((void *)&rvb, 0, sizeof(REVERBInfo));
1070 InitADSR();
1071
ef79bbde
P
1072 spuIrq = 0;
1073 spuAddr = 0xffffffff;
ef79bbde 1074 spuMemC = (unsigned char *)spuMem;
b72f17a1 1075 decode_pos = 0;
ef79bbde
P
1076 memset((void *)s_chan, 0, (MAXCHAN + 1) * sizeof(SPUCHAN));
1077 pSpuIrq = 0;
ef79bbde 1078
ef79bbde
P
1079 SetupStreams(); // prepare streaming
1080
1081 return 0;
1082}
1083
1084// SPUOPEN: called by main emu after init
1085long CALLBACK SPUopen(void)
1086{
1087 if (bSPUIsOpen) return 0; // security for some stupid main emus
1088
1089 SetupSound(); // setup sound (before init!)
ef79bbde
P
1090
1091 bSPUIsOpen = 1;
1092
1093 return PSE_SPU_ERR_SUCCESS;
1094}
1095
1096// SPUCLOSE: called before shutdown
1097long CALLBACK SPUclose(void)
1098{
1099 if (!bSPUIsOpen) return 0; // some security
1100
1101 bSPUIsOpen = 0; // no more open
1102
07c13dfd 1103 out_current->finish(); // no more sound handling
ef79bbde
P
1104
1105 return 0;
1106}
1107
1108// SPUSHUTDOWN: called by main emu on final exit
1109long CALLBACK SPUshutdown(void)
1110{
1111 SPUclose();
1112 RemoveStreams(); // no more streaming
6d75977b 1113 bSpuInit=0;
ef79bbde
P
1114
1115 return 0;
1116}
1117
1118// SPUTEST: we don't test, we are always fine ;)
1119long CALLBACK SPUtest(void)
1120{
1121 return 0;
1122}
1123
1124// SPUCONFIGURE: call config dialog
1125long CALLBACK SPUconfigure(void)
1126{
1127#ifdef _MACOSX
1128 DoConfiguration();
1129#else
ee849648 1130// StartCfgTool("CFG");
ef79bbde
P
1131#endif
1132 return 0;
1133}
1134
1135// SPUABOUT: show about window
1136void CALLBACK SPUabout(void)
1137{
1138#ifdef _MACOSX
1139 DoAbout();
1140#else
ee849648 1141// StartCfgTool("ABOUT");
ef79bbde
P
1142#endif
1143}
1144
1145// SETUP CALLBACKS
1146// this functions will be called once,
1147// passes a callback that should be called on SPU-IRQ/cdda volume change
1148void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1149{
1150 irqCallback = callback;
1151}
1152
1153void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1154{
1155 cddavCallback = CDDAVcallback;
1156}
1157
2b30c129 1158void CALLBACK SPUregisterScheduleCb(void (CALLBACK *callback)(unsigned int))
1159{
650adfd2 1160 scheduleCallback = callback;
2b30c129 1161}
1162
ef79bbde 1163// COMMON PLUGIN INFO FUNCS
e906c010 1164/*
ef79bbde
P
1165char * CALLBACK PSEgetLibName(void)
1166{
1167 return _(libraryName);
1168}
1169
1170unsigned long CALLBACK PSEgetLibType(void)
1171{
1172 return PSE_LT_SPU;
1173}
1174
1175unsigned long CALLBACK PSEgetLibVersion(void)
1176{
1177 return (1 << 16) | (6 << 8);
1178}
1179
1180char * SPUgetLibInfos(void)
1181{
1182 return _(libraryInfo);
1183}
e906c010 1184*/
6d866bb7 1185
90f1d26c 1186// debug
174c454a 1187void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
90f1d26c 1188{
174c454a 1189 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
90f1d26c 1190
1191 for(;ch<MAXCHAN;ch++)
1192 {
1193 if (!(dwChannelOn & (1<<ch)))
1194 continue;
1195 if (s_chan[ch].bFMod == 2)
1196 fmod_chans |= 1 << ch;
1197 if (s_chan[ch].bNoise)
1198 noise_chans |= 1 << ch;
174c454a 1199 if((spuCtrl&CTRL_IRQ) && s_chan[ch].pCurr <= pSpuIrq && s_chan[ch].pLoop <= pSpuIrq)
1200 irq_chans |= 1 << ch;
90f1d26c 1201 }
1202
1203 *chans_out = dwChannelOn;
174c454a 1204 *run_chans = ~dwChannelOn & ~dwChannelDead & irq_chans;
90f1d26c 1205 *fmod_chans_out = fmod_chans;
1206 *noise_chans_out = noise_chans;
1207}
1208
6d866bb7 1209// vim:shiftwidth=1:expandtab