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