1 /***************************************************************************
\r
2 registers.c - description
\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
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
16 ***************************************************************************/
\r
20 #define _IN_REGISTERS
\r
22 #include "externals.h"
\r
23 #include "registers.h"
\r
24 #include "spu_config.h"
\r
26 static void SoundOn(int start,int end,unsigned short val);
\r
27 static void SoundOff(int start,int end,unsigned short val);
\r
28 static void FModOn(int start,int end,unsigned short val);
\r
29 static void NoiseOn(int start,int end,unsigned short val);
\r
30 static void SetVolumeL(unsigned char ch,short vol);
\r
31 static void SetVolumeR(unsigned char ch,short vol);
\r
32 static void SetPitch(int ch,unsigned short val);
\r
33 static void ReverbOn(int start,int end,unsigned short val);
\r
35 ////////////////////////////////////////////////////////////////////////
\r
36 // WRITE REGISTERS: called by main emu
\r
37 ////////////////////////////////////////////////////////////////////////
\r
39 static 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
46 void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val,
\r
47 unsigned int cycles)
\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
54 if (!changed && (ignore_dupe[rofs >> 5] & (1 << (rofs & 0x1f))))
\r
56 // zero keyon/keyoff?
\r
57 if (val == 0 && (r & 0xff8) == 0xd88)
\r
60 do_samples_if_needed(cycles, 0);
\r
62 if(r>=0x0c00 && r<0x0d80) // some channel info?
\r
64 int ch=(r>>4)-0xc0; // calc channel
\r
67 //------------------------------------------------// r volume
\r
69 SetVolumeL((unsigned char)ch,val);
\r
71 //------------------------------------------------// l volume
\r
73 SetVolumeR((unsigned char)ch,val);
\r
75 //------------------------------------------------// pitch
\r
79 //------------------------------------------------// start
\r
81 // taken from regArea later
\r
83 //------------------------------------------------// level with pre-calcs
\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
95 //------------------------------------------------// adsr times with pre-calcs
\r
98 const unsigned long lval=val;
\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
109 //------------------------------------------------// adsr volume... mmm have to investigate this
\r
112 //------------------------------------------------//
\r
114 spu.s_chan[ch].pLoop=spu.spuMemC+((val&~1)<<3);
\r
116 //------------------------------------------------//
\r
123 //-------------------------------------------------//
\r
125 spu.spuAddr = (unsigned long) val<<3;
\r
127 //-------------------------------------------------//
\r
129 *(unsigned short *)(spu.spuMemC + spu.spuAddr) = val;
\r
131 spu.spuAddr &= 0x7fffe;
\r
133 //-------------------------------------------------//
\r
135 if (!(spu.spuCtrl & CTRL_IRQ)) {
\r
136 spu.spuStat&=~STAT_IRQ;
\r
137 if (val & CTRL_IRQ)
\r
138 schedule_next_irq();
\r
142 //-------------------------------------------------//
\r
144 spu.spuStat=val&0xf800;
\r
146 //-------------------------------------------------//
\r
147 case H_SPUReverbAddr:
\r
148 if(val==0xFFFF || val<=0x200)
\r
149 {rvb.StartAddr=rvb.CurrAddr=0;}
\r
152 const long iv=(unsigned long)val<<2;
\r
153 if(rvb.StartAddr!=iv)
\r
155 rvb.StartAddr=(unsigned long)val<<2;
\r
156 rvb.CurrAddr=rvb.StartAddr;
\r
160 //-------------------------------------------------//
\r
162 spu.pSpuIrq=spu.spuMemC+(((unsigned long) val<<3)&~0xf);
\r
164 //-------------------------------------------------//
\r
168 //-------------------------------------------------//
\r
172 //-------------------------------------------------//
\r
176 //auxprintf("EL %d\n",val);
\r
178 //-------------------------------------------------//
\r
180 //auxprintf("ER %d\n",val);
\r
182 //-------------------------------------------------//
\r
184 //auxprintf("ML %d\n",val);
\r
186 //-------------------------------------------------//
\r
188 //auxprintf("MR %d\n",val);
\r
190 //-------------------------------------------------//
\r
192 //auxprintf("M0 %04x\n",val);
\r
194 //-------------------------------------------------//
\r
196 //auxprintf("M1 %04x\n",val);
\r
199 //-------------------------------------------------//
\r
203 //-------------------------------------------------//
\r
205 SoundOn(16,24,val);
\r
207 //-------------------------------------------------//
\r
209 SoundOff(0,16,val);
\r
211 //-------------------------------------------------//
\r
213 SoundOff(16,24,val);
\r
215 //-------------------------------------------------//
\r
217 spu.iLeftXAVol=val & 0x7fff;
\r
218 if(spu.cddavCallback) spu.cddavCallback(0,val);
\r
221 spu.iRightXAVol=val & 0x7fff;
\r
222 if(spu.cddavCallback) spu.cddavCallback(1,val);
\r
224 //-------------------------------------------------//
\r
228 //-------------------------------------------------//
\r
232 //-------------------------------------------------//
\r
236 //-------------------------------------------------//
\r
238 NoiseOn(16,24,val);
\r
240 //-------------------------------------------------//
\r
242 ReverbOn(0,16,val);
\r
244 //-------------------------------------------------//
\r
246 ReverbOn(16,24,val);
\r
248 //-------------------------------------------------//
\r
249 case H_Reverb+0 : rvb.FB_SRC_A=val*4; goto rvbd;
\r
250 case H_Reverb+2 : rvb.FB_SRC_B=val*4; goto rvbd;
\r
251 case H_Reverb+4 : rvb.IIR_ALPHA=(short)val; goto rvbd;
\r
252 case H_Reverb+6 : rvb.ACC_COEF_A=(short)val; goto rvbd;
\r
253 case H_Reverb+8 : rvb.ACC_COEF_B=(short)val; goto rvbd;
\r
254 case H_Reverb+10 : rvb.ACC_COEF_C=(short)val; goto rvbd;
\r
255 case H_Reverb+12 : rvb.ACC_COEF_D=(short)val; goto rvbd;
\r
256 case H_Reverb+14 : rvb.IIR_COEF=(short)val; goto rvbd;
\r
257 case H_Reverb+16 : rvb.FB_ALPHA=(short)val; goto rvbd;
\r
258 case H_Reverb+18 : rvb.FB_X=(short)val; goto rvbd;
\r
259 case H_Reverb+20 : rvb.IIR_DEST_A0=val*4; goto rvbd;
\r
260 case H_Reverb+22 : rvb.IIR_DEST_A1=val*4; goto rvbd;
\r
261 case H_Reverb+24 : rvb.ACC_SRC_A0=val*4; goto rvbd;
\r
262 case H_Reverb+26 : rvb.ACC_SRC_A1=val*4; goto rvbd;
\r
263 case H_Reverb+28 : rvb.ACC_SRC_B0=val*4; goto rvbd;
\r
264 case H_Reverb+30 : rvb.ACC_SRC_B1=val*4; goto rvbd;
\r
265 case H_Reverb+32 : rvb.IIR_SRC_A0=val*4; goto rvbd;
\r
266 case H_Reverb+34 : rvb.IIR_SRC_A1=val*4; goto rvbd;
\r
267 case H_Reverb+36 : rvb.IIR_DEST_B0=val*4; goto rvbd;
\r
268 case H_Reverb+38 : rvb.IIR_DEST_B1=val*4; goto rvbd;
\r
269 case H_Reverb+40 : rvb.ACC_SRC_C0=val*4; goto rvbd;
\r
270 case H_Reverb+42 : rvb.ACC_SRC_C1=val*4; goto rvbd;
\r
271 case H_Reverb+44 : rvb.ACC_SRC_D0=val*4; goto rvbd;
\r
272 case H_Reverb+46 : rvb.ACC_SRC_D1=val*4; goto rvbd;
\r
273 case H_Reverb+48 : rvb.IIR_SRC_B1=val*4; goto rvbd;
\r
274 case H_Reverb+50 : rvb.IIR_SRC_B0=val*4; goto rvbd;
\r
275 case H_Reverb+52 : rvb.MIX_DEST_A0=val*4; goto rvbd;
\r
276 case H_Reverb+54 : rvb.MIX_DEST_A1=val*4; goto rvbd;
\r
277 case H_Reverb+56 : rvb.MIX_DEST_B0=val*4; goto rvbd;
\r
278 case H_Reverb+58 : rvb.MIX_DEST_B1=val*4; goto rvbd;
\r
279 case H_Reverb+60 : rvb.IN_COEF_L=(short)val; goto rvbd;
\r
280 case H_Reverb+62 : rvb.IN_COEF_R=(short)val; goto rvbd;
\r
285 if (spu.spuCtrl & CTRL_IRQ)
\r
286 schedule_next_irq();
\r
290 rvb.dirty = 1; // recalculate on next update
\r
293 ////////////////////////////////////////////////////////////////////////
\r
294 // READ REGISTER: called by main emu
\r
295 ////////////////////////////////////////////////////////////////////////
\r
297 unsigned short CALLBACK SPUreadRegister(unsigned long reg)
\r
299 const unsigned long r=reg&0xfff;
\r
301 if(r>=0x0c00 && r<0x0d80)
\r
305 case 12: // get adsr vol
\r
307 const int ch=(r>>4)-0xc0;
\r
308 if(spu.dwNewChannel&(1<<ch)) return 1; // we are started, but not processed? return 1
\r
309 if((spu.dwChannelOn&(1<<ch)) && // same here... we haven't decoded one sample yet, so no envelope yet. return 1 as well
\r
310 !spu.s_chan[ch].ADSRX.EnvelopeVol)
\r
312 return (unsigned short)(spu.s_chan[ch].ADSRX.EnvelopeVol>>16);
\r
315 case 14: // get loop address
\r
317 const int ch=(r>>4)-0xc0;
\r
318 return (unsigned short)((spu.s_chan[ch].pLoop-spu.spuMemC)>>3);
\r
326 return spu.spuCtrl;
\r
329 return spu.spuStat;
\r
332 return (unsigned short)(spu.spuAddr>>3);
\r
336 unsigned short s = *(unsigned short *)(spu.spuMemC + spu.spuAddr);
\r
338 spu.spuAddr &= 0x7fffe;
\r
343 // return IsSoundOn(0,16);
\r
346 // return IsSoundOn(16,24);
\r
350 return spu.regArea[(r-0xc00)>>1];
\r
353 ////////////////////////////////////////////////////////////////////////
\r
354 // SOUND ON register write
\r
355 ////////////////////////////////////////////////////////////////////////
\r
357 static void SoundOn(int start,int end,unsigned short val)
\r
361 for(ch=start;ch<end;ch++,val>>=1) // loop channels
\r
363 if((val&1) && regAreaGet(ch,6)) // mmm... start has to be set before key on !?!
\r
365 spu.s_chan[ch].pCurr=spu.spuMemC+((regAreaGet(ch,6)&~1)<<3); // must be block aligned
\r
366 spu.s_chan[ch].pLoop=spu.spuMemC+((regAreaGet(ch,14)&~1)<<3);
\r
367 spu.dwNewChannel|=(1<<ch);
\r
372 ////////////////////////////////////////////////////////////////////////
\r
373 // SOUND OFF register write
\r
374 ////////////////////////////////////////////////////////////////////////
\r
376 static void SoundOff(int start,int end,unsigned short val)
\r
379 for(ch=start;ch<end;ch++,val>>=1) // loop channels
\r
383 spu.s_chan[ch].ADSRX.State = ADSR_RELEASE;
\r
385 // Jungle Book - Rhythm 'n Groove
\r
386 // - turns off buzzing sound (loop hangs)
\r
387 spu.dwNewChannel &= ~(1<<ch);
\r
392 ////////////////////////////////////////////////////////////////////////
\r
393 // FMOD register write
\r
394 ////////////////////////////////////////////////////////////////////////
\r
396 static void FModOn(int start,int end,unsigned short val)
\r
400 for(ch=start;ch<end;ch++,val>>=1) // loop channels
\r
402 if(val&1) // -> fmod on/off
\r
406 spu.s_chan[ch].bFMod=1; // --> sound channel
\r
407 spu.s_chan[ch-1].bFMod=2; // --> freq channel
\r
412 spu.s_chan[ch].bFMod=0; // --> turn off fmod
\r
413 if(ch>0&&spu.s_chan[ch-1].bFMod==2)
\r
414 spu.s_chan[ch-1].bFMod=0;
\r
419 ////////////////////////////////////////////////////////////////////////
\r
420 // NOISE register write
\r
421 ////////////////////////////////////////////////////////////////////////
\r
423 static void NoiseOn(int start,int end,unsigned short val)
\r
427 for(ch=start;ch<end;ch++,val>>=1) // loop channels
\r
429 spu.s_chan[ch].bNoise=val&1; // -> noise on/off
\r
433 ////////////////////////////////////////////////////////////////////////
\r
434 // LEFT VOLUME register write
\r
435 ////////////////////////////////////////////////////////////////////////
\r
437 // please note: sweep and phase invert are wrong... but I've never seen
\r
440 static void SetVolumeL(unsigned char ch,short vol) // LEFT VOLUME
\r
442 if(vol&0x8000) // sweep?
\r
444 short sInc=1; // -> sweep up?
\r
445 if(vol&0x2000) sInc=-1; // -> or down?
\r
446 if(vol&0x1000) vol^=0xffff; // -> mmm... phase inverted? have to investigate this
\r
447 vol=((vol&0x7f)+1)/2; // -> sweep: 0..127 -> 0..64
\r
448 vol+=vol/(2*sInc); // -> HACK: we don't sweep right now, so we just raise/lower the volume by the half!
\r
453 if(vol&0x4000) // -> mmm... phase inverted? have to investigate this
\r
455 vol=0x3fff-(vol&0x3fff);
\r
459 spu.s_chan[ch].iLeftVolume=vol; // store volume
\r
462 ////////////////////////////////////////////////////////////////////////
\r
463 // RIGHT VOLUME register write
\r
464 ////////////////////////////////////////////////////////////////////////
\r
466 static void SetVolumeR(unsigned char ch,short vol) // RIGHT VOLUME
\r
468 if(vol&0x8000) // comments... see above :)
\r
471 if(vol&0x2000) sInc=-1;
\r
472 if(vol&0x1000) vol^=0xffff;
\r
473 vol=((vol&0x7f)+1)/2;
\r
479 if(vol&0x4000) //vol=vol^=0xffff;
\r
480 vol=0x3fff-(vol&0x3fff);
\r
485 spu.s_chan[ch].iRightVolume=vol;
\r
488 ////////////////////////////////////////////////////////////////////////
\r
489 // PITCH register write
\r
490 ////////////////////////////////////////////////////////////////////////
\r
492 static void SetPitch(int ch,unsigned short val) // SET PITCH
\r
495 if(val>0x3fff) NP=0x3fff; // get pitch val
\r
498 spu.s_chan[ch].iRawPitch=NP;
\r
499 spu.s_chan[ch].sinc=(NP<<4)|8;
\r
500 spu.s_chan[ch].sinc_inv=0;
\r
501 if (spu_config.iUseInterpolation == 1)
\r
502 spu.SB[ch * SB_SIZE + 32] = 1; // -> freq change in simple interpolation mode: set flag
\r
505 ////////////////////////////////////////////////////////////////////////
\r
506 // REVERB register write
\r
507 ////////////////////////////////////////////////////////////////////////
\r
509 static void ReverbOn(int start,int end,unsigned short val)
\r
513 for(ch=start;ch<end;ch++,val>>=1) // loop channels
\r
515 spu.s_chan[ch].bReverb=val&1; // -> reverb on/off
\r