Attribute VB_Name = "Module1"
'#Uses "..\classes\FunctionClass.cls"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Pages.inc"
'#Uses "..\include\Port.inc"

Option Explicit
'*****************************************************************************
' Module:
'        EBC
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the
'        EBC dialog script and in the EBC codegenerator script.
'
'-----------------------------------------------------------------------------
' 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
'                                       - added fuction 'XPeripheralAreEnabled'
'                                       - added fuction
'                                         'GetTooltipXPeripheralAreEnabled'
'              ahe         10.08.00     - changes fct 'SelectPinInterfaceP0H'
'                                         abd 'SelectPinInterfaceP0L'
'                                       - added fct 'Select_Pin_Interface_For_EBC'
'
'        ----------------------------------------------------------------------
'        7.0   ahe         27.03.01     final version for Pegasus
'
'        7.1   ahe         02.07.02     add function DisableEBC
'        ----------------------------------------------------------------------
'              ste         24.11.03     the functions SelectPinInterfaceP and
'                                       DeselectPinInterfaceP takes care of
'                                       register EBCMOD1
'
'*****************************************************************************

'*****************************************************************************
' Constants
'*****************************************************************************

'*****************************************************************************
'Description:
'        For each function which should be used for this module a line
'        "If (Not func.AddFunction .." must be insert in this function.
'        For each parameter that should be used for this function a
'        line "If (Not func.AddParameter .." must be insert in this function.
'
'        Note:
'        For each added function a project variable with the name
'        "EBC_NAME_Functionname" must be exist in the *.vars file.
'-----------------------------------------------------------------------------
'Parameters:
'        func             reference to the FunctionClass object
'        r                reference to the Registers object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function RefreshFunctionDescriptions(vIndex As Variant, func As FunctionClass, r As ProjectData) As Boolean
  On Error Resume Next

  Dim sDescription As String
  Dim sReturnValue As String
  Dim sVarPre As String
  
  sVarPre = "EBC"

  RefreshFunctionDescriptions = False

  ' for each function of this module the property AddFunction must be called
  '
  ' AddFunction(sFuncName As String, sFuncType As String, sFuncDescription As String,
  '             sReturnvalue As String, lSort As Long) As Boolean
  '
  ' sFuncName          | String | function name as key (use the name of the variable
  '                               for the function name e.g. SSC_NAME_Init)
  ' sFuncType          | String | type of the function
  ' sFuncDescription   | String | description of the function
  ' sReturnvalue       | String | description of the return value
  ' lSort              | Long   | ' sort of the function (0 => macro, 1 => function, 2 => ISR, 3 => TRAP)

    ' for each parameter of a function the property AddParameter must be called
    '
    'AddParameter(sParName As String, sPar As String, sParType As String, sParDescription As String) As Boolean
    '
    ' sParName         | String | parameter name (key)
    ' sPar             | String | parameter name
    ' sParType         | String | type of the parameter
    ' sParDescription  | String | description of the parameter


  '------------------------------------------------------------------------------------------------
  ' Description for EBC_NAME_Init
  '------------------------------------------------------------------------------------------------
  sDescription = GetGeneralInitDescription(sVarPre)
  sReturnValue = "None"
  If (Not func.AddFunction("EBC_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function
 
 
        If (Not func.Sort) Then Exit Function

  RefreshFunctionDescriptions = True
End Function


'*****************************************************************************
'Description:
'        This function checks if some port pins free.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        PinName         Alternatpinname
'        lowIndex        low index
'        highIndex       high index
'
'-----------------------------------------------------------------------------
'Returns:
'        True            port is free
'        False           else
'*****************************************************************************
Function IsPinInterfaceFree(r As ProjectData, PinName As String, lowIndex As Integer, highIndex As Integer) As Boolean
  On Error Resume Next
  Dim i As Integer
  
  IsPinInterfaceFree = False

  For i = lowIndex To highIndex
    
      If (IsAlternateInputPinFree(r, PinName & i)) Then
        IsPinInterfaceFree = True
      Else
        IsPinInterfaceFree = False
        Exit Function
      End If

  Next i

End Function

'*****************************************************************************
'Description:
'        This function select the port pins on P10H.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub SelectPinInterfaceP10H(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer

  If (r.v("COUNTER_P10H") = 0) Then
    For i = 8 To 12
      Call RequestAlternateOutputPin(r, "EBC_AD" & i)
    Next i
    r.v("EBC_EBCMOD1_DHPDIS") = 0 ' all pins from P10.8 - P10.12 of P10 are used for the EBC
  End If
  r.v("COUNTER_P10H") = r.v("COUNTER_P10H") + 1
  
  If IsMissing(noCount) Then
    'if no count
  Else
    If (noCount) Then
      For i = 8 To 12
        r.v("MuxFor_P10_" & i) = 0
        r.v(GetOutputRegName("10", i)) = 0
      Next i
    End If
  End If

End Sub
'*****************************************************************************
'Description:
'        This function select the port pins on P10L.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub SelectPinInterfaceP10L(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer

  If (r.v("COUNTER_P10L") = 0) Then
    For i = 0 To 7
      Call RequestAlternateOutputPin(r, "EBC_AD" & i)
    Next i
  End If
  r.v("COUNTER_P10L") = r.v("COUNTER_P10L") + 1
  
  If IsMissing(noCount) Then
    'if no count
  Else
    If (noCount) Then
      For i = 0 To 7
        r.v("MuxFor_P10_" & i) = 0
        r.v(GetOutputRegName("10", i)) = 0
      Next i
    End If
  End If

End Sub
'*****************************************************************************
'Description:
'        This function select the port pins on P2.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub SelectPinInterfaceP2(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer

  If (r.v("COUNTER_P2") = 0) Then
    For i = 13 To 15
      Call RequestAlternateOutputPin(r, "EBC_AD" & i)
    Next i
    r.v("EBC_EBCMOD1_DHPDIS") = 0 ' all pins from P2.0 - P2.2 of P2 are used for the EBC
  End If
  r.v("COUNTER_P2") = r.v("COUNTER_P2") + 1
  
  If IsMissing(noCount) Then
    'if no count
  Else
    If (noCount) Then
      For i = 0 To 2
        r.v("MuxFor_P2_" & i) = 0
        r.v(GetOutputRegName("2", i)) = 0
      Next i
    End If
  End If

End Sub
'*****************************************************************************
'Description:
'        This function select the port pins on P0.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub SelectPinInterfaceP0(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
 
  If (r.v("COUNTER_P0") = 0) Then
    For i = 0 To 7
      Call RequestAlternateOutputPin(r, "EBC_A" & i)
    Next i
    r.v("EBC_EBCMOD1_ALPDIS") = 0    ' all pins of P0 are used for the EBC
    r.v("EBC_EBCMOD1_A0PDIS") = 0    ' pin 0 of P0 is used for the EBC
  End If
  r.v("COUNTER_P0") = r.v("COUNTER_P0") + 1
  
  If IsMissing(noCount) Then
    'if no count
  Else
    If (noCount) Then
      For i = 0 To 7
        r.v("MuxFor_P0_" & i) = 0
        r.v(GetOutputRegName("0", i)) = 0
      Next i
    End If
  End If

End Sub
'*****************************************************************************
'Description:
'        This function select the port pins on P1.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub SelectPinInterfaceP1(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
 
  If (r.v("COUNTER_P1") = 0) Then
    For i = 8 To 15
      Call RequestAlternateOutputPin(r, "EBC_A" & i)
    Next i
    r.v("EBC_EBCMOD1_APDIS") = 0  ' pins P1.0 to P1.7are used for the EBC.
  End If
  r.v("COUNTER_P1") = r.v("COUNTER_P1") + 1
  
  If IsMissing(noCount) Then
    'if no count
  Else
    If (noCount) Then
      For i = 0 To 7
        r.v("MuxFor_P1_" & i) = 0
        r.v(GetOutputRegName("1", i)) = 0
      Next i
    End If
  End If

End Sub
'*****************************************************************************
'Description:
'        This function release the port pins on P0.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub ReleasePinInterfaceP0(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
  
  r.v("COUNTER_P0") = r.v("COUNTER_P0") - 1
  
  If (r.v("COUNTER_P0") = 0) Then
    For i = 0 To 7
      Call ReleaseAlternateOutputPin(r, "EBC_A" & i)
      
      r.v("EBC_EBCMOD1_ALPDIS") = 1    ' all pins of P0 are NOT used for the EBC,
                                       ' so they can be used for other purpose
      r.v("EBC_EBCMOD1_A0PDIS") = 1    ' pin 0 of P0 is NOT used for the EBC,
                                       ' so it can be used for other purpose
    Next i
  End If

End Sub
'*****************************************************************************
'Description:
'        This function release the port pins on P1.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub ReleasePinInterfaceP1(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
  
  r.v("COUNTER_P1") = r.v("COUNTER_P1") - 1
  If (r.v("COUNTER_P1") = 0) Then
    For i = 8 To 15
      Call ReleaseAlternateOutputPin(r, "EBC_A" & i)
    Next i
    r.v("EBC_EBCMOD1_APDIS") = 15    ' pins P1.1 to P1.7 are NOT used for the EBC,
                                     ' so they can be used for other purpose
  End If

End Sub
'*****************************************************************************
'Description:
'        This function release the port pins on P2.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub ReleasePinInterfaceP2(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
  
  r.v("COUNTER_P2") = r.v("COUNTER_P2") - 1
  If (r.v("COUNTER_P2") = 0) Then
    For i = 13 To 15
      Call ReleaseAlternateOutputPin(r, "EBC_AD" & i)
    Next i
    r.v("EBC_EBCMOD1_DHPDIS") = 1 ' all pins of P2.0 - P2.2 are NOT used for the EBC,
                                  ' so they can be used for other purpose
  End If

End Sub
'*****************************************************************************
'Description:
'        This function release the port pins on P10L.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub ReleasePinInterfaceP10L(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
  
  r.v("COUNTER_P10L") = r.v("COUNTER_P10L") - 1
  If (r.v("COUNTER_P10L") = 0) Then
    For i = 0 To 7
      Call ReleaseAlternateOutputPin(r, "EBC_AD" & i)
    Next i
  End If

End Sub
'*****************************************************************************
'Description:
'        This function release the port pins on P10H.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub ReleasePinInterfaceP10H(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
  
  r.v("COUNTER_P10H") = r.v("COUNTER_P10H") - 1
  
  If (r.v("COUNTER_P10H") = 0) Then
    For i = 8 To 12
      Call ReleaseAlternateOutputPin(r, "EBC_AD" & i)
    Next i
    r.v("EBC_EBCMOD1_DHPDIS") = 1 ' all pins of P10.8 - P10.12 are NOT used for the EBC,
                                  ' so they can be used for other purpose
  End If

End Sub
'*****************************************************************************
'Description:
'        This function returns a formated disabled tooltip if the pins
'        A0 .. A15 or AD0 .. AD15 not free.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        PinName         Alternatpinname
'        lowIndex        low index
'        highIndex       high index
'
'-----------------------------------------------------------------------------
'Returns:
'        Formated tooltip
'*****************************************************************************
Function GetTooltipPinInterfaceDisabled(r As ProjectData, PinName As String, lowIndex As Integer, highIndex As Integer) As String
    On Error Resume Next
    Dim sVarPre As String
    Dim s As String
    Dim i As Integer

    sVarPre = "EBC"
  
    GetTooltipPinInterfaceDisabled = ""
    s = ""
  
    For i = lowIndex To highIndex
        If (Not (IsAlternateOutputPinFree(r, PinName & i))) Then
          s = s & GetDisabledAlternatePinTooltip(r, PinName & i)
        End If
    Next i

    GetTooltipPinInterfaceDisabled = s
   
End Function

'*****************************************************************************
'Description:
'        This function select the port pins for EBC module. Do not call this
'        function if BUSCONx_BUSACTx is not = 0 !!! Only for switched between
'        BTYP's.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        iBus            number of the bus to be set
'        iValue          value of BTYP to be set
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub Select_Pin_Interface_For_EBC(r As ProjectData, iBus As Integer, iValue As Integer)
    Dim sBTYPReg As String

    sBTYPReg = "EBC_FCONCS" & iBus & "_BTYP"

    'release pin's
    If (r.v(sBTYPReg) = 0) Then         ' 8-bit DEMUX
            Call ReleasePinInterfaceP1(r)
            Call ReleasePinInterfaceP0(r)

    ElseIf (r.v(sBTYPReg) = 1) Then     ' 8-bit MUX
            Call ReleasePinInterfaceP10H(r)
            Call ReleasePinInterfaceP2(r)
            
    ElseIf (r.v(sBTYPReg) = 2) Then     ' 16-bit DEMUX
            Call ReleasePinInterfaceP1(r)
            Call ReleasePinInterfaceP0(r)
            Call ReleasePinInterfaceP10H(r)
            Call ReleasePinInterfaceP2(r)

    ElseIf (r.v(sBTYPReg) = 3) Then     ' 16-bit MUX
            Call ReleasePinInterfaceP10H(r)
            Call ReleasePinInterfaceP2(r)
            
    End If

    'select pin's
    If (iValue = 0) Then                ' 8-bit DEMUX
         Call SelectPinInterfaceP1(r)
         Call SelectPinInterfaceP0(r)

    ElseIf (iValue = 1) Then            ' 8-bit MUX
            Call SelectPinInterfaceP10H(r)
            Call SelectPinInterfaceP2(r)

    ElseIf (iValue = 2) Then            ' 16-bit DEMUX
            Call SelectPinInterfaceP1(r)
            Call SelectPinInterfaceP0(r)
            Call SelectPinInterfaceP10H(r)
            Call SelectPinInterfaceP2(r)

    ElseIf (iValue = 3) Then            ' 16-bit MUX
            Call SelectPinInterfaceP10H(r)
            Call SelectPinInterfaceP2(r)
        

    End If

End Sub

'*****************************************************************************
'Description:
'        This function checks if the EBC is disabled.
'-----------------------------------------------------------------------------
'Parameters:
'          r  ProjectData
'-----------------------------------------------------------------------------
'Returns:
'        True            if EBC is disabled
'        False           else
'*****************************************************************************
Function IsEBCDisabled(r As ProjectData) As Boolean

  If (r.v("EBC_EBCMOD0_EBCDIS") = 0) Then
      IsEBCDisabled = False
  Else
      IsEBCDisabled = True
  End If

End Function


'*****************************************************************************
'Description:
'        This function returns disabled tooltip if EBC is disabled.
'-----------------------------------------------------------------------------
'Parameters:
'          r  ProjectData
'-----------------------------------------------------------------------------
'Returns:
'       formated tooltip if EBC is disabled
'*****************************************************************************
Function GetTooltipEBCIsDisabled(r As ProjectData) As String

  If (r.v("EBC_EBCMOD0_EBCDIS") = 0) Then
      GetTooltipEBCIsDisabled = ""
  Else
      GetTooltipEBCIsDisabled = FormatDisabledTooltip("EBC is disabled")
  End If

End Function


'*****************************************************************************
'Description:
'        This function deselect iCS channel.
'-----------------------------------------------------------------------------
'Parameters:
'          r    ProjectData
'          iCS  number of channel
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub DeselectCSx(r As ProjectData, iCS As Integer)
  Dim sFCON As String

  sFCON = "EBC_FCONCS" & iCS & "_"

  r.v(sFCON & "ENCS") = 0
  
  Call ReleasePinInterfaceP10L(r)

  If (r.v(sFCON & "BTYP") = 0) Then                      '8-bit demultiplex
    Call ReleasePinInterfaceP0(r)
    Call ReleasePinInterfaceP1(r)
  End If

  If (r.v(sFCON & "BTYP") = 1 Or r.v(sFCON & "BTYP") = 3) Then '8/16-bit multiplex
    Call ReleasePinInterfaceP10H(r)
    Call ReleasePinInterfaceP2(r)
  End If

  If (r.v(sFCON & "BTYP") = 2) Then                           '16-bit demultiplex
    Call ReleasePinInterfaceP10H(r)
    Call ReleasePinInterfaceP2(r)
    Call ReleasePinInterfaceP0(r)
    Call ReleasePinInterfaceP1(r)
  End If

  r.sfr("EBC_FCONCS" & iCS) = 0

End Sub

'*****************************************************************************
'Description:
'        This function disables the EBC.
'-----------------------------------------------------------------------------
'Parameters:
'          r    ProjectData
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub DisableEBC(r As ProjectData)
    On Error Resume Next
    Dim sReg      As String
    Dim i         As Integer
    Dim iMaxCS    As Integer

    sReg = "EBC_EBCMOD0_"
    r.v(sReg & "EBCDIS") = 1
'    r.sfr("EBCMOD1") = &H005F
    
    ' disable control page
    r.v(sReg & "ALEDIS") = 1
    Call ReleaseAlternateOutputPin(r, "EBC_ALE_O")

    r.v(sReg & "BYTDIS") = 1
    r.v(sReg & "WRCFG") = 0
    If (GetPinFunction(r, "EBC_WRH_n") = 2) Then
      Call ReleaseAlternateOutputPin(r, "EBC_WRH_n")
    End If
    
    If (GetPinFunction(r, "EBC_BHE_n") = 2) Then
      Call ReleaseAlternateOutputPin(r, "EBC_BHE_n")
    End If

    If (r.Exist("MuxFor_EBC_READY_I") = True) Then
        r.v(sReg & "RDYDIS") = 1
        Call ReleaseAlternateInputPin(r, "EBC_READY_I")
        r.v(sReg & "RDYPOL") = 0
    End If

    If (IsControllerTypeOK(r) = 1) Then 'If it is a 144 pin package
        r.v(sReg & "ARBEN") = 0
        r.v(sReg & "SLAVE") = 0
        Call ReleaseAlternateInputPin(r, "EBC_HOLD_I")
        Call ReleaseAlternateOutputPin(r, "EBC_BREQ")
    
        If (GetPinFunction(r, "EBC_HLDA_I") = 1) Then
          Call ReleaseAlternateInputPin(r, "EBC_HLDA_I")
        End If
        If (GetPinFunction(r, "EBC_HLDA") = 2) Then
          Call ReleaseAlternateOutputPin(r, "EBC_HLDA")
        End If
    End If
    
    If (IsControllerTypeOK(r) = 1) Then
        iMaxCS = 4
    Else
        iMaxCS = 3
    End If

    For i = 0 To iMaxCS
        If (r.v("EBC_FCONCS" & i & "_ENCS") = 1) Then
            Call DeselectCSx(r, i)
        End If
    Next i

End Sub
