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:
'        PS
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions for generating code for the
'        module 'PS'.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        0.1  radhish       18-10-04    initial version
'        0.2  radhish       19-11-04    changes made in PS_setpowerdown function
'        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      10-01-05    code for CMCON register added

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


'*****************************************************************************
'Description:
'        This functions initiates the code generation for the PS 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 PS.h
    If (r.v("PS_USE_Init")) Then
        If (Not PSGenerateH(r, vIndex)) Then Exit Function
        If (Not r.AddFile(r.s("PS_HFILE"), "H")) Then Exit Function
    End If

'    ' write file PS.c
    If (r.v("PS_USE_Init")) Then
        If (Not PSGenerateC(r, vIndex)) Then Exit Function
         If (Not r.AddFile(r.s("PS_CFILE"), "C")) Then Exit Function
    End If

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


'*****************************************************************************
'Description:
'        Generates the code for the PS.c file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function PSGenerateC(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo PSGenerateCError
    
    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
    
    PSGenerateC = 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("PS_CFILE"))

    'open PS.c
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v("PS_CFILE") For Output As f
    
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = FormatModuleDescription(GetModuleHeaderC("PS"))
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc("PS").ModuleDescription, r.v("PS_CFILE"), r.v("MAIN_CPU_TYPE"), r.v("MAIN_COMPILER"), d)
          
            Print #f, uc.GetUserCode("(PS_General,1)", 0)   'USER CODE for application specific descriptions'
            Print #f, ""
            Print #f, Rtf.GenerateNote((r.v("PS_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("(PS_General,2)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_MACROS
            Print #f, ""  'DAvE local macro definitions'
            Print #f, uc.GetUserCode("(PS_General,3)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_DEFINES
            Print #f, ""  'DAvE local #define definitions'
            Print #f, uc.GetUserCode("(PS_General,4)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_TYPEDEFS
            Print #f, ""  'DAvE type defintions'
            Print #f, uc.GetUserCode("(PS_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("(PS_General,6)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_GLOBAL_VARIABLES
            Print #f, ""  'DAvE definitions of global variables'
            Print #f, uc.GetUserCode("(PS_General,7)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_EXTERNAL_PROTOTYPES
            Print #f, ""  'DAvE external declarations of imported functions'
            Print #f, uc.GetUserCode("(PS_General,8)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_LOCAL_PROTOTYPES
            Print #f, ""  'DAvE declarations of internal local functions'
            Print #f, uc.GetUserCode("(PS_General,9)", 0)
            Print #f, ""
            Print #f, ""
    '------------------------------------------------------------------------------------------------
    ' PS_Init function
    '------------------------------------------------------------------------------------------------
    If (r.v("PS_USE_Init") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "PS_NAME_Init")
            Print #f, uc.GetUserCode("(PS_Init,1)", 0)     'USER CODE for application specific descriptions'
            Print #f, ""
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "PS_NAME_Init")
            Print #f, "{"
            Print #f, uc.GetUserCode("(PS_Init,2)", 2)
            Print #f, ""
            
            Print #f, FormatComments(2, 1, "", "", "Slow Down Clock is " & Get_fFCLK(r) & " MHz")
            Print #f, FormatComments(2, 1, "", "", "which is 1/" & GetValueCLKREL(r) & " of the System Clock fSYS")
          '  Print #f, FormatComments(2, 0, "", "", "(the initialization of CMCON.REL is located in function " & r.v("MAIN_NAME_INIT") & ")")
            Print #f, ""
            'Print #f, "  SCU_PAGE      =  0x01;        //  switch to Page 1 "
                  If (r.v(SCU_PREFIX & "PMCON0_WKSEL") > 0) Then
                       d = "wake up with reset is selected"
                      ' Print #f, "  " & SfrBitStatement(r, SCU_PREFIX &"PMCON0_WKSEL", 1)
                       Print #f, FormatComments(2, 1, "", "", d)
                    Else
                    d = "wake up without reset is selected"
                    'Print #f, "  " & SfrBitStatement(r, SCU_PREFIX &"PMCON0_WKSEL", 0)
                    Print #f, FormatComments(2, 1, "", "", d)
                    End If
           Print #f, ""
           Print #f, ""
  
      Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "1, noSST);", "", "", "switch to page1 with out saving")
          
