Attribute VB_Name = "Module1"
'#Uses "Format.inc"
'#Uses "Const.inc"

Option Explicit
'*****************************************************************************
' Module:
'        GLOBAL.INC
'-----------------------------------------------------------------------------
' Description:
'        This module contains global functions that can
'        be used in all other script files.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        0.1  Sunil          06-10-04    initial version
'        0.2  Sheshadri      08-10-04    added GetSystemClock, GetCoreClock
'        0.3  Sunil          25-10-04    added BaudrateType datatype,
'                                        function ConvertBaudrate
'        0.4  Sunil          02-12-04    Bug in ConvertBaudrate is fixed.
'                                        (dDivider = dDivider * 10001 chaged to dDivider = dDivider * 1000)
'        0.5  Sunil          29-12-04    Module Prefix is added to register names.
'        0.6  Sunil          04-02-05    Function "GetValueNDIV" added
'        0.7  Jagadeesha     21-12-06    Functions "GetValuePDIV" and "GetValueKDIV" are added
'
'*****************************************************************************

'*****************************************************************************
' Constants
'*****************************************************************************
' User defined data type for baudrate
Type BaudrateType
  dValue As Double   ' Baudrate value
  sUnit As String    ' unit (kBaud / MBaud)
  dDivider As Double ' divider factor referring to kBaud / MBaud
End Type


Global SHINTFLAG As Integer
Global Const RATIO_FSYS_T01 = 2 ' RATIO_FSYS_T01 = fSYS / T01CounterRate
Global Const TXT_US = Chr(181) & "s" ' micro seconds'***************by ashish***************************
'constans for convert a frequency from MHz to Hz
Global Const CONVERT2HZ = 1000000

'constans for precision on frequency
Global Const FREQUENCY_PRECISION = "0.000"

Global Const TOOLTIP_DISABLED_ISR32 = "This function will be automatically selected by DAvE." & vbCrLf & _
                                           "This is the case if the initialization function is selected and " & vbCrLf & _
                                           "the interrupt is enabled. Further, the service request node must " & vbCrLf & _
                                           "be configued as CPU interrupt with a greater level than zero."

'*****************************************************************************
'Description:
'        This function sets the .Enabled, .DisabledTooltipText and
'        .EnabledTooltipText properties for all controls on the page
'        with the name szPagename of a peripherals object.
'-----------------------------------------------------------------------------
'Parameters:
'        szPagename         page name
'        c                  reference to the collection object
'        e                  reference to the extender collection object
'        bDisabled          disable condition
'        szDisabledTooltip  tooltip if control is disabled
'        szEnabledTooltip   tooltip if control is enabled
'
'*****************************************************************************
Sub SetDefaultPropertiesForAllControlsOnPage(szPagename As String, c As Object, e As Object, bDisabled As Boolean, szDisabledTooltip As String, szEnabledTooltip As String)
  On Error Resume Next

  Dim o As Object
  Dim s As String

  ' for each control
  For Each o In c
    ' on this page
    s = ""
    s = o.Container.Container.Tag
    If (s = szPagename) Then
      o.Enabled = Not bDisabled
      ' determine tooltip
      If (bDisabled) Then
        ' tooltip if control is disabled
        e(o.Tag).DisabledTooltipText = szDisabledTooltip
      Else
        ' tooltip if control is enabled
        e(o.Tag).EnabledTooltipText = szEnabledTooltip
      End If
    End If
  Next
End Sub


'*****************************************************************************
'Description:
'       The function returns the current system frequency fSYS in MHz
'-----------------------------------------------------------------------------
'Parameters:
'-----------------------------------------------------------------------------
'Returns:
'        system frequency in MHz
'*****************************************************************************
Function GetSystemClock(r As ProjectData) As Variant
    On Error GoTo GetSystemClockError

    Select Case (r.v("PLL_CTRL"))
        Case 1
            GetSystemClock = r.v("MAIN_FOSC")
        Case 2
            GetSystemClock = 150
        Case 3
            GetSystemClock = r.v("MAIN_FOSC")
        Case 4
            GetSystemClock = (r.v("MAIN_FOSC") * GetValueNDIV(r)) / (GetValuePDIV(r) * GetValueKDIV(r))
    End Select
    
GetSystemClockError:
    Exit Function
End Function

'*****************************************************************************
'Description:
'         This function returns the Core clock [MHz].
'-----------------------------------------------------------------------------
'-----------------------------------------------------------------------------
'Parameters:
'-----------------------------------------------------------------------------
'Returns:
'        Core frequency in MHz
'*****************************************************************************
Function GetCoreClock(r As ProjectData) As Variant
    On Error GoTo GetCoreClockError
      
        GetCoreClock = GetSystemClock(r) / (GetValueCLKREL(r))
      Exit Function
    
GetCoreClockError:
    ErrorBox "Error"
End Function

'*****************************************************************************
'Description:
'         This function returns the Peripheral clock [MHz].
'-----------------------------------------------------------------------------
'-----------------------------------------------------------------------------
'Parameters:
'-----------------------------------------------------------------------------
'Returns:
'        Peripheral frequency in MHz
'*****************************************************************************
Function GetPeriClock(r As ProjectData) As Variant
    On Error GoTo GetPeriClockError
      
       GetPeriClock = GetSystemClock(r) / (GetValueCLKREL(r))
      Exit Function
    
GetPeriClockError:
    ErrorBox "Error"
End Function

'*****************************************************************************
'Description:
'        Core Frequency
'        This function returns the fCCLK clock [MHz], or strin if the fin is too
'        low or to high.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'
'-----------------------------------------------------------------------------
'Returns:
'        fCCLK clock [MHz], or
'        string "to low" or "to high"
'*****************************************************************************

Function Get_fFCLK(r As ProjectData) As Variant
    Dim dF As Double

    dF = r.v("MAIN_FSYS") / (GetValueCLKREL(r))    ' same as Peripheral Clock
    
    If (r.v(CORE_PREFIX & "CMCON_FCCFG") = 1) Then
        dF = dF * 2
    End If
    If (dF < MAIN_FCLK_MIN) Then
        Get_fFCLK = TO_LOW
    ElseIf (dF > MAIN_FCLK_MAX) Then
        Get_fFCLK = TO_HIGH
    Else
        Get_fFCLK = Format(dF, "00.00")
    End If

End Function

