Index: trunk/src/VBox/Devices/Audio/DevIchAc97.cpp
===================================================================
--- trunk/src/VBox/Devices/Audio/DevIchAc97.cpp	(revision 82361)
+++ trunk/src/VBox/Devices/Audio/DevIchAc97.cpp	(revision 82362)
@@ -48,14 +48,14 @@
 
 /** Current saved state version. */
-#define AC97_SSM_VERSION    1
+#define AC97_SAVED_STATE_VERSION 1
 
 /** Default timer frequency (in Hz). */
-#define AC97_TIMER_HZ_DEFAULT 100
+#define AC97_TIMER_HZ_DEFAULT   100
 
 /** Maximum number of streams we support. */
-#define AC97_MAX_STREAMS      3
+#define AC97_MAX_STREAMS        3
 
 /** Maximum FIFO size (in bytes). */
-#define AC97_FIFO_MAX       256
+#define AC97_FIFO_MAX           256
 
 #define AC97_SR_FIFOE           RT_BIT(4)           /**< rwc, FIFO error. */
@@ -146,17 +146,19 @@
 #define AC97_DB_FACTOR          4
 
-#define AC97_REC_MASK 7
-enum
-{
-    AC97_REC_MIC = 0,
-    AC97_REC_CD,
-    AC97_REC_VIDEO,
-    AC97_REC_AUX,
-    AC97_REC_LINE_IN,
-    AC97_REC_STEREO_MIX,
-    AC97_REC_MONO_MIX,
-    AC97_REC_PHONE
-};
-
+/** @name Recording inputs?
+ * @{ */
+#define AC97_REC_MIC            UINT8_C(0)
+#define AC97_REC_CD             UINT8_C(1)
+#define AC97_REC_VIDEO          UINT8_C(2)
+#define AC97_REC_AUX            UINT8_C(3)
+#define AC97_REC_LINE_IN        UINT8_C(4)
+#define AC97_REC_STEREO_MIX     UINT8_C(5)
+#define AC97_REC_MONO_MIX       UINT8_C(6)
+#define AC97_REC_PHONE          UINT8_C(7)
+#define AC97_REC_MASK           UINT8_C(7)
+/** @} */
+
+/** @name Mixer registers / NAM BAR registers?
+ * @{ */
 enum
 {
@@ -195,24 +197,18 @@
     AC97_Vendor_ID2                = 0x7e
 };
-
-/* Codec models. */
-typedef enum
-{
-    AC97_CODEC_STAC9700 = 1,     /**< SigmaTel STAC9700 */
-    AC97_CODEC_AD1980,           /**< Analog Devices AD1980 */
-    AC97_CODEC_AD1981B           /**< Analog Devices AD1981B */
-} AC97CODEC;
-
-/* Analog Devices miscellaneous regiter bits used in AD1980. */
+/** @} */
+
+/** @name Analog Devices miscellaneous regiter bits used in AD1980.
+ * @{  */
 #define AC97_AD_MISC_LOSEL       RT_BIT(5)   /**< Surround (rear) goes to line out outputs. */
 #define AC97_AD_MISC_HPSEL       RT_BIT(10)  /**< PCM (front) goes to headphone outputs. */
-
-#define ICHAC97STATE_2_DEVINS(a_pAC97)   ((a_pAC97)->CTX_SUFF(pDevIns))
-
-enum
-{
-    BUP_SET  = RT_BIT(0),
-    BUP_LAST = RT_BIT(1)
-};
+/** @} */
+
+
+/** @name BUP flag values.
+ * @{ */
+#define BUP_SET     RT_BIT_32(0)
+#define BUP_LAST    RT_BIT_32(1)
+/** @}   */
 
 /** @name AC'97 source indices.
@@ -513,4 +509,15 @@
 
 
+/* Codec models. */
+typedef enum AC97CODEC
+{
+    AC97CODEC_INVALID = 0,      /**< Customary illegal zero value. */
+    AC97CODEC_STAC9700,         /**< SigmaTel STAC9700 */
+    AC97CODEC_AD1980,           /**< Analog Devices AD1980 */
+    AC97CODEC_AD1981B,          /**< Analog Devices AD1981B */
+    AC97CODEC_32BIT_HACK = 0x7fffffff
+} AC97CODEC;
+
+
 /**
  * The shared AC'97 device state.
@@ -534,7 +541,7 @@
     uint16_t                au16Padding1[3];
     uint8_t                 silence[128];
-    int32_t                 bup_flag;
+    uint32_t                bup_flag;
     /** Codec model. */
-    uint32_t                uCodecModel;
+    AC97CODEC               enmCodecModel;
 
     /** PCI region \#0: NAM I/O ports. */
