Attribute VB_Name = "Module1"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\classes\FunctionClass.cls
'#Uses "Include.inc"
Option Explicit
'*****************************************************************************
' Module:
'       WDT
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions for generating code for the
'        module 'WDT'.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        0.1  Radhish        27-10-04    initial version
'        0.2  Radhish        19-11-04    WDTRS bit in WDT.C is removed as
'                                         it is included in WDT.h file
'        0.3  radhish       08-12-04    code for bit protection scheme included
'        0.4  radhish       27-12-04    Module name appended before register names
'        0.5  radhish       9-01-05    Module name appended before register NMICON
'        0.6   "            7.02.05     NMICON register commented
'
'        0.7  Ashish     05.12.06   Code for WDT_vEnable and WDT_vDisable function


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


'*****************************************************************************
'Description:
'        This functions initiates the code generation for the WDT module.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function StartGenerateCode(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo StartGenerateCodeError
    StartGenerateCode = False

    ' write files WDT.h
    If (r.v("WDT_USE_Init")) Then
        If (Not WDTGenerateH(r, vIndex)) Then Exit Function
                   If (Not r.AddFile(r.s("WDT_HFILE"), "H")) Then Exit Function
    End If

    ' write file WDT.c
    If (r.v("WDT_USE_Init")) Then
        If (Not WDTGenerateC(r, vIndex)) Then Exit Function
                   If (Not r.AddFile(r.s("WDT_CFILE"), "C")) Then Exit Function
    End If

        
    StartGenerateCode = True
    Exit Function
    
StartGenerateCodeError:
    Errorbox("Code generation for WDT failed", Error$)
    Exit Function
End Function


