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