'#Uses "..\classes\FunctionClass.cls"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Pages.inc"
'#Uses "..\include\Port.inc"
'#Uses "Include.inc"            

Option Explicit

'*****************************************************************************
' Module:
'        EBC
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions which are create controls for the
'        module 'EBC'.
'        Furthermore it contains functions for the event handling of
'        these controls.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        1.0   ahe         27.04.00     final version  for C165UTAH
'
'        ----------------------------------------------------------------------
'        2.0   ahe         14.06.00     final version for C167CS
'
'        ----------------------------------------------------------------------
'        2.1   ahe         02.08.00     final version for C167CS and C161CS/JC/JI
'                                       - check if XPERCON exist
'                                       - added tooltip for XPEN_0
'              ahe         10.08.00     - changes actions for 
'                                         optBTYP_0
'                                         optBTYP_1
'                                         optBTYP_2
'                                         optBTYP_3
'             
'              ahe         23.10.00     - clerar BUSCON0-Page and addded to
'                                         BUSCONx
'                                       - added BSWCx and EWENx
'
'        ----------------------------------------------------------------------
'        7.0   ahe         27.03.01     final version for Pegasus
'
'        7.1   ahe         02.07.02     add function DisableEBC
'
'*****************************************************************************
Dim CS As Peripherals