'      Print #f, FormatCodeStatemente(2, SCU_PREFIX & "CMCON", "=", FormatTextOutput(r.sfr(SCU_PREFIX & "CMCON"), "H2"), "load Clock Control Register")
'      Print #f, ""====>by ashish
      Print #f, ""
        If (r.v("PS_Pin_Rx_1_Used") <> 0) Then
            If (r.v("UART_RxPin_P0.1_Used") = 1) Then
               d = "Pin P0.1 (input)"
               Print #f, FormatComments(2, 1, "", "", d)
               d = "- used as wake up pin via RXD_1 from powerdown "
               Print #f, FormatComments(2, 1, "", "", d)
            ElseIf (r.v("UART_RxPin_P1.0_Used") = 1) Then
               d = "Pin P1.0 (input)"
               Print #f, FormatComments(2, 1, "", "", d)
               d = "- used as wake up pin via RXD_0 from powerdown "
           Print #f, FormatComments(2, 1, "", "", d)
            End If
           Print #f, ""
        End If
        
        If (r.v("PS_Pin_Ex_0_Used") = 1 And r.v("EXINT0_Pin_P0.5_Used") = 1) Then
               d = "Pin P0.5 (input)"
            Print #f, FormatComments(2, 1, "", "", d)
              d = "- used as wake up pin via EXINT0 from powerdown "
           Print #f, FormatComments(2, 1, "", "", d)
           Print #f, ""
        End If
      
      If (r.v("PS_Pin_Ex_0_Used") = 1 And r.v("EXINT0_Pin_P1.4_Used") = 1) Then
               d = "Pin P1.4 (input)"
            Print #f, FormatComments(2, 1, "", "", d)
              d = "- used as wake up pin via EXINT0 from powerdown "
           Print #f, FormatComments(2, 1, "", "", d)
           Print #f, ""
        End If
        
      r.v(SCU_PREFIX & "PMCON0_WS") = 0
      If (r.v("PS_Pin_Rx_1_Used") <> 0) Then
         r.v(SCU_PREFIX & "PMCON0_WS") = 1
      End If
      If (r.v("PS_Pin_Ex_0_Used") = 1) Then
         r.v(SCU_PREFIX & "PMCON0_WS") = 2
      End If
      If ((r.v("PS_Pin_Rx_1_Used") <> 0) And r.v("PS_Pin_Ex_0_Used") = 1) Then
         r.v(SCU_PREFIX & "PMCON0_WS") = 3
      End If
      

            Print #f, FormatCodeStatemente(2, SCU_PREFIX & "PMCON0", "=", FormatTextOutput(r.sfr(SCU_PREFIX & "PMCON0"), "H2"), " wakeup control register")
            Print #f, ""
    
    ''********************************for PMCON1****************************************
             Print #f, FormatComments(2, 0, "", "", "---------- Modules configuration status for PMCON1 reg.----------")
      Print #f, ""
      If (r.v("PMCON1_ADC_DIS") = 1) Then
            Print #f, FormatComments(2, 1, "", "", " ADC disabled")
       Else
            Print #f, FormatComments(2, 1, "", "", " ADC enabled")
       End If
        If (r.v("PMCON1_SSC_DIS") = 1) Then
            Print #f, FormatComments(2, 1, "", "", " SSC disabled")
       Else
            Print #f, FormatComments(2, 1, "", "", " SSC enabled")
         End If
         If (r.v("PMCON1_CCU_DIS") = 1) Then
            Print #f, FormatComments(2, 1, "", "", " CCU disabled")
       Else
            Print #f, FormatComments(2, 1, "", "", " CCU enabled")
         End If
         If (r.v("PMCON1_T2_DIS") = 1) Then
            Print #f, FormatComments(2, 1, "", "", " T2 disabled")
       Else
            Print #f, FormatComments(2, 1, "", "", " T2 enabled")
         End If
         If (r.v("PMCON1_MDU_DIS") = 1) Then
            Print #f, FormatComments(2, 1, "", "", " MDU disabled")
       Else
            Print #f, FormatComments(2, 1, "", "", " MDU enabled")
         End If
         If (r.v("PMCON1_CAN_DIS") = 1) Then
            Print #f, FormatComments(2, 1, "", "", " CAN disabled")
       Else
            Print #f, FormatComments(2, 1, "", "", " CAN enabled")
         End If
         If (r.v("PMCON1_CDC_DIS") = 1) Then
            Print #f, FormatComments(2, 1, "", "", " CDC disabled")
       Else
            Print #f, FormatComments(2, 1, "", "", " CDC enabled")
         End If
       Print #f, FormatCodeStatemente(2, SCU_PREFIX & "PMCON1", "=", FormatTextOutput(r.sfr(SCU_PREFIX & "PMCON1"), "H2"), " Power mode control register 1")

       Print #f, ""
            
  '*********************************************************MODPISEL*******************