@@ -552,4 +559,9 @@
 } AC97STATE;
 AssertCompileMemberAlignment(AC97STATE, aStreams, 8);
+#ifdef VBOX_WITH_STATISTICS
+AssertCompileMemberAlignment(AC97STATE, StatTimer,        8);
+AssertCompileMemberAlignment(AC97STATE, StatBytesRead,    8);
+AssertCompileMemberAlignment(AC97STATE, StatBytesWritten, 8);
+#endif
 
 
@@ -634,10 +646,4 @@
     PDMDevHlpTimerUnlockClock2((a_pDevIns), (a_pStream)->hTimer, &(a_pThis)->CritSect)
 
-#ifdef VBOX_WITH_STATISTICS
-AssertCompileMemberAlignment(AC97STATE, StatTimer,        8);
-AssertCompileMemberAlignment(AC97STATE, StatBytesRead,    8);
-AssertCompileMemberAlignment(AC97STATE, StatBytesWritten, 8);
-#endif
-
 #ifndef VBOX_DEVICE_STRUCT_TESTCASE
 
@@ -1783,5 +1789,5 @@
  *
  * @returns IPRT status code.
- * @param   pThis               The shared AC'97 state.
+ * @param   pThisCC             The ring-3 AC'97 state.
  * @param   pMixSink            Mixer sink to add stream to.
  * @param   pCfg                Stream configuration to use.
@@ -1817,5 +1823,5 @@
  *
  * @return IPRT status code.
- * @param  pThis                The ring-3 AC'97 device state.
+ * @param  pThisCC              The ring-3 AC'97 device state.
  * @param  pDrv                 The AC'97 driver to add.
  */
@@ -1848,5 +1854,5 @@
  * associated streams.
  *
- * @param pThis                 The ring-3 AC'97 device state.
+ * @param pThisCC               The ring-3 AC'97 device state.
  * @param pDrv                  AC'97 driver to remove.
  */
@@ -1909,5 +1915,5 @@
  * Removes all driver streams from a specific mixer sink.
  *
- * @param   pThis               The ring-3 AC'97 state.
+ * @param   pThisCC             The ring-3 AC'97 state.
  * @param   pMixSink            Mixer sink to remove audio streams from.
  * @param   enmDir              Stream direction to remove.
@@ -2181,5 +2187,5 @@
  *
  * @returns Free data (in bytes).
- * @param   pStream             AC'97 stream to retrieve size for.
+ * @param   pStreamCC           AC'97 stream to retrieve size for (ring-3).
  */
 static uint32_t ichac97R3StreamGetFree(PAC97STREAMR3 pStreamCC)
@@ -2216,5 +2222,5 @@
      * master volume controls.
      */
