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