libretro: adjust psxclock description
[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
63a4f6b6 8 Portions (C) GraÅžvydas "notaz" Ignotas, 2010-2012,2014,2015
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
e62141d2 21#include <assert.h>
ef79bbde
P
22#include "stdafx.h"
23
24#define _IN_SPU
25
26#include "externals.h"
3fc2a4c2 27#include "registers.h"
07c13dfd 28#include "out.h"
3154bfab 29#include "spu_config.h"
740eb9bc 30#include "spu.h"
ef79bbde 31
de4a0279 32#ifdef __arm__
33#include "arm_features.h"
34#endif
35
8f2bb0cb 36#ifdef HAVE_ARMV7
1775933a 37 #define ssat32_to_16(v) \
38 asm("ssat %0,#16,%1" : "=r" (v) : "r" (v))
39#else
40 #define ssat32_to_16(v) do { \
41 if (v < -32768) v = -32768; \
42 else if (v > 32767) v = 32767; \
43 } while (0)
44#endif
45
fb552464 46#define PSXCLK 33868800 /* 33.8688 MHz */
47
650adfd2 48// intended to be ~1 frame
49#define IRQ_NEAR_BLOCKS 32
50
6d866bb7 51/*
ef79bbde
P
52#if defined (USEMACOSX)
53static char * libraryName = N_("Mac OS X Sound");
54#elif defined (USEALSA)
55static char * libraryName = N_("ALSA Sound");
56#elif defined (USEOSS)
57static char * libraryName = N_("OSS Sound");
58#elif defined (USESDL)
59static char * libraryName = N_("SDL Sound");
60#elif defined (USEPULSEAUDIO)
61static char * libraryName = N_("PulseAudio Sound");
62#else
63static char * libraryName = N_("NULL Sound");
64#endif
65
66static char * libraryInfo = N_("P.E.Op.S. Sound Driver V1.7\nCoded by Pete Bernert and the P.E.Op.S. team\n");
6d866bb7 67*/
ef79bbde
P
68
69// globals
70
3154bfab 71SPUInfo spu;
72SPUConfig spu_config;
ef79bbde 73
5514a050 74static int iFMod[NSSIZE];
05c7cec7 75static int RVB[NSSIZE * 2];
3154bfab 76int ChanBuf[NSSIZE];
ef79bbde 77
983a7cfd 78#define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2
79
ef79bbde
P
80////////////////////////////////////////////////////////////////////////
81// CODE AREA
82////////////////////////////////////////////////////////////////////////
83
84// dirty inline func includes
85
86#include "reverb.c"
87#include "adsr.c"
88
89////////////////////////////////////////////////////////////////////////
90// helpers for simple interpolation
91
92//
93// easy interpolation on upsampling, no special filter, just "Pete's common sense" tm
94//
95// instead of having n equal sample values in a row like:
96// ____
97// |____
98//
99// we compare the current delta change with the next delta change.
100//
101// if curr_delta is positive,
102//
103// - and next delta is smaller (or changing direction):
104// \.
105// -__
106//
107// - and next delta significant (at least twice) bigger:
108// --_
109// \.
110//
111// - and next delta is nearly same:
112// \.
113// \.
114//
115//
116// if curr_delta is negative,
117//
118// - and next delta is smaller (or changing direction):
119// _--
120// /
121//
122// - and next delta significant (at least twice) bigger:
123// /
124// __-
125//
126// - and next delta is nearly same:
127// /
128// /
129//
130
24dbf983 131static void InterpolateUp(sample_buf *sb, int sinc)
ef79bbde 132{
24dbf983 133 int *SB = sb->SB;
134 if (sb->sinc_old != sinc)
135 {
136 sb->sinc_old = sinc;
137 SB[32] = 1;
138 }
011755d7 139 if(SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass
ef79bbde 140 {
011755d7 141 const int id1=SB[30]-SB[29]; // curr delta to next val
142 const int id2=SB[31]-SB[30]; // and next delta to next-next val :)
ef79bbde 143
011755d7 144 SB[32]=0;
ef79bbde
P
145
146 if(id1>0) // curr delta positive
147 {
148 if(id2<id1)
011755d7 149 {SB[28]=id1;SB[32]=2;}
ef79bbde
P
150 else
151 if(id2<(id1<<1))
011755d7 152 SB[28]=(id1*sinc)>>16;
ef79bbde 153 else
011755d7 154 SB[28]=(id1*sinc)>>17;
ef79bbde
P
155 }
156 else // curr delta negative
157 {
158 if(id2>id1)
011755d7 159 {SB[28]=id1;SB[32]=2;}
ef79bbde
P
160 else
161 if(id2>(id1<<1))
011755d7 162 SB[28]=(id1*sinc)>>16;
ef79bbde 163 else
011755d7 164 SB[28]=(id1*sinc)>>17;
ef79bbde
P
165 }
166 }
167 else
011755d7 168 if(SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass
ef79bbde 169 {
011755d7 170 SB[32]=0;
ef79bbde 171
011755d7 172 SB[28]=(SB[28]*sinc)>>17;
173 //if(sinc<=0x8000)
174 // SB[29]=SB[30]-(SB[28]*((0x10000/sinc)-1));
76d1d09c 175 //else
011755d7 176 SB[29]+=SB[28];
ef79bbde
P
177 }
178 else // no flags? add bigger val (if possible), calc smaller step, set flag1
011755d7 179 SB[29]+=SB[28];
ef79bbde
P
180}
181
182//
183// even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm
184//
185
24dbf983 186static void InterpolateDown(sample_buf *sb, int sinc)
ef79bbde 187{
24dbf983 188 int *SB = sb->SB;
011755d7 189 if(sinc>=0x20000L) // we would skip at least one val?
ef79bbde 190 {
011755d7 191 SB[29]+=(SB[30]-SB[29])/2; // add easy weight
192 if(sinc>=0x30000L) // we would skip even more vals?
193 SB[29]+=(SB[31]-SB[30])/2; // add additional next weight
ef79bbde
P
194 }
195}
196
197////////////////////////////////////////////////////////////////////////
ef79bbde
P
198
199#include "gauss_i.h"
ef79bbde
P
200#include "xa.c"
201
5fa59798 202static void do_irq(int cycles_after)
e4f075af 203{
5fa59798 204 if (spu.spuStat & STAT_IRQ)
205 log_unhandled("spu: missed irq?\n");
206 else
e4f075af 207 {
3154bfab 208 spu.spuStat |= STAT_IRQ; // asserted status?
5fa59798 209 if (spu.irqCallback)
210 spu.irqCallback(cycles_after);
e4f075af 211 }
212}
213
214static int check_irq(int ch, unsigned char *pos)
215{
54604e03 216 if((spu.spuCtrl & (CTRL_ON|CTRL_IRQ)) == (CTRL_ON|CTRL_IRQ) && pos == spu.pSpuIrq)
e4f075af 217 {
df5f713b 218 //printf("ch%d irq %04zx\n", ch, pos - spu.spuMemC);
5fa59798 219 do_irq(0);
e4f075af 220 return 1;
221 }
222 return 0;
223}
224
54604e03 225void check_irq_io(unsigned int addr)
226{
227 unsigned int irq_addr = regAreaGet(H_SPUirqAddr) << 3;
228 //addr &= ~7; // ?
229 if((spu.spuCtrl & (CTRL_ON|CTRL_IRQ)) == (CTRL_ON|CTRL_IRQ) && addr == irq_addr)
230 {
231 //printf("io irq %04x\n", irq_addr);
5fa59798 232 do_irq(0);
233 }
234}
235
236void do_irq_io(int cycles_after)
237{
238 if ((spu.spuCtrl & (CTRL_ON|CTRL_IRQ)) == (CTRL_ON|CTRL_IRQ))
239 {
240 do_irq(cycles_after);
54604e03 241 }
242}
243
ef79bbde
P
244////////////////////////////////////////////////////////////////////////
245// START SOUND... called by main thread to setup a new sound on a channel
246////////////////////////////////////////////////////////////////////////
247
24dbf983 248static void ResetInterpolation(sample_buf *sb)
ef79bbde 249{
24dbf983 250 memset(&sb->interp, 0, sizeof(sb->interp));
251 sb->sinc_old = -1;
252}
ef79bbde 253
24dbf983 254static void StartSoundSB(sample_buf *sb)
255{
256 sb->SB[26] = 0; // init mixing vars
257 sb->SB[27] = 0;
258 ResetInterpolation(sb);
3bd31caf 259}
260
261static void StartSoundMain(int ch)
262{
263 SPUCHAN *s_chan = &spu.s_chan[ch];
264
265 StartADSR(ch);
266 StartREVERB(ch);
267
6183ddf9 268 s_chan->prevflags = 2;
269 s_chan->iSBPos = 27;
270 s_chan->spos = 0;
271 s_chan->bStarting = 1;
ef79bbde 272
a0d4a8f5 273 s_chan->pCurr = spu.spuMemC + ((regAreaGetCh(ch, 6) & ~1) << 3);
9601ca6d 274
3154bfab 275 spu.dwNewChannel&=~(1<<ch); // clear new channel bit
4ccd0fb2 276 spu.dwChannelDead&=~(1<<ch);
df68316d 277 spu.dwChannelsAudible|=1<<ch;
ef79bbde
P
278}
279
3bd31caf 280static void StartSound(int ch)
281{
282 StartSoundMain(ch);
24dbf983 283 StartSoundSB(&spu.sb[ch]);
3bd31caf 284}
285
ef79bbde
P
286////////////////////////////////////////////////////////////////////////
287// ALL KIND OF HELPERS
288////////////////////////////////////////////////////////////////////////
289
e62141d2 290INLINE int FModChangeFrequency(int pitch, int ns, int *fmod_buf)
ef79bbde 291{
f63561cb 292 pitch = (signed short)pitch;
e62141d2 293 pitch = ((32768 + fmod_buf[ns]) * pitch) >> 15;
f63561cb 294 pitch &= 0xffff;
295 if (pitch > 0x3fff)
296 pitch = 0x3fff;
ef79bbde 297
e62141d2 298 fmod_buf[ns] = 0;
ef79bbde 299
f63561cb 300 return pitch << 4;
ef79bbde
P
301}
302
24dbf983 303INLINE void StoreInterpolationGaussCubic(sample_buf *sb, int fa)
ef79bbde 304{
24dbf983 305 int gpos = sb->interp.gauss.pos & 3;
306 sb->interp.gauss.val[gpos++] = fa;
307 sb->interp.gauss.pos = gpos & 3;
ef79bbde
P
308}
309
24dbf983 310#define gval(x) (int)sb->interp.gauss.val[(gpos + x) & 3]
ef79bbde 311
24dbf983 312INLINE int GetInterpolationCubic(const sample_buf *sb, int spos)
ef79bbde 313{
24dbf983 314 int gpos = sb->interp.gauss.pos;
315 int xd = (spos >> 1) + 1;
ef79bbde
P
316 int fa;
317
24dbf983 318 fa = gval(3) - 3*gval(2) + 3*gval(1) - gval(0);
319 fa *= (xd - (2<<15)) / 6;
320 fa >>= 15;
321 fa += gval(2) - gval(1) - gval(1) + gval(0);
322 fa *= (xd - (1<<15)) >> 1;
323 fa >>= 15;
324 fa += gval(1) - gval(0);
325 fa *= xd;
326 fa >>= 15;
327 fa = fa + gval(0);
ef79bbde
P
328 return fa;
329}
330
24dbf983 331INLINE int GetInterpolationGauss(const sample_buf *sb, int spos)
332{
333 int gpos = sb->interp.gauss.pos;
334 int vl = (spos >> 6) & ~3;
335 int vr;
336 vr = (gauss[vl+0] * gval(0)) >> 15;
337 vr += (gauss[vl+1] * gval(1)) >> 15;
338 vr += (gauss[vl+2] * gval(2)) >> 15;
339 vr += (gauss[vl+3] * gval(3)) >> 15;
340 return vr;
341}
342
381ea103 343static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor)
344{
5514a050 345 static const int f[16][2] = {
346 { 0, 0 },
347 { 60, 0 },
348 { 115, -52 },
349 { 98, -55 },
350 { 122, -60 }
351 };
381ea103 352 int nSample;
353 int fa, s_1, s_2, d, s;
354
355 s_1 = dest[27];
356 s_2 = dest[26];
357
358 for (nSample = 0; nSample < 28; src++)
359 {
360 d = (int)*src;
361 s = (int)(signed short)((d & 0x0f) << 12);
362
f63561cb 363 fa = s >> shift_factor;
381ea103 364 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
f63561cb 365 ssat32_to_16(fa);
366 s_2 = s_1; s_1 = fa;
381ea103 367
368 dest[nSample++] = fa;
369
370 s = (int)(signed short)((d & 0xf0) << 8);
f63561cb 371 fa = s >> shift_factor;
381ea103 372 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
f63561cb 373 ssat32_to_16(fa);
374 s_2 = s_1; s_1 = fa;
381ea103 375
376 dest[nSample++] = fa;
377 }
378}
379
3bd31caf 380static int decode_block(void *unused, int ch, int *SB)
e11ae5c5 381{
5514a050 382 SPUCHAN *s_chan = &spu.s_chan[ch];
e11ae5c5 383 unsigned char *start;
215ff9e6 384 int predict_nr, shift_factor, flags;
e11ae5c5 385 int ret = 0;
386
5514a050 387 start = s_chan->pCurr; // set up the current pos
6183ddf9 388 if (start - spu.spuMemC < 0x1000) { // ?
389 //log_unhandled("ch%02d plays decode bufs @%05lx\n",
390 // ch, (long)(start - spu.spuMemC));
215ff9e6 391 ret = 1;
6183ddf9 392 }
e11ae5c5 393
5514a050 394 if (s_chan->prevflags & 1) // 1: stop/loop
3fc2a4c2 395 {
5514a050 396 if (!(s_chan->prevflags & 2))
215ff9e6 397 ret = 1;
e4f075af 398
5514a050 399 start = s_chan->pLoop;
3fc2a4c2 400 }
4622c0a1 401
402 check_irq(ch, start);
e11ae5c5 403
5514a050 404 predict_nr = start[0];
215ff9e6 405 shift_factor = predict_nr & 0xf;
e11ae5c5 406 predict_nr >>= 4;
e11ae5c5 407
011755d7 408 decode_block_data(SB, start + 2, predict_nr, shift_factor);
e11ae5c5 409
215ff9e6 410 flags = start[1];
4622c0a1 411 if (flags & 4 && !s_chan->bIgnoreLoop)
5514a050 412 s_chan->pLoop = start; // loop adress
e11ae5c5 413
215ff9e6 414 start += 16;
3fc2a4c2 415
5514a050 416 s_chan->pCurr = start; // store values for next cycle
417 s_chan->prevflags = flags;
6183ddf9 418 s_chan->bStarting = 0;
e11ae5c5 419
420 return ret;
421}
422
07a6dd2c 423// do block, but ignore sample data
424static int skip_block(int ch)
425{
5514a050 426 SPUCHAN *s_chan = &spu.s_chan[ch];
427 unsigned char *start = s_chan->pCurr;
215ff9e6 428 int flags;
429 int ret = 0;
430
5514a050 431 if (s_chan->prevflags & 1) {
432 if (!(s_chan->prevflags & 2))
215ff9e6 433 ret = 1;
07a6dd2c 434
5514a050 435 start = s_chan->pLoop;
215ff9e6 436 }
4622c0a1 437
438 check_irq(ch, start);
07a6dd2c 439
215ff9e6 440 flags = start[1];
4622c0a1 441 if (flags & 4 && !s_chan->bIgnoreLoop)
5514a050 442 s_chan->pLoop = start;
07a6dd2c 443
e4f075af 444 start += 16;
07a6dd2c 445
5514a050 446 s_chan->pCurr = start;
447 s_chan->prevflags = flags;
6183ddf9 448 s_chan->bStarting = 0;
e4f075af 449
89cb2058 450 return ret;
07a6dd2c 451}
452
650adfd2 453// if irq is going to trigger sooner than in upd_samples, set upd_samples
454static void scan_for_irq(int ch, unsigned int *upd_samples)
455{
5514a050 456 SPUCHAN *s_chan = &spu.s_chan[ch];
650adfd2 457 int pos, sinc, sinc_inv, end;
458 unsigned char *block;
459 int flags;
460
5514a050 461 block = s_chan->pCurr;
462 pos = s_chan->spos;
463 sinc = s_chan->sinc;
650adfd2 464 end = pos + *upd_samples * sinc;
b49db14f 465 if (s_chan->prevflags & 1) // 1: stop/loop
466 block = s_chan->pLoop;
650adfd2 467
5514a050 468 pos += (28 - s_chan->iSBPos) << 16;
650adfd2 469 while (pos < end)
470 {
3154bfab 471 if (block == spu.pSpuIrq)
650adfd2 472 break;
473 flags = block[1];
474 block += 16;
475 if (flags & 1) { // 1: stop/loop
5514a050 476 block = s_chan->pLoop;
650adfd2 477 }
478 pos += 28 << 16;
479 }
480
481 if (pos < end)
482 {
5514a050 483 sinc_inv = s_chan->sinc_inv;
650adfd2 484 if (sinc_inv == 0)
5514a050 485 sinc_inv = s_chan->sinc_inv = (0x80000000u / (uint32_t)sinc) << 1;
650adfd2 486
5514a050 487 pos -= s_chan->spos;
650adfd2 488 *upd_samples = (((uint64_t)pos * sinc_inv) >> 32) + 1;
489 //xprintf("ch%02d: irq sched: %3d %03d\n",
490 // ch, *upd_samples, *upd_samples * 60 * 263 / 44100);
491 }
492}
493
24dbf983 494#define make_do_samples(name, fmod_code, interp_start, interp_store, interp_get, interp_end) \
e62141d2 495static noinline int name(int *dst, \
3bd31caf 496 int (*decode_f)(void *context, int ch, int *SB), void *ctx, \
24dbf983 497 int ch, int ns_to, sample_buf *sb, int sinc, int *spos, int *sbpos) \
07a6dd2c 498{ \
215ff9e6 499 int ns, d, fa; \
500 int ret = ns_to; \
07a6dd2c 501 interp_start; \
502 \
215ff9e6 503 for (ns = 0; ns < ns_to; ns++) \
07a6dd2c 504 { \
505 fmod_code; \
506 \
63a4f6b6 507 *spos += sinc; \
508 while (*spos >= 0x10000) \
07a6dd2c 509 { \
24dbf983 510 fa = sb->SB[(*sbpos)++]; \
63a4f6b6 511 if (*sbpos >= 28) \
07a6dd2c 512 { \
63a4f6b6 513 *sbpos = 0; \
24dbf983 514 d = decode_f(ctx, ch, sb->SB); \
215ff9e6 515 if (d && ns < ret) \
516 ret = ns; \
07a6dd2c 517 } \
518 \
24dbf983 519 interp_store; \
63a4f6b6 520 *spos -= 0x10000; \
07a6dd2c 521 } \
522 \
24dbf983 523 interp_get; \
07a6dd2c 524 } \
525 \
07a6dd2c 526 interp_end; \
527 \
528 return ret; \
529}
530
24dbf983 531// helpers for simple linear interpolation: delay real val for two slots,
532// and calc the two deltas, for a 'look at the future behaviour'
07a6dd2c 533#define simple_interp_store \
24dbf983 534 sb->SB[28] = 0; \
535 sb->SB[29] = sb->SB[30]; \
536 sb->SB[30] = sb->SB[31]; \
537 sb->SB[31] = fa; \
538 sb->SB[32] = 1
07a6dd2c 539
540#define simple_interp_get \
011755d7 541 if(sinc<0x10000) /* -> upsampling? */ \
24dbf983 542 InterpolateUp(sb, sinc); /* --> interpolate up */ \
543 else InterpolateDown(sb, sinc); /* --> else down */ \
e62141d2 544 dst[ns] = sb->SB[29]
07a6dd2c 545
24dbf983 546make_do_samples(do_samples_nointerp, , fa = sb->SB[29],
e62141d2 547 , dst[ns] = fa, sb->SB[29] = fa)
24dbf983 548make_do_samples(do_samples_simple, , ,
07a6dd2c 549 simple_interp_store, simple_interp_get, )
24dbf983 550make_do_samples(do_samples_gauss, , ,
551 StoreInterpolationGaussCubic(sb, fa),
e62141d2 552 dst[ns] = GetInterpolationGauss(sb, *spos), )
24dbf983 553make_do_samples(do_samples_cubic, , ,
554 StoreInterpolationGaussCubic(sb, fa),
e62141d2 555 dst[ns] = GetInterpolationCubic(sb, *spos), )
24dbf983 556make_do_samples(do_samples_fmod,
e62141d2 557 sinc = FModChangeFrequency(spu.s_chan[ch].iRawPitch, ns, iFMod), ,
24dbf983 558 StoreInterpolationGaussCubic(sb, fa),
e62141d2 559 dst[ns] = GetInterpolationGauss(sb, *spos), )
24dbf983 560
e62141d2 561INLINE int do_samples_adpcm(int *dst,
24dbf983 562 int (*decode_f)(void *context, int ch, int *SB), void *ctx,
563 int ch, int ns_to, int fmod, sample_buf *sb, int sinc, int *spos, int *sbpos)
564{
565 int interp = spu.interpolation;
566 if (fmod == 1)
e62141d2 567 return do_samples_fmod(dst, decode_f, ctx, ch, ns_to, sb, sinc, spos, sbpos);
24dbf983 568 if (fmod)
569 interp = 2;
570 switch (interp) {
571 case 0:
e62141d2 572 return do_samples_nointerp(dst, decode_f, ctx, ch, ns_to, sb, sinc, spos, sbpos);
24dbf983 573 case 1:
e62141d2 574 return do_samples_simple (dst, decode_f, ctx, ch, ns_to, sb, sinc, spos, sbpos);
24dbf983 575 default:
e62141d2 576 return do_samples_gauss (dst, decode_f, ctx, ch, ns_to, sb, sinc, spos, sbpos);
24dbf983 577 case 3:
e62141d2 578 return do_samples_cubic (dst, decode_f, ctx, ch, ns_to, sb, sinc, spos, sbpos);
24dbf983 579 }
580}
07a6dd2c 581
215ff9e6 582static int do_samples_skip(int ch, int ns_to)
07a6dd2c 583{
5514a050 584 SPUCHAN *s_chan = &spu.s_chan[ch];
8f5f2dd5 585 int spos = s_chan->spos;
586 int sinc = s_chan->sinc;
215ff9e6 587 int ret = ns_to, ns, d;
b1094d0e 588
8f5f2dd5 589 spos += s_chan->iSBPos << 16;
215ff9e6 590
591 for (ns = 0; ns < ns_to; ns++)
07a6dd2c 592 {
8f5f2dd5 593 spos += sinc;
594 while (spos >= 28*0x10000)
215ff9e6 595 {
596 d = skip_block(ch);
597 if (d && ns < ret)
598 ret = ns;
8f5f2dd5 599 spos -= 28*0x10000;
215ff9e6 600 }
07a6dd2c 601 }
602
8f5f2dd5 603 s_chan->iSBPos = spos >> 16;
604 s_chan->spos = spos & 0xffff;
215ff9e6 605
606 return ret;
607}
608
e62141d2 609static int do_samples_skip_fmod(int ch, int ns_to, int *fmod_buf)
610{
611 SPUCHAN *s_chan = &spu.s_chan[ch];
612 int spos = s_chan->spos;
613 int ret = ns_to, ns, d;
614
615 spos += s_chan->iSBPos << 16;
616
617 for (ns = 0; ns < ns_to; ns++)
618 {
619 spos += FModChangeFrequency(s_chan->iRawPitch, ns, fmod_buf);
620 while (spos >= 28*0x10000)
621 {
622 d = skip_block(ch);
623 if (d && ns < ret)
624 ret = ns;
625 spos -= 28*0x10000;
626 }
627 }
628
629 s_chan->iSBPos = spos >> 16;
630 s_chan->spos = spos & 0xffff;
631
632 return ret;
633}
634
635static void do_lsfr_samples(int *dst, int ns_to, int ctrl,
215ff9e6 636 unsigned int *dwNoiseCount, unsigned int *dwNoiseVal)
637{
638 unsigned int counter = *dwNoiseCount;
639 unsigned int val = *dwNoiseVal;
640 unsigned int level, shift, bit;
641 int ns;
642
b1094d0e 643 // modified from DrHell/shalma, no fraction
215ff9e6 644 level = (ctrl >> 10) & 0x0f;
b1094d0e 645 level = 0x8000 >> level;
646
215ff9e6 647 for (ns = 0; ns < ns_to; ns++)
b1094d0e 648 {
215ff9e6 649 counter += 2;
650 if (counter >= level)
b1094d0e 651 {
215ff9e6 652 counter -= level;
653 shift = (val >> 10) & 0x1f;
b1094d0e 654 bit = (0x69696969 >> shift) & 1;
215ff9e6 655 bit ^= (val >> 15) & 1;
656 val = (val << 1) | bit;
b1094d0e 657 }
658
e62141d2 659 dst[ns] = (signed short)val;
b1094d0e 660 }
07a6dd2c 661
215ff9e6 662 *dwNoiseCount = counter;
663 *dwNoiseVal = val;
664}
665
e62141d2 666static int do_samples_noise(int *dst, int ch, int ns_to)
215ff9e6 667{
668 int ret;
669
670 ret = do_samples_skip(ch, ns_to);
671
e62141d2 672 do_lsfr_samples(dst, ns_to, spu.spuCtrl, &spu.dwNoiseCount, &spu.dwNoiseVal);
215ff9e6 673
89cb2058 674 return ret;
07a6dd2c 675}
676
665f33e1 677#ifdef HAVE_ARMV5
3a721c1f 678// asm code; lv and rv must be 0-3fff
3bd31caf 679extern void mix_chan(int *SSumLR, int count, int lv, int rv);
680extern void mix_chan_rvb(int *SSumLR, int count, int lv, int rv, int *rvb);
b17618c0 681#else
3bd31caf 682static void mix_chan(int *SSumLR, int count, int lv, int rv)
b17618c0 683{
3bd31caf 684 const int *src = ChanBuf;
b17618c0 685 int l, r;
686
687 while (count--)
688 {
689 int sval = *src++;
690
691 l = (sval * lv) >> 14;
692 r = (sval * rv) >> 14;
3bd31caf 693 *SSumLR++ += l;
694 *SSumLR++ += r;
b17618c0 695 }
696}
697
3bd31caf 698static void mix_chan_rvb(int *SSumLR, int count, int lv, int rv, int *rvb)
b17618c0 699{
3bd31caf 700 const int *src = ChanBuf;
701 int *dst = SSumLR;
702 int *drvb = rvb;
b17618c0 703 int l, r;
704
705 while (count--)
706 {
707 int sval = *src++;
708
709 l = (sval * lv) >> 14;
710 r = (sval * rv) >> 14;
711 *dst++ += l;
712 *dst++ += r;
713 *drvb++ += l;
714 *drvb++ += r;
715 }
716}
717#endif
718
b72f17a1 719// 0x0800-0x0bff Voice 1
720// 0x0c00-0x0fff Voice 3
215ff9e6 721static noinline void do_decode_bufs(unsigned short *mem, int which,
722 int count, int decode_pos)
b72f17a1 723{
215ff9e6 724 unsigned short *dst = &mem[0x800/2 + which*0x400/2];
725 const int *src = ChanBuf;
726 int cursor = decode_pos;
b72f17a1 727
728 while (count-- > 0)
729 {
ee9afdbd 730 cursor &= 0x1ff;
b72f17a1 731 dst[cursor] = *src++;
ee9afdbd 732 cursor++;
b72f17a1 733 }
734
735 // decode_pos is updated and irqs are checked later, after voice loop
736}
737
215ff9e6 738static void do_silent_chans(int ns_to, int silentch)
739{
5514a050 740 unsigned int mask;
741 SPUCHAN *s_chan;
215ff9e6 742 int ch;
743
5514a050 744 mask = silentch & 0xffffff;
745 for (ch = 0; mask != 0; ch++, mask >>= 1)
215ff9e6 746 {
5514a050 747 if (!(mask & 1)) continue;
215ff9e6 748 if (spu.dwChannelDead & (1<<ch)) continue;
5514a050 749
750 s_chan = &spu.s_chan[ch];
751 if (s_chan->pCurr > spu.pSpuIrq && s_chan->pLoop > spu.pSpuIrq)
215ff9e6 752 continue;
753
5514a050 754 s_chan->spos += s_chan->iSBPos << 16;
755 s_chan->iSBPos = 0;
215ff9e6 756
5514a050 757 s_chan->spos += s_chan->sinc * ns_to;
758 while (s_chan->spos >= 28 * 0x10000)
215ff9e6 759 {
5514a050 760 unsigned char *start = s_chan->pCurr;
215ff9e6 761
762 skip_block(ch);
5514a050 763 if (start == s_chan->pCurr || start - spu.spuMemC < 0x1000)
215ff9e6 764 {
765 // looping on self or stopped(?)
766 spu.dwChannelDead |= 1<<ch;
5514a050 767 s_chan->spos = 0;
215ff9e6 768 break;
769 }
770
5514a050 771 s_chan->spos -= 28 * 0x10000;
215ff9e6 772 }
773 }
774}
775
776static void do_channels(int ns_to)
777{
778 unsigned int mask;
05c7cec7 779 int do_rvb, ch, d;
5514a050 780 SPUCHAN *s_chan;
24dbf983 781
782 if (unlikely(spu.interpolation != spu_config.iUseInterpolation))
783 {
784 spu.interpolation = spu_config.iUseInterpolation;
785 mask = spu.dwChannelsAudible & 0xffffff;
786 for (ch = 0; mask != 0; ch++, mask >>= 1)
787 if (mask & 1)
788 ResetInterpolation(&spu.sb[ch]);
789 }
215ff9e6 790
05c7cec7 791 do_rvb = spu.rvb->StartAddr && spu_config.iUseReverb;
792 if (do_rvb)
793 memset(RVB, 0, ns_to * sizeof(RVB[0]) * 2);
3bd31caf 794
795 mask = spu.dwNewChannel & 0xffffff;
796 for (ch = 0; mask != 0; ch++, mask >>= 1) {
797 if (mask & 1)
798 StartSound(ch);
799 }
215ff9e6 800
2daa7c1d 801 mask = spu.dwChannelsAudible & 0xffffff;
215ff9e6 802 for (ch = 0; mask != 0; ch++, mask >>= 1) // loop em all...
803 {
804 if (!(mask & 1)) continue; // channel not playing? next
805
5514a050 806 s_chan = &spu.s_chan[ch];
5514a050 807 if (s_chan->bNoise)
e62141d2 808 d = do_samples_noise(ChanBuf, ch, ns_to);
215ff9e6 809 else
e62141d2 810 d = do_samples_adpcm(ChanBuf, decode_block, NULL, ch, ns_to, s_chan->bFMod,
24dbf983 811 &spu.sb[ch], s_chan->sinc, &s_chan->spos, &s_chan->iSBPos);
215ff9e6 812
6183ddf9 813 if (!s_chan->bStarting) {
e62141d2 814 d = MixADSR(ChanBuf, &s_chan->ADSRX, d);
6183ddf9 815 if (d < ns_to) {
816 spu.dwChannelsAudible &= ~(1 << ch);
817 s_chan->ADSRX.State = ADSR_RELEASE;
818 s_chan->ADSRX.EnvelopeVol = 0;
819 memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0]));
820 }
215ff9e6 821 }
822
823 if (ch == 1 || ch == 3)
824 {
825 do_decode_bufs(spu.spuMem, ch/2, ns_to, spu.decode_pos);
826 spu.decode_dirty_ch |= 1 << ch;
827 }
828
5514a050 829 if (s_chan->bFMod == 2) // fmod freq channel
215ff9e6 830 memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0]));
6df4c034 831 if (!(spu.spuCtrl & CTRL_MUTE))
832 ;
833 else if (s_chan->bRVBActive && do_rvb)
05c7cec7 834 mix_chan_rvb(spu.SSumLR, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume, RVB);
215ff9e6 835 else
3bd31caf 836 mix_chan(spu.SSumLR, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume);
215ff9e6 837 }
05c7cec7 838
740eb9bc 839 MixCD(spu.SSumLR, RVB, ns_to, spu.decode_pos);
e81f182d 840
05c7cec7 841 if (spu.rvb->StartAddr) {
7b2c4897 842 if (do_rvb)
05c7cec7 843 REVERBDo(spu.SSumLR, RVB, ns_to, spu.rvb->CurrAddr);
05c7cec7 844
845 spu.rvb->CurrAddr += ns_to / 2;
846 while (spu.rvb->CurrAddr >= 0x40000)
847 spu.rvb->CurrAddr -= 0x40000 - spu.rvb->StartAddr;
848 }
215ff9e6 849}
850
05c7cec7 851static void do_samples_finish(int *SSumLR, int ns_to,
3bd31caf 852 int silentch, int decode_pos);
63a4f6b6 853
854// optional worker thread handling
855
94f0c7c5 856#if P_HAVE_PTHREAD || defined(WANT_THREAD_CODE)
63a4f6b6 857
3bd31caf 858// worker thread state
859static struct spu_worker {
860 union {
861 struct {
92aae0e5 862 unsigned char exit_thread;
863 unsigned char prev_work_in_thread;
864 unsigned char pad[2];
3bd31caf 865 unsigned int i_ready;
866 unsigned int i_reaped;
05c7cec7 867 unsigned int last_boot_cnt; // dsp
0c1151fe 868 unsigned int ram_dirty;
92aae0e5 869 unsigned int channels_last;
3bd31caf 870 };
871 // aligning for C64X_DSP
872 unsigned int _pad0[128/4];
873 };
874 union {
875 struct {
876 unsigned int i_done;
877 unsigned int active; // dsp
878 unsigned int boot_cnt;
879 };
880 unsigned int _pad1[128/4];
881 };
882 struct work_item {
883 int ns_to;
884 int ctrl;
885 int decode_pos;
05c7cec7 886 int rvb_addr;
3bd31caf 887 unsigned int channels_new;
888 unsigned int channels_on;
889 unsigned int channels_silent;
890 struct {
891 int spos;
892 int sbpos;
893 int sinc;
894 int start;
895 int loop;
05c7cec7 896 short vol_l;
897 short vol_r;
4ecb720e 898 unsigned short ns_to;
899 unsigned short bNoise:1;
900 unsigned short bFMod:2;
901 unsigned short bRVBActive:1;
e62141d2 902 unsigned short bStarting:1;
3bd31caf 903 ADSRInfoEx adsr;
3bd31caf 904 } ch[24];
3bd31caf 905 int SSumLR[NSSIZE * 2];
906 } i[4];
907} *worker;
908
909#define WORK_MAXCNT (sizeof(worker->i) / sizeof(worker->i[0]))
910#define WORK_I_MASK (WORK_MAXCNT - 1)
911
5514a050 912static void thread_work_start(void);
3bd31caf 913static void thread_work_wait_sync(struct work_item *work, int force);
05c7cec7 914static void thread_sync_caches(void);
3bd31caf 915static int thread_get_i_done(void);
916
917static int decode_block_work(void *context, int ch, int *SB)
918{
919 const unsigned char *ram = spu.spuMemC;
920 int predict_nr, shift_factor, flags;
921 struct work_item *work = context;
922 int start = work->ch[ch].start;
923 int loop = work->ch[ch].loop;
924
925 predict_nr = ram[start];
926 shift_factor = predict_nr & 0xf;
927 predict_nr >>= 4;
928
929 decode_block_data(SB, ram + start + 2, predict_nr, shift_factor);
930
931 flags = ram[start + 1];
932 if (flags & 4)
933 loop = start; // loop adress
934
935 start += 16;
936
937 if (flags & 1) // 1: stop/loop
938 start = loop;
939
940 work->ch[ch].start = start & 0x7ffff;
941 work->ch[ch].loop = loop;
942
943 return 0;
944}
5514a050 945
3bd31caf 946static void queue_channel_work(int ns_to, unsigned int silentch)
63a4f6b6 947{
e62141d2 948 int tmpFMod[NSSIZE];
3bd31caf 949 struct work_item *work;
950 SPUCHAN *s_chan;
63a4f6b6 951 unsigned int mask;
3bd31caf 952 int ch, d;
63a4f6b6 953
3bd31caf 954 work = &worker->i[worker->i_ready & WORK_I_MASK];
955 work->ns_to = ns_to;
956 work->ctrl = spu.spuCtrl;
957 work->decode_pos = spu.decode_pos;
958 work->channels_silent = silentch;
959
92aae0e5 960 if (!worker->prev_work_in_thread) {
961 // copy adpcm and interpolation state to sb_thread
962 worker->prev_work_in_thread = 1;
963 mask = spu.dwChannelsAudible & ~spu.dwNewChannel & 0xffffff;
964 for (ch = 0; mask != 0; ch++, mask >>= 1) {
965 if (mask & 1)
966 memcpy(spu.sb_thread[ch].SB, spu.sb[ch].SB, sizeof(spu.sb_thread[ch].SB));
967 }
968 }
969
3bd31caf 970 mask = work->channels_new = spu.dwNewChannel & 0xffffff;
971 for (ch = 0; mask != 0; ch++, mask >>= 1) {
972 if (mask & 1)
e62141d2 973 StartSound(ch);
3bd31caf 974 }
975
2daa7c1d 976 mask = work->channels_on = spu.dwChannelsAudible & 0xffffff;
92aae0e5 977 worker->channels_last = mask;
3bd31caf 978 spu.decode_dirty_ch |= mask & 0x0a;
63a4f6b6 979
63a4f6b6 980 for (ch = 0; mask != 0; ch++, mask >>= 1)
981 {
982 if (!(mask & 1)) continue;
983
5514a050 984 s_chan = &spu.s_chan[ch];
3bd31caf 985 work->ch[ch].spos = s_chan->spos;
986 work->ch[ch].sbpos = s_chan->iSBPos;
987 work->ch[ch].sinc = s_chan->sinc;
988 work->ch[ch].adsr = s_chan->ADSRX;
05c7cec7 989 work->ch[ch].vol_l = s_chan->iLeftVolume;
990 work->ch[ch].vol_r = s_chan->iRightVolume;
3bd31caf 991 work->ch[ch].start = s_chan->pCurr - spu.spuMemC;
992 work->ch[ch].loop = s_chan->pLoop - spu.spuMemC;
4ecb720e 993 work->ch[ch].bNoise = s_chan->bNoise;
994 work->ch[ch].bFMod = s_chan->bFMod;
995 work->ch[ch].bRVBActive = s_chan->bRVBActive;
e62141d2 996 work->ch[ch].bStarting = s_chan->bStarting;
5514a050 997 if (s_chan->prevflags & 1)
3bd31caf 998 work->ch[ch].start = work->ch[ch].loop;
63a4f6b6 999
e62141d2 1000 if (unlikely(s_chan->bFMod == 2))
1001 {
1002 // sucks, have to do double work
1003 assert(!s_chan->bNoise);
1004 d = do_samples_gauss(tmpFMod, decode_block, NULL, ch, ns_to,
1005 &spu.sb[ch], s_chan->sinc, &s_chan->spos, &s_chan->iSBPos);
1006 if (!s_chan->bStarting) {
1007 d = MixADSR(tmpFMod, &s_chan->ADSRX, d);
1008 if (d < ns_to) {
1009 spu.dwChannelsAudible &= ~(1 << ch);
1010 s_chan->ADSRX.State = ADSR_RELEASE;
1011 s_chan->ADSRX.EnvelopeVol = 0;
1012 }
1013 }
1014 memset(&tmpFMod[d], 0, (ns_to - d) * sizeof(tmpFMod[d]));
1015 work->ch[ch].ns_to = d;
1016 continue;
1017 }
1018 if (unlikely(s_chan->bFMod))
1019 d = do_samples_skip_fmod(ch, ns_to, tmpFMod);
1020 else
1021 d = do_samples_skip(ch, ns_to);
3bd31caf 1022 work->ch[ch].ns_to = d;
1023
6183ddf9 1024 if (!s_chan->bStarting) {
1025 // note: d is not accurate on skip
1026 d = SkipADSR(&s_chan->ADSRX, d);
1027 if (d < ns_to) {
1028 spu.dwChannelsAudible &= ~(1 << ch);
1029 s_chan->ADSRX.State = ADSR_RELEASE;
1030 s_chan->ADSRX.EnvelopeVol = 0;
1031 }
3bd31caf 1032 }
e62141d2 1033 } // for (ch;;)
63a4f6b6 1034
05c7cec7 1035 work->rvb_addr = 0;
1036 if (spu.rvb->StartAddr) {
7b2c4897 1037 if (spu_config.iUseReverb)
05c7cec7 1038 work->rvb_addr = spu.rvb->CurrAddr;
05c7cec7 1039
1040 spu.rvb->CurrAddr += ns_to / 2;
1041 while (spu.rvb->CurrAddr >= 0x40000)
1042 spu.rvb->CurrAddr -= 0x40000 - spu.rvb->StartAddr;
1043 }
1044
3bd31caf 1045 worker->i_ready++;
5514a050 1046 thread_work_start();
63a4f6b6 1047}
1048
3bd31caf 1049static void do_channel_work(struct work_item *work)
63a4f6b6 1050{
3bd31caf 1051 unsigned int mask;
24dbf983 1052 int spos, sbpos;
63a4f6b6 1053 int d, ch, ns_to;
1054
3bd31caf 1055 ns_to = work->ns_to;
05c7cec7 1056
24dbf983 1057 if (unlikely(spu.interpolation != spu_config.iUseInterpolation))
1058 {
1059 spu.interpolation = spu_config.iUseInterpolation;
e62141d2 1060 mask = work->channels_on;
24dbf983 1061 for (ch = 0; mask != 0; ch++, mask >>= 1)
1062 if (mask & 1)
e62141d2 1063 ResetInterpolation(&spu.sb_thread[ch]);
24dbf983 1064 }
1065
05c7cec7 1066 if (work->rvb_addr)
1067 memset(RVB, 0, ns_to * sizeof(RVB[0]) * 2);
63a4f6b6 1068
3bd31caf 1069 mask = work->channels_new;
1070 for (ch = 0; mask != 0; ch++, mask >>= 1) {
1071 if (mask & 1)
e62141d2 1072 StartSoundSB(&spu.sb_thread[ch]);
3bd31caf 1073 }
1074
1075 mask = work->channels_on;
63a4f6b6 1076 for (ch = 0; mask != 0; ch++, mask >>= 1)
1077 {
1078 if (!(mask & 1)) continue;
1079
3bd31caf 1080 d = work->ch[ch].ns_to;
1081 spos = work->ch[ch].spos;
1082 sbpos = work->ch[ch].sbpos;
5514a050 1083
4ecb720e 1084 if (work->ch[ch].bNoise)
e62141d2 1085 do_lsfr_samples(ChanBuf, d, work->ctrl, &spu.dwNoiseCount, &spu.dwNoiseVal);
63a4f6b6 1086 else
e62141d2 1087 do_samples_adpcm(ChanBuf, decode_block_work, work, ch, d, work->ch[ch].bFMod,
1088 &spu.sb_thread[ch], work->ch[ch].sinc, &spos, &sbpos);
63a4f6b6 1089
e62141d2 1090 d = MixADSR(ChanBuf, &work->ch[ch].adsr, d);
63a4f6b6 1091 if (d < ns_to) {
3bd31caf 1092 work->ch[ch].adsr.EnvelopeVol = 0;
63a4f6b6 1093 memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0]));
1094 }
1095
1096 if (ch == 1 || ch == 3)
4ecb720e 1097 do_decode_bufs(spu.spuMem, ch/2, ns_to, work->decode_pos);
63a4f6b6 1098
4ecb720e 1099 if (work->ch[ch].bFMod == 2) // fmod freq channel
63a4f6b6 1100 memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0]));
4ecb720e 1101 if (work->ch[ch].bRVBActive && work->rvb_addr)
3bd31caf 1102 mix_chan_rvb(work->SSumLR, ns_to,
05c7cec7 1103 work->ch[ch].vol_l, work->ch[ch].vol_r, RVB);
63a4f6b6 1104 else
05c7cec7 1105 mix_chan(work->SSumLR, ns_to, work->ch[ch].vol_l, work->ch[ch].vol_r);
63a4f6b6 1106 }
05c7cec7 1107
1108 if (work->rvb_addr)
1109 REVERBDo(work->SSumLR, RVB, ns_to, work->rvb_addr);
63a4f6b6 1110}
1111
92aae0e5 1112static void sync_worker_thread(int force_no_thread)
63a4f6b6 1113{
92aae0e5 1114 int force = force_no_thread;
3bd31caf 1115 struct work_item *work;
1116 int done, used_space;
63a4f6b6 1117
7b2c4897 1118 // rvb offsets will change, thread may be using them
1119 force |= spu.rvb->dirty && spu.rvb->StartAddr;
1120
3bd31caf 1121 done = thread_get_i_done() - worker->i_reaped;
1122 used_space = worker->i_ready - worker->i_reaped;
0c1151fe 1123
3bd31caf 1124 //printf("done: %d use: %d dsp: %u/%u\n", done, used_space,
1125 // worker->boot_cnt, worker->i_done);
63a4f6b6 1126
3bd31caf 1127 while ((force && used_space > 0) || used_space >= WORK_MAXCNT || done > 0) {
1128 work = &worker->i[worker->i_reaped & WORK_I_MASK];
1129 thread_work_wait_sync(work, force);
63a4f6b6 1130
740eb9bc 1131 MixCD(work->SSumLR, RVB, work->ns_to, work->decode_pos);
05c7cec7 1132 do_samples_finish(work->SSumLR, work->ns_to,
3bd31caf 1133 work->channels_silent, work->decode_pos);
63a4f6b6 1134
3bd31caf 1135 worker->i_reaped++;
1136 done = thread_get_i_done() - worker->i_reaped;
1137 used_space = worker->i_ready - worker->i_reaped;
63a4f6b6 1138 }
92aae0e5 1139 if (force_no_thread && worker->prev_work_in_thread) {
1140 unsigned int ch, mask = worker->channels_last;
1141 worker->prev_work_in_thread = 0;
05c7cec7 1142 thread_sync_caches();
92aae0e5 1143 for (ch = 0; mask != 0; ch++, mask >>= 1) {
1144 if (mask & 1)
1145 memcpy(spu.sb[ch].SB, spu.sb_thread[ch].SB, sizeof(spu.sb_thread[ch].SB));
1146 }
1147 }
63a4f6b6 1148}
1149
1150#else
1151
1152static void queue_channel_work(int ns_to, int silentch) {}
92aae0e5 1153static void sync_worker_thread(int force_no_thread) {}
3bd31caf 1154
1155static const void * const worker = NULL;
63a4f6b6 1156
94f0c7c5 1157#endif // P_HAVE_PTHREAD || defined(WANT_THREAD_CODE)
63a4f6b6 1158
ef79bbde
P
1159////////////////////////////////////////////////////////////////////////
1160// MAIN SPU FUNCTION
6d75977b 1161// here is the main job handler...
ef79bbde
P
1162////////////////////////////////////////////////////////////////////////
1163
92aae0e5 1164void do_samples(unsigned int cycles_to, int force_no_thread)
ef79bbde 1165{
3bd31caf 1166 unsigned int silentch;
650adfd2 1167 int cycle_diff;
3bd31caf 1168 int ns_to;
ef79bbde 1169
3154bfab 1170 cycle_diff = cycles_to - spu.cycles_played;
650adfd2 1171 if (cycle_diff < -2*1048576 || cycle_diff > 2*1048576)
1172 {
6109649c 1173 log_unhandled("desync %u %d\n", cycles_to, cycle_diff);
3154bfab 1174 spu.cycles_played = cycles_to;
650adfd2 1175 return;
1176 }
e34a4bd3 1177
2daa7c1d 1178 silentch = ~(spu.dwChannelsAudible | spu.dwNewChannel) & 0xffffff;
3bd31caf 1179
92aae0e5 1180 force_no_thread |= (silentch == 0xffffff);
de4a0279 1181 if (worker != NULL)
92aae0e5 1182 sync_worker_thread(force_no_thread);
de4a0279 1183
650adfd2 1184 if (cycle_diff < 2 * 768)
1185 return;
e34a4bd3 1186
650adfd2 1187 ns_to = (cycle_diff / 768 + 1) & ~1;
1188 if (ns_to > NSSIZE) {
1189 // should never happen
6109649c 1190 log_unhandled("ns_to oflow %d %d\n", ns_to, NSSIZE);
650adfd2 1191 ns_to = NSSIZE;
1192 }
1193
1194 //////////////////////////////////////////////////////
1195 // special irq handling in the decode buffers (0x0000-0x1000)
1196 // we know:
1197 // the decode buffers are located in spu memory in the following way:
1198 // 0x0000-0x03ff CD audio left
1199 // 0x0400-0x07ff CD audio right
1200 // 0x0800-0x0bff Voice 1
1201 // 0x0c00-0x0fff Voice 3
1202 // and decoded data is 16 bit for one sample
1203 // we assume:
1204 // even if voices 1/3 are off or no cd audio is playing, the internal
1205 // play positions will move on and wrap after 0x400 bytes.
1206 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
1207 // increase this pointer on each sample by 2 bytes. If this pointer
1208 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
1209 // an IRQ.
1210
3154bfab 1211 if (unlikely((spu.spuCtrl & CTRL_IRQ)
c4c66b22 1212 && spu.pSpuIrq < spu.spuMemC+0x1000))
650adfd2 1213 {
3154bfab 1214 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
1215 int left = (irq_pos - spu.decode_pos) & 0x1ff;
650adfd2 1216 if (0 < left && left <= ns_to)
1217 {
3154bfab 1218 //xprintf("decoder irq %x\n", spu.decode_pos);
5fa59798 1219 do_irq(0);
650adfd2 1220 }
1221 }
56e500f3 1222 if (!spu.cycles_dma_end || (int)(spu.cycles_dma_end - cycles_to) < 0) {
1223 spu.cycles_dma_end = 0;
1224 check_irq_io(spu.spuAddr);
1225 }
650adfd2 1226
7b2c4897 1227 if (unlikely(spu.rvb->dirty))
1228 REVERBPrep();
1229
92aae0e5 1230 if (force_no_thread || worker == NULL || !spu_config.iUseThread) {
3bd31caf 1231 do_channels(ns_to);
05c7cec7 1232 do_samples_finish(spu.SSumLR, ns_to, silentch, spu.decode_pos);
63a4f6b6 1233 }
215ff9e6 1234 else {
3bd31caf 1235 queue_channel_work(ns_to, silentch);
4ecb720e 1236 //sync_worker_thread(1); // uncomment for debug
215ff9e6 1237 }
ef79bbde 1238
215ff9e6 1239 // advance "stopped" channels that can cause irqs
1240 // (all chans are always playing on the real thing..)
1241 if (spu.spuCtrl & CTRL_IRQ)
1242 do_silent_chans(ns_to, silentch);
1243
1244 spu.cycles_played += ns_to * 768;
1245 spu.decode_pos = (spu.decode_pos + ns_to) & 0x1ff;
6183ddf9 1246#if 0
1247 static int ccount; static time_t ctime; ccount++;
1248 if (time(NULL) != ctime)
1249 { printf("%d\n", ccount); ccount = 0; ctime = time(NULL); }
1250#endif
215ff9e6 1251}
1252
05c7cec7 1253static void do_samples_finish(int *SSumLR, int ns_to,
3bd31caf 1254 int silentch, int decode_pos)
215ff9e6 1255{
a0d4a8f5 1256 int vol_l = ((int)regAreaGet(H_SPUmvolL) << 17) >> 17;
1257 int vol_r = ((int)regAreaGet(H_SPUmvolR) << 17) >> 17;
215ff9e6 1258 int ns;
1259 int d;
78c60846 1260
3bd31caf 1261 // must clear silent channel decode buffers
1262 if(unlikely(silentch & spu.decode_dirty_ch & (1<<1)))
b72f17a1 1263 {
3154bfab 1264 memset(&spu.spuMem[0x800/2], 0, 0x400);
1265 spu.decode_dirty_ch &= ~(1<<1);
b72f17a1 1266 }
3154bfab 1267 if(unlikely(silentch & spu.decode_dirty_ch & (1<<3)))
b72f17a1 1268 {
3154bfab 1269 memset(&spu.spuMem[0xc00/2], 0, 0x400);
1270 spu.decode_dirty_ch &= ~(1<<3);
b72f17a1 1271 }
8680e822 1272
a0d4a8f5 1273 vol_l = vol_l * spu_config.iVolume >> 10;
1274 vol_r = vol_r * spu_config.iVolume >> 10;
1275
6df4c034 1276 if (!(vol_l | vol_r))
1775933a 1277 {
a0d4a8f5 1278 // muted? (rare)
3154bfab 1279 memset(spu.pS, 0, ns_to * 2 * sizeof(spu.pS[0]));
a0d4a8f5 1280 memset(SSumLR, 0, ns_to * 2 * sizeof(SSumLR[0]));
3154bfab 1281 spu.pS += ns_to * 2;
1775933a 1282 }
1283 else
650adfd2 1284 for (ns = 0; ns < ns_to * 2; )
ef79bbde 1285 {
9e7a7352 1286 d = SSumLR[ns]; SSumLR[ns] = 0;
e29eaeb9 1287 d = d * vol_l >> 14;
1775933a 1288 ssat32_to_16(d);
3154bfab 1289 *spu.pS++ = d;
97ea4077 1290 ns++;
ef79bbde 1291
9e7a7352 1292 d = SSumLR[ns]; SSumLR[ns] = 0;
e29eaeb9 1293 d = d * vol_r >> 14;
1775933a 1294 ssat32_to_16(d);
3154bfab 1295 *spu.pS++ = d;
97ea4077 1296 ns++;
ef79bbde 1297 }
650adfd2 1298}
ef79bbde 1299
650adfd2 1300void schedule_next_irq(void)
1301{
1302 unsigned int upd_samples;
1303 int ch;
ef79bbde 1304
3154bfab 1305 if (spu.scheduleCallback == NULL)
650adfd2 1306 return;
e34a4bd3 1307
650adfd2 1308 upd_samples = 44100 / 50;
e34a4bd3 1309
650adfd2 1310 for (ch = 0; ch < MAXCHAN; ch++)
1311 {
3154bfab 1312 if (spu.dwChannelDead & (1 << ch))
650adfd2 1313 continue;
5514a050 1314 if ((unsigned long)(spu.pSpuIrq - spu.s_chan[ch].pCurr) > IRQ_NEAR_BLOCKS * 16
1315 && (unsigned long)(spu.pSpuIrq - spu.s_chan[ch].pLoop) > IRQ_NEAR_BLOCKS * 16)
650adfd2 1316 continue;
4f9efe54 1317 if (spu.s_chan[ch].sinc == 0)
1318 continue;
16187bfb 1319
650adfd2 1320 scan_for_irq(ch, &upd_samples);
ef79bbde
P
1321 }
1322
3154bfab 1323 if (unlikely(spu.pSpuIrq < spu.spuMemC + 0x1000))
650adfd2 1324 {
3154bfab 1325 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
1326 int left = (irq_pos - spu.decode_pos) & 0x1ff;
650adfd2 1327 if (0 < left && left < upd_samples) {
3154bfab 1328 //xprintf("decode: %3d (%3d/%3d)\n", left, spu.decode_pos, irq_pos);
650adfd2 1329 upd_samples = left;
1330 }
1331 }
16187bfb 1332
650adfd2 1333 if (upd_samples < 44100 / 50)
3154bfab 1334 spu.scheduleCallback(upd_samples * 768);
ef79bbde
P
1335}
1336
1337// SPU ASYNC... even newer epsxe func
1338// 1 time every 'cycle' cycles... harhar
1339
650adfd2 1340// rearmed: called dynamically now
554a2220 1341
650adfd2 1342void CALLBACK SPUasync(unsigned int cycle, unsigned int flags)
ef79bbde 1343{
403a6290 1344 do_samples(cycle, 0);
fb552464 1345
3154bfab 1346 if (spu.spuCtrl & CTRL_IRQ)
650adfd2 1347 schedule_next_irq();
f8edb5bc 1348
650adfd2 1349 if (flags & 1) {
3154bfab 1350 out_current->feed(spu.pSpuBuffer, (unsigned char *)spu.pS - spu.pSpuBuffer);
1351 spu.pS = (short *)spu.pSpuBuffer;
fb552464 1352
3154bfab 1353 if (spu_config.iTempo) {
650adfd2 1354 if (!out_current->busy())
1355 // cause more samples to be generated
1356 // (and break some games because of bad sync)
3154bfab 1357 spu.cycles_played -= 44100 / 60 / 2 * 768;
ef79bbde 1358 }
650adfd2 1359 }
ef79bbde
P
1360}
1361
1362// SPU UPDATE... new epsxe func
1363// 1 time every 32 hsync lines
1364// (312/32)x50 in pal
1365// (262/32)x60 in ntsc
1366
1367// since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
1368// leave that func in the linux port, until epsxe linux is using
1369// the async function as well
1370
1371void CALLBACK SPUupdate(void)
1372{
ef79bbde
P
1373}
1374
1375// XA AUDIO
1376
740eb9bc 1377void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap, unsigned int cycle, int is_start)
ef79bbde
P
1378{
1379 if(!xap) return;
cdaef86c 1380 if(!xap->freq) return; // no xa freq ? bye
ef79bbde 1381
740eb9bc 1382 if (is_start)
1383 spu.XAPlay = spu.XAFeed = spu.XAStart;
78ce9be9 1384 if (spu.XAPlay == spu.XAFeed)
cdaef86c 1385 do_samples(cycle, 1); // catch up to prevent source underflows later
1386
1387 FeedXA(xap); // call main XA feeder
403a6290 1388 spu.xapGlobal = xap; // store info for save states
ef79bbde
P
1389}
1390
1391// CDDA AUDIO
78ce9be9 1392int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes, unsigned int cycle, int unused)
ef79bbde 1393{
983a7cfd 1394 if (!pcm) return -1;
1395 if (nbytes<=0) return -1;
ef79bbde 1396
78ce9be9 1397 if (spu.CDDAPlay == spu.CDDAFeed)
cdaef86c 1398 do_samples(cycle, 1); // catch up to prevent source underflows later
1399
403a6290 1400 FeedCDDA((unsigned char *)pcm, nbytes);
1401 return 0;
ef79bbde
P
1402}
1403
740eb9bc 1404void CALLBACK SPUsetCDvol(unsigned char ll, unsigned char lr,
1405 unsigned char rl, unsigned char rr, unsigned int cycle)
1406{
1407 if (spu.XAPlay != spu.XAFeed || spu.CDDAPlay != spu.CDDAFeed)
1408 do_samples(cycle, 1);
1409 spu.cdv.ll = ll;
1410 spu.cdv.lr = lr;
1411 spu.cdv.rl = rl;
1412 spu.cdv.rr = rr;
1413}
1414
6d75977b 1415// to be called after state load
1416void ClearWorkingState(void)
ef79bbde 1417{
5514a050 1418 memset(iFMod, 0, sizeof(iFMod));
3154bfab 1419 spu.pS=(short *)spu.pSpuBuffer; // setup soundbuffer pointer
ef79bbde
P
1420}
1421
1422// SETUPSTREAMS: init most of the spu buffers
05c7cec7 1423static void SetupStreams(void)
ef79bbde 1424{
3154bfab 1425 spu.pSpuBuffer = (unsigned char *)malloc(32768); // alloc mixing buffer
3bd31caf 1426 spu.SSumLR = calloc(NSSIZE * 2, sizeof(spu.SSumLR[0]));
ef79bbde 1427
2cb5735e 1428 spu.XAStart = malloc(44100 * sizeof(uint32_t)); // alloc xa buffer
3154bfab 1429 spu.XAEnd = spu.XAStart + 44100;
1430 spu.XAPlay = spu.XAStart;
1431 spu.XAFeed = spu.XAStart;
ef79bbde 1432
2cb5735e 1433 spu.CDDAStart = malloc(CDDA_BUFFER_SIZE); // alloc cdda buffer
403a6290 1434 spu.CDDAEnd = spu.CDDAStart + CDDA_BUFFER_SIZE / sizeof(uint32_t);
3154bfab 1435 spu.CDDAPlay = spu.CDDAStart;
1436 spu.CDDAFeed = spu.CDDAStart;
ef79bbde 1437
6d75977b 1438 ClearWorkingState();
ef79bbde
P
1439}
1440
1441// REMOVESTREAMS: free most buffer
05c7cec7 1442static void RemoveStreams(void)
ef79bbde 1443{
3154bfab 1444 free(spu.pSpuBuffer); // free mixing buffer
1445 spu.pSpuBuffer = NULL;
3bd31caf 1446 free(spu.SSumLR);
1447 spu.SSumLR = NULL;
3154bfab 1448 free(spu.XAStart); // free XA buffer
1449 spu.XAStart = NULL;
1450 free(spu.CDDAStart); // free CDDA buffer
1451 spu.CDDAStart = NULL;
ef79bbde
P
1452}
1453
5514a050 1454#if defined(C64X_DSP)
1455
1456/* special code for TI C64x DSP */
1457#include "spu_c64x.c"
1458
94f0c7c5 1459#elif P_HAVE_PTHREAD
5514a050 1460
1461#include <pthread.h>
1462#include <semaphore.h>
1463#include <unistd.h>
1464
1465static struct {
1466 pthread_t thread;
1467 sem_t sem_avail;
1468 sem_t sem_done;
1469} t;
1470
1471/* generic pthread implementation */
1472
1473static void thread_work_start(void)
1474{
1475 sem_post(&t.sem_avail);
1476}
1477
3bd31caf 1478static void thread_work_wait_sync(struct work_item *work, int force)
5514a050 1479{
1480 sem_wait(&t.sem_done);
1481}
63a4f6b6 1482
3bd31caf 1483static int thread_get_i_done(void)
de4a0279 1484{
3bd31caf 1485 return worker->i_done;
de4a0279 1486}
1487
05c7cec7 1488static void thread_sync_caches(void)
1489{
1490}
1491
63a4f6b6 1492static void *spu_worker_thread(void *unused)
1493{
3bd31caf 1494 struct work_item *work;
1495
63a4f6b6 1496 while (1) {
5514a050 1497 sem_wait(&t.sem_avail);
63a4f6b6 1498 if (worker->exit_thread)
1499 break;
1500
3bd31caf 1501 work = &worker->i[worker->i_done & WORK_I_MASK];
1502 do_channel_work(work);
1503 worker->i_done++;
63a4f6b6 1504
5514a050 1505 sem_post(&t.sem_done);
63a4f6b6 1506 }
1507
1508 return NULL;
1509}
1510
1511static void init_spu_thread(void)
1512{
1513 int ret;
1514
263c6482 1515 spu.sb_thread = spu.sb_thread_;
1516
63a4f6b6 1517 if (sysconf(_SC_NPROCESSORS_ONLN) <= 1)
1518 return;
1519
1520 worker = calloc(1, sizeof(*worker));
1521 if (worker == NULL)
1522 return;
5514a050 1523 ret = sem_init(&t.sem_avail, 0, 0);
63a4f6b6 1524 if (ret != 0)
1525 goto fail_sem_avail;
5514a050 1526 ret = sem_init(&t.sem_done, 0, 0);
63a4f6b6 1527 if (ret != 0)
1528 goto fail_sem_done;
1529
5514a050 1530 ret = pthread_create(&t.thread, NULL, spu_worker_thread, NULL);
63a4f6b6 1531 if (ret != 0)
1532 goto fail_thread;
1533
3bd31caf 1534 spu_config.iThreadAvail = 1;
63a4f6b6 1535 return;
1536
1537fail_thread:
5514a050 1538 sem_destroy(&t.sem_done);
63a4f6b6 1539fail_sem_done:
5514a050 1540 sem_destroy(&t.sem_avail);
63a4f6b6 1541fail_sem_avail:
1542 free(worker);
1543 worker = NULL;
3bd31caf 1544 spu_config.iThreadAvail = 0;
63a4f6b6 1545}
1546
1547static void exit_spu_thread(void)
1548{
1549 if (worker == NULL)
1550 return;
1551 worker->exit_thread = 1;
5514a050 1552 sem_post(&t.sem_avail);
1553 pthread_join(t.thread, NULL);
1554 sem_destroy(&t.sem_done);
1555 sem_destroy(&t.sem_avail);
63a4f6b6 1556 free(worker);
1557 worker = NULL;
1558}
1559
94f0c7c5 1560#else // if !P_HAVE_PTHREAD
63a4f6b6 1561
1562static void init_spu_thread(void)
1563{
1564}
1565
1566static void exit_spu_thread(void)
1567{
1568}
1569
1570#endif
ef79bbde
P
1571
1572// SPUINIT: this func will be called first by the main emu
1573long CALLBACK SPUinit(void)
1574{
05c7cec7 1575 int i;
1576
9bb93b77 1577 memset(&spu, 0, sizeof(spu));
30594533 1578 spu.spuMemC = calloc(1, 512 * 1024 + 16);
1579 // a guard for runaway channels - End+Mute
1580 spu.spuMemC[512 * 1024 + 1] = 1;
1581
ef79bbde
P
1582 InitADSR();
1583
5514a050 1584 spu.s_chan = calloc(MAXCHAN+1, sizeof(spu.s_chan[0])); // channel + 1 infos (1 is security for fmod handling)
05c7cec7 1585 spu.rvb = calloc(1, sizeof(REVERBInfo));
5514a050 1586
1587 spu.spuAddr = 0;
3154bfab 1588 spu.decode_pos = 0;
c4c66b22 1589 spu.pSpuIrq = spu.spuMemC;
ef79bbde 1590
ef79bbde
P
1591 SetupStreams(); // prepare streaming
1592
3154bfab 1593 if (spu_config.iVolume == 0)
1594 spu_config.iVolume = 768; // 1024 is 1.0
1595
63a4f6b6 1596 init_spu_thread();
1597
05c7cec7 1598 for (i = 0; i < MAXCHAN; i++) // loop sound channels
1599 {
1600 spu.s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
1601 spu.s_chan[i].ADSRX.SustainIncrease = 1;
1602 spu.s_chan[i].pLoop = spu.spuMemC;
1603 spu.s_chan[i].pCurr = spu.spuMemC;
9601ca6d 1604 spu.s_chan[i].bIgnoreLoop = 0;
05c7cec7 1605 }
1606
1607 spu.bSpuInit=1; // flag: we are inited
1608
ef79bbde
P
1609 return 0;
1610}
1611
1612// SPUOPEN: called by main emu after init
1613long CALLBACK SPUopen(void)
1614{
3154bfab 1615 if (spu.bSPUIsOpen) return 0; // security for some stupid main emus
ef79bbde
P
1616
1617 SetupSound(); // setup sound (before init!)
ef79bbde 1618
3154bfab 1619 spu.bSPUIsOpen = 1;
ef79bbde
P
1620
1621 return PSE_SPU_ERR_SUCCESS;
1622}
1623
1624// SPUCLOSE: called before shutdown
1625long CALLBACK SPUclose(void)
1626{
3154bfab 1627 if (!spu.bSPUIsOpen) return 0; // some security
ef79bbde 1628
3154bfab 1629 spu.bSPUIsOpen = 0; // no more open
ef79bbde 1630
07c13dfd 1631 out_current->finish(); // no more sound handling
ef79bbde
P
1632
1633 return 0;
1634}
1635
1636// SPUSHUTDOWN: called by main emu on final exit
1637long CALLBACK SPUshutdown(void)
1638{
1639 SPUclose();
ef79bbde 1640
63a4f6b6 1641 exit_spu_thread();
1642
5514a050 1643 free(spu.spuMemC);
1644 spu.spuMemC = NULL;
1645 free(spu.s_chan);
1646 spu.s_chan = NULL;
05c7cec7 1647 free(spu.rvb);
1648 spu.rvb = NULL;
5514a050 1649
1650 RemoveStreams(); // no more streaming
1651 spu.bSpuInit=0;
1652
ef79bbde
P
1653 return 0;
1654}
1655
ef79bbde
P
1656// SETUP CALLBACKS
1657// this functions will be called once,
1658// passes a callback that should be called on SPU-IRQ/cdda volume change
6e52baf5 1659void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(int))
ef79bbde 1660{
3154bfab 1661 spu.irqCallback = callback;
ef79bbde
P
1662}
1663
7d6997de 1664void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(short, short))
ef79bbde 1665{
60073003 1666 //spu.cddavCallback = CDDAVcallback;
ef79bbde
P
1667}
1668
2b30c129 1669void CALLBACK SPUregisterScheduleCb(void (CALLBACK *callback)(unsigned int))
1670{
3154bfab 1671 spu.scheduleCallback = callback;
2b30c129 1672}
1673
ef79bbde 1674// COMMON PLUGIN INFO FUNCS
e906c010 1675/*
ef79bbde
P
1676char * CALLBACK PSEgetLibName(void)
1677{
1678 return _(libraryName);
1679}
1680
1681unsigned long CALLBACK PSEgetLibType(void)
1682{
1683 return PSE_LT_SPU;
1684}
1685
1686unsigned long CALLBACK PSEgetLibVersion(void)
1687{
1688 return (1 << 16) | (6 << 8);
1689}
1690
1691char * SPUgetLibInfos(void)
1692{
1693 return _(libraryInfo);
1694}
e906c010 1695*/
6d866bb7 1696
90f1d26c 1697// debug
174c454a 1698void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
90f1d26c 1699{
174c454a 1700 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
90f1d26c 1701
5514a050 1702 if (spu.s_chan == NULL)
1703 return;
1704
90f1d26c 1705 for(;ch<MAXCHAN;ch++)
1706 {
2daa7c1d 1707 if (!(spu.dwChannelsAudible & (1<<ch)))
90f1d26c 1708 continue;
5514a050 1709 if (spu.s_chan[ch].bFMod == 2)
90f1d26c 1710 fmod_chans |= 1 << ch;
5514a050 1711 if (spu.s_chan[ch].bNoise)
90f1d26c 1712 noise_chans |= 1 << ch;
5514a050 1713 if((spu.spuCtrl&CTRL_IRQ) && spu.s_chan[ch].pCurr <= spu.pSpuIrq && spu.s_chan[ch].pLoop <= spu.pSpuIrq)
174c454a 1714 irq_chans |= 1 << ch;
90f1d26c 1715 }
1716
2daa7c1d 1717 *chans_out = spu.dwChannelsAudible;
1718 *run_chans = ~spu.dwChannelsAudible & ~spu.dwChannelDead & irq_chans;
90f1d26c 1719 *fmod_chans_out = fmod_chans;
1720 *noise_chans_out = noise_chans;
1721}
1722
6d866bb7 1723// vim:shiftwidth=1:expandtab