util/hbitmap: update orig_size on truncate
[qemu/ar7.git] / hw / audio / gus.c
blob9ab51631d9e5c26fe982ae1e4dcfe8ebdf0e003f
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.
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu/module.h"
28 #include "hw/hw.h"
29 #include "hw/audio/soundhw.h"
30 #include "audio/audio.h"
31 #include "hw/isa/isa.h"
32 #include "gusemu.h"
33 #include "gustate.h"
35 #define dolog(...) AUD_log ("audio", __VA_ARGS__)
36 #ifdef DEBUG
37 #define ldebug(...) dolog (__VA_ARGS__)
38 #else
39 #define ldebug(...)
40 #endif
42 #ifdef HOST_WORDS_BIGENDIAN
43 #define GUS_ENDIANNESS 1
44 #else
45 #define GUS_ENDIANNESS 0
46 #endif
48 #define TYPE_GUS "gus"
49 #define GUS(obj) OBJECT_CHECK (GUSState, (obj), TYPE_GUS)
51 typedef struct GUSState {
52 ISADevice dev;
53 GUSEmuState emu;
54 QEMUSoundCard card;
55 uint32_t freq;
56 uint32_t port;
57 int pos, left, shift, irqs;
58 int16_t *mixbuf;
59 uint8_t himem[1024 * 1024 + 32 + 4096];
60 int samples;
61 SWVoiceOut *voice;
62 int64_t last_ticks;
63 qemu_irq pic;
64 IsaDma *isa_dma;
65 PortioList portio_list1;
66 PortioList portio_list2;
67 } GUSState;
69 static uint32_t gus_readb(void *opaque, uint32_t nport)
71 GUSState *s = opaque;
73 return gus_read (&s->emu, nport, 1);
76 static void gus_writeb(void *opaque, uint32_t nport, uint32_t val)
78 GUSState *s = opaque;
80 gus_write (&s->emu, nport, 1, val);
83 static int write_audio (GUSState *s, int samples)
85 int net = 0;
86 int pos = s->pos;
88 while (samples) {
89 int nbytes, wbytes, wsampl;
91 nbytes = samples << s->shift;
92 wbytes = AUD_write (
93 s->voice,
94 s->mixbuf + (pos << (s->shift - 1)),
95 nbytes
98 if (wbytes) {
99 wsampl = wbytes >> s->shift;
101 samples -= wsampl;
102 pos = (pos + wsampl) % s->samples;
104 net += wsampl;
106 else {
107 break;
111 return net;
114 static void GUS_callback (void *opaque, int free)
116 int samples, to_play, net = 0;
117 GUSState *s = opaque;
119 samples = free >> s->shift;
120 to_play = audio_MIN (samples, s->left);
122 while (to_play) {
123 int written = write_audio (s, to_play);
125 if (!written) {
126 goto reset;
129 s->left -= written;
130 to_play -= written;
131 samples -= written;
132 net += written;
135 samples = audio_MIN (samples, s->samples);
136 if (samples) {
137 gus_mixvoices (&s->emu, s->freq, samples, s->mixbuf);
139 while (samples) {
140 int written = write_audio (s, samples);
141 if (!written) {
142 break;
144 samples -= written;
145 net += written;
148 s->left = samples;
150 reset:
151 gus_irqgen (&s->emu, (uint64_t)net * 1000000 / s->freq);
154 int GUS_irqrequest (GUSEmuState *emu, int hwirq, int n)
156 GUSState *s = emu->opaque;
157 /* qemu_irq_lower (s->pic); */
158 qemu_irq_raise (s->pic);
159 s->irqs += n;
160 ldebug ("irqrequest %d %d %d\n", hwirq, n, s->irqs);
161 return n;
164 void GUS_irqclear (GUSEmuState *emu, int hwirq)
166 GUSState *s = emu->opaque;
167 ldebug ("irqclear %d %d\n", hwirq, s->irqs);
168 qemu_irq_lower (s->pic);
169 s->irqs -= 1;
170 #ifdef IRQ_STORM
171 if (s->irqs > 0) {
172 qemu_irq_raise (s->pic[hwirq]);
174 #endif
177 void GUS_dmarequest (GUSEmuState *emu)
179 GUSState *s = emu->opaque;
180 IsaDmaClass *k = ISADMA_GET_CLASS(s->isa_dma);
181 ldebug ("dma request %d\n", der->gusdma);
182 k->hold_DREQ(s->isa_dma, s->emu.gusdma);
185 static int GUS_read_DMA (void *opaque, int nchan, int dma_pos, int dma_len)
187 GUSState *s = opaque;
188 IsaDmaClass *k = ISADMA_GET_CLASS(s->isa_dma);
189 char tmpbuf[4096];
190 int pos = dma_pos, mode, left = dma_len - dma_pos;
192 ldebug ("read DMA %#x %d\n", dma_pos, dma_len);
193 mode = k->has_autoinitialization(s->isa_dma, s->emu.gusdma);
194 while (left) {
195 int to_copy = audio_MIN ((size_t) left, sizeof (tmpbuf));
196 int copied;
198 ldebug ("left=%d to_copy=%d pos=%d\n", left, to_copy, pos);
199 copied = k->read_memory(s->isa_dma, nchan, tmpbuf, pos, to_copy);
200 gus_dma_transferdata (&s->emu, tmpbuf, copied, left == copied);
201 left -= copied;
202 pos += copied;
205 if (((mode >> 4) & 1) == 0) {
206 k->release_DREQ(s->isa_dma, s->emu.gusdma);
208 return dma_len;
211 static const VMStateDescription vmstate_gus = {
212 .name = "gus",
213 .version_id = 2,
214 .minimum_version_id = 2,
215 .fields = (VMStateField[]) {
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 ()
227 static const MemoryRegionPortio gus_portio_list1[] = {
228 {0x000, 1, 1, .write = gus_writeb },
229 {0x006, 10, 1, .read = gus_readb, .write = gus_writeb },
230 {0x100, 8, 1, .read = gus_readb, .write = gus_writeb },
231 PORTIO_END_OF_LIST (),
234 static const MemoryRegionPortio gus_portio_list2[] = {
235 {0, 2, 1, .read = gus_readb },
236 PORTIO_END_OF_LIST (),
239 static void gus_realizefn (DeviceState *dev, Error **errp)
241 ISADevice *d = ISA_DEVICE(dev);
242 GUSState *s = GUS (dev);
243 IsaDmaClass *k;
244 struct audsettings as;
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;
252 AUD_register_card ("gus", &s->card);
254 as.freq = s->freq;
255 as.nchannels = 2;
256 as.fmt = AUDIO_FORMAT_S16;
257 as.endianness = GUS_ENDIANNESS;
259 s->voice = AUD_open_out (
260 &s->card,
261 NULL,
262 "gus",
264 GUS_callback,
268 if (!s->voice) {
269 AUD_remove_card (&s->card);
270 error_setg(errp, "No voice");
271 return;
274 s->shift = 2;
275 s->samples = AUD_get_buffer_size_out (s->voice) >> s->shift;
276 s->mixbuf = g_malloc0 (s->samples << s->shift);
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");
283 k = ISADMA_GET_CLASS(s->isa_dma);
284 k->register_channel(s->isa_dma, s->emu.gusdma, GUS_read_DMA, s);
285 s->emu.himemaddr = s->himem;
286 s->emu.gusdatapos = s->emu.himemaddr + 1024 * 1024 + 32;
287 s->emu.opaque = s;
288 isa_init_irq (d, &s->pic, s->emu.gusirq);
290 AUD_set_active_out (s->voice, 1);
293 static int GUS_init (ISABus *bus)
295 isa_create_simple (bus, TYPE_GUS);
296 return 0;
299 static Property gus_properties[] = {
300 DEFINE_PROP_UINT32 ("freq", GUSState, freq, 44100),
301 DEFINE_PROP_UINT32 ("iobase", GUSState, port, 0x240),
302 DEFINE_PROP_UINT32 ("irq", GUSState, emu.gusirq, 7),
303 DEFINE_PROP_UINT32 ("dma", GUSState, emu.gusdma, 3),
304 DEFINE_PROP_END_OF_LIST (),
307 static void gus_class_initfn (ObjectClass *klass, void *data)
309 DeviceClass *dc = DEVICE_CLASS (klass);
311 dc->realize = gus_realizefn;
312 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
313 dc->desc = "Gravis Ultrasound GF1";
314 dc->vmsd = &vmstate_gus;
315 dc->props = gus_properties;
318 static const TypeInfo gus_info = {
319 .name = TYPE_GUS,
320 .parent = TYPE_ISA_DEVICE,
321 .instance_size = sizeof (GUSState),
322 .class_init = gus_class_initfn,
325 static void gus_register_types (void)
327 type_register_static (&gus_info);
328 isa_register_soundhw("gus", "Gravis Ultrasound GF1", GUS_init);
331 type_init (gus_register_types)