'*****************************************************************************
'Description:
'        This function is called from DAvE just before the Peripherals
'        window is shown.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnLoad(ps As Peripherals) As Boolean
    On Error GoTo OnLoadError

    OnLoad = False

    '-------------------------------------------------------------------------
    ' page: Control
    '-------------------------------------------------------------------------
    If (Not OnLoadPageControl(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: OnLoadPageConfigureControl
    '-------------------------------------------------------------------------
    If (Not OnLoadPageConfigureControl(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: Notes
    '-------------------------------------------------------------------------
    If (Not OnLoadPageNotes(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' end of pages
    '-------------------------------------------------------------------------
    
    '-------------------------------------------------------------------------
    ' call function to initialize the menu
    '-------------------------------------------------------------------------
    If (Not InitMenu(ps, "EBC\Dialog")) Then Exit Function

    OnLoad = True
    Exit Function

OnLoadError:
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This function is called from DAvE just before a page
'        is shown.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Page            no. of the page (starts with 1)
'        Pagename        name of the page
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnChangingPage(ps As Peripherals, Page As Integer, Pagename As String) As Boolean
    On Error Goto OnChangingPageError

    OnChangingPage = False

    
    '-------------------------------------------------------------------------
    'page: Control
    '-------------------------------------------------------------------------
    If (Not OnChangingPageControl (ps, Pagename)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: OnChangingPageConfigureControl
    '-------------------------------------------------------------------------
    If (Not OnChangingPageConfigureControl (ps, Pagename)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: Notes
    '-------------------------------------------------------------------------
    If (Not OnChangingPageNotes (ps, Pagename, "EBC")) Then Exit Function


    OnChangingPage = True
    Exit Function

OnChangingPageError:
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This function is called from DAvE in two cases:
'
'        1.
'            The contents of the peripherals window must be refreshed.
'            In this case the 'Controlname' parameter is a empty string
'            and the values array 'v()' contains nothing.
'
'        2.
'            A control on the peripherals has been changed.
'            In this case the 'Controlname' parameter contains the name
'            of the changed control and the values array 'v()' contains the
'            changed value(s).
'
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     name of the changed control (for case 2)
'        v()             array with the changed values (for case 2)
'        Page            no. of the page (starts with 1)
'        Pagename        name of the page
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnShow(ps As Peripherals, Controlname As String, v() As Variant, Page As Integer, Pagename As String) As Boolean
    On Error GoTo OnShowError

    OnShow = False

    '-------------------------------------------------------------------------
    ' page: Control
    '-------------------------------------------------------------------------
    If (Not OnShowPageControl(ps, Controlname, Pagename, v)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: OnShowPageConfigureControl
    '-------------------------------------------------------------------------
    If (Not OnShowPageConfigureControl(ps, Controlname, Pagename, v)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: Notes
    '-------------------------------------------------------------------------
    If (Not OnShowPageNotes(ps, Controlname, Pagename, "EBC", v)) Then Exit Function


    OnShow = True
    Exit Function

OnShowError:
    Errorbox "Can not show page.", Err.Description
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This function is called from DAvE if the user clicks on a button.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     name of the button
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnClick(ps As Peripherals, Controlname As String) As Boolean
    On Error GoTo OnClickError

    OnClick = False

    Dim np As Peripherals

    '-------------------------------------------------------------------------
    ' buttons: OnButtonClickOnPageConfigureControl
    '-------------------------------------------------------------------------
    If (Not OnButtonClickOnPageConfigureControl(ps, Controlname)) Then Exit Function



    OnClick = True
    Exit Function

OnClickError:
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This function is called from DAvE if the user clicks on a menu entry.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Toolname        name of the entry
'        ToolId          id of the entry
'        Tooltext        additional text of the entry
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnMenuClick(ps As Peripherals, Toolname As String, ToolId As Long, Tooltext As String) As Boolean
    On Error GoTo OnMenuClickError

    OnMenuClick = False

    If (Not MenuClick(ps, Toolname, ToolId, Tooltext, "EBC\Dialog")) Then Exit Function

    OnMenuClick = True
    Exit Function

OnMenuClickError:
    Exit Function
End Function
    

'-----------------------------------------------------------------------------
' local functions
'-----------------------------------------------------------------------------


'*****************************************************************************
'Description:
'        This function is called from the OnLoad function of the associated
'        module in order to add the page 'Control'.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageControl(ps As Object) As Boolean
  On Error Resume Next

  OnLoadPageControl = False

  If (Not ps.AddPage("Control")) Then Exit Function

  OnLoadPageControl = True
End Function



'*****************************************************************************
'Description:
'        This function is called from the OnChangingPage function of the
'        associated module in order load all controls for the requested page
'        if they are not already loaded.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'        Pagename         requested page name
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnChangingPageControl(ps As Object, Pagename As String) As Boolean
    On Error Resume Next

    OnChangingPageControl = False
  
    If (Not ps.ExtenderCollection("Control").Loaded) And (Pagename = "Control") Then
      ps.ExtenderCollection("Control").Loaded = True
  

        '-------------------------------
        ' frame: frmABCDIS
        '-------------------------------
        If (Not ps.AddFrame("frmABCDIS",100,100, 4150, 800, _
          "EBC Control")) Then Exit Function
            If (Not ps.AddCheck("chkEBCDIS", 200, 250, 3775, 375, _
              "Disable EBC (EBCDIS)")) Then Exit Function

        '-------------------------------
        ' frame: frmBYTDIS
        '-------------------------------
        If (Not ps.AddFrame("frmBYTDIS",4400,100, 4150, 800, _
          "#BHE Pin Control")) Then Exit Function
            If (Not ps.AddCheck("chkBYTDIS", 200, 250, 3625, 380, _
              "Disable pin #BHE (BYTDIS)")) Then Exit Function

        '-------------------------------
        ' frame: frmALEDIS
        '-------------------------------
        If (Not ps.AddFrame("frmALEDIS",100,950, 4150, 1000, _
          "ALE Pin Control")) Then Exit Function
            If (Not ps.AddCheck("chkALEDIS", 200, 350, 3850, 375, _
              "Disable pin ALE (ALEDIS)")) Then Exit Function

        '-------------------------------
        ' frame: frmWRCFG
        '-------------------------------
        If (Not ps.AddFrame("frmWRCFG",4400,950, 4150, 1000, _
          "Write Configuration (WRCFG)")) Then Exit Function
            If (Not ps.AddOption("optWRCFG_0", 200, 200, 3850, 375, _
              "#WR and #BHE retain their normal function")) Then Exit Function
            If (Not ps.AddOption("optWRCFG_1", 200, 550, 3850, 375, _
              "#WR acts as #WRL, #BHE acts as #WRH")) Then Exit Function

        '-------------------------------
        ' frame: frmSAPEN
        '-------------------------------
        If (Not ps.AddFrame("frmSAPEN",100,2000, 4150, 1000, _
          "Segment Address Pins Enable Control (SAPEN)")) Then Exit Function
            If (Not ps.AddCombo("cboSAPEN", 200, 400, 3750, 315)) Then Exit Function

        '-------------------------------
        ' frame: frmCSPEN
        '-------------------------------
        If (Not ps.AddFrame("frmCSPEN",4400,2000, 4150, 1000, _
          "#CS0 to #CS4 Pins Enable Control (CSPEN)")) Then Exit Function
            If (Not ps.AddCombo("cboCSPEN", 200, 400, 3750, 315)) Then Exit Function

        '-------------------------------
        ' frame: frmRDYDIS
        '-------------------------------
        If (Not ps.AddFrame("frmRDYDIS",100,3050, 4150, 800, _
          "READY Pin Control")) Then Exit Function
            If (Not ps.AddCheck("chkRDYDIS", 200, 250, 3850, 380, _
              "Disable pin #READY (RDYDIS)")) Then Exit Function

        '-------------------------------
        ' frame: frmRDYPOL
        '-------------------------------
        If (Not ps.AddFrame("frmRDYPOL",4400,3050, 4150, 1100, _
          "#READY Pin Polarity (RDYPOL)")) Then Exit Function
            If (Not ps.AddOption("optRDYPOL_0", 200, 250, 3850, 375, _
              "#READY is active low")) Then Exit Function
            If (Not ps.AddOption("optRDYPOL_1", 200, 650, 3850, 380, _
              "#READY is active high")) Then Exit Function

        '-------------------------------
        ' frame: frmARBEN
        '-------------------------------
        If (Not ps.AddFrame("frmARBEN",100,3900, 4150, 1400, _
          "Bus Arbitration Pins Control (ARBEN)")) Then Exit Function
            If (Not ps.AddOption("optARBEN_0", 200, 300, 3850, 380, _
              "Bus arbitration disabled, P6.7 .. P6.5 may used for general purpose IO")) Then Exit Function
            If (Not ps.AddOption("optARBEN_1", 200, 800, 3850, 375, _
              "Bus arbitration enabled, P6.7 .. P6.5 serve as #BREQ, #HLDA and #HOLD")) Then Exit Function

        '-------------------------------
        ' frame: frmSLAVE
        '-------------------------------
        If (Not ps.AddFrame("frmSLAVE",4400,4200, 4150, 1100, _
          "SLAVE Mode Control (SLAVE)")) Then Exit Function
            If (Not ps.AddOption("optSLAVE_0", 200, 250, 3850, 375, _
              "Bus arbiter acts in master mode")) Then Exit Function
            If (Not ps.AddOption("optSLAVE_1", 200, 650, 3850, 380, _
              "Bus arbiter acts in slave mode")) Then Exit Function

    End If
    OnChangingPageControl = True
End Function



'*****************************************************************************
'Description:
'        This function is called from the OnShow function of the associated
'        module. See the description for the OnShow function of the
'        associated module for details.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     control name
'        Pagename        current page name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnShowPageControl(ps As Object, Controlname As String, Pagename As String, v() As Variant) As Boolean
    On Error GoTo OnShowPageControlError

    OnShowPageControl = False

    Dim r         As ProjectData
    Dim c         As Object
    Dim e         As Object
    Dim sReg      As String
    Dim i         As Integer
    Dim bDisabled As Boolean
    Dim iMaxCS    As Integer

    sReg = "EBCMOD0_"

    Set r = ps.ProjectData
    Set c = ps.ControlCollection
    Set e = ps.ExtenderCollection

    If (Pagename = "Control") Then
        '-------------------------------------------------------------------------
        ' page: Control (action)
        '-------------------------------------------------------------------------
        Select Case (Controlname)
            '---------------------------------------------------------------------
            ' control: chkEBCDIS (action)
            '---------------------------------------------------------------------
            Case "chkEBCDIS"
                r.v(sReg & "EBCDIS") = v(1)

                If (v(1) = 1) Then
                   Call DisableEBC(r)
                Else
                End If

            '---------------------------------------------------------------------
            ' control: chkALEDIS (action)
            '---------------------------------------------------------------------
            Case "chkALEDIS"
                r.v(sReg & "ALEDIS") = v(1)
                If (v(1)) Then
                    Call ReleaseAlternateOutputPin(r, "ALE_O")
                Else
                    Call RequestAlternateOutputPin(r, "ALE_O")
                End If

            '---------------------------------------------------------------------
            ' control: chkBYTDIS (action)
            '---------------------------------------------------------------------
            Case "chkBYTDIS"
                r.v(sReg & "BYTDIS") = v(1)
                
                If (v(1) = 0) Then
                
                  Call RequestAlternateOutputPin(r, "BHE_n")
                
                Else
                
                  r.v(sReg & "WRCFG") = 0
                  If (GetPinFunction(r, "WRH_n") = 2) Then
                    Call ReleaseAlternateOutputPin(r, "WRH_n")
                  End If
                  If (GetPinFunction(r, "BHE_n") = 2) Then
                    Call ReleaseAlternateOutputPin(r, "BHE_n")
                  End If
                
                End If

            '---------------------------------------------------------------------
            ' control: optWRCFG_0 (action)
            '---------------------------------------------------------------------
            Case "optWRCFG_0"
                r.v(sReg & "WRCFG") = 0
                Call ReleaseAlternateOutputPin(r, "WRH_n")
                Call RequestAlternateOutputPin(r, "BHE_n")

            '---------------------------------------------------------------------
            ' control: optWRCFG_1 (action)
            '---------------------------------------------------------------------
            Case "optWRCFG_1"
                r.v(sReg & "WRCFG") = 1
                Call ReleaseAlternateOutputPin(r, "BHE_n")
                Call RequestAlternateOutputPin(r, "WRH_n")

            '---------------------------------------------------------------------
            ' control: cboSAPEN (action)
            '---------------------------------------------------------------------
            Case "cboSAPEN"
                If (v(1) = "No segment address pins enabled") Then
                    r.v(sReg & "SAPEN") = 0
                ElseIf (v(1) = "1 pin enabled ( A[16] )") Then
                    r.v(sReg & "SAPEN") = 1
                End If
                For i = 2 To 8
                    If (v(1) = i & " pins enabled ( A[" & 15 + i & "] .. A[16] )") Then
                        r.v(sReg & "SAPEN") = i
                    End If
                Next i

            '---------------------------------------------------------------------
            ' control: cboCSPEN (action)
            '---------------------------------------------------------------------
            Case "cboCSPEN"
                If (v(1) = "No chip select pin enabled") Then
                    r.v(sReg & "CSPEN") = 0
                ElseIf (v(1) = "Enable #CS0") Then
                    r.v(sReg & "CSPEN") = 1
                End If
                For i = 1 To 6
                    If (v(1) = "Enable #CS" & i & " .. #CS0") Then
                        r.v(sReg & "CSPEN") = i + 1
                    End If
                Next i

            '---------------------------------------------------------------------
            ' control: chkRDYDIS (action)
            '---------------------------------------------------------------------
            Case "chkRDYDIS"
                r.v(sReg & "RDYDIS") = v(1)
                If (v(1)) Then
                    Call ReleaseAlternateInputPin(r, "READY_I")
                Else
                    Call RequestAlternateInputPin(r, "READY_I")
                End If

            '---------------------------------------------------------------------
            ' control: optRDYPOL_0 (action)
            '---------------------------------------------------------------------
            Case "optRDYPOL_0"
                r.v(sReg & "RDYPOL") = 0

            '---------------------------------------------------------------------
            ' control: optRDYPOL_1 (action)
            '---------------------------------------------------------------------
            Case "optRDYPOL_1"
                r.v(sReg & "RDYPOL") = 1

            '---------------------------------------------------------------------
            ' control: optARBEN_0 (action)
            '---------------------------------------------------------------------
            Case "optARBEN_0"
                r.v(sReg & "ARBEN") = 0
                r.v(sReg & "SLAVE") = 0
                Call ReleaseAlternateInputPin(r, "HOLD_n")
                Call ReleaseAlternateOutputPin(r, "BREQ_n")
             
                If (GetPinFunction(r, "HLDA_n_I") = 1) Then
                  Call ReleaseAlternateInputPin(r, "HLDA_n_I")
                End If
                If (GetPinFunction(r, "HLDA_n_O") = 2) Then 
                  Call ReleaseAlternateOutputPin(r, "HLDA_n_O")
                End If

            '---------------------------------------------------------------------
            ' control: optARBEN_1 (action)
            '---------------------------------------------------------------------
            Case "optARBEN_1"
                r.v(sReg & "ARBEN") = 1
                Call RequestAlternateInputPin(r, "HOLD_n")
                Call RequestAlternateOutputPin(r, "HLDA_n_O")
                Call RequestAlternateOutputPin(r, "BREQ_n")

            '---------------------------------------------------------------------
            ' control: optSLAVE_0 (action)
            '---------------------------------------------------------------------
            Case "optSLAVE_0"
                r.v(sReg & "SLAVE") = 0
                Call ReleaseAlternateInputPin(r, "HLDA_n_I")
                Call RequestAlternateOutputPin(r, "HLDA_n_O") 

            '---------------------------------------------------------------------
            ' control: optSLAVE_1 (action)
            '---------------------------------------------------------------------
            Case "optSLAVE_1"
                r.v(sReg & "SLAVE") = 1
                Call ReleaseAlternateOutputPin(r, "HLDA_n_O")
                Call RequestAlternateInputPin(r, "HLDA_n_I")

          Case Else
        End Select

        '-------------------------------------------------------------------------
        ' page: Control (show)
        '-------------------------------------------------------------------------

        '-------------------------------------------------------------------------
        ' control: chkEBCDIS (show)
        '-------------------------------------------------------------------------
        With c("chkEBCDIS")
            bDisabled = (r.v("EA_EA") = 1)
            .Enabled = Not(bDisabled)
            .Value = -CInt(r.v(sReg & "EBCDIS") = 1)
        End With

        With e("chkEBCDIS")
          If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If (r.v("EA_EA") = 1) Then
              .DisabledTooltipText = .DisabledTooltipText & _
                FormatDisabledTooltip("fetch code from external ROM (pin #EA = low)")
            End If
          End If
        End With

        '-------------------------------------------------------------------------
        ' control: chkALEDIS (show)
        '-------------------------------------------------------------------------
        With c("chkALEDIS")
            bDisabled = (IsAlternateOutputPinFree(r, "ALE_O") = False Or _
                         IsEBCDisabled(r))
            .Enabled = Not(bDisabled)
            .Value = -CInt(r.v(sReg & "ALEDIS") = 1)
        End With

        With e("chkALEDIS")
          If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If (IsAlternateOutputPinFree(r, "ALE_O") = False) Then
              .DisabledTooltipText = .DisabledTooltipText & _
                GetDisabledAlternatePinTooltip(r,"ALE_O")
            End If
            If (IsEBCDisabled(r)) Then
              .DisabledTooltipText = .DisabledTooltipText & _
                GetTooltipEBCIsDisabled(r)
            End If
          End If
        End With

        '-------------------------------------------------------------------------
        ' control: chkBYTDIS (show)
        '-------------------------------------------------------------------------
        With c("chkBYTDIS")
            bDisabled = ((IsAlternateOutputPinFree(r, "BHE_n") = False And _
                         IsAlternateOutputPinFree(r, "WRH_n") = False) Or _
                         IsEBCDisabled(r))
            .Enabled = Not(bDisabled)
            .Value = -CInt(r.v(sReg & "BYTDIS") = 1)
        End With

        With e("chkBYTDIS")
          If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If (IsEBCDisabled(r)) Then
              .DisabledTooltipText = .DisabledTooltipText & _
                GetTooltipEBCIsDisabled(r)
            End If
            If (IsAlternateOutputPinFree(r, "BHE_n") = False And _
                         IsAlternateOutputPinFree(r, "WRH_n") = False) Then
              .DisabledTooltipText = .DisabledTooltipText & GetDisabledAlternatePinTooltip(r,"BHE_n")
            End If
          End If
        End With

        '-------------------------------------------------------------------------
        ' control: optWRCFG_0 (show)
        '-------------------------------------------------------------------------
        With c("optWRCFG_0")
            bDisabled = (r.v(sReg & "BYTDIS") = 1 Or IsEBCDisabled(r))
            .Enabled = Not(bDisabled)
            .Value = -CInt(r.v(sReg & "WRCFG") = 0)
        End With

        With e("optWRCFG_0")
          If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If (IsEBCDisabled(r)) Then
              .DisabledTooltipText = .DisabledTooltipText & _
                GetTooltipEBCIsDisabled(r)
            End If
            If (r.v(sReg & "BYTDIS") = 1) Then
              .DisabledTooltipText = .DisabledTooltipText & _
                FormatDisabledTooltip("the signal #BHE is disabled (BYTDIS = 1)")
            End If
          End If
        End With

        '-------------------------------------------------------------------------
        ' control: optWRCFG_1 (show)
        '-------------------------------------------------------------------------
        With c("optWRCFG_1")
            'bDisabled = (r.v(sReg & "BYTDIS") = 1)
            .Enabled = Not(bDisabled)
            .Value = -CInt(r.v(sReg & "WRCFG") = 1)
        End With

        With e("optWRCFG_1")
          If (bDisabled) Then
            .DisabledTooltipText = e("optWRCFG_0").DisabledTooltipText
          End If
        End With

        '-------------------------------------------------------------------------
        ' control: cboSAPEN (show)
        '-------------------------------------------------------------------------
        With c("cboSAPEN")
            bDisabled = True
            .Enabled = Not(bDisabled)
            .Clear
            .AddItem "No segment address pins enabled"
            .AddItem "1 pin enabled ( A[16] )"
            For i = 2 To 8
                .AddItem i & " pins enabled ( A[" & 15 + i & "] .. A[16] )"
            Next i
            .ListIndex = r.v(sReg & "SAPEN")
        End With

        With e("cboSAPEN")
            If (bDisabled) Then
              .DisabledTooltipText = ""
              .DisabledTooltipText = .DisabledTooltipText & _
                FormatNormalTooltip("The control of this setting is dependent of the startup configuration.")
            End If
        End With

        '-------------------------------------------------------------------------
        ' control: cboCSPEN (show)
        '-------------------------------------------------------------------------
        With c("cboCSPEN")
            bDisabled = True
            .Enabled = Not(bDisabled)
            .Clear
            .AddItem "No chip select pin enabled"
            .AddItem "Enable #CS0"
            For i = 1 To 6
                .AddItem "Enable #CS" & i & " .. #CS0"
            Next i
            .ListIndex = r.v(sReg & "CSPEN")
        End With

        With e("cboCSPEN")
            If (bDisabled) Then
              .DisabledTooltipText = ""
              .DisabledTooltipText = .DisabledTooltipText & _
                FormatNormalTooltip("The control of this setting is dependent of the startup configuration.")
            End If
        End With

        '-------------------------------------------------------------------------
        ' control: chkRDYDIS (show)
        '-------------------------------------------------------------------------
        With c("chkRDYDIS")
            If (r.Exist("MuxFor_READY_I") = False) Then
                bDisabled = True
            Else
                bDisabled = (IsAlternateInputPinFree(r, "READY_I") = False Or _
                             IsEBCDisabled(r))
            End If
            .Enabled = Not(bDisabled)
            .Value = -CInt(r.v(sReg & "RDYDIS") = 1)
        End With

        With e("chkRDYDIS")
          If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If (r.Exist("MuxFor_READY_I") = False) Then
              .DisabledTooltipText = .DisabledTooltipText & _
                FormatDisabledTooltip("the pin #READY does not exist")
            Else
                If (IsEBCDisabled(r)) Then
                  .DisabledTooltipText = .DisabledTooltipText & _
                    GetTooltipEBCIsDisabled(r)
                End If
                If (IsAlternateInputPinFree(r, "READY_I") = False) Then
                  .DisabledTooltipText = .DisabledTooltipText & _
                    GetDisabledAlternatePinTooltip(r,"READY_I")
                End If
            End If
          End If
        End With

        '-------------------------------------------------------------------------
        ' control: optRDYPOL_0 (show)
        '-------------------------------------------------------------------------
        With c("optRDYPOL_0")
            If (r.Exist("MuxFor_READY_I") = False) Then
                bDisabled = True
            Else
                bDisabled = (IsAlternateInputPinFree(r, "READY_I") = False Or _
                             r.v(sReg & "RDYDIS") = 1 Or IsEBCDisabled(r))
            End If
            .Enabled = Not(bDisabled)
            .Value = -CInt(r.v(sReg & "RDYPOL") = 0)
        End With

        With e("optRDYPOL_0")
          If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If (r.Exist("MuxFor_READY_I") = False) Then
              .DisabledTooltipText = .DisabledTooltipText & _
                FormatDisabledTooltip("the pin #READY does not exist")
            Else
                If (IsEBCDisabled(r)) Then
                  .DisabledTooltipText = .DisabledTooltipText & _
                    GetTooltipEBCIsDisabled(r)
                End If
                If (IsAlternateInputPinFree(r, "READY_I") = False) Then
                  .DisabledTooltipText = .DisabledTooltipText & _
                    GetDisabledAlternatePinTooltip(r,"READY_I")
                End If
                If (r.v(sReg & "RDYDIS") = 1) Then
                  .DisabledTooltipText = .DisabledTooltipText & _
                    FormatDisabledTooltip("pin #READY is disabled (RDYDIS = 1)")
                End If
            End If
          End If
        End With

        '-------------------------------------------------------------------------
        ' control: optRDYPOL_1 (show)
        '-------------------------------------------------------------------------
        With c("optRDYPOL_1")
            'bDisabled = s.a.
            .Enabled = Not(bDisabled)
            .Value = -CInt(r.v(sReg & "RDYPOL") = 1)
        End With

        With e("optRDYPOL_1")
          If (bDisabled) Then
            .DisabledTooltipText = e("optRDYPOL_0").DisabledTooltipText
          End If
        End With

        '-------------------------------------------------------------------------
        ' control: optARBEN_0 (show)
        '-------------------------------------------------------------------------
        With c("optARBEN_0")
            If (r.Exist("MuxFor_HOLD_n") = False) Then
                bDisabled = True
            Else
                bDisabled = (IsEBCDisabled(r) Or _
                             IsAlternateInputPinFree(r, "HOLD_n") = False Or _
                             IsAlternateOutputPinFree(r, "BREQ_n") = False Or _
                             (IsAlternateOutputPinFree(r, "HLDA_n_O") = False And _
                              IsAlternateInputPinFree(r, "HLDA_n_I") = False))
            End If
            .Enabled = Not(bDisabled)
             .Value = -CInt(r.v(sReg & "ARBEN") = 0)
        End With

        With e("optARBEN_0")
          If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If (r.Exist("MuxFor_HOLD_n") = False) Then
              .DisabledTooltipText = .DisabledTooltipText & _
                FormatDisabledTooltip("the pins #BREQ, #HOLD and #HLDA are not exist")
            Else
                If (IsEBCDisabled(r)) Then
                  .DisabledTooltipText = .DisabledTooltipText & _
                    GetTooltipEBCIsDisabled(r)
                End If
                If (IsAlternateInputPinFree(r, "HOLD_n") = False) Then
                  .DisabledTooltipText = .DisabledTooltipText & _
                    GetDisabledAlternatePinTooltip(r,"HOLD_n")
                End If
                If (IsAlternateInputPinFree(r, "HLDA_n_I") = False) Then
                  .DisabledTooltipText = .DisabledTooltipText & _
                    GetDisabledAlternatePinTooltip(r, "HLDA_n_I")
                End If
                If (IsAlternateOutputPinFree(r, "HLDA_n_O") = False) Then
                  .DisabledTooltipText = .DisabledTooltipText & _
                    GetDisabledAlternatePinTooltip(r, "HLDA_n_O")
                End If
                If (IsAlternateOutputPinFree(r, "BREQ_n") = False) Then
                  .DisabledTooltipText = .DisabledTooltipText & _
                    GetDisabledAlternatePinTooltip(r, "BREQ_n")
                End If
            End If
          End If
        End With

        '-------------------------------------------------------------------------
        ' control: optARBEN_1 (show)
        '-------------------------------------------------------------------------
        With c("optARBEN_1")
            'bDisabled = s.a.
            .Enabled = Not(bDisabled)
             .Value = -CInt(r.v(sReg & "ARBEN") = 1)
        End With

        With e("optARBEN_1")
          If (bDisabled) Then
            .DisabledTooltipText = e("optARBEN_0").DisabledTooltipText
          End If
        End With

        '-------------------------------------------------------------------------
        ' control: optSLAVE_0 (show)
        '-------------------------------------------------------------------------
        With c("optSLAVE_0")
            If (r.Exist("MuxFor_HOLD_n") = False) Then
                bDisabled = True
            Else
                bDisabled = (r.v(sReg & "ARBEN") = 0 Or IsEBCDisabled(r))
            End If
            .Enabled = Not(bDisabled)
             .Value = -CInt(r.v(sReg & "SLAVE") = 0)
        End With

        With e("optSLAVE_0")
            If (bDisabled) Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                If (r.Exist("MuxFor_HOLD_n") = False) Then
                    .DisabledTooltipText = .DisabledTooltipText & _
                    FormatDisabledTooltip("the pins #BREQ, #HOLD and #HLDA are not exist")
                Else
                    If (IsEBCDisabled(r)) Then
                      .DisabledTooltipText = .DisabledTooltipText & _
                        GetTooltipEBCIsDisabled(r)
                    End If
                    If (r.v(sReg & "ARBEN") = 0) Then
                      .DisabledTooltipText = .DisabledTooltipText & _
                        FormatDisabledTooltip("the bus arbitration pins are not enabled")
                    End If
                End If
            End If
        End With

        '-------------------------------------------------------------------------
        ' control: optSLAVE_1 (show)
        '-------------------------------------------------------------------------
        With c("optSLAVE_1")
            'bDisabled = s.a.
            .Enabled = Not(bDisabled)
             .Value = -CInt(r.v(sReg & "SLAVE") = 1)
        End With

        With e("optSLAVE_1")
          If (bDisabled) Then
            .DisabledTooltipText = e("optSLAVE_0").DisabledTooltipText
          End If
        End With

    End If

    OnShowPageControl = True
    Exit Function

OnShowPageControlError:
    Errorbox "Can not show page", Err.Description
    Exit Function
End Function

'*****************************************************************************
'Description:
'        This function is called from the OnLoad function of the associated
'        module in order to add the page 'ConfigureControl'.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageConfigureControl(ps As Object) As Boolean
  On Error Resume Next

  OnLoadPageConfigureControl = False

  If (Not ps.AddPage("Configure Control")) Then Exit Function

  OnLoadPageConfigureControl = True
End Function



'*****************************************************************************
'Description:
'        This function is called from the OnChangingPage function of the
'        associated module in order load all controls for the requested page
'        if they are not already loaded.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'        Pagename         requested page name
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnChangingPageConfigureControl(ps As Object, Pagename As String) As Boolean
  On Error Resume Next

  OnChangingPageConfigureControl = False
  Dim r As ProjectData

  Set r = ps.ProjectData
  
  If (Not ps.ExtenderCollection("Configure Control").Loaded) And (Pagename = "Configure Control") Then
    ps.ExtenderCollection("Configure Control").Loaded = True
  

    '-------------------------------
    ' frame: frmConf
    '-------------------------------
    If (Not ps.AddFrame("frmConf",100,100, 2400, 3700, "")) Then Exit Function
        If (Not ps.AddButton("btnCS0", 200, 300, 1800, 400, "Configure #CS0")) Then Exit Function
        If (Not ps.AddButton("btnCS1", 200, 1000, 1800, 400, "Configure #CS1")) Then Exit Function
        If (Not ps.AddButton("btnCS2", 200, 1700, 1800, 400, "Configure #CS2")) Then Exit Function
        If (Not ps.AddButton("btnCS3", 200, 2400, 1800, 400, "Configure #CS3")) Then Exit Function
        
        If (r.Exist("MuxFor_CS4_n") = True) Then
            If (Not ps.AddButton("btnCS4", 200, 3100, 1800, 400, "Configure #CS4")) Then Exit Function
        Else
            'no #CS4
        End If
        
  End If
  OnChangingPageConfigureControl = True
End Function



'*****************************************************************************
'Description:
'        This function is called from the OnShow function of the associated
'        module. See the description for the OnShow function of the
'        associated module for details.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     control name
'        Pagename        current page name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnShowPageConfigureControl(ps As Object, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowPageConfigureControlError

  OnShowPageConfigureControl = False

  Dim r As ProjectData
  Dim c As Object
  Dim e As Object

  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection

  If (Pagename = "Configure Control") Then
    '-------------------------------------------------------------------------
    ' page: ConfigureControl (action)
    '-------------------------------------------------------------------------
    Select Case (Controlname)

      Case Else
    End Select

    '-------------------------------------------------------------------------
    ' page: ConfigureControl (show)
    '-------------------------------------------------------------------------

  End If

  OnShowPageConfigureControl = True
  Exit Function

OnShowPageConfigureControlError:
  Errorbox "Can not show page", Err.Description
  Exit Function
End Function



'*****************************************************************************
'Description:
'        This function is called from the OnClick function of the associated
'        module if a button on the page 'ConfigureControl' will be clicked.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     control name of the button
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnButtonClickOnPageConfigureControl(ps As Object, Controlname As String) As Boolean
    On Error GoTo OnButtonClickOnPageConfigureControlError

    OnButtonClickOnPageConfigureControl = False

    Dim i As Integer
    Dim vIndex as Variant
    Dim iMaxCS AS Integer
    Dim r As ProjectData

    Set r = ps.ProjectData

    vIndex = ps.Parameters.v("x")


    iMaxCS = 4
    
    For i = 0 To iMaxCS
        If (Controlname = "btnCS" & i) Then
            Set CS = New Peripherals
            CS.Parameters.Add ("x")
            CS.Parameters.v("x") = vIndex
            CS.Parameters.Add ("CS")
            CS.Parameters.v("CS") = i
            Set CS.ProjectData = ps.ProjectData
            CS.Caption = "Chip Select Channel " & i
            CS.Scriptfile = DefGetPath("Scripts") & "EBC\CHIP_SELx.DAVE"
            CS.Show
        End If
    Next i



    OnButtonClickOnPageConfigureControl = True
    Exit Function

OnButtonClickOnPageConfigureControlError:
    Errorbox "Can not show page", Err.Description
    Exit Function
End Function


