2 * QEMU Soundblaster 16 emulation
4 * Copyright (c) 2003-2005 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
24 #include "qemu/osdep.h"
26 #include "hw/audio/soundhw.h"
27 #include "audio/audio.h"
28 #include "hw/isa/isa.h"
30 #include "qemu/timer.h"
31 #include "qemu/host-utils.h"
33 #include "qapi/error.h"
35 #define dolog(...) AUD_log ("sb16", __VA_ARGS__)
38 /* #define DEBUG_SB16_MOST */
41 #define ldebug(...) dolog (__VA_ARGS__)
46 static const char e3
[] = "COPYRIGHT (C) CREATIVE TECHNOLOGY LTD, 1992.";
48 #define TYPE_SB16 "sb16"
49 #define SB16(obj) OBJECT_CHECK (SB16State, (obj), TYPE_SB16)
51 typedef struct SB16State
{
87 uint8_t csp_regs
[256];
96 uint8_t last_read_byte
;
102 int bytes_per_second
;
110 uint8_t mixer_regs
[256];
111 PortioList portio_list
;
114 static void SB_audio_callback (void *opaque
, int free
);
116 static int magic_of_irq (int irq
)
128 qemu_log_mask(LOG_GUEST_ERROR
, "bad irq %d\n", irq
);
133 static int irq_of_magic (int magic
)
145 qemu_log_mask(LOG_GUEST_ERROR
, "bad irq magic %d\n", magic
);
151 static void log_dsp (SB16State
*dsp
)
153 ldebug ("%s:%s:%d:%s:dmasize=%d:freq=%d:const=%d:speaker=%d\n",
154 dsp
->fmt_stereo
? "Stereo" : "Mono",
155 dsp
->fmt_signed
? "Signed" : "Unsigned",
157 dsp
->dma_auto
? "Auto" : "Single",
165 static void speaker (SB16State
*s
, int on
)
168 /* AUD_enable (s->voice, on); */
171 static void control (SB16State
*s
, int hold
)
173 int dma
= s
->use_hdma
? s
->hdma
: s
->dma
;
174 IsaDma
*isa_dma
= s
->use_hdma
? s
->isa_hdma
: s
->isa_dma
;
175 IsaDmaClass
*k
= ISADMA_GET_CLASS(isa_dma
);
176 s
->dma_running
= hold
;
178 ldebug ("hold %d high %d dma %d\n", hold
, s
->use_hdma
, dma
);
181 k
->hold_DREQ(isa_dma
, dma
);
182 AUD_set_active_out (s
->voice
, 1);
185 k
->release_DREQ(isa_dma
, dma
);
186 AUD_set_active_out (s
->voice
, 0);
190 static void aux_timer (void *opaque
)
192 SB16State
*s
= opaque
;
194 qemu_irq_raise (s
->pic
);
200 static void continue_dma8 (SB16State
*s
)
203 struct audsettings as
;
208 as
.nchannels
= 1 << s
->fmt_stereo
;
212 s
->voice
= AUD_open_out (
225 static void dma_cmd8 (SB16State
*s
, int mask
, int dma_len
)
231 s
->fmt_stereo
= (s
->mixer_regs
[0x0e] & 2) != 0;
232 if (-1 == s
->time_const
) {
237 int tmp
= (256 - s
->time_const
);
238 s
->freq
= (1000000 + (tmp
/ 2)) / tmp
;
242 s
->block_size
= dma_len
<< s
->fmt_stereo
;
245 /* This is apparently the only way to make both Act1/PL
246 and SecondReality/FC work
248 Act1 sets block size via command 0x48 and it's an odd number
249 SR does the same with even number
250 Both use stereo, and Creatives own documentation states that
251 0x48 sets block size in bytes less one.. go figure */
252 s
->block_size
&= ~s
->fmt_stereo
;
255 s
->freq
>>= s
->fmt_stereo
;
256 s
->left_till_irq
= s
->block_size
;
257 s
->bytes_per_second
= (s
->freq
<< s
->fmt_stereo
);
258 /* s->highspeed = (mask & DMA8_HIGH) != 0; */
259 s
->dma_auto
= (mask
& DMA8_AUTO
) != 0;
260 s
->align
= (1 << s
->fmt_stereo
) - 1;
262 if (s
->block_size
& s
->align
) {
263 qemu_log_mask(LOG_GUEST_ERROR
, "warning: misaligned block size %d,"
264 " alignment %d\n", s
->block_size
, s
->align
+ 1);
267 ldebug ("freq %d, stereo %d, sign %d, bits %d, "
268 "dma %d, auto %d, fifo %d, high %d\n",
269 s
->freq
, s
->fmt_stereo
, s
->fmt_signed
, s
->fmt_bits
,
270 s
->block_size
, s
->dma_auto
, s
->fifo
, s
->highspeed
);
276 static void dma_cmd (SB16State
*s
, uint8_t cmd
, uint8_t d0
, int dma_len
)
278 s
->use_hdma
= cmd
< 0xc0;
279 s
->fifo
= (cmd
>> 1) & 1;
280 s
->dma_auto
= (cmd
>> 2) & 1;
281 s
->fmt_signed
= (d0
>> 4) & 1;
282 s
->fmt_stereo
= (d0
>> 5) & 1;
294 if (-1 != s
->time_const
) {
296 int tmp
= 256 - s
->time_const
;
297 s
->freq
= (1000000 + (tmp
/ 2)) / tmp
;
299 /* s->freq = 1000000 / ((255 - s->time_const) << s->fmt_stereo); */
300 s
->freq
= 1000000 / ((255 - s
->time_const
));
305 s
->block_size
= dma_len
+ 1;
306 s
->block_size
<<= (s
->fmt_bits
== 16);
308 /* It is clear that for DOOM and auto-init this value
309 shouldn't take stereo into account, while Miles Sound Systems
310 setsound.exe with single transfer mode wouldn't work without it
311 wonders of SB16 yet again */
312 s
->block_size
<<= s
->fmt_stereo
;
315 ldebug ("freq %d, stereo %d, sign %d, bits %d, "
316 "dma %d, auto %d, fifo %d, high %d\n",
317 s
->freq
, s
->fmt_stereo
, s
->fmt_signed
, s
->fmt_bits
,
318 s
->block_size
, s
->dma_auto
, s
->fifo
, s
->highspeed
);
320 if (16 == s
->fmt_bits
) {
322 s
->fmt
= AUD_FMT_S16
;
325 s
->fmt
= AUD_FMT_U16
;
337 s
->left_till_irq
= s
->block_size
;
339 s
->bytes_per_second
= (s
->freq
<< s
->fmt_stereo
) << (s
->fmt_bits
== 16);
341 s
->align
= (1 << (s
->fmt_stereo
+ (s
->fmt_bits
== 16))) - 1;
342 if (s
->block_size
& s
->align
) {
343 qemu_log_mask(LOG_GUEST_ERROR
, "warning: misaligned block size %d,"
344 " alignment %d\n", s
->block_size
, s
->align
+ 1);
348 struct audsettings as
;
353 as
.nchannels
= 1 << s
->fmt_stereo
;
357 s
->voice
= AUD_open_out (
371 static inline void dsp_out_data (SB16State
*s
, uint8_t val
)
373 ldebug ("outdata %#x\n", val
);
374 if ((size_t) s
->out_data_len
< sizeof (s
->out_data
)) {
375 s
->out_data
[s
->out_data_len
++] = val
;
379 static inline uint8_t dsp_get_data (SB16State
*s
)
382 return s
->in2_data
[--s
->in_index
];
385 dolog ("buffer underflow\n");
390 static void command (SB16State
*s
, uint8_t cmd
)
392 ldebug ("command %#x\n", cmd
);
394 if (cmd
> 0xaf && cmd
< 0xd0) {
396 qemu_log_mask(LOG_UNIMP
, "ADC not yet supported (command %#x)\n",
405 qemu_log_mask(LOG_GUEST_ERROR
, "%#x wrong bits\n", cmd
);
414 dsp_out_data (s
, 0x10); /* s->csp_param); */
426 /* __asm__ ("int3"); */
434 dsp_out_data (s
, 0xf8);
450 case 0x1c: /* Auto-Initialize DMA DAC, 8-bit */
451 dma_cmd8 (s
, DMA8_AUTO
, -1);
454 case 0x20: /* Direct ADC, Juice/PL */
455 dsp_out_data (s
, 0xff);
459 qemu_log_mask(LOG_UNIMP
, "0x35 - MIDI command not implemented\n");
481 dsp_out_data (s
, 0xaa);
484 case 0x47: /* Continue Auto-Initialize DMA 16bit */
492 s
->needed_bytes
= 2; /* DMA DAC, 4-bit ADPCM */
493 qemu_log_mask(LOG_UNIMP
, "0x75 - DMA DAC, 4-bit ADPCM not"
497 case 0x75: /* DMA DAC, 4-bit ADPCM Reference */
499 qemu_log_mask(LOG_UNIMP
, "0x74 - DMA DAC, 4-bit ADPCM Reference not"
503 case 0x76: /* DMA DAC, 2.6-bit ADPCM */
505 qemu_log_mask(LOG_UNIMP
, "0x74 - DMA DAC, 2.6-bit ADPCM not"
509 case 0x77: /* DMA DAC, 2.6-bit ADPCM Reference */
511 qemu_log_mask(LOG_UNIMP
, "0x74 - DMA DAC, 2.6-bit ADPCM Reference"
512 " not implemented\n");
516 qemu_log_mask(LOG_UNIMP
, "0x7d - Autio-Initialize DMA DAC, 4-bit"
517 " ADPCM Reference\n");
518 qemu_log_mask(LOG_UNIMP
, "not implemented\n");
522 qemu_log_mask(LOG_UNIMP
, "0x7d - Autio-Initialize DMA DAC, 2.6-bit"
523 " ADPCM Reference\n");
524 qemu_log_mask(LOG_UNIMP
, "not implemented\n");
533 dma_cmd8 (s
, ((cmd
& 1) == 0) | DMA8_HIGH
, -1);
536 case 0xd0: /* halt DMA operation. 8bit */
540 case 0xd1: /* speaker on */
544 case 0xd3: /* speaker off */
548 case 0xd4: /* continue DMA operation. 8bit */
549 /* KQ6 (or maybe Sierras audblst.drv in general) resets
550 the frequency between halt/continue */
554 case 0xd5: /* halt DMA operation. 16bit */
558 case 0xd6: /* continue DMA operation. 16bit */
562 case 0xd9: /* exit auto-init DMA after this block. 16bit */
566 case 0xda: /* exit auto-init DMA after this block. 8bit */
570 case 0xe0: /* DSP identification */
575 dsp_out_data (s
, s
->ver
& 0xff);
576 dsp_out_data (s
, s
->ver
>> 8);
586 for (i
= sizeof (e3
) - 1; i
>= 0; --i
)
587 dsp_out_data (s
, e3
[i
]);
591 case 0xe4: /* write test reg */
596 qemu_log_mask(LOG_UNIMP
, "Attempt to probe for ESS (0xe7)?\n");
599 case 0xe8: /* read test reg */
600 dsp_out_data (s
, s
->test_reg
);
605 dsp_out_data (s
, 0xaa);
606 s
->mixer_regs
[0x82] |= (cmd
== 0xf2) ? 1 : 2;
607 qemu_irq_raise (s
->pic
);
618 case 0xfc: /* FIXME */
623 qemu_log_mask(LOG_UNIMP
, "Unrecognized command %#x\n", cmd
);
628 if (!s
->needed_bytes
) {
633 if (!s
->needed_bytes
) {
642 qemu_log_mask(LOG_UNIMP
, "warning: command %#x,%d is not truly understood"
643 " yet\n", cmd
, s
->needed_bytes
);
648 static uint16_t dsp_get_lohi (SB16State
*s
)
650 uint8_t hi
= dsp_get_data (s
);
651 uint8_t lo
= dsp_get_data (s
);
652 return (hi
<< 8) | lo
;
655 static uint16_t dsp_get_hilo (SB16State
*s
)
657 uint8_t lo
= dsp_get_data (s
);
658 uint8_t hi
= dsp_get_data (s
);
659 return (hi
<< 8) | lo
;
662 static void complete (SB16State
*s
)
665 ldebug ("complete command %#x, in_index %d, needed_bytes %d\n",
666 s
->cmd
, s
->in_index
, s
->needed_bytes
);
668 if (s
->cmd
> 0xaf && s
->cmd
< 0xd0) {
669 d2
= dsp_get_data (s
);
670 d1
= dsp_get_data (s
);
671 d0
= dsp_get_data (s
);
674 dolog ("ADC params cmd = %#x d0 = %d, d1 = %d, d2 = %d\n",
678 ldebug ("cmd = %#x d0 = %d, d1 = %d, d2 = %d\n",
680 dma_cmd (s
, s
->cmd
, d0
, d1
+ (d2
<< 8));
686 s
->csp_mode
= dsp_get_data (s
);
689 ldebug ("CSP command 0x04: mode=%#x\n", s
->csp_mode
);
693 s
->csp_param
= dsp_get_data (s
);
694 s
->csp_value
= dsp_get_data (s
);
695 ldebug ("CSP command 0x05: param=%#x value=%#x\n",
701 d0
= dsp_get_data (s
);
702 d1
= dsp_get_data (s
);
703 ldebug ("write CSP register %d <- %#x\n", d1
, d0
);
705 ldebug ("0x83[%d] <- %#x\n", s
->csp_reg83r
, d0
);
706 s
->csp_reg83
[s
->csp_reg83r
% 4] = d0
;
710 s
->csp_regs
[d1
] = d0
;
715 d0
= dsp_get_data (s
);
716 ldebug ("read CSP register %#x -> %#x, mode=%#x\n",
717 d0
, s
->csp_regs
[d0
], s
->csp_mode
);
719 ldebug ("0x83[%d] -> %#x\n",
721 s
->csp_reg83
[s
->csp_reg83w
% 4]);
722 dsp_out_data (s
, s
->csp_reg83
[s
->csp_reg83w
% 4]);
726 dsp_out_data (s
, s
->csp_regs
[d0
]);
731 d0
= dsp_get_data (s
);
732 dolog ("cmd 0x10 d0=%#x\n", d0
);
736 dma_cmd8 (s
, 0, dsp_get_lohi (s
) + 1);
740 s
->time_const
= dsp_get_data (s
);
741 ldebug ("set time const %d\n", s
->time_const
);
747 * 0x41 is documented as setting the output sample rate,
748 * and 0x42 the input sample rate, but in fact SB16 hardware
749 * seems to have only a single sample rate under the hood,
750 * and FT2 sets output freq with this (go figure). Compare:
751 * http://homepages.cae.wisc.edu/~brodskye/sb16doc/sb16doc.html#SamplingRate
753 s
->freq
= dsp_get_hilo (s
);
754 ldebug ("set freq %d\n", s
->freq
);
758 s
->block_size
= dsp_get_lohi (s
) + 1;
759 ldebug ("set dma block len %d\n", s
->block_size
);
766 /* ADPCM stuff, ignore */
771 int freq
, samples
, bytes
;
774 freq
= s
->freq
> 0 ? s
->freq
: 11025;
775 samples
= dsp_get_lohi (s
) + 1;
776 bytes
= samples
<< s
->fmt_stereo
<< (s
->fmt_bits
== 16);
777 ticks
= muldiv64(bytes
, NANOSECONDS_PER_SECOND
, freq
);
778 if (ticks
< NANOSECONDS_PER_SECOND
/ 1024) {
779 qemu_irq_raise (s
->pic
);
785 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + ticks
789 ldebug ("mix silence %d %d %" PRId64
"\n", samples
, bytes
, ticks
);
794 d0
= dsp_get_data (s
);
796 ldebug ("E0 data = %#x\n", d0
);
797 dsp_out_data (s
, ~d0
);
802 d0
= dsp_get_data (s
);
803 dolog ("E2 = %#x\n", d0
);
808 s
->test_reg
= dsp_get_data (s
);
812 d0
= dsp_get_data (s
);
813 ldebug ("command 0xf9 with %#x\n", d0
);
816 dsp_out_data (s
, 0xff);
820 dsp_out_data (s
, 0x07);
824 dsp_out_data (s
, 0x38);
828 dsp_out_data (s
, 0x00);
834 qemu_log_mask(LOG_UNIMP
, "complete: unrecognized command %#x\n",
844 static void legacy_reset (SB16State
*s
)
846 struct audsettings as
;
858 s
->voice
= AUD_open_out (
867 /* Not sure about that... */
868 /* AUD_set_active_out (s->voice, 1); */
871 static void reset (SB16State
*s
)
873 qemu_irq_lower (s
->pic
);
875 qemu_irq_raise (s
->pic
);
876 qemu_irq_lower (s
->pic
);
879 s
->mixer_regs
[0x82] = 0;
883 s
->left_till_irq
= 0;
891 dsp_out_data (s
, 0xaa);
897 static void dsp_write(void *opaque
, uint32_t nport
, uint32_t val
)
899 SB16State
*s
= opaque
;
902 iport
= nport
- s
->port
;
904 ldebug ("write %#x <- %#x\n", nport
, val
);
916 case 0x03: /* FreeBSD kludge */
921 s
->v2x6
= 0; /* Prince of Persia, csp.sys, diagnose.exe */
924 case 0xb8: /* Panic */
929 dsp_out_data (s
, 0x38);
940 case 0x0c: /* write data or command | write status */
941 /* if (s->highspeed) */
944 if (s
->needed_bytes
== 0) {
947 if (0 == s
->needed_bytes
) {
953 if (s
->in_index
== sizeof (s
->in2_data
)) {
954 dolog ("in data overrun\n");
957 s
->in2_data
[s
->in_index
++] = val
;
958 if (s
->in_index
== s
->needed_bytes
) {
970 ldebug ("(nport=%#x, val=%#x)\n", nport
, val
);
975 static uint32_t dsp_read(void *opaque
, uint32_t nport
)
977 SB16State
*s
= opaque
;
978 int iport
, retval
, ack
= 0;
980 iport
= nport
- s
->port
;
983 case 0x06: /* reset */
987 case 0x0a: /* read data */
988 if (s
->out_data_len
) {
989 retval
= s
->out_data
[--s
->out_data_len
];
990 s
->last_read_byte
= retval
;
994 dolog ("empty output buffer for command %#x\n",
997 retval
= s
->last_read_byte
;
1002 case 0x0c: /* 0 can write */
1003 retval
= s
->can_write
? 0 : 0x80;
1006 case 0x0d: /* timer interrupt clear */
1007 /* dolog ("timer interrupt clear\n"); */
1011 case 0x0e: /* data available status | irq 8 ack */
1012 retval
= (!s
->out_data_len
|| s
->highspeed
) ? 0 : 0x80;
1013 if (s
->mixer_regs
[0x82] & 1) {
1015 s
->mixer_regs
[0x82] &= ~1;
1016 qemu_irq_lower (s
->pic
);
1020 case 0x0f: /* irq 16 ack */
1022 if (s
->mixer_regs
[0x82] & 2) {
1024 s
->mixer_regs
[0x82] &= ~2;
1025 qemu_irq_lower (s
->pic
);
1034 ldebug ("read %#x -> %#x\n", nport
, retval
);
1040 dolog ("warning: dsp_read %#x error\n", nport
);
1044 static void reset_mixer (SB16State
*s
)
1048 memset (s
->mixer_regs
, 0xff, 0x7f);
1049 memset (s
->mixer_regs
+ 0x83, 0xff, sizeof (s
->mixer_regs
) - 0x83);
1051 s
->mixer_regs
[0x02] = 4; /* master volume 3bits */
1052 s
->mixer_regs
[0x06] = 4; /* MIDI volume 3bits */
1053 s
->mixer_regs
[0x08] = 0; /* CD volume 3bits */
1054 s
->mixer_regs
[0x0a] = 0; /* voice volume 2bits */
1056 /* d5=input filt, d3=lowpass filt, d1,d2=input source */
1057 s
->mixer_regs
[0x0c] = 0;
1059 /* d5=output filt, d1=stereo switch */
1060 s
->mixer_regs
[0x0e] = 0;
1062 /* voice volume L d5,d7, R d1,d3 */
1063 s
->mixer_regs
[0x04] = (4 << 5) | (4 << 1);
1065 s
->mixer_regs
[0x22] = (4 << 5) | (4 << 1);
1067 s
->mixer_regs
[0x26] = (4 << 5) | (4 << 1);
1069 for (i
= 0x30; i
< 0x48; i
++) {
1070 s
->mixer_regs
[i
] = 0x20;
1074 static void mixer_write_indexb(void *opaque
, uint32_t nport
, uint32_t val
)
1076 SB16State
*s
= opaque
;
1078 s
->mixer_nreg
= val
;
1081 static void mixer_write_datab(void *opaque
, uint32_t nport
, uint32_t val
)
1083 SB16State
*s
= opaque
;
1086 ldebug ("mixer_write [%#x] <- %#x\n", s
->mixer_nreg
, val
);
1088 switch (s
->mixer_nreg
) {
1095 int irq
= irq_of_magic (val
);
1096 ldebug ("setting irq to %d (val=%#x)\n", irq
, val
);
1107 dma
= ctz32 (val
& 0xf);
1108 hdma
= ctz32 (val
& 0xf0);
1109 if (dma
!= s
->dma
|| hdma
!= s
->hdma
) {
1110 qemu_log_mask(LOG_GUEST_ERROR
, "attempt to change DMA 8bit"
1111 " %d(%d), 16bit %d(%d) (val=%#x)\n", dma
, s
->dma
,
1112 hdma
, s
->hdma
, val
);
1122 qemu_log_mask(LOG_GUEST_ERROR
, "attempt to write into IRQ status"
1123 " register (val=%#x)\n", val
);
1127 if (s
->mixer_nreg
>= 0x80) {
1128 ldebug ("attempt to write mixer[%#x] <- %#x\n", s
->mixer_nreg
, val
);
1133 s
->mixer_regs
[s
->mixer_nreg
] = val
;
1136 static uint32_t mixer_read(void *opaque
, uint32_t nport
)
1138 SB16State
*s
= opaque
;
1141 #ifndef DEBUG_SB16_MOST
1142 if (s
->mixer_nreg
!= 0x82) {
1143 ldebug ("mixer_read[%#x] -> %#x\n",
1144 s
->mixer_nreg
, s
->mixer_regs
[s
->mixer_nreg
]);
1147 ldebug ("mixer_read[%#x] -> %#x\n",
1148 s
->mixer_nreg
, s
->mixer_regs
[s
->mixer_nreg
]);
1150 return s
->mixer_regs
[s
->mixer_nreg
];
1153 static int write_audio (SB16State
*s
, int nchan
, int dma_pos
,
1154 int dma_len
, int len
)
1156 IsaDma
*isa_dma
= nchan
== s
->dma
? s
->isa_dma
: s
->isa_hdma
;
1157 IsaDmaClass
*k
= ISADMA_GET_CLASS(isa_dma
);
1159 uint8_t tmpbuf
[4096];
1165 int left
= dma_len
- dma_pos
;
1169 to_copy
= audio_MIN (temp
, left
);
1170 if (to_copy
> sizeof (tmpbuf
)) {
1171 to_copy
= sizeof (tmpbuf
);
1174 copied
= k
->read_memory(isa_dma
, nchan
, tmpbuf
, dma_pos
, to_copy
);
1175 copied
= AUD_write (s
->voice
, tmpbuf
, copied
);
1178 dma_pos
= (dma_pos
+ copied
) % dma_len
;
1189 static int SB_read_DMA (void *opaque
, int nchan
, int dma_pos
, int dma_len
)
1191 SB16State
*s
= opaque
;
1192 int till
, copy
, written
, free
;
1194 if (s
->block_size
<= 0) {
1195 qemu_log_mask(LOG_GUEST_ERROR
, "invalid block size=%d nchan=%d"
1196 " dma_pos=%d dma_len=%d\n", s
->block_size
, nchan
,
1201 if (s
->left_till_irq
< 0) {
1202 s
->left_till_irq
= s
->block_size
;
1206 free
= s
->audio_free
& ~s
->align
;
1207 if ((free
<= 0) || !dma_len
) {
1216 till
= s
->left_till_irq
;
1218 #ifdef DEBUG_SB16_MOST
1219 dolog ("pos:%06d %d till:%d len:%d\n",
1220 dma_pos
, free
, till
, dma_len
);
1224 if (s
->dma_auto
== 0) {
1229 written
= write_audio (s
, nchan
, dma_pos
, dma_len
, copy
);
1230 dma_pos
= (dma_pos
+ written
) % dma_len
;
1231 s
->left_till_irq
-= written
;
1233 if (s
->left_till_irq
<= 0) {
1234 s
->mixer_regs
[0x82] |= (nchan
& 4) ? 2 : 1;
1235 qemu_irq_raise (s
->pic
);
1236 if (s
->dma_auto
== 0) {
1242 #ifdef DEBUG_SB16_MOST
1243 ldebug ("pos %5d free %5d size %5d till % 5d copy %5d written %5d size %5d\n",
1244 dma_pos
, free
, dma_len
, s
->left_till_irq
, copy
, written
,
1248 while (s
->left_till_irq
<= 0) {
1249 s
->left_till_irq
= s
->block_size
+ s
->left_till_irq
;
1255 static void SB_audio_callback (void *opaque
, int free
)
1257 SB16State
*s
= opaque
;
1258 s
->audio_free
= free
;
1261 static int sb16_post_load (void *opaque
, int version_id
)
1263 SB16State
*s
= opaque
;
1266 AUD_close_out (&s
->card
, s
->voice
);
1270 if (s
->dma_running
) {
1272 struct audsettings as
;
1277 as
.nchannels
= 1 << s
->fmt_stereo
;
1281 s
->voice
= AUD_open_out (
1292 speaker (s
, s
->speaker
);
1297 static const VMStateDescription vmstate_sb16
= {
1300 .minimum_version_id
= 1,
1301 .post_load
= sb16_post_load
,
1302 .fields
= (VMStateField
[]) {
1303 VMSTATE_UINT32 (irq
, SB16State
),
1304 VMSTATE_UINT32 (dma
, SB16State
),
1305 VMSTATE_UINT32 (hdma
, SB16State
),
1306 VMSTATE_UINT32 (port
, SB16State
),
1307 VMSTATE_UINT32 (ver
, SB16State
),
1308 VMSTATE_INT32 (in_index
, SB16State
),
1309 VMSTATE_INT32 (out_data_len
, SB16State
),
1310 VMSTATE_INT32 (fmt_stereo
, SB16State
),
1311 VMSTATE_INT32 (fmt_signed
, SB16State
),
1312 VMSTATE_INT32 (fmt_bits
, SB16State
),
1313 VMSTATE_UINT32 (fmt
, SB16State
),
1314 VMSTATE_INT32 (dma_auto
, SB16State
),
1315 VMSTATE_INT32 (block_size
, SB16State
),
1316 VMSTATE_INT32 (fifo
, SB16State
),
1317 VMSTATE_INT32 (freq
, SB16State
),
1318 VMSTATE_INT32 (time_const
, SB16State
),
1319 VMSTATE_INT32 (speaker
, SB16State
),
1320 VMSTATE_INT32 (needed_bytes
, SB16State
),
1321 VMSTATE_INT32 (cmd
, SB16State
),
1322 VMSTATE_INT32 (use_hdma
, SB16State
),
1323 VMSTATE_INT32 (highspeed
, SB16State
),
1324 VMSTATE_INT32 (can_write
, SB16State
),
1325 VMSTATE_INT32 (v2x6
, SB16State
),
1327 VMSTATE_UINT8 (csp_param
, SB16State
),
1328 VMSTATE_UINT8 (csp_value
, SB16State
),
1329 VMSTATE_UINT8 (csp_mode
, SB16State
),
1330 VMSTATE_UINT8 (csp_param
, SB16State
),
1331 VMSTATE_BUFFER (csp_regs
, SB16State
),
1332 VMSTATE_UINT8 (csp_index
, SB16State
),
1333 VMSTATE_BUFFER (csp_reg83
, SB16State
),
1334 VMSTATE_INT32 (csp_reg83r
, SB16State
),
1335 VMSTATE_INT32 (csp_reg83w
, SB16State
),
1337 VMSTATE_BUFFER (in2_data
, SB16State
),
1338 VMSTATE_BUFFER (out_data
, SB16State
),
1339 VMSTATE_UINT8 (test_reg
, SB16State
),
1340 VMSTATE_UINT8 (last_read_byte
, SB16State
),
1342 VMSTATE_INT32 (nzero
, SB16State
),
1343 VMSTATE_INT32 (left_till_irq
, SB16State
),
1344 VMSTATE_INT32 (dma_running
, SB16State
),
1345 VMSTATE_INT32 (bytes_per_second
, SB16State
),
1346 VMSTATE_INT32 (align
, SB16State
),
1348 VMSTATE_INT32 (mixer_nreg
, SB16State
),
1349 VMSTATE_BUFFER (mixer_regs
, SB16State
),
1351 VMSTATE_END_OF_LIST ()
1355 static const MemoryRegionPortio sb16_ioport_list
[] = {
1356 { 4, 1, 1, .write
= mixer_write_indexb
},
1357 { 5, 1, 1, .read
= mixer_read
, .write
= mixer_write_datab
},
1358 { 6, 1, 1, .read
= dsp_read
, .write
= dsp_write
},
1359 { 10, 1, 1, .read
= dsp_read
},
1360 { 12, 1, 1, .write
= dsp_write
},
1361 { 12, 4, 1, .read
= dsp_read
},
1362 PORTIO_END_OF_LIST (),
1366 static void sb16_initfn (Object
*obj
)
1368 SB16State
*s
= SB16 (obj
);
1373 static void sb16_realizefn (DeviceState
*dev
, Error
**errp
)
1375 ISADevice
*isadev
= ISA_DEVICE (dev
);
1376 SB16State
*s
= SB16 (dev
);
1379 s
->isa_hdma
= isa_get_dma(isa_bus_from_device(isadev
), s
->hdma
);
1380 s
->isa_dma
= isa_get_dma(isa_bus_from_device(isadev
), s
->dma
);
1381 if (!s
->isa_dma
|| !s
->isa_hdma
) {
1382 error_setg(errp
, "ISA controller does not support DMA");
1386 isa_init_irq (isadev
, &s
->pic
, s
->irq
);
1388 s
->mixer_regs
[0x80] = magic_of_irq (s
->irq
);
1389 s
->mixer_regs
[0x81] = (1 << s
->dma
) | (1 << s
->hdma
);
1390 s
->mixer_regs
[0x82] = 2 << 5;
1393 s
->csp_regs
[9] = 0xf8;
1396 s
->aux_ts
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, aux_timer
, s
);
1398 error_setg(errp
, "warning: Could not create auxiliary timer");
1401 isa_register_portio_list(isadev
, &s
->portio_list
, s
->port
,
1402 sb16_ioport_list
, s
, "sb16");
1404 k
= ISADMA_GET_CLASS(s
->isa_hdma
);
1405 k
->register_channel(s
->isa_hdma
, s
->hdma
, SB_read_DMA
, s
);
1407 k
= ISADMA_GET_CLASS(s
->isa_dma
);
1408 k
->register_channel(s
->isa_dma
, s
->dma
, SB_read_DMA
, s
);
1412 AUD_register_card ("sb16", &s
->card
);
1415 static int SB16_init (ISABus
*bus
)
1417 isa_create_simple (bus
, TYPE_SB16
);
1421 static Property sb16_properties
[] = {
1422 DEFINE_PROP_UINT32 ("version", SB16State
, ver
, 0x0405), /* 4.5 */
1423 DEFINE_PROP_UINT32 ("iobase", SB16State
, port
, 0x220),
1424 DEFINE_PROP_UINT32 ("irq", SB16State
, irq
, 5),
1425 DEFINE_PROP_UINT32 ("dma", SB16State
, dma
, 1),
1426 DEFINE_PROP_UINT32 ("dma16", SB16State
, hdma
, 5),
1427 DEFINE_PROP_END_OF_LIST (),
1430 static void sb16_class_initfn (ObjectClass
*klass
, void *data
)
1432 DeviceClass
*dc
= DEVICE_CLASS (klass
);
1434 dc
->realize
= sb16_realizefn
;
1435 set_bit(DEVICE_CATEGORY_SOUND
, dc
->categories
);
1436 dc
->desc
= "Creative Sound Blaster 16";
1437 dc
->vmsd
= &vmstate_sb16
;
1438 dc
->props
= sb16_properties
;
1441 static const TypeInfo sb16_info
= {
1443 .parent
= TYPE_ISA_DEVICE
,
1444 .instance_size
= sizeof (SB16State
),
1445 .instance_init
= sb16_initfn
,
1446 .class_init
= sb16_class_initfn
,
1449 static void sb16_register_types (void)
1451 type_register_static (&sb16_info
);
1452 isa_register_soundhw("sb16", "Creative Sound Blaster 16", SB16_init
);
1455 type_init (sb16_register_types
)