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