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