gusemu_mixer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 "qemu/osdep.h"
  25. #include "gusemu.h"
  26. #include "gustate.h"
  27. #define GUSregb(position) (* (gusptr+(position)))
  28. #define GUSregw(position) (*(uint16_t *) (gusptr+(position)))
  29. #define GUSregd(position) (*(uint32_t *)(gusptr + (position)))
  30. #define GUSvoice(position) (*(uint16_t *)(voiceptr+(position)))
  31. /* samples are always 16bit stereo (4 bytes each, first right then left interleaved) */
  32. void gus_mixvoices(GUSEmuState * state, unsigned int playback_freq, unsigned int numsamples,
  33. int16_t *bufferpos)
  34. {
  35. /* note that byte registers are stored in the upper half of each voice register! */
  36. uint8_t *gusptr;
  37. int Voice;
  38. uint16_t *voiceptr;
  39. unsigned int count;
  40. for (count = 0; count < numsamples * 2; count++)
  41. *(bufferpos + count) = 0; /* clear */
  42. gusptr = state->gusdatapos;
  43. voiceptr = (uint16_t *) gusptr;
  44. if (!(GUSregb(GUS4cReset) & 0x01)) /* reset flag active? */
  45. return;
  46. for (Voice = 0; Voice <= (GUSregb(NumVoices) & 31); Voice++)
  47. {
  48. if (GUSvoice(wVSRControl) & 0x200)
  49. GUSvoice(wVSRControl) |= 0x100; /* voice stop request */
  50. if (GUSvoice(wVSRVolRampControl) & 0x200)
  51. GUSvoice(wVSRVolRampControl) |= 0x100; /* Volume ramp stop request */
  52. if (!(GUSvoice(wVSRControl) & GUSvoice(wVSRVolRampControl) & 0x100)) /* neither voice nor volume calculation active - save some time here ;) */
  53. {
  54. unsigned int sample;
  55. unsigned int LoopStart = (GUSvoice(wVSRLoopStartHi) << 16) | GUSvoice(wVSRLoopStartLo); /* 23.9 format */
  56. unsigned int LoopEnd = (GUSvoice(wVSRLoopEndHi) << 16) | GUSvoice(wVSRLoopEndLo); /* 23.9 format */
  57. unsigned int CurrPos = (GUSvoice(wVSRCurrPosHi) << 16) | GUSvoice(wVSRCurrPosLo); /* 23.9 format */
  58. int VoiceIncrement = ((((unsigned long) GUSvoice(wVSRFreq) * 44100) / playback_freq) * (14 >> 1)) /
  59. ((GUSregb(NumVoices) & 31) + 1); /* 6.10 increment/frame to 23.9 increment/sample */
  60. int PanningPos = (GUSvoice(wVSRPanning) >> 8) & 0xf;
  61. unsigned int Volume32 = 32 * GUSvoice(wVSRCurrVol); /* 32 times larger than original gus for maintaining precision while ramping */
  62. unsigned int StartVol32 = (GUSvoice(wVSRVolRampStartVol) & 0xff00) * 32;
  63. unsigned int EndVol32 = (GUSvoice(wVSRVolRampEndVol) & 0xff00) * 32;
  64. int VolumeIncrement32 = (32 * 16 * (GUSvoice(wVSRVolRampRate) & 0x3f00) >> 8) >> ((((GUSvoice(wVSRVolRampRate) & 0xc000) >> 8) >> 6) * 3); /* including 1/8/64/512 volume speed divisor */
  65. VolumeIncrement32 = (((VolumeIncrement32 * 44100 / 2) / playback_freq) * 14) / ((GUSregb(NumVoices) & 31) + 1); /* adjust ramping speed to playback speed */
  66. if (GUSvoice(wVSRControl) & 0x4000)
  67. VoiceIncrement = -VoiceIncrement; /* reverse playback */
  68. if (GUSvoice(wVSRVolRampControl) & 0x4000)
  69. VolumeIncrement32 = -VolumeIncrement32; /* reverse ramping */
  70. for (sample = 0; sample < numsamples; sample++)
  71. {
  72. int sample1, sample2, Volume;
  73. if (GUSvoice(wVSRControl) & 0x400) /* 16bit */
  74. {
  75. int offset = ((CurrPos >> 9) & 0xc0000) + (((CurrPos >> 9) & 0x1ffff) << 1);
  76. int8_t *adr;
  77. adr = (int8_t *) state->himemaddr + offset;
  78. sample1 = (*adr & 0xff) + (*(adr + 1) * 256);
  79. sample2 = (*(adr + 2) & 0xff) + (*(adr + 2 + 1) * 256);
  80. }
  81. else /* 8bit */
  82. {
  83. int offset = (CurrPos >> 9) & 0xfffff;
  84. int8_t *adr;
  85. adr = (int8_t *) state->himemaddr + offset;
  86. sample1 = (*adr) * 256;
  87. sample2 = (*(adr + 1)) * 256;
  88. }
  89. Volume = ((((Volume32 >> (4 + 5)) & 0xff) + 256) << (Volume32 >> ((4 + 8) + 5))) / 512; /* semi-logarithmic volume, +5 due to additional precision */
  90. sample1 = (((sample1 * Volume) >> 16) * (512 - (CurrPos % 512))) / 512;
  91. sample2 = (((sample2 * Volume) >> 16) * (CurrPos % 512)) / 512;
  92. sample1 += sample2;
  93. if (!(GUSvoice(wVSRVolRampControl) & 0x100))
  94. {
  95. Volume32 += VolumeIncrement32;
  96. if ((GUSvoice(wVSRVolRampControl) & 0x4000) ? (Volume32 <= StartVol32) : (Volume32 >= EndVol32)) /* ramp up boundary cross */
  97. {
  98. if (GUSvoice(wVSRVolRampControl) & 0x2000)
  99. GUSvoice(wVSRVolRampControl) |= 0x8000; /* volramp IRQ enabled? -> IRQ wait flag */
  100. if (GUSvoice(wVSRVolRampControl) & 0x800) /* loop enabled */
  101. {
  102. if (GUSvoice(wVSRVolRampControl) & 0x1000) /* bidir. loop */
  103. {
  104. GUSvoice(wVSRVolRampControl) ^= 0x4000; /* toggle dir */
  105. VolumeIncrement32 = -VolumeIncrement32;
  106. }
  107. else
  108. Volume32 = (GUSvoice(wVSRVolRampControl) & 0x4000) ? EndVol32 : StartVol32; /* unidir. loop ramp */
  109. }
  110. else
  111. {
  112. GUSvoice(wVSRVolRampControl) |= 0x100;
  113. Volume32 =
  114. (GUSvoice(wVSRVolRampControl) & 0x4000) ? StartVol32 : EndVol32;
  115. }
  116. }
  117. }
  118. if ((GUSvoice(wVSRVolRampControl) & 0xa000) == 0xa000) /* volramp IRQ set and enabled? */
  119. {
  120. GUSregd(voicevolrampirq) |= 1 << Voice; /* set irq slot */
  121. }
  122. else
  123. {
  124. GUSregd(voicevolrampirq) &= (~(1 << Voice)); /* clear irq slot */
  125. GUSvoice(wVSRVolRampControl) &= 0x7f00;
  126. }
  127. if (!(GUSvoice(wVSRControl) & 0x100))
  128. {
  129. CurrPos += VoiceIncrement;
  130. if ((GUSvoice(wVSRControl) & 0x4000) ? (CurrPos <= LoopStart) : (CurrPos >= LoopEnd)) /* playback boundary cross */
  131. {
  132. if (GUSvoice(wVSRControl) & 0x2000)
  133. GUSvoice(wVSRControl) |= 0x8000; /* voice IRQ enabled -> IRQ wait flag */
  134. if (GUSvoice(wVSRControl) & 0x800) /* loop enabled */
  135. {
  136. if (GUSvoice(wVSRControl) & 0x1000) /* pingpong loop */
  137. {
  138. GUSvoice(wVSRControl) ^= 0x4000; /* toggle dir */
  139. VoiceIncrement = -VoiceIncrement;
  140. }
  141. else
  142. CurrPos = (GUSvoice(wVSRControl) & 0x4000) ? LoopEnd : LoopStart; /* unidir. loop */
  143. }
  144. else if (!(GUSvoice(wVSRVolRampControl) & 0x400))
  145. GUSvoice(wVSRControl) |= 0x100; /* loop disabled, rollover check */
  146. }
  147. }
  148. if ((GUSvoice(wVSRControl) & 0xa000) == 0xa000) /* wavetable IRQ set and enabled? */
  149. {
  150. GUSregd(voicewavetableirq) |= 1 << Voice; /* set irq slot */
  151. }
  152. else
  153. {
  154. GUSregd(voicewavetableirq) &= (~(1 << Voice)); /* clear irq slot */
  155. GUSvoice(wVSRControl) &= 0x7f00;
  156. }
  157. /* mix samples into buffer */
  158. *(bufferpos + 2 * sample) += (int16_t) ((sample1 * PanningPos) >> 4); /* right */
  159. *(bufferpos + 2 * sample + 1) += (int16_t) ((sample1 * (15 - PanningPos)) >> 4); /* left */
  160. }
  161. /* write back voice and volume */
  162. GUSvoice(wVSRCurrVol) = Volume32 / 32;
  163. GUSvoice(wVSRCurrPosHi) = CurrPos >> 16;
  164. GUSvoice(wVSRCurrPosLo) = CurrPos & 0xffff;
  165. }
  166. voiceptr += 16; /* next voice */
  167. }
  168. }
  169. void gus_irqgen(GUSEmuState * state, unsigned int elapsed_time)
  170. /* time given in microseconds */
  171. {
  172. int requestedIRQs = 0;
  173. uint8_t *gusptr;
  174. gusptr = state->gusdatapos;
  175. if (GUSregb(TimerDataReg2x9) & 1) /* start timer 1 (80us decrement rate) */
  176. {
  177. unsigned int timer1fraction = state->timer1fraction;
  178. int newtimerirqs;
  179. newtimerirqs = (elapsed_time + timer1fraction) / (80 * (256 - GUSregb(GUS46Counter1)));
  180. state->timer1fraction = (elapsed_time + timer1fraction) % (80 * (256 - GUSregb(GUS46Counter1)));
  181. if (newtimerirqs)
  182. {
  183. if (!(GUSregb(TimerDataReg2x9) & 0x40))
  184. GUSregb(TimerStatus2x8) |= 0xc0; /* maskable bits */
  185. if (GUSregb(GUS45TimerCtrl) & 4) /* timer1 irq enable */
  186. {
  187. GUSregb(TimerStatus2x8) |= 4; /* nonmaskable bit */
  188. GUSregb(IRQStatReg2x6) |= 4; /* timer 1 irq pending */
  189. GUSregw(TimerIRQs) += newtimerirqs;
  190. requestedIRQs += newtimerirqs;
  191. }
  192. }
  193. }
  194. if (GUSregb(TimerDataReg2x9) & 2) /* start timer 2 (320us decrement rate) */
  195. {
  196. unsigned int timer2fraction = state->timer2fraction;
  197. int newtimerirqs;
  198. newtimerirqs = (elapsed_time + timer2fraction) / (320 * (256 - GUSregb(GUS47Counter2)));
  199. state->timer2fraction = (elapsed_time + timer2fraction) % (320 * (256 - GUSregb(GUS47Counter2)));
  200. if (newtimerirqs)
  201. {
  202. if (!(GUSregb(TimerDataReg2x9) & 0x20))
  203. GUSregb(TimerStatus2x8) |= 0xa0; /* maskable bits */
  204. if (GUSregb(GUS45TimerCtrl) & 8) /* timer2 irq enable */
  205. {
  206. GUSregb(TimerStatus2x8) |= 2; /* nonmaskable bit */
  207. GUSregb(IRQStatReg2x6) |= 8; /* timer 2 irq pending */
  208. GUSregw(TimerIRQs) += newtimerirqs;
  209. requestedIRQs += newtimerirqs;
  210. }
  211. }
  212. }
  213. if (GUSregb(GUS4cReset) & 0x4) /* synth IRQ enable */
  214. {
  215. if (GUSregd(voicewavetableirq))
  216. GUSregb(IRQStatReg2x6) |= 0x20;
  217. if (GUSregd(voicevolrampirq))
  218. GUSregb(IRQStatReg2x6) |= 0x40;
  219. }
  220. if ((!requestedIRQs) && GUSregb(IRQStatReg2x6))
  221. requestedIRQs++;
  222. if (GUSregb(IRQStatReg2x6))
  223. GUSregw(BusyTimerIRQs) = GUS_irqrequest(state, state->gusirq, requestedIRQs);
  224. }