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