'*****************************************************************************
'Description:
'        Core Frequency
'        This function returns the fCCLK clock [MHz] for CCU, or strin if the fin is too
'        low or to high.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r           reference to the ProjectData object
'        iNr         format of the CCU6 Clock
'                    0 -> this function returns CCU6 Clock, or string "too low" or "too high"
'                    1 -> this function returns exact CCU6 Clock even too low or too high clock.
'
'-----------------------------------------------------------------------------
'Returns:
'        fCCLK clock [MHz], or
'        string "to low" or "to high"
'*****************************************************************************
Function GetCCU_fFCLK(r As ProjectData, iNr As Integer) As Variant
    Dim dF As Double

    dF = r.v("MAIN_FSYS") / (GetValueCLKREL(r))    ' same as Peripheral Clock
    
    If (r.v(CORE_PREFIX & "CR_MISC_CCUCCFG") = 1) Then
        dF = dF * 2
    End If

    If (iNr = 0) Then
      If (dF < MAIN_FCLK_MIN) Then
          GetCCU_fFCLK = TO_LOW
      ElseIf (dF > MAIN_FCLK_MAX) Then
          GetCCU_fFCLK = TO_HIGH
      Else
          GetCCU_fFCLK = Format(dF, "00.00")
      End If
    Else
      GetCCU_fFCLK = Format(dF, "00.00")
    End If

End Function

'*****************************************************************************
'Description:
'        Core Frequency
'        This function returns the fCCLK clock [MHz] for T2CCU, or strin if the fin is too
'        low or to high.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r           reference to the ProjectData object
'        iNr         format of the T2CCU Clock
'                    0 -> this function returns T2CCU Clock, or string "too low" or "too high"
'                    1 -> this function returns exact T2CCU Clock even too low or too high clock.
'
'-----------------------------------------------------------------------------
'Returns:
'        fCCLK clock [MHz], or
'        string "to low" or "to high"
'*****************************************************************************

Function GetT2CCU_fFCLK(r As ProjectData, iNr As Integer) As Variant
    Dim dF As Double

    dF = r.v("MAIN_FSYS") / (GetValueCLKREL(r))    ' same as Peripheral Clock
    
    If (r.v(CORE_PREFIX & "CR_MISC_T2CCFG") = 1) Then
        dF = dF * 2
    End If

    If (iNr = 0) Then
      If (dF < MAIN_FCLK_MIN) Then
          GetT2CCU_fFCLK = TO_LOW
      ElseIf (dF > MAIN_FCLK_MAX) Then
          GetT2CCU_fFCLK = TO_HIGH
      Else
          GetT2CCU_fFCLK = Format(dF, "00.00")
      End If
    Else
      GetT2CCU_fFCLK = Format(dF, "00.00")
    End If

End Function

'*****************************************************************************
'Description:
'       This function checks if the passed register name is defined in the
'       register file. If ok, the same name is returned.
'       If not ok, an error message is displayed an a #error statement is returned.
'-----------------------------------------------------------------------------
'Parameters:
'        sSfrName   Register Name
'-----------------------------------------------------------------------------
'Returns:
'        okay:      Register Name
'        error:     "#error..."
'*****************************************************************************
Function SfrName(r As ProjectData, sSfrName As String) As String
    On Error Resume Next

    SfrName = ""
    If (Not r.Regs.SfrFind(sSfrName)) Then GoTo RegError
    SfrName = sSfrName
    Exit Function

RegError:
    SfrName = RegErrMsg(sSfrName)
End Function

'*****************************************************************************
'Description:
'       This function checks if the passed register name is defined in the
'       register file. If ok, its address is returned in string format.
'       If not ok, an error message is displayed an a #error statement is returned.
'-----------------------------------------------------------------------------
'Parameters:
'        sSfrName   Register Name
'-----------------------------------------------------------------------------
'Returns:
'        okay:      Register Address
'        error:     "#error..."
'*****************************************************************************
Function SfrAddress(r As ProjectData, sSfrName As String) As String
    On Error Resume Next

    SfrAddress = ""
    If (Not r.Regs.SfrFind(sSfrName)) Then GoTo RegError
    SfrAddress = r.Regs.SfrAdress
    Exit Function

RegError:
    SfrAddress = RegErrMsg(sSfrName)
End Function

'*****************************************************************************
'Description:
'       This function checks if the passed SFR bit is defined in the
'       register file and if the register is bit-addressable.
'       If ok, the bit name is returned.
'       If not ok, an error message is displayed an a #error statement is returned.
'-----------------------------------------------------------------------------
'Parameters:
'        sBitVarName    SFR bit as project variable name, e.g. "ADCON0_BSY"
'-----------------------------------------------------------------------------
'Returns:
'                       SFR bit as C register name, e.g. "BSY"
'*****************************************************************************
Function BitName(r As ProjectData, sBitVarName As String) As String
    On Error Resume Next

    Dim sSfrName As String
    Dim lAddress As Long

    BitName = ""
    If (Not r.Regs.BitFind(sBitVarName)) Then GoTo RegError
    sSfrName = r.Regs.BitSfrName
    r.Regs.SfrIndex = r.Regs.BitSfrIndex
    lAddress = Val("&H" & Right(r.Regs.SfrAdress, Len(r.Regs.SfrAdress) - 2))

    If (lAddress > &HFF) Or ((lAddress Mod 8) <> 0) Then GoTo RegError

    BitName = Mid(r.Regs.BitName, Len(sSfrName) + 2)
    Exit Function

RegError:
    BitName = RegErrMsg(sBitVarName)
End Function

'*****************************************************************************
'Description:
'       This function returns a bit mask with all passed bits set to '1'.
'       If not all of the bits are defined in the register file, an error message
'       will be displayed.
'-----------------------------------------------------------------------------
'Parameters:
'       sSfrName    SFR name, e.g. "ADCON0"
'       sBit0..7    variable number of SFR bits of SFR sSfrName, e.g. "BSY", "ADM"
'-----------------------------------------------------------------------------
'Returns:
'       bit mask    e.g. &H18 for "ADCON0", "BSY", "ADM"
'*****************************************************************************
Function BitMask(r As ProjectData, sSfrName As String, Optional sBit0 As String, Optional sBit1 As String = "", _
    Optional sBit2 As String = "", Optional sBit3 As String = "", Optional sBit4 As String = "", _
    Optional sBit5 As String = "", Optional sBit6 As String = "", Optional sBit7 As String = "") As Long
    On Error Resume Next

    Dim sBitName    As String
    Dim s           As String
    Dim iBit        As Long

    BitMask = 0
    For iBit = 0 To 7
        If (iBit = 0) Then sBitName = sBit0
        If (iBit = 1) Then sBitName = sBit1
        If (iBit = 2) Then sBitName = sBit2
        If (iBit = 3) Then sBitName = sBit3
        If (iBit = 4) Then sBitName = sBit4
        If (iBit = 5) Then sBitName = sBit5
        If (iBit = 6) Then sBitName = sBit6
        If (iBit = 7) Then sBitName = sBit7

        If (sBitName <> "") Then
            s = sSfrName & "_" & sBitName
            If (Not r.Regs.BitFind(s)) Then GoTo RegError
            BitMask = BitMask + (2 ^ r.Regs.BitPos)
        End If
    Next iBit
    Exit Function

