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