'      Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, noSST);", "", "", "switch to page0 with out saving")
'           If (r.v("MuxFor_RXD_0_(Wake-up)") > 0) Then
'                 d = "Pin P1.0 (input)"
'                 Print #f, FormatComments(2, 1, "", "", d)
'                 d = "- used as wake up pin via RXD_0 from powerdown "
'                 Print #f, FormatComments(2, 1, "", "", d)
'                 Print #f, ""
'             End If
'
'              If (r.v("MuxFor_EXINT0_0_(Wake-up)") > 0) Then
'                    d = "Pin P0.5 (input)"
'                    Print #f, FormatComments(2, 1, "", "", d)
'                    d = "- used as wake up pin via EXINT0 from powerdown "
'                    Print #f, FormatComments(2, 1, "", "", d)
'               End If
'               If (r.v("MuxFor_RXD_1_(Wake-up)") > 0) Then
'                    d = "Pin P0.1 (input)"
'                    Print #f, FormatComments(2, 1, "", "", d)
'                    d = "- used as wake up pin via RXD_1 from powerdown "
'                    Print #f, FormatComments(2, 1, "", "", d)
'               End If
'
'            Print #f, ""
'
'  Print #f, FormatCodeStatemente(2, SCU_PREFIX & "MODPISEL", "=", FormatTextOutput(r.sfr(SCU_PREFIX & "MODPISEL"), "H2"), "load peripheral input select reg")
'  Print #f, ""
  ''********************************for PMCON2****************************************
  Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "3, noSST);", "", "", "switch to page3 with out saving")
     Print #f, ""
     Print #f, FormatComments(2, 0, "", "", "---------- Modules configuration status for PMCON2 reg.----------")
      Print #f, ""
      If (r.v("PMCON2_T21_DIS") = 1) Then
            Print #f, FormatComments(2, 1, "", "", " T21 disabled")
       Else
            Print #f, FormatComments(2, 1, "", "", " T21 enabled")
       End If
            
    If (r.v("PMCON2_UART1_DIS") = 1) Then
            Print #f, FormatComments(2, 1, "", "", " UART1 disabled")
       Else
            Print #f, FormatComments(2, 1, "", "", " UART1 enabled")
       End If
       Print #f, FormatCodeStatemente(2, SCU_PREFIX & "PMCON2", "=", FormatTextOutput(r.sfr(SCU_PREFIX & "PMCON2"), "H2"), " Power mode control register 2")
      Print #f, ""
 '*********************************end PMCON2*******************************************