'*****************************************************************************
'Description:
'        Generates the code for the WDT.c file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function WDTGenerateC(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo WDTGenerateCError
    
    Dim i As Integer
    Dim f As Variant
    Dim v As Variant
    Dim d As String
    Dim dc As DAvECollection
    Dim uc As UserCode
    Dim func As FunctionClass
    
    WDTGenerateC = False

    Set dc = ObjectCollection("MODULENAMES")
    Set uc = New UserCode
    Set func = New FunctionClass

    'refresh the decriptions of the functions and macros
    Call RefreshFunctionDescriptions(func, r)

    'read usercode
    uc.ReadFile ((StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v("WDT_CFILE"))

    'open WDT.c
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v("WDT_CFILE") For Output As f
    
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = FormatModuleDescription(GetModuleHeaderC("WDT"))
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc("WDT").ModuleDescription, r.v("WDT_CFILE"), r.v("MAIN_CPU_TYPE"), r.v("MAIN_COMPILER"), d)
          
            Print #f, uc.GetUserCode("(WDT_General,1)", 0)   'USER CODE for application specific descriptions'
            Print #f, ""
            Print #f, Rtf.GenerateNote((r.v("WDT_Notes")))
            Print #f, ""
            Print #f, SECTION_PROJECT_INCLUDES
            Print #f, ""  'DAvE includes'
            Print #f, "#include """ & r.v("MAIN_HFile") & """"
            Print #f, ""
            Print #f, uc.GetUserCode("(WDT_General,2)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_MACROS
            Print #f, ""  'DAvE local macro definitions'
            Print #f, uc.GetUserCode("(WDT_General,3)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_DEFINES
            Print #f, ""  'DAvE local #define definitions'
            Print #f, uc.GetUserCode("(WDT_General,4)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_TYPEDEFS
            Print #f, ""  'DAvE type defintions'
            Print #f, uc.GetUserCode("(WDT_General,5)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_IMPORTED_GLOBAL_VARIABLES
            Print #f, ""  'DAvE external declarations of imported global variables'
            Print #f, uc.GetUserCode("(WDT_General,6)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_GLOBAL_VARIABLES
            Print #f, ""  'DAvE definitions of global variables'
            Print #f, uc.GetUserCode("(WDT_General,7)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_EXTERNAL_PROTOTYPES
            Print #f, ""  'DAvE external declarations of imported functions'
            Print #f, uc.GetUserCode("(WDT_General,8)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_LOCAL_PROTOTYPES
            Print #f, ""  'DAvE declarations of internal local functions'
            Print #f, uc.GetUserCode("(WDT_General,9)", 0)
            Print #f, ""
            Print #f, ""
    '------------------------------------------------------------------------------------------------
    ' wdt_Init function when user enable WDT in INIT
    '------------------------------------------------------------------------------------------------
    If (r.v("WDT_USE_Init") = 1 And r.v("GEN_UserDef1") = 0) Then  'WDT enable in INIT
    Print #f, FormatFunctionHeader(r, func, "WDT_NAME_Init")
            Print #f, uc.GetUserCode("(WDT_Init,1)", 0)     'USER CODE for application specific descriptions'
            Print #f, ""
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "WDT_NAME_Init")
            Print #f, "{"
            Print #f, uc.GetUserCode("(WDT_Init,2)", 2)
            Print #f, ""
      If (r.v("WDT_WDTUsed") = 0) Then
            Print #f, FormatComments(2, 1, "", "", "the watchdog timer is not used and will be disabled")
            Print #f, ""
            Print #f, "  " & SfrBitStatement(r, SCU_PREFIX & "SYSCON0_RMAP", 1)
            Print #f, "  " & SfrBitStatement(r, WDT_PREFIX & "WDTCON_WDTEN", 0)
            Print #f, "  " & SfrBitStatement(r, SCU_PREFIX & "SYSCON0_RMAP", 0)

      Else
            Print #f, FormatComments(2, 1, "", "", "the watchdog timer is used and it is enabled in INIT function,user has to take care of WDT refresh")
            Print #f, FormatComments(2, 1, "", "", "the RMAP bit of SYSCON0 is set")
            
        If (r.v(WDT_PREFIX & "WDTCON_WDTIN") = 0) Then
            Print #f, FormatComments(2, 1, "", "", "the input frequency is fSYS/2")
        Else
            Print #f, FormatComments(2, 1, "", "", "the input frequency is fSYS/128")
        End If

            Print #f, FormatComments(2, 1, "", "", "the watchdog timer reload value is " & FormatTextOutput(r.d(WDT_PREFIX & "WDTREL_WDTREL"), "H2"))
            Print #f, FormatComments(2, 1, "", "", "the watchdog timeout period is " & Format(r.d("WDT_WDTPeriod") & " ms"))
             Print #f, ""
            If (r.v(WDT_PREFIX & "WDTCON_WINBEN") = 1) Then
                 Print #f, FormatComments(2, 1, "", "", "the window boundary is enabled")
            End If
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "1, noSST);", "", "", "switch to page1 without saving")
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "MAIN_vUnlockProtecReg();", "", "", "open access to protected register")
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, noSST);", "", "", "switch to page0 with out saving")
            Print #f, ""
            Print #f, "  " & SfrBitStatement(r, SCU_PREFIX & "SYSCON0_RMAP", 1)
          Print #f, "  " & SfrName(r, WDT_PREFIX & "WDTREL") & " = " & FormatTextOutput(r.sfr(WDT_PREFIX & "WDTREL"), "H2") & ";" & "      //Watch dog timer reload register"
            Print #f, "  " & SfrBitStatement(r, WDT_PREFIX & "WDTCON_WDTEN", 1)
            
            If ((r.v("WDTCON_WDTIN") = 0) And (r.v(WDT_PREFIX & "WDTCON_WINBEN") = 0)) Then
                Print #f, "  " & SfrBitStatement(r, WDT_PREFIX & "WDTCON_WDTRS", 1)
            ElseIf ((r.v("WDTCON_WDTIN") = 0) And (r.v(WDT_PREFIX & "WDTCON_WINBEN") = 1)) Then
                Print #f, FormatCodeStatemente(2, WDT_PREFIX & "WDTCON |= 0x22;", "", "", "set WDTBEN,set WDTRS")
                Print #f, "  " & SfrName(r, WDT_PREFIX & "WDTWINB") & " = " & FormatTextOutput(r.sfr(WDT_PREFIX & "WDTWINB"), "H2") & "; " & "      //Watch dog timer window boundary register"
            
            ElseIf ((r.v("WDTCON_WDTIN") = 1) And (r.v(WDT_PREFIX & "WDTCON_WINBEN") = 0)) Then
                Print #f, FormatCodeStatemente(2, WDT_PREFIX & "WDTCON |= 0x03;", "", "", "set WDTRS,set WDTIN")
            ElseIf ((r.v("WDTCON_WDTIN") = 1) And (r.v(WDT_PREFIX & "WDTCON_WINBEN") = 1)) Then
                Print #f, FormatCodeStatemente(2, WDT_PREFIX & "WDTCON |= 0x23;", "", "", "set WDTRS,set WDTIN,set WINBEN")
                Print #f, "  " & SfrName(r, WDT_PREFIX & "WDTWINB") & " = " & FormatTextOutput(r.sfr(WDT_PREFIX & "WDTWINB"), "H2") & "; " & "      //Window boundary register reload value"
            
            End If
            

            Print #f, "  " & SfrBitStatement(r, SCU_PREFIX & "SYSCON0_RMAP", 0)
           
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "1, noSST);", "", "", "switch to page1 without saving")
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "MAIN_vlockProtecReg();", "", "", "close access to protected register")
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, noSST);", "", "", "switch to page0 with out saving")
            Print #f, ""
