psxbios: completely rework exception handling
[pcsx_rearmed.git] / plugins / dfsound / xa.c
CommitLineData
ef79bbde
P
1/***************************************************************************
2 xa.c - description
3 -------------------
4 begin : Wed May 15 2002
5 copyright : (C) 2002 by Pete Bernert
6 email : BlackDove@addcom.de
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. See also the license.txt file for *
14 * additional informations. *
15 * *
16 ***************************************************************************/
17
18#include "stdafx.h"
a4621d43 19#include "spu.h"
ef79bbde
P
20#define _IN_XA
21#include <stdint.h>
22
23// will be included from spu.c
24#ifdef _IN_SPU
25
26////////////////////////////////////////////////////////////////////////
27// XA GLOBALS
28////////////////////////////////////////////////////////////////////////
29
ef79bbde
P
30static int gauss_ptr = 0;
31static int gauss_window[8] = {0, 0, 0, 0, 0, 0, 0, 0};
32
33#define gvall0 gauss_window[gauss_ptr]
34#define gvall(x) gauss_window[(gauss_ptr+x)&3]
35#define gvalr0 gauss_window[4+gauss_ptr]
36#define gvalr(x) gauss_window[4+((gauss_ptr+x)&3)]
37
38////////////////////////////////////////////////////////////////////////
39// MIX XA & CDDA
40////////////////////////////////////////////////////////////////////////
41
3bd31caf 42INLINE void MixXA(int *SSumLR, int ns_to, int decode_pos)
ef79bbde 43{
215ff9e6 44 int cursor = decode_pos;
ef79bbde 45 int ns;
efce366c 46 short l, r;
4197fb21 47 uint32_t v = spu.XALastVal;
ef79bbde 48
3154bfab 49 if(spu.XAPlay != spu.XAFeed || spu.XARepeat > 0)
efce366c 50 {
3154bfab 51 if(spu.XAPlay == spu.XAFeed)
52 spu.XARepeat--;
efce366c 53
650adfd2 54 for(ns = 0; ns < ns_to*2; )
efce366c 55 {
3154bfab 56 if(spu.XAPlay != spu.XAFeed) v=*spu.XAPlay++;
57 if(spu.XAPlay == spu.XAEnd) spu.XAPlay=spu.XAStart;
efce366c 58
3154bfab 59 l = ((int)(short)v * spu.iLeftXAVol) >> 15;
60 r = ((int)(short)(v >> 16) * spu.iLeftXAVol) >> 15;
efce366c 61 SSumLR[ns++] += l;
62 SSumLR[ns++] += r;
1d753163 63
a4621d43
PC
64 spu.spuMem[cursor] = HTOLE16(v);
65 spu.spuMem[cursor + 0x400/2] = HTOLE16(v >> 16);
efce366c 66 cursor = (cursor + 1) & 0x1ff;
67 }
3154bfab 68 spu.XALastVal = v;
efce366c 69 }
4197fb21 70 // occasionally CDDAFeed underflows by a few samples due to poor timing,
71 // hence this 'ns_to < 8'
72 else if(spu.CDDAPlay != spu.CDDAFeed || ns_to < 8)
73 {
74 for(ns = 0; ns < ns_to*2; )
75 {
76 if(spu.CDDAPlay != spu.CDDAFeed) v=*spu.CDDAPlay++;
77 if(spu.CDDAPlay == spu.CDDAEnd) spu.CDDAPlay=spu.CDDAStart;
ef79bbde 78
4197fb21 79 l = ((int)(short)v * spu.iLeftXAVol) >> 15;
80 r = ((int)(short)(v >> 16) * spu.iLeftXAVol) >> 15;
81 SSumLR[ns++] += l;
82 SSumLR[ns++] += r;
1d753163 83
a4621d43
PC
84 spu.spuMem[cursor] = HTOLE16(v);
85 spu.spuMem[cursor + 0x400/2] = HTOLE16(v >> 16);
4197fb21 86 cursor = (cursor + 1) & 0x1ff;
87 }
88 spu.XALastVal = v;
89 }
90 else
91 spu.XALastVal = 0;
ef79bbde
P
92}
93
94////////////////////////////////////////////////////////////////////////
95// small linux time helper... only used for watchdog
96////////////////////////////////////////////////////////////////////////
97
609d9ea5 98#if 0
f05d6ca2 99static unsigned long timeGetTime_spu()
ef79bbde 100{
de4a0279 101#if defined(NO_OS)
102 return 0;
103#elif defined(_WIN32)
003cfc63 104 return GetTickCount();
105#else
ef79bbde
P
106 struct timeval tv;
107 gettimeofday(&tv, 0); // well, maybe there are better ways
108 return tv.tv_sec * 1000 + tv.tv_usec/1000; // to do that, but at least it works
003cfc63 109#endif
ef79bbde 110}
609d9ea5 111#endif
ef79bbde
P
112
113////////////////////////////////////////////////////////////////////////
114// FEED XA
115////////////////////////////////////////////////////////////////////////
116
117INLINE void FeedXA(xa_decode_t *xap)
118{
119 int sinc,spos,i,iSize,iPlace,vl,vr;
120
3154bfab 121 if(!spu.bSPUIsOpen) return;
ef79bbde 122
3154bfab 123 spu.xapGlobal = xap; // store info for save states
9cf79034 124 spu.XARepeat = 3; // set up repeat
ef79bbde 125
97ea4077 126#if 0//def XA_HACK
ef79bbde
P
127 iSize=((45500*xap->nsamples)/xap->freq); // get size
128#else
129 iSize=((44100*xap->nsamples)/xap->freq); // get size
130#endif
131 if(!iSize) return; // none? bye
132
3154bfab 133 if(spu.XAFeed<spu.XAPlay) iPlace=spu.XAPlay-spu.XAFeed; // how much space in my buf?
134 else iPlace=(spu.XAEnd-spu.XAFeed) + (spu.XAPlay-spu.XAStart);
ef79bbde
P
135
136 if(iPlace==0) return; // no place at all
137
138 //----------------------------------------------------//
609d9ea5 139#if 0
3154bfab 140 if(spu_config.iXAPitch) // pitch change option?
ef79bbde
P
141 {
142 static DWORD dwLT=0;
143 static DWORD dwFPS=0;
144 static int iFPSCnt=0;
145 static int iLastSize=0;
146 static DWORD dwL1=0;
147 DWORD dw=timeGetTime_spu(),dw1,dw2;
148
149 iPlace=iSize;
150
151 dwFPS+=dw-dwLT;iFPSCnt++;
152
153 dwLT=dw;
154
155 if(iFPSCnt>=10)
156 {
157 if(!dwFPS) dwFPS=1;
158 dw1=1000000/dwFPS;
159 if(dw1>=(dwL1-100) && dw1<=(dwL1+100)) dw1=dwL1;
160 else dwL1=dw1;
161 dw2=(xap->freq*100/xap->nsamples);
162 if((!dw1)||((dw2+100)>=dw1)) iLastSize=0;
163 else
164 {
165 iLastSize=iSize*dw2/dw1;
166 if(iLastSize>iPlace) iLastSize=iPlace;
167 iSize=iLastSize;
168 }
169 iFPSCnt=0;dwFPS=0;
170 }
171 else
172 {
173 if(iLastSize) iSize=iLastSize;
174 }
175 }
609d9ea5 176#endif
ef79bbde
P
177 //----------------------------------------------------//
178
179 spos=0x10000L;
180 sinc = (xap->nsamples << 16) / iSize; // calc freq by num / size
181
182 if(xap->stereo)
183{
184 uint32_t * pS=(uint32_t *)xap->pcm;
185 uint32_t l=0;
186
609d9ea5 187#if 0
3154bfab 188 if(spu_config.iXAPitch)
ef79bbde
P
189 {
190 int32_t l1,l2;short s;
191 for(i=0;i<iSize;i++)
192 {
3154bfab 193 if(spu_config.iUseInterpolation==2)
ef79bbde
P
194 {
195 while(spos>=0x10000L)
196 {
197 l = *pS++;
198 gauss_window[gauss_ptr] = (short)LOWORD(l);
199 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
200 gauss_ptr = (gauss_ptr+1) & 3;
201 spos -= 0x10000L;
202 }
203 vl = (spos >> 6) & ~3;
acc415b3
S
204 vr=(gauss[vl]*gvall0) >> 15;
205 vr+=(gauss[vl+1]*gvall(1)) >> 15;
206 vr+=(gauss[vl+2]*gvall(2)) >> 15;
207 vr+=(gauss[vl+3]*gvall(3)) >> 15;
208 l= vr & 0xffff;
209 vr=(gauss[vl]*gvalr0) >> 15;
210 vr+=(gauss[vl+1]*gvalr(1)) >> 15;
211 vr+=(gauss[vl+2]*gvalr(2)) >> 15;
212 vr+=(gauss[vl+3]*gvalr(3)) >> 15;
213 l |= vr << 16;
ef79bbde
P
214 }
215 else
216 {
217 while(spos>=0x10000L)
218 {
219 l = *pS++;
220 spos -= 0x10000L;
221 }
222 }
223
224 s=(short)LOWORD(l);
225 l1=s;
226 l1=(l1*iPlace)/iSize;
381ea103 227 ssat32_to_16(l1);
ef79bbde
P
228 s=(short)HIWORD(l);
229 l2=s;
230 l2=(l2*iPlace)/iSize;
381ea103 231 ssat32_to_16(l2);
ef79bbde
P
232 l=(l1&0xffff)|(l2<<16);
233
3154bfab 234 *spu.XAFeed++=l;
ef79bbde 235
3154bfab 236 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
237 if(spu.XAFeed==spu.XAPlay)
ef79bbde 238 {
3154bfab 239 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
ef79bbde
P
240 break;
241 }
242
243 spos += sinc;
244 }
245 }
246 else
609d9ea5 247#endif
ef79bbde
P
248 {
249 for(i=0;i<iSize;i++)
250 {
3154bfab 251 if(spu_config.iUseInterpolation==2)
ef79bbde
P
252 {
253 while(spos>=0x10000L)
254 {
255 l = *pS++;
256 gauss_window[gauss_ptr] = (short)LOWORD(l);
257 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
258 gauss_ptr = (gauss_ptr+1) & 3;
259 spos -= 0x10000L;
260 }
261 vl = (spos >> 6) & ~3;
acc415b3
S
262 vr=(gauss[vl]*gvall0) >> 15;
263 vr+=(gauss[vl+1]*gvall(1)) >> 15;
264 vr+=(gauss[vl+2]*gvall(2)) >> 15;
265 vr+=(gauss[vl+3]*gvall(3)) >> 15;
266 l= vr & 0xffff;
267 vr=(gauss[vl]*gvalr0) >> 15;
268 vr+=(gauss[vl+1]*gvalr(1)) >> 15;
269 vr+=(gauss[vl+2]*gvalr(2)) >> 15;
270 vr+=(gauss[vl+3]*gvalr(3)) >> 15;
271 l |= vr << 16;
ef79bbde
P
272 }
273 else
274 {
275 while(spos>=0x10000L)
276 {
277 l = *pS++;
278 spos -= 0x10000L;
279 }
280 }
281
3154bfab 282 *spu.XAFeed++=l;
ef79bbde 283
3154bfab 284 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
285 if(spu.XAFeed==spu.XAPlay)
ef79bbde 286 {
3154bfab 287 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
ef79bbde
P
288 break;
289 }
290
291 spos += sinc;
292 }
293 }
294 }
295 else
296 {
297 unsigned short * pS=(unsigned short *)xap->pcm;
298 uint32_t l;short s=0;
299
609d9ea5 300#if 0
3154bfab 301 if(spu_config.iXAPitch)
ef79bbde
P
302 {
303 int32_t l1;
304 for(i=0;i<iSize;i++)
305 {
3154bfab 306 if(spu_config.iUseInterpolation==2)
ef79bbde
P
307 {
308 while(spos>=0x10000L)
309 {
310 gauss_window[gauss_ptr] = (short)*pS++;
311 gauss_ptr = (gauss_ptr+1) & 3;
312 spos -= 0x10000L;
313 }
314 vl = (spos >> 6) & ~3;
acc415b3
S
315 vr=(gauss[vl]*gvall0) >> 15;
316 vr+=(gauss[vl+1]*gvall(1)) >> 15;
317 vr+=(gauss[vl+2]*gvall(2)) >> 15;
318 vr+=(gauss[vl+3]*gvall(3)) >> 15;
319 l1=s= vr;
ef79bbde
P
320 l1 &= 0xffff;
321 }
322 else
323 {
324 while(spos>=0x10000L)
325 {
326 s = *pS++;
327 spos -= 0x10000L;
328 }
329 l1=s;
330 }
331
332 l1=(l1*iPlace)/iSize;
381ea103 333 ssat32_to_16(l1);
ef79bbde 334 l=(l1&0xffff)|(l1<<16);
3154bfab 335 *spu.XAFeed++=l;
ef79bbde 336
3154bfab 337 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
338 if(spu.XAFeed==spu.XAPlay)
ef79bbde 339 {
3154bfab 340 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
ef79bbde
P
341 break;
342 }
343
344 spos += sinc;
345 }
346 }
347 else
609d9ea5 348#endif
ef79bbde
P
349 {
350 for(i=0;i<iSize;i++)
351 {
3154bfab 352 if(spu_config.iUseInterpolation==2)
ef79bbde
P
353 {
354 while(spos>=0x10000L)
355 {
356 gauss_window[gauss_ptr] = (short)*pS++;
357 gauss_ptr = (gauss_ptr+1) & 3;
358 spos -= 0x10000L;
359 }
360 vl = (spos >> 6) & ~3;
acc415b3
S
361 vr=(gauss[vl]*gvall0) >> 15;
362 vr+=(gauss[vl+1]*gvall(1)) >> 15;
363 vr+=(gauss[vl+2]*gvall(2)) >> 15;
364 vr+=(gauss[vl+3]*gvall(3)) >> 15;
365 l=s= vr;
ef79bbde
P
366 }
367 else
368 {
369 while(spos>=0x10000L)
370 {
371 s = *pS++;
372 spos -= 0x10000L;
373 }
374 l=s;
375 }
376
9098b863 377 l &= 0xffff;
3154bfab 378 *spu.XAFeed++=(l|(l<<16));
ef79bbde 379
3154bfab 380 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
381 if(spu.XAFeed==spu.XAPlay)
ef79bbde 382 {
3154bfab 383 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
ef79bbde
P
384 break;
385 }
386
387 spos += sinc;
388 }
389 }
390 }
391}
392
393////////////////////////////////////////////////////////////////////////
394// FEED CDDA
395////////////////////////////////////////////////////////////////////////
396
983a7cfd 397INLINE int FeedCDDA(unsigned char *pcm, int nBytes)
ef79bbde 398{
983a7cfd 399 int space;
3154bfab 400 space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
983a7cfd 401 if(space<nBytes)
402 return 0x7761; // rearmed_wait
403
ef79bbde
P
404 while(nBytes>0)
405 {
3154bfab 406 if(spu.CDDAFeed==spu.CDDAEnd) spu.CDDAFeed=spu.CDDAStart;
407 space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
408 if(spu.CDDAFeed+space/4>spu.CDDAEnd)
409 space=(spu.CDDAEnd-spu.CDDAFeed)*4;
983a7cfd 410 if(space>nBytes)
411 space=nBytes;
412
3154bfab 413 memcpy(spu.CDDAFeed,pcm,space);
414 spu.CDDAFeed+=space/4;
983a7cfd 415 nBytes-=space;
416 pcm+=space;
ef79bbde 417 }
983a7cfd 418
419 return 0x676f; // rearmed_go
ef79bbde
P
420}
421
422#endif