1 /***************************************************************************
2                          externals.h  -  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 #ifndef __P_SOUND_EXTERNALS_H__
19 #define __P_SOUND_EXTERNALS_H__
20 
21 #include <stdint.h>
22 
23 /////////////////////////////////////////////////////////
24 // generic defines
25 /////////////////////////////////////////////////////////
26 
27 #ifdef __GNUC__
28 #define noinline __attribute__((noinline))
29 #define unlikely(x) __builtin_expect((x), 0)
30 #else
31 #define noinline
32 #define unlikely(x) x
33 #endif
34 #if defined(__GNUC__) && !defined(_TMS320C6X)
35 #define preload __builtin_prefetch
36 #else
37 #define preload(...)
38 #endif
39 
40 #define PSE_LT_SPU                  4
41 #define PSE_SPU_ERR_SUCCESS         0
42 #define PSE_SPU_ERR                 -60
43 #define PSE_SPU_ERR_NOTCONFIGURED   PSE_SPU_ERR - 1
44 #define PSE_SPU_ERR_INIT            PSE_SPU_ERR - 2
45 #ifndef max
46 #define max(a,b)            (((a) > (b)) ? (a) : (b))
47 #define min(a,b)            (((a) < (b)) ? (a) : (b))
48 #endif
49 
50 ////////////////////////////////////////////////////////////////////////
51 // spu defines
52 ////////////////////////////////////////////////////////////////////////
53 
54 // num of channels
55 #define MAXCHAN     24
56 
57 // note: must be even due to the way reverb works now
58 #define NSSIZE ((44100 / 50 + 16) & ~1)
59 
60 ///////////////////////////////////////////////////////////
61 // struct defines
62 ///////////////////////////////////////////////////////////
63 
64 enum ADSR_State {
65  ADSR_ATTACK = 0,
66  ADSR_DECAY = 1,
67  ADSR_SUSTAIN = 2,
68  ADSR_RELEASE = 3,
69 };
70 
71 // ADSR INFOS PER CHANNEL
72 typedef struct
73 {
74  unsigned char  State:2;                               // ADSR_State
75  unsigned char  AttackModeExp:1;
76  unsigned char  SustainModeExp:1;
77  unsigned char  SustainIncrease:1;
78  unsigned char  ReleaseModeExp:1;
79  unsigned char  AttackRate;
80  unsigned char  DecayRate;
81  unsigned char  SustainLevel;
82  unsigned char  SustainRate;
83  unsigned char  ReleaseRate;
84  int            EnvelopeVol;
85 } ADSRInfoEx;
86 
87 ///////////////////////////////////////////////////////////
88 
89 // Tmp Flags
90 
91 // used for debug channel muting
92 #define FLAG_MUTE  1
93 
94 // used for simple interpolation
95 #define FLAG_IPOL0 2
96 #define FLAG_IPOL1 4
97 
98 ///////////////////////////////////////////////////////////
99 
100 // MAIN CHANNEL STRUCT
101 typedef struct
102 {
103  int               iSBPos;                             // mixing stuff
104  int               spos;
105  int               sinc;
106  int               sinc_inv;
107 
108  unsigned char *   pCurr;                              // current pos in sound mem
109  unsigned char *   pLoop;                              // loop ptr in sound mem
110 
111  unsigned int      bReverb:1;                          // can we do reverb on this channel? must have ctrl register bit, to get active
112  unsigned int      bRVBActive:1;                       // reverb active flag
113  unsigned int      bNoise:1;                           // noise active flag
114  unsigned int      bFMod:2;                            // freq mod (0=off, 1=sound channel, 2=freq channel)
115  unsigned int      prevflags:3;                        // flags from previous block
116 
117  int               iLeftVolume;                        // left volume
118  int               iRightVolume;                       // right volume
119  ADSRInfoEx        ADSRX;
120  int               iRawPitch;                          // raw pitch (0...3fff)
121 } SPUCHAN;
122 
123 ///////////////////////////////////////////////////////////
124 
125 typedef struct
126 {
127  int StartAddr;      // reverb area start addr in samples
128  int CurrAddr;       // reverb area curr addr in samples
129 
130  int VolLeft;
131  int VolRight;
132 
133  int FB_SRC_A;       // (offset)
134  int FB_SRC_B;       // (offset)
135  int IIR_ALPHA;      // (coef.)
136  int ACC_COEF_A;     // (coef.)
137  int ACC_COEF_B;     // (coef.)
138  int ACC_COEF_C;     // (coef.)
139  int ACC_COEF_D;     // (coef.)
140  int IIR_COEF;       // (coef.)
141  int FB_ALPHA;       // (coef.)
142  int FB_X;           // (coef.)
143  int IIR_DEST_A0;    // (offset)
144  int IIR_DEST_A1;    // (offset)
145  int ACC_SRC_A0;     // (offset)
146  int ACC_SRC_A1;     // (offset)
147  int ACC_SRC_B0;     // (offset)
148  int ACC_SRC_B1;     // (offset)
149  int IIR_SRC_A0;     // (offset)
150  int IIR_SRC_A1;     // (offset)
151  int IIR_DEST_B0;    // (offset)
152  int IIR_DEST_B1;    // (offset)
153  int ACC_SRC_C0;     // (offset)
154  int ACC_SRC_C1;     // (offset)
155  int ACC_SRC_D0;     // (offset)
156  int ACC_SRC_D1;     // (offset)
157  int IIR_SRC_B1;     // (offset)
158  int IIR_SRC_B0;     // (offset)
159  int MIX_DEST_A0;    // (offset)
160  int MIX_DEST_A1;    // (offset)
161  int MIX_DEST_B0;    // (offset)
162  int MIX_DEST_B1;    // (offset)
163  int IN_COEF_L;      // (coef.)
164  int IN_COEF_R;      // (coef.)
165 
166  int dirty;          // registers changed
167 
168  // MIX_DEST_xx - FB_SRC_x
169  int FB_SRC_A0, FB_SRC_A1, FB_SRC_B0, FB_SRC_B1;
170 } REVERBInfo;
171 
172 ///////////////////////////////////////////////////////////
173 
174 // psx buffers / addresses
175 
176 #define SB_SIZE (32 + 4)
177 
178 typedef struct
179 {
180  unsigned short  spuCtrl;
181  unsigned short  spuStat;
182 
183  unsigned int    spuAddr;
184  union {
185   unsigned char  *spuMemC;
186   unsigned short *spuMem;
187  };
188  unsigned char * pSpuIrq;
189 
190  unsigned int    cycles_played;
191  int             decode_pos;
192  int             decode_dirty_ch;
193  unsigned int    bSpuInit:1;
194  unsigned int    bSPUIsOpen:1;
195  unsigned int    bMemDirty:1;          // had external write to SPU RAM
196 
197  unsigned int    dwNoiseVal;           // global noise generator
198  unsigned int    dwNoiseCount;
199  unsigned int    dwNewChannel;         // flags for faster testing, if new channel starts
200  unsigned int    dwChannelOn;          // not silent channels
201  unsigned int    dwChannelDead;        // silent+not useful channels
202 
203  unsigned char * pSpuBuffer;
204  short         * pS;
205 
206  void (CALLBACK *irqCallback)(void);   // func of main emu, called on spu irq
207  void (CALLBACK *cddavCallback)(unsigned short,unsigned short);
208  void (CALLBACK *scheduleCallback)(unsigned int);
209 
210  xa_decode_t   * xapGlobal;
211  unsigned int  * XAFeed;
212  unsigned int  * XAPlay;
213  unsigned int  * XAStart;
214  unsigned int  * XAEnd;
215 
216  unsigned int  * CDDAFeed;
217  unsigned int  * CDDAPlay;
218  unsigned int  * CDDAStart;
219  unsigned int  * CDDAEnd;
220 
221  unsigned int    XARepeat;
222  unsigned int    XALastVal;
223 
224  int             iLeftXAVol;
225  int             iRightXAVol;
226 
227  SPUCHAN       * s_chan;
228  REVERBInfo    * rvb;
229 
230  // buffers
231  int           * SB;
232  int           * SSumLR;
233 
234  int             pad[29];
235  unsigned short  regArea[0x400];
236 } SPUInfo;
237 
238 ///////////////////////////////////////////////////////////
239 // SPU.C globals
240 ///////////////////////////////////////////////////////////
241 
242 #ifndef _IN_SPU
243 
244 extern SPUInfo spu;
245 
246 void do_samples(unsigned int cycles_to, int do_sync);
247 void schedule_next_irq(void);
248 
249 #define regAreaGet(ch,offset) \
250   spu.regArea[((ch<<4)|(offset))>>1]
251 
252 #define do_samples_if_needed(c, sync) \
253  do { \
254   if (sync || (int)((c) - spu.cycles_played) >= 16 * 768) \
255    do_samples(c, sync); \
256  } while (0)
257 
258 #endif
259 
260 #endif /* __P_SOUND_EXTERNALS_H__ */
261