2 * GUSEMU32 - mixing engine (similar to Interwave GF1 compatibility)
4 * Copyright (C) 2000-2007 Tibor "TS" Schütz
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
28 #define GUSregb(position) (* (gusptr+(position)))
29 #define GUSregw(position) (*(GUSword *) (gusptr+(position)))
30 #define GUSregd(position) (*(GUSdword *)(gusptr+(position)))
32 #define GUSvoice(position) (*(GUSword *)(voiceptr+(position)))
34 /* samples are always 16bit stereo (4 bytes each, first right then left interleaved) */
35 void gus_mixvoices(GUSEmuState
* state
, unsigned int playback_freq
, unsigned int numsamples
,
38 /* note that byte registers are stored in the upper half of each voice register! */
44 for (count
= 0; count
< numsamples
* 2; count
++)
45 *(bufferpos
+ count
) = 0; /* clear */
47 gusptr
= state
->gusdatapos
;
48 voiceptr
= (GUSword
*) gusptr
;
49 if (!(GUSregb(GUS4cReset
) & 0x01)) /* reset flag active? */
52 for (Voice
= 0; Voice
<= (GUSregb(NumVoices
) & 31); Voice
++)
54 if (GUSvoice(wVSRControl
) & 0x200)
55 GUSvoice(wVSRControl
) |= 0x100; /* voice stop request */
56 if (GUSvoice(wVSRVolRampControl
) & 0x200)
57 GUSvoice(wVSRVolRampControl
) |= 0x100; /* Volume ramp stop request */
58 if (!(GUSvoice(wVSRControl
) & GUSvoice(wVSRVolRampControl
) & 0x100)) /* neither voice nor volume calculation active - save some time here ;) */
62 unsigned int LoopStart
= (GUSvoice(wVSRLoopStartHi
) << 16) | GUSvoice(wVSRLoopStartLo
); /* 23.9 format */
63 unsigned int LoopEnd
= (GUSvoice(wVSRLoopEndHi
) << 16) | GUSvoice(wVSRLoopEndLo
); /* 23.9 format */
64 unsigned int CurrPos
= (GUSvoice(wVSRCurrPosHi
) << 16) | GUSvoice(wVSRCurrPosLo
); /* 23.9 format */
65 int VoiceIncrement
= ((((unsigned long) GUSvoice(wVSRFreq
) * 44100) / playback_freq
) * (14 >> 1)) /
66 ((GUSregb(NumVoices
) & 31) + 1); /* 6.10 increment/frame to 23.9 increment/sample */
68 int PanningPos
= (GUSvoice(wVSRPanning
) >> 8) & 0xf;
70 unsigned int Volume32
= 32 * GUSvoice(wVSRCurrVol
); /* 32 times larger than original gus for maintaining precision while ramping */
71 unsigned int StartVol32
= (GUSvoice(wVSRVolRampStartVol
) & 0xff00) * 32;
72 unsigned int EndVol32
= (GUSvoice(wVSRVolRampEndVol
) & 0xff00) * 32;
73 int VolumeIncrement32
= (32 * 16 * (GUSvoice(wVSRVolRampRate
) & 0x3f00) >> 8) >> ((((GUSvoice(wVSRVolRampRate
) & 0xc000) >> 8) >> 6) * 3); /* including 1/8/64/512 volume speed divisor */
74 VolumeIncrement32
= (((VolumeIncrement32
* 44100 / 2) / playback_freq
) * 14) / ((GUSregb(NumVoices
) & 31) + 1); /* adjust ramping speed to playback speed */
76 if (GUSvoice(wVSRControl
) & 0x4000)
77 VoiceIncrement
= -VoiceIncrement
; /* reverse playback */
78 if (GUSvoice(wVSRVolRampControl
) & 0x4000)
79 VolumeIncrement32
= -VolumeIncrement32
; /* reverse ramping */
81 for (sample
= 0; sample
< numsamples
; sample
++)
83 int sample1
, sample2
, Volume
;
84 if (GUSvoice(wVSRControl
) & 0x400) /* 16bit */
86 int offset
= ((CurrPos
>> 9) & 0xc0000) + (((CurrPos
>> 9) & 0x1ffff) << 1);
88 adr
= (GUSchar
*) state
->himemaddr
+ offset
;
89 sample1
= (*adr
& 0xff) + (*(adr
+ 1) * 256);
90 sample2
= (*(adr
+ 2) & 0xff) + (*(adr
+ 2 + 1) * 256);
94 int offset
= (CurrPos
>> 9) & 0xfffff;
96 adr
= (GUSchar
*) state
->himemaddr
+ offset
;
97 sample1
= (*adr
) * 256;
98 sample2
= (*(adr
+ 1)) * 256;
101 Volume
= ((((Volume32
>> (4 + 5)) & 0xff) + 256) << (Volume32
>> ((4 + 8) + 5))) / 512; /* semi-logarithmic volume, +5 due to additional precision */
102 sample1
= (((sample1
* Volume
) >> 16) * (512 - (CurrPos
% 512))) / 512;
103 sample2
= (((sample2
* Volume
) >> 16) * (CurrPos
% 512)) / 512;
106 if (!(GUSvoice(wVSRVolRampControl
) & 0x100))
108 Volume32
+= VolumeIncrement32
;
109 if ((GUSvoice(wVSRVolRampControl
) & 0x4000) ? (Volume32
<= StartVol32
) : (Volume32
>= EndVol32
)) /* ramp up boundary cross */
111 if (GUSvoice(wVSRVolRampControl
) & 0x2000)
112 GUSvoice(wVSRVolRampControl
) |= 0x8000; /* volramp IRQ enabled? -> IRQ wait flag */
113 if (GUSvoice(wVSRVolRampControl
) & 0x800) /* loop enabled */
115 if (GUSvoice(wVSRVolRampControl
) & 0x1000) /* bidir. loop */
117 GUSvoice(wVSRVolRampControl
) ^= 0x4000; /* toggle dir */
118 VolumeIncrement32
= -VolumeIncrement32
;
121 Volume32
= (GUSvoice(wVSRVolRampControl
) & 0x4000) ? EndVol32
: StartVol32
; /* unidir. loop ramp */
125 GUSvoice(wVSRVolRampControl
) |= 0x100;
127 (GUSvoice(wVSRVolRampControl
) & 0x4000) ? StartVol32
: EndVol32
;
131 if ((GUSvoice(wVSRVolRampControl
) & 0xa000) == 0xa000) /* volramp IRQ set and enabled? */
133 GUSregd(voicevolrampirq
) |= 1 << Voice
; /* set irq slot */
137 GUSregd(voicevolrampirq
) &= (~(1 << Voice
)); /* clear irq slot */
138 GUSvoice(wVSRVolRampControl
) &= 0x7f00;
141 if (!(GUSvoice(wVSRControl
) & 0x100))
143 CurrPos
+= VoiceIncrement
;
144 if ((GUSvoice(wVSRControl
) & 0x4000) ? (CurrPos
<= LoopStart
) : (CurrPos
>= LoopEnd
)) /* playback boundary cross */
146 if (GUSvoice(wVSRControl
) & 0x2000)
147 GUSvoice(wVSRControl
) |= 0x8000; /* voice IRQ enabled -> IRQ wait flag */
148 if (GUSvoice(wVSRControl
) & 0x800) /* loop enabled */
150 if (GUSvoice(wVSRControl
) & 0x1000) /* pingpong loop */
152 GUSvoice(wVSRControl
) ^= 0x4000; /* toggle dir */
153 VoiceIncrement
= -VoiceIncrement
;
156 CurrPos
= (GUSvoice(wVSRControl
) & 0x4000) ? LoopEnd
: LoopStart
; /* unidir. loop */
158 else if (!(GUSvoice(wVSRVolRampControl
) & 0x400))
159 GUSvoice(wVSRControl
) |= 0x100; /* loop disabled, rollover check */
162 if ((GUSvoice(wVSRControl
) & 0xa000) == 0xa000) /* wavetable IRQ set and enabled? */
164 GUSregd(voicewavetableirq
) |= 1 << Voice
; /* set irq slot */
168 GUSregd(voicewavetableirq
) &= (~(1 << Voice
)); /* clear irq slot */
169 GUSvoice(wVSRControl
) &= 0x7f00;
172 /* mix samples into buffer */
173 *(bufferpos
+ 2 * sample
) += (short) ((sample1
* PanningPos
) >> 4); /* right */
174 *(bufferpos
+ 2 * sample
+ 1) += (short) ((sample1
* (15 - PanningPos
)) >> 4); /* left */
176 /* write back voice and volume */
177 GUSvoice(wVSRCurrVol
) = Volume32
/ 32;
178 GUSvoice(wVSRCurrPosHi
) = CurrPos
>> 16;
179 GUSvoice(wVSRCurrPosLo
) = CurrPos
& 0xffff;
181 voiceptr
+= 16; /* next voice */
185 void gus_irqgen(GUSEmuState
* state
, unsigned int elapsed_time
)
186 /* time given in microseconds */
188 int requestedIRQs
= 0;
190 gusptr
= state
->gusdatapos
;
191 if (GUSregb(TimerDataReg2x9
) & 1) /* start timer 1 (80us decrement rate) */
193 unsigned int timer1fraction
= state
->timer1fraction
;
195 newtimerirqs
= (elapsed_time
+ timer1fraction
) / (80 * (256 - GUSregb(GUS46Counter1
)));
196 state
->timer1fraction
= (elapsed_time
+ timer1fraction
) % (80 * (256 - GUSregb(GUS46Counter1
)));
199 if (!(GUSregb(TimerDataReg2x9
) & 0x40))
200 GUSregb(TimerStatus2x8
) |= 0xc0; /* maskable bits */
201 if (GUSregb(GUS45TimerCtrl
) & 4) /* timer1 irq enable */
203 GUSregb(TimerStatus2x8
) |= 4; /* nonmaskable bit */
204 GUSregb(IRQStatReg2x6
) |= 4; /* timer 1 irq pending */
205 GUSregw(TimerIRQs
) += newtimerirqs
;
206 requestedIRQs
+= newtimerirqs
;
210 if (GUSregb(TimerDataReg2x9
) & 2) /* start timer 2 (320us decrement rate) */
212 unsigned int timer2fraction
= state
->timer2fraction
;
214 newtimerirqs
= (elapsed_time
+ timer2fraction
) / (320 * (256 - GUSregb(GUS47Counter2
)));
215 state
->timer2fraction
= (elapsed_time
+ timer2fraction
) % (320 * (256 - GUSregb(GUS47Counter2
)));
218 if (!(GUSregb(TimerDataReg2x9
) & 0x20))
219 GUSregb(TimerStatus2x8
) |= 0xa0; /* maskable bits */
220 if (GUSregb(GUS45TimerCtrl
) & 8) /* timer2 irq enable */
222 GUSregb(TimerStatus2x8
) |= 2; /* nonmaskable bit */
223 GUSregb(IRQStatReg2x6
) |= 8; /* timer 2 irq pending */
224 GUSregw(TimerIRQs
) += newtimerirqs
;
225 requestedIRQs
+= newtimerirqs
;
229 if (GUSregb(GUS4cReset
) & 0x4) /* synth IRQ enable */
231 if (GUSregd(voicewavetableirq
))
232 GUSregb(IRQStatReg2x6
) |= 0x20;
233 if (GUSregd(voicevolrampirq
))
234 GUSregb(IRQStatReg2x6
) |= 0x40;
236 if ((!requestedIRQs
) && GUSregb(IRQStatReg2x6
))
238 if (GUSregb(IRQStatReg2x6
))
239 GUSregw(BusyTimerIRQs
) = GUS_irqrequest(state
, state
->gusirq
, requestedIRQs
);