Attribute VB_Name = "Module1"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\include\Port.inc"
'#Uses "..\classes\ParameterClass.cls"
'#Uses "..\classes\FunctionClass.cls"
'#Uses "Include.inc"
Option Explicit
'*****************************************************************************
' Module:
'        SCU
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions for generating code for the
'        module 'SCU'.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        3.0   ahe          13.09.00    final version for C161CS/JC/JI (PM)
'
'        ----------------------------------------------------------------------
'        1.0   ahe          02.05.01    final version for Pegasus
'        1.1   ste          06.01.03    spelling correction
'        ----------------------------------------------------------------------
'        2.0   Raja         07.09.04    final version for XC164CM Pegasus light
'
'*****************************************************************************




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

    ' write files SCU.h
    If (r.v("SCU_USE_Init")) Then
      If (Not SCUGenerateH(r, vIndex)) Then Exit Function
                   If (Not r.AddFile(r.s("SCU_HFILE"))) Then Exit Function
    End If

    ' write file SCU.c
    If (r.v("SCU_USE_Init")) Then
      If (Not SCUGenerateC(r, vIndex)) Then Exit Function
                   If (Not r.AddFile(r.s("SCU_CFILE"))) Then Exit Function
    End If

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


'*****************************************************************************
'Description:
'        Generates the code for the SCU.c file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function SCUGenerateC(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo SCUGenerateCError
    
    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
    Dim sVarPre As String
    Dim sName As String
    Dim n As String
    Dim sPara As String
    Dim sAlternatePinName(1 To 2) As String
    Dim sValue As String

    sVarPre = "SCU"
    
    SCUGenerateC = False

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

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

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

    'open ASC.c
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v(sVarPre & "_CFILE") For Output As f
    
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = FormatModuleDescription(GetModuleHeaderC(sVarPre))
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc(sVarPre).ModuleDescription, r.v(sVarPre & "_CFILE"), r.v("MAIN_CPU_TYPE"), r.v("MAIN_COMPILER"), d)
          
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,1)", 0)   'USER CODE for application specific descriptions'
    Print #f, ""
    Print #f, Rtf.GenerateNote((r.v(sVarPre & "_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("(" & sVarPre & "_General,2)", 0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_MACROS
    Print #f, ""  'DAvE local macro definitions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,3)", 0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_DEFINES
    Print #f, ""  'DAvE local #define definitions'
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,4)", 0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_TYPEDEFS
    Print #f, ""  'DAvE type defintions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_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("(" & sVarPre & "_General,6)", 0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_GLOBAL_VARIABLES
    Print #f, ""  'DAvE definitions of global variables'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,7)", 0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_EXTERNAL_PROTOTYPES
    Print #f, ""  'DAvE external declarations of imported functions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,8)", 0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_LOCAL_PROTOTYPES
    Print #f, ""  'DAvE declarations of internal local functions'
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,9)", 0)
    Print #f, ""
    Print #f, ""
    
    '------------------------------------------------------------------------------------------------
    ' SCU_vInit function
    '------------------------------------------------------------------------------------------------
    n = "Init"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)
      Print #f, "{"
      Print #f, uc.GetUserCode("(" & n & ",2)", 2)
      Print #f, ""

      '------------------------------------------------------------------------------------------------
      ' SLEEP Mode Configuration (SYSCON1):
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "System Control Register 1 (SYSCON1):")

      '------------------------------------------------- SYSCON1_PDCFG
      Select Case (r.v("SYSCON1_PDCFG"))
                  Case (0)
                       d = "port drivers are always on"
                  Case (1)
                       d = "port drivers are off in IDLE or Sleep mode"
                  Case (2)
                       d = "port drivers are off in power down mode"
      End Select
      Print #f, FormatCommentsForInit(1, d)

      '------------------------------------------------- SYSCON1_PFCFG
      Select Case (r.v("SYSCON1_PFCFG"))
                  Case (0)
                       d = "programm Flash is always on"
                  Case (1)
                       d = "programm Flash is off in IDLE or Sleep mode"
      End Select
      Print #f, FormatCommentsForInit(1, d)

      '------------------------------------------------- SYSCON1_CPSYS
      Select Case (r.v("SYSCON1_CPSYS"))
                  Case (0)
                       d = "clock prescaler for system is fpll / 1"
                  Case (1)
                       d = "clock prescaler for system is fpll / 2"
      End Select
      Print #f, FormatCommentsForInit(1, d)

      '------------------------------------------------- set SYSCON1
      Print #f, ""
      d = "this register must be set in the start-up file"
      Print #f, FormatCommentsForSource(2, 2, d)
      d = "SYSCON1  =  " & FormatTextOutput(r.sfr("SYSCON1"), "H4")
      Print #f, FormatCommentsForSource(2, 2, d)
      Print #f, ""

      '------------------------------------------------------------------------------------------------
      ' Configuration of Peripheral Management (SYSCON3):
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of Peripheral Management (SYSCON3):")
      
      '------------------------------------------------- SYSCON3_PERDISx
      If (r.v("SYSCON3_ADCDIS") = 0) Then
        d = "Analog/Digital converter (ADC) is enabled"
      Else
        d = "Analog/Digital converter (ADC) is disabled"
      End If
      Print #f, FormatCommentsForInit(1, d)

      '------------------------------------------------- SYSCON3_PERDISx
      If (r.v("SYSCON3_ASC0DIS") = 0) Then
        d = "asynchronous serial channel (ASC0) is enabled"
      Else
        d = "asynchronous serial channel (ASC0) is disabled"
      End If
      Print #f, FormatCommentsForInit(1, d)
      
      '------------------------------------------------- SYSCON3_PERDISx
      If (r.v("SYSCON3_SSC0DIS") = 0) Then
        d = "synchronous serial channel 0 (SSC0) is enabled"
      Else
        d = "synchronous serial channel 0 (SSC0) is disabled"
      End If
      Print #f, FormatCommentsForInit(1, d)

      '------------------------------------------------- SYSCON3_PERDISx
      If (r.v("SYSCON3_GPTDIS") = 0) Then
        d = "general purpose timer block (GPT1/2) is enabled"
      Else
        d = "general purpose timer block (GPT1/2) is disabled"
      End If
      Print #f, FormatCommentsForInit(1, d)

      '------------------------------------------------- SYSCON3_PERDISx
      If (r.v("SYSCON3_PFMDIS") = 0) Then
        d = "on-chip programm flash memory is enabled"
      Else
        d = "on-chip programm flash memory is disabled"
      End If
      Print #f, FormatCommentsForInit(1, d)
      
      If (r.Regs.BitFind("SYSCON3_CC1DIS") = True) Then
      '------------------------------------------------- SYSCON3_PERDISx
            If (r.v("SYSCON3_CC1DIS") = 0) Then
              d = "CAPCOM unit 1 is enabled"
            Else
              d = "CAPCOM unit 1 is disabled"
            End If
            Print #f, FormatCommentsForInit(1, d)
      End If
      
      '------------------------------------------------- SYSCON3_PERDISx
      If (r.v("SYSCON3_CC2DIS") = 0) Then
        d = "CAPCOM unit 2 is enabled"
      Else
        d = "CAPCOM unit 2 is disabled"
      End If
      Print #f, FormatCommentsForInit(1, d)

      If (r.Regs.BitFind("SYSCON3_CC6DIS") = True) Then
          '------------------------------------------------- SYSCON3_PERDISx
          If (r.v("SYSCON3_CC6DIS") = 0) Then
            d = "CAPCOM unit 6 is enabled"
          Else
            d = "CAPCOM unit 6 is disabled"
          End If
          Print #f, FormatCommentsForInit(1, d)
      End If

      If (r.Regs.BitFind("SYSCON3_ASC1DIS") = True) Then
          '------------------------------------------------- SYSCON3_PERDISx
          If (r.v("SYSCON3_ASC1DIS") = 0) Then
            d = "asynchronous serial channel 1 (ASC1) is enabled"
          Else
            d = "asynchronous serial channel 1 (ASC1) is disabled"
          End If
          Print #f, FormatCommentsForInit(1, d)
      End If

      If (r.Regs.BitFind("SYSCON3_I2CDIS") = True) Then
          '------------------------------------------------- SYSCON3_PERDISx
          If (r.v("SYSCON3_I2CDIS") = 0) Then
            d = "on-chip I2C bus module (I2C) is enabled"
          Else
            d = "on-chip I2C bus module (I2C) is disabled"
          End If
          Print #f, FormatCommentsForInit(1, d)
      End If

      If (r.Regs.BitFind("SYSCON3_SDLMDIS") = True) Then
          '------------------------------------------------- SYSCON3_PERDISx
          If (r.v("SYSCON3_SDLMDIS") = 0) Then
            d = "on-chip SDLM (J1850) module (SDLM) is enabled"
          Else
            d = "on-chip SDLM (J1850) module (SDLM) is disabled"
          End If
          Print #f, FormatCommentsForInit(1, d)
      End If
      
      '------------------------------------------------- SYSCON3_PERDISx
      If (r.v("SYSCON3_CANDIS") = 0) Then
        d = "on-chip controller area network unit (CAN) is enabled"
      Else
        d = "on-chip controller area network unit (CAN) is disabled"
      End If
      Print #f, FormatCommentsForInit(1, d)

      '------------------------------------------------- SYSCON3_PERDISx
      If (r.v("SYSCON3_SSC1DIS") = 0) Then
        d = "synchronous serial channel 1 (SSC1) is enabled"
      Else
        d = "synchronous serial channel 1 (SSC1) is disabled"
      End If
      Print #f, FormatCommentsForInit(1, d)

      '------------------------------------------------- set SYSCON3
      Print #f, ""
      d = "this register must be set in the start-up file"
      Print #f, FormatCommentsForSource(2, 2, d)
      d = "SYSCON3  =  " & FormatTextOutput(r.sfr("SYSCON3"), "H4")
      Print #f, FormatCommentsForSource(2, 2, d)
      Print #f, ""

      '------------------------------------------------------------------------------------------------
      ' Configuration of Clock Output Frequency:
      '------------------------------------------------------------------------------------------------
      If (GetPinFunction(r, "FOUT") = 2 Or GetPinFunction(r, "CLKOUT") = 2) Then
          Print #f, FormatSectionHeader(2, 1, "Configuration of Clock Output Frequency:")

          '-----------------------------------------------------------------------
          'If FOUT used
          '-----------------------------------------------------------------------
          If (GetPinFunction(r, "FOUT") = 2) Then
              d = "programmable frequency fout is output on pin FOUT (" & GetPinName("FOUT") & ")"
              Print #f, FormatCommentsForInit(1, d)

              '------------------------------------------------- FOCON_FOEN
              If (r.v("FOCON_FOEN") = 0) Then
                d = "counter is not started after initialization"
              Else
                d = "counter is started after initialization"
              End If
              Print #f, FormatCommentsForInit(1, d)

              '------------------------------------------------- FOCON_FOSS
              If (r.v("FOCON_FOSS") = 0) Then
                d = "frequency output of the toggle latch (DC = 50%)"
              Else
                d = "frequency output of the reload counter (DC depends on FORV)"
              End If
              Print #f, FormatCommentsForInit(1, d)

              d = "real output frequency is " & Format(GetRealOutputFrequency(r) * 1000, "0.000") & " kHz"
              Print #f, FormatCommentsForInit(1, d)
          End If

          If (r.v("FOCON_CLKEN") = 1) Then
              d = "CLKOUT signal is enabled"
              Print #f, FormatCommentsForInit(1, d)
          End If

          Print #f, ""
          Print #f, "  " & r.v("MAIN_NAME_UnlockProtecReg") & "();       // unlock FOCON"
          v = r.sfr("FOCON")
          Print #f, FormatCodeStatemente(2, "FOCON", "=", FormatTextOutput(v, "H4"), "load FOCON register")
          Print #f, ""

          '-----------------------------------------------------------------------
          'If FOUT used
          '-----------------------------------------------------------------------
          If (GetPinFunction(r, "FOUT") = 2) Then
              '------------------------------------------------------------------------------------------------
              ' Configuration of the used ... Port Pins:
              '------------------------------------------------------------------------------------------------
              Print #f, FormatSectionHeader(2, 1, "Configuration of the used " & sVarPre & " Port Pins:")
          
              'sAlternatePinName(1) = "FOUT"
              'sAlternatePinName(2) = "CLKOUT"
              'Call SetAlternatePinStatements(f, r, sAlternatePinName())

              ' P3.15 has a MUX for alternate output, so the Direction must not switch via DP3
              ' Switching DP3.15 to output and P3.15 to 0 is only necessary for the FOUT inactive level
              ' (see Spec. 1.1 6-45)
              Print #f, "  ///  - P3.15 is used for Programmable Clock Output (FOUT)"
              Print #f, ""
              Print #f, "  P3   = (P3   & ~(uword)0x8000);             // reset data register"
              Print #f, "  DP3  = (DP3  & ~(uword)0x8000) | 0x8000;    // set direction register"
              Print #f, ""
          End If

      End If ' end of "Configuration of Clock Output Frequency:"

      Print #f, ""
      Print #f, uc.GetUserCode("(" & n & ",3)", 2)
      Print #f, ""
      Print #f, "} " & END_OF_FUNCTION & r.v("SCU_NAME_Init")
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' Other function
    '------------------------------------------------------------------------------------------------

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SCU_SetPowerDown function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetPowerDown"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)
      Print #f, "{"
      Print #f, ""
      Print #f, uc.GetUserCode("(" & n & ",2)", 2)
      Print #f, ""
      Print #f, "  PWRDN;"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SCU_SetIdle function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetIdle"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)
      Print #f, "{"
      Print #f, ""
      Print #f, "  " & r.v("MAIN_NAME_UnlockProtecReg") & "();             // unlock SYSCON3"
      Print #f, "  SYSCON3 = SYSCON3 & ~(uword)0x0003;  // set SLEEPCON"
      Print #f, ""
      Print #f, uc.GetUserCode("(" & n & ",2)", 2)
      Print #f, ""
      Print #f, "  IDLE;"
      Print #f, ""
      Print #f, uc.GetUserCode("(" & n & ",3)", 2)
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SCU_SetSleep function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetSleep"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)
      Print #f, "{"
      Print #f, ""
      Print #f, "  " & r.v("MAIN_NAME_UnlockProtecReg") & "();                        // unlock SYSCON3"
      Print #f, "  SYSCON3 = (SYSCON3 & ~(uword)0x0003) | 0x0001;  // set SLEEPCON"
      Print #f, ""
      Print #f, uc.GetUserCode("(" & n & ",2)", 2)
      Print #f, ""
      Print #f, "  IDLE;"
      Print #f, ""
      Print #f, uc.GetUserCode("(" & n & ",3)", 2)
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SCU_EnableModul function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "EnableModule"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)
      Print #f, "{"
      Print #f, "  " & r.v("MAIN_NAME_UnlockProtecReg") & "();"
      Print #f, "  SYSCON3 = SYSCON3 & (~uwModule);"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SCU_DisableModul function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "DisableModule"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)
      Print #f, "{"
      Print #f, "  " & r.v("MAIN_NAME_UnlockProtecReg") & "();"
      Print #f, "  SYSCON3 = SYSCON3 | uwModule;"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If


    '------------------------------------------------------------------------------------------------
    ' Other function
    '------------------------------------------------------------------------------------------------

    Print #f, ""
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,10)", 0)
    Print #f, ""
    
    'close file
    Close f
    
    SCUGenerateC = True
    Exit Function
    
