2 * QEMU Soundblaster 16 emulation
4 * Copyright (c) 2003 Vassili Karpov (malc)
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
26 #define MIN(a, b) ((a)>(b)?(b):(a))
27 #define LENOFA(a) ((int) (sizeof(a)/sizeof(a[0])))
29 #define DEREF(x) (void)x
30 #define log(...) fprintf (stderr, "sb16: " __VA_ARGS__)
32 /* #define DEBUG_SB16 */
34 #define lwarn(...) fprintf (stderr, "sb16: " __VA_ARGS__)
35 #define linfo(...) fprintf (stderr, "sb16: " __VA_ARGS__)
36 #define ldebug(...) fprintf (stderr, "sb16: " __VA_ARGS__)
43 #define IO_READ_PROTO(name) \
44 uint32_t name (void *opaque, uint32_t nport)
45 #define IO_WRITE_PROTO(name) \
46 void name (void *opaque, uint32_t nport, uint32_t val)
56 } sb = {5, 4, 5, 1, 5, 0x220, -1};
58 static int mix_block, noirq;
60 typedef struct SB16State {
86 uint8_t mixer_regs[0x83];
89 /* XXX: suppress that and use a context */
90 static struct SB16State dsp;
92 static void log_dsp (SB16State *dsp)
94 linfo ("%c:%c:%d:%c:dmabuf=%d:pos=%d:freq=%d:timeconst=%d:speaker=%d\n",
95 dsp->fmt_stereo ? 'S' : 'M',
96 dsp->fmt_signed ? 'S' : 'U',
98 dsp->dma_auto ? 'a' : 's',
106 static void control (int hold)
108 linfo ("%d high %d\n", hold, dsp.use_hdma);
111 DMA_hold_DREQ (sb.hdma);
113 DMA_hold_DREQ (sb.dma);
117 DMA_release_DREQ (sb.hdma);
119 DMA_release_DREQ (sb.dma);
123 static void dma_cmd (uint8_t cmd, uint8_t d0, int dma_len)
128 dsp.use_hdma = cmd < 0xc0;
129 dsp.fifo = (cmd >> 1) & 1;
130 dsp.dma_auto = (cmd >> 2) & 1;
142 dsp.fmt_signed = (d0 >> 4) & 1;
143 dsp.fmt_stereo = (d0 >> 5) & 1;
145 if (-1 != dsp.time_const) {
148 tmp = 256 - dsp.time_const;
149 dsp.freq = (1000000 + (tmp / 2)) / tmp;
151 bps = 1 << (16 == dsp.fmt_bits);
154 dsp.dma_buffer_size = (dma_len + 1) * bps;
156 linfo ("frequency %d, stereo %d, signed %d, bits %d, size %d, auto %d\n",
157 dsp.freq, dsp.fmt_stereo, dsp.fmt_signed, dsp.fmt_bits,
158 dsp.dma_buffer_size, dsp.dma_auto);
160 if (16 == dsp.fmt_bits) {
161 if (dsp.fmt_signed) {
169 if (dsp.fmt_signed) {
178 dsp.left_till_irq = dsp.dma_buffer_size;
181 mix_block = sb.mix_block;
186 align = bps << dsp.fmt_stereo;
187 mix_block = ((dsp.freq * align) / 100) & ~(align - 1);
190 AUD_reset (dsp.freq, 1 << dsp.fmt_stereo, fmt);
195 static void command (SB16State *dsp, uint8_t cmd)
197 linfo ("%#x\n", cmd);
199 if (cmd > 0xaf && cmd < 0xd0) {
208 log("%#x wrong bits", cmd);
211 dsp->needed_bytes = 3;
218 /* IMS uses those when probing for sound devices */
222 dsp->needed_bytes = 1;
226 dsp->needed_bytes = 2;
227 dsp->dma_buffer_size = 0;
231 dsp->out_data[dsp->out_data_len++] = 0xff;
235 lwarn ("MIDI commands not implemented\n");
240 dsp->time_const = -1;
241 dsp->needed_bytes = 1;
247 dsp->time_const = -1;
248 dsp->needed_bytes = 2;
251 case 0x47: /* Continue Auto-Initialize DMA 16bit */
255 dsp->needed_bytes = 2;
258 case 0x27: /* ????????? */
272 if (dsp->fmt_signed) d0 |= 16;
273 if (dsp->fmt_stereo) d0 |= 32;
274 dma_cmd (cmd == 0x90 ? 0xc4 : 0xc0, d0, -1);
314 dsp->needed_bytes = 1;
318 dsp->out_data[dsp->out_data_len++] = sb.ver_lo;
319 dsp->out_data[dsp->out_data_len++] = sb.ver_hi;
323 dsp->out_data[dsp->out_data_len++] = 0xaa;
324 dsp->mixer_regs[0x82] |= dsp->mixer_regs[0x80];
325 pic_set_irq (sb.irq, 1);
329 log("%#x is unknown", cmd);
340 static void complete (SB16State *dsp)
342 linfo ("complete command %#x, in_index %d, needed_bytes %d\n",
343 dsp->cmd, dsp->in_index, dsp->needed_bytes);
345 if (dsp->cmd > 0xaf && dsp->cmd < 0xd0) {
348 d0 = dsp->in_data[0];
349 d1 = dsp->in_data[1];
350 d2 = dsp->in_data[2];
352 ldebug ("d0 = %d, d1 = %d, d2 = %d\n",
354 dma_cmd (dsp->cmd, d0, d1 + (d2 << 8));
368 d0 = dsp->in_data[0];
369 d1 = dsp->in_data[1];
371 save_left = dsp->left_till_irq;
372 save_pos = dsp->dma_pos;
373 dma_cmd (0xc0, 0, d0 + (d1 << 8));
374 dsp->left_till_irq = save_left;
375 dsp->dma_pos = save_pos;
377 linfo ("set buffer size data[%d, %d] %d pos %d\n",
378 d0, d1, dsp->dma_buffer_size, dsp->dma_pos);
383 dsp->time_const = dsp->in_data[0];
384 linfo ("set time const %d\n", dsp->time_const);
389 dsp->freq = dsp->in_data[1] + (dsp->in_data[0] << 8);
390 linfo ("set freq %#x, %#x = %d\n",
391 dsp->in_data[1], dsp->in_data[0], dsp->freq);
395 dsp->dma_buffer_size = dsp->in_data[1] + (dsp->in_data[0] << 8);
396 linfo ("set dma len %#x, %#x = %d\n",
397 dsp->in_data[1], dsp->in_data[0], dsp->dma_buffer_size);
401 dsp->out_data_len = 1;
402 linfo ("data = %#x\n", dsp->in_data[0]);
403 dsp->out_data[0] = dsp->in_data[0] ^ 0xff;
407 log ("unrecognized command %#x", dsp->cmd);
416 static IO_WRITE_PROTO (dsp_write)
418 SB16State *dsp = opaque;
421 iport = nport - sb.port;
427 else if ((1 == val) && (0 == dsp->v2x6)) {
429 dsp->out_data[dsp->out_data_len++] = 0xaa;
435 case 0xc: /* write data or command | write status */
436 if (0 == dsp->needed_bytes) {
438 if (0 == dsp->needed_bytes) {
443 dsp->in_data[dsp->in_index++] = val;
444 if (dsp->in_index == dsp->needed_bytes) {
445 dsp->needed_bytes = 0;
454 log ("(nport=%#x, val=%#x)", nport, val);
459 static IO_READ_PROTO (dsp_read)
461 SB16State *dsp = opaque;
464 iport = nport - sb.port;
468 case 0x6: /* reset */
471 case 0xa: /* read data */
472 if (dsp->out_data_len) {
473 retval = dsp->out_data[--dsp->out_data_len];
475 log("empty output buffer\n");
480 case 0xc: /* 0 can write */
484 case 0xd: /* timer interrupt clear */
485 log("timer interrupt clear\n");
488 case 0xe: /* data available status | irq 8 ack */
489 /* XXX drop pic irq line here? */
491 retval = (0 == dsp->out_data_len) ? 0 : 0x80;
492 dsp->mixer_regs[0x82] &= ~dsp->mixer_regs[0x80];
493 pic_set_irq (sb.irq, 0);
496 case 0xf: /* irq 16 ack */
497 /* XXX drop pic irq line here? */
500 dsp->mixer_regs[0x82] &= ~dsp->mixer_regs[0x80];
501 pic_set_irq (sb.irq, 0);
508 if ((0xc != iport) && (0xe != iport)) {
509 ldebug ("nport=%#x iport %#x = %#x\n",
510 nport, iport, retval);
519 static IO_WRITE_PROTO(mixer_write_indexb)
521 SB16State *dsp = opaque;
522 dsp->mixer_nreg = val & 0xff;
525 static IO_WRITE_PROTO(mixer_write_datab)
527 SB16State *dsp = opaque;
528 dsp->mixer_regs[dsp->mixer_nreg] = val;
531 static IO_WRITE_PROTO(mixer_write_indexw)
533 mixer_write_indexb (opaque, nport, val & 0xff);
534 mixer_write_datab (opaque, nport, (val >> 8) & 0xff);
537 static IO_READ_PROTO(mixer_read)
539 SB16State *dsp = opaque;
540 return dsp->mixer_regs[dsp->mixer_nreg];
545 if (0 == dsp.speaker)
551 static int write_audio (uint32_t addr, int len, int size)
554 uint8_t tmpbuf[4096];
565 left_till_end = len - dsp.dma_pos;
567 to_copy = MIN (temp, left_till_end);
568 if (to_copy > sizeof(tmpbuf))
569 to_copy = sizeof(tmpbuf);
570 cpu_physical_memory_read(addr + dsp.dma_pos, tmpbuf, to_copy);
571 copied = AUD_write (tmpbuf, to_copy);
574 dsp.dma_pos += copied;
576 if (dsp.dma_pos == len) {
582 if (copied != to_copy)
589 static int SB_read_DMA (void *opaque, target_ulong addr, int size)
591 SB16State *dsp = opaque;
592 int free, till, copy, written;
594 if (0 == dsp->speaker)
597 if (dsp->left_till_irq < 0) {
598 dsp->left_till_irq += dsp->dma_buffer_size;
602 free = AUD_get_free ();
604 if ((free <= 0) || (0 == size)) {
609 copy = MIN (free, mix_block);
615 till = dsp->left_till_irq;
617 ldebug ("addr:%#010x free:%d till:%d size:%d\n",
618 addr, free, till, size);
620 if (0 == dsp->dma_auto) {
625 written = write_audio (addr, size, copy);
626 dsp->left_till_irq -= written;
627 AUD_adjust_estimate (free - written);
629 if (dsp->left_till_irq <= 0) {
630 dsp->mixer_regs[0x82] |= dsp->mixer_regs[0x80];
632 ldebug ("request irq\n");
633 pic_set_irq(sb.irq, 1);
636 if (0 == dsp->dma_auto) {
641 ldebug ("pos %5d free %5d size %5d till % 5d copy %5d dma size %5d\n",
642 dsp->dma_pos, free, size, dsp->left_till_irq, copy,
643 dsp->dma_buffer_size);
645 if (dsp->left_till_irq <= 0) {
646 dsp->left_till_irq += dsp->dma_buffer_size;
652 static int magic_of_irq (int irq)
664 log ("bad irq %d\n", irq);
669 static int irq_of_magic (int magic)
681 log ("bad irq magic %d\n", magic);
686 void SB16_init (void)
690 static const uint8_t dsp_write_ports[] = {0x6, 0xc};
691 static const uint8_t dsp_read_ports[] = {0x6, 0xa, 0xc, 0xd, 0xe, 0xf};
693 s->mixer_regs[0x0e] = ~0;
694 s->mixer_regs[0x80] = magic_of_irq (sb.irq);
695 s->mixer_regs[0x81] = 0x20 | (sb.dma << 1);
697 DEREF (irq_of_magic);
699 for (i = 0x30; i < 0x48; i++) {
700 s->mixer_regs[i] = 0x20;
703 for (i = 0; i < LENOFA (dsp_write_ports); i++) {
704 register_ioport_write (sb.port + dsp_write_ports[i], 1, 1, dsp_write, s);
707 for (i = 0; i < LENOFA (dsp_read_ports); i++) {
708 register_ioport_read (sb.port + dsp_read_ports[i], 1, 1, dsp_read, s);
711 register_ioport_write (sb.port + 0x4, 1, 1, mixer_write_indexb, s);
712 register_ioport_write (sb.port + 0x4, 1, 2, mixer_write_indexw, s);
713 register_ioport_read (sb.port + 0x5, 1, 1, mixer_read, s);
714 register_ioport_write (sb.port + 0x5, 1, 1, mixer_write_datab, s);
716 DMA_register_channel (sb.hdma, SB_read_DMA, s);
717 DMA_register_channel (sb.dma, SB_read_DMA, s);