'#Uses "..\classes\FunctionClass.cls"
'#Uses "..\classes\ParameterClass.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:
'                 
'        ----------------------------------------------------------------------
'        7.0   ahe         27.03.01     final version for Pegasus
'
'        7.1   ahe         02.07.02     no changes
'
'*****************************************************************************


'*****************************************************************************
'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
    Dim iCS As Integer

    iCS = ps.Parameters.v("CS")

    OnLoad = False

    '-------------------------------------------------------------------------
    ' page: OnLoadPageCSxControl
    '-------------------------------------------------------------------------
    If (Not OnLoadPageCSxControl(ps, iCS)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: OnLoadPageCSxTiming
    '-------------------------------------------------------------------------
    If (Not OnLoadPageCSxTiming(ps, iCS)) Then Exit Function

    '-------------------------------------------------------------------------
    ' end of pages
    '-------------------------------------------------------------------------
    
    '-------------------------------------------------------------------------
    ' call function to initialize the menu
    '-------------------------------------------------------------------------
    If (Not InitMenu(ps, "EBC\CHIP_SELx")) 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
    
    Dim sVarPre As String
    Dim vIndex as Variant
    Dim iCS As Integer

    iCS = ps.Parameters.v("CS")
    vIndex = ps.Parameters.v("x")
    sVarPre = "EBC" & vIndex

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

    '-------------------------------------------------------------------------
    'page: OnChangingPageCSxTiming
    '-------------------------------------------------------------------------
    If (Not OnChangingPageCSxTiming (ps, Pagename, iCS)) 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
    
    Dim sVarPre As String
    Dim vIndex as Variant
    Dim iCS As Integer

    iCS = ps.Parameters.v("CS")
    vIndex = ps.Parameters.v("x")
    sVarPre = "EBC" & vIndex


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

    '-------------------------------------------------------------------------
    ' page: OnShowPageCSxTiming
    '-------------------------------------------------------------------------
    If (Not OnShowPageCSxTiming(ps, Controlname, Pagename, v, iCS)) 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

    OnClick = True
    Exit Function

OnClickError:
    Errorbox "Can not Text in OnClick.", Err.Description
    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\CHIP_SELx")) 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 'CSxControl'.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageCSxControl(ps As Object, iCS As Integer) As Boolean
  On Error Resume Next

  OnLoadPageCSxControl = False

  If (Not ps.AddPage("#CS" & iCS & " Control")) Then Exit Function

  OnLoadPageCSxControl = 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 OnChangingPageCSxControl(ps As Object, Pagename As String, iCS As Integer) As Boolean
  On Error Resume Next

  OnChangingPageCSxControl = False
  
  If (Not ps.ExtenderCollection("#CS" & iCS & " Control").Loaded) And (Pagename = "#CS" & iCS & " Control") Then
    ps.ExtenderCollection("#CS" & iCS & " Control").Loaded = True
  

    '-------------------------------
    ' frame: frmENCSx
    '-------------------------------
    If (Not ps.AddFrame("frmENCSx",100,100, 4150, 1000, _
      "Chip Select " & iCS & " Control")) Then Exit Function
        If (Not ps.AddCheck("chkENCSx", 200, 350, 3800, 375, _
          "Enable chip select " & iCS & " (ENCS" & iCS & ")")) Then Exit Function

    '-------------------------------
    ' frame: frmBTYPx
    '-------------------------------
    If (Not ps.AddFrame("frmBTYPx",100,1150, 4150, 1800, _
      "Bus Type Selection (BTYP" & iCS & ")")) Then Exit Function
        If (Not ps.AddOption("optBTYPx_0", 200, 250, 3800, 380, _
          "8-bit Demultiplexed bus")) Then Exit Function
        If (Not ps.AddOption("optBTYPx_1", 200, 600, 3800, 380, _
          "8-bit Multiplexed bus")) Then Exit Function
        If (Not ps.AddOption("optBTYPx_2", 200, 950, 3800, 380, _
          "16-bit Demultiplexed bus")) Then Exit Function
        If (Not ps.AddOption("optBTYPx_3", 200, 1300, 3800, 380, _
          "16-bit Multiplexed bus")) Then Exit Function

    '-------------------------------
    ' frame: frmRDYENx
    '-------------------------------
    If (Not ps.AddFrame("frmRDYENx",100,3000, 4150, 1200, _
      "Ready Control (RDYEN" & iCS & ")")) Then Exit Function
        If (Not ps.AddOption("optRDYEN_0", 200, 250, 3700, 380, _
          "Access time is controlled by bitfield PHE" & iCS)) Then Exit Function
        If (Not ps.AddOption("optRDYEN_1", 200, 700, 3700, 400, _
          "Access time is controlled by bitfield PHE" & iCS & " and #READY signal")) Then Exit Function

    '-------------------------------
    ' frame: frmRDYMODx
    '-------------------------------
    If (Not ps.AddFrame("frmRDYMODx",100,4250, 4150, 1000, _
      "Ready Mode Control (RDYMOD" & iCS & ")")) Then Exit Function
        If (Not ps.AddOption("optRDYMODx_0", 200, 200, 3800, 375, _
          "Asynchronous #READY")) Then Exit Function
        If (Not ps.AddOption("optRDYMODx_1", 200, 550, 3800, 400, _
          "Synchronous #READY")) Then Exit Function

    If (iCS > 0) Then
        '-------------------------------
        ' frame: frmADDRSELx
        '-------------------------------
        If (Not ps.AddFrame("frmADDRSELx",4400,100, 4150, 2850, _
          "Address Area (ADDRSEL" & iCS & ")")) Then Exit Function
            If (Not ps.AddLabel("lblWindow", 0, 200, 500, 1575, 320, _
              "Window size")) Then Exit Function
            If (Not ps.AddCombo("cboRGSZ", 2000, 450, 1815, 315)) Then Exit Function
            If (Not ps.AddLabel("lblReq", 0, 200, 1200, 1575, 395, _
              "Required start address (A23 .. A12)")) Then Exit Function
            If (Not ps.AddText("txtReq", 2000, 1200, 1000, 315)) Then Exit Function
            If (Not ps.AddLabel("lblRel", 0, 175, 2025, 1575, 395, _
              "Relevant start address (A23 .. A0)")) Then Exit Function
            If (Not ps.AddLabel("txtRel", 1, 2000, 2050, 1000, 325, _
              "0x000000")) Then Exit Function
    End If

  End If
  OnChangingPageCSxControl = 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 OnShowPageCSxControl(ps As Object, Controlname As String, Pagename As String, v() As Variant, iCS As Integer) As Boolean
  On Error GoTo OnShowPageCSxControlError

  OnShowPageCSxControl = False

  Dim r As ProjectData
  Dim c As Object
  Dim e As Object
  Dim sFCON As String
  Dim sADDReg As String
  Dim sDisabledTooltip As String
  Dim bDisabled As Boolean
  Dim lCopyRGSAD As Long
  Dim i As Integer

  Dim bPinInterface_A0_To_A7_IsNotFree As Boolean
  Dim bPinInterface_A8_To_A15_IsNotFree As Boolean
  Dim bPinInterface_AD0_To_AD7_IsNotFree As Boolean
  Dim bPinInterface_AD8_To_AD15_IsNotFree As Boolean

  Dim sTooltipFor_A0_To_A7 As String
  Dim sTooltipFor_A8_To_A15 As String
  Dim sTooltipFor_AD0_To_AD7 As String
  Dim sTooltipFor_AD8_To_AD15 As String

  sFCON = "FCONCS" & iCS & "_"
  sADDReg = "ADDRSEL" & iCS & "_"

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

  If (Pagename = "#CS" & iCS & " Control") Then
    '-------------------------------------------------------------------------
    ' page: CSxControl (action)
    '-------------------------------------------------------------------------
    Select Case (Controlname)
        '---------------------------------------------------------------------
        ' control: chkENCSx (action)
        '---------------------------------------------------------------------
        Case "chkENCSx"

             If (v(1) = 0) Then
               Call DeselectCSx(r, iCS)
             Else
               r.v(sFCON & "ENCS" & iCS) = 1
               Call SelectPinInterfaceP0L(r)
               
               If (IsPinInterfaceFree(r, "A", 8, 15) And IsPinInterfaceFree(r, "A", 0, 7)) Then
                 Call SelectPinInterfaceP1L(r)
                 Call SelectPinInterfaceP1H(r)
                 r.v(sFCON & "BTYP" & iCS) = 0
               Else
                 Call SelectPinInterfaceP0H(r)
                 r.v(sFCON & "BTYP" & iCS) = 1
               End If
             
             End If


        '---------------------------------------------------------------------
        ' control: optBTYPx_0 (action)
        '---------------------------------------------------------------------
        Case "optBTYPx_0"
            Call Select_Pin_Interface_For_EBC(r, iCS, 0)
            r.v(sFCON & "BTYP" & iCS) = 0

        '---------------------------------------------------------------------
        ' control: optBTYPx_1 (action)
        '---------------------------------------------------------------------
        Case "optBTYPx_1"
            Call Select_Pin_Interface_For_EBC(r, iCS, 1)
            r.v(sFCON & "BTYP" & iCS) = 1

        '---------------------------------------------------------------------
        ' control: optBTYPx_2 (action)
        '---------------------------------------------------------------------
        Case "optBTYPx_2"
            Call Select_Pin_Interface_For_EBC(r, iCS, 2)
            r.v(sFCON & "BTYP" & iCS) = 2

        '---------------------------------------------------------------------
        ' control: optBTYPx_3 (action)
        '---------------------------------------------------------------------
        Case "optBTYPx_3"
            Call Select_Pin_Interface_For_EBC(r, iCS, 3)
            r.v(sFCON & "BTYP" & iCS) = 3

        '---------------------------------------------------------------------
        ' control: optRDYEN_0 (action)
        '---------------------------------------------------------------------
        Case "optRDYEN_0"
            r.v(sFCON & "RDYEN" & iCS) = 0
            r.v(sFCON & "RDYMOD" & iCS) = 0

        '---------------------------------------------------------------------
        ' control: optRDYEN_1 (action)
        '---------------------------------------------------------------------
        Case "optRDYEN_1"
            r.v(sFCON & "RDYEN" & iCS) = 1

        '---------------------------------------------------------------------
        ' control: optRDYMODx_0 (action)
        '---------------------------------------------------------------------
        Case "optRDYMODx_0"
            r.v(sFCON & "RDYMOD" & iCS) = 0

        '---------------------------------------------------------------------
        ' control: optRDYMODx_1 (action)
        '---------------------------------------------------------------------
        Case "optRDYMODx_1"
            r.v(sFCON & "RDYMOD" & iCS) = 1

        '---------------------------------------------------------------------
        ' control: cboRGSZ (action)
        '---------------------------------------------------------------------
        Case "cboRGSZ"
                 For i = 0 To 7
                   If (v(1) = (2^(2 + i) & " kByte")) Then
                     r.v(sADDReg & "RGSZ" & iCS) = i
                   End If
                 Next i
                 If (v(1) = "1 MByte") Then
                   r.v(sADDReg & "RGSZ" & iCS) = 8
                 End If
                 If (v(1) = "2 MByte") Then
                   r.v(sADDReg & "RGSZ" & iCS) = 9
                 End If
                 If (v(1) = "4 MByte") Then
                   r.v(sADDReg & "RGSZ" & iCS) = 10
                 End If
                 If (v(1) = "8 MByte") Then
                   r.v(sADDReg & "RGSZ" & iCS) = 11
                 End If


        '---------------------------------------------------------------------
        ' control: txtReq (action)
        '---------------------------------------------------------------------
        Case "txtReq"
                 If (IsInputFormatOK(v(1), "H4", c("lblReq").Caption, 0, 4095)) Then
                   r.v(sADDReg & "RGSAD" & iCS) = v(1)
                 Else
                   Exit Function
                 End If



      Case Else
    End Select

    '-------------------------------------------------------------------------
    ' page: CSxControl (show)
    '-------------------------------------------------------------------------
    bPinInterface_AD0_To_AD7_IsNotFree = Not(IsPinInterfaceFree(r, "AD", 0, 7))
    bPinInterface_AD8_To_AD15_IsNotFree = Not(IsPinInterfaceFree(r, "AD", 8, 15))
    bPinInterface_A0_To_A7_IsNotFree = Not(IsPinInterfaceFree(r, "A", 0, 7))
    bPinInterface_A8_To_A15_IsNotFree = Not(IsPinInterfaceFree(r, "A", 8, 15))

    sTooltipFor_A0_To_A7 = GetTooltipPinInterfaceDisabled(r, "A", 0, 7)
    sTooltipFor_A8_To_A15 = GetTooltipPinInterfaceDisabled(r, "A", 8, 15)
    sTooltipFor_AD0_To_AD7 = GetTooltipPinInterfaceDisabled(r, "AD", 0, 7)
    sTooltipFor_AD8_To_AD15 = GetTooltipPinInterfaceDisabled(r, "AD", 8, 15)

    '************************************************************************************************
    'Disabled for next frames
    '************************************************************************************************
    If (iCS > 0) Then
        bDisabled = (IsEBCDisabled(r) Or _
                     r.v(sFCON & "ENCS" & iCS) = 0 Or _
                     bPinInterface_AD0_To_AD7_IsNotFree Or _
                     ( bPinInterface_AD8_To_AD15_IsNotFree And _
                      ( bPinInterface_A0_To_A7_IsNotFree Or _
                        bPinInterface_A8_To_A15_IsNotFree _
                      ) _
                     ) _
                    )
    
        sDisabledTooltip = ""
        sDisabledTooltip = TOOLTIP_DISABLED_IF
        If (IsEBCDisabled(r)) Then
          sDisabledTooltip = sDisabledTooltip & _
            GetTooltipEBCIsDisabled(r)
        End If
        If (bPinInterface_AD0_To_AD7_IsNotFree) Then
          sDisabledTooltip = sDisabledTooltip & sTooltipFor_AD0_To_AD7
        End If
        If (bPinInterface_AD8_To_AD15_IsNotFree And bPinInterface_A0_To_A7_IsNotFree) Then
          sDisabledTooltip = sDisabledTooltip & sTooltipFor_AD8_To_AD15
          sDisabledTooltip = sDisabledTooltip & sTooltipFor_A0_To_A7
        End If
        If (bPinInterface_AD8_To_AD15_IsNotFree And bPinInterface_A8_To_A15_IsNotFree) Then
          sDisabledTooltip = sDisabledTooltip & sTooltipFor_AD8_To_AD15
          sDisabledTooltip = sDisabledTooltip & sTooltipFor_A8_To_A15
        End If
        If (r.v(sFCON & "ENCS" & iCS) = 0) Then
          sDisabledTooltip = sDisabledTooltip & _
            FormatDisabledTooltip("the chip select channel " & iCS & " is disabled (ENCS" & iCS & " = 0)")
        End If

        SetDefaultPropertiesForAllControlsOnFrame(c("frmADDRSELx").Caption, c, e, bDisabled, sDisabledTooltip, "")
    End IF


    '-------------------------------------------------------------------------
    ' control: chkENCSx (show)
    '-------------------------------------------------------------------------
    With c("chkENCSx")
      bDisabled = (IsEBCDisabled(r) Or _
                   bPinInterface_AD0_To_AD7_IsNotFree Or _
                   ( bPinInterface_AD8_To_AD15_IsNotFree And _
                    ( bPinInterface_A0_To_A7_IsNotFree Or bPinInterface_A8_To_A15_IsNotFree _
                    ) _
                   ) _
                  Or (r.v("EA_EA") = 1 And (iCS = 0)) )
        .Enabled = Not(bDisabled)
        .Value = -CInt(r.v(sFCON & "ENCS" & iCS) = 1)
    End With

    With e("chkENCSx")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (IsEBCDisabled(r)) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            GetTooltipEBCIsDisabled(r)
        End If
        If (bPinInterface_AD0_To_AD7_IsNotFree) Then
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_AD0_To_AD7
        End If
        If (bPinInterface_AD8_To_AD15_IsNotFree And bPinInterface_A0_To_A7_IsNotFree) Then
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_AD8_To_AD15
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_A0_To_A7
        End If
        If (bPinInterface_AD8_To_AD15_IsNotFree And bPinInterface_A8_To_A15_IsNotFree) Then
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_AD8_To_AD15
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_A8_To_A15
        End If
        If (r.v("EA_EA") = 1 And (iCS = 0)) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("fetch code from external ROM (pin #EA = low)")
        End If
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: optBTYPx_0 (show)
    '-------------------------------------------------------------------------
    With c("optBTYPx_0")
        bDisabled = (IsEBCDisabled(r) Or _
                     r.v(sFCON & "ENCS" & iCS) = 0 Or _
                     bPinInterface_A0_To_A7_IsNotFree Or _
                     bPinInterface_A8_To_A15_IsNotFree Or _
                     (r.v("EA_EA") = 1 And (iCS = 0)) )
        .Enabled = Not(bDisabled)
        .Value = -CInt(r.v(sFCON & "BTYP" & iCS) = 0)
    End With

    With e("optBTYPx_0")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (IsEBCDisabled(r)) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            GetTooltipEBCIsDisabled(r)
        End If
        If (r.v(sFCON & "ENCS" & iCS) = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            FormatDisabledTooltip("the chip select channel " & iCS & _
            " is disabled (ENCS" & iCS & " = 0)")
        End If
        If (bPinInterface_A0_To_A7_IsNotFree) Then
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_A0_To_A7
        End If
        If (bPinInterface_A8_To_A15_IsNotFree) Then
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_A8_To_A15
        End If
        If (r.v("EA_EA") = 1 And (iCS = 0)) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            FormatDisabledTooltip("fetch code from external ROM (pin #EA = low)")
        End If
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: optBTYPx_1 (show)
    '-------------------------------------------------------------------------
    With c("optBTYPx_1")
        bDisabled = (IsEBCDisabled(r) Or _
                     r.v(sFCON & "ENCS" & iCS) = 0 Or _
                     bPinInterface_AD8_To_AD15_IsNotFree _
                     Or (r.v("EA_EA") = 1 And (iCS = 0)) )
        .Enabled = Not(bDisabled)
        .Value = -CInt(r.v(sFCON & "BTYP" & iCS) = 1)
    End With

    With e("optBTYPx_1")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (IsEBCDisabled(r)) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            GetTooltipEBCIsDisabled(r)
        End If
        If (r.v(sFCON & "ENCS" & iCS) = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            FormatDisabledTooltip("the chip select channel " & iCS & _
            " is disabled (ENCS" & iCS & " = 0)")
        End If
        If (bPinInterface_AD8_To_AD15_IsNotFree) Then
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_A8_To_A15
        End If
        If (r.v("EA_EA") = 1 And (iCS = 0)) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            FormatDisabledTooltip("fetch code from external ROM (pin #EA = low)")
        End If
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: optBTYPx_2 (show)
    '-------------------------------------------------------------------------
    With c("optBTYPx_2")
      bDisabled = (IsEBCDisabled(r) Or _
                    r.v(sFCON & "ENCS" & iCS) = 0 Or _
                    bPinInterface_AD8_To_AD15_IsNotFree Or _
                    bPinInterface_A0_To_A7_IsNotFree Or _
                    bPinInterface_A8_To_A15_IsNotFree _
                   Or (r.v("EA_EA") = 1 And (iCS = 0)) )
      .Enabled = Not(bDisabled)
        .Value = -CInt(r.v(sFCON & "BTYP" & iCS) = 2)
    End With

    With e("optBTYPx_2")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (IsEBCDisabled(r)) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            GetTooltipEBCIsDisabled(r)
        End If
        If (r.v(sFCON & "ENCS" & iCS) = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            FormatDisabledTooltip("the chip select channel " & iCS & _
            " is disabled (ENCS" & iCS & " = 0)")
        End If
        If (bPinInterface_A0_To_A7_IsNotFree) Then
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_A0_To_A7
        End If
        If (bPinInterface_A8_To_A15_IsNotFree) Then
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_A8_To_A15
        End If
        If (bPinInterface_AD8_To_AD15_IsNotFree) Then
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_AD8_To_AD15
        End If
        If (r.v("EA_EA") = 1 And (iCS = 0)) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            FormatDisabledTooltip("fetch code from external ROM (pin #EA = low)")
        End If
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: optBTYPx_3 (show)
    '-------------------------------------------------------------------------
    With c("optBTYPx_3")
      bDisabled = (IsEBCDisabled(r) Or _
                   r.v(sFCON & "ENCS" & iCS) = 0 Or _
                   bPinInterface_AD8_To_AD15_IsNotFree _
                   Or (r.v("EA_EA") = 1 And (iCS = 0)) )
      .Enabled = Not(bDisabled)
        .Value = -CInt(r.v(sFCON & "BTYP" & iCS) = 3)
    End With

    With e("optBTYPx_3")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (IsEBCDisabled(r)) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            GetTooltipEBCIsDisabled(r)
        End If
        If (r.v(sFCON & "ENCS" & iCS) = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            FormatDisabledTooltip("the chip select channel " & iCS & _
            " is disabled (ENCS" & iCS & " = 0)")
        End If
        If (bPinInterface_AD8_To_AD15_IsNotFree) Then
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_AD8_To_AD15
        End If
        If (r.v("EA_EA") = 1 And (iCS = 0)) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            FormatDisabledTooltip("fetch code from external ROM (pin #EA = low)")
        End If
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: optRDYEN_0 (show)
    '-------------------------------------------------------------------------
    With c("optRDYEN_0")
        bDisabled = (IsEBCDisabled(r) Or _
                     r.v(sFCON & "ENCS" & iCS) = 0 Or _
                     bPinInterface_AD0_To_AD7_IsNotFree Or _
                     ( bPinInterface_AD8_To_AD15_IsNotFree And _
                      ( bPinInterface_A0_To_A7_IsNotFree Or _
                        bPinInterface_A8_To_A15_IsNotFree _
                      ) _
                     ) _
                    )
        .Enabled = Not(bDisabled)
        .Value = -CInt(r.v(sFCON & "RDYEN" & iCS) = 0)
    End With

    With e("optRDYEN_0")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (IsEBCDisabled(r)) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            GetTooltipEBCIsDisabled(r)
        End If
        If (bPinInterface_AD0_To_AD7_IsNotFree) Then
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_AD0_To_AD7
        End If
        If (bPinInterface_AD8_To_AD15_IsNotFree And bPinInterface_A0_To_A7_IsNotFree) Then
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_AD8_To_AD15
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_A0_To_A7
        End If
        If (bPinInterface_AD8_To_AD15_IsNotFree And bPinInterface_A8_To_A15_IsNotFree) Then
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_AD8_To_AD15
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_A8_To_A15
        End If
        If (r.v(sFCON & "ENCS" & iCS) = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            FormatDisabledTooltip("the chip select channel " & iCS & " is disabled (ENCS" & iCS & " = 0)")
        End If
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: optRDYEN_1 (show)
    '-------------------------------------------------------------------------
    With c("optRDYEN_1")
        'bDisabled = s.a.
        .Enabled = Not(bDisabled)
        .Value = -CInt(r.v(sFCON & "RDYEN" & iCS) = 1)
    End With

    With e("optRDYEN_1")
      If (bDisabled) Then
        .DisabledTooltipText = e("optRDYEN_0").DisabledTooltipText
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: optRDYMODx_0 (show)
    '-------------------------------------------------------------------------
    With c("optRDYMODx_0")
        bDisabled = (IsEBCDisabled(r) Or _
                     r.v(sFCON & "ENCS" & iCS) = 0 Or _
                     r.v(sFCON & "RDYEN" & iCS) = 0 Or _
                     bPinInterface_AD0_To_AD7_IsNotFree Or _
                     ( bPinInterface_AD8_To_AD15_IsNotFree And _
                      ( bPinInterface_A0_To_A7_IsNotFree Or _
                        bPinInterface_A8_To_A15_IsNotFree _
                      ) _
                     ) _
                    )
        .Enabled = Not(bDisabled)
        .Value = -CInt(r.v(sFCON & "RDYMOD" & iCS) = 0)
    End With

    With e("optRDYMODx_0")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (IsEBCDisabled(r)) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            GetTooltipEBCIsDisabled(r)
        End If
        If (bPinInterface_AD0_To_AD7_IsNotFree) Then
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_AD0_To_AD7
        End If
        If (bPinInterface_AD8_To_AD15_IsNotFree And bPinInterface_A0_To_A7_IsNotFree) Then
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_AD8_To_AD15
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_A0_To_A7
        End If
        If (bPinInterface_AD8_To_AD15_IsNotFree And bPinInterface_A8_To_A15_IsNotFree) Then
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_AD8_To_AD15
          .DisabledTooltipText = .DisabledTooltipText & sTooltipFor_A8_To_A15
        End If
        If (r.v(sFCON & "ENCS" & iCS) = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            FormatDisabledTooltip("the chip select channel " & iCS & " is disabled (ENCS" & iCS & " = 0)")
        End If
        If (r.v(sFCON & "RDYEN" & iCS) = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & _
            FormatDisabledTooltip("access time is controlled only by bitfield PHE" & iCS)
        End If
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: optRDYMODx_1 (show)
    '-------------------------------------------------------------------------
    With c("optRDYMODx_1")
        'bDisabled = s.a.
        .Enabled = Not(bDisabled)
        .Value = -CInt(r.v(sFCON & "RDYMOD" & iCS) = 1)
    End With

    With e("optRDYMODx_1")
      If (bDisabled) Then
        .DisabledTooltipText = e("optRDYMODx_0").DisabledTooltipText
      End If
    End With

    If (iCS > 0) Then
        '-------------------------------------------------------------------------
        ' control: lblWindow (show)
        '-------------------------------------------------------------------------
        With c("lblWindow")
          ' INSERT THE STATEMENTS WHICH HAVE TO BE EXECUTED
          ' TO REFRESH THE CONTENTS OF THE CONTROL: lblWindow
        End With

        '-------------------------------------------------------------------------
        ' control: cboRGSZ (show)
        '-------------------------------------------------------------------------
        With c("cboRGSZ")
            .Clear
            For i = 0 To 7
              .AddItem 2^(2 + i) & " kByte"
            Next i
            .AddItem "1 MByte"
            .AddItem "2 MByte"
            .AddItem "4 MByte"
            .AddItem "8 MByte"
            .ListIndex = r.v(sADDReg & "RGSZ" & iCS)
        End With

        '-------------------------------------------------------------------------
        ' control: lblReqAdd (show)
        '-------------------------------------------------------------------------
        With c("lblReq")
          ' INSERT THE STATEMENTS WHICH HAVE TO BE EXECUTED
          ' TO REFRESH THE CONTENTS OF THE CONTROL: lblReqAdd
        End With

        '-------------------------------------------------------------------------
        ' control: txtReq (show)
        '-------------------------------------------------------------------------
        With c("txtReq")
            .Text = FormatTextOutput(r.v(sADDReg & "RGSAD" & iCS), "H3")
        End With

        '-------------------------------------------------------------------------
        ' control: lblRel (show)
        '-------------------------------------------------------------------------
        With c("lblRel")
          ' INSERT THE STATEMENTS WHICH HAVE TO BE EXECUTED
          ' TO REFRESH THE CONTENTS OF THE CONTROL: lblRel
        End With

        '-------------------------------------------------------------------------
        ' control: txtRel (show)
        '-------------------------------------------------------------------------
        With c("txtRel")
            lCopyRGSAD = r.v(sADDReg & "RGSAD" & iCS)
            If (r.v(sADDReg & "RGSZ" & iCS) > 0) Then
              lCopyRGSAD = 0
              lCopyRGSAD = (r.v(sADDReg & "RGSAD" & iCS) \  _
                            2^(r.v(sADDReg & "RGSZ" & iCS))) * _
                            2^(r.v(sADDReg & "RGSZ" & iCS))
            End If
            .Caption = FormatTextOutput(lCopyRGSAD, "H3") & "000"
        End With
    End If


  End If


  OnShowPageCSxControl = True
  Exit Function

OnShowPageCSxControlError:
  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 'CSxTiming'.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageCSxTiming(ps As Object, iCS As Integer) As Boolean
  On Error Resume Next

  OnLoadPageCSxTiming = False

  If (Not ps.AddPage("#CS" & iCS & " Timing")) Then Exit Function

  OnLoadPageCSxTiming = 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 OnChangingPageCSxTiming(ps As Object, Pagename As String, iCS As Integer) As Boolean
  On Error Resume Next

  OnChangingPageCSxTiming = False
  
  If (Not ps.ExtenderCollection("#CS" & iCS & " Timing").Loaded) And (Pagename = "#CS" & iCS & " Timing") Then
    ps.ExtenderCollection("#CS" & iCS & " Timing").Loaded = True
  

    '-------------------------------
    ' frame: frmPHAx
    '-------------------------------
    If (Not ps.AddFrame("frmPHAx",100,100, 2700, 1000, _
      "Phase A (PHA" & iCS & ")")) Then Exit Function
        If (Not ps.AddCombo("cboPHAx", 200, 400, 2300, 315)) Then Exit Function

    '-------------------------------
    ' frame: frmPHBx
    '-------------------------------
    If (Not ps.AddFrame("frmPHBx",3000,100, 2700, 1000, _
      "Phase B (PHB" & iCS & ")")) Then Exit Function
        If (Not ps.AddCombo("cboPHBx", 200, 400, 2300, 315)) Then Exit Function

    '-------------------------------
    ' frame: frmPHCx
    '-------------------------------
    If (Not ps.AddFrame("frmPHCx",5900,100, 2700, 1000, _
      "Phase C (PHC" & iCS & ")")) Then Exit Function
        If (Not ps.AddCombo("cboPHCx", 200, 400, 2300, 315)) Then Exit Function

    '-------------------------------
    ' frame: frmPHDx
    '-------------------------------
    If (Not ps.AddFrame("frmPHDx",100,1150, 2700, 1000, _
      "Phase D (PHD" & iCS & ")")) Then Exit Function
        If (Not ps.AddCombo("cboPHDx", 200, 400, 2300, 315)) Then Exit Function

    '-------------------------------
    ' frame: frmPHEx
    '-------------------------------
    If (Not ps.AddFrame("frmPHEx",3000,1150, 2700, 1000, _
      "Phase E (PHE" & iCS & ")")) Then Exit Function
        If (Not ps.AddCombo("cboPHEx", 200, 400, 2300, 315)) Then Exit Function

    '-------------------------------
    ' frame: frmRDPHFx
    '-------------------------------
    If (Not ps.AddFrame("frmRDPHFx",100,2200, 2700, 1000, _
      "Read Phase F (RDPHF" & iCS & ")")) Then Exit Function
        If (Not ps.AddCombo("cboRDPHFx", 200, 400, 2300, 315)) Then Exit Function

    '-------------------------------
    ' frame: frmWRPHF
    '-------------------------------
    If (Not ps.AddFrame("frmWRPHF",3000,2200, 2700, 1000, _
      "Write Phase F (WRPHF" & iCS & ")")) Then Exit Function
        If (Not ps.AddCombo("cboWRPHFx", 200, 400, 2300, 315)) Then Exit Function

  End If
  OnChangingPageCSxTiming = 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 OnShowPageCSxTiming(ps As Object, Controlname As String, Pagename As String, v() As Variant, iCS As Integer) As Boolean
  On Error GoTo OnShowPageCSxTimingError

  OnShowPageCSxTiming = False

  Dim r                 As ProjectData
  Dim c                 As Object
  Dim e                 As Object
  Dim sFCON             As String
  Dim sTCON             As String
  Dim i                 As Integer
  Dim bDisabled         As Boolean
  Dim sDisabledTooltip  As String

  sFCON = "FCONCS" & iCS & "_"
  sTCON = "TCONCS" & iCS & "_"

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

  If (Pagename = "#CS" & iCS & " Timing") Then
    '-------------------------------------------------------------------------
    ' page: CSxTiming (action)
    '-------------------------------------------------------------------------
    Select Case (Controlname)
        '---------------------------------------------------------------------
        ' control: cboPHAx (action)
        '---------------------------------------------------------------------
        Case "cboPHAx"
            For i = 0 To 3
                If (v(1) = i & " clock cycles") Then
                    r.v(sTCON & "PHA" & iCS) = i
                End If
            Next i

        '---------------------------------------------------------------------
        ' control: cboPHBx (action)
        '---------------------------------------------------------------------
        Case "cboPHBx"
            For i = 1 To 2
                If (v(1) = i & " clock cycles") Then
                    r.v(sTCON & "PHB" & iCS) = i - 1
                End If
            Next i

        '---------------------------------------------------------------------
        ' control: cboPHCx (action)
        '---------------------------------------------------------------------
        Case "cboPHCx"
            For i = 0 To 3
                If (v(1) = i & " clock cycles") Then
                    r.v(sTCON & "PHC" & iCS) = i
                End If
            Next i

        '---------------------------------------------------------------------
        ' control: cboPHDx (action)
        '---------------------------------------------------------------------
        Case "cboPHDx"
            For i = 0 To 1
                If (v(1) = i & " clock cycles") Then
                    r.v(sTCON & "PHD" & iCS) = i
                End If
            Next i

        '---------------------------------------------------------------------
        ' control: cboPHEx (action)
        '---------------------------------------------------------------------
        Case "cboPHEx"
            For i = 1 To 32
                If (v(1) = i & " clock cycles") Then
                    r.v(sTCON & "PHE" & iCS) = i - 1
                End If
            Next i

        '---------------------------------------------------------------------
        ' control: cboRDPHFx (action)
        '---------------------------------------------------------------------
        Case "cboRDPHFx"
            For i = 0 To 3
                If (v(1) = i & " clock cycles") Then
                    r.v(sTCON & "RDPHF" & iCS) = i
                End If
            Next i

        '---------------------------------------------------------------------
        ' control: cboWRPHFx (action)
        '---------------------------------------------------------------------
        Case "cboWRPHFx"
            For i = 0 To 3
                If (v(1) = i & " clock cycles") Then
                    r.v(sTCON & "WRPHF" & iCS) = i
                End If
            Next i

      Case Else
    End Select

    '-------------------------------------------------------------------------
    ' page: CSxTiming (show)
    '-------------------------------------------------------------------------
    bDisabled = (IsEBCDisabled(r) Or r.v(sFCON & "ENCS" & iCS) = 0)
    
    sDisabledTooltip = ""
    sDisabledTooltip = TOOLTIP_DISABLED_IF
    If (IsEBCDisabled(r)) Then
      sDisabledTooltip = sDisabledTooltip & _
        GetTooltipEBCIsDisabled(r)
    End If
    If (r.v(sFCON & "ENCS" & iCS) = 0) Then
      sDisabledTooltip = sDisabledTooltip & _
        FormatDisabledTooltip("the chip select channel " & iCS & " is disabled (ENCS" & iCS & " = 0)")
    End If

    SetDefaultPropertiesForAllControlsOnPage(Pagename, c, e, bDisabled, sDisabledTooltip, "")

    '-------------------------------------------------------------------------
    ' control: cboPHAx (show)
    '-------------------------------------------------------------------------
    With c("cboPHAx")
        .Clear
        For i = 0 To 3
            .AddItem i & " clock cycles"
        Next i
        .ListIndex = r.v(sTCON & "PHA" & iCS)
    End With

    '-------------------------------------------------------------------------
    ' control: cboPHBx (show)
    '-------------------------------------------------------------------------
    With c("cboPHBx")
        .Clear
        For i = 1 To 2
            .AddItem i & " clock cycles"
        Next i
        .ListIndex = r.v(sTCON & "PHB" & iCS)
    End With

    '-------------------------------------------------------------------------
    ' control: cboPHCx (show)
    '-------------------------------------------------------------------------
    With c("cboPHCx")
        .Clear
        For i = 0 To 3
            .AddItem i & " clock cycles"
        Next i
        .ListIndex = r.v(sTCON & "PHC" & iCS)
    End With

    '-------------------------------------------------------------------------
    ' control: cboPHDx (show)
    '-------------------------------------------------------------------------
    With c("cboPHDx")
        .Clear
        For i = 0 To 1
            .AddItem i & " clock cycles"
        Next i
        .ListIndex = r.v(sTCON & "PHD" & iCS)
    End With

    '-------------------------------------------------------------------------
    ' control: cboPHEx (show)
    '-------------------------------------------------------------------------
    With c("cboPHEx")
        .Clear
        For i = 1 To 32
            .AddItem i & " clock cycles"
        Next i
        .ListIndex = r.v(sTCON & "PHE" & iCS)
    End With

    '-------------------------------------------------------------------------
    ' control: cboRDPHFx (show)
    '-------------------------------------------------------------------------
    With c("cboRDPHFx")
        .Clear
        For i = 0 To 3
            .AddItem i & " clock cycles"
        Next i
        .ListIndex = r.v(sTCON & "RDPHF" & iCS)
    End With

    '-------------------------------------------------------------------------
    ' control: cboWRPHFx (show)
    '-------------------------------------------------------------------------
    With c("cboWRPHFx")
        .Clear
        For i = 0 To 3
            .AddItem i & " clock cycles"
        Next i
        .ListIndex = r.v(sTCON & "WRPHF" & iCS)
    End With

  End If

  OnShowPageCSxTiming = True
  Exit Function

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