SCUGenerateCError:
    Close f
    Exit Function
End Function


'*****************************************************************************
'Description:
'        Generates the code for the SCU.h file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function SCUGenerateH(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo SCUGenerateHError
    
    Dim i As Integer
    Dim v As Variant
    Dim f As Variant
    Dim n As String
    Dim l As Long
    Dim d As String
    Dim dc As DAvECollection
    Dim uc As UserCode
    Dim para As ParameterClass
    Dim func As FunctionClass
    Dim sRegPre As String
    Dim sVarPre As String

    sVarPre = "SCU"
   
    SCUGenerateH = False

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

    'refresh the decriptions of the functions and macros
    RefreshFunctionDescriptions(vIndex, func, r)
    
    'refresh the decriptions of the parameters
    RefreshParameterDescriptions(vIndex, para, r)

    'read usercode
    uc.ReadFile ((StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v(sVarPre & "_HFILE"))
    
    'open ASC.h
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v(sVarPre & "_HFILE") For Output As f
    
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = FormatModuleDescription(GetModuleHeaderH(sVarPre))
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc(sVarPre).ModuleDescription, r.v(sVarPre & "_HFILE"), r.v("MAIN_CPU_TYPE"), r.v("MAIN_COMPILER"), d)
            
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,1)", 0)
    Print #f, ""
    Print #f, Rtf.GenerateNote((r.v(sVarPre & "_Notes")))
    Print #f, ""
    Print #f, "#ifndef _" & StripExtension(r.v(sVarPre & "_HFILE")) & "_H_"
    Print #f, "#define _" & StripExtension(r.v(sVarPre & "_HFILE")) & "_H_"
    Print #f, ""
    Print #f, SECTION_PROJECT_INCLUDES
    Print #f, ""  'DAvE includes'
    Print #f, uc.GetUserCode("(" & sVarPre & "_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("(" & sVarPre & "_Header,3)", 0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_DEFINES
    Print #f, ""  'DAvE global #define definitions'
    Print #f, ""

    n = "ADC"
    Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "0x0001")
    n = "ASC0"
    Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "0x0002")
    n = "SSC0"
    Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "0x0004")
    n = "GPT1_2"
    Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "0x0008")
    n = "PFM"
    Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "0x0020")
    
    If (r.Regs.BitFind("SYSCON3_CC1DIS") = True) Then
        n = "CC1"
        Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "0x0040")
    End If
        
    n = "CC2"
    Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "0x0080")

    If (r.Regs.BitFind("SYSCON3_CC6DIS") = True) Then
        n = "CC6"
        Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "0x0100")
    End If

    If (r.Regs.BitFind("SYSCON3_ASC1DIS") = True) Then
        n = "ASC1"
        Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "0x0400")
    End If

    If (r.Regs.BitFind("SYSCON3_I2CDIS") = True) Then
        n = "I2C"
        Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "0x0800")
    End If

    If (r.Regs.BitFind("SYSCON3_SDLMDIS") = True) Then
        n = "SDLM"
        Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "0x1000")
    End If

    n = "CAN"
    Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "0x2000")
    n = "SSC1"
    Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "0x8000")

    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,4)", 0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_TYPEDEFS
    Print #f, ""  'DAvE type definitions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_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("(" & sVarPre & "_Header,6)", 0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_GLOBAL_VARIABLES
    Print #f, ""  'DAvE definition of global variables'
    Print #f, uc.GetUserCode("(" & sVarPre & "_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")
      n = 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(sVarPre & "_USE_" & n) = 1 And func.FunctionSort(d) = 1) Then
            Print #f, FormatFunctionDeclaration(r, func, d) & ";"
      End If
    Next l
    Print #f, ""
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,8)", 0)
    Print #f, ""
    Print #f, ""
                          'DAvE declarations of global macros'

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SCU_uwGetSystemStatus macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetSystemStatus"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " SYSSTAT"
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SCU_v.. macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "StartCount"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " P3 = (P3 & ~(uword)0x8000) | 0x8000; FOCON_FOEN = 1"
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SCU_v.. macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "StopCount"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " FOCON_FOEN = 0; P3 = (P3 & ~(uword)0x8000)"
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SCU_v.. macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetReloadValue"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " FOCON = (FOCON & ~(uword)0x3F00) | (uwValue << 8)"
      Print #f, ""
      Print #f, ""
    End If


    Print #f, ""
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,8)", 0)
    Print #f, ""
    Print #f, ""
    Print #f, "#endif  // ifndef _" & StripExtension(r.v(sVarPre & "_HFILE")) & "_H_"
    
    'close file
    Close f
    
    SCUGenerateH = True
    Exit Function
    
SCUGenerateHError:
    Close f
    Exit Function
End Function

