1 /***************************************************************************
\r
2 freeze.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
22 #include "externals.h"
\r
23 #include "registers.h"
\r
26 ////////////////////////////////////////////////////////////////////////
\r
28 ////////////////////////////////////////////////////////////////////////
\r
38 int SustainIncrease;
\r
50 // no mutexes used anymore... don't need them to sync access
\r
53 int bNew; // start flag
\r
55 int iSBPos; // mixing stuff
\r
58 int SB[32+32]; // Pete added another 32 dwords in 1.6 ... prevents overflow issues with gaussian/cubic interpolation (thanx xodnizel!), and can be used for even better interpolations, eh? :)
\r
61 unsigned char * pStart; // start ptr into sound mem
\r
62 unsigned char * pCurr; // current pos in sound mem
\r
63 unsigned char * pLoop; // loop ptr in sound mem
\r
65 int bOn; // is channel active (sample playing?)
\r
66 int bStop; // is channel stopped (sample _can_ still be playing, ADSR Release phase)
\r
67 int bReverb; // can we do reverb on this channel? must have ctrl register bit, to get active
\r
68 int iActFreq; // current psx pitch
\r
69 int iUsedFreq; // current pc pitch
\r
70 int iLeftVolume; // left volume
\r
71 int iLeftVolRaw; // left psx volume value
\r
72 int bIgnoreLoop; // ignore loop bit, if an external loop address is used
\r
73 int iMute; // mute mode
\r
74 int iRightVolume; // right volume
\r
75 int iRightVolRaw; // right psx volume value
\r
76 int iRawPitch; // raw pitch (0...3fff)
\r
77 int iIrqDone; // debug irq done flag
\r
78 int s_1; // last decoding infos
\r
80 int bRVBActive; // reverb active flag
\r
81 int iRVBOffset; // reverb offset
\r
82 int iRVBRepeat; // reverb repeat
\r
83 int bNoise; // noise active flag
\r
84 int bFMod; // freq mod (0=off, 1=sound channel, 2=freq channel)
\r
85 int iRVBNum; // another reverb helper
\r
86 int iOldNoise; // old noise val for this channel
\r
87 ADSRInfo ADSR; // active ADSR settings
\r
88 ADSRInfoEx_orig ADSRX; // next ADSR settings (will be moved to active on sample start)
\r
94 uint32_t ulFreezeVersion;
\r
95 uint32_t ulFreezeSize;
\r
96 unsigned char cSPUPort[0x200];
\r
97 unsigned char cSPURam[0x80000];
\r
103 unsigned short spuIrq;
\r
110 SPUCHAN_orig s_chan[MAXCHAN];
\r
114 ////////////////////////////////////////////////////////////////////////
\r
116 void LoadStateV5(SPUFreeze_t * pF); // newest version
\r
117 void LoadStateUnknown(SPUFreeze_t * pF); // unknown format
\r
121 // we want to retain compatibility between versions,
\r
122 // so use original channel struct
\r
123 static void save_channel(SPUCHAN_orig *d, const SPUCHAN *s, int ch)
\r
125 memset(d, 0, sizeof(*d));
\r
126 d->bNew = !!(dwNewChannel & (1<<ch));
\r
127 d->iSBPos = s->iSBPos;
\r
130 memcpy(d->SB, s->SB, sizeof(d->SB));
\r
131 d->pStart = (unsigned char *)((regAreaGet(ch,6)&~1)<<3);
\r
132 d->pCurr = s->pCurr;
\r
133 d->pLoop = s->pLoop;
\r
134 d->bOn = !!(dwChannelOn & (1<<ch));
\r
135 d->bStop = s->bStop;
\r
136 d->bReverb = s->bReverb;
\r
137 d->bIgnoreLoop = s->bJump;
\r
140 d->iLeftVolume = s->iLeftVolume;
\r
141 d->bIgnoreLoop = 0;
\r
142 d->iRightVolume = s->iRightVolume;
\r
143 d->iRawPitch = s->iRawPitch;
\r
144 d->s_1 = s->SB[27]; // yes it's reversed
\r
145 d->s_2 = s->SB[26];
\r
146 d->bRVBActive = s->bRVBActive;
\r
147 d->bNoise = s->bNoise;
\r
148 d->bFMod = s->bFMod;
\r
149 d->ADSRX.State = s->ADSRX.State;
\r
150 d->ADSRX.AttackModeExp = s->ADSRX.AttackModeExp;
\r
151 d->ADSRX.AttackRate = s->ADSRX.AttackRate;
\r
152 d->ADSRX.DecayRate = s->ADSRX.DecayRate;
\r
153 d->ADSRX.SustainLevel = s->ADSRX.SustainLevel;
\r
154 d->ADSRX.SustainModeExp = s->ADSRX.SustainModeExp;
\r
155 d->ADSRX.SustainIncrease = s->ADSRX.SustainIncrease;
\r
156 d->ADSRX.SustainRate = s->ADSRX.SustainRate;
\r
157 d->ADSRX.ReleaseModeExp = s->ADSRX.ReleaseModeExp;
\r
158 d->ADSRX.ReleaseRate = s->ADSRX.ReleaseRate;
\r
159 d->ADSRX.EnvelopeVol = s->ADSRX.EnvelopeVol;
\r
160 d->ADSRX.lVolume = d->bOn; // hmh
\r
163 static void load_channel(SPUCHAN *d, const SPUCHAN_orig *s, int ch)
\r
165 memset(d, 0, sizeof(*d));
\r
166 if (s->bNew) dwNewChannel |= 1<<ch;
\r
167 d->iSBPos = s->iSBPos;
\r
170 memcpy(d->SB, s->SB, sizeof(d->SB));
\r
171 d->pCurr = (void *)((long)s->pCurr & 0x7fff0);
\r
172 d->pLoop = (void *)((long)s->pLoop & 0x7fff0);
\r
173 if (s->bOn) dwChannelOn |= 1<<ch;
\r
174 d->bStop = s->bStop;
\r
175 d->bReverb = s->bReverb;
\r
176 d->iLeftVolume = s->iLeftVolume;
\r
177 d->iRightVolume = s->iRightVolume;
\r
178 d->iRawPitch = s->iRawPitch;
\r
179 d->bRVBActive = s->bRVBActive;
\r
180 d->bNoise = s->bNoise;
\r
181 d->bFMod = s->bFMod;
\r
182 d->bJump = s->bIgnoreLoop;
\r
183 d->ADSRX.State = s->ADSRX.State;
\r
184 d->ADSRX.AttackModeExp = s->ADSRX.AttackModeExp;
\r
185 d->ADSRX.AttackRate = s->ADSRX.AttackRate;
\r
186 d->ADSRX.DecayRate = s->ADSRX.DecayRate;
\r
187 d->ADSRX.SustainLevel = s->ADSRX.SustainLevel;
\r
188 d->ADSRX.SustainModeExp = s->ADSRX.SustainModeExp;
\r
189 d->ADSRX.SustainIncrease = s->ADSRX.SustainIncrease;
\r
190 d->ADSRX.SustainRate = s->ADSRX.SustainRate;
\r
191 d->ADSRX.ReleaseModeExp = s->ADSRX.ReleaseModeExp;
\r
192 d->ADSRX.ReleaseRate = s->ADSRX.ReleaseRate;
\r
193 d->ADSRX.EnvelopeVol = s->ADSRX.EnvelopeVol;
\r
196 ////////////////////////////////////////////////////////////////////////
\r
197 // SPUFREEZE: called by main emu on savestate load/save
\r
198 ////////////////////////////////////////////////////////////////////////
\r
200 long CALLBACK SPUfreeze(uint32_t ulFreezeMode,SPUFreeze_t * pF)
\r
202 int i;SPUOSSFreeze_t * pFO;
\r
204 if(!pF) return 0; // first check
\r
206 if(ulFreezeMode) // info or save?
\r
207 {//--------------------------------------------------//
\r
208 if(ulFreezeMode==1)
\r
209 memset(pF,0,sizeof(SPUFreeze_t)+sizeof(SPUOSSFreeze_t));
\r
211 strcpy(pF->szSPUName,"PBOSS");
\r
212 pF->ulFreezeVersion=5;
\r
213 pF->ulFreezeSize=sizeof(SPUFreeze_t)+sizeof(SPUOSSFreeze_t);
\r
215 if(ulFreezeMode==2) return 1; // info mode? ok, bye
\r
217 RemoveTimer(); // stop timer
\r
219 memcpy(pF->cSPURam,spuMem,0x80000); // copy common infos
\r
220 memcpy(pF->cSPUPort,regArea,0x200);
\r
222 if(xapGlobal && XAPlay!=XAFeed) // some xa
\r
224 pF->xaS=*xapGlobal;
\r
227 memset(&pF->xaS,0,sizeof(xa_decode_t)); // or clean xa
\r
229 pFO=(SPUOSSFreeze_t *)(pF+1); // store special stuff
\r
231 pFO->spuIrq=spuIrq;
\r
232 if(pSpuIrq) pFO->pSpuIrq = (unsigned long)pSpuIrq-(unsigned long)spuMemC;
\r
234 pFO->spuAddr=spuAddr;
\r
235 if(pFO->spuAddr==0) pFO->spuAddr=0xbaadf00d;
\r
237 dwChannelOn&=~dwPendingChanOff;
\r
238 dwPendingChanOff=0;
\r
240 for(i=0;i<MAXCHAN;i++)
\r
242 save_channel(&pFO->s_chan[i],&s_chan[i],i);
\r
243 if(pFO->s_chan[i].pCurr)
\r
244 pFO->s_chan[i].pCurr-=(unsigned long)spuMemC;
\r
245 if(pFO->s_chan[i].pLoop)
\r
246 pFO->s_chan[i].pLoop-=(unsigned long)spuMemC;
\r
249 SetupTimer(); // sound processing on again
\r
252 //--------------------------------------------------//
\r
255 if(ulFreezeMode!=0) return 0; // bad mode? bye
\r
257 RemoveTimer(); // we stop processing while doing the save!
\r
259 memcpy(spuMem,pF->cSPURam,0x80000); // get ram
\r
260 memcpy(regArea,pF->cSPUPort,0x200);
\r
262 if(pF->xaS.nsamples<=4032) // start xa again
\r
263 SPUplayADPCMchannel(&pF->xaS);
\r
266 dwPendingChanOff=0;
\r
268 if(!strcmp(pF->szSPUName,"PBOSS") && pF->ulFreezeVersion==5)
\r
270 else LoadStateUnknown(pF);
\r
274 // repair some globals
\r
275 for(i=0;i<=62;i+=2)
\r
276 SPUwriteRegister(H_Reverb+i,regArea[(H_Reverb+i-0xc00)>>1]);
\r
277 SPUwriteRegister(H_SPUReverbAddr,regArea[(H_SPUReverbAddr-0xc00)>>1]);
\r
278 SPUwriteRegister(H_SPUrvolL,regArea[(H_SPUrvolL-0xc00)>>1]);
\r
279 SPUwriteRegister(H_SPUrvolR,regArea[(H_SPUrvolR-0xc00)>>1]);
\r
281 SPUwriteRegister(H_SPUctrl,(unsigned short)(regArea[(H_SPUctrl-0xc00)>>1]|0x4000));
\r
282 SPUwriteRegister(H_SPUstat,regArea[(H_SPUstat-0xc00)>>1]);
\r
283 SPUwriteRegister(H_CDLeft,regArea[(H_CDLeft-0xc00)>>1]);
\r
284 SPUwriteRegister(H_CDRight,regArea[(H_CDRight-0xc00)>>1]);
\r
286 // fix to prevent new interpolations from crashing
\r
287 for(i=0;i<MAXCHAN;i++) s_chan[i].SB[28]=0;
\r
289 SetupTimer(); // start sound processing again
\r
294 ////////////////////////////////////////////////////////////////////////
\r
296 void LoadStateV5(SPUFreeze_t * pF)
\r
298 int i;SPUOSSFreeze_t * pFO;
\r
300 pFO=(SPUOSSFreeze_t *)(pF+1);
\r
302 spuIrq = pFO->spuIrq;
\r
303 if(pFO->pSpuIrq) pSpuIrq = spuMemC+((long)pFO->pSpuIrq&0x7fff0); else pSpuIrq=NULL;
\r
307 spuAddr = pFO->spuAddr;
\r
308 if (spuAddr == 0xbaadf00d) spuAddr = 0;
\r
314 for(i=0;i<MAXCHAN;i++)
\r
316 load_channel(&s_chan[i],&pFO->s_chan[i],i);
\r
318 s_chan[i].pCurr+=(unsigned long)spuMemC;
\r
319 s_chan[i].pLoop+=(unsigned long)spuMemC;
\r
323 ////////////////////////////////////////////////////////////////////////
\r
325 void LoadStateUnknown(SPUFreeze_t * pF)
\r
329 for(i=0;i<MAXCHAN;i++)
\r
332 s_chan[i].pLoop=spuMemC;
\r
340 for(i=0;i<0xc0;i++)
\r
342 SPUwriteRegister(0x1f801c00+i*2,regArea[i]);
\r
346 ////////////////////////////////////////////////////////////////////////
\r