RegError:
    RegErrMsg (s)
End Function

'*****************************************************************************
'Description:
'       This function generates a C-Statement that sets or clears a SFR bit.
'       The best addressing method is always used:
'           bit-addressable SFRs        e.g.    BSY = 1;
'           non-bit-addressable SFRs    e.g.    ADCON0 |= 0x10;
'
'       If the SFR bit is not defined in the register file, an error message
'       is displayed and an #error statement is returned.
'-----------------------------------------------------------------------------
'Parameters:
'       sBitVarName           SFR bit as project variable name, e.g. "ADCON0_BSY"
'       iBitVal               value to be set: 0...1
'       bTerminated           True:  statement with ';' and appended comment
'                                    (used for functions)
'                             False: statement without ';' and without comment
'                                    (used for macros)
'-----------------------------------------------------------------------------
'Returns:
'       C Statement           e.g. "ADCON0 &= ~(ubyte)0x10;  //   clear BSY"
'*****************************************************************************
Function SfrBitStatement(r As ProjectData, sBitVarName As String, iBitVal As Integer, Optional bTerminated As Boolean = True) As String
    On Error Resume Next

    Dim sBitName    As String
    Dim sSfrName    As String
    Dim sTmp        As String
    Dim sAppendix   As String
    Dim lAddress    As Long

    SfrBitStatement = ""
    If (Not r.Regs.BitFind(sBitVarName)) Then GoTo RegError
    sSfrName = r.Regs.BitSfrName
    r.Regs.SfrIndex = r.Regs.BitSfrIndex
    lAddress = Val("&H" & Right(r.Regs.SfrAdress, Len(r.Regs.SfrAdress) - 2))
    sBitName = Mid(r.Regs.BitName, Len(sSfrName) + 2)

    If (bTerminated) Then
        sAppendix = ";"
    Else
        sAppendix = ""
    End If

    If (lAddress <= &HFF) And ((lAddress Mod 8) = 0) Then
        ' SFR is bit-addressable

        sTmp = sBitName & " = " & iBitVal & sAppendix
    Else
        ' SFR is not bit-addressable

        If (iBitVal = 1) Then
            sTmp = sSfrName & " |= " & FormatTextOutput(2 ^ r.Regs.BitPos, "H2") & sAppendix
        Else
            sTmp = sSfrName & " &= ~(ubyte)" & FormatTextOutput(2 ^ r.Regs.BitPos, "H2") & sAppendix
        End If

        If (bTerminated) Then
            If (iBitVal = 1) Then
                sTmp = FillRight(19, sTmp) & " //   set   " & sBitName
            Else
                sTmp = FillRight(19, sTmp) & " //   clear " & sBitName
            End If
        End If
    End If

    SfrBitStatement = sTmp

    Exit Function

RegError:
    SfrBitStatement = RegErrMsg(sBitVarName)
End Function

'*****************************************************************************
'Description:
'       This function generates a C-Statement that tests a SFR bit.
'       The best addressing method is always used:
'           bit-addressable SFRs        e.g.    BSY
'           non-bit-addressable SFRs    e.g.    (ADCON0 & 0x10) != 0
'
'       If the SFR bit is not defined in the register file, an error message
'       is displayed and an #error statement is returned.
'-----------------------------------------------------------------------------
'Parameters:
'       sBitVarName           SFR bit as project variable name, e.g. "ADCON0_BSY"
'-----------------------------------------------------------------------------
'Returns:
'       C Statement           e.g. "((ADCON0 & 0x10) != 0)"
'*****************************************************************************
Function SfrBitTest(r As ProjectData, sBitVarName As String) As String
    On Error Resume Next

    Dim sBitName    As String
    Dim sSfrName    As String
    Dim lAddress    As Long

    SfrBitTest = ""
    If (Not r.Regs.BitFind(sBitVarName)) Then GoTo RegError
    sSfrName = r.Regs.BitSfrName
    r.Regs.SfrIndex = r.Regs.BitSfrIndex
    lAddress = Val("&H" & Right(r.Regs.SfrAdress, Len(r.Regs.SfrAdress) - 2))

    If (lAddress <= &HFF) And ((lAddress Mod 8) = 0) Then
        ' SFR is bit-addressable

        sBitName = Mid(r.Regs.BitName, Len(sSfrName) + 2)
        SfrBitTest = sBitName
    Else
        ' SFR is not bit-addressable

        SfrBitTest = "((" & sSfrName & " & " & FormatTextOutput(2 ^ r.Regs.BitPos, "H2") & ") != 0)"
    End If
    Exit Function

RegError:
    SfrBitTest = RegErrMsg(sBitVarName)
End Function

'*****************************************************************************
'Description:
'       This function returns the bit field position for the passed bit field.
'       If the bit field is not defined in the register file, an error message
'       will be displayed.
'-----------------------------------------------------------------------------
'Parameters:
'       sBFldVarName   bit field as project variable name, e.g. "ADCON1_ADSTC"
'-----------------------------------------------------------------------------
'Returns:
'       bit field position   e.g. 2 for "ADCON1_ADSTC"
'*****************************************************************************
Function BFldPos(r As ProjectData, sBFldVarName As String) As Long
    On Error Resume Next

    BFldPos = 0
    If (Not r.Regs.BitFind(sBFldVarName)) Then GoTo RegError
    BFldPos = r.Regs.BitPos - r.Regs.BitWidth + 1
    Exit Function

RegError:
    RegErrMsg (sBFldVarName)
End Function

