master
c 326 lines 8.67 KB
Raw
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 */
24
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu/module.h"
28 #include "hw/audio/model.h"
29 #include "qemu/audio.h"
30 #include "hw/core/irq.h"
31 #include "hw/isa/isa.h"
32 #include "hw/core/qdev-properties.h"
33 #include "migration/vmstate.h"
34 #include "gusemu.h"
35 #include "qemu/error-report.h"
36 #include "qom/object.h"
37
38 #define DEBUG 0
39
40 #define ldebug(fmt, ...) do { \
41 if (DEBUG) { \
42 error_report("gus: " fmt, ##__VA_ARGS__); \
43 } \
44 } while (0)
45
46 #define TYPE_GUS "gus"
47 OBJECT_DECLARE_SIMPLE_TYPE(GUSState, GUS)
48
49 struct GUSState {
50 ISADevice dev;
51 GUSEmuState emu;
52 AudioBackend *audio_be;
53 uint32_t freq;
54 uint32_t port;
55 int pos, left, shift, irqs;
56 int16_t *mixbuf;
57 uint8_t himem[1024 * 1024 + 32 + 4096];
58 int samples;
59 SWVoiceOut *voice;
60 int64_t last_ticks;
61 qemu_irq pic;
62 IsaDma *isa_dma;
63 PortioList portio_list1;
64 PortioList portio_list2;
65 };
66
67 static uint32_t gus_readb(void *opaque, uint32_t nport)
68 {
69 GUSState *s = opaque;
70
71 return gus_read (&s->emu, nport, 1);
72 }
73
74 static void gus_writeb(void *opaque, uint32_t nport, uint32_t val)
75 {
76 GUSState *s = opaque;
77
78 gus_write (&s->emu, nport, 1, val);
79 }
80
81 static int write_audio (GUSState *s, int samples)
82 {
83 int net = 0;
84 int pos = s->pos;
85
86 while (samples) {
87 int nbytes, wbytes, wsampl;
88
89 nbytes = samples << s->shift;
90 wbytes = audio_be_write(
91 s->audio_be,
92 s->voice,
93 s->mixbuf + (pos << (s->shift - 1)),
94 nbytes
95 );
96
97 if (wbytes) {
98 wsampl = wbytes >> s->shift;
99
100 samples -= wsampl;
101 pos = (pos + wsampl) % s->samples;
102
103 net += wsampl;
104 }
105 else {
106 break;
107 }
108 }
109
110 return net;
111 }
112
113 static void GUS_callback (void *opaque, int free)
114 {
115 int samples, to_play, net = 0;
116 GUSState *s = opaque;
117
118 samples = free >> s->shift;
119 to_play = MIN (samples, s->left);
120
121 while (to_play) {
122 int written = write_audio (s, to_play);
123
124 if (!written) {
125 goto reset;
126 }
127
128 s->left -= written;
129 to_play -= written;
130 samples -= written;
131 net += written;
132 }
133
134 samples = MIN (samples, s->samples);
135 if (samples) {
136 gus_mixvoices (&s->emu, s->freq, samples, s->mixbuf);
137
138 while (samples) {
139 int written = write_audio (s, samples);
140 if (!written) {
141 break;
142 }
143 samples -= written;
144 net += written;
145 }
146 }
147 s->left = samples;
148
149 reset:
150 gus_irqgen (&s->emu, (uint64_t)net * 1000000 / s->freq);
151 }
152
153 int GUS_irqrequest (GUSEmuState *emu, int hwirq, int n)
154 {
155 GUSState *s = emu->opaque;
156 /* qemu_irq_lower (s->pic); */
157 qemu_irq_raise (s->pic);
158 s->irqs += n;
159 ldebug("irqrequest %d %d %d", hwirq, n, s->irqs);
160 return n;
161 }
162
163 void GUS_irqclear (GUSEmuState *emu, int hwirq)
164 {
165 GUSState *s = emu->opaque;
166 ldebug("irqclear %d %d", hwirq, s->irqs);
167 qemu_irq_lower (s->pic);
168 s->irqs -= 1;
169 #ifdef IRQ_STORM
170 if (s->irqs > 0) {
171 qemu_irq_raise (s->pic[hwirq]);
172 }
173 #endif
174 }
175
176 void GUS_dmarequest (GUSEmuState *emu)
177 {
178 GUSState *s = emu->opaque;
179 IsaDmaClass *k = ISADMA_GET_CLASS(s->isa_dma);
180 ldebug("dma request %d", s->emu.gusdma);
181 k->hold_DREQ(s->isa_dma, s->emu.gusdma);
182 }
183
184 static int GUS_read_DMA (void *opaque, int nchan, int dma_pos, int dma_len)
185 {
186 GUSState *s = opaque;
187 IsaDmaClass *k = ISADMA_GET_CLASS(s->isa_dma);
188 QEMU_UNINITIALIZED char tmpbuf[4096];
189 int pos = dma_pos, mode, left = dma_len - dma_pos;
190
191 ldebug("read DMA 0x%x %d", dma_pos, dma_len);
192 mode = k->has_autoinitialization(s->isa_dma, s->emu.gusdma);
193 while (left) {
194 int to_copy = MIN ((size_t) left, sizeof (tmpbuf));
195 int copied;
196
197 ldebug("left=%d to_copy=%d pos=%d", left, to_copy, pos);
198 copied = k->read_memory(s->isa_dma, nchan, tmpbuf, pos, to_copy);
199 gus_dma_transferdata (&s->emu, tmpbuf, copied, left == copied);
200 left -= copied;
201 pos += copied;
202 }
203
204 if (((mode >> 4) & 1) == 0) {
205 k->release_DREQ(s->isa_dma, s->emu.gusdma);
206 }
207 return dma_len;
208 }
209
210 static const VMStateDescription vmstate_gus = {
211 .name = "gus",
212 .version_id = 2,
213 .minimum_version_id = 2,
214 .fields = (const VMStateField[]) {
215 VMSTATE_INT32 (pos, GUSState),
216 VMSTATE_INT32 (left, GUSState),
217 VMSTATE_INT32 (shift, GUSState),
218 VMSTATE_INT32 (irqs, GUSState),
219 VMSTATE_INT32 (samples, GUSState),
220 VMSTATE_INT64 (last_ticks, GUSState),
221 VMSTATE_BUFFER (himem, GUSState),
222 VMSTATE_END_OF_LIST ()
223 }
224 };
225
226 static const MemoryRegionPortio gus_portio_list1[] = {
227 {0x000, 1, 1, .write = gus_writeb },
228 {0x006, 10, 1, .read = gus_readb, .write = gus_writeb },
229 {0x100, 8, 1, .read = gus_readb, .write = gus_writeb },
230 PORTIO_END_OF_LIST (),
231 };
232
233 static const MemoryRegionPortio gus_portio_list2[] = {
234 {0, 2, 1, .read = gus_readb },
235 PORTIO_END_OF_LIST (),
236 };
237
238 static void gus_realizefn (DeviceState *dev, Error **errp)
239 {
240 ISADevice *d = ISA_DEVICE(dev);
241 ISABus *bus = isa_bus_from_device(d);
242 GUSState *s = GUS (dev);
243 IsaDmaClass *k;
244 struct audsettings as;
245
246 if (!audio_be_check(&s->audio_be, errp)) {
247 return;
248 }
249
250 s->isa_dma = isa_bus_get_dma(bus, s->emu.gusdma);
251 if (!s->isa_dma) {
252 error_setg(errp, "ISA controller does not support DMA");
253 return;
254 }
255
256 as.freq = s->freq;
257 as.nchannels = 2;
258 as.fmt = AUDIO_FORMAT_S16;
259 as.big_endian = HOST_BIG_ENDIAN;
260
261 s->voice = audio_be_open_out(
262 s->audio_be,
263 NULL,
264 "gus",
265 s,
266 GUS_callback,
267 &as
268 );
269
270 if (!s->voice) {
271 error_setg(errp, "No voice");
272 return;
273 }
274
275 s->shift = 2;
276 s->samples = audio_be_get_buffer_size_out(s->audio_be, s->voice) >> s->shift;
277 s->mixbuf = g_malloc0 (s->samples << s->shift);
278
279 isa_register_portio_list(d, &s->portio_list1, s->port,
280 gus_portio_list1, s, "gus");
281 isa_register_portio_list(d, &s->portio_list2, (s->port + 0x100) & 0xf00,
282 gus_portio_list2, s, "gus");
283
284 k = ISADMA_GET_CLASS(s->isa_dma);
285 k->register_channel(s->isa_dma, s->emu.gusdma, GUS_read_DMA, s);
286 s->emu.himemaddr = s->himem;
287 s->emu.gusdatapos = s->emu.himemaddr + 1024 * 1024 + 32;
288 s->emu.opaque = s;
289 s->pic = isa_bus_get_irq(bus, s->emu.gusirq);
290
291 audio_be_set_active_out(s->audio_be, s->voice, 1);
292 }
293
294 static const Property gus_properties[] = {
295 DEFINE_AUDIO_PROPERTIES(GUSState, audio_be),
296 DEFINE_PROP_UINT32 ("freq", GUSState, freq, 44100),
297 DEFINE_PROP_UINT32 ("iobase", GUSState, port, 0x240),
298 DEFINE_PROP_UINT32 ("irq", GUSState, emu.gusirq, 7),
299 DEFINE_PROP_UINT32 ("dma", GUSState, emu.gusdma, 3),
300 };
301
302 static void gus_class_initfn(ObjectClass *klass, const void *data)
303 {
304 DeviceClass *dc = DEVICE_CLASS (klass);
305
306 dc->realize = gus_realizefn;
307 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
308 dc->desc = "Gravis Ultrasound GF1";
309 dc->vmsd = &vmstate_gus;
310 device_class_set_props(dc, gus_properties);
311 }
312
313 static const TypeInfo gus_info = {
314 .name = TYPE_GUS,
315 .parent = TYPE_ISA_DEVICE,
316 .instance_size = sizeof (GUSState),
317 .class_init = gus_class_initfn,
318 };
319
320 static void gus_register_types (void)
321 {
322 type_register_static (&gus_info);
323 audio_register_model("gus", "Gravis Ultrasound GF1", TYPE_GUS);
324 }
325
326 type_init (gus_register_types)