Statistics
| Branch: | Revision:

root / hw / gusemu_mixer.c @ 78895427

History | View | Annotate | Download (11.9 kB)

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