xref: /openbmc/qemu/hw/audio/sb16.c (revision fcf5ef2a)
1 /*
2  * QEMU Soundblaster 16 emulation
3  *
4  * Copyright (c) 2003-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 "qemu/osdep.h"
25 #include "hw/hw.h"
26 #include "hw/audio/audio.h"
27 #include "audio/audio.h"
28 #include "hw/isa/isa.h"
29 #include "hw/qdev.h"
30 #include "qemu/timer.h"
31 #include "qemu/host-utils.h"
32 
33 #define dolog(...) AUD_log ("sb16", __VA_ARGS__)
34 
35 /* #define DEBUG */
36 /* #define DEBUG_SB16_MOST */
37 
38 #ifdef DEBUG
39 #define ldebug(...) dolog (__VA_ARGS__)
40 #else
41 #define ldebug(...)
42 #endif
43 
44 static const char e3[] = "COPYRIGHT (C) CREATIVE TECHNOLOGY LTD, 1992.";
45 
46 #define TYPE_SB16 "sb16"
47 #define SB16(obj) OBJECT_CHECK (SB16State, (obj), TYPE_SB16)
48 
49 typedef struct SB16State {
50     ISADevice parent_obj;
51 
52     QEMUSoundCard card;
53     qemu_irq pic;
54     uint32_t irq;
55     uint32_t dma;
56     uint32_t hdma;
57     uint32_t port;
58     uint32_t ver;
59     IsaDma *isa_dma;
60     IsaDma *isa_hdma;
61 
62     int in_index;
63     int out_data_len;
64     int fmt_stereo;
65     int fmt_signed;
66     int fmt_bits;
67     audfmt_e fmt;
68     int dma_auto;
69     int block_size;
70     int fifo;
71     int freq;
72     int time_const;
73     int speaker;
74     int needed_bytes;
75     int cmd;
76     int use_hdma;
77     int highspeed;
78     int can_write;
79 
80     int v2x6;
81 
82     uint8_t csp_param;
83     uint8_t csp_value;
84     uint8_t csp_mode;
85     uint8_t csp_regs[256];
86     uint8_t csp_index;
87     uint8_t csp_reg83[4];
88     int csp_reg83r;
89     int csp_reg83w;
90 
91     uint8_t in2_data[10];
92     uint8_t out_data[50];
93     uint8_t test_reg;
94     uint8_t last_read_byte;
95     int nzero;
96 
97     int left_till_irq;
98 
99     int dma_running;
100     int bytes_per_second;
101     int align;
102     int audio_free;
103     SWVoiceOut *voice;
104 
105     QEMUTimer *aux_ts;
106     /* mixer state */
107     int mixer_nreg;
108     uint8_t mixer_regs[256];
109     PortioList portio_list;
110 } SB16State;
111 
112 static void SB_audio_callback (void *opaque, int free);
113 
114 static int magic_of_irq (int irq)
115 {
116     switch (irq) {
117     case 5:
118         return 2;
119     case 7:
120         return 4;
121     case 9:
122         return 1;
123     case 10:
124         return 8;
125     default:
126         dolog ("bad irq %d\n", irq);
127         return 2;
128     }
129 }
130 
131 static int irq_of_magic (int magic)
132 {
133     switch (magic) {
134     case 1:
135         return 9;
136     case 2:
137         return 5;
138     case 4:
139         return 7;
140     case 8:
141         return 10;
142     default:
143         dolog ("bad irq magic %d\n", magic);
144         return -1;
145     }
146 }
147 
148 #if 0
149 static void log_dsp (SB16State *dsp)
150 {
151     ldebug ("%s:%s:%d:%s:dmasize=%d:freq=%d:const=%d:speaker=%d\n",
152             dsp->fmt_stereo ? "Stereo" : "Mono",
153             dsp->fmt_signed ? "Signed" : "Unsigned",
154             dsp->fmt_bits,
155             dsp->dma_auto ? "Auto" : "Single",
156             dsp->block_size,
157             dsp->freq,
158             dsp->time_const,
159             dsp->speaker);
160 }
161 #endif
162 
163 static void speaker (SB16State *s, int on)
164 {
165     s->speaker = on;
166     /* AUD_enable (s->voice, on); */
167 }
168 
169 static void control (SB16State *s, int hold)
170 {
171     int dma = s->use_hdma ? s->hdma : s->dma;
172     IsaDma *isa_dma = s->use_hdma ? s->isa_hdma : s->isa_dma;
173     IsaDmaClass *k = ISADMA_GET_CLASS(isa_dma);
174     s->dma_running = hold;
175 
176     ldebug ("hold %d high %d dma %d\n", hold, s->use_hdma, dma);
177 
178     if (hold) {
179         k->hold_DREQ(isa_dma, dma);
180         AUD_set_active_out (s->voice, 1);
181     }
182     else {
183         k->release_DREQ(isa_dma, dma);
184         AUD_set_active_out (s->voice, 0);
185     }
186 }
187 
188 static void aux_timer (void *opaque)
189 {
190     SB16State *s = opaque;
191     s->can_write = 1;
192     qemu_irq_raise (s->pic);
193 }
194 
195 #define DMA8_AUTO 1
196 #define DMA8_HIGH 2
197 
198 static void continue_dma8 (SB16State *s)
199 {
200     if (s->freq > 0) {
201         struct audsettings as;
202 
203         s->audio_free = 0;
204 
205         as.freq = s->freq;
206         as.nchannels = 1 << s->fmt_stereo;
207         as.fmt = s->fmt;
208         as.endianness = 0;
209 
210         s->voice = AUD_open_out (
211             &s->card,
212             s->voice,
213             "sb16",
214             s,
215             SB_audio_callback,
216             &as
217             );
218     }
219 
220     control (s, 1);
221 }
222 
223 static void dma_cmd8 (SB16State *s, int mask, int dma_len)
224 {
225     s->fmt = AUD_FMT_U8;
226     s->use_hdma = 0;
227     s->fmt_bits = 8;
228     s->fmt_signed = 0;
229     s->fmt_stereo = (s->mixer_regs[0x0e] & 2) != 0;
230     if (-1 == s->time_const) {
231         if (s->freq <= 0)
232             s->freq = 11025;
233     }
234     else {
235         int tmp = (256 - s->time_const);
236         s->freq = (1000000 + (tmp / 2)) / tmp;
237     }
238 
239     if (dma_len != -1) {
240         s->block_size = dma_len << s->fmt_stereo;
241     }
242     else {
243         /* This is apparently the only way to make both Act1/PL
244            and SecondReality/FC work
245 
246            Act1 sets block size via command 0x48 and it's an odd number
247            SR does the same with even number
248            Both use stereo, and Creatives own documentation states that
249            0x48 sets block size in bytes less one.. go figure */
250         s->block_size &= ~s->fmt_stereo;
251     }
252 
253     s->freq >>= s->fmt_stereo;
254     s->left_till_irq = s->block_size;
255     s->bytes_per_second = (s->freq << s->fmt_stereo);
256     /* s->highspeed = (mask & DMA8_HIGH) != 0; */
257     s->dma_auto = (mask & DMA8_AUTO) != 0;
258     s->align = (1 << s->fmt_stereo) - 1;
259 
260     if (s->block_size & s->align) {
261         dolog ("warning: misaligned block size %d, alignment %d\n",
262                s->block_size, s->align + 1);
263     }
264 
265     ldebug ("freq %d, stereo %d, sign %d, bits %d, "
266             "dma %d, auto %d, fifo %d, high %d\n",
267             s->freq, s->fmt_stereo, s->fmt_signed, s->fmt_bits,
268             s->block_size, s->dma_auto, s->fifo, s->highspeed);
269 
270     continue_dma8 (s);
271     speaker (s, 1);
272 }
273 
274 static void dma_cmd (SB16State *s, uint8_t cmd, uint8_t d0, int dma_len)
275 {
276     s->use_hdma = cmd < 0xc0;
277     s->fifo = (cmd >> 1) & 1;
278     s->dma_auto = (cmd >> 2) & 1;
279     s->fmt_signed = (d0 >> 4) & 1;
280     s->fmt_stereo = (d0 >> 5) & 1;
281 
282     switch (cmd >> 4) {
283     case 11:
284         s->fmt_bits = 16;
285         break;
286 
287     case 12:
288         s->fmt_bits = 8;
289         break;
290     }
291 
292     if (-1 != s->time_const) {
293 #if 1
294         int tmp = 256 - s->time_const;
295         s->freq = (1000000 + (tmp / 2)) / tmp;
296 #else
297         /* s->freq = 1000000 / ((255 - s->time_const) << s->fmt_stereo); */
298         s->freq = 1000000 / ((255 - s->time_const));
299 #endif
300         s->time_const = -1;
301     }
302 
303     s->block_size = dma_len + 1;
304     s->block_size <<= (s->fmt_bits == 16);
305     if (!s->dma_auto) {
306         /* It is clear that for DOOM and auto-init this value
307            shouldn't take stereo into account, while Miles Sound Systems
308            setsound.exe with single transfer mode wouldn't work without it
309            wonders of SB16 yet again */
310         s->block_size <<= s->fmt_stereo;
311     }
312 
313     ldebug ("freq %d, stereo %d, sign %d, bits %d, "
314             "dma %d, auto %d, fifo %d, high %d\n",
315             s->freq, s->fmt_stereo, s->fmt_signed, s->fmt_bits,
316             s->block_size, s->dma_auto, s->fifo, s->highspeed);
317 
318     if (16 == s->fmt_bits) {
319         if (s->fmt_signed) {
320             s->fmt = AUD_FMT_S16;
321         }
322         else {
323             s->fmt = AUD_FMT_U16;
324         }
325     }
326     else {
327         if (s->fmt_signed) {
328             s->fmt = AUD_FMT_S8;
329         }
330         else {
331             s->fmt = AUD_FMT_U8;
332         }
333     }
334 
335     s->left_till_irq = s->block_size;
336 
337     s->bytes_per_second = (s->freq << s->fmt_stereo) << (s->fmt_bits == 16);
338     s->highspeed = 0;
339     s->align = (1 << (s->fmt_stereo + (s->fmt_bits == 16))) - 1;
340     if (s->block_size & s->align) {
341         dolog ("warning: misaligned block size %d, alignment %d\n",
342                s->block_size, s->align + 1);
343     }
344 
345     if (s->freq) {
346         struct audsettings as;
347 
348         s->audio_free = 0;
349 
350         as.freq = s->freq;
351         as.nchannels = 1 << s->fmt_stereo;
352         as.fmt = s->fmt;
353         as.endianness = 0;
354 
355         s->voice = AUD_open_out (
356             &s->card,
357             s->voice,
358             "sb16",
359             s,
360             SB_audio_callback,
361             &as
362             );
363     }
364 
365     control (s, 1);
366     speaker (s, 1);
367 }
368 
369 static inline void dsp_out_data (SB16State *s, uint8_t val)
370 {
371     ldebug ("outdata %#x\n", val);
372     if ((size_t) s->out_data_len < sizeof (s->out_data)) {
373         s->out_data[s->out_data_len++] = val;
374     }
375 }
376 
377 static inline uint8_t dsp_get_data (SB16State *s)
378 {
379     if (s->in_index) {
380         return s->in2_data[--s->in_index];
381     }
382     else {
383         dolog ("buffer underflow\n");
384         return 0;
385     }
386 }
387 
388 static void command (SB16State *s, uint8_t cmd)
389 {
390     ldebug ("command %#x\n", cmd);
391 
392     if (cmd > 0xaf && cmd < 0xd0) {
393         if (cmd & 8) {
394             dolog ("ADC not yet supported (command %#x)\n", cmd);
395         }
396 
397         switch (cmd >> 4) {
398         case 11:
399         case 12:
400             break;
401         default:
402             dolog ("%#x wrong bits\n", cmd);
403         }
404         s->needed_bytes = 3;
405     }
406     else {
407         s->needed_bytes = 0;
408 
409         switch (cmd) {
410         case 0x03:
411             dsp_out_data (s, 0x10); /* s->csp_param); */
412             goto warn;
413 
414         case 0x04:
415             s->needed_bytes = 1;
416             goto warn;
417 
418         case 0x05:
419             s->needed_bytes = 2;
420             goto warn;
421 
422         case 0x08:
423             /* __asm__ ("int3"); */
424             goto warn;
425 
426         case 0x0e:
427             s->needed_bytes = 2;
428             goto warn;
429 
430         case 0x09:
431             dsp_out_data (s, 0xf8);
432             goto warn;
433 
434         case 0x0f:
435             s->needed_bytes = 1;
436             goto warn;
437 
438         case 0x10:
439             s->needed_bytes = 1;
440             goto warn;
441 
442         case 0x14:
443             s->needed_bytes = 2;
444             s->block_size = 0;
445             break;
446 
447         case 0x1c:              /* Auto-Initialize DMA DAC, 8-bit */
448             dma_cmd8 (s, DMA8_AUTO, -1);
449             break;
450 
451         case 0x20:              /* Direct ADC, Juice/PL */
452             dsp_out_data (s, 0xff);
453             goto warn;
454 
455         case 0x35:
456             dolog ("0x35 - MIDI command not implemented\n");
457             break;
458 
459         case 0x40:
460             s->freq = -1;
461             s->time_const = -1;
462             s->needed_bytes = 1;
463             break;
464 
465         case 0x41:
466             s->freq = -1;
467             s->time_const = -1;
468             s->needed_bytes = 2;
469             break;
470 
471         case 0x42:
472             s->freq = -1;
473             s->time_const = -1;
474             s->needed_bytes = 2;
475             goto warn;
476 
477         case 0x45:
478             dsp_out_data (s, 0xaa);
479             goto warn;
480 
481         case 0x47:                /* Continue Auto-Initialize DMA 16bit */
482             break;
483 
484         case 0x48:
485             s->needed_bytes = 2;
486             break;
487 
488         case 0x74:
489             s->needed_bytes = 2; /* DMA DAC, 4-bit ADPCM */
490             dolog ("0x75 - DMA DAC, 4-bit ADPCM not implemented\n");
491             break;
492 
493         case 0x75:              /* DMA DAC, 4-bit ADPCM Reference */
494             s->needed_bytes = 2;
495             dolog ("0x74 - DMA DAC, 4-bit ADPCM Reference not implemented\n");
496             break;
497 
498         case 0x76:              /* DMA DAC, 2.6-bit ADPCM */
499             s->needed_bytes = 2;
500             dolog ("0x74 - DMA DAC, 2.6-bit ADPCM not implemented\n");
501             break;
502 
503         case 0x77:              /* DMA DAC, 2.6-bit ADPCM Reference */
504             s->needed_bytes = 2;
505             dolog ("0x74 - DMA DAC, 2.6-bit ADPCM Reference not implemented\n");
506             break;
507 
508         case 0x7d:
509             dolog ("0x7d - Autio-Initialize DMA DAC, 4-bit ADPCM Reference\n");
510             dolog ("not implemented\n");
511             break;
512 
513         case 0x7f:
514             dolog (
515                 "0x7d - Autio-Initialize DMA DAC, 2.6-bit ADPCM Reference\n"
516                 );
517             dolog ("not implemented\n");
518             break;
519 
520         case 0x80:
521             s->needed_bytes = 2;
522             break;
523 
524         case 0x90:
525         case 0x91:
526             dma_cmd8 (s, ((cmd & 1) == 0) | DMA8_HIGH, -1);
527             break;
528 
529         case 0xd0:              /* halt DMA operation. 8bit */
530             control (s, 0);
531             break;
532 
533         case 0xd1:              /* speaker on */
534             speaker (s, 1);
535             break;
536 
537         case 0xd3:              /* speaker off */
538             speaker (s, 0);
539             break;
540 
541         case 0xd4:              /* continue DMA operation. 8bit */
542             /* KQ6 (or maybe Sierras audblst.drv in general) resets
543                the frequency between halt/continue */
544             continue_dma8 (s);
545             break;
546 
547         case 0xd5:              /* halt DMA operation. 16bit */
548             control (s, 0);
549             break;
550 
551         case 0xd6:              /* continue DMA operation. 16bit */
552             control (s, 1);
553             break;
554 
555         case 0xd9:              /* exit auto-init DMA after this block. 16bit */
556             s->dma_auto = 0;
557             break;
558 
559         case 0xda:              /* exit auto-init DMA after this block. 8bit */
560             s->dma_auto = 0;
561             break;
562 
563         case 0xe0:              /* DSP identification */
564             s->needed_bytes = 1;
565             break;
566 
567         case 0xe1:
568             dsp_out_data (s, s->ver & 0xff);
569             dsp_out_data (s, s->ver >> 8);
570             break;
571 
572         case 0xe2:
573             s->needed_bytes = 1;
574             goto warn;
575 
576         case 0xe3:
577             {
578                 int i;
579                 for (i = sizeof (e3) - 1; i >= 0; --i)
580                     dsp_out_data (s, e3[i]);
581             }
582             break;
583 
584         case 0xe4:              /* write test reg */
585             s->needed_bytes = 1;
586             break;
587 
588         case 0xe7:
589             dolog ("Attempt to probe for ESS (0xe7)?\n");
590             break;
591 
592         case 0xe8:              /* read test reg */
593             dsp_out_data (s, s->test_reg);
594             break;
595 
596         case 0xf2:
597         case 0xf3:
598             dsp_out_data (s, 0xaa);
599             s->mixer_regs[0x82] |= (cmd == 0xf2) ? 1 : 2;
600             qemu_irq_raise (s->pic);
601             break;
602 
603         case 0xf9:
604             s->needed_bytes = 1;
605             goto warn;
606 
607         case 0xfa:
608             dsp_out_data (s, 0);
609             goto warn;
610 
611         case 0xfc:              /* FIXME */
612             dsp_out_data (s, 0);
613             goto warn;
614 
615         default:
616             dolog ("Unrecognized command %#x\n", cmd);
617             break;
618         }
619     }
620 
621     if (!s->needed_bytes) {
622         ldebug ("\n");
623     }
624 
625  exit:
626     if (!s->needed_bytes) {
627         s->cmd = -1;
628     }
629     else {
630         s->cmd = cmd;
631     }
632     return;
633 
634  warn:
635     dolog ("warning: command %#x,%d is not truly understood yet\n",
636            cmd, s->needed_bytes);
637     goto exit;
638 
639 }
640 
641 static uint16_t dsp_get_lohi (SB16State *s)
642 {
643     uint8_t hi = dsp_get_data (s);
644     uint8_t lo = dsp_get_data (s);
645     return (hi << 8) | lo;
646 }
647 
648 static uint16_t dsp_get_hilo (SB16State *s)
649 {
650     uint8_t lo = dsp_get_data (s);
651     uint8_t hi = dsp_get_data (s);
652     return (hi << 8) | lo;
653 }
654 
655 static void complete (SB16State *s)
656 {
657     int d0, d1, d2;
658     ldebug ("complete command %#x, in_index %d, needed_bytes %d\n",
659             s->cmd, s->in_index, s->needed_bytes);
660 
661     if (s->cmd > 0xaf && s->cmd < 0xd0) {
662         d2 = dsp_get_data (s);
663         d1 = dsp_get_data (s);
664         d0 = dsp_get_data (s);
665 
666         if (s->cmd & 8) {
667             dolog ("ADC params cmd = %#x d0 = %d, d1 = %d, d2 = %d\n",
668                    s->cmd, d0, d1, d2);
669         }
670         else {
671             ldebug ("cmd = %#x d0 = %d, d1 = %d, d2 = %d\n",
672                     s->cmd, d0, d1, d2);
673             dma_cmd (s, s->cmd, d0, d1 + (d2 << 8));
674         }
675     }
676     else {
677         switch (s->cmd) {
678         case 0x04:
679             s->csp_mode = dsp_get_data (s);
680             s->csp_reg83r = 0;
681             s->csp_reg83w = 0;
682             ldebug ("CSP command 0x04: mode=%#x\n", s->csp_mode);
683             break;
684 
685         case 0x05:
686             s->csp_param = dsp_get_data (s);
687             s->csp_value = dsp_get_data (s);
688             ldebug ("CSP command 0x05: param=%#x value=%#x\n",
689                     s->csp_param,
690                     s->csp_value);
691             break;
692 
693         case 0x0e:
694             d0 = dsp_get_data (s);
695             d1 = dsp_get_data (s);
696             ldebug ("write CSP register %d <- %#x\n", d1, d0);
697             if (d1 == 0x83) {
698                 ldebug ("0x83[%d] <- %#x\n", s->csp_reg83r, d0);
699                 s->csp_reg83[s->csp_reg83r % 4] = d0;
700                 s->csp_reg83r += 1;
701             }
702             else {
703                 s->csp_regs[d1] = d0;
704             }
705             break;
706 
707         case 0x0f:
708             d0 = dsp_get_data (s);
709             ldebug ("read CSP register %#x -> %#x, mode=%#x\n",
710                     d0, s->csp_regs[d0], s->csp_mode);
711             if (d0 == 0x83) {
712                 ldebug ("0x83[%d] -> %#x\n",
713                         s->csp_reg83w,
714                         s->csp_reg83[s->csp_reg83w % 4]);
715                 dsp_out_data (s, s->csp_reg83[s->csp_reg83w % 4]);
716                 s->csp_reg83w += 1;
717             }
718             else {
719                 dsp_out_data (s, s->csp_regs[d0]);
720             }
721             break;
722 
723         case 0x10:
724             d0 = dsp_get_data (s);
725             dolog ("cmd 0x10 d0=%#x\n", d0);
726             break;
727 
728         case 0x14:
729             dma_cmd8 (s, 0, dsp_get_lohi (s) + 1);
730             break;
731 
732         case 0x40:
733             s->time_const = dsp_get_data (s);
734             ldebug ("set time const %d\n", s->time_const);
735             break;
736 
737         case 0x42:              /* FT2 sets output freq with this, go figure */
738 #if 0
739             dolog ("cmd 0x42 might not do what it think it should\n");
740 #endif
741         case 0x41:
742             s->freq = dsp_get_hilo (s);
743             ldebug ("set freq %d\n", s->freq);
744             break;
745 
746         case 0x48:
747             s->block_size = dsp_get_lohi (s) + 1;
748             ldebug ("set dma block len %d\n", s->block_size);
749             break;
750 
751         case 0x74:
752         case 0x75:
753         case 0x76:
754         case 0x77:
755             /* ADPCM stuff, ignore */
756             break;
757 
758         case 0x80:
759             {
760                 int freq, samples, bytes;
761                 int64_t ticks;
762 
763                 freq = s->freq > 0 ? s->freq : 11025;
764                 samples = dsp_get_lohi (s) + 1;
765                 bytes = samples << s->fmt_stereo << (s->fmt_bits == 16);
766                 ticks = muldiv64(bytes, NANOSECONDS_PER_SECOND, freq);
767                 if (ticks < NANOSECONDS_PER_SECOND / 1024) {
768                     qemu_irq_raise (s->pic);
769                 }
770                 else {
771                     if (s->aux_ts) {
772                         timer_mod (
773                             s->aux_ts,
774                             qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ticks
775                             );
776                     }
777                 }
778                 ldebug ("mix silence %d %d %" PRId64 "\n", samples, bytes, ticks);
779             }
780             break;
781 
782         case 0xe0:
783             d0 = dsp_get_data (s);
784             s->out_data_len = 0;
785             ldebug ("E0 data = %#x\n", d0);
786             dsp_out_data (s, ~d0);
787             break;
788 
789         case 0xe2:
790 #ifdef DEBUG
791             d0 = dsp_get_data (s);
792             dolog ("E2 = %#x\n", d0);
793 #endif
794             break;
795 
796         case 0xe4:
797             s->test_reg = dsp_get_data (s);
798             break;
799 
800         case 0xf9:
801             d0 = dsp_get_data (s);
802             ldebug ("command 0xf9 with %#x\n", d0);
803             switch (d0) {
804             case 0x0e:
805                 dsp_out_data (s, 0xff);
806                 break;
807 
808             case 0x0f:
809                 dsp_out_data (s, 0x07);
810                 break;
811 
812             case 0x37:
813                 dsp_out_data (s, 0x38);
814                 break;
815 
816             default:
817                 dsp_out_data (s, 0x00);
818                 break;
819             }
820             break;
821 
822         default:
823             dolog ("complete: unrecognized command %#x\n", s->cmd);
824             return;
825         }
826     }
827 
828     ldebug ("\n");
829     s->cmd = -1;
830 }
831 
832 static void legacy_reset (SB16State *s)
833 {
834     struct audsettings as;
835 
836     s->freq = 11025;
837     s->fmt_signed = 0;
838     s->fmt_bits = 8;
839     s->fmt_stereo = 0;
840 
841     as.freq = s->freq;
842     as.nchannels = 1;
843     as.fmt = AUD_FMT_U8;
844     as.endianness = 0;
845 
846     s->voice = AUD_open_out (
847         &s->card,
848         s->voice,
849         "sb16",
850         s,
851         SB_audio_callback,
852         &as
853         );
854 
855     /* Not sure about that... */
856     /* AUD_set_active_out (s->voice, 1); */
857 }
858 
859 static void reset (SB16State *s)
860 {
861     qemu_irq_lower (s->pic);
862     if (s->dma_auto) {
863         qemu_irq_raise (s->pic);
864         qemu_irq_lower (s->pic);
865     }
866 
867     s->mixer_regs[0x82] = 0;
868     s->dma_auto = 0;
869     s->in_index = 0;
870     s->out_data_len = 0;
871     s->left_till_irq = 0;
872     s->needed_bytes = 0;
873     s->block_size = -1;
874     s->nzero = 0;
875     s->highspeed = 0;
876     s->v2x6 = 0;
877     s->cmd = -1;
878 
879     dsp_out_data (s, 0xaa);
880     speaker (s, 0);
881     control (s, 0);
882     legacy_reset (s);
883 }
884 
885 static void dsp_write(void *opaque, uint32_t nport, uint32_t val)
886 {
887     SB16State *s = opaque;
888     int iport;
889 
890     iport = nport - s->port;
891 
892     ldebug ("write %#x <- %#x\n", nport, val);
893     switch (iport) {
894     case 0x06:
895         switch (val) {
896         case 0x00:
897             if (s->v2x6 == 1) {
898                 reset (s);
899             }
900             s->v2x6 = 0;
901             break;
902 
903         case 0x01:
904         case 0x03:              /* FreeBSD kludge */
905             s->v2x6 = 1;
906             break;
907 
908         case 0xc6:
909             s->v2x6 = 0;        /* Prince of Persia, csp.sys, diagnose.exe */
910             break;
911 
912         case 0xb8:              /* Panic */
913             reset (s);
914             break;
915 
916         case 0x39:
917             dsp_out_data (s, 0x38);
918             reset (s);
919             s->v2x6 = 0x39;
920             break;
921 
922         default:
923             s->v2x6 = val;
924             break;
925         }
926         break;
927 
928     case 0x0c:                  /* write data or command | write status */
929 /*         if (s->highspeed) */
930 /*             break; */
931 
932         if (s->needed_bytes == 0) {
933             command (s, val);
934 #if 0
935             if (0 == s->needed_bytes) {
936                 log_dsp (s);
937             }
938 #endif
939         }
940         else {
941             if (s->in_index == sizeof (s->in2_data)) {
942                 dolog ("in data overrun\n");
943             }
944             else {
945                 s->in2_data[s->in_index++] = val;
946                 if (s->in_index == s->needed_bytes) {
947                     s->needed_bytes = 0;
948                     complete (s);
949 #if 0
950                     log_dsp (s);
951 #endif
952                 }
953             }
954         }
955         break;
956 
957     default:
958         ldebug ("(nport=%#x, val=%#x)\n", nport, val);
959         break;
960     }
961 }
962 
963 static uint32_t dsp_read(void *opaque, uint32_t nport)
964 {
965     SB16State *s = opaque;
966     int iport, retval, ack = 0;
967 
968     iport = nport - s->port;
969 
970     switch (iport) {
971     case 0x06:                  /* reset */
972         retval = 0xff;
973         break;
974 
975     case 0x0a:                  /* read data */
976         if (s->out_data_len) {
977             retval = s->out_data[--s->out_data_len];
978             s->last_read_byte = retval;
979         }
980         else {
981             if (s->cmd != -1) {
982                 dolog ("empty output buffer for command %#x\n",
983                        s->cmd);
984             }
985             retval = s->last_read_byte;
986             /* goto error; */
987         }
988         break;
989 
990     case 0x0c:                  /* 0 can write */
991         retval = s->can_write ? 0 : 0x80;
992         break;
993 
994     case 0x0d:                  /* timer interrupt clear */
995         /* dolog ("timer interrupt clear\n"); */
996         retval = 0;
997         break;
998 
999     case 0x0e:                  /* data available status | irq 8 ack */
1000         retval = (!s->out_data_len || s->highspeed) ? 0 : 0x80;
1001         if (s->mixer_regs[0x82] & 1) {
1002             ack = 1;
1003             s->mixer_regs[0x82] &= ~1;
1004             qemu_irq_lower (s->pic);
1005         }
1006         break;
1007 
1008     case 0x0f:                  /* irq 16 ack */
1009         retval = 0xff;
1010         if (s->mixer_regs[0x82] & 2) {
1011             ack = 1;
1012             s->mixer_regs[0x82] &= ~2;
1013             qemu_irq_lower (s->pic);
1014         }
1015         break;
1016 
1017     default:
1018         goto error;
1019     }
1020 
1021     if (!ack) {
1022         ldebug ("read %#x -> %#x\n", nport, retval);
1023     }
1024 
1025     return retval;
1026 
1027  error:
1028     dolog ("warning: dsp_read %#x error\n", nport);
1029     return 0xff;
1030 }
1031 
1032 static void reset_mixer (SB16State *s)
1033 {
1034     int i;
1035 
1036     memset (s->mixer_regs, 0xff, 0x7f);
1037     memset (s->mixer_regs + 0x83, 0xff, sizeof (s->mixer_regs) - 0x83);
1038 
1039     s->mixer_regs[0x02] = 4;    /* master volume 3bits */
1040     s->mixer_regs[0x06] = 4;    /* MIDI volume 3bits */
1041     s->mixer_regs[0x08] = 0;    /* CD volume 3bits */
1042     s->mixer_regs[0x0a] = 0;    /* voice volume 2bits */
1043 
1044     /* d5=input filt, d3=lowpass filt, d1,d2=input source */
1045     s->mixer_regs[0x0c] = 0;
1046 
1047     /* d5=output filt, d1=stereo switch */
1048     s->mixer_regs[0x0e] = 0;
1049 
1050     /* voice volume L d5,d7, R d1,d3 */
1051     s->mixer_regs[0x04] = (4 << 5) | (4 << 1);
1052     /* master ... */
1053     s->mixer_regs[0x22] = (4 << 5) | (4 << 1);
1054     /* MIDI ... */
1055     s->mixer_regs[0x26] = (4 << 5) | (4 << 1);
1056 
1057     for (i = 0x30; i < 0x48; i++) {
1058         s->mixer_regs[i] = 0x20;
1059     }
1060 }
1061 
1062 static void mixer_write_indexb(void *opaque, uint32_t nport, uint32_t val)
1063 {
1064     SB16State *s = opaque;
1065     (void) nport;
1066     s->mixer_nreg = val;
1067 }
1068 
1069 static void mixer_write_datab(void *opaque, uint32_t nport, uint32_t val)
1070 {
1071     SB16State *s = opaque;
1072 
1073     (void) nport;
1074     ldebug ("mixer_write [%#x] <- %#x\n", s->mixer_nreg, val);
1075 
1076     switch (s->mixer_nreg) {
1077     case 0x00:
1078         reset_mixer (s);
1079         break;
1080 
1081     case 0x80:
1082         {
1083             int irq = irq_of_magic (val);
1084             ldebug ("setting irq to %d (val=%#x)\n", irq, val);
1085             if (irq > 0) {
1086                 s->irq = irq;
1087             }
1088         }
1089         break;
1090 
1091     case 0x81:
1092         {
1093             int dma, hdma;
1094 
1095             dma = ctz32 (val & 0xf);
1096             hdma = ctz32 (val & 0xf0);
1097             if (dma != s->dma || hdma != s->hdma) {
1098                 dolog (
1099                     "attempt to change DMA "
1100                     "8bit %d(%d), 16bit %d(%d) (val=%#x)\n",
1101                     dma, s->dma, hdma, s->hdma, val);
1102             }
1103 #if 0
1104             s->dma = dma;
1105             s->hdma = hdma;
1106 #endif
1107         }
1108         break;
1109 
1110     case 0x82:
1111         dolog ("attempt to write into IRQ status register (val=%#x)\n",
1112                val);
1113         return;
1114 
1115     default:
1116         if (s->mixer_nreg >= 0x80) {
1117             ldebug ("attempt to write mixer[%#x] <- %#x\n", s->mixer_nreg, val);
1118         }
1119         break;
1120     }
1121 
1122     s->mixer_regs[s->mixer_nreg] = val;
1123 }
1124 
1125 static uint32_t mixer_read(void *opaque, uint32_t nport)
1126 {
1127     SB16State *s = opaque;
1128 
1129     (void) nport;
1130 #ifndef DEBUG_SB16_MOST
1131     if (s->mixer_nreg != 0x82) {
1132         ldebug ("mixer_read[%#x] -> %#x\n",
1133                 s->mixer_nreg, s->mixer_regs[s->mixer_nreg]);
1134     }
1135 #else
1136     ldebug ("mixer_read[%#x] -> %#x\n",
1137             s->mixer_nreg, s->mixer_regs[s->mixer_nreg]);
1138 #endif
1139     return s->mixer_regs[s->mixer_nreg];
1140 }
1141 
1142 static int write_audio (SB16State *s, int nchan, int dma_pos,
1143                         int dma_len, int len)
1144 {
1145     IsaDma *isa_dma = nchan == s->dma ? s->isa_dma : s->isa_hdma;
1146     IsaDmaClass *k = ISADMA_GET_CLASS(isa_dma);
1147     int temp, net;
1148     uint8_t tmpbuf[4096];
1149 
1150     temp = len;
1151     net = 0;
1152 
1153     while (temp) {
1154         int left = dma_len - dma_pos;
1155         int copied;
1156         size_t to_copy;
1157 
1158         to_copy = audio_MIN (temp, left);
1159         if (to_copy > sizeof (tmpbuf)) {
1160             to_copy = sizeof (tmpbuf);
1161         }
1162 
1163         copied = k->read_memory(isa_dma, nchan, tmpbuf, dma_pos, to_copy);
1164         copied = AUD_write (s->voice, tmpbuf, copied);
1165 
1166         temp -= copied;
1167         dma_pos = (dma_pos + copied) % dma_len;
1168         net += copied;
1169 
1170         if (!copied) {
1171             break;
1172         }
1173     }
1174 
1175     return net;
1176 }
1177 
1178 static int SB_read_DMA (void *opaque, int nchan, int dma_pos, int dma_len)
1179 {
1180     SB16State *s = opaque;
1181     int till, copy, written, free;
1182 
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);
1186         return dma_pos;
1187     }
1188 
1189     if (s->left_till_irq < 0) {
1190         s->left_till_irq = s->block_size;
1191     }
1192 
1193     if (s->voice) {
1194         free = s->audio_free & ~s->align;
1195         if ((free <= 0) || !dma_len) {
1196             return dma_pos;
1197         }
1198     }
1199     else {
1200         free = dma_len;
1201     }
1202 
1203     copy = free;
1204     till = s->left_till_irq;
1205 
1206 #ifdef DEBUG_SB16_MOST
1207     dolog ("pos:%06d %d till:%d len:%d\n",
1208            dma_pos, free, till, dma_len);
1209 #endif
1210 
1211     if (till <= copy) {
1212         if (s->dma_auto == 0) {
1213             copy = till;
1214         }
1215     }
1216 
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;
1220 
1221     if (s->left_till_irq <= 0) {
1222         s->mixer_regs[0x82] |= (nchan & 4) ? 2 : 1;
1223         qemu_irq_raise (s->pic);
1224         if (s->dma_auto == 0) {
1225             control (s, 0);
1226             speaker (s, 0);
1227         }
1228     }
1229 
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,
1233             s->block_size);
1234 #endif
1235 
1236     while (s->left_till_irq <= 0) {
1237         s->left_till_irq = s->block_size + s->left_till_irq;
1238     }
1239 
1240     return dma_pos;
1241 }
1242 
1243 static void SB_audio_callback (void *opaque, int free)
1244 {
1245     SB16State *s = opaque;
1246     s->audio_free = free;
1247 }
1248 
1249 static int sb16_post_load (void *opaque, int version_id)
1250 {
1251     SB16State *s = opaque;
1252 
1253     if (s->voice) {
1254         AUD_close_out (&s->card, s->voice);
1255         s->voice = NULL;
1256     }
1257 
1258     if (s->dma_running) {
1259         if (s->freq) {
1260             struct audsettings as;
1261 
1262             s->audio_free = 0;
1263 
1264             as.freq = s->freq;
1265             as.nchannels = 1 << s->fmt_stereo;
1266             as.fmt = s->fmt;
1267             as.endianness = 0;
1268 
1269             s->voice = AUD_open_out (
1270                 &s->card,
1271                 s->voice,
1272                 "sb16",
1273                 s,
1274                 SB_audio_callback,
1275                 &as
1276                 );
1277         }
1278 
1279         control (s, 1);
1280         speaker (s, s->speaker);
1281     }
1282     return 0;
1283 }
1284 
1285 static const VMStateDescription vmstate_sb16 = {
1286     .name = "sb16",
1287     .version_id = 1,
1288     .minimum_version_id = 1,
1289     .post_load = sb16_post_load,
1290     .fields = (VMStateField[]) {
1291         VMSTATE_UINT32 (irq, SB16State),
1292         VMSTATE_UINT32 (dma, SB16State),
1293         VMSTATE_UINT32 (hdma, SB16State),
1294         VMSTATE_UINT32 (port, SB16State),
1295         VMSTATE_UINT32 (ver, SB16State),
1296         VMSTATE_INT32 (in_index, SB16State),
1297         VMSTATE_INT32 (out_data_len, SB16State),
1298         VMSTATE_INT32 (fmt_stereo, SB16State),
1299         VMSTATE_INT32 (fmt_signed, SB16State),
1300         VMSTATE_INT32 (fmt_bits, SB16State),
1301         VMSTATE_UINT32 (fmt, SB16State),
1302         VMSTATE_INT32 (dma_auto, SB16State),
1303         VMSTATE_INT32 (block_size, SB16State),
1304         VMSTATE_INT32 (fifo, SB16State),
1305         VMSTATE_INT32 (freq, SB16State),
1306         VMSTATE_INT32 (time_const, SB16State),
1307         VMSTATE_INT32 (speaker, SB16State),
1308         VMSTATE_INT32 (needed_bytes, SB16State),
1309         VMSTATE_INT32 (cmd, SB16State),
1310         VMSTATE_INT32 (use_hdma, SB16State),
1311         VMSTATE_INT32 (highspeed, SB16State),
1312         VMSTATE_INT32 (can_write, SB16State),
1313         VMSTATE_INT32 (v2x6, SB16State),
1314 
1315         VMSTATE_UINT8 (csp_param, SB16State),
1316         VMSTATE_UINT8 (csp_value, SB16State),
1317         VMSTATE_UINT8 (csp_mode, SB16State),
1318         VMSTATE_UINT8 (csp_param, SB16State),
1319         VMSTATE_BUFFER (csp_regs, SB16State),
1320         VMSTATE_UINT8 (csp_index, SB16State),
1321         VMSTATE_BUFFER (csp_reg83, SB16State),
1322         VMSTATE_INT32 (csp_reg83r, SB16State),
1323         VMSTATE_INT32 (csp_reg83w, SB16State),
1324 
1325         VMSTATE_BUFFER (in2_data, SB16State),
1326         VMSTATE_BUFFER (out_data, SB16State),
1327         VMSTATE_UINT8 (test_reg, SB16State),
1328         VMSTATE_UINT8 (last_read_byte, SB16State),
1329 
1330         VMSTATE_INT32 (nzero, SB16State),
1331         VMSTATE_INT32 (left_till_irq, SB16State),
1332         VMSTATE_INT32 (dma_running, SB16State),
1333         VMSTATE_INT32 (bytes_per_second, SB16State),
1334         VMSTATE_INT32 (align, SB16State),
1335 
1336         VMSTATE_INT32 (mixer_nreg, SB16State),
1337         VMSTATE_BUFFER (mixer_regs, SB16State),
1338 
1339         VMSTATE_END_OF_LIST ()
1340     }
1341 };
1342 
1343 static const MemoryRegionPortio sb16_ioport_list[] = {
1344     {  4, 1, 1, .write = mixer_write_indexb },
1345     {  5, 1, 1, .read = mixer_read, .write = mixer_write_datab },
1346     {  6, 1, 1, .read = dsp_read, .write = dsp_write },
1347     { 10, 1, 1, .read = dsp_read },
1348     { 12, 1, 1, .write = dsp_write },
1349     { 12, 4, 1, .read = dsp_read },
1350     PORTIO_END_OF_LIST (),
1351 };
1352 
1353 
1354 static void sb16_initfn (Object *obj)
1355 {
1356     SB16State *s = SB16 (obj);
1357 
1358     s->cmd = -1;
1359 }
1360 
1361 static void sb16_realizefn (DeviceState *dev, Error **errp)
1362 {
1363     ISADevice *isadev = ISA_DEVICE (dev);
1364     SB16State *s = SB16 (dev);
1365     IsaDmaClass *k;
1366 
1367     isa_init_irq (isadev, &s->pic, s->irq);
1368 
1369     s->mixer_regs[0x80] = magic_of_irq (s->irq);
1370     s->mixer_regs[0x81] = (1 << s->dma) | (1 << s->hdma);
1371     s->mixer_regs[0x82] = 2 << 5;
1372 
1373     s->csp_regs[5] = 1;
1374     s->csp_regs[9] = 0xf8;
1375 
1376     reset_mixer (s);
1377     s->aux_ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, aux_timer, s);
1378     if (!s->aux_ts) {
1379         dolog ("warning: Could not create auxiliary timer\n");
1380     }
1381 
1382     isa_register_portio_list(isadev, &s->portio_list, s->port,
1383                              sb16_ioport_list, s, "sb16");
1384 
1385     s->isa_hdma = isa_get_dma(isa_bus_from_device(isadev), s->hdma);
1386     k = ISADMA_GET_CLASS(s->isa_hdma);
1387     k->register_channel(s->isa_hdma, s->hdma, SB_read_DMA, s);
1388 
1389     s->isa_dma = isa_get_dma(isa_bus_from_device(isadev), s->dma);
1390     k = ISADMA_GET_CLASS(s->isa_dma);
1391     k->register_channel(s->isa_dma, s->dma, SB_read_DMA, s);
1392 
1393     s->can_write = 1;
1394 
1395     AUD_register_card ("sb16", &s->card);
1396 }
1397 
1398 static int SB16_init (ISABus *bus)
1399 {
1400     isa_create_simple (bus, TYPE_SB16);
1401     return 0;
1402 }
1403 
1404 static Property sb16_properties[] = {
1405     DEFINE_PROP_UINT32 ("version", SB16State, ver,  0x0405), /* 4.5 */
1406     DEFINE_PROP_UINT32 ("iobase",  SB16State, port, 0x220),
1407     DEFINE_PROP_UINT32 ("irq",     SB16State, irq,  5),
1408     DEFINE_PROP_UINT32 ("dma",     SB16State, dma,  1),
1409     DEFINE_PROP_UINT32 ("dma16",   SB16State, hdma, 5),
1410     DEFINE_PROP_END_OF_LIST (),
1411 };
1412 
1413 static void sb16_class_initfn (ObjectClass *klass, void *data)
1414 {
1415     DeviceClass *dc = DEVICE_CLASS (klass);
1416 
1417     dc->realize = sb16_realizefn;
1418     set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
1419     dc->desc = "Creative Sound Blaster 16";
1420     dc->vmsd = &vmstate_sb16;
1421     dc->props = sb16_properties;
1422 }
1423 
1424 static const TypeInfo sb16_info = {
1425     .name          = TYPE_SB16,
1426     .parent        = TYPE_ISA_DEVICE,
1427     .instance_size = sizeof (SB16State),
1428     .instance_init = sb16_initfn,
1429     .class_init    = sb16_class_initfn,
1430 };
1431 
1432 static void sb16_register_types (void)
1433 {
1434     type_register_static (&sb16_info);
1435     isa_register_soundhw("sb16", "Creative Sound Blaster 16", SB16_init);
1436 }
1437 
1438 type_init (sb16_register_types)
1439