'
'         Print #f, "} " & END_OF_FUNCTION & r.v("PS_NAME_Init")
'            Print #f, ""
'            Print #f, ""
'    End If
''**************************************end PMCON2******************************************
'****************************** for MODPISEL****************************************
''Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, noSST);", "", "", "switch to page0 with out saving")
''           If (r.v("MuxFor_RXD_0_(Wake-up)") > 0) Then
''                 d = "Pin P1.0 (input)"
''                 Print #f, FormatComments(2, 1, "", "", d)
''                 d = "- used as wake up pin via RXD_0 from powerdown "
''                 Print #f, FormatComments(2, 1, "", "", d)
''                 Print #f, ""
''             End If
''
''              If (r.v("MuxFor_EXINT0_0_(Wake-up)") > 0) Then
''                    d = "Pin P0.5 (input)"
''                    Print #f, FormatComments(2, 1, "", "", d)
''                    d = "- used as wake up pin via EXINT0 from powerdown "
''                    Print #f, FormatComments(2, 1, "", "", d)
''               End If
''               If (r.v("MuxFor_RXD_1_(Wake-up)") > 0) Then
''                    d = "Pin P0.1 (input)"
''                    Print #f, FormatComments(2, 1, "", "", d)
''                    d = "- used as wake up pin via RXD_1 from powerdown "
''                    Print #f, FormatComments(2, 1, "", "", d)
''               End If
           Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, noSST);", "", "", "switch to page0 with out saving")
            Print #f, ""
            If (r.v("PS_Pin_Ex_0_Used") = 1) Then
             Print #f, FormatCodeStatemente(2, CORE_PREFIX & "IEN0", "|= 0x01;", "", "interrupt enable  register 0")
            Print #f, FormatCodeStatemente(2, CORE_PREFIX & "TCON", "=", FormatTextOutput(r.sfr(CORE_PREFIX & "TCON"), "H2"), "Set TCON.IT0 ")
          Print #f, ""
          End If
            Print #f, FormatCodeStatemente(2, SCU_PREFIX & "MODPISEL", "=", FormatTextOutput(r.sfr(SCU_PREFIX & "MODPISEL"), "H2"), "load peripheral input select reg")
            Print #f, ""
  '****************************** end MODPISEL****************************************
  
         Print #f, "} " & END_OF_FUNCTION & r.v("PS_NAME_Init")
            Print #f, ""
            Print #f, ""
    End If
    '------------------------------------------------------------------------------------------------
    ' PS_SetPowerDown function
    '------------------------------------------------------------------------------------------------
    If (r.v("PS_USE_SetPowerDown") = 1) Then
            Print #f, FormatFunctionHeader(r, func, "PS_NAME_SetPowerDown")
                      '<--                     max. 78 characters                                -->'
                      
                      
            Print #f, FormatFunctionDeclaration(r, func, "PS_NAME_SetPowerDown")
            Print #f, "{"
            Print #f, FormatComments(2, 0, "", "", "enter software power down mode")
 Print #f, FormatCodeStatemente(2, "STR_PAGE(" & SCU_PAGE_NAME & "1, _su);", "", "", "push current SCU page and switch to page1")
            Print #f, ""
         
           '----------------- set the external wake-up configuration --------------
            Print #f, FormatComments(2, 1, "", "", "External wake-up enabled")
           
            Print #f, ""

        If (r.v(SCU_PREFIX & "PMCON0_WS") = 0) Then
            Print #f, FormatComments(2, 1, "", "", TXT_WAKE_UP_SOURCE_0)
            Print #f, "  " & SCU_PREFIX & "PMCON0 &=~(ubyte) 0x01;"
        ElseIf ((r.v(SCU_PREFIX & "PMCON0_WS") = 1) And r.v(SCU_PREFIX & "MODPISEL_URRIS") = 0) Then
            Print #f, FormatComments(2, 1, "", "", TXT_WAKE_UP_SOURCE_1)
            Print #f, "  " & SCU_PREFIX & "PMCON0|=0x01;"
         ElseIf ((r.v(SCU_PREFIX & "PMCON0_WS") = 1) And r.v(SCU_PREFIX & "MODPISEL_URRIS") = 1) Then
            Print #f, FormatComments(2, 1, "", "", TXT_WAKE_UP_SOURCE_5)
            Print #f, "  " & SCU_PREFIX & "PMCON0|=0x01;"
        ElseIf (r.v(SCU_PREFIX & "PMCON0_WS") = 2) Then
            Print #f, FormatComments(2, 1, "", "", TXT_WAKE_UP_SOURCE_2)
            Print #f, "  " & SCU_PREFIX & "PMCON0|=0x02;"
        ElseIf ((r.v(SCU_PREFIX & "PMCON0_WS") = 3) And r.v(SCU_PREFIX & "MODPISEL_URRIS") = 0) Then
            Print #f, FormatComments(2, 1, "", "", TXT_WAKE_UP_SOURCE_3)
            Print #f, "  " & SCU_PREFIX & "PMCON0|=0x03;"
            Print #f, ""
          ElseIf ((r.v(SCU_PREFIX & "PMCON0_WS") = 3) And r.v(SCU_PREFIX & "MODPISEL_URRIS") = 1) Then
            Print #f, FormatComments(2, 1, "", "", TXT_WAKE_UP_SOURCE_4)
            Print #f, "  " & SCU_PREFIX & "PMCON0|=0x03;"
            Print #f, ""
        End If
         
            Print #f, FormatCodeStatemente(2, "MAIN_vUnlockProtecReg();", "", "", "open access to protected register")
           Print #f, " "
          Print #f, "  " & SfrBitStatement(r, SCU_PREFIX & "PMCON0_PD", 1)
          Print #f, " "
            Print #f, FormatCodeStatemente(2, "MAIN_vlockProtecReg();", "", "", "close access to protected register")
          Print #f, ""
          
 Print #f, FormatCodeStatemente(2, "RST_PAGE(_su);", "", "", "restore the old SCU page")
          Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("PS_NAME_SetPowerDown")
            Print #f, ""
            Print #f, ""
                             
      End If
    '------------------------------------------------------------------------------------------------
    ' PS_SetSlowDown function
    '------------------------------------------------------------------------------------------------
    If (r.v("PS_USE_SetSlowDown") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "PS_NAME_SetSlowDown")
    Print #f, FormatFunctionDeclaration(r, func, "PS_NAME_SetSlowDown")
            Print #f, "{"
            Print #f, ""
 Print #f, FormatCodeStatemente(2, "STR_PAGE(" & SCU_PAGE_NAME & "1, _su);", "", "", "push current SCU page and switch to page1")
            Print #f, ""
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "MAIN_vUnlockProtecReg();", "", "", "open access to protected register")
            Print #f, ""
            Print #f, "  " & SfrBitStatement(r, SCU_PREFIX & "PMCON0_SD", 1)
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "MAIN_vlockProtecReg();", "", "", "close access to protected register")
            Print #f, ""
          
 Print #f, FormatCodeStatemente(2, "RST_PAGE(_su);", "", "", "restore the old SCU page")
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("PS_NAME_SetSlowDown")
            Print #f, ""
            Print #f, ""
        End If
    '------------------------------------------------------------------------------------------------
    ' PS_ResetSlowDown function
    '------------------------------------------------------------------------------------------------
       If (r.v("PS_USE_ResetSlowDown") = 1) Then
            Print #f, FormatFunctionHeader(r, func, "PS_NAME_ResetSlowDown")
            Print #f, FormatFunctionDeclaration(r, func, "PS_NAME_ResetSlowDown")
            Print #f, "{"
                       Print #f, ""
 Print #f, FormatCodeStatemente(2, "STR_PAGE(" & SCU_PAGE_NAME & "1, _su);", "", "", "push current SCU page and switch to page1")
            Print #f, ""
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "MAIN_vUnlockProtecReg();", "", "", "open access to protected register")
            Print #f, ""
            Print #f, "  " & SfrBitStatement(r, SCU_PREFIX & "PMCON0_SD", 0)
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "MAIN_vlockProtecReg();", "", "", "close access to protected register")
            Print #f, ""
          
 Print #f, FormatCodeStatemente(2, "RST_PAGE(_su);", "", "", "restore the old SCU page")
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("PS_NAME_ResetSlowDown")
            Print #f, ""
            Print #f, ""
        End If
            
            Print #f, uc.GetUserCode("(PS_General,10)", 0)
            Print #f, ""
    'close file
    Close f
    
    PSGenerateC = True
    Exit Function
    