'            Print #f, FormatCodeStatemente(2, "SCU_NMICON", "=", FormatTextOutput("1", "H2"), "NMICON_NMIWDT is enabled ")

 '           Print #f, "  " & SfrBitStatement(r, SCU_PREFIX & "SYSCON0_RMAP", 1)
            
'            Print #f, "  " & SfrName(r, WDT_PREFIX & "WDTCON") & " = " & FormatTextOutput(r.sfr(WDT_PREFIX & "WDTCON"), "H2") & ";"

            
      
            Print #f, uc.GetUserCode("(WDT_Init,3)", 2)
            Print #f, ""
            Print #f, ""

      End If
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("WDT_NAME_Init")
            Print #f, ""
            Print #f, ""
    End If
    '------------------------------------------------------------------------------------------------
    ' wdt_Init function when user Disable WDT in INIT
    '------------------------------------------------------------------------------------------------
  
    If (r.v("WDT_USE_Init") = 1 And r.v("GEN_UserDef1") = 1) Then 'WDT disable in INIT
    Print #f, FormatFunctionHeader(r, func, "WDT_NAME_Init")
            Print #f, uc.GetUserCode("(WDT_Init,1)", 0)     'USER CODE for application specific descriptions'
            Print #f, ""
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "WDT_NAME_Init")
            Print #f, "{"
            Print #f, uc.GetUserCode("(WDT_Init,2)", 2)
            Print #f, ""
      If (r.v("WDT_WDTUsed") = 0) Then
            Print #f, FormatComments(2, 1, "", "", "the watchdog timer is not used and will be disabled INIT")
            Print #f, ""
            Print #f, "  " & SfrBitStatement(r, SCU_PREFIX & "SYSCON0_RMAP", 1)
            Print #f, "  " & SfrBitStatement(r, WDT_PREFIX & "WDTCON_WDTEN", 0)
            Print #f, "  " & SfrBitStatement(r, SCU_PREFIX & "SYSCON0_RMAP", 0)

      Else
            Print #f, FormatComments(2, 1, "", "", "the watchdog timer is not enabled in INIT,user has to take care of WDT enable and WDT refresh")
            Print #f, FormatComments(2, 1, "", "", "the RMAP bit of SYSCON0 is set")
            
        If (r.v(WDT_PREFIX & "WDTCON_WDTIN") = 0) Then
            Print #f, FormatComments(2, 1, "", "", "the input frequency is fSYS/2")
        Else
            Print #f, FormatComments(2, 1, "", "", "the input frequency is fSYS/128")
        End If

            Print #f, FormatComments(2, 1, "", "", "the watchdog timer reload value is " & FormatTextOutput(r.d(WDT_PREFIX & "WDTREL_WDTREL"), "H2"))
            Print #f, FormatComments(2, 1, "", "", "the watchdog timeout period is " & Format(r.d("WDT_WDTPeriod") & " ms"))
             Print #f, ""
            If (r.v(WDT_PREFIX & "WDTCON_WINBEN") = 1) Then
                 Print #f, FormatComments(2, 1, "", "", "the window boundary is enabled")
            End If
           '' Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "1, noSST);", "", "", "switch to page1 without saving")
           '' Print #f, ""
            ''Print #f, FormatCodeStatemente(2, "MAIN_vUnlockProtecReg();", "", "", "open access to protected register")
            ''Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, noSST);", "", "", "switch to page0 with out saving")
            Print #f, ""
            Print #f, "  " & SfrBitStatement(r, SCU_PREFIX & "SYSCON0_RMAP", 1)
           '' Print #f, "  " & SfrBitStatement(r, WDT_PREFIX & "WDTCON_WDTEN", 1)'WDT is disabled
            Print #f, "  " & SfrName(r, WDT_PREFIX & "WDTREL") & " = " & FormatTextOutput(r.sfr(WDT_PREFIX & "WDTREL"), "H2") & ";" & "      //Watch dog timer reload register"
            
            If ((r.v("WDTCON_WDTIN") = 0) And (r.v(WDT_PREFIX & "WDTCON_WINBEN") = 0)) Then
              ''  Print #f, "  " & SfrBitStatement(r, WDT_PREFIX & "WDTCON_WDTRS", 1) 'no refresh required..user will take care himself of refresh
            ElseIf ((r.v("WDTCON_WDTIN") = 0) And (r.v(WDT_PREFIX & "WDTCON_WINBEN") = 1)) Then
              ''  Print #f, FormatCodeStatemente(2, WDT_PREFIX & "WDTCON |= 0x22;", "", "", "set WDTBEN,set WDTRS")
               Print #f, FormatCodeStatemente(2, WDT_PREFIX & "WDTCON |= 0x20;", "", "", "set WDTBEN,")
                Print #f, "  " & SfrName(r, WDT_PREFIX & "WDTWINB") & " = " & FormatTextOutput(r.sfr(WDT_PREFIX & "WDTWINB"), "H2") & "; " & "      //Watch dog timer window boundary register"
            
            ElseIf ((r.v("WDTCON_WDTIN") = 1) And (r.v(WDT_PREFIX & "WDTCON_WINBEN") = 0)) Then
                ''Print #f, FormatCodeStatemente(2, WDT_PREFIX & "WDTCON |= 0x03;", "", "", "set WDTRS,set WDTIN")
                Print #f, FormatCodeStatemente(2, WDT_PREFIX & "WDTCON |= 0x01;", "", "", "set WDTIN")
            ElseIf ((r.v("WDTCON_WDTIN") = 1) And (r.v(WDT_PREFIX & "WDTCON_WINBEN") = 1)) Then
                ''Print #f, FormatCodeStatemente(2, WDT_PREFIX & "WDTCON |= 0x23;", "", "", "set WDTRS,set WDTIN,set WINBIN")
                Print #f, FormatCodeStatemente(2, WDT_PREFIX & "WDTCON |= 0x21;", "", "", "set WDTIN,set WINBIN")
                Print #f, "  " & SfrName(r, WDT_PREFIX & "WDTWINB") & " = " & FormatTextOutput(r.sfr(WDT_PREFIX & "WDTWINB"), "H2") & "; " & "      //Window boundary register reload value"
            
            End If
            

            Print #f, "  " & SfrBitStatement(r, SCU_PREFIX & "SYSCON0_RMAP", 0)
           
           '' Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "1, noSST);", "", "", "switch to page1 without saving")
           '' Print #f, ""
            ''Print #f, FormatCodeStatemente(2, "MAIN_vlockProtecReg();", "", "", "close access to protected register")
          ''  Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, noSST);", "", "", "switch to page0 with out saving")
            Print #f, ""
