gus.c 8.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * QEMU Proxy for Gravis Ultrasound GF1 emulation by Tibor "TS" Schütz
 *
 * Copyright (c) 2002-2005 Vassili Karpov (malc)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "hw.h"
#include "audiodev.h"
#include "audio/audio.h"
#include "isa.h"
#include "gusemu.h"
#include "gustate.h"

#define dolog(...) AUD_log ("audio", __VA_ARGS__)
#ifdef DEBUG
#define ldebug(...) dolog (__VA_ARGS__)
#else
#define ldebug(...)
#endif

38
#ifdef HOST_WORDS_BIGENDIAN
39 40 41 42 43 44
#define GUS_ENDIANNESS 1
#else
#define GUS_ENDIANNESS 0
#endif

#define IO_READ_PROTO(name) \
M
malc 已提交
45
    static uint32_t name (void *opaque, uint32_t nport)
46
#define IO_WRITE_PROTO(name) \
M
malc 已提交
47
    static void name (void *opaque, uint32_t nport, uint32_t val)
48 49

typedef struct GUSState {
50
    ISADevice dev;
51 52
    GUSEmuState emu;
    QEMUSoundCard card;
53 54
    uint32_t freq;
    uint32_t port;
55
    int pos, left, shift, irqs;
56
    GUSsample *mixbuf;
57 58 59 60
    uint8_t himem[1024 * 1024 + 32 + 4096];
    int samples;
    SWVoiceOut *voice;
    int64_t last_ticks;
61
    qemu_irq pic;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
} GUSState;

IO_READ_PROTO (gus_readb)
{
    GUSState *s = opaque;

    return gus_read (&s->emu, nport, 1);
}

IO_READ_PROTO (gus_readw)
{
    GUSState *s = opaque;

    return gus_read (&s->emu, nport, 2);
}

IO_WRITE_PROTO (gus_writeb)
{
    GUSState *s = opaque;

    gus_write (&s->emu, nport, 1, val);
}

IO_WRITE_PROTO (gus_writew)
{
    GUSState *s = opaque;

    gus_write (&s->emu, nport, 2, val);
}

static int write_audio (GUSState *s, int samples)
{
    int net = 0;
    int pos = s->pos;

    while (samples) {
        int nbytes, wbytes, wsampl;

        nbytes = samples << s->shift;
        wbytes = AUD_write (
            s->voice,
            s->mixbuf + (pos << (s->shift - 1)),
            nbytes
            );

        if (wbytes) {
            wsampl = wbytes >> s->shift;

            samples -= wsampl;
            pos = (pos + wsampl) % s->samples;

            net += wsampl;
        }
        else {
            break;
        }
    }

    return net;
}

static void GUS_callback (void *opaque, int free)
{
    int samples, to_play, net = 0;
    GUSState *s = opaque;

    samples = free >> s->shift;
    to_play = audio_MIN (samples, s->left);

    while (to_play) {
        int written = write_audio (s, to_play);

        if (!written) {
            goto reset;
        }

        s->left -= written;
        to_play -= written;
        samples -= written;
        net += written;
    }

    samples = audio_MIN (samples, s->samples);
    if (samples) {
        gus_mixvoices (&s->emu, s->freq, samples, s->mixbuf);

        while (samples) {
            int written = write_audio (s, samples);
            if (!written) {
                break;
            }
            samples -= written;
            net += written;
        }
    }
    s->left = samples;

M
malc 已提交
159 160
 reset:
    gus_irqgen (&s->emu, muldiv64 (net, 1000000, s->freq));
161 162 163 164 165
}

int GUS_irqrequest (GUSEmuState *emu, int hwirq, int n)
{
    GUSState *s = emu->opaque;
166 167
    /* qemu_irq_lower (s->pic); */
    qemu_irq_raise (s->pic);
168 169 170 171 172 173 174 175 176
    s->irqs += n;
    ldebug ("irqrequest %d %d %d\n", hwirq, n, s->irqs);
    return n;
}

void GUS_irqclear (GUSEmuState *emu, int hwirq)
{
    GUSState *s = emu->opaque;
    ldebug ("irqclear %d %d\n", hwirq, s->irqs);
177
    qemu_irq_lower (s->pic);
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    s->irqs -= 1;
#ifdef IRQ_STORM
    if (s->irqs > 0) {
        qemu_irq_raise (s->pic[hwirq]);
    }
#endif
}

void GUS_dmarequest (GUSEmuState *der)
{
    /* GUSState *s = (GUSState *) der; */
    ldebug ("dma request %d\n", der->gusdma);
    DMA_hold_DREQ (der->gusdma);
}

M
malc 已提交
193
static int GUS_read_DMA (void *opaque, int nchan, int dma_pos, int dma_len)
194 195
{
    GUSState *s = opaque;
196
    char tmpbuf[4096];
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    int pos = dma_pos, mode, left = dma_len - dma_pos;

    ldebug ("read DMA %#x %d\n", dma_pos, dma_len);
    mode = DMA_get_channel_mode (s->emu.gusdma);
    while (left) {
        int to_copy = audio_MIN ((size_t) left, sizeof (tmpbuf));
        int copied;

        ldebug ("left=%d to_copy=%d pos=%d\n", left, to_copy, pos);
        copied = DMA_read_memory (nchan, tmpbuf, pos, to_copy);
        gus_dma_transferdata (&s->emu, tmpbuf, copied, left == copied);
        left -= copied;
        pos += copied;
    }

    if (0 == ((mode >> 4) & 1)) {
        DMA_release_DREQ (s->emu.gusdma);
    }
    return dma_len;
}

