]> git.proxmox.com Git - mirror_qemu.git/blame - hw/audio/gus.c
include/qemu/osdep.h: Don't include qapi/error.h
[mirror_qemu.git] / hw / audio / gus.c
CommitLineData
423d65f4
AZ
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 */
6086a565 24#include "qemu/osdep.h"
da34e65c 25#include "qapi/error.h"
83c9f4ca 26#include "hw/hw.h"
0d09e41a 27#include "hw/audio/audio.h"
423d65f4 28#include "audio/audio.h"
0d09e41a 29#include "hw/isa/isa.h"
47b43a1f
PB
30#include "gusemu.h"
31#include "gustate.h"
423d65f4
AZ
32
33#define dolog(...) AUD_log ("audio", __VA_ARGS__)
34#ifdef DEBUG
35#define ldebug(...) dolog (__VA_ARGS__)
36#else
37#define ldebug(...)
38#endif
39
e2542fe2 40#ifdef HOST_WORDS_BIGENDIAN
423d65f4
AZ
41#define GUS_ENDIANNESS 1
42#else
43#define GUS_ENDIANNESS 0
44#endif
45
11c7549d
AF
46#define TYPE_GUS "gus"
47#define GUS(obj) OBJECT_CHECK (GUSState, (obj), TYPE_GUS)
48
423d65f4 49typedef struct GUSState {
9df34396 50 ISADevice dev;
423d65f4
AZ
51 GUSEmuState emu;
52 QEMUSoundCard card;
9df34396
GH
53 uint32_t freq;
54 uint32_t port;
423d65f4 55 int pos, left, shift, irqs;
731ba0ce 56 GUSsample *mixbuf;
423d65f4
AZ
57 uint8_t himem[1024 * 1024 + 32 + 4096];
58 int samples;
59 SWVoiceOut *voice;
60 int64_t last_ticks;
9df34396 61 qemu_irq pic;
467be5f2 62 IsaDma *isa_dma;
423d65f4
AZ
63} GUSState;
64
8307c294 65static uint32_t gus_readb(void *opaque, uint32_t nport)
423d65f4
AZ
66{
67 GUSState *s = opaque;
68
69 return gus_read (&s->emu, nport, 1);
70}
71
8307c294 72static void gus_writeb(void *opaque, uint32_t nport, uint32_t val)
423d65f4
AZ
73{
74 GUSState *s = opaque;
75
76 gus_write (&s->emu, nport, 1, val);
77}
78
423d65f4
AZ
79static int write_audio (GUSState *s, int samples)
80{
81 int net = 0;
82 int pos = s->pos;
83
84 while (samples) {
85 int nbytes, wbytes, wsampl;
86
87 nbytes = samples << s->shift;
88 wbytes = AUD_write (
89 s->voice,
90 s->mixbuf + (pos << (s->shift - 1)),
91 nbytes
92 );
93
94 if (wbytes) {
95 wsampl = wbytes >> s->shift;
96
97 samples -= wsampl;
98 pos = (pos + wsampl) % s->samples;
99
100 net += wsampl;
101 }
102 else {
103 break;
104 }
105 }
106
107 return net;
108}
109
110static void GUS_callback (void *opaque, int free)
111{
112 int samples, to_play, net = 0;
113 GUSState *s = opaque;
114
115 samples = free >> s->shift;
116 to_play = audio_MIN (samples, s->left);
117
118 while (to_play) {
119 int written = write_audio (s, to_play);
120
121 if (!written) {
122 goto reset;
123 }
124
125 s->left -= written;
126 to_play -= written;
127 samples -= written;
128 net += written;
129 }
130
131 samples = audio_MIN (samples, s->samples);
132 if (samples) {
133 gus_mixvoices (&s->emu, s->freq, samples, s->mixbuf);
134
135 while (samples) {
136 int written = write_audio (s, samples);
137 if (!written) {
138 break;
139 }
140 samples -= written;
141 net += written;
142 }
143 }
144 s->left = samples;
145
4f4cc0ef 146 reset:
147 gus_irqgen (&s->emu, muldiv64 (net, 1000000, s->freq));
423d65f4
AZ
148}
149
150int GUS_irqrequest (GUSEmuState *emu, int hwirq, int n)
151{
152 GUSState *s = emu->opaque;
9df34396
GH
153 /* qemu_irq_lower (s->pic); */
154 qemu_irq_raise (s->pic);
423d65f4
AZ
155 s->irqs += n;
156 ldebug ("irqrequest %d %d %d\n", hwirq, n, s->irqs);
157 return n;
158}
159
160void GUS_irqclear (GUSEmuState *emu, int hwirq)
161{
162 GUSState *s = emu->opaque;
163 ldebug ("irqclear %d %d\n", hwirq, s->irqs);
9df34396 164 qemu_irq_lower (s->pic);
423d65f4
AZ
165 s->irqs -= 1;
166#ifdef IRQ_STORM
167 if (s->irqs > 0) {
168 qemu_irq_raise (s->pic[hwirq]);
169 }
170#endif
171}
172
467be5f2 173void GUS_dmarequest (GUSEmuState *emu)
423d65f4 174{
467be5f2
HP
175 GUSState *s = emu->opaque;
176 IsaDmaClass *k = ISADMA_GET_CLASS(s->isa_dma);
423d65f4 177 ldebug ("dma request %d\n", der->gusdma);
467be5f2 178 k->hold_DREQ(s->isa_dma, s->emu.gusdma);
423d65f4
AZ
179}
180
371bc573 181static int GUS_read_DMA (void *opaque, int nchan, int dma_pos, int dma_len)
423d65f4
AZ
182{
183 GUSState *s = opaque;
467be5f2 184 IsaDmaClass *k = ISADMA_GET_CLASS(s->isa_dma);
731ba0ce 185 char tmpbuf[4096];
423d65f4
AZ
186 int pos = dma_pos, mode, left = dma_len - dma_pos;
187
188 ldebug ("read DMA %#x %d\n", dma_pos, dma_len);
467be5f2 189 mode = k->has_autoinitialization(s->isa_dma, s->emu.gusdma);
423d65f4
AZ
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);
467be5f2 195 copied = k->read_memory(s->isa_dma, nchan, tmpbuf, pos, to_copy);
423d65f4
AZ
196 gus_dma_transferdata (&s->emu, tmpbuf, copied, left == copied);
197 left -= copied;
198 pos += copied;
199 }
200
2ab5bf67 201 if (((mode >> 4) & 1) == 0) {
467be5f2 202 k->release_DREQ(s->isa_dma, s->emu.gusdma);
423d65f4
AZ
203 }
204 return dma_len;
205}
206
709ae102
JQ
207static const VMStateDescription vmstate_gus = {
208 .name = "gus",
209 .version_id = 2,
210 .minimum_version_id = 2,
d49805ae 211 .fields = (VMStateField[]) {
cf4dc461 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 ()
709ae102
JQ
220 }
221};
5b5ef0db 222
d7adb96f
RH
223static const MemoryRegionPortio gus_portio_list1[] = {
224 {0x000, 1, 1, .write = gus_writeb },
d7adb96f 225 {0x006, 10, 1, .read = gus_readb, .write = gus_writeb },
d7adb96f 226 {0x100, 8, 1, .read = gus_readb, .write = gus_writeb },
cf4dc461 227 PORTIO_END_OF_LIST (),
d7adb96f
RH
228};
229
230static const MemoryRegionPortio gus_portio_list2[] = {
54da54e5 231 {0, 2, 1, .read = gus_readb },
cf4dc461 232 PORTIO_END_OF_LIST (),
d7adb96f
RH
233};
234
db895a1e 235static void gus_realizefn (DeviceState *dev, Error **errp)
423d65f4 236{
db895a1e 237 ISADevice *d = ISA_DEVICE(dev);
11c7549d 238 GUSState *s = GUS (dev);
467be5f2 239 IsaDmaClass *k;
1ea879e5 240 struct audsettings as;
423d65f4 241
1a7dafce 242 AUD_register_card ("gus", &s->card);
423d65f4 243
9df34396 244 as.freq = s->freq;
423d65f4
AZ
245 as.nchannels = 2;
246 as.fmt = AUD_FMT_S16;
247 as.endianness = GUS_ENDIANNESS;
248
249 s->voice = AUD_open_out (
250 &s->card,
251 NULL,
252 "gus",
253 s,
254 GUS_callback,
255 &as
256 );
257
258 if (!s->voice) {
259 AUD_remove_card (&s->card);
db895a1e
AF
260 error_setg(errp, "No voice");
261 return;
423d65f4
AZ
262 }
263
264 s->shift = 2;
265 s->samples = AUD_get_buffer_size_out (s->voice) >> s->shift;
7267c094 266 s->mixbuf = g_malloc0 (s->samples << s->shift);
423d65f4 267
db895a1e
AF
268 isa_register_portio_list (d, s->port, gus_portio_list1, s, "gus");
269 isa_register_portio_list (d, (s->port + 0x100) & 0xf00,
d7adb96f 270 gus_portio_list2, s, "gus");
423d65f4 271
467be5f2
HP
272 s->isa_dma = isa_get_dma(isa_bus_from_device(d), s->emu.gusdma);
273 k = ISADMA_GET_CLASS(s->isa_dma);
274 k->register_channel(s->isa_dma, s->emu.gusdma, GUS_read_DMA, s);
423d65f4
AZ
275 s->emu.himemaddr = s->himem;
276 s->emu.gusdatapos = s->emu.himemaddr + 1024 * 1024 + 32;
277 s->emu.opaque = s;
db895a1e 278 isa_init_irq (d, &s->pic, s->emu.gusirq);
423d65f4
AZ
279
280 AUD_set_active_out (s->voice, 1);
423d65f4 281}
9df34396 282
36cd6f6f 283static int GUS_init (ISABus *bus)
9df34396 284{
11c7549d 285 isa_create_simple (bus, TYPE_GUS);
9df34396
GH
286 return 0;
287}
288
39bffca2
AL
289static Property gus_properties[] = {
290 DEFINE_PROP_UINT32 ("freq", GUSState, freq, 44100),
c7bcc85d 291 DEFINE_PROP_UINT32 ("iobase", GUSState, port, 0x240),
39bffca2
AL
292 DEFINE_PROP_UINT32 ("irq", GUSState, emu.gusirq, 7),
293 DEFINE_PROP_UINT32 ("dma", GUSState, emu.gusdma, 3),
294 DEFINE_PROP_END_OF_LIST (),
295};
296
cf4dc461 297static void gus_class_initfn (ObjectClass *klass, void *data)
8f04ee08 298{
cf4dc461 299 DeviceClass *dc = DEVICE_CLASS (klass);
db895a1e
AF
300
301 dc->realize = gus_realizefn;
125ee0ed 302 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
39bffca2
AL
303 dc->desc = "Gravis Ultrasound GF1";
304 dc->vmsd = &vmstate_gus;
305 dc->props = gus_properties;
8f04ee08
AL
306}
307
8c43a6f0 308static const TypeInfo gus_info = {
11c7549d 309 .name = TYPE_GUS,
39bffca2
AL
310 .parent = TYPE_ISA_DEVICE,
311 .instance_size = sizeof (GUSState),
312 .class_init = gus_class_initfn,
9df34396
GH
313};
314
83f7d43a 315static void gus_register_types (void)
9df34396 316{
cf4dc461 317 type_register_static (&gus_info);
36cd6f6f 318 isa_register_soundhw("gus", "Gravis Ultrasound GF1", GUS_init);
9df34396 319}
83f7d43a
AF
320
321type_init (gus_register_types)