'            Print #f, FormatCodeStatemente(2, "SCU_NMICON", "=", FormatTextOutput("1", "H2"), "NMICON_NMIWDT is enabled ")

 '           Print #f, "  " & SfrBitStatement(r, SCU_PREFIX & "SYSCON0_RMAP", 1)
            
'            Print #f, "  " & SfrName(r, WDT_PREFIX & "WDTCON") & " = " & FormatTextOutput(r.sfr(WDT_PREFIX & "WDTCON"), "H2") & ";"

            
      
            Print #f, uc.GetUserCode("(WDT_Init,3)", 2)
            Print #f, ""
            Print #f, ""

      End If
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("WDT_NAME_Init")
            Print #f, ""
            Print #f, ""
    End If
    
    
    
    '------------------------------------------------------------------------------------------------
    ' WDT_vEnable function
    '------------------------------------------------------------------------------------------------
    If (r.v("WDT_PARA_CLR_BIT") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "WDT_WinEna")
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "WDT_WinEna")
            Print #f, "{"
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "1, noSST);", "", "", "switch to page1 without saving")
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "MAIN_vUnlockProtecReg();", "", "", "open access to protected register")
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, noSST);", "", "", "switch to page0 with out saving")
            Print #f, ""
            'Print #f, "  " & SfrBitStatement(r, SCU_PREFIX & "SYSCON0_RMAP", 1)
            Print #f, "  SET_RMAP();"
            Print #f, "  " & SfrBitStatement(r, WDT_PREFIX & "WDTCON_WDTEN", 1) 'Enable WDT
            'Print #f, "  " & SfrBitStatement(r, SCU_PREFIX & "SYSCON0_RMAP", 0)
            Print #f, "  RESET_RMAP();"
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "1, noSST);", "", "", "switch to page1 without saving")
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "MAIN_vlockProtecReg();", "", "", "close access to protected register")
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, noSST);", "", "", "switch to page0 with out saving")
            Print #f, "} " & END_OF_FUNCTION & r.v("WDT_WinEna")
            Print #f, ""
            Print #f, ""
    End If
    
    
    '------------------------------------------------------------------------------------------------
    ' WDT_vDisable function
    '------------------------------------------------------------------------------------------------
    If (r.v("WDT_PARA_NO_CLR_BIT") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "GEN_UserDef0")
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "GEN_UserDef0")
            Print #f, "{"
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "1, noSST);", "", "", "switch to page1 without saving")
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "MAIN_vUnlockProtecReg();", "", "", "open access to protected register")
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, noSST);", "", "", "switch to page0 with out saving")
            Print #f, ""
            'Print #f, "  " & SfrBitStatement(r, SCU_PREFIX & "SYSCON0_RMAP", 1)
            Print #f, "  SET_RMAP();"
            Print #f, "  " & SfrBitStatement(r, WDT_PREFIX & "WDTCON_WDTEN", 0) 'Disable WDT
            'Print #f, "  " & SfrBitStatement(r, SCU_PREFIX & "SYSCON0_RMAP", 0)
            Print #f, "  RESET_RMAP();"
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "1, noSST);", "", "", "switch to page1 without saving")
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "MAIN_vlockProtecReg();", "", "", "close access to protected register")
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, noSST);", "", "", "switch to page0 with out saving")
            Print #f, "} " & END_OF_FUNCTION & r.v("GEN_UserDef0")
            Print #f, ""
            Print #f, ""
    End If


    '------------------------------------------------------------------------------------------------
    ' WDT_Reset function
    '------------------------------------------------------------------------------------------------
    If (r.v("WDT_USE_Reset") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "WDT_NAME_Reset")
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "WDT_NAME_Reset")
            Print #f, "{"
            Print #f, "  bit bTmp;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "1, SST1);", "", "", "switch to page1")
            Print #f, "  bTmp = " & SfrBitTest(r, SCU_PREFIX & "PMCON0_WDTRST") & ";"
