add a thp-based huge page alloc fallback
[pcsx_rearmed.git] / plugins / dfsound / freeze.c
CommitLineData
ef79bbde
P
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
ef79bbde
P
25\r
26////////////////////////////////////////////////////////////////////////\r
27// freeze structs\r
28////////////////////////////////////////////////////////////////////////\r
29\r
c668f248 30typedef 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
6d866bb7 48typedef 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
c668f248 61 int lVolume;\r
62 int lDummy1;\r
63 int lDummy2;\r
6d866bb7 64} ADSRInfoEx_orig;\r
65\r
66typedef 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
8e1040b6 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
6d866bb7 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
ef79bbde
P
109typedef 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
119typedef struct\r
120{\r
121 unsigned short spuIrq;\r
0ad5d4a1 122 unsigned short decode_pos;\r
ef79bbde
P
123 uint32_t pSpuIrq;\r
124 uint32_t spuAddr;\r
403a6290 125 uint32_t rvb_cur;\r
126 uint16_t xa_left;\r
127 uint16_t cdda_left;\r
128 uint32_t cycles_played;\r
ef79bbde 129\r
6d866bb7 130 SPUCHAN_orig s_chan[MAXCHAN]; \r
ef79bbde
P
131\r
132} SPUOSSFreeze_t;\r
133\r
134////////////////////////////////////////////////////////////////////////\r
135\r
403a6290 136static SPUOSSFreeze_t * LoadStateV5(SPUFreeze_t * pF, uint32_t cycles);\r
137static void LoadStateUnknown(SPUFreeze_t * pF, uint32_t cycles); // unknown format\r
ef79bbde 138\r
6d866bb7 139// we want to retain compatibility between versions,\r
140// so use original channel struct\r
381ea103 141static void save_channel(SPUCHAN_orig *d, const SPUCHAN *s, int ch)\r
6d866bb7 142{\r
143 memset(d, 0, sizeof(*d));\r
3154bfab 144 d->bNew = !!(spu.dwNewChannel & (1<<ch));\r
6d866bb7 145 d->iSBPos = s->iSBPos;\r
146 d->spos = s->spos;\r
147 d->sinc = s->sinc;\r
de4a0279 148 memcpy(d->SB, spu.SB + ch * SB_SIZE, sizeof(d->SB[0]) * SB_SIZE);\r
a0d4a8f5 149 d->iStart = (regAreaGetCh(ch, 6) & ~1) << 3;\r
8e1040b6 150 d->iCurr = 0; // set by the caller\r
151 d->iLoop = 0; // set by the caller\r
2daa7c1d 152 d->bOn = !!(spu.dwChannelsAudible & (1<<ch));\r
9ad8abfa 153 d->bStop = s->ADSRX.State == ADSR_RELEASE;\r
6d866bb7 154 d->bReverb = s->bReverb;\r
7e44d49d 155 d->iActFreq = 1;\r
156 d->iUsedFreq = 2;\r
6d866bb7 157 d->iLeftVolume = s->iLeftVolume;\r
e4f075af 158 // this one is nasty but safe, save compat is important\r
159 d->bIgnoreLoop = (s->prevflags ^ 2) << 1;\r
6d866bb7 160 d->iRightVolume = s->iRightVolume;\r
161 d->iRawPitch = s->iRawPitch;\r
de4a0279 162 d->s_1 = spu.SB[ch * SB_SIZE + 27]; // yes it's reversed\r
163 d->s_2 = spu.SB[ch * SB_SIZE + 26];\r
6d866bb7 164 d->bRVBActive = s->bRVBActive;\r
6d866bb7 165 d->bNoise = s->bNoise;\r
166 d->bFMod = s->bFMod;\r
6d866bb7 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
381ea103 181static void load_channel(SPUCHAN *d, const SPUCHAN_orig *s, int ch)\r
6d866bb7 182{\r
183 memset(d, 0, sizeof(*d));\r
3154bfab 184 if (s->bNew) spu.dwNewChannel |= 1<<ch;\r
6d866bb7 185 d->iSBPos = s->iSBPos;\r
650adfd2 186 if ((uint32_t)d->iSBPos >= 28) d->iSBPos = 27;\r
6d866bb7 187 d->spos = s->spos;\r
188 d->sinc = s->sinc;\r
650adfd2 189 d->sinc_inv = 0;\r
de4a0279 190 memcpy(spu.SB + ch * SB_SIZE, s->SB, sizeof(spu.SB[0]) * SB_SIZE);\r
2d724e28 191 d->pCurr = (void *)((uintptr_t)s->iCurr & 0x7fff0);\r
192 d->pLoop = (void *)((uintptr_t)s->iLoop & 0x7fff0);\r
6d866bb7 193 d->bReverb = s->bReverb;\r
6d866bb7 194 d->iLeftVolume = s->iLeftVolume;\r
6d866bb7 195 d->iRightVolume = s->iRightVolume;\r
196 d->iRawPitch = s->iRawPitch;\r
6d866bb7 197 d->bRVBActive = s->bRVBActive;\r
6d866bb7 198 d->bNoise = s->bNoise;\r
199 d->bFMod = s->bFMod;\r
e4f075af 200 d->prevflags = (s->bIgnoreLoop >> 1) ^ 2;\r
6d866bb7 201 d->ADSRX.State = s->ADSRX.State;\r
9ad8abfa 202 if (s->bStop) d->ADSRX.State = ADSR_RELEASE;\r
6d866bb7 203 d->ADSRX.AttackModeExp = s->ADSRX.AttackModeExp;\r
204 d->ADSRX.AttackRate = s->ADSRX.AttackRate;\r
205 d->ADSRX.DecayRate = s->ADSRX.DecayRate;\r
206 d->ADSRX.SustainLevel = s->ADSRX.SustainLevel;\r
207 d->ADSRX.SustainModeExp = s->ADSRX.SustainModeExp;\r
208 d->ADSRX.SustainIncrease = s->ADSRX.SustainIncrease;\r
209 d->ADSRX.SustainRate = s->ADSRX.SustainRate;\r
210 d->ADSRX.ReleaseModeExp = s->ADSRX.ReleaseModeExp;\r
211 d->ADSRX.ReleaseRate = s->ADSRX.ReleaseRate;\r
212 d->ADSRX.EnvelopeVol = s->ADSRX.EnvelopeVol;\r
2daa7c1d 213 if (s->bOn) spu.dwChannelsAudible |= 1<<ch;\r
6f6fe969 214 else d->ADSRX.EnvelopeVol = 0;\r
6d866bb7 215}\r
216\r
650adfd2 217// force load from regArea to variables\r
218static void load_register(unsigned long reg, unsigned int cycles)\r
219{\r
3154bfab 220 unsigned short *r = &spu.regArea[((reg & 0xfff) - 0xc00) >> 1];\r
650adfd2 221 *r ^= 1;\r
222 SPUwriteRegister(reg, *r ^ 1, cycles);\r
223}\r
224\r
ef79bbde
P
225////////////////////////////////////////////////////////////////////////\r
226// SPUFREEZE: called by main emu on savestate load/save\r
227////////////////////////////////////////////////////////////////////////\r
228\r
650adfd2 229long CALLBACK SPUfreeze(uint32_t ulFreezeMode, SPUFreeze_t * pF,\r
230 uint32_t cycles)\r
ef79bbde 231{\r
403a6290 232 SPUOSSFreeze_t * pFO = NULL;\r
233 int i;\r
ef79bbde
P
234\r
235 if(!pF) return 0; // first check\r
236\r
237 if(ulFreezeMode) // info or save?\r
238 {//--------------------------------------------------//\r
403a6290 239 int xa_left = 0, cdda_left = 0;\r
240 do_samples(cycles, 1);\r
241\r
ef79bbde
P
242 if(ulFreezeMode==1) \r
243 memset(pF,0,sizeof(SPUFreeze_t)+sizeof(SPUOSSFreeze_t));\r
244\r
245 strcpy(pF->szSPUName,"PBOSS");\r
246 pF->ulFreezeVersion=5;\r
247 pF->ulFreezeSize=sizeof(SPUFreeze_t)+sizeof(SPUOSSFreeze_t);\r
248\r
249 if(ulFreezeMode==2) return 1; // info mode? ok, bye\r
250 // save mode:\r
3154bfab 251 memcpy(pF->cSPURam,spu.spuMem,0x80000); // copy common infos\r
252 memcpy(pF->cSPUPort,spu.regArea,0x200);\r
ef79bbde 253\r
3154bfab 254 if(spu.xapGlobal && spu.XAPlay!=spu.XAFeed) // some xa\r
ef79bbde 255 {\r
403a6290 256 xa_left = spu.XAFeed - spu.XAPlay;\r
257 if (xa_left < 0)\r
258 xa_left = spu.XAEnd - spu.XAPlay + spu.XAFeed - spu.XAStart;\r
259 pF->xaS = *spu.xapGlobal;\r
260 }\r
261 else if (spu.CDDAPlay != spu.CDDAFeed)\r
262 {\r
263 // abuse the xa struct to store leftover cdda samples\r
264 unsigned int *p = spu.CDDAPlay;\r
265 cdda_left = spu.CDDAFeed - spu.CDDAPlay;\r
266 if (cdda_left < 0)\r
267 cdda_left = spu.CDDAEnd - spu.CDDAPlay + spu.CDDAFeed - spu.CDDAStart;\r
268 if (cdda_left > sizeof(pF->xaS.pcm) / 4)\r
269 cdda_left = sizeof(pF->xaS.pcm) / 4;\r
270 if (p + cdda_left <= spu.CDDAEnd)\r
271 memcpy(pF->xaS.pcm, p, cdda_left * 4);\r
272 else {\r
273 memcpy(pF->xaS.pcm, p, (spu.CDDAEnd - p) * 4);\r
274 memcpy((char *)pF->xaS.pcm + (spu.CDDAEnd - p) * 4, spu.CDDAStart,\r
275 (cdda_left - (spu.CDDAEnd - p)) * 4);\r
276 }\r
277 pF->xaS.nsamples = 0;\r
ef79bbde 278 }\r
403a6290 279 else\r
280 memset(&pF->xaS,0,sizeof(xa_decode_t)); // or clean xa\r
ef79bbde
P
281\r
282 pFO=(SPUOSSFreeze_t *)(pF+1); // store special stuff\r
283\r
3154bfab 284 pFO->spuIrq = spu.regArea[(H_SPUirqAddr - 0x0c00) / 2];\r
2d724e28 285 if(spu.pSpuIrq) pFO->pSpuIrq = spu.pSpuIrq - spu.spuMemC;\r
ef79bbde 286\r
3154bfab 287 pFO->spuAddr=spu.spuAddr;\r
ef79bbde 288 if(pFO->spuAddr==0) pFO->spuAddr=0xbaadf00d;\r
0ad5d4a1 289 pFO->decode_pos = spu.decode_pos;\r
403a6290 290 pFO->rvb_cur = spu.rvb->CurrAddr;\r
291 pFO->xa_left = xa_left;\r
292 pFO->cdda_left = cdda_left;\r
293 pFO->cycles_played = spu.cycles_played;\r
ef79bbde
P
294\r
295 for(i=0;i<MAXCHAN;i++)\r
296 {\r
5514a050 297 save_channel(&pFO->s_chan[i],&spu.s_chan[i],i);\r
298 if(spu.s_chan[i].pCurr)\r
299 pFO->s_chan[i].iCurr=spu.s_chan[i].pCurr-spu.spuMemC;\r
300 if(spu.s_chan[i].pLoop)\r
301 pFO->s_chan[i].iLoop=spu.s_chan[i].pLoop-spu.spuMemC;\r
ef79bbde
P
302 }\r
303\r
ef79bbde
P
304 return 1;\r
305 //--------------------------------------------------//\r
306 }\r
307 \r
308 if(ulFreezeMode!=0) return 0; // bad mode? bye\r
309\r
3154bfab 310 memcpy(spu.spuMem,pF->cSPURam,0x80000); // get ram\r
311 memcpy(spu.regArea,pF->cSPUPort,0x200);\r
0c1151fe 312 spu.bMemDirty = 1;\r
ef79bbde 313\r
403a6290 314 if (!strcmp(pF->szSPUName,"PBOSS") && pF->ulFreezeVersion==5)\r
315 pFO = LoadStateV5(pF, cycles);\r
650adfd2 316 else LoadStateUnknown(pF, cycles);\r
ef79bbde 317\r
403a6290 318 spu.XAPlay = spu.XAFeed = spu.XAStart;\r
319 spu.CDDAPlay = spu.CDDAFeed = spu.CDDAStart;\r
320 if (pFO && pFO->xa_left && pF->xaS.nsamples) { // start xa again\r
321 FeedXA(&pF->xaS);\r
322 spu.XAPlay = spu.XAFeed - pFO->xa_left;\r
323 if (spu.XAPlay < spu.XAStart)\r
324 spu.XAPlay = spu.XAStart;\r
325 }\r
326 else if (pFO && pFO->cdda_left) { // start cdda again\r
327 FeedCDDA((void *)pF->xaS.pcm, pFO->cdda_left * 4);\r
328 }\r
329\r
ef79bbde
P
330 // repair some globals\r
331 for(i=0;i<=62;i+=2)\r
650adfd2 332 load_register(H_Reverb+i, cycles);\r
333 load_register(H_SPUReverbAddr, cycles);\r
334 load_register(H_SPUrvolL, cycles);\r
335 load_register(H_SPUrvolR, cycles);\r
ef79bbde 336\r
650adfd2 337 load_register(H_SPUctrl, cycles);\r
338 load_register(H_SPUstat, cycles);\r
339 load_register(H_CDLeft, cycles);\r
340 load_register(H_CDRight, cycles);\r
ef79bbde
P
341\r
342 // fix to prevent new interpolations from crashing\r
de4a0279 343 for(i=0;i<MAXCHAN;i++) spu.SB[i * SB_SIZE + 28]=0;\r
ef79bbde 344\r
6d75977b 345 ClearWorkingState();\r
ef79bbde 346\r
c4c66b22 347 if (spu.spuCtrl & CTRL_IRQ)\r
348 schedule_next_irq();\r
349\r
ef79bbde
P
350 return 1;\r
351}\r
352\r
353////////////////////////////////////////////////////////////////////////\r
354\r
403a6290 355static SPUOSSFreeze_t * LoadStateV5(SPUFreeze_t * pF, uint32_t cycles)\r
ef79bbde
P
356{\r
357 int i;SPUOSSFreeze_t * pFO;\r
358\r
359 pFO=(SPUOSSFreeze_t *)(pF+1);\r
360\r
c4c66b22 361 spu.pSpuIrq = spu.spuMemC + ((spu.regArea[(H_SPUirqAddr - 0x0c00) / 2] << 3) & ~0xf);\r
ef79bbde
P
362\r
363 if(pFO->spuAddr)\r
364 {\r
5514a050 365 if (pFO->spuAddr == 0xbaadf00d) spu.spuAddr = 0;\r
366 else spu.spuAddr = pFO->spuAddr & 0x7fffe;\r
ef79bbde 367 }\r
0ad5d4a1 368 spu.decode_pos = pFO->decode_pos & 0x1ff;\r
403a6290 369 spu.rvb->CurrAddr = pFO->rvb_cur;\r
370 spu.cycles_played = pFO->cycles_played ? pFO->cycles_played : cycles;\r
ef79bbde 371\r
3154bfab 372 spu.dwNewChannel=0;\r
2daa7c1d 373 spu.dwChannelsAudible=0;\r
3154bfab 374 spu.dwChannelDead=0;\r
ef79bbde
P
375 for(i=0;i<MAXCHAN;i++)\r
376 {\r
5514a050 377 load_channel(&spu.s_chan[i],&pFO->s_chan[i],i);\r
ef79bbde 378\r
2d724e28 379 spu.s_chan[i].pCurr+=(uintptr_t)spu.spuMemC;\r
380 spu.s_chan[i].pLoop+=(uintptr_t)spu.spuMemC;\r
ef79bbde 381 }\r
403a6290 382 return pFO;\r
ef79bbde
P
383}\r
384\r
385////////////////////////////////////////////////////////////////////////\r
386\r
403a6290 387static void LoadStateUnknown(SPUFreeze_t * pF, uint32_t cycles)\r
ef79bbde
P
388{\r
389 int i;\r
390\r
391 for(i=0;i<MAXCHAN;i++)\r
392 {\r
5514a050 393 spu.s_chan[i].pLoop=spu.spuMemC;\r
ef79bbde
P
394 }\r
395\r
3154bfab 396 spu.dwNewChannel=0;\r
2daa7c1d 397 spu.dwChannelsAudible=0;\r
3154bfab 398 spu.dwChannelDead=0;\r
c4c66b22 399 spu.pSpuIrq=spu.spuMemC;\r
403a6290 400 spu.cycles_played = cycles;\r
ef79bbde
P
401\r
402 for(i=0;i<0xc0;i++)\r
403 {\r
650adfd2 404 load_register(0x1f801c00 + i*2, cycles);\r
ef79bbde
P
405 }\r
406}\r
407\r
408////////////////////////////////////////////////////////////////////////\r
403a6290 409// vim:shiftwidth=1:expandtab\r