PSGenerateCError:
    Close f
    Exit Function
End Function


'*****************************************************************************
'Description:
'        Generates the code for the PS.h file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function PSGenerateH(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo PSGenerateHError
    
    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
        Dim iMod As Integer
   
    PSGenerateH = False

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

    'refresh the decriptions of the functions and macros
    RefreshFunctionDescriptions(func, r)
    
    'read usercode
    uc.ReadFile ((StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v("PS_HFILE"))
    
    'open PS.h
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v("PS_HFILE") For Output As f
    
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = FormatModuleDescription(GetModuleHeaderH("PS"))
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc("PS").ModuleDescription, r.v("PS_HFILE"), r.v("MAIN_CPU_TYPE"), r.v("MAIN_COMPILER"), d)
            
            Print #f, uc.GetUserCode("(PS_Header,1)", 0)
            Print #f, ""
            Print #f, Rtf.GenerateNote((r.v("PS_Notes")))
            Print #f, ""
            Print #f, "#ifndef _" & StripExtension(r.v("PS_HFILE")) & "_H_"
            Print #f, "#define _" & StripExtension(r.v("PS_HFILE")) & "_H_"
            Print #f, ""
            Print #f, SECTION_PROJECT_INCLUDES
            Print #f, ""  'DAvE includes'
            Print #f, uc.GetUserCode("(PS_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("(PS_Header,3)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_DEFINES
            Print #f, ""  'DAvE global #define definitions'
            Print #f, uc.GetUserCode("(PS_Header,4)", 0)
            Print #f, ""

           'Print #f, "#define " & r.v("PS_PARA_XXX") & " bla bla bla"

            Print #f, ""
            Print #f, SECTION_TYPEDEFS
            Print #f, ""  'DAvE type definitions'
            Print #f, uc.GetUserCode("(PS_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("(PS_Header,6)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_GLOBAL_VARIABLES
            Print #f, ""  'DAvE definition of global variables'
            Print #f, uc.GetUserCode("(PS_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("PS_USE_" & sName) = 1) And (func.FunctionSort(d) = 1)) Then
            Print #f, FormatFunctionDeclaration(r, func, d) & ";"
        End If
    Next l

            Print #f, ""
            Print #f, uc.GetUserCode("(PS_Header,8)", 0)
            Print #f, ""
            Print #f, ""
    'DAvE declarations of global macros'
    '------------------------------------------------------------------------------------------------
    ' PS_SetIdle macro
    '------------------------------------------------------------------------------------------------
    If (r.v("PS_USE_SetIdle") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "PS_NAME_SetIdle")

            Print #f, "#define " & r.v("PS_NAME_SetIdle") & "() " & SfrBitStatement(r, CORE_PREFIX & "PCON_IDLE", 1, False)
            Print #f, ""
            Print #f, ""
    End If
  
    '------------------------------------------------------------------------------------------------
    ' PS_DisableModule macro
    '------------------------------------------------------------------------------------------------
    If (r.v("PS_USE_DisableModule") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "PS_NAME_DisableModule")

            Print #f, "#define " & (r.v("PS_NAME_DisableModule")) & "(Module) " & r.v("PS_NAME_DisableModule") & "_(Module)"
            Print #f, ""
                        Print #f, "#define " & r.v("PS_NAME_DisableModule") & "_(Module) " & r.v("PS_NAME_DisableModule") & "_##Module"
            Print #f, ""

        For iMod = 0 To NUMBER_OF_PM_MODULES - 1
                    sName = ModuleId(iMod)
            Print #f, "#define " & r.v("PS_NAME_DisableModule") & "_" & FillRight(20, r.v("PS_PARA_MODULE_" & sName)) & " " & _
                                SfrBitStatement(r, ModuleDisableBit(iMod), 1, False)
        Next iMod
            Print #f, ""
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' PS_EnableModule macro
    '------------------------------------------------------------------------------------------------
    If (r.v("PS_USE_EnableModule") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "PS_NAME_EnableModule")

            Print #f, "#define " & (r.v("PS_NAME_EnableModule")) & "(Module) " & r.v("PS_NAME_EnableModule") & "_(Module)"
            Print #f, ""
                        Print #f, "#define " & r.v("PS_NAME_EnableModule") & "_(Module) " & r.v("PS_NAME_EnableModule") & "_##Module"
            Print #f, ""

        For iMod = 0 To NUMBER_OF_PM_MODULES - 1
                    sName = ModuleId(iMod)
            Print #f, "#define " & r.v("PS_NAME_EnableModule") & "_" & FillRight(20, r.v("PS_PARA_MODULE_" & sName)) & " " & _
                                SfrBitStatement(r, ModuleDisableBit(iMod), 0, False)
        Next iMod
            Print #f, ""
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
            Print #f, uc.GetUserCode("(PS_Header,9)", 0)
            Print #f, ""
            Print #f, "#endif  // ifndef _" & StripExtension(r.v("PS_HFILE")) & "_H_"
    
    'close file
    Close f
    
    PSGenerateH = True
    Exit Function
    
PSGenerateHError:
    Close f
    errorbox "error ps.h", Err.Description
    Exit Function
End Function