'            Print #f, "  if(ubClrWDTR == " & r.v("WDT_PARA_CLR_BIT") & ")"
'            Print #f, "  {"
'            Print #f, "    " & SfrBitStatement(r, "PMCON0_WDTRST", 0)
'            Print #f, "  }"
           Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "1, RST1);", "", "", "restore the old SCU page")

            Print #f, "  return(bTmp);"
            Print #f, "} " & END_OF_FUNCTION & r.v("WDT_NAME_Reset")
            Print #f, ""
            Print #f, ""
    End If

   
    '------------------------------------------------------------------------------------------------
            Print #f, uc.GetUserCode("(WDT_General,10)", 0)
            Print #f, ""
    
    'close file
    Close f
    
    WDTGenerateC = True
    Exit Function
    
WDTGenerateCError:
    Close f
    Exit Function
End Function


'*****************************************************************************
'Description:
'        Generates the code for the WDT.h file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function WDTGenerateH(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo WDTGenerateHError
    
    Dim i As Integer
    Dim f As Variant
    Dim d As String
    Dim dc As DAvECollection
    Dim uc As UserCode
    Dim func As FunctionClass
    Dim l As Long
    Dim sName As String
   
    WDTGenerateH = False

    Set dc = ObjectCollection("MODULENAMES")
    Set uc = New UserCode
    Set func = New FunctionClass

    'refresh the decriptions of the functions and macros
    Call RefreshFunctionDescriptions(func, r)
    
    'read usercode
    uc.ReadFile ((StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v("WDT_HFILE"))
    
    'open WDT.h
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v("WDT_HFILE") For Output As f
    
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = FormatModuleDescription(GetModuleHeaderH("WDT"))
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc("WDT").ModuleDescription, r.v("WDT_HFILE"), r.v("MAIN_CPU_TYPE"), r.v("MAIN_COMPILER"), d)
            
            Print #f, uc.GetUserCode("(WDT_Header,1)", 0)
            Print #f, ""
            Print #f, Rtf.GenerateNote((r.v("WDT_Notes")))
            Print #f, ""
            Print #f, "#ifndef _" & StripExtension(r.v("WDT_HFILE")) & "_H_"
            Print #f, "#define _" & StripExtension(r.v("WDT_HFILE")) & "_H_"
            Print #f, ""
            Print #f, SECTION_PROJECT_INCLUDES
            Print #f, ""  'DAvE includes'
            Print #f, uc.GetUserCode("(WDT_Header,2)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_MACROS
            Print #f, ""  'DAvE global macro definitions; not used as functions'
            Print #f, uc.GetUserCode("(WDT_Header,3)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_DEFINES
            Print #f, ""  'DAvE global #define definitions'
            Print #f, uc.GetUserCode("(WDT_Header,4)", 0)
            Print #f, ""

