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
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu/module.h"
28 #include "hw/audio/soundhw.h"
29 #include "audio/audio.h"
31 #include "hw/isa/isa.h"
32 #include "hw/qdev-properties.h"
33 #include "migration/vmstate.h"
37 #define dolog(...) AUD_log ("audio", __VA_ARGS__)
39 #define ldebug(...) dolog (__VA_ARGS__)
44 #ifdef HOST_WORDS_BIGENDIAN
45 #define GUS_ENDIANNESS 1
47 #define GUS_ENDIANNESS 0
50 #define TYPE_GUS "gus"
51 #define GUS(obj) OBJECT_CHECK (GUSState, (obj), TYPE_GUS)
53 typedef struct GUSState
{
59 int pos
, left
, shift
, irqs
;
61 uint8_t himem
[1024 * 1024 + 32 + 4096];
67 PortioList portio_list1
;
68 PortioList portio_list2
;
71 static uint32_t gus_readb(void *opaque
, uint32_t nport
)
75 return gus_read (&s
->emu
, nport
, 1);
78 static void gus_writeb(void *opaque
, uint32_t nport
, uint32_t val
)
82 gus_write (&s
->emu
, nport
, 1, val
);
85 static int write_audio (GUSState
*s
, int samples
)
91 int nbytes
, wbytes
, wsampl
;
93 nbytes
= samples
<< s
->shift
;
96 s
->mixbuf
+ (pos
<< (s
->shift
- 1)),
101 wsampl
= wbytes
>> s
->shift
;
104 pos
= (pos
+ wsampl
) % s
->samples
;
116 static void GUS_callback (void *opaque
, int free
)
118 int samples
, to_play
, net
= 0;
119 GUSState
*s
= opaque
;
121 samples
= free
>> s
->shift
;
122 to_play
= MIN (samples
, s
->left
);
125 int written
= write_audio (s
, to_play
);
137 samples
= MIN (samples
, s
->samples
);
139 gus_mixvoices (&s
->emu
, s
->freq
, samples
, s
->mixbuf
);
142 int written
= write_audio (s
, samples
);
153 gus_irqgen (&s
->emu
, (uint64_t)net
* 1000000 / s
->freq
);
156 int GUS_irqrequest (GUSEmuState
*emu
, int hwirq
, int n
)
158 GUSState
*s
= emu
->opaque
;
159 /* qemu_irq_lower (s->pic); */
160 qemu_irq_raise (s
->pic
);
162 ldebug ("irqrequest %d %d %d\n", hwirq
, n
, s
->irqs
);
166 void GUS_irqclear (GUSEmuState
*emu
, int hwirq
)
168 GUSState
*s
= emu
->opaque
;
169 ldebug ("irqclear %d %d\n", hwirq
, s
->irqs
);
170 qemu_irq_lower (s
->pic
);
174 qemu_irq_raise (s
->pic
[hwirq
]);
179 void GUS_dmarequest (GUSEmuState
*emu
)
181 GUSState
*s
= emu
->opaque
;
182 IsaDmaClass
*k
= ISADMA_GET_CLASS(s
->isa_dma
);
183 ldebug ("dma request %d\n", der
->gusdma
);
184 k
->hold_DREQ(s
->isa_dma
, s
->emu
.gusdma
);
187 static int GUS_read_DMA (void *opaque
, int nchan
, int dma_pos
, int dma_len
)
189 GUSState
*s
= opaque
;
190 IsaDmaClass
*k
= ISADMA_GET_CLASS(s
->isa_dma
);
192 int pos
= dma_pos
, mode
, left
= dma_len
- dma_pos
;
194 ldebug ("read DMA %#x %d\n", dma_pos
, dma_len
);
195 mode
= k
->has_autoinitialization(s
->isa_dma
, s
->emu
.gusdma
);
197 int to_copy
= MIN ((size_t) left
, sizeof (tmpbuf
));
200 ldebug ("left=%d to_copy=%d pos=%d\n", left
, to_copy
, pos
);
201 copied
= k
->read_memory(s
->isa_dma
, nchan
, tmpbuf
, pos
, to_copy
);
202 gus_dma_transferdata (&s
->emu
, tmpbuf
, copied
, left
== copied
);
207 if (((mode
>> 4) & 1) == 0) {
208 k
->release_DREQ(s
->isa_dma
, s
->emu
.gusdma
);
213 static const VMStateDescription vmstate_gus
= {
216 .minimum_version_id
= 2,
217 .fields
= (VMStateField
[]) {
218 VMSTATE_INT32 (pos
, GUSState
),
219 VMSTATE_INT32 (left
, GUSState
),
220 VMSTATE_INT32 (shift
, GUSState
),
221 VMSTATE_INT32 (irqs
, GUSState
),
222 VMSTATE_INT32 (samples
, GUSState
),
223 VMSTATE_INT64 (last_ticks
, GUSState
),
224 VMSTATE_BUFFER (himem
, GUSState
),
225 VMSTATE_END_OF_LIST ()
229 static const MemoryRegionPortio gus_portio_list1
[] = {
230 {0x000, 1, 1, .write
= gus_writeb
},
231 {0x006, 10, 1, .read
= gus_readb
, .write
= gus_writeb
},
232 {0x100, 8, 1, .read
= gus_readb
, .write
= gus_writeb
},
233 PORTIO_END_OF_LIST (),
236 static const MemoryRegionPortio gus_portio_list2
[] = {
237 {0, 2, 1, .read
= gus_readb
},
238 PORTIO_END_OF_LIST (),
241 static void gus_realizefn (DeviceState
*dev
, Error
**errp
)
243 ISADevice
*d
= ISA_DEVICE(dev
);
244 GUSState
*s
= GUS (dev
);
246 struct audsettings as
;
248 s
->isa_dma
= isa_get_dma(isa_bus_from_device(d
), s
->emu
.gusdma
);
250 error_setg(errp
, "ISA controller does not support DMA");
254 AUD_register_card ("gus", &s
->card
);
258 as
.fmt
= AUDIO_FORMAT_S16
;
259 as
.endianness
= GUS_ENDIANNESS
;
261 s
->voice
= AUD_open_out (
271 AUD_remove_card (&s
->card
);
272 error_setg(errp
, "No voice");
277 s
->samples
= AUD_get_buffer_size_out (s
->voice
) >> s
->shift
;
278 s
->mixbuf
= g_malloc0 (s
->samples
<< s
->shift
);
280 isa_register_portio_list(d
, &s
->portio_list1
, s
->port
,
281 gus_portio_list1
, s
, "gus");
282 isa_register_portio_list(d
, &s
->portio_list2
, (s
->port
+ 0x100) & 0xf00,
283 gus_portio_list2
, s
, "gus");
285 k
= ISADMA_GET_CLASS(s
->isa_dma
);
286 k
->register_channel(s
->isa_dma
, 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 (d
, &s
->pic
, s
->emu
.gusirq
);
292 AUD_set_active_out (s
->voice
, 1);
295 static int GUS_init (ISABus
*bus
)
297 isa_create_simple (bus
, TYPE_GUS
);
301 static Property gus_properties
[] = {
302 DEFINE_AUDIO_PROPERTIES(GUSState
, card
),
303 DEFINE_PROP_UINT32 ("freq", GUSState
, freq
, 44100),
304 DEFINE_PROP_UINT32 ("iobase", GUSState
, port
, 0x240),
305 DEFINE_PROP_UINT32 ("irq", GUSState
, emu
.gusirq
, 7),
306 DEFINE_PROP_UINT32 ("dma", GUSState
, emu
.gusdma
, 3),
307 DEFINE_PROP_END_OF_LIST (),
310 static void gus_class_initfn (ObjectClass
*klass
, void *data
)
312 DeviceClass
*dc
= DEVICE_CLASS (klass
);
314 dc
->realize
= gus_realizefn
;
315 set_bit(DEVICE_CATEGORY_SOUND
, dc
->categories
);
316 dc
->desc
= "Gravis Ultrasound GF1";
317 dc
->vmsd
= &vmstate_gus
;
318 dc
->props
= gus_properties
;
321 static const TypeInfo gus_info
= {
323 .parent
= TYPE_ISA_DEVICE
,
324 .instance_size
= sizeof (GUSState
),
325 .class_init
= gus_class_initfn
,
328 static void gus_register_types (void)
330 type_register_static (&gus_info
);
331 isa_register_soundhw("gus", "Gravis Ultrasound GF1", GUS_init
);
334 type_init (gus_register_types
)