J
Juan Quintela 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
static const VMStateDescription vmstate_gus = {
    .name = "gus",
    .version_id = 2,
    .minimum_version_id = 2,
    .minimum_version_id_old = 2,
    .fields      = (VMStateField []) {
        VMSTATE_INT32(pos, GUSState),
        VMSTATE_INT32(left, GUSState),
        VMSTATE_INT32(shift, GUSState),
        VMSTATE_INT32(irqs, GUSState),
        VMSTATE_INT32(samples, GUSState),
        VMSTATE_INT64(last_ticks, GUSState),
        VMSTATE_BUFFER(himem, GUSState),
        VMSTATE_END_OF_LIST()
    }
};
M
malc 已提交
234

235
static int gus_initfn (ISADevice *dev)
236
{
M
malc 已提交
237
    GUSState *s = DO_UPCAST (GUSState, dev, dev);
M
malc 已提交
238
    struct audsettings as;
239

240
    AUD_register_card ("gus", &s->card);
241

242
    as.freq = s->freq;
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    as.nchannels = 2;
    as.fmt = AUD_FMT_S16;
    as.endianness = GUS_ENDIANNESS;

    s->voice = AUD_open_out (
        &s->card,
        NULL,
        "gus",
        s,
        GUS_callback,
        &as
        );

    if (!s->voice) {
        AUD_remove_card (&s->card);
        return -1;
    }

    s->shift = 2;
    s->samples = AUD_get_buffer_size_out (s->voice) >> s->shift;
263
    s->mixbuf = g_malloc0 (s->samples << s->shift);
264

265 266
    register_ioport_write (s->port, 1, 1, gus_writeb, s);
    register_ioport_write (s->port, 1, 2, gus_writew, s);
M
malc 已提交
267
    isa_init_ioport_range (dev, s->port, 2);
268

269 270
    register_ioport_read ((s->port + 0x100) & 0xf00, 1, 1, gus_readb, s);
    register_ioport_read ((s->port + 0x100) & 0xf00, 1, 2, gus_readw, s);
M
malc 已提交
271
    isa_init_ioport_range (dev, (s->port + 0x100) & 0xf00, 2);
272

273 274 275 276
    register_ioport_write (s->port + 6, 10, 1, gus_writeb, s);
    register_ioport_write (s->port + 6, 10, 2, gus_writew, s);
    register_ioport_read (s->port + 6, 10, 1, gus_readb, s);
    register_ioport_read (s->port + 6, 10, 2, gus_readw, s);
M
malc 已提交
277
    isa_init_ioport_range (dev, s->port + 6, 10);
278

279 280 281 282
    register_ioport_write (s->port + 0x100, 8, 1, gus_writeb, s);
    register_ioport_write (s->port + 0x100, 8, 2, gus_writew, s);
    register_ioport_read (s->port + 0x100, 8, 1, gus_readb, s);
    register_ioport_read (s->port + 0x100, 8, 2, gus_readw, s);
M
malc 已提交
283
    isa_init_ioport_range (dev, s->port + 0x100, 8);
284

285
    DMA_register_channel (s->emu.gusdma, GUS_read_DMA, s);
286 287 288
    s->emu.himemaddr = s->himem;
    s->emu.gusdatapos = s->emu.himemaddr + 1024 * 1024 + 32;
    s->emu.opaque = s;
289
    isa_init_irq (dev, &s->pic, s->emu.gusirq);
290 291

    AUD_set_active_out (s->voice, 1);
M
malc 已提交
292

293 294
    return 0;
}
295 296 297

int GUS_init (qemu_irq *pic)
{
298
    isa_create_simple ("gus");
299 300 301 302 303
    return 0;
}

static ISADeviceInfo gus_info = {
    .qdev.name     = "gus",
304
    .qdev.desc     = "Gravis Ultrasound GF1",
305
    .qdev.size     = sizeof (GUSState),
306
    .qdev.vmsd     = &vmstate_gus,
307 308 309 310 311 312 313 314 315 316
    .init          = gus_initfn,
    .qdev.props    = (Property[]) {
        DEFINE_PROP_UINT32 ("freq",    GUSState, freq,        44100),
        DEFINE_PROP_HEX32  ("iobase",  GUSState, port,        0x240),
        DEFINE_PROP_UINT32 ("irq",     GUSState, emu.gusirq,  7),
        DEFINE_PROP_UINT32 ("dma",     GUSState, emu.gusdma,  3),
        DEFINE_PROP_END_OF_LIST (),
    },
};

317
static void gus_register (void)
318
{
319
    isa_qdev_register (&gus_info);
320
}
321
device_init (gus_register)