'*****************************************************************************
'Description:
'       todo: workaround for missing information in the register file
'       This functions checks if the SFR is located in the mapped SFR area.
'-----------------------------------------------------------------------------
'Parameters:
'       sSfrName    SFR name, e.g. "ADCON0"
'-----------------------------------------------------------------------------
'Returns:
'       True    SFR is located in the mapped SFR area (accessible only when RMAP=1)
'       False   SFR is not located in the mapped SFR area
'*****************************************************************************
Function IsMappedSfr(sSfrName As String) As Boolean 'todo workaround for missing information in the register file
    On Error Resume Next

    IsMappedSfr = False
    Select Case sSfrName
        ' WDT registers
        Case WDT_PREFIX & "WDTCON"
                IsMappedSfr = True
        Case WDT_PREFIX & "WDTREL"
                IsMappedSfr = True
        Case WDT_PREFIX & "WDTWINB"
                IsMappedSfr = True
        Case WDT_PREFIX & "WDTL"
                IsMappedSfr = True
        Case WDT_PREFIX & "WDTH"
                IsMappedSfr = True
        ' FLASH registers
        Case FLASH_PREFIX & "FSER0"
                IsMappedSfr = True
        Case FLASH_PREFIX & "EESER0"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCON2"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCSP1"
                IsMappedSfr = True
        Case FLASH_PREFIX & "EESER1"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCSP0"
                IsMappedSfr = True
        Case FLASH_PREFIX & "EECSP"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCON0"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCS0"
                IsMappedSfr = True
        Case FLASH_PREFIX & "EECS"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCON1"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FPTCR"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCS1"
                IsMappedSfr = True
        Case FLASH_PREFIX & "EECPP0"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FPTPS0"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FSER1"
                IsMappedSfr = True
        Case FLASH_PREFIX & "EECPP1"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FPTPS1"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCSP2"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FTKAS"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCS2"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCPP0"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FSER2"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FCPP1"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FTCON"
                IsMappedSfr = True
        Case FLASH_PREFIX & "FTRL"
                IsMappedSfr = True
        Case "FLASH_PAGE"
                IsMappedSfr = True
                
        ' OCDS registers
        Case OCDS_PREFIX & "MMCR2"
                IsMappedSfr = True
        Case OCDS_PREFIX & "MMCR"
                IsMappedSfr = True
        Case OCDS_PREFIX & "MMSR"
                IsMappedSfr = True
        Case OCDS_PREFIX & "MMBPCR"
                IsMappedSfr = True
        Case OCDS_PREFIX & "MMICR"
                IsMappedSfr = True
        Case OCDS_PREFIX & "MMDR_R"
                IsMappedSfr = True
        Case OCDS_PREFIX & "MMDR_T"
                IsMappedSfr = True
        Case OCDS_PREFIX & "HWBPSR"
                IsMappedSfr = True
        Case OCDS_PREFIX & "HWBPDR_0L"
                IsMappedSfr = True
        Case OCDS_PREFIX & "HWBPDR_0H"
                IsMappedSfr = True
        Case OCDS_PREFIX & "HWBPDR_1L"
                IsMappedSfr = True
        Case OCDS_PREFIX & "HWBPDR_1H"
                IsMappedSfr = True
        Case OCDS_PREFIX & "HWBPDR_2L"
                IsMappedSfr = True
        Case OCDS_PREFIX & "HWBPDR_2H"
                IsMappedSfr = True
        Case OCDS_PREFIX & "HWBPDR_3L"
                IsMappedSfr = True
        Case OCDS_PREFIX & "HWBPDR_3H"
                IsMappedSfr = True
    End Select
    
End Function

'*****************************************************************************
'Description:
'       This function generates the declaration of a SFR.
'       The used syntax depends on the currently selected compiler.
'-----------------------------------------------------------------------------
'Parameters:
'       sName       SFR name, e.g. "ADCON0"
'       sAddr       SFR address, e.g. "0xD8"
'       sComment    comment, e.g. "// ADC control register"
'-----------------------------------------------------------------------------
'Returns:
'       SFR declaration     e.g. "sfr ADCON0 = 0xD8;    // ADC control register"
'*****************************************************************************
Function SfrDeclaration(r As ProjectData, sName As String, sAddr As String, sComment As String) As String
    On Error Resume Next

'    If (r.v("MAIN_COMPILER") = "Keil") Then
'        SfrDeclaration = "sfr " & FillRight(10, sName) & " = " & sAddr & ";    " & sComment
'     ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
'        SfrDeclaration = "sfr at " & sAddr & " " & FillRight(10, sName) & "  ; " & sComment
'
'    Else
'        SfrDeclaration = "_sfrbyte " & FillRight(10, sName) & " _at(" & sAddr & ");    " & sComment
'    End If
    
     If (r.v("MAIN_COMPILER") = "Keil") Then
        SfrDeclaration = "SFR(" & FillRight(10, sName) & ", " & sAddr & ");    " & sComment
     ElseIf (r.v("MAIN_COMPILER") = "SDCC") Or (r.v("MAIN_COMPILER") = "IAR") Then
        SfrDeclaration = "SFR(" & FillRight(10, sName) & ", " & sAddr & ");    " & sComment
     Else
        SfrDeclaration = "_sfrbyte " & FillRight(10, sName) & " _at(" & sAddr & ");    " & sComment
     End If
End Function

'*****************************************************************************
'Description:
'       This function generates the declaration of a SFR bit
'       (only valid for bit-addressable SFRs)
'       The used syntax depends on the currently selected compiler.
'-----------------------------------------------------------------------------
'Parameters:
'       sName       SFR bit name, e.g. "BSY"
'       sAddr       SFR bit address, e.g. "0xDB"
'       sComment    comment, e.g. "// busy flag"
'-----------------------------------------------------------------------------
'Returns:
'       SFR bit declaration     e.g. "sbit BSY = 0xDB;    // busy flag"
'*****************************************************************************
Function SfrBitDeclaration(r As ProjectData, sName As String, sAddr As String, sComment As String) As String
    On Error Resume Next

    If (r.v("MAIN_COMPILER") = "Keil") Then
        SfrBitDeclaration = "sbit " & FillRight(10, Identifier(r, sName)) & " = " & sAddr & ";    " & sComment
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
        SfrBitDeclaration = "sbit at  " & sAddr & "  " & FillRight(10, Identifier(r, sName)) & "  ; " & sComment
    ElseIf (r.v("MAIN_COMPILER") = "IAR") Then
        SfrBitDeclaration = "__bit __no_init volatile bool  " & FillRight(10, Identifier(r, sName)) & "@" & "  (" & sAddr & ")" & ";" & sComment
    Else
        SfrBitDeclaration = "_sfrbit " & FillRight(10, Identifier(r, sName)) & " _at(" & sAddr & ");    " & sComment
    End If