-    if ((index == AC97_Master_Volume_Mute) || (index == AC97_Headphone_Volume_Mute) || (index == AC97_Master_Volume_Mono_Mute))
+    if (index == AC97_Master_Volume_Mute || index == AC97_Headphone_Volume_Mute || index == AC97_Master_Volume_Mono_Mute)
     {
         if (uVal & RT_BIT(5))  /* D5 bit set? */
@@ -2517,5 +2523,5 @@
     ichac97MixerSet(pThis, AC97_MIC_ADC_Rate            , 0xbb80 /* 48000 Hz by default */);
 
-    if (pThis->uCodecModel == AC97_CODEC_AD1980)
+    if (pThis->enmCodecModel == AC97CODEC_AD1980)
     {
         /* Analog Devices 1980 (AD1980) */
@@ -2525,5 +2531,5 @@
         ichac97MixerSet(pThis, AC97_Headphone_Volume_Mute   , 0x8000);
     }
-    else if (pThis->uCodecModel == AC97_CODEC_AD1981B)
+    else if (pThis->enmCodecModel == AC97CODEC_AD1981B)
     {
         /* Analog Devices 1981B (AD1981B) */
@@ -3320,5 +3326,5 @@
                     break;
                 case AC97_Master_Volume_Mute:
-                    if (pThis->uCodecModel == AC97_CODEC_AD1980)
+                    if (pThis->enmCodecModel == AC97CODEC_AD1980)
                     {
                         if (ichac97MixerGet(pThis, AC97_AD_Misc) & AC97_AD_MISC_LOSEL)
@@ -3332,5 +3338,5 @@
                     break;
                 case AC97_Headphone_Volume_Mute:
-                    if (pThis->uCodecModel == AC97_CODEC_AD1980)
+                    if (pThis->enmCodecModel == AC97CODEC_AD1980)
                     {
                         if (ichac97MixerGet(pThis, AC97_AD_Misc) & AC97_AD_MISC_HPSEL)
@@ -3500,10 +3506,9 @@
  * Saves (serializes) an AC'97 stream using SSM.
  *
- * @returns IPRT status code.
  * @param   pDevIns             Device instance.
  * @param   pSSM                Saved state manager (SSM) handle to use.
  * @param   pStream             AC'97 stream to save.
  */
-static int ichac97R3SaveStream(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, PAC97STREAM pStream)
+static void ichac97R3SaveStream(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, PAC97STREAM pStream)
 {
     PAC97BMREGS   pRegs = &pStream->Regs;
@@ -3520,6 +3525,4 @@
     pHlp->pfnSSMPutU32(pSSM, pRegs->bd.addr);
     pHlp->pfnSSMPutU32(pSSM, pRegs->bd.ctl_len);
-
-    return VINF_SUCCESS;
 }
 
@@ -3532,5 +3535,4 @@
     PAC97STATER3    pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PAC97STATER3);
     PCPDMDEVHLPR3   pHlp    = pDevIns->pHlpR3;
-
     LogFlowFuncEnter();
 
@@ -3539,21 +3541,20 @@
     pHlp->pfnSSMPutU32(pSSM, pThis->cas);
 
+    /*
+     * The order that the streams are saved here is fixed, so don't change.
+     */
     /** @todo r=andy For the next saved state version, add unique stream identifiers and a stream count. */
-    /* Note: The order the streams are loaded here is critical, so don't touch. */
     for (unsigned i = 0; i < AC97_MAX_STREAMS; i++)
-    {
-        int rc2 = ichac97R3SaveStream(pDevIns, pSSM, &pThis->aStreams[i]);
-        AssertRC(rc2);
-    }
+        ichac97R3SaveStream(pDevIns, pSSM, &pThis->aStreams[i]);
 
     pHlp->pfnSSMPutMem(pSSM, pThis->mixer_data, sizeof(pThis->mixer_data));
 
-    uint8_t active[AC97SOUNDSOURCE_MAX];
-
-    active[AC97SOUNDSOURCE_PI_INDEX] = ichac97R3StreamIsEnabled(pThisCC, &pThis->aStreams[AC97SOUNDSOURCE_PI_INDEX]) ? 1 : 0;
-    active[AC97SOUNDSOURCE_PO_INDEX] = ichac97R3StreamIsEnabled(pThisCC, &pThis->aStreams[AC97SOUNDSOURCE_PO_INDEX]) ? 1 : 0;
-    active[AC97SOUNDSOURCE_MC_INDEX] = ichac97R3StreamIsEnabled(pThisCC, &pThis->aStreams[AC97SOUNDSOURCE_MC_INDEX]) ? 1 : 0;
-
-    pHlp->pfnSSMPutMem(pSSM, active, sizeof(active));
+    /* The stream order is against fixed and set in stone. */
+    uint8_t afActiveStrms[AC97SOUNDSOURCE_MAX];
+    afActiveStrms[AC97SOUNDSOURCE_PI_INDEX] = ichac97R3StreamIsEnabled(pThisCC, &pThis->aStreams[AC97SOUNDSOURCE_PI_INDEX]);
+    afActiveStrms[AC97SOUNDSOURCE_PO_INDEX] = ichac97R3StreamIsEnabled(pThisCC, &pThis->aStreams[AC97SOUNDSOURCE_PO_INDEX]);
+    afActiveStrms[AC97SOUNDSOURCE_MC_INDEX] = ichac97R3StreamIsEnabled(pThisCC, &pThis->aStreams[AC97SOUNDSOURCE_MC_INDEX]);
+    AssertCompile(RT_ELEMENTS(afActiveStrms) == 3);
+    pHlp->pfnSSMPutMem(pSSM, afActiveStrms, sizeof(afActiveStrms));
 
     LogFlowFuncLeaveRC(VINF_SUCCESS);
@@ -3597,5 +3598,5 @@
     LogRel2(("ichac97LoadExec: uVersion=%RU32, uPass=0x%x\n", uVersion, uPass));
 
-    AssertMsgReturn (uVersion == AC97_SSM_VERSION, ("%RU32\n", uVersion), VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION);
+    AssertMsgReturn (uVersion == AC97_SAVED_STATE_VERSION, ("%RU32\n", uVersion), VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION);
     Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
 
@@ -3604,6 +3605,8 @@
     pHlp->pfnSSMGetU32(pSSM, &pThis->cas);
 
-    /** @todo r=andy For the next saved state version, add unique stream identifiers and a stream count. */
-    /* Note: The order the streams are loaded here is critical, so don't touch. */
+    /*
+     * The order the streams are loaded here is critical (defined by
+     * AC97SOUNDSOURCE_XX_INDEX), so don't touch!
+     */
     for (unsigned i = 0; i < AC97_MAX_STREAMS; i++)
     {
@@ -3613,9 +3616,4 @@
 
     pHlp->pfnSSMGetMem(pSSM, pThis->mixer_data, sizeof(pThis->mixer_data));
-
-    /** @todo r=andy Stream IDs are hardcoded to certain streams. */
-    uint8_t uaStrmsActive[AC97SOUNDSOURCE_MAX];
-    int rc2 = pHlp->pfnSSMGetMem(pSSM, uaStrmsActive, sizeof(uaStrmsActive));
-    AssertRCReturn(rc2, rc2);
 
     ichac97R3MixerRecordSelect(pThis, ichac97MixerGet(pThis, AC97_Record_Select));
@@ -3632,13 +3630,19 @@
     ichac97R3MixerSetGain(pThis, pThisCC, AC97_Record_Gain_Mute,        PDMAUDIOMIXERCTL_LINE_IN,
                           ichac97MixerGet(pThis, AC97_Record_Gain_Mute));
-    if (pThis->uCodecModel == AC97_CODEC_AD1980)
+    if (pThis->enmCodecModel == AC97CODEC_AD1980)
         if (ichac97MixerGet(pThis, AC97_AD_Misc) & AC97_AD_MISC_HPSEL)
             ichac97R3MixerSetVolume(pThis, pThisCC, AC97_Headphone_Volume_Mute, PDMAUDIOMIXERCTL_VOLUME_MASTER,
                                     ichac97MixerGet(pThis, AC97_Headphone_Volume_Mute));
 
-    /** @todo r=andy Stream IDs are hardcoded to certain streams. */
+    /*
+     * Again the stream order is set is stone.
+     */
+    uint8_t afActiveStrms[AC97SOUNDSOURCE_MAX];
+    int rc2 = pHlp->pfnSSMGetMem(pSSM, afActiveStrms, sizeof(afActiveStrms));
+    AssertRCReturn(rc2, rc2);
+
     for (unsigned i = 0; i < AC97_MAX_STREAMS; i++)
     {
-        const bool          fEnable   = RT_BOOL(uaStrmsActive[i]);
+        const bool          fEnable   = RT_BOOL(afActiveStrms[i]);
         const PAC97STREAM   pStream   = &pThis->aStreams[i];
         const PAC97STREAMR3 pStreamCC = &pThisCC->aStreams[i];
@@ -3938,5 +3942,5 @@
  * @returns VBox status code.
  * @param   pDevIns     The device instance.
- * @param   pThis       The ring-3 AC'97 device state.
+ * @param   pThisCC     The ring-3 AC'97 device state.
  * @param   iLun        The logical unit which is being replaced.
  */
@@ -4031,9 +4035,9 @@
      */
     if (!strcmp(szCodec, "STAC9700"))
-        pThis->uCodecModel = AC97_CODEC_STAC9700;
+        pThis->enmCodecModel = AC97CODEC_STAC9700;
     else if (!strcmp(szCodec, "AD1980"))
-        pThis->uCodecModel = AC97_CODEC_AD1980;
+        pThis->enmCodecModel = AC97CODEC_AD1980;
     else if (!strcmp(szCodec, "AD1981B"))
-        pThis->uCodecModel = AC97_CODEC_AD1981B;
+        pThis->enmCodecModel = AC97CODEC_AD1981B;
     else
         return PDMDevHlpVMSetError(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES, RT_SRC_POS,
@@ -4074,10 +4078,10 @@
     PCIDevSetInterruptPin(pPciDev,          0x01);   /* 3d ro - INTA#. */               Assert(pPciDev->abConfig[0x3d] == 0x01);
 
-    if (pThis->uCodecModel == AC97_CODEC_AD1980)
+    if (pThis->enmCodecModel == AC97CODEC_AD1980)
     {
         PCIDevSetSubSystemVendorId(pPciDev, 0x1028); /* 2c ro - Dell.) */
         PCIDevSetSubSystemId(pPciDev,       0x0177); /* 2e ro. */
     }
-    else if (pThis->uCodecModel == AC97_CODEC_AD1981B)
+    else if (pThis->enmCodecModel == AC97CODEC_AD1981B)
     {
         PCIDevSetSubSystemVendorId(pPciDev, 0x1028); /* 2c ro - Dell.) */
@@ -4110,5 +4114,5 @@
      * Saved state.
      */
-    rc = PDMDevHlpSSMRegister(pDevIns, AC97_SSM_VERSION, sizeof(*pThis), ichac97R3SaveExec, ichac97R3LoadExec);
+    rc = PDMDevHlpSSMRegister(pDevIns, AC97_SAVED_STATE_VERSION, sizeof(*pThis), ichac97R3SaveExec, ichac97R3LoadExec);
     if (RT_FAILURE(rc))
         return rc;
