xref: /qemu/hw/audio/gus.c (revision 7a4e543d)
1 /*
2  * QEMU Proxy for Gravis Ultrasound GF1 emulation by Tibor "TS" Schütz
3  *
4  * Copyright (c) 2002-2005 Vassili Karpov (malc)
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 "hw/hw.h"
26 #include "hw/audio/audio.h"
27 #include "audio/audio.h"
28 #include "hw/isa/isa.h"
29 #include "gusemu.h"
30 #include "gustate.h"
31 
32 #define dolog(...) AUD_log ("audio", __VA_ARGS__)
33 #ifdef DEBUG
34 #define ldebug(...) dolog (__VA_ARGS__)
35 #else
36 #define ldebug(...)
37 #endif
38 
39 #ifdef HOST_WORDS_BIGENDIAN
40 #define GUS_ENDIANNESS 1
41 #else
42 #define GUS_ENDIANNESS 0
43 #endif
44 
45 #define TYPE_GUS "gus"
46 #define GUS(obj) OBJECT_CHECK (GUSState, (obj), TYPE_GUS)
47 
48 typedef struct GUSState {
49     ISADevice dev;
50     GUSEmuState emu;
51     QEMUSoundCard card;
52     uint32_t freq;
53     uint32_t port;
54     int pos, left, shift, irqs;
55     GUSsample *mixbuf;
56     uint8_t himem[1024 * 1024 + 32 + 4096];
57     int samples;
58     SWVoiceOut *voice;
59     int64_t last_ticks;
60     qemu_irq pic;
61     IsaDma *isa_dma;
62 } GUSState;
63 
64 static uint32_t gus_readb(void *opaque, uint32_t nport)
65 {
66     GUSState *s = opaque;
67 
68     return gus_read (&s->emu, nport, 1);
69 }
70 
71 static void gus_writeb(void *opaque, uint32_t nport, uint32_t val)
72 {
73     GUSState *s = opaque;
74 
75     gus_write (&s->emu, nport, 1, val);
76 }
77 
78 static int write_audio (GUSState *s, int samples)
79 {
80     int net = 0;
81     int pos = s->pos;
82 
83     while (samples) {
84         int nbytes, wbytes, wsampl;
85 
86         nbytes = samples << s->shift;
87         wbytes = AUD_write (
88             s->voice,
89             s->mixbuf + (pos << (s->shift - 1)),
90             nbytes
91             );
92 
93         if (wbytes) {
94             wsampl = wbytes >> s->shift;
95 
96             samples -= wsampl;
97             pos = (pos + wsampl) % s->samples;
98 
99             net += wsampl;
100         }
101         else {
102             break;
103         }
104     }
105 
106     return net;
107 }
108 
109 static void GUS_callback (void *opaque, int free)
110 {
111     int samples, to_play, net = 0;
112     GUSState *s = opaque;
113 
114     samples = free >> s->shift;
115     to_play = audio_MIN (samples, s->left);
116 
117     while (to_play) {
118         int written = write_audio (s, to_play);
119 
120         if (!written) {
121             goto reset;
122         }
123 
124         s->left -= written;
125         to_play -= written;
126         samples -= written;
127         net += written;
128     }
129 
130     samples = audio_MIN (samples, s->samples);
131     if (samples) {
132         gus_mixvoices (&s->emu, s->freq, samples, s->mixbuf);
133 
134         while (samples) {
135             int written = write_audio (s, samples);
136             if (!written) {
137                 break;
138             }
139             samples -= written;
140             net += written;
141         }
142     }
143     s->left = samples;
144 
145  reset:
146     gus_irqgen (&s->emu, muldiv64 (net, 1000000, s->freq));
147 }
148 
149 int GUS_irqrequest (GUSEmuState *emu, int hwirq, int n)
150 {
151     GUSState *s = emu->opaque;
152     /* qemu_irq_lower (s->pic); */
153     qemu_irq_raise (s->pic);
154     s->irqs += n;
155     ldebug ("irqrequest %d %d %d\n", hwirq, n, s->irqs);
156     return n;
157 }
158 
159 void GUS_irqclear (GUSEmuState *emu, int hwirq)
160 {
161     GUSState *s = emu->opaque;
162     ldebug ("irqclear %d %d\n", hwirq, s->irqs);
163     qemu_irq_lower (s->pic);
164     s->irqs -= 1;
165 #ifdef IRQ_STORM
166     if (s->irqs > 0) {
167         qemu_irq_raise (s->pic[hwirq]);
168     }
169 #endif
170 }
171 
172 void GUS_dmarequest (GUSEmuState *emu)
173 {
174     GUSState *s = emu->opaque;
175     IsaDmaClass *k = ISADMA_GET_CLASS(s->isa_dma);
176     ldebug ("dma request %d\n", der->gusdma);
177     k->hold_DREQ(s->isa_dma, s->emu.gusdma);
178 }
179 
180 static int GUS_read_DMA (void *opaque, int nchan, int dma_pos, int dma_len)
181 {
182     GUSState *s = opaque;
183     IsaDmaClass *k = ISADMA_GET_CLASS(s->isa_dma);
184     char tmpbuf[4096];
185     int pos = dma_pos, mode, left = dma_len - dma_pos;
186 
187     ldebug ("read DMA %#x %d\n", dma_pos, dma_len);
188     mode = k->has_autoinitialization(s->isa_dma, s->emu.gusdma);
189     while (left) {
190         int to_copy = audio_MIN ((size_t) left, sizeof (tmpbuf));
191         int copied;
192 
193         ldebug ("left=%d to_copy=%d pos=%d\n", left, to_copy, pos);
194         copied = k->read_memory(s->isa_dma, nchan, tmpbuf, pos, to_copy);
195         gus_dma_transferdata (&s->emu, tmpbuf, copied, left == copied);
196         left -= copied;
197         pos += copied;
198     }
199 
200     if (((mode >> 4) & 1) == 0) {
201         k->release_DREQ(s->isa_dma, s->emu.gusdma);
202     }
203     return dma_len;
204 }
205 
206 static const VMStateDescription vmstate_gus = {
207     .name = "gus",
208     .version_id = 2,
209     .minimum_version_id = 2,
210     .fields = (VMStateField[]) {
211         VMSTATE_INT32 (pos, GUSState),
212         VMSTATE_INT32 (left, GUSState),
213         VMSTATE_INT32 (shift, GUSState),
214         VMSTATE_INT32 (irqs, GUSState),
215         VMSTATE_INT32 (samples, GUSState),
216         VMSTATE_INT64 (last_ticks, GUSState),
217         VMSTATE_BUFFER (himem, GUSState),
218         VMSTATE_END_OF_LIST ()
219     }
220 };
221 
222 static const MemoryRegionPortio gus_portio_list1[] = {
223     {0x000,  1, 1, .write = gus_writeb },
224     {0x006, 10, 1, .read = gus_readb, .write = gus_writeb },
225     {0x100,  8, 1, .read = gus_readb, .write = gus_writeb },
226     PORTIO_END_OF_LIST (),
227 };
228 
229 static const MemoryRegionPortio gus_portio_list2[] = {
230     {0, 2, 1, .read = gus_readb },
231     PORTIO_END_OF_LIST (),
232 };
233 
234 static void gus_realizefn (DeviceState *dev, Error **errp)
235 {
236     ISADevice *d = ISA_DEVICE(dev);
237     GUSState *s = GUS (dev);
238     IsaDmaClass *k;
239     struct audsettings as;
240 
241     AUD_register_card ("gus", &s->card);
242 
243     as.freq = s->freq;
244     as.nchannels = 2;
245     as.fmt = AUD_FMT_S16;
246     as.endianness = GUS_ENDIANNESS;
247 
248     s->voice = AUD_open_out (
249         &s->card,
250         NULL,
251         "gus",
252         s,
253         GUS_callback,
254         &as
255         );
256 
257     if (!s->voice) {
258         AUD_remove_card (&s->card);
259         error_setg(errp, "No voice");
260         return;
261     }
262 
263     s->shift = 2;
264     s->samples = AUD_get_buffer_size_out (s->voice) >> s->shift;
265     s->mixbuf = g_malloc0 (s->samples << s->shift);
266 
267     isa_register_portio_list (d, s->port, gus_portio_list1, s, "gus");
268     isa_register_portio_list (d, (s->port + 0x100) & 0xf00,
269                               gus_portio_list2, s, "gus");
270 
271     s->isa_dma = isa_get_dma(isa_bus_from_device(d), s->emu.gusdma);
272     k = ISADMA_GET_CLASS(s->isa_dma);
273     k->register_channel(s->isa_dma, s->emu.gusdma, GUS_read_DMA, s);
274     s->emu.himemaddr = s->himem;
275     s->emu.gusdatapos = s->emu.himemaddr + 1024 * 1024 + 32;
276     s->emu.opaque = s;
277     isa_init_irq (d, &s->pic, s->emu.gusirq);
278 
279     AUD_set_active_out (s->voice, 1);
280 }
281 
282 static int GUS_init (ISABus *bus)
283 {
284     isa_create_simple (bus, TYPE_GUS);
285     return 0;
286 }
287 
288 static Property gus_properties[] = {
289     DEFINE_PROP_UINT32 ("freq",    GUSState, freq,        44100),
290     DEFINE_PROP_UINT32 ("iobase",  GUSState, port,        0x240),
291     DEFINE_PROP_UINT32 ("irq",     GUSState, emu.gusirq,  7),
292     DEFINE_PROP_UINT32 ("dma",     GUSState, emu.gusdma,  3),
293     DEFINE_PROP_END_OF_LIST (),
294 };
295 
296 static void gus_class_initfn (ObjectClass *klass, void *data)
297 {
298     DeviceClass *dc = DEVICE_CLASS (klass);
299 
300     dc->realize = gus_realizefn;
301     set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
302     dc->desc = "Gravis Ultrasound GF1";
303     dc->vmsd = &vmstate_gus;
304     dc->props = gus_properties;
305 }
306 
307 static const TypeInfo gus_info = {
308     .name          = TYPE_GUS,
309     .parent        = TYPE_ISA_DEVICE,
310     .instance_size = sizeof (GUSState),
311     .class_init    = gus_class_initfn,
312 };
313 
314 static void gus_register_types (void)
315 {
316     type_register_static (&gus_info);
317     isa_register_soundhw("gus", "Gravis Ultrasound GF1", GUS_init);
318 }
319 
320 type_init (gus_register_types)
321