balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 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 | #include "hw.h" |
| 25 | #include "audiodev.h" |
| 26 | #include "audio/audio.h" |
| 27 | #include "isa.h" |
| 28 | #include "gusemu.h" |
| 29 | #include "gustate.h" |
| 30 | |
| 31 | #define dolog(...) AUD_log ("audio", __VA_ARGS__) |
| 32 | #ifdef DEBUG |
| 33 | #define ldebug(...) dolog (__VA_ARGS__) |
| 34 | #else |
| 35 | #define ldebug(...) |
| 36 | #endif |
| 37 | |
Juan Quintela | e2542fe | 2009-07-27 16:13:06 +0200 | [diff] [blame] | 38 | #ifdef HOST_WORDS_BIGENDIAN |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 39 | #define GUS_ENDIANNESS 1 |
| 40 | #else |
| 41 | #define GUS_ENDIANNESS 0 |
| 42 | #endif |
| 43 | |
| 44 | #define IO_READ_PROTO(name) \ |
malc | 371bc57 | 2008-12-11 00:14:28 +0000 | [diff] [blame] | 45 | static uint32_t name (void *opaque, uint32_t nport) |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 46 | #define IO_WRITE_PROTO(name) \ |
malc | 371bc57 | 2008-12-11 00:14:28 +0000 | [diff] [blame] | 47 | static void name (void *opaque, uint32_t nport, uint32_t val) |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 48 | |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 49 | typedef struct GUSState { |
Gerd Hoffmann | 9df3439 | 2009-09-10 11:43:32 +0200 | [diff] [blame] | 50 | ISADevice dev; |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 51 | GUSEmuState emu; |
| 52 | QEMUSoundCard card; |
Gerd Hoffmann | 9df3439 | 2009-09-10 11:43:32 +0200 | [diff] [blame] | 53 | uint32_t freq; |
| 54 | uint32_t port; |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 55 | int pos, left, shift, irqs; |
malc | 731ba0c | 2008-06-08 01:42:47 +0000 | [diff] [blame] | 56 | GUSsample *mixbuf; |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 57 | uint8_t himem[1024 * 1024 + 32 + 4096]; |
| 58 | int samples; |
| 59 | SWVoiceOut *voice; |
| 60 | int64_t last_ticks; |
Gerd Hoffmann | 9df3439 | 2009-09-10 11:43:32 +0200 | [diff] [blame] | 61 | qemu_irq pic; |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 62 | } GUSState; |
| 63 | |
| 64 | IO_READ_PROTO (gus_readb) |
| 65 | { |
| 66 | GUSState *s = opaque; |
| 67 | |
| 68 | return gus_read (&s->emu, nport, 1); |
| 69 | } |
| 70 | |
| 71 | IO_READ_PROTO (gus_readw) |
| 72 | { |
| 73 | GUSState *s = opaque; |
| 74 | |
| 75 | return gus_read (&s->emu, nport, 2); |
| 76 | } |
| 77 | |
| 78 | IO_WRITE_PROTO (gus_writeb) |
| 79 | { |
| 80 | GUSState *s = opaque; |
| 81 | |
| 82 | gus_write (&s->emu, nport, 1, val); |
| 83 | } |
| 84 | |
| 85 | IO_WRITE_PROTO (gus_writew) |
| 86 | { |
| 87 | GUSState *s = opaque; |
| 88 | |
| 89 | gus_write (&s->emu, nport, 2, val); |
| 90 | } |
| 91 | |
| 92 | static int write_audio (GUSState *s, int samples) |
| 93 | { |
| 94 | int net = 0; |
| 95 | int pos = s->pos; |
| 96 | |
| 97 | while (samples) { |
| 98 | int nbytes, wbytes, wsampl; |
| 99 | |
| 100 | nbytes = samples << s->shift; |
| 101 | wbytes = AUD_write ( |
| 102 | s->voice, |
| 103 | s->mixbuf + (pos << (s->shift - 1)), |
| 104 | nbytes |
| 105 | ); |
| 106 | |
| 107 | if (wbytes) { |
| 108 | wsampl = wbytes >> s->shift; |
| 109 | |
| 110 | samples -= wsampl; |
| 111 | pos = (pos + wsampl) % s->samples; |
| 112 | |
| 113 | net += wsampl; |
| 114 | } |
| 115 | else { |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return net; |
| 121 | } |
| 122 | |
| 123 | static void GUS_callback (void *opaque, int free) |
| 124 | { |
| 125 | int samples, to_play, net = 0; |
| 126 | GUSState *s = opaque; |
| 127 | |
| 128 | samples = free >> s->shift; |
| 129 | to_play = audio_MIN (samples, s->left); |
| 130 | |
| 131 | while (to_play) { |
| 132 | int written = write_audio (s, to_play); |
| 133 | |
| 134 | if (!written) { |
| 135 | goto reset; |
| 136 | } |
| 137 | |
| 138 | s->left -= written; |
| 139 | to_play -= written; |
| 140 | samples -= written; |
| 141 | net += written; |
| 142 | } |
| 143 | |
| 144 | samples = audio_MIN (samples, s->samples); |
| 145 | if (samples) { |
| 146 | gus_mixvoices (&s->emu, s->freq, samples, s->mixbuf); |
| 147 | |
| 148 | while (samples) { |
| 149 | int written = write_audio (s, samples); |
| 150 | if (!written) { |
| 151 | break; |
| 152 | } |
| 153 | samples -= written; |
| 154 | net += written; |
| 155 | } |
| 156 | } |
| 157 | s->left = samples; |
| 158 | |
malc | 4f4cc0e | 2009-09-18 08:16:03 +0400 | [diff] [blame] | 159 | reset: |
| 160 | gus_irqgen (&s->emu, muldiv64 (net, 1000000, s->freq)); |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | int GUS_irqrequest (GUSEmuState *emu, int hwirq, int n) |
| 164 | { |
| 165 | GUSState *s = emu->opaque; |
Gerd Hoffmann | 9df3439 | 2009-09-10 11:43:32 +0200 | [diff] [blame] | 166 | /* qemu_irq_lower (s->pic); */ |
| 167 | qemu_irq_raise (s->pic); |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 168 | s->irqs += n; |
| 169 | ldebug ("irqrequest %d %d %d\n", hwirq, n, s->irqs); |
| 170 | return n; |
| 171 | } |
| 172 | |
| 173 | void GUS_irqclear (GUSEmuState *emu, int hwirq) |
| 174 | { |
| 175 | GUSState *s = emu->opaque; |
| 176 | ldebug ("irqclear %d %d\n", hwirq, s->irqs); |
Gerd Hoffmann | 9df3439 | 2009-09-10 11:43:32 +0200 | [diff] [blame] | 177 | qemu_irq_lower (s->pic); |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 178 | s->irqs -= 1; |
| 179 | #ifdef IRQ_STORM |
| 180 | if (s->irqs > 0) { |
| 181 | qemu_irq_raise (s->pic[hwirq]); |
| 182 | } |
| 183 | #endif |
| 184 | } |
| 185 | |
| 186 | void GUS_dmarequest (GUSEmuState *der) |
| 187 | { |
| 188 | /* GUSState *s = (GUSState *) der; */ |
| 189 | ldebug ("dma request %d\n", der->gusdma); |
| 190 | DMA_hold_DREQ (der->gusdma); |
| 191 | } |
| 192 | |
malc | 371bc57 | 2008-12-11 00:14:28 +0000 | [diff] [blame] | 193 | static int GUS_read_DMA (void *opaque, int nchan, int dma_pos, int dma_len) |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 194 | { |
| 195 | GUSState *s = opaque; |
malc | 731ba0c | 2008-06-08 01:42:47 +0000 | [diff] [blame] | 196 | char tmpbuf[4096]; |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 197 | int pos = dma_pos, mode, left = dma_len - dma_pos; |
| 198 | |
| 199 | ldebug ("read DMA %#x %d\n", dma_pos, dma_len); |
| 200 | mode = DMA_get_channel_mode (s->emu.gusdma); |
| 201 | while (left) { |
| 202 | int to_copy = audio_MIN ((size_t) left, sizeof (tmpbuf)); |
| 203 | int copied; |
| 204 | |
| 205 | ldebug ("left=%d to_copy=%d pos=%d\n", left, to_copy, pos); |
| 206 | copied = DMA_read_memory (nchan, tmpbuf, pos, to_copy); |
| 207 | gus_dma_transferdata (&s->emu, tmpbuf, copied, left == copied); |
| 208 | left -= copied; |
| 209 | pos += copied; |
| 210 | } |
| 211 | |
| 212 | if (0 == ((mode >> 4) & 1)) { |
| 213 | DMA_release_DREQ (s->emu.gusdma); |
| 214 | } |
| 215 | return dma_len; |
| 216 | } |
| 217 | |
Juan Quintela | 709ae10 | 2009-12-02 11:49:38 +0100 | [diff] [blame] | 218 | static const VMStateDescription vmstate_gus = { |
| 219 | .name = "gus", |
| 220 | .version_id = 2, |
| 221 | .minimum_version_id = 2, |
| 222 | .minimum_version_id_old = 2, |
| 223 | .fields = (VMStateField []) { |
malc | cf4dc46 | 2012-02-07 22:11:04 +0400 | [diff] [blame] | 224 | VMSTATE_INT32 (pos, GUSState), |
| 225 | VMSTATE_INT32 (left, GUSState), |
| 226 | VMSTATE_INT32 (shift, GUSState), |
| 227 | VMSTATE_INT32 (irqs, GUSState), |
| 228 | VMSTATE_INT32 (samples, GUSState), |
| 229 | VMSTATE_INT64 (last_ticks, GUSState), |
| 230 | VMSTATE_BUFFER (himem, GUSState), |
| 231 | VMSTATE_END_OF_LIST () |
Juan Quintela | 709ae10 | 2009-12-02 11:49:38 +0100 | [diff] [blame] | 232 | } |
| 233 | }; |
malc | 5b5ef0d | 2008-06-21 17:14:54 +0000 | [diff] [blame] | 234 | |
Richard Henderson | d7adb96 | 2011-08-15 15:25:26 -0700 | [diff] [blame] | 235 | static const MemoryRegionPortio gus_portio_list1[] = { |
| 236 | {0x000, 1, 1, .write = gus_writeb }, |
| 237 | {0x000, 1, 2, .write = gus_writew }, |
| 238 | {0x006, 10, 1, .read = gus_readb, .write = gus_writeb }, |
| 239 | {0x006, 10, 2, .read = gus_readw, .write = gus_writew }, |
| 240 | {0x100, 8, 1, .read = gus_readb, .write = gus_writeb }, |
| 241 | {0x100, 8, 2, .read = gus_readw, .write = gus_writew }, |
malc | cf4dc46 | 2012-02-07 22:11:04 +0400 | [diff] [blame] | 242 | PORTIO_END_OF_LIST (), |
Richard Henderson | d7adb96 | 2011-08-15 15:25:26 -0700 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | static const MemoryRegionPortio gus_portio_list2[] = { |
| 246 | {0, 1, 1, .read = gus_readb }, |
| 247 | {0, 1, 2, .read = gus_readw }, |
malc | cf4dc46 | 2012-02-07 22:11:04 +0400 | [diff] [blame] | 248 | PORTIO_END_OF_LIST (), |
Richard Henderson | d7adb96 | 2011-08-15 15:25:26 -0700 | [diff] [blame] | 249 | }; |
| 250 | |
Gerd Hoffmann | 9df3439 | 2009-09-10 11:43:32 +0200 | [diff] [blame] | 251 | static int gus_initfn (ISADevice *dev) |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 252 | { |
malc | 8acbc9b | 2011-10-09 19:04:16 +0400 | [diff] [blame] | 253 | GUSState *s = DO_UPCAST (GUSState, dev, dev); |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 254 | struct audsettings as; |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 255 | |
malc | 1a7dafc | 2009-05-14 03:11:35 +0400 | [diff] [blame] | 256 | AUD_register_card ("gus", &s->card); |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 257 | |
Gerd Hoffmann | 9df3439 | 2009-09-10 11:43:32 +0200 | [diff] [blame] | 258 | as.freq = s->freq; |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 259 | as.nchannels = 2; |
| 260 | as.fmt = AUD_FMT_S16; |
| 261 | as.endianness = GUS_ENDIANNESS; |
| 262 | |
| 263 | s->voice = AUD_open_out ( |
| 264 | &s->card, |
| 265 | NULL, |
| 266 | "gus", |
| 267 | s, |
| 268 | GUS_callback, |
| 269 | &as |
| 270 | ); |
| 271 | |
| 272 | if (!s->voice) { |
| 273 | AUD_remove_card (&s->card); |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 274 | return -1; |
| 275 | } |
| 276 | |
| 277 | s->shift = 2; |
| 278 | s->samples = AUD_get_buffer_size_out (s->voice) >> s->shift; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 279 | s->mixbuf = g_malloc0 (s->samples << s->shift); |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 280 | |
Richard Henderson | d7adb96 | 2011-08-15 15:25:26 -0700 | [diff] [blame] | 281 | isa_register_portio_list (dev, s->port, gus_portio_list1, s, "gus"); |
| 282 | isa_register_portio_list (dev, (s->port + 0x100) & 0xf00, |
| 283 | gus_portio_list2, s, "gus"); |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 284 | |
malc | a5e8e46 | 2009-09-10 19:59:41 +0400 | [diff] [blame] | 285 | DMA_register_channel (s->emu.gusdma, GUS_read_DMA, s); |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 286 | s->emu.himemaddr = s->himem; |
| 287 | s->emu.gusdatapos = s->emu.himemaddr + 1024 * 1024 + 32; |
| 288 | s->emu.opaque = s; |
malc | a5e8e46 | 2009-09-10 19:59:41 +0400 | [diff] [blame] | 289 | isa_init_irq (dev, &s->pic, s->emu.gusirq); |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 290 | |
| 291 | AUD_set_active_out (s->voice, 1); |
malc | 5b5ef0d | 2008-06-21 17:14:54 +0000 | [diff] [blame] | 292 | |
balrog | 423d65f | 2008-01-14 22:09:11 +0000 | [diff] [blame] | 293 | return 0; |
| 294 | } |
Gerd Hoffmann | 9df3439 | 2009-09-10 11:43:32 +0200 | [diff] [blame] | 295 | |
Hervé Poussineau | 4a0f031 | 2011-12-15 22:10:01 +0100 | [diff] [blame] | 296 | int GUS_init (ISABus *bus) |
Gerd Hoffmann | 9df3439 | 2009-09-10 11:43:32 +0200 | [diff] [blame] | 297 | { |
Hervé Poussineau | 48a18b3 | 2011-12-15 22:09:51 +0100 | [diff] [blame] | 298 | isa_create_simple (bus, "gus"); |
Gerd Hoffmann | 9df3439 | 2009-09-10 11:43:32 +0200 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 302 | static Property gus_properties[] = { |
| 303 | DEFINE_PROP_UINT32 ("freq", GUSState, freq, 44100), |
| 304 | DEFINE_PROP_HEX32 ("iobase", GUSState, port, 0x240), |
| 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 | |
malc | cf4dc46 | 2012-02-07 22:11:04 +0400 | [diff] [blame] | 310 | static void gus_class_initfn (ObjectClass *klass, void *data) |
Anthony Liguori | 8f04ee0 | 2011-12-04 11:52:49 -0600 | [diff] [blame] | 311 | { |
malc | cf4dc46 | 2012-02-07 22:11:04 +0400 | [diff] [blame] | 312 | DeviceClass *dc = DEVICE_CLASS (klass); |
| 313 | ISADeviceClass *ic = ISA_DEVICE_CLASS (klass); |
Anthony Liguori | 8f04ee0 | 2011-12-04 11:52:49 -0600 | [diff] [blame] | 314 | ic->init = gus_initfn; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 315 | dc->desc = "Gravis Ultrasound GF1"; |
| 316 | dc->vmsd = &vmstate_gus; |
| 317 | dc->props = gus_properties; |
Anthony Liguori | 8f04ee0 | 2011-12-04 11:52:49 -0600 | [diff] [blame] | 318 | } |
| 319 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 320 | static TypeInfo gus_info = { |
| 321 | .name = "gus", |
| 322 | .parent = TYPE_ISA_DEVICE, |
| 323 | .instance_size = sizeof (GUSState), |
| 324 | .class_init = gus_class_initfn, |
Gerd Hoffmann | 9df3439 | 2009-09-10 11:43:32 +0200 | [diff] [blame] | 325 | }; |
| 326 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 327 | static void gus_register_types (void) |
Gerd Hoffmann | 9df3439 | 2009-09-10 11:43:32 +0200 | [diff] [blame] | 328 | { |
malc | cf4dc46 | 2012-02-07 22:11:04 +0400 | [diff] [blame] | 329 | type_register_static (&gus_info); |
Gerd Hoffmann | 9df3439 | 2009-09-10 11:43:32 +0200 | [diff] [blame] | 330 | } |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 331 | |
| 332 | type_init (gus_register_types) |