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
23 #include "externals.h"
\r
24 #include "registers.h"
\r
27 ////////////////////////////////////////////////////////////////////////
\r
29 ////////////////////////////////////////////////////////////////////////
\r
41 unsigned int ReleaseVal;
\r
43 int ReleaseStartTime;
\r
57 int SustainIncrease;
\r
69 // no mutexes used anymore... don't need them to sync access
\r
72 int bNew; // start flag
\r
74 int iSBPos; // mixing stuff
\r
77 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
80 int iStart; // start ptr into sound mem
\r
81 int iCurr; // current pos in sound mem
\r
82 int iLoop; // loop ptr in sound mem
\r
84 int bOn; // is channel active (sample playing?)
\r
85 int bStop; // is channel stopped (sample _can_ still be playing, ADSR Release phase)
\r
86 int bReverb; // can we do reverb on this channel? must have ctrl register bit, to get active
\r
87 int iActFreq; // current psx pitch
\r
88 int iUsedFreq; // current pc pitch
\r
89 int iLeftVolume; // left volume
\r
90 int iLeftVolRaw; // left psx volume value
\r
91 int bIgnoreLoop; // ignore loop bit, if an external loop address is used
\r
92 int iMute; // mute mode
\r
93 int iRightVolume; // right volume
\r
94 int iRightVolRaw; // right psx volume value
\r
95 int iRawPitch; // raw pitch (0...3fff)
\r
96 int iIrqDone; // debug irq done flag
\r
97 int s_1; // last decoding infos
\r
99 int bRVBActive; // reverb active flag
\r
100 int iRVBOffset; // reverb offset
\r
101 int iRVBRepeat; // reverb repeat
\r
102 int bNoise; // noise active flag
\r
103 int bFMod; // freq mod (0=off, 1=sound channel, 2=freq channel)
\r
104 int iRVBNum; // another reverb helper
\r
105 int iOldNoise; // old noise val for this channel
\r
106 ADSRInfo ADSR; // active ADSR settings
\r
107 ADSRInfoEx_orig ADSRX; // next ADSR settings (will be moved to active on sample start)
\r
110 typedef struct SPUFreeze
\r
113 uint32_t ulFreezeVersion;
\r
114 uint32_t ulFreezeSize;
\r
115 unsigned char cSPUPort[0x200];
\r
116 unsigned char cSPURam[0x80000];
\r
122 unsigned short spuIrq;
\r
123 unsigned short decode_pos;
\r
128 uint16_t cdda_left;
\r
129 uint32_t cycles_played;
\r
131 SPUCHAN_orig s_chan[MAXCHAN];
\r
133 uint32_t cycles_dma_end;
\r
134 uint32_t decode_dirty_ch;
\r
135 uint32_t dwNoiseVal;
\r
136 uint32_t dwNoiseCount;
\r
138 uint32_t XALastVal;
\r
139 uint32_t last_keyon_cycles;
\r
143 ////////////////////////////////////////////////////////////////////////
\r
145 static SPUOSSFreeze_t * LoadStateV5(SPUFreeze_t * pF, uint32_t cycles);
\r
146 static void LoadStateUnknown(SPUFreeze_t * pF, uint32_t cycles); // unknown format
\r
148 // we want to retain compatibility between versions,
\r
149 // so use original channel struct
\r
150 static void save_channel(SPUCHAN_orig *d, const SPUCHAN *s, int ch)
\r
152 memset(d, 0, sizeof(*d));
\r
153 d->bNew = !!(spu.dwNewChannel & (1<<ch));
\r
154 d->iSBPos = s->iSBPos;
\r
157 assert(sizeof(d->SB) >= sizeof(spu.sb[ch]));
\r
158 memcpy(d->SB, &spu.sb[ch], sizeof(spu.sb[ch]));
\r
159 d->iStart = (regAreaGetCh(ch, 6) & ~1) << 3;
\r
160 d->iCurr = 0; // set by the caller
\r
161 d->iLoop = 0; // set by the caller
\r
162 d->bOn = !!(spu.dwChannelsAudible & (1<<ch));
\r
163 d->bStop = s->ADSRX.State == ADSR_RELEASE;
\r
164 d->bReverb = s->bReverb;
\r
167 d->iLeftVolume = s->iLeftVolume;
\r
168 // this one is nasty but safe, save compat is important
\r
169 d->bIgnoreLoop = (s->prevflags ^ 2) << 1;
\r
170 d->iRightVolume = s->iRightVolume;
\r
171 d->iRawPitch = s->iRawPitch;
\r
172 d->s_1 = spu.sb[ch].SB[27]; // yes it's reversed
\r
173 d->s_2 = spu.sb[ch].SB[26];
\r
174 d->bRVBActive = s->bRVBActive;
\r
175 d->bNoise = s->bNoise;
\r
176 d->bFMod = s->bFMod;
\r
177 d->ADSRX.State = s->ADSRX.State;
\r
178 d->ADSRX.AttackModeExp = s->ADSRX.AttackModeExp;
\r
179 d->ADSRX.AttackRate = s->ADSRX.AttackRate;
\r
180 d->ADSRX.DecayRate = s->ADSRX.DecayRate;
\r
181 d->ADSRX.SustainLevel = s->ADSRX.SustainLevel;
\r
182 d->ADSRX.SustainModeExp = s->ADSRX.SustainModeExp;
\r
183 d->ADSRX.SustainIncrease = s->ADSRX.SustainIncrease;
\r
184 d->ADSRX.SustainRate = s->ADSRX.SustainRate;
\r
185 d->ADSRX.ReleaseModeExp = s->ADSRX.ReleaseModeExp;
\r
186 d->ADSRX.ReleaseRate = s->ADSRX.ReleaseRate;
\r
187 d->ADSRX.EnvelopeVol = s->ADSRX.EnvelopeVol;
\r
188 d->ADSRX.lVolume = d->bOn; // hmh
\r
191 static void load_channel(SPUCHAN *d, const SPUCHAN_orig *s, int ch)
\r
193 memset(d, 0, sizeof(*d));
\r
194 if (s->bNew) spu.dwNewChannel |= 1<<ch;
\r
195 d->iSBPos = s->iSBPos;
\r
196 if ((uint32_t)d->iSBPos >= 28) d->iSBPos = 27;
\r
200 memcpy(&spu.sb[ch], s->SB, sizeof(spu.sb[ch]));
\r
201 d->pCurr = (void *)((uintptr_t)s->iCurr & 0x7fff0);
\r
202 d->pLoop = (void *)((uintptr_t)s->iLoop & 0x7fff0);
\r
203 d->bReverb = s->bReverb;
\r
204 d->iLeftVolume = s->iLeftVolume;
\r
205 d->iRightVolume = s->iRightVolume;
\r
206 d->iRawPitch = s->iRawPitch;
\r
207 d->bRVBActive = s->bRVBActive;
\r
208 d->bNoise = s->bNoise;
\r
209 d->bFMod = s->bFMod;
\r
210 d->prevflags = (s->bIgnoreLoop >> 1) ^ 2;
\r
211 d->ADSRX.State = s->ADSRX.State;
\r
212 if (s->bStop) d->ADSRX.State = ADSR_RELEASE;
\r
213 d->ADSRX.AttackModeExp = s->ADSRX.AttackModeExp;
\r
214 d->ADSRX.AttackRate = s->ADSRX.AttackRate;
\r
215 d->ADSRX.DecayRate = s->ADSRX.DecayRate;
\r
216 d->ADSRX.SustainLevel = s->ADSRX.SustainLevel;
\r
217 d->ADSRX.SustainModeExp = s->ADSRX.SustainModeExp;
\r
218 d->ADSRX.SustainIncrease = s->ADSRX.SustainIncrease;
\r
219 d->ADSRX.SustainRate = s->ADSRX.SustainRate;
\r
220 d->ADSRX.ReleaseModeExp = s->ADSRX.ReleaseModeExp;
\r
221 d->ADSRX.ReleaseRate = s->ADSRX.ReleaseRate;
\r
222 d->ADSRX.EnvelopeVol = s->ADSRX.EnvelopeVol;
\r
223 if (s->bOn) spu.dwChannelsAudible |= 1<<ch;
\r
224 else d->ADSRX.EnvelopeVol = 0;
\r
227 // force load from regArea to variables
\r
228 static void load_register(unsigned long reg, unsigned int cycles)
\r
230 unsigned short *r = &spu.regArea[((reg & 0xfff) - 0xc00) >> 1];
\r
232 SPUwriteRegister(reg, *r ^ 1, cycles);
\r
235 ////////////////////////////////////////////////////////////////////////
\r
236 // SPUFREEZE: called by main emu on savestate load/save
\r
237 ////////////////////////////////////////////////////////////////////////
\r
239 long CALLBACK SPUfreeze(unsigned int ulFreezeMode, SPUFreeze_t * pF,
\r
240 unsigned int cycles)
\r
242 SPUOSSFreeze_t * pFO = NULL;
\r
245 if(!pF) return 0; // first check
\r
247 if(ulFreezeMode) // info or save?
\r
248 {//--------------------------------------------------//
\r
249 int xa_left = 0, cdda_left = 0;
\r
250 do_samples(cycles, 1);
\r
252 if(ulFreezeMode==1)
\r
253 memset(pF,0,sizeof(SPUFreeze_t)+sizeof(SPUOSSFreeze_t));
\r
255 strcpy(pF->szSPUName,"PBOSS");
\r
256 pF->ulFreezeVersion=5;
\r
257 pF->ulFreezeSize=sizeof(SPUFreeze_t)+sizeof(SPUOSSFreeze_t);
\r
259 if(ulFreezeMode==2) return 1; // info mode? ok, bye
\r
261 regAreaGet(H_SPUctrl) = spu.spuCtrl;
\r
262 regAreaGet(H_SPUstat) = spu.spuStat;
\r
263 memcpy(pF->cSPURam,spu.spuMem,0x80000); // copy common infos
\r
264 memcpy(pF->cSPUPort,spu.regArea,0x200);
\r
266 if(spu.xapGlobal && spu.XAPlay!=spu.XAFeed) // some xa
\r
268 xa_left = spu.XAFeed - spu.XAPlay;
\r
270 xa_left = spu.XAEnd - spu.XAPlay + spu.XAFeed - spu.XAStart;
\r
271 pF->xaS = *spu.xapGlobal;
\r
273 else if (spu.CDDAPlay != spu.CDDAFeed)
\r
275 // abuse the xa struct to store leftover cdda samples
\r
276 unsigned int *p = spu.CDDAPlay;
\r
277 cdda_left = spu.CDDAFeed - spu.CDDAPlay;
\r
279 cdda_left = spu.CDDAEnd - spu.CDDAPlay + spu.CDDAFeed - spu.CDDAStart;
\r
280 if (cdda_left > sizeof(pF->xaS.pcm) / 4)
\r
281 cdda_left = sizeof(pF->xaS.pcm) / 4;
\r
282 if (p + cdda_left <= spu.CDDAEnd)
\r
283 memcpy(pF->xaS.pcm, p, cdda_left * 4);
\r
285 memcpy(pF->xaS.pcm, p, (spu.CDDAEnd - p) * 4);
\r
286 memcpy((char *)pF->xaS.pcm + (spu.CDDAEnd - p) * 4, spu.CDDAStart,
\r
287 (cdda_left - (spu.CDDAEnd - p)) * 4);
\r
289 pF->xaS.nsamples = 0;
\r
292 memset(&pF->xaS,0,sizeof(xa_decode_t)); // or clean xa
\r
294 pFO=(SPUOSSFreeze_t *)(pF+1); // store special stuff
\r
296 pFO->spuIrq = spu.regArea[(H_SPUirqAddr - 0x0c00) / 2];
\r
297 if(spu.pSpuIrq) pFO->pSpuIrq = spu.pSpuIrq - spu.spuMemC;
\r
299 pFO->spuAddr=spu.spuAddr;
\r
300 if(pFO->spuAddr==0) pFO->spuAddr=0xbaadf00d;
\r
301 pFO->decode_pos = spu.decode_pos;
\r
302 pFO->rvb_cur = spu.rvb->CurrAddr;
\r
303 pFO->xa_left = xa_left;
\r
304 pFO->cdda_left = cdda_left;
\r
305 pFO->cycles_played = spu.cycles_played;
\r
306 pFO->cycles_dma_end = spu.cycles_dma_end;
\r
307 pFO->decode_dirty_ch = spu.decode_dirty_ch;
\r
308 pFO->dwNoiseVal = spu.dwNoiseVal;
\r
309 pFO->dwNoiseCount = spu.dwNoiseCount;
\r
310 pFO->XARepeat = spu.XARepeat;
\r
311 pFO->XALastVal = spu.XALastVal;
\r
312 pFO->last_keyon_cycles = spu.last_keyon_cycles;
\r
314 for(i=0;i<MAXCHAN;i++)
\r
316 save_channel(&pFO->s_chan[i],&spu.s_chan[i],i);
\r
317 if(spu.s_chan[i].pCurr)
\r
318 pFO->s_chan[i].iCurr=spu.s_chan[i].pCurr-spu.spuMemC;
\r
319 if(spu.s_chan[i].pLoop)
\r
320 pFO->s_chan[i].iLoop=spu.s_chan[i].pLoop-spu.spuMemC;
\r
324 //--------------------------------------------------//
\r
327 if(ulFreezeMode!=0) return 0; // bad mode? bye
\r
329 memcpy(spu.spuMem,pF->cSPURam,0x80000); // get ram
\r
330 memcpy(spu.regArea,pF->cSPUPort,0x200);
\r
332 spu.spuCtrl = regAreaGet(H_SPUctrl);
\r
333 spu.spuStat = regAreaGet(H_SPUstat);
\r
335 if (!strcmp(pF->szSPUName,"PBOSS") && pF->ulFreezeVersion==5)
\r
336 pFO = LoadStateV5(pF, cycles);
\r
337 else LoadStateUnknown(pF, cycles);
\r
339 spu.XAPlay = spu.XAFeed = spu.XAStart;
\r
340 spu.CDDAPlay = spu.CDDAFeed = spu.CDDAStart;
\r
341 if (pFO && pFO->xa_left && pF->xaS.nsamples) { // start xa again
\r
343 spu.XAPlay = spu.XAFeed - pFO->xa_left;
\r
344 if (spu.XAPlay < spu.XAStart)
\r
345 spu.XAPlay = spu.XAStart;
\r
347 else if (pFO && pFO->cdda_left) { // start cdda again
\r
348 FeedCDDA((void *)pF->xaS.pcm, pFO->cdda_left * 4);
\r
351 // not in old savestates
\r
352 spu.cycles_dma_end = 0;
\r
353 spu.decode_dirty_ch = spu.dwChannelsAudible & 0x0a;
\r
354 spu.dwNoiseVal = 0;
\r
355 spu.dwNoiseCount = 0;
\r
358 spu.last_keyon_cycles = cycles - 16*786u;
\r
359 if (pFO && pF->ulFreezeSize >= sizeof(*pF) + sizeof(*pFO)) {
\r
360 spu.cycles_dma_end = pFO->cycles_dma_end;
\r
361 spu.decode_dirty_ch = pFO->decode_dirty_ch;
\r
362 spu.dwNoiseVal = pFO->dwNoiseVal;
\r
363 spu.dwNoiseCount = pFO->dwNoiseCount;
\r
364 spu.XARepeat = pFO->XARepeat;
\r
365 spu.XALastVal = pFO->XALastVal;
\r
366 spu.last_keyon_cycles = pFO->last_keyon_cycles;
\r
369 // repair some globals
\r
370 for(i=0;i<=62;i+=2)
\r
371 load_register(H_Reverb+i, cycles);
\r
372 load_register(H_SPUReverbAddr, cycles);
\r
373 load_register(H_SPUrvolL, cycles);
\r
374 load_register(H_SPUrvolR, cycles);
\r
375 load_register(H_CDLeft, cycles);
\r
376 load_register(H_CDRight, cycles);
\r
379 spu.rvb->StartAddr = regAreaGet(H_SPUReverbAddr) << 2;
\r
380 if (spu.rvb->CurrAddr < spu.rvb->StartAddr)
\r
381 spu.rvb->CurrAddr = spu.rvb->StartAddr;
\r
382 // fix to prevent new interpolations from crashing
\r
383 spu.interpolation = -1;
\r
385 ClearWorkingState();
\r
387 if (spu.spuCtrl & CTRL_IRQ)
\r
388 schedule_next_irq();
\r
393 ////////////////////////////////////////////////////////////////////////
\r
395 static SPUOSSFreeze_t * LoadStateV5(SPUFreeze_t * pF, uint32_t cycles)
\r
397 int i;SPUOSSFreeze_t * pFO;
\r
399 pFO=(SPUOSSFreeze_t *)(pF+1);
\r
401 spu.pSpuIrq = spu.spuMemC + ((spu.regArea[(H_SPUirqAddr - 0x0c00) / 2] << 3) & ~0xf);
\r
405 if (pFO->spuAddr == 0xbaadf00d) spu.spuAddr = 0;
\r
406 else spu.spuAddr = pFO->spuAddr & 0x7fffe;
\r
408 spu.decode_pos = pFO->decode_pos & 0x1ff;
\r
409 spu.rvb->CurrAddr = pFO->rvb_cur;
\r
410 spu.cycles_played = pFO->cycles_played ? pFO->cycles_played : cycles;
\r
412 spu.dwNewChannel=0;
\r
413 spu.dwChannelsAudible=0;
\r
414 spu.dwChannelDead=0;
\r
415 for(i=0;i<MAXCHAN;i++)
\r
417 load_channel(&spu.s_chan[i],&pFO->s_chan[i],i);
\r
419 spu.s_chan[i].pCurr+=(uintptr_t)spu.spuMemC;
\r
420 spu.s_chan[i].pLoop+=(uintptr_t)spu.spuMemC;
\r
426 ////////////////////////////////////////////////////////////////////////
\r
428 static void LoadStateUnknown(SPUFreeze_t * pF, uint32_t cycles)
\r
432 for(i=0;i<MAXCHAN;i++)
\r
434 spu.s_chan[i].pLoop=spu.spuMemC;
\r
437 spu.dwNewChannel=0;
\r
438 spu.dwChannelsAudible=0;
\r
439 spu.dwChannelDead=0;
\r
440 spu.pSpuIrq=spu.spuMemC;
\r
441 spu.cycles_played = cycles;
\r
443 for(i=0;i<0xc0;i++)
\r
445 load_register(0x1f801c00 + i*2, cycles);
\r
449 ////////////////////////////////////////////////////////////////////////
\r
450 // vim:shiftwidth=1:expandtab
\r