End Function
'*****************************************************************************
'Description:
'       This function generates the declaration of a SFR bit
'       (only valid for bit-addressable SFRs)
'       The used syntax depends on the currently selected compiler.
'-----------------------------------------------------------------------------
'Parameters:
'       sName       SFR bit name, e.g. "BSY"
'       sAddr       SFR bit address, e.g. "0xDB"
'       sComment    comment, e.g. "// busy flag"
'-----------------------------------------------------------------------------
'Returns:
'       SFR bit declaration     e.g. "sbit BSY = 0xDB;    // busy flag"
'*****************************************************************************
Function SfrBitDeclarationMod(r As ProjectData, sName As String, sSfr As String, sBitPos As Variant, sComment As String) As String
    On Error Resume Next
    If (r.v("MAIN_COMPILER") = "Keil") Then
        SfrBitDeclarationMod = "SBIT(" & FillRight(10, Identifier(r, sName)) & " ," & sSfr & ",BIT" & sBitPos & ");"
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Or (r.v("MAIN_COMPILER") = "IAR") Then
        SfrBitDeclarationMod = "SBIT(" & FillRight(10, Identifier(r, sName)) & " ," & sSfr & ",BIT" & sBitPos & ");"
    End If
End Function

'*****************************************************************************
'Description:
'       This function converts a SFR name or SFR bit name into a valid
'       identifier for the C-compilers. The first occurence of character '/'
'       is replaced by the character '_'.
'-----------------------------------------------------------------------------
'Parameters:
'       sName       input name, e.g. "C/T2"
'-----------------------------------------------------------------------------
'Returns:
'       identifier, e.g. "C_T2"
'*****************************************************************************
Function Identifier(r As ProjectData, sName As String) As String
    On Error Resume Next

    Dim iPos As Integer
    Dim iLen As Integer

    Identifier = sName
    iPos = InStr(sName, "/")
    If (iPos > 0) Then
        iLen = Len(sName)
        Identifier = Left(sName, iPos - 1) & "_" & Right(sName, iLen - iPos)
    End If
End Function

'*****************************************************************************
'Description:
'       This function generates the declaration of an interrupt service routine.
'       The used syntax depends on the currently selected compiler.
'-----------------------------------------------------------------------------
'Parameters:
'       sFuncName           function name, e.g. "XXX_viDummy"
'       sIntNumberName      interrupt number, e.g. "5"
'-----------------------------------------------------------------------------
'Returns:
'       ISR declaration     e.g. "void XXX_viDummy 5"
'*****************************************************************************
Function IsrDeclaration(r As ProjectData, sFuncName As String, sIntNumberName As String) As String
    On Error Resume Next
    
    Select Case (r.v("MAIN_COMPILER"))
        Case "Keil"
            IsrDeclaration = "void " & sFuncName & "(void) interrupt " & sIntNumberName
        Case "SDCC"
            IsrDeclaration = "void " & sFuncName & "(void) interrupt " & sIntNumberName
        Case "IAR"
            IsrDeclaration = "#pragma vector = " & sIntNumberName & vbCrLf & _
                             "__interrupt void " & sFuncName & "(void)"
        Case Else
            IsrDeclaration = "ERROR"
    End Select
End Function

'*****************************************************************************
'Description:
'       Calculation of an interrupt number (used for ISR declarations)
'-----------------------------------------------------------------------------
'Parameters:
'       iIntVectorAddress   interrupt vector address, e.g. &H0B
'-----------------------------------------------------------------------------
'Returns:
'       interrupt number    e.g. 1 for address &H0B, 2 for address &H13 etc.
'*****************************************************************************
Function IntNumber(r As ProjectData, iIntVectorAddress As Integer) As Variant
  If (r.v("MAIN_COMPILER") = "Keil") Or (r.v("MAIN_COMPILER") = "SDCC") Then
    IntNumber = (iIntVectorAddress - 3) \ 8
  ElseIf (r.v("MAIN_COMPILER") = "IAR") Then
    IntNumber = FormatTextOutput(iIntVectorAddress, "H4")
  End If

End Function

'*****************************************************************************
'Description:
'       Display of an error message in case of a failed SFR/bit name check.
'       A #error statement is returned.
'-----------------------------------------------------------------------------
'Parameters:
'       sName       name of the SFR or SFR-bit, e.g. "SYSSSCON"
'-----------------------------------------------------------------------------
'Returns:
'       error statement     e.g. "#error ... SYSSSCON ..."
'*****************************************************************************
Private Function RegErrMsg(sName As String) As String
    On Error Resume Next
    Dim s As String

    s = "Internal Error: Code generation for SFR/Bit '" & sName & "' not possible"
    Call ErrorBox(s)
    RegErrMsg = vbCrLf & "#error " & s & vbCrLf
End Function

'*****************************************************************************
'Description:
'        This function converts a baud rate in [baud] to an other unit
'
'-----------------------------------------------------------------------------
'Parameters:
'        dValue           the baudrate in [baud]
'-----------------------------------------------------------------------------
'Returns:
'        .dValue          the new value for the baud rate
'        .sUnit           the new unit of the baud rate
'        .bDivider        the new divider of the new baud rate, referring to [baud]
'*****************************************************************************
Function ConvertBaudrate(dValue As Double) As BaudrateType
  On Error GoTo ConvertBaudrateError
  Dim dTempValue As Double
  Dim sUnit As String
  Dim dDivider As Double

  dTempValue = dValue
  sUnit = "baud"
  dDivider = 1

  If (dTempValue >= 1000) Then
    dTempValue = (dTempValue / 1000)
    sUnit = "kbaud"
    dDivider = dDivider * 1000

   ' This code is intentionally commented out.
   ' If (dTempValue >= 1000) Then
   '   dTempValue = (dTempValue / 1000)
   '   sUnit = "Mbaud"
   '   dDivider = dDivider * 1000
   ' End If
  
  End If

  ConvertBaudrate.dValue = dTempValue
  ConvertBaudrate.sUnit = sUnit
  ConvertBaudrate.dDivider = dDivider

  Exit Function

