Return path: Open a return path on QEMUFile for sockets
[qemu/ar7.git] / hw / audio / gus.c
blobe0c8a4ee1eed547cb72bc3efefb3986ce32ed6b4
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 "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"
31 #define dolog(...) AUD_log ("audio", __VA_ARGS__)
32 #ifdef DEBUG
33 #define ldebug(...) dolog (__VA_ARGS__)
34 #else
35 #define ldebug(...)
36 #endif
38 #ifdef HOST_WORDS_BIGENDIAN
39 #define GUS_ENDIANNESS 1
40 #else
41 #define GUS_ENDIANNESS 0
42 #endif
44 #define TYPE_GUS "gus"
45 #define GUS(obj) OBJECT_CHECK (GUSState, (obj), TYPE_GUS)
47 typedef struct GUSState {
48 ISADevice dev;
49 GUSEmuState emu;
50 QEMUSoundCard card;
51 uint32_t freq;
52 uint32_t port;
53 int pos, left, shift, irqs;
54 GUSsample *mixbuf;
55 uint8_t himem[1024 * 1024 + 32 + 4096];
56 int samples;
57 SWVoiceOut *voice;
58 int64_t last_ticks;
59 qemu_irq pic;
60 } GUSState;
62 static uint32_t gus_readb(void *opaque, uint32_t nport)
64 GUSState *s = opaque;
66 return gus_read (&s->emu, nport, 1);
69 static void gus_writeb(void *opaque, uint32_t nport, uint32_t val)
71 GUSState *s = opaque;
73 gus_write (&s->emu, nport, 1, val);
76 static int write_audio (GUSState *s, int samples)
78 int net = 0;
79 int pos = s->pos;
81 while (samples) {
82 int nbytes, wbytes, wsampl;
84 nbytes = samples << s->shift;
85 wbytes = AUD_write (
86 s->voice,
87 s->mixbuf + (pos << (s->shift - 1)),
88 nbytes
91 if (wbytes) {
92 wsampl = wbytes >> s->shift;
94 samples -= wsampl;
95 pos = (pos + wsampl) % s->samples;
97 net += wsampl;
99 else {
100 break;
104 return net;
107 static void GUS_callback (void *opaque, int free)
109 int samples, to_play, net = 0;
110 GUSState *s = opaque;
112 samples = free >> s->shift;
113 to_play = audio_MIN (samples, s->left);
115 while (to_play) {
116 int written = write_audio (s, to_play);
118 if (!written) {
119 goto reset;
122 s->left -= written;
123 to_play -= written;
124 samples -= written;
125 net += written;
128 samples = audio_MIN (samples, s->samples);
129 if (samples) {
130 gus_mixvoices (&s->emu, s->freq, samples, s->mixbuf);
132 while (samples) {
133 int written = write_audio (s, samples);
134 if (!written) {
135 break;
137 samples -= written;
138 net += written;
141 s->left = samples;
143 reset:
144 gus_irqgen (&s->emu, muldiv64 (net, 1000000, s->freq));
147 int GUS_irqrequest (GUSEmuState *emu, int hwirq, int n)
149 GUSState *s = emu->opaque;
150 /* qemu_irq_lower (s->pic); */
151 qemu_irq_raise (s->pic);
152 s->irqs += n;
153 ldebug ("irqrequest %d %d %d\n", hwirq, n, s->irqs);
154 return n;
157 void GUS_irqclear (GUSEmuState *emu, int hwirq)
159 GUSState *s = emu->opaque;
160 ldebug ("irqclear %d %d\n", hwirq, s->irqs);
161 qemu_irq_lower (s->pic);
162 s->irqs -= 1;
163 #ifdef IRQ_STORM
164 if (s->irqs > 0) {
165 qemu_irq_raise (s->pic[hwirq]);
167 #endif
170 void GUS_dmarequest (GUSEmuState *der)
172 /* GUSState *s = (GUSState *) der; */
173 ldebug ("dma request %d\n", der->gusdma);
174 DMA_hold_DREQ (der->gusdma);
177 static int GUS_read_DMA (void *opaque, int nchan, int dma_pos, int dma_len)
179 GUSState *s = opaque;
180 char tmpbuf[4096];
181 int pos = dma_pos, mode, left = dma_len - dma_pos;
183 ldebug ("read DMA %#x %d\n", dma_pos, dma_len);
184 mode = DMA_get_channel_mode (s->emu.gusdma);
185 while (left) {
186 int to_copy = audio_MIN ((size_t) left, sizeof (tmpbuf));
187 int copied;
189 ldebug ("left=%d to_copy=%d pos=%d\n", left, to_copy, pos);
190 copied = DMA_read_memory (nchan, tmpbuf, pos, to_copy);
191 gus_dma_transferdata (&s->emu, tmpbuf, copied, left == copied);
192 left -= copied;
193 pos += copied;
196 if (((mode >> 4) & 1) == 0) {
197 DMA_release_DREQ (s->emu.gusdma);
199 return dma_len;
202 static const VMStateDescription vmstate_gus = {
203 .name = "gus",
204 .version_id = 2,
205 .minimum_version_id = 2,
206 .fields = (VMStateField[]) {
207 VMSTATE_INT32 (pos, GUSState),
208 VMSTATE_INT32 (left, GUSState),
209 VMSTATE_INT32 (shift, GUSState),
210 VMSTATE_INT32 (irqs, GUSState),
211 VMSTATE_INT32 (samples, GUSState),
212 VMSTATE_INT64 (last_ticks, GUSState),
213 VMSTATE_BUFFER (himem, GUSState),
214 VMSTATE_END_OF_LIST ()
218 static const MemoryRegionPortio gus_portio_list1[] = {
219 {0x000, 1, 1, .write = gus_writeb },
220 {0x006, 10, 1, .read = gus_readb, .write = gus_writeb },
221 {0x100, 8, 1, .read = gus_readb, .write = gus_writeb },
222 PORTIO_END_OF_LIST (),
225 static const MemoryRegionPortio gus_portio_list2[] = {
226 {0, 2, 1, .read = gus_readb },
227 PORTIO_END_OF_LIST (),
230 static void gus_realizefn (DeviceState *dev, Error **errp)
232 ISADevice *d = ISA_DEVICE(dev);
233 GUSState *s = GUS (dev);
234 struct audsettings as;
236 AUD_register_card ("gus", &s->card);
238 as.freq = s->freq;
239 as.nchannels = 2;
240 as.fmt = AUD_FMT_S16;
241 as.endianness = GUS_ENDIANNESS;
243 s->voice = AUD_open_out (
244 &s->card,
245 NULL,
246 "gus",
248 GUS_callback,
252 if (!s->voice) {
253 AUD_remove_card (&s->card);
254 error_setg(errp, "No voice");
255 return;
258 s->shift = 2;
259 s->samples = AUD_get_buffer_size_out (s->voice) >> s->shift;
260 s->mixbuf = g_malloc0 (s->samples << s->shift);
262 isa_register_portio_list (d, s->port, gus_portio_list1, s, "gus");
263 isa_register_portio_list (d, (s->port + 0x100) & 0xf00,
264 gus_portio_list2, s, "gus");
266 DMA_register_channel (s->emu.gusdma, GUS_read_DMA, s);
267 s->emu.himemaddr = s->himem;
268 s->emu.gusdatapos = s->emu.himemaddr + 1024 * 1024 + 32;
269 s->emu.opaque = s;
270 isa_init_irq (d, &s->pic, s->emu.gusirq);
272 AUD_set_active_out (s->voice, 1);
275 static int GUS_init (ISABus *bus)
277 isa_create_simple (bus, TYPE_GUS);
278 return 0;
281 static Property gus_properties[] = {
282 DEFINE_PROP_UINT32 ("freq", GUSState, freq, 44100),
283 DEFINE_PROP_UINT32 ("iobase", GUSState, port, 0x240),
284 DEFINE_PROP_UINT32 ("irq", GUSState, emu.gusirq, 7),
285 DEFINE_PROP_UINT32 ("dma", GUSState, emu.gusdma, 3),
286 DEFINE_PROP_END_OF_LIST (),
289 static void gus_class_initfn (ObjectClass *klass, void *data)
291 DeviceClass *dc = DEVICE_CLASS (klass);
293 dc->realize = gus_realizefn;
294 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
295 dc->desc = "Gravis Ultrasound GF1";
296 dc->vmsd = &vmstate_gus;
297 dc->props = gus_properties;
300 static const TypeInfo gus_info = {
301 .name = TYPE_GUS,
302 .parent = TYPE_ISA_DEVICE,
303 .instance_size = sizeof (GUSState),
304 .class_init = gus_class_initfn,
307 static void gus_register_types (void)
309 type_register_static (&gus_info);
310 isa_register_soundhw("gus", "Gravis Ultrasound GF1", GUS_init);
313 type_init (gus_register_types)