| 1 | /***************************************************************************\r |
| 2 | freeze.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_FREEZE\r |
| 21 | \r |
| 22 | #include "externals.h"\r |
| 23 | #include "registers.h"\r |
| 24 | #include "spu.h"\r |
| 25 | \r |
| 26 | ////////////////////////////////////////////////////////////////////////\r |
| 27 | // freeze structs\r |
| 28 | ////////////////////////////////////////////////////////////////////////\r |
| 29 | \r |
| 30 | typedef struct\r |
| 31 | {\r |
| 32 | int AttackModeExp;\r |
| 33 | int AttackTime;\r |
| 34 | int DecayTime;\r |
| 35 | int SustainLevel;\r |
| 36 | int SustainModeExp;\r |
| 37 | int SustainModeDec;\r |
| 38 | int SustainTime;\r |
| 39 | int ReleaseModeExp;\r |
| 40 | unsigned int ReleaseVal;\r |
| 41 | int ReleaseTime;\r |
| 42 | int ReleaseStartTime; \r |
| 43 | int ReleaseVol; \r |
| 44 | int lTime;\r |
| 45 | int lVolume;\r |
| 46 | } ADSRInfo;\r |
| 47 | \r |
| 48 | typedef struct\r |
| 49 | {\r |
| 50 | int State;\r |
| 51 | int AttackModeExp;\r |
| 52 | int AttackRate;\r |
| 53 | int DecayRate;\r |
| 54 | int SustainLevel;\r |
| 55 | int SustainModeExp;\r |
| 56 | int SustainIncrease;\r |
| 57 | int SustainRate;\r |
| 58 | int ReleaseModeExp;\r |
| 59 | int ReleaseRate;\r |
| 60 | int EnvelopeVol;\r |
| 61 | int lVolume;\r |
| 62 | int lDummy1;\r |
| 63 | int lDummy2;\r |
| 64 | } ADSRInfoEx_orig;\r |
| 65 | \r |
| 66 | typedef struct\r |
| 67 | {\r |
| 68 | // no mutexes used anymore... don't need them to sync access\r |
| 69 | //HANDLE hMutex;\r |
| 70 | \r |
| 71 | int bNew; // start flag\r |
| 72 | \r |
| 73 | int iSBPos; // mixing stuff\r |
| 74 | int spos;\r |
| 75 | int sinc;\r |
| 76 | 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 |
| 77 | int sval;\r |
| 78 | \r |
| 79 | int iStart; // start ptr into sound mem\r |
| 80 | int iCurr; // current pos in sound mem\r |
| 81 | int iLoop; // loop ptr in sound mem\r |
| 82 | \r |
| 83 | int bOn; // is channel active (sample playing?)\r |
| 84 | int bStop; // is channel stopped (sample _can_ still be playing, ADSR Release phase)\r |
| 85 | int bReverb; // can we do reverb on this channel? must have ctrl register bit, to get active\r |
| 86 | int iActFreq; // current psx pitch\r |
| 87 | int iUsedFreq; // current pc pitch\r |
| 88 | int iLeftVolume; // left volume\r |
| 89 | int iLeftVolRaw; // left psx volume value\r |
| 90 | int bIgnoreLoop; // ignore loop bit, if an external loop address is used\r |
| 91 | int iMute; // mute mode\r |
| 92 | int iRightVolume; // right volume\r |
| 93 | int iRightVolRaw; // right psx volume value\r |
| 94 | int iRawPitch; // raw pitch (0...3fff)\r |
| 95 | int iIrqDone; // debug irq done flag\r |
| 96 | int s_1; // last decoding infos\r |
| 97 | int s_2;\r |
| 98 | int bRVBActive; // reverb active flag\r |
| 99 | int iRVBOffset; // reverb offset\r |
| 100 | int iRVBRepeat; // reverb repeat\r |
| 101 | int bNoise; // noise active flag\r |
| 102 | int bFMod; // freq mod (0=off, 1=sound channel, 2=freq channel)\r |
| 103 | int iRVBNum; // another reverb helper\r |
| 104 | int iOldNoise; // old noise val for this channel \r |
| 105 | ADSRInfo ADSR; // active ADSR settings\r |
| 106 | ADSRInfoEx_orig ADSRX; // next ADSR settings (will be moved to active on sample start)\r |
| 107 | } SPUCHAN_orig;\r |
| 108 | \r |
| 109 | typedef struct\r |
| 110 | {\r |
| 111 | char szSPUName[8];\r |
| 112 | uint32_t ulFreezeVersion;\r |
| 113 | uint32_t ulFreezeSize;\r |
| 114 | unsigned char cSPUPort[0x200];\r |
| 115 | unsigned char cSPURam[0x80000];\r |
| 116 | xa_decode_t xaS; \r |
| 117 | } SPUFreeze_t;\r |
| 118 | \r |
| 119 | typedef struct\r |
| 120 | {\r |
| 121 | unsigned short spuIrq;\r |
| 122 | uint32_t pSpuIrq;\r |
| 123 | uint32_t spuAddr;\r |
| 124 | uint32_t dummy1;\r |
| 125 | uint32_t dummy2;\r |
| 126 | uint32_t dummy3;\r |
| 127 | \r |
| 128 | SPUCHAN_orig s_chan[MAXCHAN]; \r |
| 129 | \r |
| 130 | } SPUOSSFreeze_t;\r |
| 131 | \r |
| 132 | ////////////////////////////////////////////////////////////////////////\r |
| 133 | \r |
| 134 | void LoadStateV5(SPUFreeze_t * pF); // newest version\r |
| 135 | void LoadStateUnknown(SPUFreeze_t * pF); // unknown format\r |
| 136 | \r |
| 137 | extern int lastch;\r |
| 138 | \r |
| 139 | // we want to retain compatibility between versions,\r |
| 140 | // so use original channel struct\r |
| 141 | static void save_channel(SPUCHAN_orig *d, const SPUCHAN *s, int ch)\r |
| 142 | {\r |
| 143 | memset(d, 0, sizeof(*d));\r |
| 144 | d->bNew = !!(dwNewChannel & (1<<ch));\r |
| 145 | d->iSBPos = s->iSBPos;\r |
| 146 | d->spos = s->spos;\r |
| 147 | d->sinc = s->sinc;\r |
| 148 | memcpy(d->SB, s->SB, sizeof(d->SB));\r |
| 149 | d->iStart = (regAreaGet(ch,6)&~1)<<3;\r |
| 150 | d->iCurr = 0; // set by the caller\r |
| 151 | d->iLoop = 0; // set by the caller\r |
| 152 | d->bOn = !!(dwChannelOn & (1<<ch));\r |
| 153 | d->bStop = s->bStop;\r |
| 154 | d->bReverb = s->bReverb;\r |
| 155 | d->iActFreq = 1;\r |
| 156 | d->iUsedFreq = 2;\r |
| 157 | d->iLeftVolume = s->iLeftVolume;\r |
| 158 | // this one is nasty but safe, save compat is important\r |
| 159 | d->bIgnoreLoop = (s->prevflags ^ 2) << 1;\r |
| 160 | d->iRightVolume = s->iRightVolume;\r |
| 161 | d->iRawPitch = s->iRawPitch;\r |
| 162 | d->s_1 = s->SB[27]; // yes it's reversed\r |
| 163 | d->s_2 = s->SB[26];\r |
| 164 | d->bRVBActive = s->bRVBActive;\r |
| 165 | d->bNoise = s->bNoise;\r |
| 166 | d->bFMod = s->bFMod;\r |
| 167 | d->ADSRX.State = s->ADSRX.State;\r |
| 168 | d->ADSRX.AttackModeExp = s->ADSRX.AttackModeExp;\r |
| 169 | d->ADSRX.AttackRate = s->ADSRX.AttackRate;\r |
| 170 | d->ADSRX.DecayRate = s->ADSRX.DecayRate;\r |
| 171 | d->ADSRX.SustainLevel = s->ADSRX.SustainLevel;\r |
| 172 | d->ADSRX.SustainModeExp = s->ADSRX.SustainModeExp;\r |
| 173 | d->ADSRX.SustainIncrease = s->ADSRX.SustainIncrease;\r |
| 174 | d->ADSRX.SustainRate = s->ADSRX.SustainRate;\r |
| 175 | d->ADSRX.ReleaseModeExp = s->ADSRX.ReleaseModeExp;\r |
| 176 | d->ADSRX.ReleaseRate = s->ADSRX.ReleaseRate;\r |
| 177 | d->ADSRX.EnvelopeVol = s->ADSRX.EnvelopeVol;\r |
| 178 | d->ADSRX.lVolume = d->bOn; // hmh\r |
| 179 | }\r |
| 180 | \r |
| 181 | static void load_channel(SPUCHAN *d, const SPUCHAN_orig *s, int ch)\r |
| 182 | {\r |
| 183 | memset(d, 0, sizeof(*d));\r |
| 184 | if (s->bNew) dwNewChannel |= 1<<ch;\r |
| 185 | d->iSBPos = s->iSBPos;\r |
| 186 | d->spos = s->spos;\r |
| 187 | d->sinc = s->sinc;\r |
| 188 | memcpy(d->SB, s->SB, sizeof(d->SB));\r |
| 189 | d->pCurr = (void *)((long)s->iCurr & 0x7fff0);\r |
| 190 | d->pLoop = (void *)((long)s->iLoop & 0x7fff0);\r |
| 191 | d->bStop = s->bStop;\r |
| 192 | d->bReverb = s->bReverb;\r |
| 193 | d->iLeftVolume = s->iLeftVolume;\r |
| 194 | d->iRightVolume = s->iRightVolume;\r |
| 195 | d->iRawPitch = s->iRawPitch;\r |
| 196 | d->bRVBActive = s->bRVBActive;\r |
| 197 | d->bNoise = s->bNoise;\r |
| 198 | d->bFMod = s->bFMod;\r |
| 199 | d->prevflags = (s->bIgnoreLoop >> 1) ^ 2;\r |
| 200 | d->ADSRX.State = s->ADSRX.State;\r |
| 201 | d->ADSRX.AttackModeExp = s->ADSRX.AttackModeExp;\r |
| 202 | d->ADSRX.AttackRate = s->ADSRX.AttackRate;\r |
| 203 | d->ADSRX.DecayRate = s->ADSRX.DecayRate;\r |
| 204 | d->ADSRX.SustainLevel = s->ADSRX.SustainLevel;\r |
| 205 | d->ADSRX.SustainModeExp = s->ADSRX.SustainModeExp;\r |
| 206 | d->ADSRX.SustainIncrease = s->ADSRX.SustainIncrease;\r |
| 207 | d->ADSRX.SustainRate = s->ADSRX.SustainRate;\r |
| 208 | d->ADSRX.ReleaseModeExp = s->ADSRX.ReleaseModeExp;\r |
| 209 | d->ADSRX.ReleaseRate = s->ADSRX.ReleaseRate;\r |
| 210 | d->ADSRX.EnvelopeVol = s->ADSRX.EnvelopeVol;\r |
| 211 | if (s->bOn) dwChannelOn |= 1<<ch;\r |
| 212 | else d->ADSRX.EnvelopeVol = 0;\r |
| 213 | }\r |
| 214 | \r |
| 215 | ////////////////////////////////////////////////////////////////////////\r |
| 216 | // SPUFREEZE: called by main emu on savestate load/save\r |
| 217 | ////////////////////////////////////////////////////////////////////////\r |
| 218 | \r |
| 219 | long CALLBACK SPUfreeze(uint32_t ulFreezeMode,SPUFreeze_t * pF)\r |
| 220 | {\r |
| 221 | int i;SPUOSSFreeze_t * pFO;\r |
| 222 | \r |
| 223 | if(!pF) return 0; // first check\r |
| 224 | \r |
| 225 | if(ulFreezeMode) // info or save?\r |
| 226 | {//--------------------------------------------------//\r |
| 227 | if(ulFreezeMode==1) \r |
| 228 | memset(pF,0,sizeof(SPUFreeze_t)+sizeof(SPUOSSFreeze_t));\r |
| 229 | \r |
| 230 | strcpy(pF->szSPUName,"PBOSS");\r |
| 231 | pF->ulFreezeVersion=5;\r |
| 232 | pF->ulFreezeSize=sizeof(SPUFreeze_t)+sizeof(SPUOSSFreeze_t);\r |
| 233 | \r |
| 234 | if(ulFreezeMode==2) return 1; // info mode? ok, bye\r |
| 235 | // save mode:\r |
| 236 | memcpy(pF->cSPURam,spuMem,0x80000); // copy common infos\r |
| 237 | memcpy(pF->cSPUPort,regArea,0x200);\r |
| 238 | \r |
| 239 | if(xapGlobal && XAPlay!=XAFeed) // some xa\r |
| 240 | {\r |
| 241 | pF->xaS=*xapGlobal; \r |
| 242 | }\r |
| 243 | else \r |
| 244 | memset(&pF->xaS,0,sizeof(xa_decode_t)); // or clean xa\r |
| 245 | \r |
| 246 | pFO=(SPUOSSFreeze_t *)(pF+1); // store special stuff\r |
| 247 | \r |
| 248 | pFO->spuIrq=spuIrq;\r |
| 249 | if(pSpuIrq) pFO->pSpuIrq = (unsigned long)pSpuIrq-(unsigned long)spuMemC;\r |
| 250 | \r |
| 251 | pFO->spuAddr=spuAddr;\r |
| 252 | if(pFO->spuAddr==0) pFO->spuAddr=0xbaadf00d;\r |
| 253 | \r |
| 254 | for(i=0;i<MAXCHAN;i++)\r |
| 255 | {\r |
| 256 | save_channel(&pFO->s_chan[i],&s_chan[i],i);\r |
| 257 | if(s_chan[i].pCurr)\r |
| 258 | pFO->s_chan[i].iCurr=s_chan[i].pCurr-spuMemC;\r |
| 259 | if(s_chan[i].pLoop)\r |
| 260 | pFO->s_chan[i].iLoop=s_chan[i].pLoop-spuMemC;\r |
| 261 | }\r |
| 262 | \r |
| 263 | return 1;\r |
| 264 | //--------------------------------------------------//\r |
| 265 | }\r |
| 266 | \r |
| 267 | if(ulFreezeMode!=0) return 0; // bad mode? bye\r |
| 268 | \r |
| 269 | memcpy(spuMem,pF->cSPURam,0x80000); // get ram\r |
| 270 | memcpy(regArea,pF->cSPUPort,0x200);\r |
| 271 | \r |
| 272 | if(pF->xaS.nsamples<=4032) // start xa again\r |
| 273 | SPUplayADPCMchannel(&pF->xaS);\r |
| 274 | \r |
| 275 | xapGlobal=0;\r |
| 276 | \r |
| 277 | if(!strcmp(pF->szSPUName,"PBOSS") && pF->ulFreezeVersion==5)\r |
| 278 | LoadStateV5(pF);\r |
| 279 | else LoadStateUnknown(pF);\r |
| 280 | \r |
| 281 | lastch = -1;\r |
| 282 | \r |
| 283 | // repair some globals\r |
| 284 | for(i=0;i<=62;i+=2)\r |
| 285 | SPUwriteRegister(H_Reverb+i,regArea[(H_Reverb+i-0xc00)>>1]);\r |
| 286 | SPUwriteRegister(H_SPUReverbAddr,regArea[(H_SPUReverbAddr-0xc00)>>1]);\r |
| 287 | SPUwriteRegister(H_SPUrvolL,regArea[(H_SPUrvolL-0xc00)>>1]);\r |
| 288 | SPUwriteRegister(H_SPUrvolR,regArea[(H_SPUrvolR-0xc00)>>1]);\r |
| 289 | \r |
| 290 | SPUwriteRegister(H_SPUctrl,(unsigned short)(regArea[(H_SPUctrl-0xc00)>>1]|0x4000));\r |
| 291 | SPUwriteRegister(H_SPUstat,regArea[(H_SPUstat-0xc00)>>1]);\r |
| 292 | SPUwriteRegister(H_CDLeft,regArea[(H_CDLeft-0xc00)>>1]);\r |
| 293 | SPUwriteRegister(H_CDRight,regArea[(H_CDRight-0xc00)>>1]);\r |
| 294 | \r |
| 295 | // fix to prevent new interpolations from crashing\r |
| 296 | for(i=0;i<MAXCHAN;i++) s_chan[i].SB[28]=0;\r |
| 297 | \r |
| 298 | ClearWorkingState();\r |
| 299 | \r |
| 300 | return 1;\r |
| 301 | }\r |
| 302 | \r |
| 303 | ////////////////////////////////////////////////////////////////////////\r |
| 304 | \r |
| 305 | void LoadStateV5(SPUFreeze_t * pF)\r |
| 306 | {\r |
| 307 | int i;SPUOSSFreeze_t * pFO;\r |
| 308 | \r |
| 309 | pFO=(SPUOSSFreeze_t *)(pF+1);\r |
| 310 | \r |
| 311 | spuIrq = pFO->spuIrq;\r |
| 312 | if(pFO->pSpuIrq) pSpuIrq = spuMemC+((long)pFO->pSpuIrq&0x7fff0); else pSpuIrq=NULL;\r |
| 313 | \r |
| 314 | if(pFO->spuAddr)\r |
| 315 | {\r |
| 316 | spuAddr = pFO->spuAddr;\r |
| 317 | if (spuAddr == 0xbaadf00d) spuAddr = 0;\r |
| 318 | }\r |
| 319 | \r |
| 320 | dwNewChannel=0;\r |
| 321 | dwChannelOn=0;\r |
| 322 | dwChannelDead=0;\r |
| 323 | for(i=0;i<MAXCHAN;i++)\r |
| 324 | {\r |
| 325 | load_channel(&s_chan[i],&pFO->s_chan[i],i);\r |
| 326 | \r |
| 327 | s_chan[i].pCurr+=(unsigned long)spuMemC;\r |
| 328 | s_chan[i].pLoop+=(unsigned long)spuMemC;\r |
| 329 | }\r |
| 330 | }\r |
| 331 | \r |
| 332 | ////////////////////////////////////////////////////////////////////////\r |
| 333 | \r |
| 334 | void LoadStateUnknown(SPUFreeze_t * pF)\r |
| 335 | {\r |
| 336 | int i;\r |
| 337 | \r |
| 338 | for(i=0;i<MAXCHAN;i++)\r |
| 339 | {\r |
| 340 | s_chan[i].bStop=0;\r |
| 341 | s_chan[i].pLoop=spuMemC;\r |
| 342 | }\r |
| 343 | \r |
| 344 | dwNewChannel=0;\r |
| 345 | dwChannelOn=0;\r |
| 346 | dwChannelDead=0;\r |
| 347 | pSpuIrq=0;\r |
| 348 | \r |
| 349 | for(i=0;i<0xc0;i++)\r |
| 350 | {\r |
| 351 | SPUwriteRegister(0x1f801c00+i*2,regArea[i]);\r |
| 352 | }\r |
| 353 | }\r |
| 354 | \r |
| 355 | ////////////////////////////////////////////////////////////////////////\r |