'    If (r.v("WDT_USE_Reset") = 1) Then
'            Print #f, "#define " & FillRight(15, r.v("WDT_PARA_CLR_BIT")) & " 0"
'            Print #f, "#define " & FillRight(15, r.v("WDT_PARA_NO_CLR_BIT")) & " 1"
'    End If

            Print #f, ""
            Print #f, SECTION_TYPEDEFS
            Print #f, ""  'DAvE type definitions'
            Print #f, uc.GetUserCode("(WDT_Header,5)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_IMPORTED_GLOBAL_VARIABLES
            Print #f, ""  'DAvE external declarations of imported global variables'
            Print #f, uc.GetUserCode("(WDT_Header,6)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_GLOBAL_VARIABLES
            Print #f, ""  'DAvE definition of global variables'
            Print #f, uc.GetUserCode("(WDT_Header,7)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_GLOBAL_PROTOTYPES
            Print #f, ""  'DAvE declarations of functions which should become global'

    ' for each function in this 'func' object
    For l = 1 To func.FunctionCount
        ' get the function name which was insert in 'Include.inc' with 'AddFunction()'
        d = func.FunctionName(l)

        ' get only the third part of the function name (e.g "Init")
        sName = Right(d, Len(d) - InStr(d, "NAME") - Len("NAME"))
        ' only if the use variable of the function is selected and the function is a really function
        ' (not a macro or a ISR)
