standalone: allow scaler to cut off the letterbox
[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 <assert.h>\r
19 #include "stdafx.h"\r
20 \r
21 #define _IN_FREEZE\r
22 \r
23 #include "externals.h"\r
24 #include "registers.h"\r
25 #include "spu.h"\r
26 \r
27 ////////////////////////////////////////////////////////////////////////\r
28 // freeze structs\r
29 ////////////////////////////////////////////////////////////////////////\r
30 \r
31 typedef struct\r
32 {\r
33  int            AttackModeExp;\r
34  int            AttackTime;\r
35  int            DecayTime;\r
36  int            SustainLevel;\r
37  int            SustainModeExp;\r
38  int            SustainModeDec;\r
39  int            SustainTime;\r
40  int            ReleaseModeExp;\r
41  unsigned int   ReleaseVal;\r
42  int            ReleaseTime;\r
43  int            ReleaseStartTime; \r
44  int            ReleaseVol; \r
45  int            lTime;\r
46  int            lVolume;\r
47 } ADSRInfo;\r
48 \r
49 typedef struct\r
50 {\r
51  int            State;\r
52  int            AttackModeExp;\r
53  int            AttackRate;\r
54  int            DecayRate;\r
55  int            SustainLevel;\r
56  int            SustainModeExp;\r
57  int            SustainIncrease;\r
58  int            SustainRate;\r
59  int            ReleaseModeExp;\r
60  int            ReleaseRate;\r
61  int            EnvelopeVol;\r
62  int            lVolume;\r
63  int            lDummy1;\r
64  int            lDummy2;\r
65 } ADSRInfoEx_orig;\r
66 \r
67 typedef struct\r
68 {\r
69  // no mutexes used anymore... don't need them to sync access\r
70  //HANDLE            hMutex;\r
71 \r
72  int               bNew;                               // start flag\r
73 \r
74  int               iSBPos;                             // mixing stuff\r
75  int               spos;\r
76  int               sinc;\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
78  int               sval;\r
79 \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
83 \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
98  int               s_2;\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
108 } SPUCHAN_orig;\r
109 \r
110 typedef struct SPUFreeze\r
111 {\r
112  char          szSPUName[8];\r
113  uint32_t ulFreezeVersion;\r
114  uint32_t ulFreezeSize;\r
115  unsigned char cSPUPort[0x200];\r
116  unsigned char cSPURam[0x80000];\r
117  xa_decode_t   xaS;     \r
118 } SPUFreeze_t;\r
119 \r
120 typedef struct\r
121 {\r
122  unsigned short  spuIrq;\r
123  unsigned short  decode_pos;\r
124  uint32_t   pSpuIrq;\r
125  uint32_t   spuAddr;\r
126  uint32_t   rvb_cur;\r
127  uint16_t   xa_left;\r
128  uint16_t   cdda_left;\r
129  uint32_t   cycles_played;\r
130 \r
131  SPUCHAN_orig s_chan[MAXCHAN];   \r
132 \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
137  uint32_t   XARepeat;\r
138  uint32_t   XALastVal;\r
139  uint32_t   last_keyon_cycles;\r
140 \r
141 } SPUOSSFreeze_t;\r
142 \r
143 ////////////////////////////////////////////////////////////////////////\r
144 \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
147 \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
151 {\r
152  memset(d, 0, sizeof(*d));\r
153  d->bNew = !!(spu.dwNewChannel & (1<<ch));\r
154  d->iSBPos = s->iSBPos;\r
155  d->spos = s->spos;\r
156  d->sinc = s->sinc;\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
165  d->iActFreq = 1;\r
166  d->iUsedFreq = 2;\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
189 }\r
190 \r
191 static void load_channel(SPUCHAN *d, const SPUCHAN_orig *s, int ch)\r
192 {\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
197  d->spos = s->spos;\r
198  d->sinc = s->sinc;\r
199  d->sinc_inv = 0;\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
225 }\r
226 \r
227 // force load from regArea to variables\r
228 static void load_register(unsigned long reg, unsigned int cycles)\r
229 {\r
230  unsigned short *r = &spu.regArea[((reg & 0xfff) - 0xc00) >> 1];\r
231  *r ^= 1;\r
232  SPUwriteRegister(reg, *r ^ 1, cycles);\r
233 }\r
234 \r
235 ////////////////////////////////////////////////////////////////////////\r
236 // SPUFREEZE: called by main emu on savestate load/save\r
237 ////////////////////////////////////////////////////////////////////////\r
238 \r
239 long CALLBACK SPUfreeze(unsigned int ulFreezeMode, SPUFreeze_t * pF,\r
240  unsigned int cycles)\r
241 {\r
242  SPUOSSFreeze_t * pFO = NULL;\r
243  int i;\r
244 \r
245  if(!pF) return 0;                                     // first check\r
246 \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
251 \r
252    if(ulFreezeMode==1)                                 \r
253     memset(pF,0,sizeof(SPUFreeze_t)+sizeof(SPUOSSFreeze_t));\r
254 \r
255    strcpy(pF->szSPUName,"PBOSS");\r
256    pF->ulFreezeVersion=5;\r
257    pF->ulFreezeSize=sizeof(SPUFreeze_t)+sizeof(SPUOSSFreeze_t);\r
258 \r
259    if(ulFreezeMode==2) return 1;                       // info mode? ok, bye\r
260                                                        // save mode:\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
265 \r
266    if(spu.xapGlobal && spu.XAPlay!=spu.XAFeed)         // some xa\r
267     {\r
268      xa_left = spu.XAFeed - spu.XAPlay;\r
269      if (xa_left < 0)\r
270       xa_left = spu.XAEnd - spu.XAPlay + spu.XAFeed - spu.XAStart;\r
271      pF->xaS = *spu.xapGlobal;\r
272     }\r
273    else if (spu.CDDAPlay != spu.CDDAFeed)\r
274     {\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
278      if (cdda_left < 0)\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
284      else {\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
288      }\r
289      pF->xaS.nsamples = 0;\r
290     }\r
291    else\r
292     memset(&pF->xaS,0,sizeof(xa_decode_t));            // or clean xa\r
293 \r
294    pFO=(SPUOSSFreeze_t *)(pF+1);                       // store special stuff\r
295 \r
296    pFO->spuIrq = spu.regArea[(H_SPUirqAddr - 0x0c00) / 2];\r
297    if(spu.pSpuIrq) pFO->pSpuIrq = spu.pSpuIrq - spu.spuMemC;\r
298 \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
313 \r
314    for(i=0;i<MAXCHAN;i++)\r
315     {\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
321     }\r
322 \r
323    return 1;\r
324    //--------------------------------------------------//\r
325   }\r
326                                                        \r
327  if(ulFreezeMode!=0) return 0;                         // bad mode? bye\r
328 \r
329  memcpy(spu.spuMem,pF->cSPURam,0x80000);               // get ram\r
330  memcpy(spu.regArea,pF->cSPUPort,0x200);\r
331  spu.bMemDirty = 1;\r
332  spu.spuCtrl = regAreaGet(H_SPUctrl);\r
333  spu.spuStat = regAreaGet(H_SPUstat);\r
334 \r
335  if (!strcmp(pF->szSPUName,"PBOSS") && pF->ulFreezeVersion==5)\r
336    pFO = LoadStateV5(pF, cycles);\r
337  else LoadStateUnknown(pF, cycles);\r
338 \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
342   FeedXA(&pF->xaS);\r
343   spu.XAPlay = spu.XAFeed - pFO->xa_left;\r
344   if (spu.XAPlay < spu.XAStart)\r
345    spu.XAPlay = spu.XAStart;\r
346  }\r
347  else if (pFO && pFO->cdda_left) {                     // start cdda again\r
348   FeedCDDA((void *)pF->xaS.pcm, pFO->cdda_left * 4);\r
349  }\r
350 \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
356  spu.XARepeat = 0;\r
357  spu.XALastVal = 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
367  }\r
368 \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
377 \r
378  // reverb\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
384 \r
385  ClearWorkingState();\r
386 \r
387  if (spu.spuCtrl & CTRL_IRQ)\r
388   schedule_next_irq();\r
389 \r
390  return 1;\r
391 }\r
392 \r
393 ////////////////////////////////////////////////////////////////////////\r
394 \r
395 static SPUOSSFreeze_t * LoadStateV5(SPUFreeze_t * pF, uint32_t cycles)\r
396 {\r
397  int i;SPUOSSFreeze_t * pFO;\r
398 \r
399  pFO=(SPUOSSFreeze_t *)(pF+1);\r
400 \r
401  spu.pSpuIrq = spu.spuMemC + ((spu.regArea[(H_SPUirqAddr - 0x0c00) / 2] << 3) & ~0xf);\r
402 \r
403  if(pFO->spuAddr)\r
404   {\r
405    if (pFO->spuAddr == 0xbaadf00d) spu.spuAddr = 0;\r
406    else spu.spuAddr = pFO->spuAddr & 0x7fffe;\r
407   }\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
411 \r
412  spu.dwNewChannel=0;\r
413  spu.dwChannelsAudible=0;\r
414  spu.dwChannelDead=0;\r
415  for(i=0;i<MAXCHAN;i++)\r
416   {\r
417    load_channel(&spu.s_chan[i],&pFO->s_chan[i],i);\r
418 \r
419    spu.s_chan[i].pCurr+=(uintptr_t)spu.spuMemC;\r
420    spu.s_chan[i].pLoop+=(uintptr_t)spu.spuMemC;\r
421   }\r
422 \r
423  return pFO;\r
424 }\r
425 \r
426 ////////////////////////////////////////////////////////////////////////\r
427 \r
428 static void LoadStateUnknown(SPUFreeze_t * pF, uint32_t cycles)\r
429 {\r
430  int i;\r
431 \r
432  for(i=0;i<MAXCHAN;i++)\r
433   {\r
434    spu.s_chan[i].pLoop=spu.spuMemC;\r
435   }\r
436 \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
442 \r
443  for(i=0;i<0xc0;i++)\r
444   {\r
445    load_register(0x1f801c00 + i*2, cycles);\r
446   }\r
447 }\r
448 \r
449 ////////////////////////////////////////////////////////////////////////\r
450 // vim:shiftwidth=1:expandtab\r