ConvertBaudrateError:
  ConvertBaudrate.dValue = dValue
  ConvertBaudrate.sUnit = "baud"
  ConvertBaudrate.dDivider = 1
End Function

'*****************************************************************************
'Description:
'        This function checks the paramter v if its value is lower as the parameter
'        MinValue or greater as the parameter MaxValue. If so then a massage
'        box is opened.
'-----------------------------------------------------------------------------
'Parameters:
'        v               value which should be checked
'        MinValue        minimum value for the parameter v
'        MaxValue        maximum value for the parameter v
'-----------------------------------------------------------------------------
'Returns:
'        True            the value v is in range
'        False           else
'*****************************************************************************
Function IsInputValueOK(v As Variant, MinValue As Double, MaxValue As Double) As Boolean
  On Error Resume Next

  IsInputValueOK = False

  If (v < MinValue) Then  ' if the value is to low
    MsgBox "The input value must be greater.", vbCritical, "DAvE"
    Exit Function
  Else
    If (v > MaxValue) Then   ' if the value is to high
      MsgBox "The input value must be lower.", vbCritical, "DAvE"
      Exit Function
    Else
      IsInputValueOK = True  ' the value is OK
    End If
  End If

End Function

'*****************************************************************************
'Description:
'        This function generates the declaration for the interrupt service
'        routine in the generated code.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        ISRName          the name of the service request routine (e.g SSC0_viTx)
'        VectorName       the name of the service request routine vector (e.g TX_INT)
'-----------------------------------------------------------------------------
'Returns:
'        Service request declaration
'*****************************************************************************
Function GenerateIsrDeclaration(r As ProjectData, ISRName As String, VectorName As Variant) As String
    On Error Resume Next
    
    If (InStr(r.v("MAIN_COMPILER"), "SDCC")) Then
        GenerateIsrDeclaration = "void " & ISRName & "(void) interrupt " & VectorName
    End If

    If (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
        GenerateIsrDeclaration = "void " & ISRName & "(void) interrupt " & VectorName
    End If

    If (InStr(r.v("MAIN_COMPILER"), "IAR")) Then
        GenerateIsrDeclaration = "#pragma vector = " & VectorName & vbCrLf & _
                                 "__interrupt void " & ISRName & "(void)"

    End If

End Function

'*****************************************************************************
'Description:
'        This function returns the NDIV value selected.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'-----------------------------------------------------------------------------
'Returns:
'        NDIV value
'*****************************************************************************
Function GetValueNDIV(r As ProjectData) As Integer
    On Error Resume Next
    
    Dim count As Integer
    
    GetValueNDIV = 2
    GetValueNDIV = r.v(SCU_PREFIX & "PLL_CON1_NDIV") * 64 + r.v(SCU_PREFIX & "PLL_CON_NDIV") + 2
End Function
'*****************************************************************************
'Description:
'        This function returns the PDIV value selected.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'-----------------------------------------------------------------------------
'Returns:
'        PDIV value
'*****************************************************************************
Function GetValuePDIV(r As ProjectData) As Integer
    On Error Resume Next
    Dim count As Integer
    
    GetValuePDIV = 2
    
    GetValuePDIV = r.v(SCU_PREFIX & "PLL_CON1_PDIV") + 2
End Function
'*****************************************************************************
'Description:
'        This function returns the KDIV value selected.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'-----------------------------------------------------------------------------
'Returns:
'        KDIV value
'*****************************************************************************
Function GetValueKDIV(r As ProjectData) As Integer
    On Error Resume Next
    
    Dim count As Integer
    
    GetValueKDIV = 1
    
    Select Case r.v(SCU_PREFIX & "CMCON_KDIV")
            Case 0
                GetValueKDIV = 1
            Case 1
                GetValueKDIV = 2
            Case 2
                GetValueKDIV = 2
            Case 3
                GetValueKDIV = 4
           
    End Select
    
End Function
'****************************************************************************************************
'Description:
'        This function returns the boolean value depending on the boundary condition check for PDIV.
'
'----------------------------------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        Index            reference to the combo index value for KDIV
'----------------------------------------------------------------------------------------------------
'Returns:
'        True/False
'****************************************************************************************************
Function GetValuePDIVLIMIT(r As ProjectData, Index As Integer) As Boolean
    On Error Resume Next
    
    GetValuePDIVLIMIT = False
    
    If (((r.v("MAIN_FOSC") / (2 * Index)) > 0.8) And ((r.v("MAIN_FOSC") / (2 * Index)) < 8)) Then
       GetValuePDIVLIMIT = True
    End If
    
End Function
'****************************************************************************************************
'Description:
'        This function returns the boolean value depending on the boundary condition check for NDIV.
'
'----------------------------------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        Index            reference to the combo index value for NDIV / PDIV value
'----------------------------------------------------------------------------------------------------
'Returns:
'        True/False
'****************************************************************************************************
Function GetValueNDIVLIMIT(r As ProjectData, Index As Variant) As Boolean
    On Error Resume Next
    
    GetValueNDIVLIMIT = False
     
    If ((r.v("MAIN_FOSC") * Index) > 100 And (r.v("MAIN_FOSC") * Index) < 175) Then
       GetValueNDIVLIMIT = True
    End If
    
End Function

'*****************************************************************************
'Description:
'        This function returns the CLKREL value selected.
'
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'-----------------------------------------------------------------------------
'Returns:
'        CLKREL value
'*****************************************************************************
Function GetValueCLKREL(r As ProjectData) As Integer
    On Error Resume Next
    
    GetValueCLKREL = 3 * 2
    
    Select Case r.v(SCU_PREFIX & "CMCON_CLKREL")
        Case 0
            GetValueCLKREL = 3 * 2
        Case 1
            GetValueCLKREL = 6 * 2
        Case 2
            GetValueCLKREL = 9 * 2
        Case 3
            GetValueCLKREL = 12 * 2
        Case 4
            GetValueCLKREL = 18 * 2
        Case 5
            GetValueCLKREL = 24 * 2
        Case 6
            GetValueCLKREL = 36 * 2
        Case 7
            GetValueCLKREL = 48 * 2
        Case 8
            GetValueCLKREL = 72 * 2
        Case 9
            GetValueCLKREL = 96 * 2
        Case 10
            GetValueCLKREL = 144 * 2
        Case 11
            GetValueCLKREL = 192 * 2
        Case 12
            GetValueCLKREL = 288 * 2
        Case 13
            GetValueCLKREL = 384 * 2
        Case 14
            GetValueCLKREL = 576 * 2
    End Select
    
End Function


'*****************************************************************************
'Description:
'        This function makes all activities to enable an interrupt.
'        It sets the service request enable bit and selectes the service
'        request routine.
'        Example:
'        Call EnableSRN(r, "SSC0", "SSC0_SRCT", "Tx")
'
'        For this example the register SSC0_SRCT and the variable
'        SSC0_USE_Tx must exist.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        VarPre           variable prefix (e.g. SSC0)
'        SRCRegName       the name of the service request control register
'                         (e.g. SSC0_SRCT)
'        ISRName          the name of the service request routine (e.g Tx)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub EnableSRN(r As ProjectData, VarPre As String, SRCRegName As String, ISRName As String)
On Error Resume Next
'  r.v(SRCRegName & "_SRE") = 1
  r.v(VarPre & "_USE_" & ISRName) = 1
'  r.v(SRCRegName & "_TOS") = 0
'  r.v(SRCRegName & "_SRPN") = 0
End Sub

'*****************************************************************************
'Description:
'        This function makes all activities to disable an interrupt.
'        It resets the service request enable bit, set the priority to zero
'        and deselectes the service request routine.
'        Example:
'        Call DisableSRN(r, "SSC0", "SRCT", "Tx")
'
'        For this example the register SSC0_SRCT and the variable
'        SSC0_USE_Tx must exist.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the ProjectData object
'        VarPre           variable prefix (e.g. SSC0)
'        SRCRegName       the name of the service request control register
'                         (e.g. SSC0_SRCT)
'        ISRName          the name of the service request routine (e.g Tx)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub DisableSRN(r As ProjectData, VarPre As String, SRCRegName As String, ISRName As String)
On Error Resume Next
'  r.v(SRCRegName & "_SRE") = 0
  r.v(VarPre & "_USE_" & ISRName) = 0
'  r.v(SRCRegName & "_TOS") = 0
'  r.v(SRCRegName & "_SRPN") = 0
End Sub

'*****************************************************************************
'Description:
'        This function returns the current function of the selected pin
'
'        0 : no function
'        1 : pin is used as input
'        2 : pin is used as output
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sPinName              the name of the pin (specified with function
'                              'AddPin' or 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        The number of the pin function.
'        0 : no function
'        1 : pin is used as input
'        2 : pin is used as output
'*****************************************************************************
Function GetPinFunction(r As ProjectData, sPinName As String) As Integer
  If (r.exist("MuxFor_" & sPinName) = True) Then
      GetPinFunction = r.v("MuxFor_" & sPinName)
  Else
      GetPinFunction = 0
  End If
End Function
'*****************************************************************************
'Description:
'             This functoin returns a module clock in MHz.
'-----------------------------------------------------------------------------
'Parameters:
'       r   As Projectdata
'       sRegPre   Register Prefix
'       sVarPre   Module Prefix
'
'-----------------------------------------------------------------------------
'Returns:
'        module clock [MHz]
'*****************************************************************************
'Function Glo_GetModuleClock(r As ProjectData, sRegPre As String, sVarPre As String) As Double
'    On Error Resume Next
'    Glo_GetModuleClock = 1
    
'    If (r.v(sRegPre & "FDR_DM") < 2) Then
'        'if normal mode
'        Glo_GetModuleClock = GetSystemClock(r) * 1 / (1024 - r.l(sRegPre & "FDR_STEP"))
'    Else
'        'if fractional mode
'        Glo_GetModuleClock = GetSystemClock(r) * r.l(sRegPre & "FDR_STEP") / 1024
'    End If

'End Function


'*****************************************************************************
'Description:
'        This function checks if the module clock is disabled.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            the module clock is disabled
'        False           else
'*****************************************************************************
'Function Glo_IsModuleClockDisabled(r As ProjectData, sRegPre As String, sVarPre As String) As Boolean
 ' On Error Resume Next

'  Glo_IsModuleClockDisabled = False
 ' Glo_IsModuleClockDisabled = (Glo_IsModuleDisabled(r, sRegPre, sVarPre) = True) Or (r.v(sRegPre & "FDR_DM") = 0)

'End Function


'*****************************************************************************
'Description:
'        This function checks if the module is disabled.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            the module is disabled
'        False           else
'*****************************************************************************
'Function Glo_IsModuleDisabled(r As ProjectData, sRegPre As String, sVarPre As String) As Boolean
'  On Error Resume Next
  
'  Glo_IsModuleDisabled = False
'  Glo_IsModuleDisabled = (r.v(sRegPre & "CLC_DISR") = 1)

''End Function


'*****************************************************************************

'*****************************************************************************
'Description:
'        This function returns a formated disabled tooltip if the module
'        clock is disabled.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        Formated tooltip
'*****************************************************************************
''Function Glo_GetTooltipModuleClockDisabled(r As ProjectData, sRegPre As String, sVarPre As String) As String
''  On Error Resume Next
''
''  Glo_GetTooltipModuleClockDisabled = ""
''
''  Glo_GetTooltipModuleClockDisabled = Glo_GetTooltipModuleClockDisabled & Glo_GetTooltipModuleDisabled(r, sRegPre, sVarPre)
''
''  If (Glo_IsModuleClockDisabled(r, sRegPre, sVarPre) = True) Then
''    Glo_GetTooltipModuleClockDisabled = Glo_GetTooltipModuleClockDisabled & FormatDisabledToolTip("the " & sVarPre & " module clock is disabled")
''  End If
''
''End Function

'*****************************************************************************
'Description:
'        This function returns a formated disabled tooltip if the module
'        is disabled.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        Formated tooltip
'*****************************************************************************
''Function Glo_GetTooltipModuleDisabled(r As ProjectData, sRegPre As String, sVarPre As String) As String
''  On Error Resume Next
''
''  Glo_GetTooltipModuleDisabled = ""
''  If (Glo_IsModuleDisabled(r, sRegPre, sVarPre) = True) Then
''    Glo_GetTooltipModuleDisabled = Glo_GetTooltipModuleDisabled & FormatDisabledToolTip("the " & sVarPre & " module is disabled")
''  End If

''End Function

'*****************************************************************************
'Description:
'             This functoin updates a module clock.
'-----------------------------------------------------------------------------
'Parameters:
'       r   As Projectdata
'       sRegPre   Register Prefix
'       sVarPre   Module Prefix
'
'-----------------------------------------------------------------------------
'Returns:
'        none
'*****************************************************************************
'Sub Glo_UpdateModuleClock(r As ProjectData, sRegPre As String, sVarPre As String)
'    On Error Resume Next
'    Dim d As Double
'    Dim l As Long
'    l = CONVERT2HZ
    
'    If (r.d(sVarPre & "_RequiredClock") * l > Glo_GetMaxModuleClock(r, sRegPre, sVarPre) * l) Then
'        r.d(sVarPre & "_RequiredClock") = Glo_GetMaxModuleClock(r, sRegPre, sVarPre)
'    ElseIf (r.d(sVarPre & "_RequiredClock") * l < Glo_GetMinModuleClock(r, sRegPre, sVarPre) * l) Then
'        r.d(sVarPre & "_RequiredClock") = Glo_GetMinModuleClock(r, sRegPre, sVarPre)
'    End If

'    ' STE 07.11.02
'    If (r.v(sRegPre & "FDR_DM") = 0) Then
'        r.d(sVarPre & "_RequiredClock") = Glo_GetMaxModuleClock(r, sRegPre, sVarPre)
'        d = 1023

    'ElseIf (r.v(sRegPre & "FDR_DM") < 2) Then
        'if normal mode
'        d = 1024 - GetSystemClock(r) / r.d(sVarPre & "_RequiredClock")
'        If (d > 1023) Then
'            d = 1023
'        End If
'    Else
'        'if fractional mode
'        d = r.d(sVarPre & "_RequiredClock") / GetSystemClock(r) * 1024
'        If (d > 1023) Then
'            d = 1023
'        ElseIf (d = 0) Then
'            d = 0
'        End If
'    End If

'    r.l(sRegPre & "FDR_STEP") = d


'End Sub

'*****************************************************************************

'*****************************************************************************
'Description:
'             This functoin returns a minimal module clock in MHz.
'-----------------------------------------------------------------------------
'Parameters:
'       r         As Projectdata
'       sRegPre   Register Prefix
'       sVarPre   Module Prefix
'
'-----------------------------------------------------------------------------
'Returns:
'        minimal module clock [MHz]
'*****************************************************************************
Function Glo_GetMinModuleClock(r As ProjectData, sRegPre As String, sVarPre As String) As Double
    On Error Resume Next
    Glo_GetMinModuleClock = 1
    
    Glo_GetMinModuleClock = 1 / 1024 * GetSystemClock(r)

End Function

'*****************************************************************************
'Description:
'             This functoin returns a maximal module clock in MHz.
'-----------------------------------------------------------------------------
'Parameters:
'       r   As Projectdata
'       sRegPre   Register Prefix
'       sVarPre   Module Prefix
'
'-----------------------------------------------------------------------------
'Returns:
'        maximal module clock [MHz]
'*****************************************************************************
'Function Glo_GetMaxModuleClock(r As ProjectData, sRegPre As String, sVarPre As String) As Double
'    On Error Resume Next
'    Glo_GetMaxModuleClock = 1
    
'    If (r.v(sRegPre & "FDR_DM") < 2) Then
'        'if normal mode
'        Glo_GetMaxModuleClock = GetSystemClock(r)
'    Else
'        'if fractional mode
'        Glo_GetMaxModuleClock = 1023 / 1024 * GetSystemClock(r)
'    End If

'End Function

'*****************************************************************************

'**********************************************************************************
'Description:
'        This function prints the description for interrupt type selection options.
'----------------------------------------------------------------------------------
'Parameters:
'           Choice         type of selection 1 or 2
'----------------------------------------------------------------------------------
'Returns:
'           None
'**********************************************************************************
Public Function GetDescriptionForISR(r As ProjectData, f As Variant)
    On Error Resume Next
     
     Print #f, FormatComments(0, 0, "", "", "You have two choices for interrupt type select in Project Settings Page under Global Settings Section.")
     Print #f, FormatComments(0, 0, "", "", "If you select CHOICE 1 then ISR will be generated with push and pop.")
     Print #f, FormatComments(0, 0, "", "", "If you select CHOICE 2 then ISR will be generated without push and pop.")
     Print #f, FormatComments(0, 0, "", "", "Default choice is CHOICE 2.")
     
     If (r.v("ISR_CHOICE") = 1) Then
        Print #f, FormatComments(0, 0, "", "", "Current selection is CHOICE 1.")
     ElseIf (r.v("ISR_CHOICE") = 2) Then
        Print #f, FormatComments(0, 0, "", "", "Current selection is CHOICE 2 ")
     End If
     Print #f, ""
End Function

'*****************************************************************************
'Description:
'        This function checks the cpu type and returns true if CAN module is present
'        else 0 if CAN module is not present
'
'-----------------------------------------------------------------------------
'Parameters:
'        r               Project Data
'-----------------------------------------------------------------------------
'Returns:
'        True            the CAN module is present
'        False           else
'*****************************************************************************
Function IsCpuTypeOK(r As ProjectData) As Boolean
  On Error Resume Next

  IsCpuTypeOK = False
  
  If ((r.v("MAIN_CPU_TYPE") = "XC878CM") Or (r.v("MAIN_CPU_TYPE") = "XC878CM-13FF") Or (r.v("MAIN_CPU_TYPE") = "XC878CM-16FF")) Then
    
     IsCpuTypeOK = True
  Else
  
    IsCpuTypeOK = False
  End If

End Function

Function GetValueCOREL(r As ProjectData) As Integer
    On Error Resume Next
    GetValueCOREL = 3
    Select Case r.v(SCU_PREFIX & "COCON_COREL")
        Case 0
            GetValueCOREL = 3
        Case 1
            GetValueCOREL = 6
        Case 2
            GetValueCOREL = 9
        Case 3
            GetValueCOREL = 12
        Case 4
            GetValueCOREL = 15
        Case 5
            GetValueCOREL = 18
        Case 6
            GetValueCOREL = 21
        Case 7
            GetValueCOREL = 24
        Case 8
            GetValueCOREL = 30
        Case 9
            GetValueCOREL = 36
        Case 10
            GetValueCOREL = 39
        Case 11
            GetValueCOREL = 42
        Case 12
            GetValueCOREL = 48
        Case 13
            GetValueCOREL = 54
        Case 14
            GetValueCOREL = 60
   End Select
End Function
