]> Git Repo - qemu.git/blame - hw/gus.c
c4231a: port to vmstate
[qemu.git] / hw / gus.c
CommitLineData
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 */
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
e2542fe2 38#ifdef HOST_WORDS_BIGENDIAN
423d65f4
AZ
39#define GUS_ENDIANNESS 1
40#else
41#define GUS_ENDIANNESS 0
42#endif
43
44#define IO_READ_PROTO(name) \
371bc573 45 static uint32_t name (void *opaque, uint32_t nport)
423d65f4 46#define IO_WRITE_PROTO(name) \
371bc573 47 static void name (void *opaque, uint32_t nport, uint32_t val)
423d65f4 48
423d65f4 49typedef struct GUSState {
9df34396 50 ISADevice dev;
423d65f4
AZ
51 GUSEmuState emu;
52 QEMUSoundCard card;
9df34396
GH
53 uint32_t freq;
54 uint32_t port;
423d65f4 55 int pos, left, shift, irqs;
731ba0ce 56 GUSsample *mixbuf;
423d65f4
AZ
57 uint8_t himem[1024 * 1024 + 32 + 4096];
58 int samples;
59 SWVoiceOut *voice;
60 int64_t last_ticks;
9df34396 61 qemu_irq pic;
423d65f4
AZ
62} GUSState;
63
64IO_READ_PROTO (gus_readb)
65{
66 GUSState *s = opaque;
67
68 return gus_read (&s->emu, nport, 1);
69}
70
71IO_READ_PROTO (gus_readw)
72{
73 GUSState *s = opaque;
74
75 return gus_read (&s->emu, nport, 2);
76}
77
78IO_WRITE_PROTO (gus_writeb)
79{
80 GUSState *s = opaque;
81
82 gus_write (&s->emu, nport, 1, val);
83}
84
85IO_WRITE_PROTO (gus_writew)
86{
87 GUSState *s = opaque;
88
89 gus_write (&s->emu, nport, 2, val);
90}
91
92static 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
123static 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
4f4cc0ef 159 reset:
160 gus_irqgen (&s->emu, muldiv64 (net, 1000000, s->freq));
423d65f4
AZ
161}
162
163int GUS_irqrequest (GUSEmuState *emu, int hwirq, int n)
164{
165 GUSState *s = emu->opaque;
9df34396
GH
166 /* qemu_irq_lower (s->pic); */
167 qemu_irq_raise (s->pic);
423d65f4
AZ
168 s->irqs += n;
169 ldebug ("irqrequest %d %d %d\n", hwirq, n, s->irqs);
170 return n;
171}
172
173void GUS_irqclear (GUSEmuState *emu, int hwirq)
174{
175 GUSState *s = emu->opaque;
176 ldebug ("irqclear %d %d\n", hwirq, s->irqs);
9df34396 177 qemu_irq_lower (s->pic);
423d65f4
AZ
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
186void 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
371bc573 193static int GUS_read_DMA (void *opaque, int nchan, int dma_pos, int dma_len)
423d65f4
AZ
194{
195 GUSState *s = opaque;
731ba0ce 196 char tmpbuf[4096];
423d65f4
AZ
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
5b5ef0db 218static void GUS_save (QEMUFile *f, void *opaque)
219{
5b5ef0db 220 GUSState *s = opaque;
221
db8baf14 222 qemu_put_be32 (f, s->pos);
223 qemu_put_be32 (f, s->left);
224 qemu_put_be32 (f, s->shift);
225 qemu_put_be32 (f, s->irqs);
226 qemu_put_be32 (f, s->samples);
227 qemu_put_be64 (f, s->last_ticks);
5b5ef0db 228 qemu_put_buffer (f, s->himem, sizeof (s->himem));
229}
230
231static int GUS_load (QEMUFile *f, void *opaque, int version_id)
232{
5b5ef0db 233 GUSState *s = opaque;
234
8210169a 235 if (version_id != 2)
5b5ef0db 236 return -EINVAL;
237
db8baf14 238 s->pos = qemu_get_be32 (f);
239 s->left = qemu_get_be32 (f);
240 s->shift = qemu_get_be32 (f);
241 s->irqs = qemu_get_be32 (f);
242 s->samples = qemu_get_be32 (f);
243 s->last_ticks = qemu_get_be64 (f);
5b5ef0db 244 qemu_get_buffer (f, s->himem, sizeof (s->himem));
245 return 0;
246}
247
9df34396 248static int gus_initfn (ISADevice *dev)
423d65f4 249{
9df34396 250 GUSState *s = DO_UPCAST(GUSState, dev, dev);
1ea879e5 251 struct audsettings as;
423d65f4 252
1a7dafce 253 AUD_register_card ("gus", &s->card);
423d65f4 254
9df34396 255 as.freq = s->freq;
423d65f4
AZ
256 as.nchannels = 2;
257 as.fmt = AUD_FMT_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);
423d65f4
AZ
271 return -1;
272 }
273
274 s->shift = 2;
275 s->samples = AUD_get_buffer_size_out (s->voice) >> s->shift;
276 s->mixbuf = qemu_mallocz (s->samples << s->shift);
423d65f4 277
9df34396
GH
278 register_ioport_write (s->port, 1, 1, gus_writeb, s);
279 register_ioport_write (s->port, 1, 2, gus_writew, s);
423d65f4 280
9df34396
GH
281 register_ioport_read ((s->port + 0x100) & 0xf00, 1, 1, gus_readb, s);
282 register_ioport_read ((s->port + 0x100) & 0xf00, 1, 2, gus_readw, s);
423d65f4 283
9df34396
GH
284 register_ioport_write (s->port + 6, 10, 1, gus_writeb, s);
285 register_ioport_write (s->port + 6, 10, 2, gus_writew, s);
286 register_ioport_read (s->port + 6, 10, 1, gus_readb, s);
287 register_ioport_read (s->port + 6, 10, 2, gus_readw, s);
423d65f4
AZ
288
289
9df34396
GH
290 register_ioport_write (s->port + 0x100, 8, 1, gus_writeb, s);
291 register_ioport_write (s->port + 0x100, 8, 2, gus_writew, s);
292 register_ioport_read (s->port + 0x100, 8, 1, gus_readb, s);
293 register_ioport_read (s->port + 0x100, 8, 2, gus_readw, s);
423d65f4 294
a5e8e46b 295 DMA_register_channel (s->emu.gusdma, GUS_read_DMA, s);
423d65f4
AZ
296 s->emu.himemaddr = s->himem;
297 s->emu.gusdatapos = s->emu.himemaddr + 1024 * 1024 + 32;
298 s->emu.opaque = s;
a5e8e46b 299 isa_init_irq (dev, &s->pic, s->emu.gusirq);
423d65f4
AZ
300
301 AUD_set_active_out (s->voice, 1);
5b5ef0db 302
8210169a 303 register_savevm ("gus", 0, 2, GUS_save, GUS_load, s);
423d65f4
AZ
304 return 0;
305}
9df34396
GH
306
307int GUS_init (qemu_irq *pic)
308{
a5e8e46b 309 isa_create_simple ("gus");
9df34396
GH
310 return 0;
311}
312
313static ISADeviceInfo gus_info = {
314 .qdev.name = "gus",
a5e8e46b 315 .qdev.desc = "Gravis Ultrasound GF1",
9df34396
GH
316 .qdev.size = sizeof (GUSState),
317 .init = gus_initfn,
318 .qdev.props = (Property[]) {
319 DEFINE_PROP_UINT32 ("freq", GUSState, freq, 44100),
320 DEFINE_PROP_HEX32 ("iobase", GUSState, port, 0x240),
321 DEFINE_PROP_UINT32 ("irq", GUSState, emu.gusirq, 7),
322 DEFINE_PROP_UINT32 ("dma", GUSState, emu.gusdma, 3),
323 DEFINE_PROP_END_OF_LIST (),
324 },
325};
326
a5e8e46b 327static void gus_register (void)
9df34396 328{
a5e8e46b 329 isa_qdev_register (&gus_info);
9df34396 330}
a5e8e46b 331device_init (gus_register)
This page took 0.331154 seconds and 4 git commands to generate.