gusemu_mixer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * GUSEMU32 - mixing engine (similar to Interwave GF1 compatibility)
  3. *
  4. * Copyright (C) 2000-2007 Tibor "TS" Schütz
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "gusemu.h"
  25. #include "gustate.h"
  26. #define GUSregb(position) (* (gusptr+(position)))
  27. #define GUSregw(position) (*(GUSword *) (gusptr+(position)))
  28. #define GUSregd(position) (*(GUSdword *)(gusptr+(position)))
  29. #define GUSvoice(position) (*(GUSword *)(voiceptr+(position)))
  30. /* samples are always 16bit stereo (4 bytes each, first right then left interleaved) */
  31. void gus_mixvoices(GUSEmuState * state, unsigned int playback_freq, unsigned int numsamples,
  32. GUSsample *bufferpos)
  33. {
  34. /* note that byte registers are stored in the upper half of each voice register! */
  35. GUSbyte *gusptr;
  36. int Voice;
  37. GUSword *voiceptr;
  38. unsigned int count;
  39. for (count = 0; count < numsamples * 2; count++)
  40. *(bufferpos + count) = 0; /* clear */
  41. gusptr = state->gusdatapos;
  42. voiceptr = (GUSword *) gusptr;
  43. if (!(GUSregb(GUS4cReset) & 0x01)) /* reset flag active? */
  44. return;
  45. for (Voice = 0; Voice <= (GUSregb(NumVoices) & 31); Voice++)
  46. {
  47. if (GUSvoice(wVSRControl) & 0x200)
  48. GUSvoice(wVSRControl) |= 0x100; /* voice stop request */
  49. if (GUSvoice(wVSRVolRampControl) & 0x200)
  50. GUSvoice(wVSRVolRampControl) |= 0x100; /* Volume ramp stop request */
  51. if (!(GUSvoice(wVSRControl) & GUSvoice(wVSRVolRampControl) & 0x100)) /* neither voice nor volume calculation active - save some time here ;) */
  52. {
  53. unsigned int sample;
  54. unsigned int LoopStart = (GUSvoice(wVSRLoopStartHi) << 16) | GUSvoice(wVSRLoopStartLo); /* 23.9 format */
  55. unsigned int LoopEnd = (GUSvoice(wVSRLoopEndHi) << 16) | GUSvoice(wVSRLoopEndLo); /* 23.9 format */
  56. unsigned int CurrPos = (GUSvoice(wVSRCurrPosHi) << 16) | GUSvoice(wVSRCurrPosLo); /* 23.9 format */
  57. int VoiceIncrement = ((((unsigned long) GUSvoice(wVSRFreq) * 44100) / playback_freq) * (14 >> 1)) /
  58. ((GUSregb(NumVoices) & 31) + 1); /* 6.10 increment/frame to 23.9 increment/sample */
  59. int PanningPos = (GUSvoice(wVSRPanning) >> 8) & 0xf;
  60. unsigned int Volume32 = 32 * GUSvoice(wVSRCurrVol); /* 32 times larger than original gus for maintaining precision while ramping */
  61. unsigned int StartVol32 = (GUSvoice(wVSRVolRampStartVol) & 0xff00) * 32;
  62. unsigned int EndVol32 = (GUSvoice(wVSRVolRampEndVol) & 0xff00) * 32;
  63. int VolumeIncrement32 = (32 * 16 * (GUSvoice(wVSRVolRampRate) & 0x3f00) >> 8) >> ((((GUSvoice(wVSRVolRampRate) & 0xc000) >> 8) >> 6) * 3); /* including 1/8/64/512 volume speed divisor */
  64. VolumeIncrement32 = (((VolumeIncrement32 * 44100 / 2) / playback_freq) * 14) / ((GUSregb(NumVoices) & 31) + 1); /* adjust ramping speed to playback speed */
  65. if (GUSvoice(wVSRControl) & 0x4000)
  66. VoiceIncrement = -VoiceIncrement; /* reverse playback */
  67. if (GUSvoice(wVSRVolRampControl) & 0x4000)
  68. VolumeIncrement32 = -VolumeIncrement32; /* reverse ramping */
  69. for (sample = 0; sample < numsamples; sample++)
  70. {
  71. int sample1, sample2, Volume;
  72. if (GUSvoice(wVSRControl) & 0x400) /* 16bit */
  73. {
  74. int offset = ((CurrPos >> 9) & 0xc0000) + (((CurrPos >> 9) & 0x1ffff) << 1);
  75. GUSchar *adr;
  76. adr = (GUSchar *) state->himemaddr + offset;
  77. sample1 = (*adr & 0xff) + (*(adr + 1) * 256);
  78. sample2 = (*(adr + 2) & 0xff) + (*(adr + 2 + 1) * 256);
  79. }
  80. else /* 8bit */
  81. {
  82. int offset = (CurrPos >> 9) & 0xfffff;
  83. GUSchar *adr;
  84. adr = (GUSchar *) state->himemaddr + offset;
  85. sample1 = (*adr) * 256;
  86. sample2 = (*(adr + 1)) * 256;
  87. }
  88. Volume = ((((Volume32 >> (4 + 5)) & 0xff) + 256) << (Volume32 >> ((4 + 8) + 5))) / 512; /* semi-logarithmic volume, +5 due to additional precision */
  89. sample1 = (((sample1 * Volume) >> 16) * (512 - (CurrPos % 512))) / 512;
  90. sample2 = (((sample2 * Volume) >> 16) * (CurrPos % 512)) / 512;
  91. sample1 += sample2;
  92. if (!(GUSvoice(wVSRVolRampControl) & 0x100))
  93. {
  94. Volume32 += VolumeIncrement32;
  95. if ((GUSvoice(wVSRVolRampControl) & 0x4000) ? (Volume32 <= StartVol32) : (Volume32 >= EndVol32)) /* ramp up boundary cross */
  96. {
  97. if (GUSvoice(wVSRVolRampControl) & 0x2000)
  98. GUSvoice(wVSRVolRampControl) |= 0x8000; /* volramp IRQ enabled? -> IRQ wait flag */
  99. if (GUSvoice(wVSRVolRampControl) & 0x800) /* loop enabled */
  100. {
  101. if (GUSvoice(wVSRVolRampControl) & 0x1000) /* bidir. loop */
  102. {
  103. GUSvoice(wVSRVolRampControl) ^= 0x4000; /* toggle dir */
  104. VolumeIncrement32 = -VolumeIncrement32;
  105. }
  106. else
  107. Volume32 = (GUSvoice(wVSRVolRampControl) & 0x4000) ? EndVol32 : StartVol32; /* unidir. loop ramp */
  108. }
  109. else
  110. {
  111. GUSvoice(wVSRVolRampControl) |= 0x100;
  112. Volume32 =
  113. (GUSvoice(wVSRVolRampControl) & 0x4000) ? StartVol32 : EndVol32;
  114. }
  115. }
  116. }
  117. if ((GUSvoice(wVSRVolRampControl) & 0xa000) == 0xa000) /* volramp IRQ set and enabled? */
  118. {
  119. GUSregd(voicevolrampirq) |= 1 << Voice; /* set irq slot */
  120. }
  121. else
  122. {
  123. GUSregd(voicevolrampirq) &= (~(1 << Voice)); /* clear irq slot */
  124. GUSvoice(wVSRVolRampControl) &= 0x7f00;
  125. }
  126. if (!(GUSvoice(wVSRControl) & 0x100))
  127. {
  128. CurrPos += VoiceIncrement;
  129. if ((GUSvoice(wVSRControl) & 0x4000) ? (CurrPos <= LoopStart) : (CurrPos >= LoopEnd)) /* playback boundary cross */
  130. {
  131. if (GUSvoice(wVSRControl) & 0x2000)
  132. GUSvoice(wVSRControl) |= 0x8000; /* voice IRQ enabled -> IRQ wait flag */
  133. if (GUSvoice(wVSRControl) & 0x800) /* loop enabled */
  134. {
  135. if (GUSvoice(wVSRControl) & 0x1000) /* pingpong loop */
  136. {
  137. GUSvoice(wVSRControl) ^= 0x4000; /* toggle dir */
  138. VoiceIncrement = -VoiceIncrement;
  139. }
  140. else
  141. CurrPos = (GUSvoice(wVSRControl) & 0x4000) ? LoopEnd : LoopStart; /* unidir. loop */
  142. }
  143. else if (!(GUSvoice(wVSRVolRampControl) & 0x400))
  144. GUSvoice(wVSRControl) |= 0x100; /* loop disabled, rollover check */
  145. }
  146. }
  147. if ((GUSvoice(wVSRControl) & 0xa000) == 0xa000) /* wavetable IRQ set and enabled? */
  148. {
  149. GUSregd(voicewavetableirq) |= 1 << Voice; /* set irq slot */
  150. }
  151. else
  152. {
  153. GUSregd(voicewavetableirq) &= (~(1 << Voice)); /* clear irq slot */
  154. GUSvoice(wVSRControl) &= 0x7f00;
  155. }
  156. /* mix samples into buffer */
  157. *(bufferpos + 2 * sample) += (GUSsample) ((sample1 * PanningPos) >> 4); /* right */
  158. *(bufferpos + 2 * sample + 1) += (GUSsample) ((sample1 * (15 - PanningPos)) >> 4); /* left */
  159. }
  160. /* write back voice and volume */
  161. GUSvoice(wVSRCurrVol) = Volume32 / 32;
  162. GUSvoice(wVSRCurrPosHi) = CurrPos >> 16;
  163. GUSvoice(wVSRCurrPosLo) = CurrPos & 0xffff;
  164. }
  165. voiceptr += 16; /* next voice */
  166. }
  167. }
  168. void gus_irqgen(GUSEmuState * state, unsigned int elapsed_time)
  169. /* time given in microseconds */
  170. {
  171. int requestedIRQs = 0;
  172. GUSbyte *gusptr;
  173. gusptr = state->gusdatapos;
  174. if (GUSregb(TimerDataReg2x9) & 1) /* start timer 1 (80us decrement rate) */
  175. {
  176. unsigned int timer1fraction = state->timer1fraction;
  177. int newtimerirqs;
  178. newtimerirqs = (elapsed_time + timer1fraction) / (80 * (256 - GUSregb(GUS46Counter1)));
  179. state->timer1fraction = (elapsed_time + timer1fraction) % (80 * (256 - GUSregb(GUS46Counter1)));
  180. if (newtimerirqs)
  181. {
  182. if (!(GUSregb(TimerDataReg2x9) & 0x40))
  183. GUSregb(TimerStatus2x8) |= 0xc0; /* maskable bits */
  184. if (GUSregb(GUS45TimerCtrl) & 4) /* timer1 irq enable */
  185. {
  186. GUSregb(TimerStatus2x8) |= 4; /* nonmaskable bit */
  187. GUSregb(IRQStatReg2x6) |= 4; /* timer 1 irq pending */
  188. GUSregw(TimerIRQs) += newtimerirqs;
  189. requestedIRQs += newtimerirqs;
  190. }
  191. }
  192. }
  193. if (GUSregb(TimerDataReg2x9) & 2) /* start timer 2 (320us decrement rate) */
  194. {
  195. unsigned int timer2fraction = state->timer2fraction;
  196. int newtimerirqs;
  197. newtimerirqs = (elapsed_time + timer2fraction) / (320 * (256 - GUSregb(GUS47Counter2)));
  198. state->timer2fraction = (elapsed_time + timer2fraction) % (320 * (256 - GUSregb(GUS47Counter2)));
  199. if (newtimerirqs)
  200. {
  201. if (!(GUSregb(TimerDataReg2x9) & 0x20))
  202. GUSregb(TimerStatus2x8) |= 0xa0; /* maskable bits */
  203. if (GUSregb(GUS45TimerCtrl) & 8) /* timer2 irq enable */
  204. {
  205. GUSregb(TimerStatus2x8) |= 2; /* nonmaskable bit */
  206. GUSregb(IRQStatReg2x6) |= 8; /* timer 2 irq pending */
  207. GUSregw(TimerIRQs) += newtimerirqs;
  208. requestedIRQs += newtimerirqs;
  209. }
  210. }
  211. }
  212. if (GUSregb(GUS4cReset) & 0x4) /* synth IRQ enable */
  213. {
  214. if (GUSregd(voicewavetableirq))
  215. GUSregb(IRQStatReg2x6) |= 0x20;
  216. if (GUSregd(voicevolrampirq))
  217. GUSregb(IRQStatReg2x6) |= 0x40;
  218. }
  219. if ((!requestedIRQs) && GUSregb(IRQStatReg2x6))
  220. requestedIRQs++;
  221. if (GUSregb(IRQStatReg2x6))
  222. GUSregw(BusyTimerIRQs) = GUS_irqrequest(state, state->gusirq, requestedIRQs);
  223. }