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
26 #include "audio/audio.h"
29 #include "qemu-timer.h"
30 #include "host-utils.h"
32 #define dolog(...) AUD_log ("sb16", __VA_ARGS__)
35 /* #define DEBUG_SB16_MOST */
38 #define ldebug(...) dolog (__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)
48 static const char e3
[] = "COPYRIGHT (C) CREATIVE TECHNOLOGY LTD, 1992.";
50 typedef struct SB16State
{
83 uint8_t csp_regs
[256];
92 uint8_t last_read_byte
;
106 uint8_t mixer_regs
[256];
109 static void SB_audio_callback (void *opaque
, int free
);
111 static int magic_of_irq (int irq
)
123 dolog ("bad irq %d\n", irq
);
128 static int irq_of_magic (int magic
)
140 dolog ("bad irq magic %d\n", magic
);
146 static void log_dsp (SB16State
*dsp
)
148 ldebug ("%s:%s:%d:%s:dmasize=%d:freq=%d:const=%d:speaker=%d\n",
149 dsp
->fmt_stereo
? "Stereo" : "Mono",
150 dsp
->fmt_signed
? "Signed" : "Unsigned",
152 dsp
->dma_auto
? "Auto" : "Single",
160 static void speaker (SB16State
*s
, int on
)
163 /* AUD_enable (s->voice, on); */
166 static void control (SB16State
*s
, int hold
)
168 int dma
= s
->use_hdma
? s
->hdma
: s
->dma
;
169 s
->dma_running
= hold
;
171 ldebug ("hold %d high %d dma %d\n", hold
, s
->use_hdma
, dma
);
175 AUD_set_active_out (s
->voice
, 1);
178 DMA_release_DREQ (dma
);
179 AUD_set_active_out (s
->voice
, 0);
183 static void aux_timer (void *opaque
)
185 SB16State
*s
= opaque
;
187 qemu_irq_raise (s
->pic
);
193 static void continue_dma8 (SB16State
*s
)
196 struct audsettings as
;
201 as
.nchannels
= 1 << s
->fmt_stereo
;
205 s
->voice
= AUD_open_out (
218 static void dma_cmd8 (SB16State
*s
, int mask
, int dma_len
)
224 s
->fmt_stereo
= (s
->mixer_regs
[0x0e] & 2) != 0;
225 if (-1 == s
->time_const
) {
230 int tmp
= (256 - s
->time_const
);
231 s
->freq
= (1000000 + (tmp
/ 2)) / tmp
;
235 s
->block_size
= dma_len
<< s
->fmt_stereo
;
238 /* This is apparently the only way to make both Act1/PL
239 and SecondReality/FC work
241 Act1 sets block size via command 0x48 and it's an odd number
242 SR does the same with even number
243 Both use stereo, and Creatives own documentation states that
244 0x48 sets block size in bytes less one.. go figure */
245 s
->block_size
&= ~s
->fmt_stereo
;
248 s
->freq
>>= s
->fmt_stereo
;
249 s
->left_till_irq
= s
->block_size
;
250 s
->bytes_per_second
= (s
->freq
<< s
->fmt_stereo
);
251 /* s->highspeed = (mask & DMA8_HIGH) != 0; */
252 s
->dma_auto
= (mask
& DMA8_AUTO
) != 0;
253 s
->align
= (1 << s
->fmt_stereo
) - 1;
255 if (s
->block_size
& s
->align
) {
256 dolog ("warning: misaligned block size %d, alignment %d\n",
257 s
->block_size
, s
->align
+ 1);
260 ldebug ("freq %d, stereo %d, sign %d, bits %d, "
261 "dma %d, auto %d, fifo %d, high %d\n",
262 s
->freq
, s
->fmt_stereo
, s
->fmt_signed
, s
->fmt_bits
,
263 s
->block_size
, s
->dma_auto
, s
->fifo
, s
->highspeed
);
269 static void dma_cmd (SB16State
*s
, uint8_t cmd
, uint8_t d0
, int dma_len
)
271 s
->use_hdma
= cmd
< 0xc0;
272 s
->fifo
= (cmd
>> 1) & 1;
273 s
->dma_auto
= (cmd
>> 2) & 1;
274 s
->fmt_signed
= (d0
>> 4) & 1;
275 s
->fmt_stereo
= (d0
>> 5) & 1;
287 if (-1 != s
->time_const
) {
289 int tmp
= 256 - s
->time_const
;
290 s
->freq
= (1000000 + (tmp
/ 2)) / tmp
;
292 /* s->freq = 1000000 / ((255 - s->time_const) << s->fmt_stereo); */
293 s
->freq
= 1000000 / ((255 - s
->time_const
));
298 s
->block_size
= dma_len
+ 1;
299 s
->block_size
<<= (s
->fmt_bits
== 16);
301 /* It is clear that for DOOM and auto-init this value
302 shouldn't take stereo into account, while Miles Sound Systems
303 setsound.exe with single transfer mode wouldn't work without it
304 wonders of SB16 yet again */
305 s
->block_size
<<= s
->fmt_stereo
;
308 ldebug ("freq %d, stereo %d, sign %d, bits %d, "
309 "dma %d, auto %d, fifo %d, high %d\n",
310 s
->freq
, s
->fmt_stereo
, s
->fmt_signed
, s
->fmt_bits
,
311 s
->block_size
, s
->dma_auto
, s
->fifo
, s
->highspeed
);
313 if (16 == s
->fmt_bits
) {
315 s
->fmt
= AUD_FMT_S16
;
318 s
->fmt
= AUD_FMT_U16
;
330 s
->left_till_irq
= s
->block_size
;
332 s
->bytes_per_second
= (s
->freq
<< s
->fmt_stereo
) << (s
->fmt_bits
== 16);
334 s
->align
= (1 << (s
->fmt_stereo
+ (s
->fmt_bits
== 16))) - 1;
335 if (s
->block_size
& s
->align
) {
336 dolog ("warning: misaligned block size %d, alignment %d\n",
337 s
->block_size
, s
->align
+ 1);
341 struct audsettings as
;
346 as
.nchannels
= 1 << s
->fmt_stereo
;
350 s
->voice
= AUD_open_out (
364 static inline void dsp_out_data (SB16State
*s
, uint8_t val
)
366 ldebug ("outdata %#x\n", val
);
367 if ((size_t) s
->out_data_len
< sizeof (s
->out_data
)) {
368 s
->out_data
[s
->out_data_len
++] = val
;
372 static inline uint8_t dsp_get_data (SB16State
*s
)
375 return s
->in2_data
[--s
->in_index
];
378 dolog ("buffer underflow\n");
383 static void command (SB16State
*s
, uint8_t cmd
)
385 ldebug ("command %#x\n", cmd
);
387 if (cmd
> 0xaf && cmd
< 0xd0) {
389 dolog ("ADC not yet supported (command %#x)\n", cmd
);
397 dolog ("%#x wrong bits\n", cmd
);
406 dsp_out_data (s
, 0x10); /* s->csp_param); */
418 /* __asm__ ("int3"); */
426 dsp_out_data (s
, 0xf8);
442 case 0x1c: /* Auto-Initialize DMA DAC, 8-bit */
443 dma_cmd8 (s
, DMA8_AUTO
, -1);
446 case 0x20: /* Direct ADC, Juice/PL */
447 dsp_out_data (s
, 0xff);
451 dolog ("0x35 - MIDI command not implemented\n");
473 dsp_out_data (s
, 0xaa);
476 case 0x47: /* Continue Auto-Initialize DMA 16bit */
484 s
->needed_bytes
= 2; /* DMA DAC, 4-bit ADPCM */
485 dolog ("0x75 - DMA DAC, 4-bit ADPCM not implemented\n");
488 case 0x75: /* DMA DAC, 4-bit ADPCM Reference */
490 dolog ("0x74 - DMA DAC, 4-bit ADPCM Reference not implemented\n");
493 case 0x76: /* DMA DAC, 2.6-bit ADPCM */
495 dolog ("0x74 - DMA DAC, 2.6-bit ADPCM not implemented\n");
498 case 0x77: /* DMA DAC, 2.6-bit ADPCM Reference */
500 dolog ("0x74 - DMA DAC, 2.6-bit ADPCM Reference not implemented\n");
504 dolog ("0x7d - Autio-Initialize DMA DAC, 4-bit ADPCM Reference\n");
505 dolog ("not implemented\n");
510 "0x7d - Autio-Initialize DMA DAC, 2.6-bit ADPCM Reference\n"
512 dolog ("not implemented\n");
521 dma_cmd8 (s
, ((cmd
& 1) == 0) | DMA8_HIGH
, -1);
524 case 0xd0: /* halt DMA operation. 8bit */
528 case 0xd1: /* speaker on */
532 case 0xd3: /* speaker off */
536 case 0xd4: /* continue DMA operation. 8bit */
537 /* KQ6 (or maybe Sierras audblst.drv in general) resets
538 the frequency between halt/continue */
542 case 0xd5: /* halt DMA operation. 16bit */
546 case 0xd6: /* continue DMA operation. 16bit */
550 case 0xd9: /* exit auto-init DMA after this block. 16bit */
554 case 0xda: /* exit auto-init DMA after this block. 8bit */
558 case 0xe0: /* DSP identification */
563 dsp_out_data (s
, s
->ver
& 0xff);
564 dsp_out_data (s
, s
->ver
>> 8);
574 for (i
= sizeof (e3
) - 1; i
>= 0; --i
)
575 dsp_out_data (s
, e3
[i
]);
579 case 0xe4: /* write test reg */
584 dolog ("Attempt to probe for ESS (0xe7)?\n");
587 case 0xe8: /* read test reg */
588 dsp_out_data (s
, s
->test_reg
);
593 dsp_out_data (s
, 0xaa);
594 s
->mixer_regs
[0x82] |= (cmd
== 0xf2) ? 1 : 2;
595 qemu_irq_raise (s
->pic
);
606 case 0xfc: /* FIXME */
611 dolog ("Unrecognized command %#x\n", cmd
);
616 if (!s
->needed_bytes
) {
621 if (!s
->needed_bytes
) {
630 dolog ("warning: command %#x,%d is not truly understood yet\n",
631 cmd
, s
->needed_bytes
);
636 static uint16_t dsp_get_lohi (SB16State
*s
)
638 uint8_t hi
= dsp_get_data (s
);
639 uint8_t lo
= dsp_get_data (s
);
640 return (hi
<< 8) | lo
;
643 static uint16_t dsp_get_hilo (SB16State
*s
)
645 uint8_t lo
= dsp_get_data (s
);
646 uint8_t hi
= dsp_get_data (s
);
647 return (hi
<< 8) | lo
;
650 static void complete (SB16State
*s
)
653 ldebug ("complete command %#x, in_index %d, needed_bytes %d\n",
654 s
->cmd
, s
->in_index
, s
->needed_bytes
);
656 if (s
->cmd
> 0xaf && s
->cmd
< 0xd0) {
657 d2
= dsp_get_data (s
);
658 d1
= dsp_get_data (s
);
659 d0
= dsp_get_data (s
);
662 dolog ("ADC params cmd = %#x d0 = %d, d1 = %d, d2 = %d\n",
666 ldebug ("cmd = %#x d0 = %d, d1 = %d, d2 = %d\n",
668 dma_cmd (s
, s
->cmd
, d0
, d1
+ (d2
<< 8));
674 s
->csp_mode
= dsp_get_data (s
);
677 ldebug ("CSP command 0x04: mode=%#x\n", s
->csp_mode
);
681 s
->csp_param
= dsp_get_data (s
);
682 s
->csp_value
= dsp_get_data (s
);
683 ldebug ("CSP command 0x05: param=%#x value=%#x\n",
689 d0
= dsp_get_data (s
);
690 d1
= dsp_get_data (s
);
691 ldebug ("write CSP register %d <- %#x\n", d1
, d0
);
693 ldebug ("0x83[%d] <- %#x\n", s
->csp_reg83r
, d0
);
694 s
->csp_reg83
[s
->csp_reg83r
% 4] = d0
;
698 s
->csp_regs
[d1
] = d0
;
703 d0
= dsp_get_data (s
);
704 ldebug ("read CSP register %#x -> %#x, mode=%#x\n",
705 d0
, s
->csp_regs
[d0
], s
->csp_mode
);
707 ldebug ("0x83[%d] -> %#x\n",
709 s
->csp_reg83
[s
->csp_reg83w
% 4]);
710 dsp_out_data (s
, s
->csp_reg83
[s
->csp_reg83w
% 4]);
714 dsp_out_data (s
, s
->csp_regs
[d0
]);
719 d0
= dsp_get_data (s
);
720 dolog ("cmd 0x10 d0=%#x\n", d0
);
724 dma_cmd8 (s
, 0, dsp_get_lohi (s
) + 1);
728 s
->time_const
= dsp_get_data (s
);
729 ldebug ("set time const %d\n", s
->time_const
);
732 case 0x42: /* FT2 sets output freq with this, go figure */
734 dolog ("cmd 0x42 might not do what it think it should\n");
737 s
->freq
= dsp_get_hilo (s
);
738 ldebug ("set freq %d\n", s
->freq
);
742 s
->block_size
= dsp_get_lohi (s
) + 1;
743 ldebug ("set dma block len %d\n", s
->block_size
);
750 /* ADPCM stuff, ignore */
755 int freq
, samples
, bytes
;
758 freq
= s
->freq
> 0 ? s
->freq
: 11025;
759 samples
= dsp_get_lohi (s
) + 1;
760 bytes
= samples
<< s
->fmt_stereo
<< (s
->fmt_bits
== 16);
761 ticks
= muldiv64 (bytes
, get_ticks_per_sec (), freq
);
762 if (ticks
< get_ticks_per_sec () / 1024) {
763 qemu_irq_raise (s
->pic
);
769 qemu_get_clock_ns (vm_clock
) + ticks
773 ldebug ("mix silence %d %d %" PRId64
"\n", samples
, bytes
, ticks
);
778 d0
= dsp_get_data (s
);
780 ldebug ("E0 data = %#x\n", d0
);
781 dsp_out_data (s
, ~d0
);
786 d0
= dsp_get_data (s
);
787 dolog ("E2 = %#x\n", d0
);
792 s
->test_reg
= dsp_get_data (s
);
796 d0
= dsp_get_data (s
);
797 ldebug ("command 0xf9 with %#x\n", d0
);
800 dsp_out_data (s
, 0xff);
804 dsp_out_data (s
, 0x07);
808 dsp_out_data (s
, 0x38);
812 dsp_out_data (s
, 0x00);
818 dolog ("complete: unrecognized command %#x\n", s
->cmd
);
828 static void legacy_reset (SB16State
*s
)
830 struct audsettings as
;
842 s
->voice
= AUD_open_out (
851 /* Not sure about that... */
852 /* AUD_set_active_out (s->voice, 1); */
855 static void reset (SB16State
*s
)
857 qemu_irq_lower (s
->pic
);
859 qemu_irq_raise (s
->pic
);
860 qemu_irq_lower (s
->pic
);
863 s
->mixer_regs
[0x82] = 0;
867 s
->left_till_irq
= 0;
875 dsp_out_data (s
, 0xaa);
881 static IO_WRITE_PROTO (dsp_write
)
883 SB16State
*s
= opaque
;
886 iport
= nport
- s
->port
;
888 ldebug ("write %#x <- %#x\n", nport
, val
);
900 case 0x03: /* FreeBSD kludge */
905 s
->v2x6
= 0; /* Prince of Persia, csp.sys, diagnose.exe */
908 case 0xb8: /* Panic */
913 dsp_out_data (s
, 0x38);
924 case 0x0c: /* write data or command | write status */
925 /* if (s->highspeed) */
928 if (0 == s
->needed_bytes
) {
931 if (0 == s
->needed_bytes
) {
937 if (s
->in_index
== sizeof (s
->in2_data
)) {
938 dolog ("in data overrun\n");
941 s
->in2_data
[s
->in_index
++] = val
;
942 if (s
->in_index
== s
->needed_bytes
) {
954 ldebug ("(nport=%#x, val=%#x)\n", nport
, val
);
959 static IO_READ_PROTO (dsp_read
)
961 SB16State
*s
= opaque
;
962 int iport
, retval
, ack
= 0;
964 iport
= nport
- s
->port
;
967 case 0x06: /* reset */
971 case 0x0a: /* read data */
972 if (s
->out_data_len
) {
973 retval
= s
->out_data
[--s
->out_data_len
];
974 s
->last_read_byte
= retval
;
978 dolog ("empty output buffer for command %#x\n",
981 retval
= s
->last_read_byte
;
986 case 0x0c: /* 0 can write */
987 retval
= s
->can_write
? 0 : 0x80;
990 case 0x0d: /* timer interrupt clear */
991 /* dolog ("timer interrupt clear\n"); */
995 case 0x0e: /* data available status | irq 8 ack */
996 retval
= (!s
->out_data_len
|| s
->highspeed
) ? 0 : 0x80;
997 if (s
->mixer_regs
[0x82] & 1) {
999 s
->mixer_regs
[0x82] &= 1;
1000 qemu_irq_lower (s
->pic
);
1004 case 0x0f: /* irq 16 ack */
1006 if (s
->mixer_regs
[0x82] & 2) {
1008 s
->mixer_regs
[0x82] &= 2;
1009 qemu_irq_lower (s
->pic
);
1018 ldebug ("read %#x -> %#x\n", nport
, retval
);
1024 dolog ("warning: dsp_read %#x error\n", nport
);
1028 static void reset_mixer (SB16State
*s
)
1032 memset (s
->mixer_regs
, 0xff, 0x7f);
1033 memset (s
->mixer_regs
+ 0x83, 0xff, sizeof (s
->mixer_regs
) - 0x83);
1035 s
->mixer_regs
[0x02] = 4; /* master volume 3bits */
1036 s
->mixer_regs
[0x06] = 4; /* MIDI volume 3bits */
1037 s
->mixer_regs
[0x08] = 0; /* CD volume 3bits */
1038 s
->mixer_regs
[0x0a] = 0; /* voice volume 2bits */
1040 /* d5=input filt, d3=lowpass filt, d1,d2=input source */
1041 s
->mixer_regs
[0x0c] = 0;
1043 /* d5=output filt, d1=stereo switch */
1044 s
->mixer_regs
[0x0e] = 0;
1046 /* voice volume L d5,d7, R d1,d3 */
1047 s
->mixer_regs
[0x04] = (4 << 5) | (4 << 1);
1049 s
->mixer_regs
[0x22] = (4 << 5) | (4 << 1);
1051 s
->mixer_regs
[0x26] = (4 << 5) | (4 << 1);
1053 for (i
= 0x30; i
< 0x48; i
++) {
1054 s
->mixer_regs
[i
] = 0x20;
1058 static IO_WRITE_PROTO (mixer_write_indexb
)
1060 SB16State
*s
= opaque
;
1062 s
->mixer_nreg
= val
;
1065 static IO_WRITE_PROTO (mixer_write_datab
)
1067 SB16State
*s
= opaque
;
1070 ldebug ("mixer_write [%#x] <- %#x\n", s
->mixer_nreg
, val
);
1072 switch (s
->mixer_nreg
) {
1079 int irq
= irq_of_magic (val
);
1080 ldebug ("setting irq to %d (val=%#x)\n", irq
, val
);
1091 dma
= ctz32 (val
& 0xf);
1092 hdma
= ctz32 (val
& 0xf0);
1093 if (dma
!= s
->dma
|| hdma
!= s
->hdma
) {
1095 "attempt to change DMA "
1096 "8bit %d(%d), 16bit %d(%d) (val=%#x)\n",
1097 dma
, s
->dma
, hdma
, s
->hdma
, val
);
1107 dolog ("attempt to write into IRQ status register (val=%#x)\n",
1112 if (s
->mixer_nreg
>= 0x80) {
1113 ldebug ("attempt to write mixer[%#x] <- %#x\n", s
->mixer_nreg
, val
);
1118 s
->mixer_regs
[s
->mixer_nreg
] = val
;
1121 static IO_WRITE_PROTO (mixer_write_indexw
)
1123 mixer_write_indexb (opaque
, nport
, val
& 0xff);
1124 mixer_write_datab (opaque
, nport
, (val
>> 8) & 0xff);
1127 static IO_READ_PROTO (mixer_read
)
1129 SB16State
*s
= opaque
;
1132 #ifndef DEBUG_SB16_MOST
1133 if (s
->mixer_nreg
!= 0x82) {
1134 ldebug ("mixer_read[%#x] -> %#x\n",
1135 s
->mixer_nreg
, s
->mixer_regs
[s
->mixer_nreg
]);
1138 ldebug ("mixer_read[%#x] -> %#x\n",
1139 s
->mixer_nreg
, s
->mixer_regs
[s
->mixer_nreg
]);
1141 return s
->mixer_regs
[s
->mixer_nreg
];
1144 static int write_audio (SB16State
*s
, int nchan
, int dma_pos
,
1145 int dma_len
, int len
)
1148 uint8_t tmpbuf
[4096];
1154 int left
= dma_len
- dma_pos
;
1158 to_copy
= audio_MIN (temp
, left
);
1159 if (to_copy
> sizeof (tmpbuf
)) {
1160 to_copy
= sizeof (tmpbuf
);
1163 copied
= DMA_read_memory (nchan
, tmpbuf
, dma_pos
, to_copy
);
1164 copied
= AUD_write (s
->voice
, tmpbuf
, copied
);
1167 dma_pos
= (dma_pos
+ copied
) % dma_len
;
1178 static int SB_read_DMA (void *opaque
, int nchan
, int dma_pos
, int dma_len
)
1180 SB16State
*s
= opaque
;
1181 int till
, copy
, written
, free
;
1183 if (s
->block_size
<= 0) {
1184 dolog ("invalid block size=%d nchan=%d dma_pos=%d dma_len=%d\n",
1185 s
->block_size
, nchan
, dma_pos
, dma_len
);
1189 if (s
->left_till_irq
< 0) {
1190 s
->left_till_irq
= s
->block_size
;
1194 free
= s
->audio_free
& ~s
->align
;
1195 if ((free
<= 0) || !dma_len
) {
1204 till
= s
->left_till_irq
;
1206 #ifdef DEBUG_SB16_MOST
1207 dolog ("pos:%06d %d till:%d len:%d\n",
1208 dma_pos
, free
, till
, dma_len
);
1212 if (0 == s
->dma_auto
) {
1217 written
= write_audio (s
, nchan
, dma_pos
, dma_len
, copy
);
1218 dma_pos
= (dma_pos
+ written
) % dma_len
;
1219 s
->left_till_irq
-= written
;
1221 if (s
->left_till_irq
<= 0) {
1222 s
->mixer_regs
[0x82] |= (nchan
& 4) ? 2 : 1;
1223 qemu_irq_raise (s
->pic
);
1224 if (0 == s
->dma_auto
) {
1230 #ifdef DEBUG_SB16_MOST
1231 ldebug ("pos %5d free %5d size %5d till % 5d copy %5d written %5d size %5d\n",
1232 dma_pos
, free
, dma_len
, s
->left_till_irq
, copy
, written
,
1236 while (s
->left_till_irq
<= 0) {
1237 s
->left_till_irq
= s
->block_size
+ s
->left_till_irq
;
1243 static void SB_audio_callback (void *opaque
, int free
)
1245 SB16State
*s
= opaque
;
1246 s
->audio_free
= free
;
1249 static int sb16_post_load (void *opaque
, int version_id
)
1251 SB16State
*s
= opaque
;
1254 AUD_close_out (&s
->card
, s
->voice
);
1258 if (s
->dma_running
) {
1260 struct audsettings as
;
1265 as
.nchannels
= 1 << s
->fmt_stereo
;
1269 s
->voice
= AUD_open_out (
1280 speaker (s
, s
->speaker
);
1285 static const VMStateDescription vmstate_sb16
= {
1288 .minimum_version_id
= 1,
1289 .minimum_version_id_old
= 1,
1290 .post_load
= sb16_post_load
,
1291 .fields
= (VMStateField
[]) {
1292 VMSTATE_UINT32(irq
, SB16State
),
1293 VMSTATE_UINT32(dma
, SB16State
),
1294 VMSTATE_UINT32(hdma
, SB16State
),
1295 VMSTATE_UINT32(port
, SB16State
),
1296 VMSTATE_UINT32(ver
, SB16State
),
1297 VMSTATE_INT32(in_index
, SB16State
),
1298 VMSTATE_INT32(out_data_len
, SB16State
),
1299 VMSTATE_INT32(fmt_stereo
, SB16State
),
1300 VMSTATE_INT32(fmt_signed
, SB16State
),
1301 VMSTATE_INT32(fmt_bits
, SB16State
),
1302 VMSTATE_UINT32(fmt
, SB16State
),
1303 VMSTATE_INT32(dma_auto
, SB16State
),
1304 VMSTATE_INT32(block_size
, SB16State
),
1305 VMSTATE_INT32(fifo
, SB16State
),
1306 VMSTATE_INT32(freq
, SB16State
),
1307 VMSTATE_INT32(time_const
, SB16State
),
1308 VMSTATE_INT32(speaker
, SB16State
),
1309 VMSTATE_INT32(needed_bytes
, SB16State
),
1310 VMSTATE_INT32(cmd
, SB16State
),
1311 VMSTATE_INT32(use_hdma
, SB16State
),
1312 VMSTATE_INT32(highspeed
, SB16State
),
1313 VMSTATE_INT32(can_write
, SB16State
),
1314 VMSTATE_INT32(v2x6
, SB16State
),
1316 VMSTATE_UINT8(csp_param
, SB16State
),
1317 VMSTATE_UINT8(csp_value
, SB16State
),
1318 VMSTATE_UINT8(csp_mode
, SB16State
),
1319 VMSTATE_UINT8(csp_param
, SB16State
),
1320 VMSTATE_BUFFER(csp_regs
, SB16State
),
1321 VMSTATE_UINT8(csp_index
, SB16State
),
1322 VMSTATE_BUFFER(csp_reg83
, SB16State
),
1323 VMSTATE_INT32(csp_reg83r
, SB16State
),
1324 VMSTATE_INT32(csp_reg83w
, SB16State
),
1326 VMSTATE_BUFFER(in2_data
, SB16State
),
1327 VMSTATE_BUFFER(out_data
, SB16State
),
1328 VMSTATE_UINT8(test_reg
, SB16State
),
1329 VMSTATE_UINT8(last_read_byte
, SB16State
),
1331 VMSTATE_INT32(nzero
, SB16State
),
1332 VMSTATE_INT32(left_till_irq
, SB16State
),
1333 VMSTATE_INT32(dma_running
, SB16State
),
1334 VMSTATE_INT32(bytes_per_second
, SB16State
),
1335 VMSTATE_INT32(align
, SB16State
),
1337 VMSTATE_INT32(mixer_nreg
, SB16State
),
1338 VMSTATE_BUFFER(mixer_regs
, SB16State
),
1340 VMSTATE_END_OF_LIST()
1344 static const MemoryRegionPortio sb16_ioport_list
[] = {
1345 { 4, 1, 1, .write
= mixer_write_indexb
},
1346 { 4, 1, 2, .write
= mixer_write_indexw
},
1347 { 5, 1, 1, .read
= mixer_read
, .write
= mixer_write_datab
},
1348 { 6, 1, 1, .read
= dsp_read
, .write
= dsp_write
},
1349 { 10, 1, 1, .read
= dsp_read
},
1350 { 12, 1, 1, .write
= dsp_write
},
1351 { 12, 4, 1, .read
= dsp_read
},
1352 PORTIO_END_OF_LIST(),
1356 static int sb16_initfn (ISADevice
*dev
)
1360 s
= DO_UPCAST (SB16State
, dev
, dev
);
1363 isa_init_irq (dev
, &s
->pic
, s
->irq
);
1365 s
->mixer_regs
[0x80] = magic_of_irq (s
->irq
);
1366 s
->mixer_regs
[0x81] = (1 << s
->dma
) | (1 << s
->hdma
);
1367 s
->mixer_regs
[0x82] = 2 << 5;
1370 s
->csp_regs
[9] = 0xf8;
1373 s
->aux_ts
= qemu_new_timer_ns (vm_clock
, aux_timer
, s
);
1375 dolog ("warning: Could not create auxiliary timer\n");
1378 isa_register_portio_list (dev
, s
->port
, sb16_ioport_list
, s
, "sb16");
1380 DMA_register_channel (s
->hdma
, SB_read_DMA
, s
);
1381 DMA_register_channel (s
->dma
, SB_read_DMA
, s
);
1384 AUD_register_card ("sb16", &s
->card
);
1388 int SB16_init (qemu_irq
*pic
)
1390 isa_create_simple ("sb16");
1394 static ISADeviceInfo sb16_info
= {
1395 .qdev
.name
= "sb16",
1396 .qdev
.desc
= "Creative Sound Blaster 16",
1397 .qdev
.size
= sizeof (SB16State
),
1398 .qdev
.vmsd
= &vmstate_sb16
,
1399 .init
= sb16_initfn
,
1400 .qdev
.props
= (Property
[]) {
1401 DEFINE_PROP_HEX32 ("version", SB16State
, ver
, 0x0405), /* 4.5 */
1402 DEFINE_PROP_HEX32 ("iobase", SB16State
, port
, 0x220),
1403 DEFINE_PROP_UINT32 ("irq", SB16State
, irq
, 5),
1404 DEFINE_PROP_UINT32 ("dma", SB16State
, dma
, 1),
1405 DEFINE_PROP_UINT32 ("dma16", SB16State
, hdma
, 5),
1406 DEFINE_PROP_END_OF_LIST (),
1410 static void sb16_register (void)
1412 isa_qdev_register (&sb16_info
);
1414 device_init (sb16_register
)