]>
git.proxmox.com Git - qemu.git/blob - hw/sb16.c
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
30 #define MIN(a, b) ((a)>(b)?(b):(a))
31 #define LENOFA(a) ((int) (sizeof(a)/sizeof(a[0])))
33 #define DEREF(x) (void)x
34 #define log(...) fprintf (stderr, "sb16: " __VA_ARGS__)
35 #define Fail(...) do { \
36 fprintf (stderr, "sb16: " __VA_ARGS__); \
41 #define lwarn(...) fprintf (stderr, "sb16: " __VA_ARGS__)
42 #define linfo(...) fprintf (stderr, "sb16: " __VA_ARGS__)
43 #define ldebug(...) fprintf (stderr, "sb16: " __VA_ARGS__)
50 #define IO_READ_PROTO(name) \
51 uint32_t name (struct CPUX86State *env, uint32_t nport)
52 #define IO_WRITE_PROTO(name) \
53 void name (struct CPUX86State *env, uint32_t nport, uint32_t val)
63 } sb
= {4, 5, 5, 1, 5, 0x220, -1};
65 static int mix_block
, noirq
;
99 static void log_dsp (const char *cap
)
103 linfo ("%c:%c:%d:%c:dmabuf=%d:pos=%d:freq=%d:timeconst=%d:speaker=%d\n",
104 dsp
.fmt_stereo
? 'S' : 'M',
105 dsp
.fmt_signed
? 'S' : 'U',
107 dsp
.dma_auto
? 'a' : 's',
115 static void control (int hold
)
117 linfo ("%d high %d\n", hold
, dsp
.use_hdma
);
120 DMA_hold_DREQ (sb
.hdma
);
122 DMA_hold_DREQ (sb
.dma
);
126 DMA_release_DREQ (sb
.hdma
);
128 DMA_release_DREQ (sb
.dma
);
132 static void dma_cmd (uint8_t cmd
, uint8_t d0
, int dma_len
)
137 dsp
.use_hdma
= cmd
< 0xc0;
138 dsp
.fifo
= (cmd
>> 1) & 1;
139 dsp
.dma_auto
= (cmd
>> 2) & 1;
151 dsp
.fmt_signed
= (d0
>> 4) & 1;
152 dsp
.fmt_stereo
= (d0
>> 5) & 1;
154 if (-1 != dsp
.time_const
) {
157 tmp
= 256 - dsp
.time_const
;
158 dsp
.freq
= (1000000 + (tmp
/ 2)) / tmp
;
160 bps
= 1 << (16 == dsp
.fmt_bits
);
163 dsp
.dma_buffer_size
= (dma_len
+ 1) * bps
;
165 linfo ("frequency %d, stereo %d, signed %d, bits %d, size %d, auto %d\n",
166 dsp
.freq
, dsp
.fmt_stereo
, dsp
.fmt_signed
, dsp
.fmt_bits
,
167 dsp
.dma_buffer_size
, dsp
.dma_auto
);
169 if (16 == dsp
.fmt_bits
) {
170 if (dsp
.fmt_signed
) {
178 if (dsp
.fmt_signed
) {
187 dsp
.left_till_irq
= dsp
.dma_buffer_size
;
190 mix_block
= sb
.mix_block
;
195 align
= bps
<< dsp
.fmt_stereo
;
196 mix_block
= ((dsp
.freq
* align
) / 100) & ~(align
- 1);
199 AUD_reset (dsp
.freq
, 1 << dsp
.fmt_stereo
, fmt
);
204 static void command (uint8_t cmd
)
210 linfo ("%#x\n", cmd
);
212 if (cmd
> 0xaf && cmd
< 0xd0) {
224 dsp
.needed_bytes
= 3;
229 dsp
.needed_bytes
= 1;
233 dsp
.needed_bytes
= 2;
234 dsp
.dma_buffer_size
= 0;
238 dsp
.out_data
[dsp
.out_data_len
++] = 0xff;
242 lwarn ("MIDI commands not implemented\n");
248 dsp
.needed_bytes
= 1;
255 dsp
.needed_bytes
= 2;
258 case 0x47: /* Continue Auto-Initialize DMA 16bit */
262 dsp
.needed_bytes
= 2;
265 case 0x27: /* ????????? */
279 if (dsp
.fmt_signed
) d0
|= 16;
280 if (dsp
.fmt_stereo
) d0
|= 32;
281 dma_cmd (cmd
== 0x90 ? 0xc4 : 0xc0, d0
, -1);
321 dsp
.needed_bytes
= 1;
325 dsp
.out_data
[dsp
.out_data_len
++] = sb
.ver_lo
;
326 dsp
.out_data
[dsp
.out_data_len
++] = sb
.ver_hi
;
330 dsp
.out_data
[dsp
.out_data_len
++] = 0xaa;
331 mixer
.regs
[0x82] |= 1;
332 pic_set_irq (sb
.irq
, 1);
344 Fail ("%#x %s", cmd
, msg
);
348 static void complete (void)
350 linfo ("complete command %#x, in_index %d, needed_bytes %d\n",
351 dsp
.cmd
, dsp
.in_index
, dsp
.needed_bytes
);
353 if (dsp
.cmd
> 0xaf && dsp
.cmd
< 0xd0) {
360 ldebug ("d0 = %d, d1 = %d, d2 = %d\n",
362 dma_cmd (dsp
.cmd
, d0
, d1
+ (d2
<< 8));
379 save_left
= dsp
.left_till_irq
;
380 save_pos
= dsp
.dma_pos
;
381 dma_cmd (0xc0, 0, d0
+ (d1
<< 8));
382 dsp
.left_till_irq
= save_left
;
383 dsp
.dma_pos
= save_pos
;
385 linfo ("set buffer size data[%d, %d] %d pos %d\n",
386 d0
, d1
, dsp
.dma_buffer_size
, dsp
.dma_pos
);
391 dsp
.time_const
= dsp
.in_data
[0];
392 linfo ("set time const %d\n", dsp
.time_const
);
397 dsp
.freq
= dsp
.in_data
[1] + (dsp
.in_data
[0] << 8);
398 linfo ("set freq %#x, %#x = %d\n",
399 dsp
.in_data
[1], dsp
.in_data
[0], dsp
.freq
);
403 dsp
.dma_buffer_size
= dsp
.in_data
[1] + (dsp
.in_data
[0] << 8);
404 linfo ("set dma len %#x, %#x = %d\n",
405 dsp
.in_data
[1], dsp
.in_data
[0], dsp
.dma_buffer_size
);
409 dsp
.out_data_len
= 1;
410 linfo ("data = %#x\n", dsp
.in_data
[0]);
411 dsp
.out_data
[0] = dsp
.in_data
[0] ^ 0xff;
423 Fail ("unrecognized command %#x", dsp
.cmd
);
426 static IO_WRITE_PROTO (dsp_write
)
430 iport
= nport
- sb
.port
;
436 else if ((1 == val
) && (0 == dsp
.v2x6
)) {
438 dsp
.out_data
[dsp
.out_data_len
++] = 0xaa;
444 case 0xc: /* write data or command | write status */
445 if (0 == dsp
.needed_bytes
) {
447 if (0 == dsp
.needed_bytes
) {
452 dsp
.in_data
[dsp
.in_index
++] = val
;
453 if (dsp
.in_index
== dsp
.needed_bytes
) {
454 dsp
.needed_bytes
= 0;
463 Fail ("(nport=%#x, val=%#x)", nport
, val
);
467 static IO_READ_PROTO (dsp_read
)
473 iport
= nport
- sb
.port
;
477 case 0x6: /* reset */
480 case 0xa: /* read data */
481 if (dsp
.out_data_len
) {
482 retval
= dsp
.out_data
[--dsp
.out_data_len
];
486 lwarn ("empty output buffer\n");
489 msg
= "empty output buffer";
495 case 0xc: /* 0 can write */
499 case 0xd: /* timer interrupt clear */
502 case 0xe: /* data available status | irq 8 ack */
503 retval
= (0 == dsp
.out_data_len
) ? 0 : 0x80;
506 case 0xf: /* irq 16 ack */
508 mixer
.regs
[0x82] &= ~2;
516 if ((0xc != iport
) && (0xe != iport
)) {
517 ldebug ("(nport=%#x, size=%d) iport %#x = %#x\n",
518 nport
, size
, iport
, retval
);
524 Fail ("(nport=%#x) %s", nport
, msg
);
527 static IO_WRITE_PROTO(mixer_write_indexb
)
529 mixer
.nreg
= val
& 0xff;
532 static IO_WRITE_PROTO(mixer_write_datab
)
534 mixer
.regs
[mixer
.nreg
] = val
;
537 static IO_WRITE_PROTO(mixer_write_indexw
)
539 mixer_write_indexb (env
, nport
, val
& 0xff);
540 mixer_write_datab (env
, nport
, (val
>> 8) & 0xff);
543 static IO_READ_PROTO(mixer_read
)
545 return mixer
.regs
[mixer
.nreg
];
550 if (0 == dsp
.speaker
)
556 static int write_audio (uint32_t addr
, int len
, int size
)
569 left_till_end
= len
- dsp
.dma_pos
;
571 to_copy
= MIN (temp
, left_till_end
);
573 copied
= AUD_write ((void *) (addr
+ dsp
.dma_pos
), to_copy
);
576 dsp
.dma_pos
+= copied
;
578 if (dsp
.dma_pos
== len
) {
584 if (copied
!= to_copy
)
591 static int SB_read_DMA (uint32_t addr
, int size
, int *_irq
)
593 int free
, till
, copy
, written
;
595 if (0 == dsp
.speaker
)
598 if (dsp
.left_till_irq
< 0) {
599 dsp
.left_till_irq
+= dsp
.dma_buffer_size
;
603 free
= AUD_get_free ();
605 if ((free
<= 0) || (0 == size
)) {
610 copy
= MIN (free
, mix_block
);
616 till
= dsp
.left_till_irq
;
618 ldebug ("addr:%#010x free:%d till:%d size:%d\n",
619 addr
, free
, till
, size
);
620 /* linfo ("pos %d free %d size %d till %d copy %d auto %d noirq %d\n", */
621 /* dsp.dma_pos, free, size, till, copy, dsp.dma_auto, noirq); */
623 if (0 == dsp
.dma_auto
) {
628 written
= write_audio (addr
, size
, copy
);
629 dsp
.left_till_irq
-= written
;
630 AUD_adjust_estimate (free
- written
);
632 if (dsp
.left_till_irq
<= 0) {
633 mixer
.regs
[0x82] |= mixer
.regs
[0x80];
637 if (0 == dsp
.dma_auto
) {
642 ldebug ("pos %5d free %5d size %5d till % 5d copy %5d dma size %5d\n",
643 dsp
.dma_pos
, free
, size
, dsp
.left_till_irq
, copy
,
644 dsp
.dma_buffer_size
);
646 if (dsp
.left_till_irq
<= 0) {
647 dsp
.left_till_irq
+= dsp
.dma_buffer_size
;
653 static int dma_misc_handler (int moo
)
658 static int magic_of_irq (int irq
)
670 log ("bad irq %d\n", irq
);
675 static int irq_of_magic (int magic
)
687 log ("bad irq magic %d\n", magic
);
692 void SB16_init (void)
695 static const uint8_t dsp_write_ports
[] = {0x6, 0xc};
696 static const uint8_t dsp_read_ports
[] = {0x6, 0xa, 0xc, 0xd, 0xe, 0xf};
698 mixer
.regs
[0x0e] = ~0;
699 mixer
.regs
[0x80] = magic_of_irq (sb
.irq
);
700 mixer
.regs
[0x81] = 0x20 | (sb
.dma
<< 1);
702 DEREF (irq_of_magic
);
704 for (i
= 0x30; i
< 0x48; i
++) {
705 mixer
.regs
[i
] = 0x20;
708 for (i
= 0; i
< LENOFA (dsp_write_ports
); i
++) {
709 register_ioport_write (sb
.port
+ dsp_write_ports
[i
], 1, dsp_write
, 1);
712 for (i
= 0; i
< LENOFA (dsp_read_ports
); i
++) {
713 register_ioport_read (sb
.port
+ dsp_read_ports
[i
], 1, dsp_read
, 1);
716 register_ioport_write (sb
.port
+ 0x4, 1, mixer_write_indexb
, 1);
717 register_ioport_write (sb
.port
+ 0x4, 1, mixer_write_indexw
, 2);
718 register_ioport_read (sb
.port
+ 0x5, 1, mixer_read
, 1);
719 register_ioport_write (sb
.port
+ 0x5, 1, mixer_write_datab
, 1);
721 DMA_register_channel (sb
.hdma
, SB_read_DMA
, dma_misc_handler
);
722 DMA_register_channel (sb
.dma
, SB_read_DMA
, dma_misc_handler
);