net/colo-compare.c: Fix a crash in COLO Primary.
[qemu/ar7.git] / hw / audio / gus.c
blobb3e2a7fdd5044babfcd9bbed1b8eaddd2d05a470
1 /*
2 * QEMU Proxy for Gravis Ultrasound GF1 emulation by Tibor "TS" Schütz
4 * Copyright (c) 2002-2005 Vassili Karpov (malc)
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
22 * THE SOFTWARE.
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "hw/hw.h"
27 #include "hw/audio/soundhw.h"
28 #include "audio/audio.h"
29 #include "hw/isa/isa.h"
30 #include "gusemu.h"
31 #include "gustate.h"
33 #define dolog(...) AUD_log ("audio", __VA_ARGS__)
34 #ifdef DEBUG
35 #define ldebug(...) dolog (__VA_ARGS__)
36 #else
37 #define ldebug(...)
38 #endif
40 #ifdef HOST_WORDS_BIGENDIAN
41 #define GUS_ENDIANNESS 1
42 #else
43 #define GUS_ENDIANNESS 0
44 #endif
46 #define TYPE_GUS "gus"
47 #define GUS(obj) OBJECT_CHECK (GUSState, (obj), TYPE_GUS)
49 typedef struct GUSState {
50 ISADevice dev;
51 GUSEmuState emu;
52 QEMUSoundCard card;
53 uint32_t freq;
54 uint32_t port;
55 int pos, left, shift, irqs;
56 int16_t *mixbuf;
57 uint8_t himem[1024 * 1024 + 32 + 4096];
58 int samples;
59 SWVoiceOut *voice;
60 int64_t last_ticks;
61 qemu_irq pic;
62 IsaDma *isa_dma;
63 PortioList portio_list1;
64 PortioList portio_list2;
65 } GUSState;
67 static uint32_t gus_readb(void *opaque, uint32_t nport)
69 GUSState *s = opaque;
71 return gus_read (&s->emu, nport, 1);
74 static void gus_writeb(void *opaque, uint32_t nport, uint32_t val)
76 GUSState *s = opaque;
78 gus_write (&s->emu, nport, 1, val);
81 static int write_audio (GUSState *s, int samples)
83 int net = 0;
84 int pos = s->pos;
86 while (samples) {
87 int nbytes, wbytes, wsampl;
89 nbytes = samples << s->shift;
90 wbytes = AUD_write (
91 s->voice,
92 s->mixbuf + (pos << (s->shift - 1)),
93 nbytes
96 if (wbytes) {
97 wsampl = wbytes >> s->shift;
99 samples -= wsampl;
100 pos = (pos + wsampl) % s->samples;
102 net += wsampl;
104 else {
105 break;
109 return net;
112 static void GUS_callback (void *opaque, int free)
114 int samples, to_play, net = 0;
115 GUSState *s = opaque;
117 samples = free >> s->shift;
118 to_play = audio_MIN (samples, s->left);
120 while (to_play) {
121 int written = write_audio (s, to_play);
123 if (!written) {
124 goto reset;
127 s->left -= written;
128 to_play -= written;
129 samples -= written;
130 net += written;
133 samples = audio_MIN (samples, s->samples);
134 if (samples) {
135 gus_mixvoices (&s->emu, s->freq, samples, s->mixbuf);
137 while (samples) {
138 int written = write_audio (s, samples);
139 if (!written) {
140 break;
142 samples -= written;
143 net += written;
146 s->left = samples;
148 reset:
149 gus_irqgen (&s->emu, (uint64_t)net * 1000000 / s->freq);
152 int GUS_irqrequest (GUSEmuState *emu, int hwirq, int n)
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;
162 void GUS_irqclear (GUSEmuState *emu, int hwirq)
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]);
172 #endif
175 void GUS_dmarequest (GUSEmuState *emu)
177 GUSState *s = emu->opaque;
178 IsaDmaClass *k = ISADMA_GET_CLASS(s->isa_dma);
179 ldebug ("dma request %d\n", der->gusdma);
180 k->hold_DREQ(s->isa_dma, s->emu.gusdma);
183 static int GUS_read_DMA (void *opaque, int nchan, int dma_pos, int dma_len)
185 GUSState *s = opaque;
186 IsaDmaClass *k = ISADMA_GET_CLASS(s->isa_dma);
187 char tmpbuf[4096];
188 int pos = dma_pos, mode, left = dma_len - dma_pos;
190 ldebug ("read DMA %#x %d\n", dma_pos, dma_len);
191 mode = k->has_autoinitialization(s->isa_dma, s->emu.gusdma);
192 while (left) {
193 int to_copy = audio_MIN ((size_t) left, sizeof (tmpbuf));
194 int copied;
196 ldebug ("left=%d to_copy=%d pos=%d\n", left, to_copy, pos);
197 copied = k->read_memory(s->isa_dma, nchan, tmpbuf, pos, to_copy);
198 gus_dma_transferdata (&s->emu, tmpbuf, copied, left == copied);
199 left -= copied;
200 pos += copied;
203 if (((mode >> 4) & 1) == 0) {
204 k->release_DREQ(s->isa_dma, s->emu.gusdma);
206 return dma_len;
209 static const VMStateDescription vmstate_gus = {
210 .name = "gus",
211 .version_id = 2,
212 .minimum_version_id = 2,
213 .fields = (VMStateField[]) {
214 VMSTATE_INT32 (pos, GUSState),
215 VMSTATE_INT32 (left, GUSState),
216 VMSTATE_INT32 (shift, GUSState),
217 VMSTATE_INT32 (irqs, GUSState),
218 VMSTATE_INT32 (samples, GUSState),
219 VMSTATE_INT64 (last_ticks, GUSState),
220 VMSTATE_BUFFER (himem, GUSState),
221 VMSTATE_END_OF_LIST ()
225 static const MemoryRegionPortio gus_portio_list1[] = {
226 {0x000, 1, 1, .write = gus_writeb },
227 {0x006, 10, 1, .read = gus_readb, .write = gus_writeb },
228 {0x100, 8, 1, .read = gus_readb, .write = gus_writeb },
229 PORTIO_END_OF_LIST (),
232 static const MemoryRegionPortio gus_portio_list2[] = {
233 {0, 2, 1, .read = gus_readb },
234 PORTIO_END_OF_LIST (),
237 static void gus_realizefn (DeviceState *dev, Error **errp)
239 ISADevice *d = ISA_DEVICE(dev);
240 GUSState *s = GUS (dev);
241 IsaDmaClass *k;
242 struct audsettings as;
244 s->isa_dma = isa_get_dma(isa_bus_from_device(d), s->emu.gusdma);
245 if (!s->isa_dma) {
246 error_setg(errp, "ISA controller does not support DMA");
247 return;
250 AUD_register_card ("gus", &s->card);
252 as.freq = s->freq;
253 as.nchannels = 2;
254 as.fmt = AUDIO_FORMAT_S16;
255 as.endianness = GUS_ENDIANNESS;
257 s->voice = AUD_open_out (
258 &s->card,
259 NULL,
260 "gus",
262 GUS_callback,
266 if (!s->voice) {
267 AUD_remove_card (&s->card);
268 error_setg(errp, "No voice");
269 return;
272 s->shift = 2;
273 s->samples = AUD_get_buffer_size_out (s->voice) >> s->shift;
274 s->mixbuf = g_malloc0 (s->samples << s->shift);
276 isa_register_portio_list(d, &s->portio_list1, s->port,
277 gus_portio_list1, s, "gus");
278 isa_register_portio_list(d, &s->portio_list2, (s->port + 0x100) & 0xf00,
279 gus_portio_list2, s, "gus");
281 k = ISADMA_GET_CLASS(s->isa_dma);
282 k->register_channel(s->isa_dma, s->emu.gusdma, GUS_read_DMA, s);
283 s->emu.himemaddr = s->himem;
284 s->emu.gusdatapos = s->emu.himemaddr + 1024 * 1024 + 32;
285 s->emu.opaque = s;
286 isa_init_irq (d, &s->pic, s->emu.gusirq);
288 AUD_set_active_out (s->voice, 1);
291 static int GUS_init (ISABus *bus)
293 isa_create_simple (bus, TYPE_GUS);
294 return 0;
297 static Property gus_properties[] = {
298 DEFINE_PROP_UINT32 ("freq", GUSState, freq, 44100),
299 DEFINE_PROP_UINT32 ("iobase", GUSState, port, 0x240),
300 DEFINE_PROP_UINT32 ("irq", GUSState, emu.gusirq, 7),
301 DEFINE_PROP_UINT32 ("dma", GUSState, emu.gusdma, 3),
302 DEFINE_PROP_END_OF_LIST (),
305 static void gus_class_initfn (ObjectClass *klass, void *data)
307 DeviceClass *dc = DEVICE_CLASS (klass);
309 dc->realize = gus_realizefn;
310 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
311 dc->desc = "Gravis Ultrasound GF1";
312 dc->vmsd = &vmstate_gus;
313 dc->props = gus_properties;
316 static const TypeInfo gus_info = {
317 .name = TYPE_GUS,
318 .parent = TYPE_ISA_DEVICE,
319 .instance_size = sizeof (GUSState),
320 .class_init = gus_class_initfn,
323 static void gus_register_types (void)
325 type_register_static (&gus_info);
326 isa_register_soundhw("gus", "Gravis Ultrasound GF1", GUS_init);
329 type_init (gus_register_types)