'#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 P0H.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub SelectPinInterfaceP0H(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
  
  If (r.v("COUNTER_P0H") = 0) Then
    For i = 8 To 15
      Call RequestAlternateOutputPin(r, "AD" & i)
    Next i
    r.v("EBCMOD1_DHPDIS") = 0 ' all pins of P0H are used for the EBC
  End If
  r.v("COUNTER_P0H") = r.v("COUNTER_P0H") + 1

  If IsMissing(noCount) Then
    'if no count
  Else
    If (noCount) Then
      For i = 0 To 7
        r.v("MuxFor_P0H_" & i) = 0
        r.v(GetOutputRegName("0H", i)) = 0
      Next i
    End If
  End If

End Sub

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

  If (r.v("COUNTER_P0L") = 0) Then
    For i = 0 To 7
      Call RequestAlternateOutputPin(r, "AD" & i)
    Next i
  End If
  r.v("COUNTER_P0L") = r.v("COUNTER_P0L") + 1
  
  If IsMissing(noCount) Then
    'if no count
  Else
    If (noCount) Then
      For i = 0 To 7
        r.v("MuxFor_P0L_" & i) = 0
        r.v(GetOutputRegName("0L", i)) = 0
      Next i
    End If
  End If

End Sub

'*****************************************************************************
'Description:
'        This function select the port pins on P1H.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub SelectPinInterfaceP1H(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
  
  If (r.v("COUNTER_P1H") = 0) Then
    For i = 8 To 15
      Call RequestAlternateOutputPin(r, "A" & i)
    Next i
      r.v("EBCMOD1_APDIS") = 0  ' pins P1.1 to P1.15 are used for the EBC,
  End If
  r.v("COUNTER_P1H") = r.v("COUNTER_P1H") + 1

  If IsMissing(noCount) Then
    'if no count
  Else
    If (noCount) Then
      For i = 0 To 7
        r.v("MuxFor_P1H_" & i) = 0
      Next i
    End If
  End If

End Sub

'*****************************************************************************
'Description:
'        This function select the port pins on P1L.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub SelectPinInterfaceP1L(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
  
  If (r.v("COUNTER_P1L") = 0) Then
    For i = 0 To 7
      Call RequestAlternateOutputPin(r, "A" & i)
    Next i
    r.v("EBCMOD1_ALPDIS") = 0    ' all pins of P1L are used for the EBC
    r.v("EBCMOD1_A0PDIS") = 0    ' pin 0 of P1L is used for the EBC
'    r.v("EBCMOD1_APDIS") = 0     ' pins P1.1 to P1.15 are used for the EBC
  End If
  r.v("COUNTER_P1L") = r.v("COUNTER_P1L") + 1

  If IsMissing(noCount) Then
    'if no count
  Else
    If (noCount) Then
      For i = 0 To 7
        r.v("MuxFor_P1L_" & i) = 0
      Next i
    End If
  End If

End Sub



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

End Sub

'*****************************************************************************
'Description:
'        This function release the port pins on P0H.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub ReleasePinInterfaceP0H(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
  
  r.v("COUNTER_P0H") = r.v("COUNTER_P0H") - 1
  If (r.v("COUNTER_P0H") = 0) Then
    For i = 8 To 15
      Call ReleaseAlternateOutputPin(r, "AD" & i)  
    Next i
    r.v("EBCMOD1_DHPDIS") = 1 ' all pins of P0H 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 P1L.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub ReleasePinInterfaceP1L(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
  
    r.v("COUNTER_P1L") = r.v("COUNTER_P1L") - 1
    If (r.v("COUNTER_P1L") = 0) Then
      For i = 0 To 7
        Call ReleaseAlternateOutputPin(r, "A" & i)  
      Next i
      r.v("EBCMOD1_ALPDIS") = 1    ' all pins of P1L are NOT used for the EBC,
                                   ' so they can be used for other purpose
      r.v("EBCMOD1_A0PDIS") = 1    ' pin 0 of P1L is NOT used for the EBC,
                                   ' so it can be used for other purpose
'      r.v("EBCMOD1_APDIS") = 15    ' pins P1.1 to P1.15 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 P1H.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        noCount         with this parameter all Muxis are set to 0
'-----------------------------------------------------------------------------
'Returns:
'       None
'*****************************************************************************
Sub ReleasePinInterfaceP1H(r As ProjectData, Optional noCount As Variant)
  On Error Resume Next
  Dim i As Integer
  
    r.v("COUNTER_P1H") = r.v("COUNTER_P1H") - 1
    If (r.v("COUNTER_P1H") = 0) Then
      For i = 8 To 15
        Call ReleaseAlternateOutputPin(r, "A" & i)  
      Next i
      r.v("EBCMOD1_APDIS") = 15    ' pins P1.1 to P1.15 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 = "FCONCS" & iBus & "_BTYP" & iBus

    'release pin's
    If (r.v(sBTYPReg) = 0) Then         ' 8-bit DEMUX
       'Call ReleasePinInterfaceP0L(r)
        Call ReleasePinInterfaceP1H(r)
        Call ReleasePinInterfaceP1L(r)

    ElseIf (r.v(sBTYPReg) = 1) Then     ' 8-bit MUX
        'Call ReleasePinInterfaceP0L(r)
        Call ReleasePinInterfaceP0H(r)

    ElseIf (r.v(sBTYPReg) = 2) Then     ' 16-bit DEMUX
        'Call ReleasePinInterfaceP0L(r)
        Call ReleasePinInterfaceP0H(r)
        Call ReleasePinInterfaceP1H(r)
        Call ReleasePinInterfaceP1L(r)

    ElseIf (r.v(sBTYPReg) = 3) Then     ' 16-bit MUX
        'Call ReleasePinInterfaceP0L(r)
        Call ReleasePinInterfaceP0H(r)

    End If

    'select pin's
    If (iValue = 0) Then                ' 8-bit DEMUX
       'Call SelectPinInterfaceP0L(r)
        Call SelectPinInterfaceP1H(r)
        Call SelectPinInterfaceP1L(r)

    ElseIf (iValue = 1) Then            ' 8-bit MUX
       'Call SelectPinInterfaceP0L(r)
        Call SelectPinInterfaceP0H(r)

    ElseIf (iValue = 2) Then            ' 16-bit DEMUX
       'Call SelectPinInterfaceP0L(r)
        Call SelectPinInterfaceP0H(r)
        Call SelectPinInterfaceP1H(r)
        Call SelectPinInterfaceP1L(r)

    ElseIf (iValue = 3) Then            ' 16-bit MUX
       'Call SelectPinInterfaceP0L(r)
        Call SelectPinInterfaceP0H(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("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("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 = "FCONCS" & iCS & "_"

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

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

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

  If (r.v(sFCON & "BTYP" & iCS) = 2) Then                           '16-bit demultiplex
    Call ReleasePinInterfaceP0H(r)
    Call ReleasePinInterfaceP1L(r)
    Call ReleasePinInterfaceP1H(r)
  End If

  r.sfr("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 = "EBCMOD0_"
    r.v(sReg & "EBCDIS") = 1
'    r.sfr("EBCMOD1") = &H005F
    
    ' disable control page
    r.v(sReg & "ALEDIS") = 1
    Call ReleaseAlternateOutputPin(r, "ALE_O")

    r.v(sReg & "BYTDIS") = 1
    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

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

    If (r.Exist("MuxFor_HOLD_n") = True) Then
        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
    End If
    
    iMaxCS = 4

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

End Sub