Merge pull request #705 from pcercuei/lightrec-disable-threaded-compiler
[pcsx_rearmed.git] / plugins / dfsound / registers.c
... / ...
CommitLineData
1/***************************************************************************\r
2 registers.c - description\r
3 -------------------\r
4 begin : Wed May 15 2002\r
5 copyright : (C) 2002 by Pete Bernert\r
6 email : BlackDove@addcom.de\r
7 ***************************************************************************/\r
8/***************************************************************************\r
9 * *\r
10 * This program is free software; you can redistribute it and/or modify *\r
11 * it under the terms of the GNU General Public License as published by *\r
12 * the Free Software Foundation; either version 2 of the License, or *\r
13 * (at your option) any later version. See also the license.txt file for *\r
14 * additional informations. *\r
15 * *\r
16 ***************************************************************************/\r
17\r
18#include "stdafx.h"\r
19\r
20#define _IN_REGISTERS\r
21\r
22#include "externals.h"\r
23#include "registers.h"\r
24#include "spu_config.h"\r
25\r
26static void SoundOn(int start,int end,unsigned short val);\r
27static void SoundOff(int start,int end,unsigned short val);\r
28static void FModOn(int start,int end,unsigned short val);\r
29static void NoiseOn(int start,int end,unsigned short val);\r
30static void SetVolumeL(unsigned char ch,short vol);\r
31static void SetVolumeR(unsigned char ch,short vol);\r
32static void SetPitch(int ch,unsigned short val);\r
33static void ReverbOn(int start,int end,unsigned short val);\r
34\r
35////////////////////////////////////////////////////////////////////////\r
36// WRITE REGISTERS: called by main emu\r
37////////////////////////////////////////////////////////////////////////\r
38\r
39static const uint32_t ignore_dupe[8] = {\r
40 // ch 0-15 c40 c80 cc0\r
41 0x7f7f7f7f, 0x7f7f7f7f, 0x7f7f7f7f, 0x7f7f7f7f,\r
42 // ch 16-24 d40 control reverb\r
43 0x7f7f7f7f, 0x7f7f7f7f, 0xff05ff0f, 0xffffffff\r
44};\r
45\r
46void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val,\r
47 unsigned int cycles)\r
48{\r
49 int r = reg & 0xfff;\r
50 int rofs = (r - 0xc00) >> 1;\r
51 int changed = spu.regArea[rofs] != val;\r
52 spu.regArea[rofs] = val;\r
53\r
54 if (!changed && (ignore_dupe[rofs >> 5] & (1 << (rofs & 0x1f))))\r
55 return;\r
56 // zero keyon/keyoff?\r
57 if (val == 0 && (r & 0xff8) == 0xd88)\r
58 return;\r
59\r
60 do_samples_if_needed(cycles, 0);\r
61\r
62 if(r>=0x0c00 && r<0x0d80) // some channel info?\r
63 {\r
64 int ch=(r>>4)-0xc0; // calc channel\r
65 switch(r&0x0f)\r
66 {\r
67 //------------------------------------------------// r volume\r
68 case 0: \r
69 SetVolumeL((unsigned char)ch,val);\r
70 break;\r
71 //------------------------------------------------// l volume\r
72 case 2: \r
73 SetVolumeR((unsigned char)ch,val);\r
74 break;\r
75 //------------------------------------------------// pitch\r
76 case 4: \r
77 SetPitch(ch,val);\r
78 goto upd_irq;\r
79 //------------------------------------------------// start\r
80 case 6: \r
81 // taken from regArea later\r
82 break;\r
83 //------------------------------------------------// level with pre-calcs\r
84 case 8:\r
85 {\r
86 const unsigned long lval=val;\r
87 //---------------------------------------------//\r
88 spu.s_chan[ch].ADSRX.AttackModeExp=(lval&0x8000)?1:0;\r
89 spu.s_chan[ch].ADSRX.AttackRate=(lval>>8) & 0x007f;\r
90 spu.s_chan[ch].ADSRX.DecayRate=(lval>>4) & 0x000f;\r
91 spu.s_chan[ch].ADSRX.SustainLevel=lval & 0x000f;\r
92 //---------------------------------------------//\r
93 }\r
94 break;\r
95 //------------------------------------------------// adsr times with pre-calcs\r
96 case 10:\r
97 {\r
98 const unsigned long lval=val;\r
99\r
100 //----------------------------------------------//\r
101 spu.s_chan[ch].ADSRX.SustainModeExp = (lval&0x8000)?1:0;\r
102 spu.s_chan[ch].ADSRX.SustainIncrease= (lval&0x4000)?0:1;\r
103 spu.s_chan[ch].ADSRX.SustainRate = (lval>>6) & 0x007f;\r
104 spu.s_chan[ch].ADSRX.ReleaseModeExp = (lval&0x0020)?1:0;\r
105 spu.s_chan[ch].ADSRX.ReleaseRate = lval & 0x001f;\r
106 //----------------------------------------------//\r
107 }\r
108 break;\r
109 //------------------------------------------------// adsr volume... mmm have to investigate this\r
110 case 12:\r
111 break;\r
112 //------------------------------------------------//\r
113 case 14: // loop?\r
114 spu.s_chan[ch].pLoop=spu.spuMemC+((val&~1)<<3);\r
115 spu.s_chan[ch].bIgnoreLoop = 1;\r
116 goto upd_irq;\r
117 //------------------------------------------------//\r
118 }\r
119 return;\r
120 }\r
121\r
122 switch(r)\r
123 {\r
124 //-------------------------------------------------//\r
125 case H_SPUaddr:\r
126 spu.spuAddr = (unsigned long) val<<3;\r
127 break;\r
128 //-------------------------------------------------//\r
129 case H_SPUdata:\r
130 *(unsigned short *)(spu.spuMemC + spu.spuAddr) = val;\r
131 spu.spuAddr += 2;\r
132 spu.spuAddr &= 0x7fffe;\r
133 break;\r
134 //-------------------------------------------------//\r
135 case H_SPUctrl:\r
136 if (!(spu.spuCtrl & CTRL_IRQ)) {\r
137 spu.spuStat&=~STAT_IRQ;\r
138 if (val & CTRL_IRQ)\r
139 schedule_next_irq();\r
140 }\r
141 spu.spuCtrl=val;\r
142 break;\r
143 //-------------------------------------------------//\r
144 case H_SPUstat:\r
145 spu.spuStat=val&0xf800;\r
146 break;\r
147 //-------------------------------------------------//\r
148 case H_SPUReverbAddr:\r
149 goto rvbd;\r
150 //-------------------------------------------------//\r
151 case H_SPUirqAddr:\r
152 spu.pSpuIrq=spu.spuMemC+(((unsigned long) val<<3)&~0xf);\r
153 goto upd_irq;\r
154 //-------------------------------------------------//\r
155 case H_SPUrvolL:\r
156 spu.rvb->VolLeft=val;\r
157 break;\r
158 //-------------------------------------------------//\r
159 case H_SPUrvolR:\r
160 spu.rvb->VolRight=val;\r
161 break;\r
162 //-------------------------------------------------//\r
163\r
164 case H_SPUmvolL:\r
165 case H_SPUmvolR:\r
166 if (val & 0x8000)\r
167 log_unhandled("w master sweep: %08lx %04x\n", reg, val);\r
168 break;\r
169\r
170 case 0x0dac:\r
171 if (val != 4)\r
172 log_unhandled("1f801dac %04x\n", val);\r
173 break;\r
174\r
175/*\r
176 case H_ExtLeft:\r
177 //auxprintf("EL %d\n",val);\r
178 break;\r
179 //-------------------------------------------------//\r
180 case H_ExtRight:\r
181 //auxprintf("ER %d\n",val);\r
182 break;\r
183 //-------------------------------------------------//\r
184 case H_SPUmvolL:\r
185 //auxprintf("ML %d\n",val);\r
186 break;\r
187 //-------------------------------------------------//\r
188 case H_SPUmvolR:\r
189 //auxprintf("MR %d\n",val);\r
190 break;\r
191 //-------------------------------------------------//\r
192 case H_SPUMute1:\r
193 //auxprintf("M0 %04x\n",val);\r
194 break;\r
195 //-------------------------------------------------//\r
196 case H_SPUMute2:\r
197 //auxprintf("M1 %04x\n",val);\r
198 break;\r
199*/\r
200 //-------------------------------------------------//\r
201 case H_SPUon1:\r
202 SoundOn(0,16,val);\r
203 break;\r
204 //-------------------------------------------------//\r
205 case H_SPUon2:\r
206 SoundOn(16,24,val);\r
207 break;\r
208 //-------------------------------------------------//\r
209 case H_SPUoff1:\r
210 SoundOff(0,16,val);\r
211 break;\r
212 //-------------------------------------------------//\r
213 case H_SPUoff2:\r
214 SoundOff(16,24,val);\r
215 break;\r
216 //-------------------------------------------------//\r
217 case H_CDLeft:\r
218 spu.iLeftXAVol=(int16_t)val;\r
219 if(spu.cddavCallback) spu.cddavCallback(0,(int16_t)val);\r
220 break;\r
221 case H_CDRight:\r
222 spu.iRightXAVol=(int16_t)val;\r
223 if(spu.cddavCallback) spu.cddavCallback(1,(int16_t)val);\r
224 break;\r
225 //-------------------------------------------------//\r
226 case H_FMod1:\r
227 FModOn(0,16,val);\r
228 break;\r
229 //-------------------------------------------------//\r
230 case H_FMod2:\r
231 FModOn(16,24,val);\r
232 break;\r
233 //-------------------------------------------------//\r
234 case H_Noise1:\r
235 NoiseOn(0,16,val);\r
236 break;\r
237 //-------------------------------------------------//\r
238 case H_Noise2:\r
239 NoiseOn(16,24,val);\r
240 break;\r
241 //-------------------------------------------------//\r
242 case H_RVBon1:\r
243 ReverbOn(0,16,val);\r
244 break;\r
245 //-------------------------------------------------//\r
246 case H_RVBon2:\r
247 ReverbOn(16,24,val);\r
248 break;\r
249 //-------------------------------------------------//\r
250 case H_Reverb+0 : goto rvbd;\r
251 case H_Reverb+2 : goto rvbd;\r
252 case H_Reverb+4 : spu.rvb->IIR_ALPHA=(short)val; break;\r
253 case H_Reverb+6 : spu.rvb->ACC_COEF_A=(short)val; break;\r
254 case H_Reverb+8 : spu.rvb->ACC_COEF_B=(short)val; break;\r
255 case H_Reverb+10 : spu.rvb->ACC_COEF_C=(short)val; break;\r
256 case H_Reverb+12 : spu.rvb->ACC_COEF_D=(short)val; break;\r
257 case H_Reverb+14 : spu.rvb->IIR_COEF=(short)val; break;\r
258 case H_Reverb+16 : spu.rvb->FB_ALPHA=(short)val; break;\r
259 case H_Reverb+18 : spu.rvb->FB_X=(short)val; break;\r
260 case H_Reverb+20 : goto rvbd;\r
261 case H_Reverb+22 : goto rvbd;\r
262 case H_Reverb+24 : goto rvbd;\r
263 case H_Reverb+26 : goto rvbd;\r
264 case H_Reverb+28 : goto rvbd;\r
265 case H_Reverb+30 : goto rvbd;\r
266 case H_Reverb+32 : goto rvbd;\r
267 case H_Reverb+34 : goto rvbd;\r
268 case H_Reverb+36 : goto rvbd;\r
269 case H_Reverb+38 : goto rvbd;\r
270 case H_Reverb+40 : goto rvbd;\r
271 case H_Reverb+42 : goto rvbd;\r
272 case H_Reverb+44 : goto rvbd;\r
273 case H_Reverb+46 : goto rvbd;\r
274 case H_Reverb+48 : goto rvbd;\r
275 case H_Reverb+50 : goto rvbd;\r
276 case H_Reverb+52 : goto rvbd;\r
277 case H_Reverb+54 : goto rvbd;\r
278 case H_Reverb+56 : goto rvbd;\r
279 case H_Reverb+58 : goto rvbd;\r
280 case H_Reverb+60 : spu.rvb->IN_COEF_L=(short)val; break;\r
281 case H_Reverb+62 : spu.rvb->IN_COEF_R=(short)val; break;\r
282 }\r
283 return;\r
284\r
285upd_irq:\r
286 if (spu.spuCtrl & CTRL_IRQ)\r
287 schedule_next_irq();\r
288 return;\r
289\r
290rvbd:\r
291 spu.rvb->dirty = 1; // recalculate on next update\r
292}\r
293\r
294////////////////////////////////////////////////////////////////////////\r
295// READ REGISTER: called by main emu\r
296////////////////////////////////////////////////////////////////////////\r
297\r
298unsigned short CALLBACK SPUreadRegister(unsigned long reg)\r
299{\r
300 const unsigned long r=reg&0xfff;\r
301 \r
302 if(r>=0x0c00 && r<0x0d80)\r
303 {\r
304 switch(r&0x0f)\r
305 {\r
306 case 12: // get adsr vol\r
307 {\r
308 const int ch=(r>>4)-0xc0;\r
309 if(spu.dwNewChannel&(1<<ch)) return 1; // we are started, but not processed? return 1\r
310 if((spu.dwChannelsAudible&(1<<ch)) && // same here... we haven't decoded one sample yet, so no envelope yet. return 1 as well\r
311 !spu.s_chan[ch].ADSRX.EnvelopeVol)\r
312 return 1;\r
313 return (unsigned short)(spu.s_chan[ch].ADSRX.EnvelopeVol>>16);\r
314 }\r
315\r
316 case 14: // get loop address\r
317 {\r
318 const int ch=(r>>4)-0xc0;\r
319 return (unsigned short)((spu.s_chan[ch].pLoop-spu.spuMemC)>>3);\r
320 }\r
321 }\r
322 }\r
323\r
324 switch(r)\r
325 {\r
326 case H_SPUctrl:\r
327 return spu.spuCtrl;\r
328\r
329 case H_SPUstat:\r
330 return (spu.spuStat & ~0x3F) | (spu.spuCtrl & 0x3F);\r
331 \r
332 case H_SPUaddr:\r
333 return (unsigned short)(spu.spuAddr>>3);\r
334\r
335 case H_SPUdata:\r
336 {\r
337 unsigned short s = *(unsigned short *)(spu.spuMemC + spu.spuAddr);\r
338 spu.spuAddr += 2;\r
339 spu.spuAddr &= 0x7fffe;\r
340 return s;\r
341 }\r
342\r
343 //case H_SPUIsOn1:\r
344 // return IsSoundOn(0,16);\r
345\r
346 //case H_SPUIsOn2:\r
347 // return IsSoundOn(16,24);\r
348 \r
349 case H_SPUMute1:\r
350 case H_SPUMute2:\r
351 log_unhandled("r isOn: %08lx\n", reg);\r
352 break;\r
353 }\r
354\r
355 return spu.regArea[(r-0xc00)>>1];\r
356}\r
357 \r
358////////////////////////////////////////////////////////////////////////\r
359// SOUND ON register write\r
360////////////////////////////////////////////////////////////////////////\r
361\r
362static void SoundOn(int start,int end,unsigned short val)\r
363{\r
364 int ch;\r
365\r
366 for(ch=start;ch<end;ch++,val>>=1) // loop channels\r
367 {\r
368 if((val&1) && regAreaGetCh(ch, 6)) // mmm... start has to be set before key on !?!\r
369 {\r
370 spu.s_chan[ch].bIgnoreLoop = 0;\r
371 spu.dwNewChannel|=(1<<ch);\r
372 }\r
373 }\r
374}\r
375\r
376////////////////////////////////////////////////////////////////////////\r
377// SOUND OFF register write\r
378////////////////////////////////////////////////////////////////////////\r
379\r
380static void SoundOff(int start,int end,unsigned short val)\r
381{\r
382 int ch;\r
383 for(ch=start;ch<end;ch++,val>>=1) // loop channels\r
384 {\r
385 if(val&1)\r
386 {\r
387 spu.s_chan[ch].ADSRX.State = ADSR_RELEASE;\r
388\r
389 // Jungle Book - Rhythm 'n Groove\r
390 // - turns off buzzing sound (loop hangs)\r
391 spu.dwNewChannel &= ~(1<<ch);\r
392 } \r
393 }\r
394}\r
395\r
396////////////////////////////////////////////////////////////////////////\r
397// FMOD register write\r
398////////////////////////////////////////////////////////////////////////\r
399\r
400static void FModOn(int start,int end,unsigned short val)\r
401{\r
402 int ch;\r
403\r
404 for(ch=start;ch<end;ch++,val>>=1) // loop channels\r
405 {\r
406 if(val&1) // -> fmod on/off\r
407 {\r
408 if(ch>0) \r
409 {\r
410 spu.s_chan[ch].bFMod=1; // --> sound channel\r
411 spu.s_chan[ch-1].bFMod=2; // --> freq channel\r
412 }\r
413 }\r
414 else\r
415 {\r
416 spu.s_chan[ch].bFMod=0; // --> turn off fmod\r
417 if(ch>0&&spu.s_chan[ch-1].bFMod==2)\r
418 spu.s_chan[ch-1].bFMod=0;\r
419 }\r
420 }\r
421}\r
422\r
423////////////////////////////////////////////////////////////////////////\r
424// NOISE register write\r
425////////////////////////////////////////////////////////////////////////\r
426\r
427static void NoiseOn(int start,int end,unsigned short val)\r
428{\r
429 int ch;\r
430\r
431 for(ch=start;ch<end;ch++,val>>=1) // loop channels\r
432 {\r
433 spu.s_chan[ch].bNoise=val&1; // -> noise on/off\r
434 }\r
435}\r
436\r
437////////////////////////////////////////////////////////////////////////\r
438// LEFT VOLUME register write\r
439////////////////////////////////////////////////////////////////////////\r
440\r
441// please note: sweep and phase invert are wrong... but I've never seen\r
442// them used\r
443\r
444static void SetVolumeL(unsigned char ch,short vol) // LEFT VOLUME\r
445{\r
446 if(vol&0x8000) // sweep?\r
447 {\r
448 short sInc=1; // -> sweep up?\r
449 log_unhandled("ch%d sweepl %04x\n", ch, vol);\r
450 if(vol&0x2000) sInc=-1; // -> or down?\r
451 if(vol&0x1000) vol^=0xffff; // -> mmm... phase inverted? have to investigate this\r
452 vol=((vol&0x7f)+1)/2; // -> sweep: 0..127 -> 0..64\r
453 vol+=vol/(2*sInc); // -> HACK: we don't sweep right now, so we just raise/lower the volume by the half!\r
454 vol*=128;\r
455 }\r
456 else // no sweep:\r
457 {\r
458 if(vol&0x4000) // -> mmm... phase inverted? have to investigate this\r
459 //vol^=0xffff;\r
460 vol=0x3fff-(vol&0x3fff);\r
461 }\r
462\r
463 vol&=0x3fff;\r
464 spu.s_chan[ch].iLeftVolume=vol; // store volume\r
465}\r
466\r
467////////////////////////////////////////////////////////////////////////\r
468// RIGHT VOLUME register write\r
469////////////////////////////////////////////////////////////////////////\r
470\r
471static void SetVolumeR(unsigned char ch,short vol) // RIGHT VOLUME\r
472{\r
473 if(vol&0x8000) // comments... see above :)\r
474 {\r
475 short sInc=1;\r
476 log_unhandled("ch%d sweepr %04x\n", ch, vol);\r
477 if(vol&0x2000) sInc=-1;\r
478 if(vol&0x1000) vol^=0xffff;\r
479 vol=((vol&0x7f)+1)/2; \r
480 vol+=vol/(2*sInc);\r
481 vol*=128;\r
482 }\r
483 else \r
484 {\r
485 if(vol&0x4000) //vol=vol^=0xffff;\r
486 vol=0x3fff-(vol&0x3fff);\r
487 }\r
488\r
489 vol&=0x3fff;\r
490\r
491 spu.s_chan[ch].iRightVolume=vol;\r
492}\r
493\r
494////////////////////////////////////////////////////////////////////////\r
495// PITCH register write\r
496////////////////////////////////////////////////////////////////////////\r
497\r
498static void SetPitch(int ch,unsigned short val) // SET PITCH\r
499{\r
500 int NP;\r
501 if(val>0x3fff) NP=0x3fff; // get pitch val\r
502 else NP=val;\r
503\r
504 spu.s_chan[ch].iRawPitch = NP;\r
505 spu.s_chan[ch].sinc = NP << 4;\r
506 spu.s_chan[ch].sinc_inv = 0;\r
507 spu.SB[ch * SB_SIZE + 32] = 1; // -> freq change in simple interpolation mode: set flag\r
508\r
509 // don't mess spu.dwChannelsAudible as adsr runs independently\r
510}\r
511\r
512////////////////////////////////////////////////////////////////////////\r
513// REVERB register write\r
514////////////////////////////////////////////////////////////////////////\r
515\r
516static void ReverbOn(int start,int end,unsigned short val)\r
517{\r
518 int ch;\r
519\r
520 for(ch=start;ch<end;ch++,val>>=1) // loop channels\r
521 {\r
522 spu.s_chan[ch].bReverb=val&1; // -> reverb on/off\r
523 }\r
524}\r