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
26 #include "audio/audio.h"
31 #define dolog(...) AUD_log ("audio", __VA_ARGS__)
33 #define ldebug(...) dolog (__VA_ARGS__)
38 #ifdef HOST_WORDS_BIGENDIAN
39 #define GUS_ENDIANNESS 1
41 #define GUS_ENDIANNESS 0
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)
49 typedef struct GUSState {
55 int pos, left, shift, irqs;
57 uint8_t himem[1024 * 1024 + 32 + 4096];
64 IO_READ_PROTO (gus_readb)
68 return gus_read (&s->emu, nport, 1);
71 IO_READ_PROTO (gus_readw)
75 return gus_read (&s->emu, nport, 2);
78 IO_WRITE_PROTO (gus_writeb)
82 gus_write (&s->emu, nport, 1, val);
85 IO_WRITE_PROTO (gus_writew)
89 gus_write (&s->emu, nport, 2, val);
92 static int write_audio (GUSState *s, int samples)
98 int nbytes, wbytes, wsampl;
100 nbytes = samples << s->shift;
103 s->mixbuf + (pos << (s->shift - 1)),
108 wsampl = wbytes >> s->shift;
111 pos = (pos + wsampl) % s->samples;
123 static void GUS_callback (void *opaque, int free)
125 int samples, to_play, net = 0;
126 GUSState *s = opaque;
128 samples = free >> s->shift;
129 to_play = audio_MIN (samples, s->left);
132 int written = write_audio (s, to_play);
144 samples = audio_MIN (samples, s->samples);
146 gus_mixvoices (&s->emu, s->freq, samples, s->mixbuf);
149 int written = write_audio (s, samples);
160 gus_irqgen (&s->emu, muldiv64 (net, 1000000, s->freq));
163 int GUS_irqrequest (GUSEmuState *emu, int hwirq, int n)
165 GUSState *s = emu->opaque;
166 /* qemu_irq_lower (s->pic); */
167 qemu_irq_raise (s->pic);
169 ldebug ("irqrequest %d %d %d\n", hwirq, n, s->irqs);
173 void GUS_irqclear (GUSEmuState *emu, int hwirq)
175 GUSState *s = emu->opaque;
176 ldebug ("irqclear %d %d\n", hwirq, s->irqs);
177 qemu_irq_lower (s->pic);
181 qemu_irq_raise (s->pic[hwirq]);
186 void GUS_dmarequest (GUSEmuState *der)
188 /* GUSState *s = (GUSState *) der; */
189 ldebug ("dma request %d\n", der->gusdma);
190 DMA_hold_DREQ (der->gusdma);
193 static int GUS_read_DMA (void *opaque, int nchan, int dma_pos, int dma_len)
195 GUSState *s = opaque;
197 int pos = dma_pos, mode, left = dma_len - dma_pos;
199 ldebug ("read DMA %#x %d\n", dma_pos, dma_len);
200 mode = DMA_get_channel_mode (s->emu.gusdma);
202 int to_copy = audio_MIN ((size_t) left, sizeof (tmpbuf));
205 ldebug ("left=%d to_copy=%d pos=%d\n", left, to_copy, pos);
206 copied = DMA_read_memory (nchan, tmpbuf, pos, to_copy);
207 gus_dma_transferdata (&s->emu, tmpbuf, copied, left == copied);
212 if (0 == ((mode >> 4) & 1)) {
213 DMA_release_DREQ (s->emu.gusdma);
218 static const VMStateDescription vmstate_gus = {
221 .minimum_version_id = 2,
222 .minimum_version_id_old = 2,
223 .fields = (VMStateField []) {
224 VMSTATE_INT32(pos, GUSState),
225 VMSTATE_INT32(left, GUSState),
226 VMSTATE_INT32(shift, GUSState),
227 VMSTATE_INT32(irqs, GUSState),
228 VMSTATE_INT32(samples, GUSState),
229 VMSTATE_INT64(last_ticks, GUSState),
230 VMSTATE_BUFFER(himem, GUSState),
231 VMSTATE_END_OF_LIST()
235 static int gus_initfn (ISADevice *dev)
237 GUSState *s = DO_UPCAST(GUSState, dev, dev);
238 struct audsettings as;
240 AUD_register_card ("gus", &s->card);
244 as.fmt = AUD_FMT_S16;
245 as.endianness = GUS_ENDIANNESS;
247 s->voice = AUD_open_out (
257 AUD_remove_card (&s->card);
262 s->samples = AUD_get_buffer_size_out (s->voice) >> s->shift;
263 s->mixbuf = qemu_mallocz (s->samples << s->shift);
265 register_ioport_write (s->port, 1, 1, gus_writeb, s);
266 register_ioport_write (s->port, 1, 2, gus_writew, s);
267 isa_init_ioport_range(dev, s->port, 2);
269 register_ioport_read ((s->port + 0x100) & 0xf00, 1, 1, gus_readb, s);
270 register_ioport_read ((s->port + 0x100) & 0xf00, 1, 2, gus_readw, s);
271 isa_init_ioport_range(dev, (s->port + 0x100) & 0xf00, 2);
273 register_ioport_write (s->port + 6, 10, 1, gus_writeb, s);
274 register_ioport_write (s->port + 6, 10, 2, gus_writew, s);
275 register_ioport_read (s->port + 6, 10, 1, gus_readb, s);
276 register_ioport_read (s->port + 6, 10, 2, gus_readw, s);
277 isa_init_ioport_range(dev, s->port + 6, 10);
280 register_ioport_write (s->port + 0x100, 8, 1, gus_writeb, s);
281 register_ioport_write (s->port + 0x100, 8, 2, gus_writew, s);
282 register_ioport_read (s->port + 0x100, 8, 1, gus_readb, s);
283 register_ioport_read (s->port + 0x100, 8, 2, gus_readw, s);
284 isa_init_ioport_range(dev, s->port + 0x100, 8);
286 DMA_register_channel (s->emu.gusdma, GUS_read_DMA, s);
287 s->emu.himemaddr = s->himem;
288 s->emu.gusdatapos = s->emu.himemaddr + 1024 * 1024 + 32;
290 isa_init_irq (dev, &s->pic, s->emu.gusirq);
292 AUD_set_active_out (s->voice, 1);
297 int GUS_init (qemu_irq *pic)
299 isa_create_simple ("gus");
303 static ISADeviceInfo gus_info = {
305 .qdev.desc = "Gravis Ultrasound GF1",
306 .qdev.size = sizeof (GUSState),
307 .qdev.vmsd = &vmstate_gus,
309 .qdev.props = (Property[]) {
310 DEFINE_PROP_UINT32 ("freq", GUSState, freq, 44100),
311 DEFINE_PROP_HEX32 ("iobase", GUSState, port, 0x240),
312 DEFINE_PROP_UINT32 ("irq", GUSState, emu.gusirq, 7),
313 DEFINE_PROP_UINT32 ("dma", GUSState, emu.gusdma, 3),
314 DEFINE_PROP_END_OF_LIST (),
318 static void gus_register (void)
320 isa_qdev_register (&gus_info);
322 device_init (gus_register)