add a thp-based huge page alloc fallback
[pcsx_rearmed.git] / plugins / dfsound / freeze.c
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  unsigned short  decode_pos;\r
123  uint32_t   pSpuIrq;\r
124  uint32_t   spuAddr;\r
125  uint32_t   rvb_cur;\r
126  uint16_t   xa_left;\r
127  uint16_t   cdda_left;\r
128  uint32_t   cycles_played;\r
129 \r
130  SPUCHAN_orig s_chan[MAXCHAN];   \r
131 \r
132 } SPUOSSFreeze_t;\r
133 \r
134 ////////////////////////////////////////////////////////////////////////\r
135 \r
136 static SPUOSSFreeze_t * LoadStateV5(SPUFreeze_t * pF, uint32_t cycles);\r
137 static void LoadStateUnknown(SPUFreeze_t * pF, uint32_t cycles); // unknown format\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 = !!(spu.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, spu.SB + ch * SB_SIZE, sizeof(d->SB[0]) * SB_SIZE);\r
149  d->iStart = (regAreaGetCh(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 = !!(spu.dwChannelsAudible & (1<<ch));\r
153  d->bStop = s->ADSRX.State == ADSR_RELEASE;\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 = spu.SB[ch * SB_SIZE + 27]; // yes it's reversed\r
163  d->s_2 = spu.SB[ch * SB_SIZE + 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) spu.dwNewChannel |= 1<<ch;\r
185  d->iSBPos = s->iSBPos;\r
186  if ((uint32_t)d->iSBPos >= 28) d->iSBPos = 27;\r
187  d->spos = s->spos;\r
188  d->sinc = s->sinc;\r
189  d->sinc_inv = 0;\r
190  memcpy(spu.SB + ch * SB_SIZE, s->SB, sizeof(spu.SB[0]) * SB_SIZE);\r
191  d->pCurr = (void *)((uintptr_t)s->iCurr & 0x7fff0);\r
192  d->pLoop = (void *)((uintptr_t)s->iLoop & 0x7fff0);\r
193  d->bReverb = s->bReverb;\r
194  d->iLeftVolume = s->iLeftVolume;\r
195  d->iRightVolume = s->iRightVolume;\r
196  d->iRawPitch = s->iRawPitch;\r
197  d->bRVBActive = s->bRVBActive;\r
198  d->bNoise = s->bNoise;\r
199  d->bFMod = s->bFMod;\r
200  d->prevflags = (s->bIgnoreLoop >> 1) ^ 2;\r
201  d->ADSRX.State = s->ADSRX.State;\r
202  if (s->bStop) d->ADSRX.State = ADSR_RELEASE;\r
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
213  if (s->bOn) spu.dwChannelsAudible |= 1<<ch;\r
214  else d->ADSRX.EnvelopeVol = 0;\r
215 }\r
216 \r
217 // force load from regArea to variables\r
218 static void load_register(unsigned long reg, unsigned int cycles)\r
219 {\r
220  unsigned short *r = &spu.regArea[((reg & 0xfff) - 0xc00) >> 1];\r
221  *r ^= 1;\r
222  SPUwriteRegister(reg, *r ^ 1, cycles);\r
223 }\r
224 \r
225 ////////////////////////////////////////////////////////////////////////\r
226 // SPUFREEZE: called by main emu on savestate load/save\r
227 ////////////////////////////////////////////////////////////////////////\r
228 \r
229 long CALLBACK SPUfreeze(uint32_t ulFreezeMode, SPUFreeze_t * pF,\r
230  uint32_t cycles)\r
231 {\r
232  SPUOSSFreeze_t * pFO = NULL;\r
233  int i;\r
234 \r
235  if(!pF) return 0;                                     // first check\r
236 \r
237  if(ulFreezeMode)                                      // info or save?\r
238   {//--------------------------------------------------//\r
239    int xa_left = 0, cdda_left = 0;\r
240    do_samples(cycles, 1);\r
241 \r
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
251    memcpy(pF->cSPURam,spu.spuMem,0x80000);             // copy common infos\r
252    memcpy(pF->cSPUPort,spu.regArea,0x200);\r
253 \r
254    if(spu.xapGlobal && spu.XAPlay!=spu.XAFeed)         // some xa\r
255     {\r
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
278     }\r
279    else\r
280     memset(&pF->xaS,0,sizeof(xa_decode_t));            // or clean xa\r
281 \r
282    pFO=(SPUOSSFreeze_t *)(pF+1);                       // store special stuff\r
283 \r
284    pFO->spuIrq = spu.regArea[(H_SPUirqAddr - 0x0c00) / 2];\r
285    if(spu.pSpuIrq) pFO->pSpuIrq = spu.pSpuIrq - spu.spuMemC;\r
286 \r
287    pFO->spuAddr=spu.spuAddr;\r
288    if(pFO->spuAddr==0) pFO->spuAddr=0xbaadf00d;\r
289    pFO->decode_pos = spu.decode_pos;\r
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
294 \r
295    for(i=0;i<MAXCHAN;i++)\r
296     {\r
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
302     }\r
303 \r
304    return 1;\r
305    //--------------------------------------------------//\r
306   }\r
307                                                        \r
308  if(ulFreezeMode!=0) return 0;                         // bad mode? bye\r
309 \r
310  memcpy(spu.spuMem,pF->cSPURam,0x80000);               // get ram\r
311  memcpy(spu.regArea,pF->cSPUPort,0x200);\r
312  spu.bMemDirty = 1;\r
313 \r
314  if (!strcmp(pF->szSPUName,"PBOSS") && pF->ulFreezeVersion==5)\r
315    pFO = LoadStateV5(pF, cycles);\r
316  else LoadStateUnknown(pF, cycles);\r
317 \r
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
330  // repair some globals\r
331  for(i=0;i<=62;i+=2)\r
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
336 \r
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
341 \r
342  // fix to prevent new interpolations from crashing\r
343  for(i=0;i<MAXCHAN;i++) spu.SB[i * SB_SIZE + 28]=0;\r
344 \r
345  ClearWorkingState();\r
346 \r
347  if (spu.spuCtrl & CTRL_IRQ)\r
348   schedule_next_irq();\r
349 \r
350  return 1;\r
351 }\r
352 \r
353 ////////////////////////////////////////////////////////////////////////\r
354 \r
355 static SPUOSSFreeze_t * LoadStateV5(SPUFreeze_t * pF, uint32_t cycles)\r
356 {\r
357  int i;SPUOSSFreeze_t * pFO;\r
358 \r
359  pFO=(SPUOSSFreeze_t *)(pF+1);\r
360 \r
361  spu.pSpuIrq = spu.spuMemC + ((spu.regArea[(H_SPUirqAddr - 0x0c00) / 2] << 3) & ~0xf);\r
362 \r
363  if(pFO->spuAddr)\r
364   {\r
365    if (pFO->spuAddr == 0xbaadf00d) spu.spuAddr = 0;\r
366    else spu.spuAddr = pFO->spuAddr & 0x7fffe;\r
367   }\r
368  spu.decode_pos = pFO->decode_pos & 0x1ff;\r
369  spu.rvb->CurrAddr = pFO->rvb_cur;\r
370  spu.cycles_played = pFO->cycles_played ? pFO->cycles_played : cycles;\r
371 \r
372  spu.dwNewChannel=0;\r
373  spu.dwChannelsAudible=0;\r
374  spu.dwChannelDead=0;\r
375  for(i=0;i<MAXCHAN;i++)\r
376   {\r
377    load_channel(&spu.s_chan[i],&pFO->s_chan[i],i);\r
378 \r
379    spu.s_chan[i].pCurr+=(uintptr_t)spu.spuMemC;\r
380    spu.s_chan[i].pLoop+=(uintptr_t)spu.spuMemC;\r
381   }\r
382  return pFO;\r
383 }\r
384 \r
385 ////////////////////////////////////////////////////////////////////////\r
386 \r
387 static void LoadStateUnknown(SPUFreeze_t * pF, uint32_t cycles)\r
388 {\r
389  int i;\r
390 \r
391  for(i=0;i<MAXCHAN;i++)\r
392   {\r
393    spu.s_chan[i].pLoop=spu.spuMemC;\r
394   }\r
395 \r
396  spu.dwNewChannel=0;\r
397  spu.dwChannelsAudible=0;\r
398  spu.dwChannelDead=0;\r
399  spu.pSpuIrq=spu.spuMemC;\r
400  spu.cycles_played = cycles;\r
401 \r
402  for(i=0;i<0xc0;i++)\r
403   {\r
404    load_register(0x1f801c00 + i*2, cycles);\r
405   }\r
406 }\r
407 \r
408 ////////////////////////////////////////////////////////////////////////\r
409 // vim:shiftwidth=1:expandtab\r