''       If ((r.v("WDT_PARA_CLR_BIT") = 1) = 1 And (func.FunctionSort(d) = 1)) Then
''        Print #f, FormatFunctionDeclaration(r, func, d) & ";"
''            ElseIf ((r.v("WDT_PARA_NO_CLR_BIT") = 1) And (func.FunctionSort(d) = 1)) Then
''                 Print #f, FormatFunctionDeclaration(r, func, d) & ";"
''            ElseIf ((r.v("WDT_USE_" & sName) = 1) And (func.FunctionSort(d) = 1)) Then
''                Print #f, FormatFunctionDeclaration(r, func, d) & ";"
''    End If
     If (sName = "WinEna") Then
        If ((r.v("WDT_PARA_CLR_BIT") = 1) And (func.FunctionSort(d) = 1)) Then
        Print #f, FormatFunctionDeclaration(r, func, d) & ";"
       End If
     End If

 If (sName = "UserDef0") Then
   If ((r.v("WDT_PARA_NO_CLR_BIT") = 1) And (func.FunctionSort(d) = 1)) Then
    Print #f, FormatFunctionDeclaration(r, func, d) & ";"
  End If
 End If

 If (sName = "Init" Or sName = "Reset" Or sName = "Refresh" Or sName = "Windowb") Then
   If ((r.v("WDT_USE_" & sName) = 1) And (func.FunctionSort(d) = 1)) Then
    Print #f, FormatFunctionDeclaration(r, func, d) & ";"
   End If
 End If
    Next l
Print #f, ""
            Print #f, uc.GetUserCode("(WDT_Header,8)", 0)
            Print #f, ""
            Print #f, ""
                          'DAvE declarations of global macros'
    '------------------------------------------------------------------------------------------------
    ' WDT_Refresh macro
    '------------------------------------------------------------------------------------------------
    If (r.v("WDT_USE_Refresh") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "WDT_NAME_Refresh")
          
            'Print #f, "#define " & r.v("WDT_NAME_Refresh") & "() " & SfrBitStatement(r, WDT_PREFIX & "WDTCON_WDTRS", 1, False)
            Print #f, "#define " & r.v("WDT_NAME_Refresh") & "() " & " { " & SfrBitStatement(r, "IEN0_EA", 0) & " SET_RMAP(); " & SfrBitStatement(r, WDT_PREFIX & "WDTCON_WDTRS", 1, False) & "; \"
            Print #f, "  " & " RESET_RMAP(); " & SfrBitStatement(r, "IEN0_EA", 1) & " }"
            Print #f, ""
            Print #f, ""
    End If
    If (r.v("WDT_USE_Windowb") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "WDT_NAME_Windowb")
          
            'Print #f, "#define " & r.v("WDT_NAME_Windowb") & "() " & SfrBitStatement(r, WDT_PREFIX & "WDTCON_WINBEN", 1, False)
            Print #f, "#define " & r.v("WDT_NAME_Windowb") & "() " & " { SET_RMAP(); " & SfrBitStatement(r, WDT_PREFIX & "WDTCON_WINBEN", 1, False) & "; \"
            Print #f, "  " & " RESET_RMAP(); }"
            Print #f, ""
            Print #f, ""
    End If
            Print #f, SECTION_INTERRUPT_VECTORS
            Print #f, ""  'DAvE interrupt vector definitions
            Print #f, ""
            Print #f, uc.GetUserCode("(WDT_Header,9)", 0)
            Print #f, ""
            Print #f, "#endif  // ifndef _" & StripExtension(r.v("WDT_HFILE")) & "_H_"
    
    'close file
    Close f
    
    WDTGenerateH = True
    Exit Function
    
WDTGenerateHError:
    Close f
    Exit Function
End Function












