Index: trunk/src/VBox/Devices/Audio/DevSB16.cpp
===================================================================
--- trunk/src/VBox/Devices/Audio/DevSB16.cpp	(revision 55020)
+++ trunk/src/VBox/Devices/Audio/DevSB16.cpp	(revision 55021)
@@ -73,6 +73,5 @@
 /** Current saved state version. */
 #define SB16_SAVE_STATE_VERSION         2
-/** The version used in VirtualBox version 3.0 and earlier. This didn't include
- * the config dump. */
+/** The version used in VirtualBox version 3.0 and earlier. This didn't include the config dump. */
 #define SB16_SAVE_STATE_VERSION_VBOX_30 1
 #endif /* VBOX */
@@ -96,5 +95,6 @@
 
 #ifndef VBOX
-static struct {
+static struct
+{
     int ver_lo;
     int ver_hi;
@@ -124,5 +124,5 @@
     {
         /** Node for storing this driver in our device driver
-         *  list of AC97STATE. */
+         *  list of SB16STATE. */
         RTLISTNODE                     Node;
         struct
@@ -208,10 +208,9 @@
     int nzero;
 
-    int left_till_irq;
+    int left_till_irq; /** Note: Can be < 0. */
 
     int dma_running;
     int bytes_per_second;
     int align;
-    uint32_t audio_free;
 
 #ifdef VBOX_WITH_PDM_AUDIO_DRIVER
@@ -226,4 +225,5 @@
     uint64_t                       uTicksIO;
 #else
+    uint32_t audio_free;
     SWVoiceOut *voice;
 #endif /* VBOX_WITH_PDM_AUDIO_DRIVER */
@@ -392,11 +392,13 @@
 
 #ifndef VBOX
-    if (hold) {
-        DMA_hold_DREQ (dma);
-        AUD_set_active_out (pThis->voice, 1);
-    }
-    else {
-        DMA_release_DREQ (dma);
-        AUD_set_active_out (pThis->voice, 0);
+    if (hold)
+    {
+        DMA_hold_DREQ(dma);
+        AUD_set_active_out(pThis->voice, 1);
+    }
+    else
+    {
+        DMA_release_DREQ(dma);
+        AUD_set_active_out(pThis->voice, 0);
     }
 #else  /* VBOX */
@@ -453,6 +455,4 @@
     if (pThis->freq > 0)
     {
-        pThis->audio_free = 0;
-
 #ifdef VBOX_WITH_PDM_AUDIO_DRIVER
         PDMAUDIOSTREAMCFG streamCfg;
@@ -465,4 +465,6 @@
         AssertRC(rc);
 #else
+        pThis->audio_free = 0;
+
         audsettings_t streamCfg;
         streamCfg.freq = pThis->freq;
@@ -528,19 +530,17 @@
     pThis->align = (1 << pThis->fmt_stereo) - 1;
 
-    if (pThis->block_size & pThis->align) {
+    if (pThis->block_size & pThis->align)
         LogFlowFunc(("warning: misaligned block size %d, alignment %d\n",
-               pThis->block_size, pThis->align + 1));
-    }
-
-    LogFlowFunc(("freq %d, stereo %d, sign %d, bits %d, "
-            "dma %d, auto %d, fifo %d, high %d\n",
-            pThis->freq, pThis->fmt_stereo, pThis->fmt_signed, pThis->fmt_bits,
-            pThis->block_size, pThis->dma_auto, pThis->fifo, pThis->highspeed));
-
-    continue_dma8 (pThis);
+                     pThis->block_size, pThis->align + 1));
+
+    LogFlowFunc(("freq %d, stereo %d, sign %d, bits %d, dma %d, auto %d, fifo %d, high %d\n",
+                 pThis->freq, pThis->fmt_stereo, pThis->fmt_signed, pThis->fmt_bits,
+                 pThis->block_size, pThis->dma_auto, pThis->fifo, pThis->highspeed));
+
+    continue_dma8(pThis);
     sb16SpeakerControl(pThis, 1);
 }
 
-static void dma_cmd (PSB16STATE pThis, uint8_t cmd, uint8_t d0, int dma_len)
+static void dma_cmd(PSB16STATE pThis, uint8_t cmd, uint8_t d0, int dma_len)
 {
     pThis->use_hdma   = cmd < 0xc0;
@@ -577,8 +577,10 @@
     if (!pThis->dma_auto)
     {
-        /* It is clear that for DOOM and auto-init this value
-           shouldn't take stereo into account, while Miles Sound Systems
-           setsound.exe with single transfer mode wouldn't work without it
-           wonders of SB16 yet again */
+        /*
+         * It is clear that for DOOM and auto-init this value
+         * shouldn't take stereo into account, while Miles Sound Systems
+         * setsound.exe with single transfer mode wouldn't work without it
+         * wonders of SB16 yet again.
+         */
         pThis->block_size <<= pThis->fmt_stereo;
     }
@@ -606,6 +608,4 @@
     if (pThis->freq)
     {
-        pThis->audio_free = 0;
-
 #ifdef VBOX_WITH_PDM_AUDIO_DRIVER
         PDMAUDIOSTREAMCFG streamCfg;
@@ -618,4 +618,6 @@
         AssertRC(rc);
 #else
+        pThis->audio_free = 0;
+
         audsettings_t streamCfg;
         streamCfg.freq = pThis->freq;
@@ -657,257 +659,259 @@
 }
 
-static void command (PSB16STATE pThis, uint8_t cmd)
+static void sb16HandleCommand(PSB16STATE pThis, uint8_t cmd)
 {
     LogFlowFunc(("command %#x\n", cmd));
 
-    if (cmd > 0xaf && cmd < 0xd0) {
-        if (cmd & 8) {
+    if (cmd > 0xaf && cmd < 0xd0)
+    {
+        if (cmd & 8) /** @todo Handle recording. */
             LogFlowFunc(("ADC not yet supported (command %#x)\n", cmd));
+
+        switch (cmd >> 4)
+        {
+            case 11:
+            case 12:
+                break;
+            default:
+                LogFlowFunc(("%#x wrong bits\n", cmd));
         }
 
-        switch (cmd >> 4) {
-        case 11:
-        case 12:
-            break;
-        default:
-            LogFlowFunc(("%#x wrong bits\n", cmd));
+        pThis->needed_bytes = 3;
+    }
+    else
+    {
+        pThis->needed_bytes = 0;
+
+        switch (cmd)
+        {
+            case 0x03:
+                dsp_out_data(pThis, 0x10); /* pThis->csp_param); */
+                goto warn;
+
+            case 0x04:
+                pThis->needed_bytes = 1;
+                goto warn;
+
+            case 0x05:
+                pThis->needed_bytes = 2;
+                goto warn;
+
+            case 0x08:
+                /* __asm__ ("int3"); */
+                goto warn;
+
+            case 0x0e:
+                pThis->needed_bytes = 2;
+                goto warn;
+
+            case 0x09:
+                dsp_out_data(pThis, 0xf8);
+                goto warn;
+
+            case 0x0f:
+                pThis->needed_bytes = 1;
+                goto warn;
+
+            case 0x10:
+                pThis->needed_bytes = 1;
+                goto warn;
+
+            case 0x14:
+                pThis->needed_bytes = 2;
+                pThis->block_size = 0;
+                break;
+
+            case 0x1c:              /* Auto-Initialize DMA DAC, 8-bit */
+                dma_cmd8(pThis, DMA8_AUTO, -1);
+                break;
+
+            case 0x20:              /* Direct ADC, Juice/PL */
+                dsp_out_data(pThis, 0xff);
+                goto warn;
+
+            case 0x35:
+                LogFlowFunc(("0x35 - MIDI command not implemented\n"));
+                break;
+
+            case 0x40:
+                pThis->freq = -1;
+                pThis->time_const = -1;
+                pThis->needed_bytes = 1;
+                break;
+
+            case 0x41:
+                pThis->freq = -1;
+                pThis->time_const = -1;
+                pThis->needed_bytes = 2;
+                break;
+
+            case 0x42:
+                pThis->freq = -1;
+                pThis->time_const = -1;
+                pThis->needed_bytes = 2;
+                goto warn;
+
+            case 0x45:
+                dsp_out_data(pThis, 0xaa);
+                goto warn;
+
+            case 0x47:                /* Continue Auto-Initialize DMA 16bit */
+                break;
+
+            case 0x48:
+                pThis->needed_bytes = 2;
+                break;
+
+            case 0x74:
+                pThis->needed_bytes = 2; /* DMA DAC, 4-bit ADPCM */
+                LogFlowFunc(("0x75 - DMA DAC, 4-bit ADPCM not implemented\n"));
+                break;
+
+            case 0x75:              /* DMA DAC, 4-bit ADPCM Reference */
+                pThis->needed_bytes = 2;
+                LogFlowFunc(("0x74 - DMA DAC, 4-bit ADPCM Reference not implemented\n"));
+                break;
+
+            case 0x76:              /* DMA DAC, 2.6-bit ADPCM */
+                pThis->needed_bytes = 2;
+                LogFlowFunc(("0x74 - DMA DAC, 2.6-bit ADPCM not implemented\n"));
+                break;
+
+            case 0x77:              /* DMA DAC, 2.6-bit ADPCM Reference */
+                pThis->needed_bytes = 2;
+                LogFlowFunc(("0x74 - DMA DAC, 2.6-bit ADPCM Reference not implemented\n"));
+                break;
+
+            case 0x7d:
+                LogFlowFunc(("0x7d - Autio-Initialize DMA DAC, 4-bit ADPCM Reference\n"));
+                LogFlowFunc(("not implemented\n"));
+                break;
+
+            case 0x7f:
+                LogFlowFunc(("0x7d - Autio-Initialize DMA DAC, 2.6-bit ADPCM Reference\n"));
+                LogFlowFunc(("not implemented\n"));
+                break;
+
+            case 0x80:
+                pThis->needed_bytes = 2;
+                break;
+
+            case 0x90:
+            case 0x91:
+                dma_cmd8(pThis, (((cmd & 1) == 0) ? 1 : 0) | DMA8_HIGH, -1);
+                break;
+
+            case 0xd0:              /* halt DMA operation. 8bit */
+                sb16Control(pThis, 0);
+                break;
+
+            case 0xd1:              /* speaker on */
+                sb16SpeakerControl(pThis, 1);
+                break;
+
+            case 0xd3:              /* speaker off */
+                sb16SpeakerControl(pThis, 0);
+                break;
+
+            case 0xd4:              /* continue DMA operation. 8bit */
+                /* KQ6 (or maybe Sierras audblst.drv in general) resets
+                   the frequency between halt/continue */
+                continue_dma8(pThis);
+                break;
+
+            case 0xd5:              /* halt DMA operation. 16bit */
+                sb16Control(pThis, 0);
+                break;
+
+            case 0xd6:              /* continue DMA operation. 16bit */
+                sb16Control(pThis, 1);
+                break;
+
+            case 0xd9:              /* exit auto-init DMA after this block. 16bit */
+                pThis->dma_auto = 0;
+                break;
+
+            case 0xda:              /* exit auto-init DMA after this block. 8bit */
+                pThis->dma_auto = 0;
+                break;
+
+            case 0xe0:              /* DSP identification */
+                pThis->needed_bytes = 1;
+                break;
+
+            case 0xe1:
+                dsp_out_data(pThis, pThis->ver & 0xff);
+                dsp_out_data(pThis, pThis->ver >> 8);
+                break;
+
+            case 0xe2:
+                pThis->needed_bytes = 1;
+                goto warn;
+
+            case 0xe3:
+            {
+                for (size_t i = sizeof (e3) - 1; i >= 0; --i)
+                    dsp_out_data(pThis, e3[i]);
+
+                break;
+            }
+
+            case 0xe4:              /* write test reg */
+                pThis->needed_bytes = 1;
+                break;
+
+            case 0xe7:
+                LogFlowFunc(("Attempt to probe for ESS (0xe7)?\n"));
+                break;
+
+            case 0xe8:              /* read test reg */
+                dsp_out_data(pThis, pThis->test_reg);
+                break;
+
+            case 0xf2:
+            case 0xf3:
+                dsp_out_data(pThis, 0xaa);
+                pThis->mixer_regs[0x82] |= (cmd == 0xf2) ? 1 : 2;
+#ifndef VBOX
+                qemu_irq_raise (pThis->pic[pThis->irq]);
+#else
+                PDMDevHlpISASetIrq(pThis->pDevIns, pThis->irq, 1);
+#endif
+                break;
+
+            case 0xf9:
+                pThis->needed_bytes = 1;
+                goto warn;
+
+            case 0xfa:
+                dsp_out_data (pThis, 0);
+                goto warn;
+
+            case 0xfc:              /* FIXME */
+                dsp_out_data (pThis, 0);
+                goto warn;
+
+            default:
+                LogFlowFunc(("Unrecognized command %#x\n", cmd));
+                break;
         }
-        pThis->needed_bytes = 3;
-    }
-    else {
-        pThis->needed_bytes = 0;
-
-        switch (cmd) {
-        case 0x03:
-            dsp_out_data (pThis, 0x10); /* pThis->csp_param); */
-            goto warn;
-
-        case 0x04:
-            pThis->needed_bytes = 1;
-            goto warn;
-
-        case 0x05:
-            pThis->needed_bytes = 2;
-            goto warn;
-
-        case 0x08:
-            /* __asm__ ("int3"); */
-            goto warn;
-
-        case 0x0e:
-            pThis->needed_bytes = 2;
-            goto warn;
-
-        case 0x09:
-            dsp_out_data (pThis, 0xf8);
-            goto warn;
-
-        case 0x0f:
-            pThis->needed_bytes = 1;
-            goto warn;
-
-        case 0x10:
-            pThis->needed_bytes = 1;
-            goto warn;
-
-        case 0x14:
-            pThis->needed_bytes = 2;
-            pThis->block_size = 0;
-            break;
-
-        case 0x1c:              /* Auto-Initialize DMA DAC, 8-bit */
-            dma_cmd8 (pThis, DMA8_AUTO, -1);
-            break;
-
-        case 0x20:              /* Direct ADC, Juice/PL */
-            dsp_out_data (pThis, 0xff);
-            goto warn;
-
-        case 0x35:
-            LogFlowFunc(("0x35 - MIDI command not implemented\n"));
-            break;
-
-        case 0x40:
-            pThis->freq = -1;
-            pThis->time_const = -1;
-            pThis->needed_bytes = 1;
-            break;
-
-        case 0x41:
-            pThis->freq = -1;
-            pThis->time_const = -1;
-            pThis->needed_bytes = 2;
-            break;
-
-        case 0x42:
-            pThis->freq = -1;
-            pThis->time_const = -1;
-            pThis->needed_bytes = 2;
-            goto warn;
-
-        case 0x45:
-            dsp_out_data (pThis, 0xaa);
-            goto warn;
-
-        case 0x47:                /* Continue Auto-Initialize DMA 16bit */
-            break;
-
-        case 0x48:
-            pThis->needed_bytes = 2;
-            break;
-
-        case 0x74:
-            pThis->needed_bytes = 2; /* DMA DAC, 4-bit ADPCM */
-            LogFlowFunc(("0x75 - DMA DAC, 4-bit ADPCM not implemented\n"));
-            break;
-
-        case 0x75:              /* DMA DAC, 4-bit ADPCM Reference */
-            pThis->needed_bytes = 2;
-            LogFlowFunc(("0x74 - DMA DAC, 4-bit ADPCM Reference not implemented\n"));
-            break;
-
-        case 0x76:              /* DMA DAC, 2.6-bit ADPCM */
-            pThis->needed_bytes = 2;
-            LogFlowFunc(("0x74 - DMA DAC, 2.6-bit ADPCM not implemented\n"));
-            break;
-
-        case 0x77:              /* DMA DAC, 2.6-bit ADPCM Reference */
-            pThis->needed_bytes = 2;
-            LogFlowFunc(("0x74 - DMA DAC, 2.6-bit ADPCM Reference not implemented\n"));
-            break;
-
-        case 0x7d:
-            LogFlowFunc(("0x7d - Autio-Initialize DMA DAC, 4-bit ADPCM Reference\n"));
-            LogFlowFunc(("not implemented\n"));
-            break;
-
-        case 0x7f:
-            LogFlowFunc(("0x7d - Autio-Initialize DMA DAC, 2.6-bit ADPCM Reference\n"));
-            LogFlowFunc(("not implemented\n"));
-            break;
-
-        case 0x80:
-            pThis->needed_bytes = 2;
-            break;
-
-        case 0x90:
-        case 0x91:
-            dma_cmd8(pThis, (((cmd & 1) == 0) ? 1 : 0) | DMA8_HIGH, -1);
-            break;
-
-        case 0xd0:              /* halt DMA operation. 8bit */
-            sb16Control(pThis, 0);
-            break;
-
-        case 0xd1:              /* speaker on */
-            sb16SpeakerControl(pThis, 1);
-            break;
-
-        case 0xd3:              /* speaker off */
-            sb16SpeakerControl(pThis, 0);
-            break;
-
-        case 0xd4:              /* continue DMA operation. 8bit */
-            /* KQ6 (or maybe Sierras audblst.drv in general) resets
-               the frequency between halt/continue */
-            continue_dma8 (pThis);
-            break;
-
-        case 0xd5:              /* halt DMA operation. 16bit */
-            sb16Control(pThis, 0);
-            break;
-
-        case 0xd6:              /* continue DMA operation. 16bit */
-            sb16Control(pThis, 1);
-            break;
-
-        case 0xd9:              /* exit auto-init DMA after this block. 16bit */
-            pThis->dma_auto = 0;
-            break;
-
-        case 0xda:              /* exit auto-init DMA after this block. 8bit */
-            pThis->dma_auto = 0;
-            break;
-
-        case 0xe0:              /* DSP identification */
-            pThis->needed_bytes = 1;
-            break;
-
-        case 0xe1:
-            dsp_out_data (pThis, pThis->ver & 0xff);
-            dsp_out_data (pThis, pThis->ver >> 8);
-            break;
-
-        case 0xe2:
-            pThis->needed_bytes = 1;
-            goto warn;
-
-        case 0xe3:
-            {
-                int i;
-                for (i = sizeof (e3) - 1; i >= 0; --i)
-                    dsp_out_data (pThis, e3[i]);
-            }
-            break;
-
-        case 0xe4:              /* write test reg */
-            pThis->needed_bytes = 1;
-            break;
-
-        case 0xe7:
-            LogFlowFunc(("Attempt to probe for ESS (0xe7)?\n"));
-            break;
-
-        case 0xe8:              /* read test reg */
-            dsp_out_data (pThis, pThis->test_reg);
-            break;
-
-        case 0xf2:
-        case 0xf3:
-            dsp_out_data (pThis, 0xaa);
-            pThis->mixer_regs[0x82] |= (cmd == 0xf2) ? 1 : 2;
-#ifndef VBOX
-            qemu_irq_raise (pThis->pic[pThis->irq]);
-#else
-            PDMDevHlpISASetIrq(pThis->pDevIns, pThis->irq, 1);
-#endif
-            break;
-
-        case 0xf9:
-            pThis->needed_bytes = 1;
-            goto warn;
-
-        case 0xfa:
-            dsp_out_data (pThis, 0);
-            goto warn;
-
-        case 0xfc:              /* FIXME */
-            dsp_out_data (pThis, 0);
-            goto warn;
-
-        default:
-            LogFlowFunc(("Unrecognized command %#x\n", cmd));
-            break;
-        }
-    }
-
-    if (!pThis->needed_bytes) {
+    }
+
+    if (!pThis->needed_bytes)
         LogFlow(("\n"));
-    }
-
- exit:
-    if (!pThis->needed_bytes) {
+
+exit:
+
+     if (!pThis->needed_bytes)
         pThis->cmd = -1;
-    }
-    else {
+     else
         pThis->cmd = cmd;
-    }
+
     return;
 
- warn:
+warn:
     LogFlowFunc(("warning: command %#x,%d is not truly understood yet\n",
-           cmd, pThis->needed_bytes));
+                 cmd, pThis->needed_bytes));
     goto exit;
-
 }
 
@@ -926,5 +930,5 @@
 }
 
-static void complete (PSB16STATE pThis)
+static void complete(PSB16STATE pThis)
 {
     int d0, d1, d2;
@@ -932,21 +936,22 @@
             pThis->cmd, pThis->in_index, pThis->needed_bytes));
 
-    if (pThis->cmd > 0xaf && pThis->cmd < 0xd0) {
+    if (pThis->cmd > 0xaf && pThis->cmd < 0xd0)
+    {
         d2 = dsp_get_data (pThis);
         d1 = dsp_get_data (pThis);
         d0 = dsp_get_data (pThis);
 
-        if (pThis->cmd & 8) {
-            LogFlowFunc(("ADC params cmd = %#x d0 = %d, d1 = %d, d2 = %d\n",
-                   pThis->cmd, d0, d1, d2));
+        if (pThis->cmd & 8)
+            LogFlowFunc(("ADC params cmd = %#x d0 = %d, d1 = %d, d2 = %d\n", pThis->cmd, d0, d1, d2));
+        else
+        {
+            LogFlowFunc(("cmd = %#x d0 = %d, d1 = %d, d2 = %d\n", pThis->cmd, d0, d1, d2));
+            dma_cmd(pThis, pThis->cmd, d0, d1 + (d2 << 8));
         }
-        else {
-            LogFlowFunc(("cmd = %#x d0 = %d, d1 = %d, d2 = %d\n",
-                    pThis->cmd, d0, d1, d2));
-            dma_cmd (pThis, pThis->cmd, d0, d1 + (d2 << 8));
-        }
-    }
-    else {
-        switch (pThis->cmd) {
+    }
+    else
+    {
+        switch (pThis->cmd)
+        {
         case 0x04:
             pThis->csp_mode = dsp_get_data (pThis);
@@ -965,22 +970,24 @@
 
         case 0x0e:
-            d0 = dsp_get_data (pThis);
-            d1 = dsp_get_data (pThis);
+        {
+            d0 = dsp_get_data(pThis);
+            d1 = dsp_get_data(pThis);
             LogFlowFunc(("write CSP register %d <- %#x\n", d1, d0));
-            if (d1 == 0x83) {
+            if (d1 == 0x83)
+            {
                 LogFlowFunc(("0x83[%d] <- %#x\n", pThis->csp_reg83r, d0));
                 pThis->csp_reg83[pThis->csp_reg83r % 4] = d0;
                 pThis->csp_reg83r += 1;
             }
-            else {
+            else
                 pThis->csp_regs[d1] = d0;
-            }
-            break;
+            break;
+        }
 
         case 0x0f:
-            d0 = dsp_get_data (pThis);
-            LogFlowFunc(("read CSP register %#x -> %#x, mode=%#x\n",
-                    d0, pThis->csp_regs[d0], pThis->csp_mode));
-            if (d0 == 0x83) {
+            d0 = dsp_get_data(pThis);
+            LogFlowFunc(("read CSP register %#x -> %#x, mode=%#x\n", d0, pThis->csp_regs[d0], pThis->csp_mode));
+            if (d0 == 0x83)
+            {
                 LogFlowFunc(("0x83[%d] -> %#x\n",
                         pThis->csp_reg83w,
@@ -989,11 +996,10 @@
                 pThis->csp_reg83w += 1;
             }
-            else {
-                dsp_out_data (pThis, pThis->csp_regs[d0]);
-            }
+            else
+                dsp_out_data(pThis, pThis->csp_regs[d0]);
             break;
 
         case 0x10:
-            d0 = dsp_get_data (pThis);
+            d0 = dsp_get_data(pThis);
             LogFlowFunc(("cmd 0x10 d0=%#x\n", d0));
             break;
@@ -1004,5 +1010,5 @@
 
         case 0x40:
-            pThis->time_const = dsp_get_data (pThis);
+            pThis->time_const = dsp_get_data(pThis);
             LogFlowFunc(("set time const %d\n", pThis->time_const));
             break;
@@ -1013,10 +1019,10 @@
 #endif
         case 0x41:
-            pThis->freq = dsp_get_hilo (pThis);
+            pThis->freq = dsp_get_hilo(pThis);
             LogFlowFunc(("set freq %d\n", pThis->freq));
             break;
 
         case 0x48:
-            pThis->block_size = dsp_get_lohi (pThis) + 1;
+            pThis->block_size = dsp_get_lohi(pThis) + 1;
             LogFlowFunc(("set dma block len %d\n", pThis->block_size));
             break;
@@ -1030,70 +1036,71 @@
 
         case 0x80:
-            {
-                int freq, samples, bytes;
-                uint64_t ticks;
-
-                freq = pThis->freq > 0 ? pThis->freq : 11025;
-                samples = dsp_get_lohi (pThis) + 1;
-                bytes = samples << pThis->fmt_stereo << ((pThis->fmt_bits == 16) ? 1 : 0);
+        {
+            int freq, samples, bytes;
+            uint64_t ticks;
+
+            freq = pThis->freq > 0 ? pThis->freq : 11025;
+            samples = dsp_get_lohi (pThis) + 1;
+            bytes = samples << pThis->fmt_stereo << ((pThis->fmt_bits == 16) ? 1 : 0);
 #ifndef VBOX
-                ticks = (bytes * ticks_per_sec) / freq;
-                if (ticks < ticks_per_sec / 1024) {
-                    qemu_irq_raise (pThis->pic[pThis->irq]);
+            ticks = (bytes * ticks_per_sec) / freq;
+            if (ticks < ticks_per_sec / 1024) {
+                qemu_irq_raise (pThis->pic[pThis->irq]);
+            }
+            else {
+                if (pThis->aux_ts) {
+                    qemu_mod_timer (
+                        pThis->aux_ts,
+                        qemu_get_clock (vm_clock) + ticks
+                        );
                 }
-                else {
-                    if (pThis->aux_ts) {
-                        qemu_mod_timer (
-                            pThis->aux_ts,
-                            qemu_get_clock (vm_clock) + ticks
-                            );
-                    }
-                }
-                LogFlowFunc(("mix silence %d %d %" PRId64 "\n", samples, bytes, ticks));
+            }
+            LogFlowFunc(("mix silence %d %d %" PRId64 "\n", samples, bytes, ticks));
 #else  /* VBOX */
-                ticks = (bytes * TMTimerGetFreq(pThis->pTimerIRQ)) / freq;
-                if (ticks < TMTimerGetFreq(pThis->pTimerIRQ) / 1024)
-                    PDMDevHlpISASetIrq(pThis->pDevIns, pThis->irq, 1);
-                else
-                    TMTimerSet(pThis->pTimerIRQ, TMTimerGet(pThis->pTimerIRQ) + ticks);
-                LogFlowFunc(("mix silence %d %d % %RU64\n", samples, bytes, ticks));
+            ticks = (bytes * TMTimerGetFreq(pThis->pTimerIRQ)) / freq;
+            if (ticks < TMTimerGetFreq(pThis->pTimerIRQ) / 1024)
+                PDMDevHlpISASetIrq(pThis->pDevIns, pThis->irq, 1);
+            else
+                TMTimerSet(pThis->pTimerIRQ, TMTimerGet(pThis->pTimerIRQ) + ticks);
+            LogFlowFunc(("mix silence %d %d % %RU64\n", samples, bytes, ticks));
 #endif /* VBOX */
-            }
-            break;
+
+            break;
+        }
 
         case 0xe0:
-            d0 = dsp_get_data (pThis);
+            d0 = dsp_get_data(pThis);
             pThis->out_data_len = 0;
             LogFlowFunc(("E0 data = %#x\n", d0));
-            dsp_out_data (pThis, ~d0);
+            dsp_out_data(pThis, ~d0);
             break;
 
         case 0xe2:
-            d0 = dsp_get_data (pThis);
+            d0 = dsp_get_data(pThis);
             LogFlow(("SB16:E2 = %#x\n", d0));
             break;
 
         case 0xe4:
-            pThis->test_reg = dsp_get_data (pThis);
+            pThis->test_reg = dsp_get_data(pThis);
             break;
 
         case 0xf9:
-            d0 = dsp_get_data (pThis);
+            d0 = dsp_get_data(pThis);
             LogFlowFunc(("command 0xf9 with %#x\n", d0));
             switch (d0) {
             case 0x0e:
-                dsp_out_data (pThis, 0xff);
+                dsp_out_data(pThis, 0xff);
                 break;
 
             case 0x0f:
-                dsp_out_data (pThis, 0x07);
+                dsp_out_data(pThis, 0x07);
                 break;
 
             case 0x37:
-                dsp_out_data (pThis, 0x38);
+                dsp_out_data(pThis, 0x38);
                 break;
 
             default:
-                dsp_out_data (pThis, 0x00);
+                dsp_out_data(pThis, 0x00);
                 break;
             }
@@ -1111,5 +1118,5 @@
 }
 
-static void legacy_reset(PSB16STATE pThis)
+static void sb16ResetLegacy(PSB16STATE pThis)
 {
     pThis->freq       = 11025;
@@ -1157,5 +1164,6 @@
 #else  /* VBOX */
     PDMDevHlpISASetIrq(pThis->pDevIns, pThis->irq, 0);
-    if (pThis->dma_auto) {
+    if (pThis->dma_auto)
+    {
         PDMDevHlpISASetIrq(pThis->pDevIns, pThis->irq, 1);
         PDMDevHlpISASetIrq(pThis->pDevIns, pThis->irq, 0);
@@ -1178,5 +1186,5 @@
     sb16SpeakerControl(pThis, 0);
     sb16Control(pThis, 0);
-    legacy_reset (pThis);
+    sb16ResetLegacy(pThis);
 }
 
@@ -1245,5 +1253,5 @@
             if (0 == pThis->needed_bytes)
             {
-                command(pThis, val);
+                sb16HandleCommand(pThis, val);
 #if 0
                 if (0 == pThis->needed_bytes) {
@@ -1381,5 +1389,5 @@
 
     RTListForEach(&pThis->lstDrv, pDrv, SB16DRIVER, Node)
-        pDrv->Out.phStrmOut   = NULL;
+        pDrv->Out.phStrmOut = NULL;
 
     pThis->pSinkOutput = NULL;
@@ -1726,5 +1734,5 @@
     {
         LogFlowFunc(("invalid block size=%d nchan=%d dma_pos=%d dma_len=%d\n",
-               pThis->block_size, nchan, dma_pos, dma_len));
+                     pThis->block_size, nchan, dma_pos, dma_len));
         return dma_pos;
     }
@@ -1790,5 +1798,4 @@
     written = sb16WriteAudio(pThis, nchan, dma_pos, dma_len, copy);
     dma_pos = (dma_pos + written) % dma_len;
-    Assert(pThis->left_till_irq >= written);
     pThis->left_till_irq -= written;
 
@@ -2036,6 +2043,4 @@
         if (pThis->freq)
         {
-            pThis->audio_free = 0;
-
 #ifdef VBOX_WITH_PDM_AUDIO_DRIVER
             PDMAUDIOSTREAMCFG streamCfg;
@@ -2048,4 +2053,6 @@
             AssertRC(rc);
 #else
+            pThis->audio_free = 0;
+
             audsettings_t as;
             as.freq = pThis->freq;
@@ -2140,5 +2147,5 @@
 static DECLCALLBACK(int) sb16LiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
 {
-    PSB16STATE pThis = PDMINS_2_DATA (pDevIns, PSB16STATE);
+    PSB16STATE pThis = PDMINS_2_DATA(pDevIns, PSB16STATE);
 
     SSMR3PutS32(pSSM, pThis->irqCfg);
@@ -2152,5 +2159,5 @@
 static DECLCALLBACK(int) sb16SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
 {
-    PSB16STATE pThis = PDMINS_2_DATA (pDevIns, PSB16STATE);
+    PSB16STATE pThis = PDMINS_2_DATA(pDevIns, PSB16STATE);
 
     sb16LiveExec (pDevIns, pSSM, 0);
@@ -2161,5 +2168,5 @@
 static DECLCALLBACK(int) sb16LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
 {
-    PSB16STATE pThis = PDMINS_2_DATA (pDevIns, PSB16STATE);
+    PSB16STATE pThis = PDMINS_2_DATA(pDevIns, PSB16STATE);
 
     AssertMsgReturn(    uVersion == SB16_SAVE_STATE_VERSION
@@ -2185,5 +2192,6 @@
             || hdma != pThis->hdmaCfg
             || port != pThis->portCfg
-            || ver  != pThis->verCfg )
+            || ver  != pThis->verCfg)
+        {
             return SSMR3SetCfgError(pSSM, RT_SRC_POS,
                                     N_("config changed: irq=%x/%x dma=%x/%x hdma=%x/%x port=%x/%x ver=%x/%x (saved/config)"),
@@ -2193,5 +2201,7 @@
                                     port, pThis->portCfg,
                                     ver,  pThis->verCfg);
-    }
+        }
+    }
+
     if (uPass != SSM_PASS_FINAL)
         return VINF_SUCCESS;
@@ -2201,4 +2211,5 @@
 }
 
+#ifdef VBOX_WITH_PDM_AUDIO_DRIVER
 static int sb16OpenOut(PSB16STATE pThis, PPDMAUDIOSTREAMCFG pCfg)
 {
@@ -2244,4 +2255,5 @@
     return rc;
 }
+#endif /* VBOX_WITH_PDM_AUDIO_DRIVER */
 
 /**
@@ -2366,21 +2378,4 @@
         AssertMsgFailedReturn(("Error creating IRQ timer, rc=%Rrc\n", rc), rc);
 
-#ifdef VBOX_WITH_PDM_AUDIO_DRIVER
-    rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, sb16TimerIO, pThis,
-                                TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "SB16 IO timer", &pThis->pTimerIO);
-    if (RT_FAILURE(rc))
-        AssertMsgFailedReturn(("Error creating I/O timer, rc=%Rrc\n", rc), rc);
-    else
-    {
-        pThis->uTicksIO = PDMDevHlpTMTimeVirtGetFreq(pDevIns) / 200; /** Hz. @todo Make this configurable! */
-        if (pThis->uTicksIO < 100)
-            pThis->uTicksIO = 100;
-        LogFunc(("I/O timer ticks=%RU64\n", pThis->uTicksIO));
-
-        /* Fire off timer. */
-        TMTimerSet(pThis->pTimerIO, TMTimerGet(pThis->pTimerIO) + pThis->uTicksIO);
-    }
-#endif /* VBOX_WITH_PDM_AUDIO_DRIVER */
-
     rc = PDMDevHlpIOPortRegister(pDevIns, pThis->port + 0x04,  2, pThis,
                                  mixer_write, mixer_read, NULL, NULL, "SB16");
@@ -2428,8 +2423,8 @@
     rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThis->IBase, &pThis->pDrvBase, "Audio Driver Port");
     if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
-        LogFunc(("ac97: No attached driver!\n"));
+        LogFunc(("SB16: No attached driver!\n"));
     else if (RT_FAILURE(rc))
     {
-        AssertMsgFailed(("Failed to attach AC97 LUN #0! rc=%Rrc\n", rc));
+        AssertMsgFailed(("Failed to attach SB16 LUN #0! rc=%Rrc\n", rc));
         return rc;
     }
@@ -2437,5 +2432,5 @@
 
 #ifdef VBOX_WITH_PDM_AUDIO_DRIVER
-    legacy_reset(pThis);
+    sb16ResetLegacy(pThis);
 
     PSB16DRIVER pDrv;
@@ -2443,16 +2438,24 @@
     RTListForEach(&pThis->lstDrv, pDrv, SB16DRIVER, Node)
     {
-        /*
-         * Only primary drivers are critical for the VM to run. Everything else
-         * might not worth showing an own error message box in the GUI.
-         */
-        if (!(pDrv->Flags & PDMAUDIODRVFLAG_PRIMARY))
-            continue;
-
         PPDMIAUDIOCONNECTOR pCon = pDrv->pConnector;
         AssertPtr(pCon);
-        if (!pCon->pfnIsOutputOK(pCon, pDrv->Out.pStrmOut))
+
+        bool fIsOK = pCon->pfnIsOutputOK(pCon, pDrv->Out.pStrmOut);
+        if (fIsOK)
         {
-            LogRel(("SB16: WARNING: Unable to open PCM OUT!\n"));
+            rc = pCon->pfnEnableOut(pCon, pDrv->Out.pStrmOut, true /* fEnable */);
+            fIsOK = RT_SUCCESS(rc);
+        }
+
+        if (!fIsOK)
+        {
+            /*
+             * Only primary drivers are critical for the VM to run. Everything else
+             * might not worth showing an own error message box in the GUI.
+             */
+            if (!(pDrv->Flags & PDMAUDIODRVFLAG_PRIMARY))
+                continue;
+
+            LogRel(("SB16: Warning: Unable to enable/use output for LUN#%RU8\n", uLUN));
 
             pCon->pfnCloseOut(pCon, pDrv->Out.pStrmOut);
@@ -2465,8 +2468,28 @@
                    "with the consequence that no sound is audible"));
         }
+
+        uLUN++;
+    }
+
+    if (RT_SUCCESS(rc))
+    {
+        rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, sb16TimerIO, pThis,
+                                    TMTIMER_FLAGS_DEFAULT_CRIT_SECT, "SB16 IO timer", &pThis->pTimerIO);
+        if (RT_FAILURE(rc))
+            AssertMsgFailedReturn(("Error creating I/O timer, rc=%Rrc\n", rc), rc);
+        else
+        {
+            pThis->uTicksIO = PDMDevHlpTMTimeVirtGetFreq(pDevIns) / 200; /** Hz. @todo Make this configurable! */
+            if (pThis->uTicksIO < 100)
+                pThis->uTicksIO = 100;
+            LogFunc(("I/O timer ticks=%RU64\n", pThis->uTicksIO));
+
+            /* Fire off timer. */
+            TMTimerSet(pThis->pTimerIO, TMTimerGet(pThis->pTimerIO) + pThis->uTicksIO);
+        }
     }
 #else
     AUD_register_card("sb16", &pThis->card);
-    legacy_reset(pThis);
+    sb16ResetLegacy(pThis);
 
     if (!AUD_is_host_voice_out_ok(pThis->voice))
