Attribute VB_Name = "Module1"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\classes\FunctionClass.cls
'#Uses "..\classes\ModuleClass.cls
'#Uses "..\classes\Port.cls
'#Uses "..\classes\PortPin.cls
'#Uses "Include.inc"
'#Uses "SharedInterrupts.inc"
Option Explicit
'*****************************************************************************
' Module:
'        MAIN
'-----------------------------------------------------------------------------
' Description:
'        This module contains functions for the generation of following files:
'        - MAIN C File
'        - MAIN H File
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        0.1  Sheshadri      15-09-04    initial version
'        0.2  Sunil          16-11-04    global function prototypes added.
'                                        removed unused variable "ir" of type InterruptClass.
'        0.3  Sheshadri      06-12-04    Unused variables removed
'                                        PLL Mode Parameter Codegeneration modified
'        0.4  radhish        27-12-04    Module name appended before register names
'        0.5  Sunil          29-12-04    Bug fixing code added (for Step-A).
'        0.6  Sunil          01-02-05    Code to configure external oscillator NDIV is added.
'        0.7  Sunil          02-02-05    Functions MAIN_vlockProtecReg();  and MAIN_vUnlockProtecReg();
'                                        are replaced with macros.
'        0.8  Sheshadri     18-01-06    XC886 T21_T2CON Bits redefn removed
'
'
'*****************************************************************************

'*****************************************************************************
'Description:
'        This function generates C- and H-File for the MAIN module.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the Project data object
'        vIndex          parameter for the module
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function StartGenerateCode(r As ProjectData, vIndex As Variant) As Boolean
  On Error GoTo StartGenerateCodeError
  StartGenerateCode = False
        
  Dim sVarPre As String
  sVarPre = "MAIN"

  SHINTFLAG = 0
    If (((r.v("T2_USE_IsrTmr") = 1) And (r.v("T2_USE_Init") = 1)) Or ((r.v("BRG_Interrupt_Select") = 1) And (r.v("UART_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN0") = 1) And (r.v("CAN_USE_Init") = 1))) Then
       SHINTFLAG = 1
    End If
    If (((r.v("ADC_USE_Isr") = 1) And (r.v("ADC_USE_Init") = 1)) Or (((r.v("CAN_USE_SRN1") = 1) Or (r.v("CAN_USE_SRN2") = 1)) And (r.v("CAN_USE_Init") = 1))) Then
       SHINTFLAG = 1
    End If
    If (((r.v("T21_USE_IsrTmr") = 1) And (r.v("T21_USE_Init") = 1)) Or ((r.v("IR_USE_IsrExt2") = 1) And (r.v("IR_USE_Init") = 1)) Or ((r.v("UART1_USE_Isr") = 1) And (r.v("UART1_USE_Init") = 1)) Or ((r.v("BRG1_Interrupt_Select") = 1) And (r.v("UART1_USE_Init") = 1))) Then
       'SHINTFLAG = 1
       If ((r.v(SCU_PREFIX & "EXICON0_EXINT2") <> 3)) Then   ' Added after alpha release
           SHINTFLAG = 1
       End If
    End If
    If (((r.v("IR_USE_IsrExt3") = 1) And (r.v("IR_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN3") = 1) And (r.v("CAN_USE_Init") = 1))) Then
       If (Not ((r.v(SCU_PREFIX & "EXICON0_EXINT3") = 3) And (r.v(SCU_PREFIX & "EXICON1_EXINT4") = 3) And (r.v(SCU_PREFIX & "EXICON1_EXINT5") = 3) And (r.v(SCU_PREFIX & "EXICON1_EXINT6") = 3))) Or ((r.v("CAN_USE_SRN3") = 1) And (r.v("CAN_USE_Init") = 1)) Then
           SHINTFLAG = 1
       End If
    End If
    If (((r.v(CORE_PREFIX & "IEN1_ECCIP0") = 1) And (r.v("CCU6_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN4") = 1) And (r.v("CAN_USE_Init") = 1))) Then
        SHINTFLAG = 1
    End If
    If (((r.v(CORE_PREFIX & "IEN1_ECCIP1") = 1) And (r.v("CCU6_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN5") = 1) And (r.v("CAN_USE_Init") = 1))) Then
        SHINTFLAG = 1
    End If
    If (((r.v(CORE_PREFIX & "IEN1_ECCIP2") = 1) And (r.v("CCU6_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN6") = 1) And (r.v("CAN_USE_Init") = 1))) Then
        SHINTFLAG = 1
    End If
    If (((r.v(CORE_PREFIX & "IEN1_ECCIP3") = 1) And (r.v("CCU6_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN7") = 1) And (r.v("CAN_USE_Init") = 1))) Then
        SHINTFLAG = 1
    End If
  ' write files for project.h
  If (Not MainGenerateH(r)) Then Exit Function
        If (Not r.AddFile(r.s("MAIN_HFILE"), "H")) Then Exit Function
  
  ' write files for project.c
  If (Not MainGenerateC(r)) Then Exit Function
       If (Not r.AddFile(r.s("MAIN_CFile"), "C")) Then Exit Function
   
  If ((r.v("MAIN_COMPILER") = "Keil") And (r.v("MAIN_KeilStartup") = 1)) Then
    If (Not MainGenerateStartupFile(r)) Then Exit Function
        If (Not r.AddFile("START_XC.A51", "A51")) Then Exit Function
  End If
      
  If ((r.v("MAIN_COMPILER") = "SDCC") And (r.v("MAIN_KeilStartup") = 1)) Then
    If (Not MainGenerateStartupFile_SDCC(r)) Then Exit Function
        If (Not r.AddFile("startupxc886.s", "A51")) Then Exit Function
  End If
      
  If (SHINTFLAG = 1) Then
     ' write files for shint.h
       If (Not SharedIntGenerateH(r)) Then Exit Function
          If (Not r.AddFile("SHARED_INT.H", "H")) Then Exit Function
     ' write files for shint.c
       If (Not SharedIntGenerateC(r)) Then Exit Function
          If (Not r.AddFile("SHARED_INT.C", "C")) Then Exit Function
  End If

  StartGenerateCode = True

  Exit Function
    
StartGenerateCodeError:
    Call Errorbox("Code generation failed", Error$)
    Exit Function
End Function

'*****************************************************************************
'Description:
'        This function generates the C-File for this module.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the Project data object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function MainGenerateC(r As ProjectData) As Boolean
    On Error GoTo MainGenerateCError
    
    Dim l           As Long
    Dim f           As Variant
    Dim v           As Variant
    Dim d           As String
    Dim p           As Integer
    Dim dc          As DAvECollection
    Dim uc          As UserCode
    Dim sIndex      As String
    Dim n           As String
    Dim func        As FunctionClass
    Dim mo          As ModuleClass
    Dim cp          As DAvECollection
    Dim iPort       As Integer
    Dim iPin        As Integer
    Dim sReg        As String
    Dim cPorts      As DAvECollection
    Dim Port        As Port
    Dim PortPin     As PortPin
    Dim sPin        As String
    Dim sDir        As String
    Dim sPageUsed   As Boolean
    Dim freq As Double
    sPageUsed = False
    MainGenerateC = False

    Set dc = ObjectCollection("MODULENAMES")
    Set uc = New UserCode
    Set func = New FunctionClass
    Set cp = ObjectCollection("IRSOURCES")
    Set cPorts = ObjectCollection("PORTS")
    Set PortPin = ObjectCollection("PORTPINS")

    Dim sVarPre As String
    sVarPre = "MAIN"
    'refresh the decriptions of the functions and macros
     Call RefreshFunctionDescriptions(func, r)
    
    'read usercode
    uc.ReadFile ((StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v("MAIN_CFile"))

    'open MAIN.c
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v("MAIN_CFile") For Output As f
 
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = Rtf.RtfDescriptionFirstLine & "This file contains the Project initialization function."
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc("MAIN").ModuleDescription, r.v("MAIN_CFile"), r.v("MAIN_CPU_TYPE"), (r.v("MAIN_COMPILER")), d)

            Print #f, uc.GetUserCode("(MAIN_General,1)", 0)   'USER CODE for application specific descriptions'
            Print #f, ""
            Print #f, Rtf.GenerateNote((r.v("MAIN_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("(MAIN_General,2)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_MACROS
            Print #f, ""  'DAvE local macro definitions'
            Print #f, uc.GetUserCode("(MAIN_General,3)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_DEFINES
            Print #f, ""  'DAvE local #define definitions'
            Print #f, uc.GetUserCode("(MAIN_General,4)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_TYPEDEFS
            Print #f, ""  'DAvE type defintions'
            Print #f, uc.GetUserCode("(MAIN_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("(MAIN_General,6)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_GLOBAL_VARIABLES
            Print #f, ""  'DAvE definitions of global variables'
            Print #f, uc.GetUserCode("(MAIN_General,7)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_EXTERNAL_PROTOTYPES
      
 
           
            Print #f, ""
            
            Print #f, ""  'DAvE external declarations of imported functions'
            Print #f, uc.GetUserCode("(MAIN_General,8)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_LOCAL_PROTOTYPES
            Print #f, ""  'DAvE declarations of internal local functions'
            Print #f, uc.GetUserCode("(MAIN_General,9)", 0)
            Print #f, ""
            Print #f, ""
    
    '------------------------------------------------------------------------------------------------
    ' MAIN_Init function
    '------------------------------------------------------------------------------------------------
        Print #f, FormatFunctionHeader(r, func, "MAIN_NAME_Init")
        
        
        Print #f, uc.GetUserCode("(MAIN_Init,1)", 0)     'USER CODE for application specific descriptions'
        Print #f, ""
                  '<--                     max. 78 characters                                -->'
        Print #f, FormatFunctionDeclaration(r, func, "MAIN_NAME_Init")
        Print #f, "{"
        
        ' Temporary varibale for delay
        ' this is added in the startup code
        'If r.v(SCU_PREFIX & "OSC_CON_OSCSS") = 1 Then
        '    Print #f, ""
        '    Print #f, "  volatile ubyte ubCounter;"
        '    Print #f, ""
        'End If
        
        Print #f, uc.GetUserCode("(MAIN_Init,2)", 2)
        Print #f, ""
    
      '------------------------------------------------------------------------------------------------
      ' Configuration of the System Clock:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of the System Clock:")

      If (r.v(SCU_PREFIX & "OSC_CON_OSCSS") = 0) Then
          d = "On Chip Osc is Selected"
      ElseIf (r.v(SCU_PREFIX & "OSC_CON_OSCSS") = 1) Then
          d = "External Osc is selected (configuration is done in the startup file 'START_XC.A51')"
      End If
      Print #f, FormatCommentsForInit(1, d)
     
      If (r.v("PLL_CTRL") = 1) Then
          d = "Direct Drive (PLL Bypass Operation)"
      ElseIf (r.v("PLL_CTRL") = 2) Then
          d = "PLL Base Mode.... VCO output frequency 150 .. 200 MHz"
      ElseIf (r.v("PLL_CTRL") = 3) Then
          d = "Prescalar Mode (VCO Bypass Operation)"
      ElseIf (r.v("PLL_CTRL") = 4) Then
          v = GetValueNDIV(r)
          d = "PLL Mode, NDIV =  " & v
      End If
      Print #f, FormatCommentsForInit(1, d)

      d = "input frequency is " & r.v("MAIN_FOSC") & " MHz"
      Print #f, FormatCommentsForInit(1, d)
      
    d = ""
    Print #f, ""
    If (r.v(SCU_PREFIX & "OSC_CON_OSCSS") = 1) Then
        ' write the code to configure external oscillator
        ' This code is added in the startup file
'        Print #f, "  // Select the External Oscillator"
'        Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "1, SST2);", "", "", "switch to page1")
'        Print #f, FormatCodeStatemente(2, SCU_PREFIX & "PLL_CON", "|=", FormatTextOutput(&H4, "H2"), "OSCDISC = 1")
'        Print #f, FormatCodeStatemente(2, SCU_PREFIX & "OSC_CON", "|=", FormatTextOutput(&H4, "H2"), "OSCSS = 1")
'        Print #f, FormatCodeStatemente(2, SCU_PREFIX & "OSC_CON", "&=", FormatTextOutput(&HF7, "H2"), "XPD = 0       power xtal")
'        Print #f, ""
'        Print #f, "  for(ubCounter=0;ubCounter<10;ubCounter++) {}; // delay around 1.5 ms"
'        Print #f, ""
'        Print #f, "  do"
'        Print #f, "  {"
'        Print #f, FormatCodeStatemente(4, SCU_PREFIX & "OSC_CON", "|=", FormatTextOutput(&H2, "H2"), "ORDRES = 1")
'        Print #f, "    while ((" & SCU_PREFIX & "OSC_CON & 0x2)) {}; // wait until ORDRES = 0"
'        Print #f, "  }while (!(" & SCU_PREFIX & "OSC_CON & 0x1));     // wait until OSCR = 1"
'        Print #f, ""
'
'        Print #f, FormatCodeStatemente(2, "MAIN_vUnlockProtecReg();", "", "", "open access to protected register")
'        Print #f, FormatCodeStatemente(2, SCU_PREFIX & "PLL_CON", "=", "(" & FormatTextOutput(r.v(SCU_PREFIX & "PLL_CON_NDIV") * 16, "H2") & " + 0x8)", _
'                                      "Write NDIV, set VCOBYP, reset OSCDISC")
'        Print #f, FormatCodeStatemente(2, "MAIN_vlockProtecReg();", "", "", "close access to protected register")
'        Print #f, FormatCodeStatemente(2, SCU_PREFIX & "PLL_CON", "|=", "0x02", "")
'        Print #f, "  while (!(" & SCU_PREFIX & "PLL_CON & 0x1)){}; // Wait until LOCK = 1"
'        Print #f, FormatCodeStatemente(2, SCU_PREFIX & "PLL_CON", "&=", "0xF7", "Reset VCOBYP")

        sPageUsed = False
    Else
      If (r.v("PLL_CTRL") = 4) Then
        Print #f, ""
          v = GetValueNDIV(r)
        If (v <> 20) Then
            Print #f, "  // changing PLL parameters"
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "1, noSST);", "", "", "switch to page1")
            Print #f, "  while((" & SCU_PREFIX & "OSC_CON & 0x01) == 0){};  //  Wait until OSCR = 1"
            Print #f, FormatCodeStatemente(2, "MAIN_vUnlockProtecReg();", "", "", "open access to protected register")
            Print #f, FormatCodeStatemente(2, SCU_PREFIX & "PLL_CON", "=", "(" & FormatTextOutput(r.v(SCU_PREFIX & "PLL_CON_NDIV") * 16, "H2") & " + 0x8)", _
                                      "Write NDIV, set VCOBYP, reset OSCDISC")
            Print #f, FormatCodeStatemente(2, "MAIN_vlockProtecReg();", "", "", "close access to protected register")
            Print #f, FormatCodeStatemente(2, SCU_PREFIX & "PLL_CON", "|=", "0x02", "")
            Print #f, "  while (!(" & SCU_PREFIX & "PLL_CON & 0x1)){}; // Wait until LOCK = 1"
            Print #f, FormatCodeStatemente(2, SCU_PREFIX & "PLL_CON", "&=", "0xF7", "Reset VCOBYP")
            
            sPageUsed = True
        End If
       End If
    End If
    
      ' generate this only when the value is changed
    ''If ((r.v(SCU_PREFIX & "CMCON_CLKREL") <> 0) Or (r.v(SCU_PREFIX & "CMCON_FCCFG") = 1)) Then
    ''If (r.sfr(SCU_PREFIX & "CMCON") <> 0) Then
        Print #f, ""
        v = r.sfr(SCU_PREFIX & "CMCON")
        If (r.v(SCU_PREFIX & "CMCON_FCCFG") = 1) Then
            Print #f, "  //   FCLK runs at 2 times the frequency of PCLK."
        End If
   If (sPageUsed = False) Then
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "1, noSST);", "", "", "switch to page1")
        End If
        Print #f, ""
        Print #f, FormatCodeStatemente(2, SCU_PREFIX & "CMCON", "=", FormatTextOutput(v, "H2"), "load Clock Control Register")
       sPageUsed = True
       Print #f, ""
        'End If
    
    If (sPageUsed = True) Then
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, noSST);", "", "", "switch to page0")
            Print #f, ""
    End If
  
            Print #f, ""
            Print #f, FormatCommentsForSource(2, 1, "*********************************************************************************")
            Print #f, FormatCommentsForSource(2, 1, "Note : All peripheral related IO configurations are done in the respective peripheral modules (alternate functions selection)")
            Print #f, FormatCommentsForSource(2, 1, "*********************************************************************************")
            Print #f, ""
            
          '------------------------------------------------------------------------------------------------
          For Each mo In dc
              If (mo.ModuleName <> "MAIN") Then
                If (r.v(mo.ModuleName & "_USE_Init") = 1) Then
                    Print #f, FormatComments(2, 1, "", "", "Initialization of module '" & mo.ModuleDescription & "'")
                    Print #f, "  " & r.v(mo.ModuleName & "_NAME_INIT") & "();"
                    Print #f, ""
                End If
              End If
            Next mo
            If (SHINTFLAG = 1) Then
                Print #f, FormatComments(2, 1, "", "", "Initialization of  '" & "Shared interrupts" & "'")
                Print #f, "  " & r.v("SHINT_NAME_INIT") & "();"
            End If
            Print #f, ""
            '----------COCON configuration------------------------------------------------------
            'If (sPageUsed = False) Then
            'Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "1, noSST);", "", "", "switch to page1")
           'End If
            

         If (r.v("SCU_USE_ClockOut") <> 0) And (r.v(SCU_PREFIX & "COCON_COUTS") = 0) Then
          Print #f, FormatSectionHeader(2, 1, "Configuration of the CLKOUT Settings:")
          freq = r.v("MAIN_FOSC")
          d = "CLKOUT Frequency is " & freq & " MHz"
          Print #f, FormatCommentsForInit(1, d)
          Print #f, ""
          End If
         If ((r.v(SCU_PREFIX & "COCON_COREL") <> 0) Or (r.v(SCU_PREFIX & "COCON_COUTS") = 1)) Then
         'Print #f, ""
         v = r.sfr(SCU_PREFIX & "COCON")
                   '---comment generation------------
        Print #f, FormatSectionHeader(2, 1, "Configuration of the CLKOUT Settings:")

      If (Get_fsysCLKOUT(r) = "too low" Or Get_fsysCLKOUT(r) = "too high") Then
    Else
  d = "CLKOUT Frequency is " & Get_fsysCLKOUT(r) & " MHz"
   Print #f, FormatCommentsForInit(1, d)
 Print #f, ""
End If
 'end comment generation condition
' If (sPageUsed = False) Then
If (r.sfr(SCU_PREFIX & "COCON") <> 0) Then
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "1, noSST);", "", "", "switch to page1")
     '  End If
        Print #f, ""
        Print #f, FormatCodeStatemente(2, SCU_PREFIX & "COCON", "=", FormatTextOutput(v, "H2"), "Clock Output Control Register")
       
        Print #f, ""
       ' sPageUsed = True
        End If
 'If (sPageUsed = True) Then
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, noSST);", "", "", "switch to page0")
            Print #f, ""
 'End If
    End If
            '-------------end COCON config-----------------------------------------------------
         '-----------------------------CLKOUT CONFIG-----------------------------------------------------
          If r.v("SCU_USE_ClockOut") = 1 Then
              Print #f, FormatComments(2, 1, "", "", "Pin CLKOUT (P0.0) is selected for On-Chip OSC Clock Output "); ""


              Print #f, ""
              Print #f, FormatCodeStatemente(2, "PORT_PAGE", "=", FormatTextOutput(2, "H2"), "switch to page 2")

              Print #f, "  P0_ALTSEL0      |= 0x01;      //  set AltSel0 "
              Print #f, "  P0_ALTSEL1      &= ~(ubyte)0x01;      //  set AltSel1"
              Print #f, ""
              Print #f, FormatCodeStatemente(2, "PORT_PAGE", "=", FormatTextOutput(0, "H2"), "switch to page 0")
              Print #f, "  P0_DIR          |= 0x01;      //  set P0.0 OutPut"
              Print #f, ""
'              Print #f, FormatCodeStatemente(2, "PORT_PAGE", "=", FormatTextOutput(0, "H2"), "switch to page 0")
'              Print #f, ""
          '-------------for  (P0.7)----------------------------------------------------
          ElseIf r.v("SCU_USE_ClockOut") = 2 Then
              Print #f, FormatComments(2, 1, "", "", "Pin CLKOUT (P0.7) is selected for On-Chip OSC Clock Output "); ""


              Print #f, ""
              Print #f, FormatCodeStatemente(2, "PORT_PAGE", "=", FormatTextOutput(2, "H2"), "switch to page 2")

              Print #f, "  P0_ALTSEL0      |= 0x80;      //  set AltSel0 "
              Print #f, "  P0_ALTSEL1      &= ~(ubyte)0x80;      //  set AltSel1"
              Print #f, ""
              Print #f, FormatCodeStatemente(2, "PORT_PAGE", "=", FormatTextOutput(0, "H2"), "switch to page 0")
              Print #f, "  P0_DIR          |= 0x80;      //  set P0.7 OutPut"
              Print #f, ""
'              Print #f, FormatCodeStatemente(2, "PORT_PAGE", "=", FormatTextOutput(0, "H2"), "switch to page 0")
'              Print #f, ""
          '-------------end  (P0.7)----------------------------------------------------
          '-------------for  (P0.7) AND (P0.0)----------------------------------------------------
          ElseIf r.v("SCU_USE_ClockOut") = 3 Then
              Print #f, FormatComments(2, 1, "", "", "Pin CLKOUT (P0.0) and (P0.7) is selected for On-Chip OSC Clock Output "); ""
              Print #f, ""
              Print #f, FormatCodeStatemente(2, "PORT_PAGE", "=", FormatTextOutput(2, "H2"), "switch to page 2")

              Print #f, "  P0_ALTSEL0      |= 0x81;      //  set AltSel0 "
              Print #f, "  P0_ALTSEL1      &= ~(ubyte)0x81;      //  set AltSel1"
              Print #f, ""
              Print #f, FormatCodeStatemente(2, "PORT_PAGE", "=", FormatTextOutput(0, "H2"), "switch to page 0")
              Print #f, "  P0_DIR          |= 0x81;      //  set P0.0 and P0.7 as OutPut"
              Print #f, ""
'              Print #f, FormatCodeStatemente(2, "PORT_PAGE", "=", FormatTextOutput(0, "H2"), "switch to page 0")
'              Print #f, ""

          '-------------end  (P0.7) AND (P0.0)----------------------------------------------------
          End If

          '----------------------------end CLKOUT----------------------------------------------------------
          
         'Interrupt Priority.....
            d = "Interrupt Priority"
            Print #f, FormatCommentsForSource(2, 0, d)
            Print #f, ""
            
            v = r.sfr(CORE_PREFIX & "IP")
            Print #f, FormatCodeStatemente(2, CORE_PREFIX & "IP", "=", FormatTextOutput(v, "H2"), "load Interrupt Priority Register")
            
            v = r.sfr(CORE_PREFIX & "IPH")
            Print #f, FormatCodeStatemente(2, CORE_PREFIX & "IPH", "=", FormatTextOutput(v, "H2"), "load Interrupt Priority High Register")
            
            v = r.sfr(CORE_PREFIX & "IP1")
            Print #f, FormatCodeStatemente(2, CORE_PREFIX & "IP1", "=", FormatTextOutput(v, "H2"), "load Interrupt Priority 1 Register")
            
            v = r.sfr(CORE_PREFIX & "IPH1")
            Print #f, FormatCodeStatemente(2, CORE_PREFIX & "IPH1", "=", FormatTextOutput(v, "H2"), "load Interrupt Priority 1 High Register")
            Print #f, ""
            
            Print #f, ""
            Print #f, uc.GetUserCode("(MAIN_Init,3)", 2)
            Print #f, ""
            
          If (r.v(SCU_PREFIX & "SYSCON0_IMODE") = 1) Then
            d = "Interrupt structure 2 mode 1 is selected."
             Print #f, FormatCommentsForSource(2, 0, d)
             Print #f, ""
             Print #f, "  " & SCU_PREFIX & "SYSCON0" & " |= " & _
                 FormatTextOutput(BitMask(r, SCU_PREFIX & "SYSCON0", "IMODE"), "H2") & ";"
          Else
            d = "Interrupt structure 2 mode 0 is selected."
            Print #f, FormatCommentsForSource(2, 2, d)
          End If
            Print #f, ""
          If (r.v("ISR_CHOICE") = 1) Then
            d = "Interrupt service routine choice 1 is selected."
             Print #f, FormatCommentsForSource(2, 0, d)
             Print #f, ""
          ElseIf (r.v("ISR_CHOICE") = 2) Then
             d = "Interrupt service routine choice 2 is selected."
             Print #f, FormatCommentsForSource(2, 2, d)
          End If
            Print #f, ""
          If (r.v(CORE_PREFIX & "IEN0_EA") = 1) Then
            d = "globally enable interrupts"
            Print #f, FormatCommentsForSource(2, 0, d)
            
            v = r.v(CORE_PREFIX & "IEN0_EA")
            Print #f, FormatCodeStatemente(2, "EA", "=", FormatTextOutput(v, "F"), "")
          
          Else
            d = "globally disable interrupts"
            Print #f, FormatCommentsForSource(2, 2, d)
          End If
    
            
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("MAIN_NAME_Init")
            Print #f, ""
            Print #f, ""
           '************************* To Generate SHARED ISRs in MAIN.C ***************************
'           If (((r.v("T2_USE_IsrTmr") = 1) And (r.v("T2_USE_Init") = 1)) Or ((r.v("BRG_Interrupt_Select") = 1) And (r.v("UART_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN0") = 1) And (r.v("CAN_USE_Init") = 1))) Then
'              If (Not Main_vXINTR5Isr(r, f, func, uc)) Then Exit Function
'           End If
'           If (((r.v("ADC_USE_Isr") = 1) And (r.v("ADC_USE_Init") = 1)) Or (((r.v("CAN_USE_SRN1") = 1) Or (r.v("CAN_USE_SRN2") = 1)) And (r.v("CAN_USE_Init") = 1))) Then
'              If (Not Main_vXINTR6Isr(r, f, func, uc)) Then Exit Function
'           End If
'           If (((r.v("T21_USE_IsrTmr") = 1) And (r.v("T21_USE_Init") = 1)) Or ((r.v("IR_USE_IsrExt2") = 1) And (r.v("IR_USE_Init") = 1)) Or ((r.v("UART1_USE_Isr") = 1) And (r.v("UART1_USE_Init") = 1)) Or ((r.v("BRG1_Interrupt_Select") = 1) And (r.v("UART1_USE_Init") = 1))) Then
'              If (Not Main_vXINTR8Isr(r, f, func, uc)) Then Exit Function
'           End If
'           If (((r.v("IR_USE_IsrExt3") = 1) And (r.v("IR_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN3") = 1) And (r.v("CAN_USE_Init") = 1))) Then
'              If (Not ((r.v(SCU_PREFIX & "EXICON0_EXINT3") = 3) And (r.v(SCU_PREFIX & "EXICON1_EXINT4") = 3) And (r.v(SCU_PREFIX & "EXICON1_EXINT5") = 3) And (r.v(SCU_PREFIX & "EXICON1_EXINT6") = 3))) Or ((r.v("CAN_USE_SRN3") = 1) And (r.v("CAN_USE_Init") = 1)) Then
'                 If (Not Main_vXINTR9Isr(r, f, func, uc)) Then Exit Function
'              End If
'           End If
'           If (((r.v(CORE_PREFIX & "IEN1_ECCIP0") = 1) And (r.v("CCU6_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN4") = 1) And (r.v("CAN_USE_Init") = 1))) Then
'              If (Not Main_vXINTR10Isr(r, f, func, uc)) Then Exit Function
'           End If
'           If (((r.v(CORE_PREFIX & "IEN1_ECCIP1") = 1) And (r.v("CCU6_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN5") = 1) And (r.v("CAN_USE_Init") = 1))) Then
'              If (Not Main_vXINTR11Isr(r, f, func, uc)) Then Exit Function
'           End If
'           If (((r.v(CORE_PREFIX & "IEN1_ECCIP2") = 1) And (r.v("CCU6_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN6") = 1) And (r.v("CAN_USE_Init") = 1))) Then
'              If (Not Main_vXINTR12Isr(r, f, func, uc)) Then Exit Function
'           End If
'           If (((r.v(CORE_PREFIX & "IEN1_ECCIP3") = 1) And (r.v("CCU6_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN7") = 1) And (r.v("CAN_USE_Init") = 1))) Then
'              If (Not Main_vXINTR13Isr(r, f, func, uc)) Then Exit Function
'           End If
          '************************* To Generate SHARED ISRs in MAIN.C ***************************
            
    '------------------------------------------------------------------------------------------------
    ' main function
    '------------------------------------------------------------------------------------------------
            Print #f, FormatFunctionHeader(r, func, "MAIN_NAME_Main")
            Print #f, uc.GetUserCode("(MAIN_Main,1)", 0)     'USER CODE for application specific descriptions'
            Print #f, ""
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "MAIN_NAME_Main")
            Print #f, "{"
            Print #f, uc.GetUserCode("(MAIN_Main,2)", 2)
            Print #f, ""

            Print #f, "  " & r.v("MAIN_NAME_Init") & "();"
            Print #f, ""
            Print #f, uc.GetUserCode("(MAIN_Main,3)", 2)
            Print #f, ""
            Print #f, "  while(1)"
            Print #f, "  {"
            Print #f, ""
            Print #f, uc.GetUserCode("(MAIN_Main,4)", 2)
            Print #f, ""
            Print #f, "  }"
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("MAIN_NAME_Main")
            Print #f, ""
            Print #f, ""
    
    '------------------------------------------------------------------------------------------------
            Print #f, uc.GetUserCode("(MAIN_General,10)", 0)
            Print #f, ""
            
    'Close file
    Close f
    
    MainGenerateC = True
    Exit Function
    
MainGenerateCError: 's
    Call Errorbox("Can not generate main sourcefile", Error$)
    Close f
    Exit Function
End Function

'*****************************************************************************
'Description:
'        This function generates the H-File for this module.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the Project data object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function MainGenerateH(r As ProjectData) As Boolean
    On Error GoTo MainGenerateHError
    
    Dim f As Variant
    Dim d As String
    Dim dc As DAvECollection
    Dim uc As UserCode
    Dim func As FunctionClass
    Dim mo As ModuleClass

    Dim lSfrIndex As Long
    Dim lBitIndex As Long
    Dim lAddress As Long
    Dim iIndex As Integer
    Dim sSfrName As String
    Dim sBitName As String
    Dim cPorts As DAvECollection
    Dim Port As Port
    Dim bPrintSfr As Boolean
   
    MainGenerateH = 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("MAIN_HFILE"))
    
    'open MAIN.h
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v("MAIN_HFILE") For Output As f
    
    d = ""
    d = d & Rtf.RtfDescriptionFirstLine & "This is the include header file for all other modules."
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc("MAIN").ModuleDescription, r.v("MAIN_HFile"), r.v("MAIN_CPU_TYPE"), (r.v("MAIN_COMPILER")), d)
    
            Print #f, uc.GetUserCode("(MAIN_Header,1)", 0)
            Print #f, ""
            Print #f, Rtf.GenerateNote((r.v("MAIN_Notes")))
            Print #f, ""
            Print #f, "#ifndef _" & StripExtension(r.v("MAIN_HFILE")) & "_H_"
            Print #f, "#define _" & StripExtension(r.v("MAIN_HFILE")) & "_H_"
            Print #f, ""
            Print #f, SECTION_PROJECT_INCLUDES
            Print #f, ""  'DAvE includes'
            Print #f, uc.GetUserCode("(MAIN_Header,2)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_MACROS
            Print #f, ""  'DAvE global macro definitions; not used as functions'
                Print #f, "// Please ensure that SCU_PAGE is switched to Page 1 before using these macros"
                Print #f, "#define MAIN_vUnlockProtecReg() " & SCU_PREFIX & "PASSWD = 0x9B "
                Print #f, "#define MAIN_vlockProtecReg()   " & SCU_PREFIX & "PASSWD = 0xAB"
                Print #f, ""
            Print #f, ""
            If (r.v("MAIN_COMPILER") = "SDCC") Then
                Print #f, "// Macros used for push and pop operation of SYSCON0"
                Print #f, "#define pushsyscon() " & " { _asm push SDCC_SYSCON0 _endasm;}"
                Print #f, "#define popsyscon()  " & " { _asm pop SDCC_SYSCON0  _endasm;}"
                Print #f, ""
                Print #f, ""
                Print #f, ""
            End If
            Print #f, uc.GetUserCode("(MAIN_Header,3)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_DEFINES
            Print #f, ""  'DAvE global #define definitions'
            Print #f, uc.GetUserCode("(MAIN_Header,4)", 0)
            Print #f, ""
            Print #f, "#define bool  bit"
            Print #f, "#define ulong unsigned long"
            Print #f, "#define uword unsigned int"
            Print #f, "#define ubyte unsigned char"
''            If (r.v("MAIN_COMPILER") = "SDCC") Then   /** BUG fixed by Ashish on 16.05.07
''              Print #f, "#define bit char"
''            End If
            Print #f, ""
            Print #f, ""
            
            If (r.v("MAIN_COMPILER") = "Keil") Then
                 Print #f, "#define KEIL"
            End If
            If (r.v("MAIN_COMPILER") = "SDCC") Then
    '             Print #f, "#ifndef SDCC"
    '             Print #f, "#define SDCC"
    '             Print #f, "#endif"
            End If
            Print #f, ""
            
            Print #f, ""
            Print #f, SECTION_GLOBAL_PROTOTYPES
            Print #f, ""  'DAvE declarations of functions which should become global'
    
            Print #f, ""
            Print #f, uc.GetUserCode("(MAIN_Header,5)", 0)
            Print #f, ""
                        
            Print #f, ""
            Print #f, FormatSectionHeader(0, 0, "Declaration of SFRs")
            Print #f, ""
            Print #f, FormatComments(0, 0, "Notes: ", "       ", _
                      "You can avoid the problem that your compiler does not yet " & _
                      "support the latest derivatives if you use the SFR definitions generated " & _
                      "by DAvE instead of those that come along with your compiler " & _
                      "(in the ""Register File"").")
            Print #f, ""
            Print #f, FormatComments(0, 0, "       ", "       ", "PORT SFRs are defined in file '" & r.v("IO_HFile") & "'.")
            Print #f, ""
        '-----------------------------------------------------------------
            Print #f, FormatComments(0, 0, "", "", "SFR byte definitions")

            For lSfrIndex = 1 To r.Regs.SfrCount
        
              r.Regs.SfrIndex = lSfrIndex
              sSfrName = r.Regs.SfrName
              
              lAddress = Val("&H" & Right(r.Regs.SfrAdress, Len(r.Regs.SfrAdress) - 2))
        
              If (lAddress <= &HFF) Then
                  If (IsMappedSfr(sSfrName)) Then
                    d = FormatComments(0, 0, "", "", "located in the mapped SFR area")
                  Else
                    d = ""
                  End If
                  If (Left(sSfrName, 3) = "CAN") Then
                     ' CAN Related
                    Else
                      Print #f, SfrDeclaration(r, sSfrName, r.Regs.SfrAdress, d)
                  End If
                  If ((Left(sSfrName, 5) = "CAN_A") Or (Left(sSfrName, 5) = "CAN_D")) Then
                      Print #f, SfrDeclaration(r, sSfrName, r.Regs.SfrAdress, d)
                  End If
              End If
        
            Next lSfrIndex
            Print #f, ""

        '-----------------------------------------------------------------
           Print #f, FormatComments(0, 0, "", "", "SFR bit definitions")

  Set cPorts = ObjectCollection("PORTS")

  sSfrName = ""

  For lBitIndex = 1 To r.Regs.BitCount
    r.Regs.BitIndex = lBitIndex

    If (r.Regs.BitSfrName <> sSfrName) Then
      sSfrName = r.Regs.BitSfrName

      If (Not r.Regs.SfrFind(sSfrName)) Then Exit Function  'in this case only SW bugs can cause an error

      lAddress = Val("&H" & Right(r.Regs.SfrAdress, Len(r.Regs.SfrAdress) - 2))

      'Applicable are only bit-addressable SFRs apart from CPU registers and Ports:
      If ((lAddress <= &HFF) And (lAddress Mod 8) = 0) Then
        bPrintSfr = True
        If (sSfrName = "ACC") Then bPrintSfr = False
        If (sSfrName = "B") Then bPrintSfr = False
        If (sSfrName = "T21_T2CON") Then bPrintSfr = False

        For Each Port In cPorts
          If (Left(sSfrName, 2) = "P" & Port.PortNumber) Then bPrintSfr = False
        Next
      Else
        bPrintSfr = False
      End If
      'CAN RELATED
          If (Left(sSfrName, 3) = "CAN") Then bPrintSfr = False

      If (bPrintSfr) Then
        ' Print a headline for each SFR:
        If (IsMappedSfr(sSfrName)) Then
          d = " (located in the mapped SFR area)"
        Else
          d = ""
        End If
        If (sSfrName = "T2_T2CON") Then
          d = " and T21_T2CON"
        End If
            Print #f, ""
            Print #f, FormatComments(0, 0, "", "", sSfrName & d)
      End If
    End If

    If (bPrintSfr) Then
      sBitName = Mid$(r.Regs.BitName, Len(sSfrName) + 2)
            Print #f, SfrBitDeclaration(r, sBitName, FormatTextOutput(lAddress + r.Regs.BitPos, "H2"), "")
    End If
  Next lBitIndex
  
  If (r.v("MAIN_COMPILER") = "Keil") Then
' following code added for sfr16 reg defnition
            Print #f, ""
            Print #f, ""
            Print #f, FormatComments(0, 0, "", "", "Definition of the 16-bit SFR")
            Print #f, FormatComments(0, 0, "", "", "sfr16 data type to access two 8-bit SFRs as a single 16-bit SFR.")
            Print #f, ""
            For lSfrIndex = 1 To r.Regs.SfrCount
                r.Regs.SfrIndex = lSfrIndex
                sSfrName = r.Regs.SfrName
                lAddress = Val("&H" & Right(r.Regs.SfrAdress, Len(r.Regs.SfrAdress) - 2))
                  If (Left(sSfrName, 7) = "CAN_ADL") Then
                      Print #f, FormatCodeStatemente(0, "sfr16 " & sSfrName & "H =", "", r.Regs.SfrAdress, "16-bit Address")
                  End If
                  If (Left(sSfrName, 9) = "CAN_DATA0") Then
                      Print #f, FormatCodeStatemente(0, "sfr16 " & sSfrName & "1 =", "", r.Regs.SfrAdress, "16-bit Address")
                  End If
                  If (Left(sSfrName, 9) = "CAN_DATA2") Then
                      Print #f, FormatCodeStatemente(0, "sfr16 " & sSfrName & "3 =", "", r.Regs.SfrAdress, "16-bit Address")
                  End If
                  If ((sSfrName = "CCU6_T12PRL") Or (sSfrName = "CCU6_T13PRL")) Then
                      Print #f, FormatCodeStatemente(0, "sfr16 " & sSfrName & "H =", "", r.Regs.SfrAdress, "16-bit Address")
                  End If
                  If ((sSfrName = "CCU6_T12L") Or (sSfrName = "CCU6_T13L")) Then
                      Print #f, FormatCodeStatemente(0, "sfr16 " & sSfrName & "H =", "", r.Regs.SfrAdress, "16-bit Address")
                  End If
                  If ((Left(sSfrName, 8) = "CCU6_CC6") And (Right(sSfrName, 1) = "L")) Then
                      Print #f, FormatCodeStatemente(0, "sfr16 " & sSfrName & "H =", "", r.Regs.SfrAdress, "16-bit Address")
                  End If
                  If ((Left(sSfrName, 6) = "T2_T2L") Or (Left(sSfrName, 7) = "T21_T2L")) Then
                      Print #f, FormatCodeStatemente(0, "sfr16 " & sSfrName & "H =", "", r.Regs.SfrAdress, "16-bit Address")
                  End If
                  If ((Left(sSfrName, 7) = "T2_RC2L") Or (Left(sSfrName, 8) = "T21_RC2L")) Then
                      Print #f, FormatCodeStatemente(0, "sfr16 " & sSfrName & "H =", "", r.Regs.SfrAdress, "16-bit Address")
                  End If
            Next lSfrIndex
   End If
 
  If (r.v("MAIN_COMPILER") = "SDCC") Then
' following code added for push/pop sfr reg defnition
    Print #f, ""
    Print #f, FormatComments(0, 0, "", "", "Definition of push/pop SFR")
    Print #f, ""
    For lSfrIndex = 1 To r.Regs.SfrCount
        r.Regs.SfrIndex = lSfrIndex
        sSfrName = r.Regs.SfrName
        lAddress = Val("&H" & Right(r.Regs.SfrAdress, Len(r.Regs.SfrAdress) - 2))
          If (Left(sSfrName, 7) = "CAN_ADL") Then
              Print #f, "#define SDCC_" & sSfrName; " "; r.Regs.SfrAdress & "    // " & sSfrName & " Address"
          End If
          If (Left(sSfrName, 7) = "CAN_ADH") Then
              Print #f, "#define SDCC_" & sSfrName; " "; r.Regs.SfrAdress & "    // " & sSfrName & " Address"
          End If
          If (Left(sSfrName, 9) = "CAN_DATA0") Then
              Print #f, "#define SDCC_" & sSfrName; " "; r.Regs.SfrAdress & "  // " & sSfrName & " Address"
          End If
          If (Left(sSfrName, 9) = "CAN_DATA1") Then
              Print #f, "#define SDCC_" & sSfrName; " "; r.Regs.SfrAdress & "  // " & sSfrName & " Address"
          End If
          If (Left(sSfrName, 9) = "CAN_DATA2") Then
              Print #f, "#define SDCC_" & sSfrName; " "; r.Regs.SfrAdress & "  // " & sSfrName & " Address"
          End If
          If (Left(sSfrName, 9) = "CAN_DATA3") Then
              Print #f, "#define SDCC_" & sSfrName; " "; r.Regs.SfrAdress & "  // " & sSfrName & " Address"
          End If
          If (Left(sSfrName, 7) = "SYSCON0") Then
              Print #f, "#define SDCC_" & sSfrName; " "; r.Regs.SfrAdress & "    // " & sSfrName & " Address"
          End If
            
          If (Left(sSfrName, 8) = "SCU_PAGE") Then
              Print #f, "#define SDCC_" & sSfrName; " "; r.Regs.SfrAdress & "    // " & sSfrName & " Address"
          End If
     Next lSfrIndex
   End If
 
            Print #f, ""
            Print #f, FormatComments(0, 0, "", "", "Definition of the PAGE SFR")
            Print #f, ""
            
            Print #f, FormatComments(0, 0, "", "", "PORT_PAGE")
            For iIndex = 0 To 3
                Print #f, "#define " & PORT_PAGE_NAME & iIndex & " PORT_PAGE=" & iIndex & " // PORT_PAGE postfix"
            Next
            
            Print #f, ""
            Print #f, FormatComments(0, 0, "", "", "ADC_PAGE")
            For iIndex = 0 To 6
                Print #f, "#define " & ADC_PAGE_NAME & iIndex & " ADC_PAGE=" & iIndex & " // ADC_PAGE postfix"
            Next
            
            Print #f, ""
            Print #f, FormatComments(0, 0, "", "", "SCU_PAGE")
            For iIndex = 0 To 3
                Print #f, "#define " & SCU_PAGE_NAME & iIndex & " SCU_PAGE=" & iIndex & " // SCU_PAGE postfix"
            Next
            
            Print #f, ""
            Print #f, FormatComments(0, 0, "", "", "CCU6_PAGE")
            For iIndex = 0 To 3
                Print #f, "#define " & CCU_PAGE_NAME & iIndex & " CCU6_PAGE=" & iIndex & " // CCU6_PAGE postfix"
            Next
            
'            Print #f, ""
'            Print #f, FormatComments(0, 0, "", "", "FLASH_PAGE")
'            For iIndex = 0 To 2
'                Print #f, "#define " & FLASH_PAGE_NAME & iIndex & " FLASH_PAGE=" & iIndex & " // FLASH_PAGE postfix"
'            Next
            
            Print #f, ""
            Print #f, "#define SST0  0x80        // Save SFR page to ST0"
            Print #f, "#define RST0  0xC0        // Restore SFR page from ST0"
            Print #f, "#define SST1  0x90        // Save SFR page to ST1"
            Print #f, "#define RST1  0xD0        // Restore SFR page from ST1"
            Print #f, "#define SST2  0xA0        // Save SFR page to ST2"
            Print #f, "#define RST2  0xE0        // Restore SFR page from ST2"
            Print #f, "#define SST3  0xB0        // Save SFR page to ST3"
            Print #f, "#define RST3  0xF0        // Restore SFR page from ST3"
            Print #f, "#define noSST 0x00        // Switch page without saving"
            
            Print #f, ""
            Print #f, "#define SFR_PAGE(pg,op) pg+op"
            
            Print #f, ""
            Print #f, FormatComments(0, 0, "", "", "SYSCON0_RMAP")
            Print #f, FormatComments(0, 0, "", "", "The access to the mapped SFR area is enabled.")
            Print #f, "#define SET_RMAP() " & SCU_PREFIX & "SYSCON0" & " |= " & _
                 FormatTextOutput(BitMask(r, SCU_PREFIX & "SYSCON0", "RMAP"), "H2")
            Print #f, ""
            Print #f, FormatComments(0, 0, "", "", "The access to the standard SFR area is enabled.")
            Print #f, "#define RESET_RMAP() " & SCU_PREFIX & "SYSCON0" & " &= ~" & _
                 FormatTextOutput(BitMask(r, SCU_PREFIX & "SYSCON0", "RMAP"), "H2")
            '------------------------ Added for Power Saving Module ------------------------------
            Print #f, ""
            Print #f, ""
'            Print #f, "#define " & SCU_PAGE_NAME & "  SCU_PAGE" & " // SCU_PAGE"
'            Print #f, ""
            
            If (r.v("MAIN_COMPILER") = "Keil") Then
             
             Print #f, "#define " & SCU_PAGE_NAME & "  SCU_PAGE" & " // SCU_PAGE"
             Print #f, ""
            
             Print #f, "#define STR_PAGE(pg,op) " & " { _push_(op); \"
             Print #f, "  " & " pg ; }"
             Print #f, ""
             Print #f, "#define RST_PAGE(op)  _pop_(op)"
            End If
            If (r.v("MAIN_COMPILER") = "SDCC") Then
               Print #f, "#define " & SCU_PAGE_NAME & "  " & "SDCC_" & "SCU_PAGE" & " // SCU_PAGE"
               Print #f, ""
               Print #f, "#define STR_PAGE(pg,op) " & " { _asm push op _endasm; \"
               Print #f, "  " & " pg ; }"
               Print #f, ""
               Print #f, "#define RST_PAGE(op)   " & " { _asm pop op _endasm;}"
            End If
            
            Print #f, ""
            Print #f, SECTION_TYPEDEFS
            Print #f, ""  'DAvE type definitions'
            Print #f, uc.GetUserCode("(MAIN_Header,6)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_IMPORTED_GLOBAL_VARIABLES
            Print #f, ""  'DAvE external declarations of imported global variables'
            Print #f, uc.GetUserCode("(MAIN_Header,7)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_GLOBAL_VARIABLES
            Print #f, ""  'DAvE definition of global variables'
            Print #f, uc.GetUserCode("(MAIN_Header,8)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_GLOBAL_PROTOTYPES
            Print #f, ""  'DAvE declarations of functions which should become global'
            Print #f, uc.GetUserCode("(MAIN_Header,9)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_INTERRUPT_VECTORS
            Print #f, ""  'DAvE interrupt vector definitions
'            If ((r.v("T2_USE_IsrTmr") = 1) Or (r.v("BRG_Interrupt_Select") = 1) Or (r.v("CAN_USE_SRN0") = 1)) Then
'               Print #f, "#define  XINTR5INT   " & IntNumber(&H2B)
'               Print #f, ""
'            End If
'            If ((r.v("ADC_USE_Isr") = 1) Or (r.v("CAN_USE_SRN1") = 1) Or (r.v("CAN_USE_SRN2") = 1)) Then
'               Print #f, "#define  XINTR6INT   " & IntNumber(&H33)
'               Print #f, ""
'            End If
'            If ((r.v("T21_USE_IsrTmr") = 1) Or (r.v(CORE_PREFIX & "IEN1_EX2") = 1) Or (r.v("UART1_USE_Isr") = 1) Or (r.v("BRG1_Interrupt_Select") = 1)) Then
'                Print #f, "#define  XINTR8INT   " & IntNumber(&H43)
'                Print #f, ""
'            End If
'            If ((r.v(CORE_PREFIX & "IEN1_EXM") = 1) Or (r.v("CAN_USE_SRN3") = 1)) Then
'               Print #f, "#define  XINTR9INT   " & IntNumber(&H4B)
'               Print #f, ""
'            End If
'           If ((r.v("CCU6_USE_NodeI0") = 1) Or (r.v("CAN_USE_SRN4") = 1)) Then
'              Print #f, "#define  XINTR10INT  " & IntNumber(&H53)
'              Print #f, ""
'           End If
'           If ((r.v("CCU6_USE_NodeI1") = 1) Or (r.v("CAN_USE_SRN5") = 1)) Then
'              Print #f, "#define  XINTR11INT  " & IntNumber(&H5B)
'              Print #f, ""
'           End If
'           If ((r.v("CCU6_USE_NodeI2") = 1) Or (r.v("CAN_USE_SRN6") = 1)) Then
'              Print #f, "#define  XINTR12INT  " & IntNumber(&H63)
'              Print #f, ""
'           End If
'           If ((r.v("CCU6_USE_NodeI3") = 1) Or (r.v("CAN_USE_SRN7") = 1)) Then
'              Print #f, "#define  XINTR13INT  " & IntNumber(&H6B)
'              Print #f, ""
'           End If
            Print #f, uc.GetUserCode("(MAIN_Header,10)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_PROJECT_INCLUDES
            Print #f, ""
            Print #f, ""
            Print #f, ""
       If (r.v("MAIN_COMPILER") = "Keil") Then
            Print #f, "#include <intrins.h>"
            Print #f, ""
'       ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
'            Print #f, "#include <xc866.h>"
'            Print #f, ""
       End If

        For Each mo In dc
          If (mo.ModuleName <> "MAIN") Then
            If (r.v(mo.ModuleName & "_USE_Init") = 1) Then
                  Print #f, "#include """ & r.v(mo.ModuleName & "_HFile") & """"
            End If
          End If
        Next mo
       If (SHINTFLAG = 1) Then
         Print #f, "#include """ & "SHARED_INT.H" & """"
       End If
       Print #f, ""
       Print #f, ""
       Print #f, ""
      '************************************************************************************
      ' For SDCC compiler ,If you have multiple source files in your project, interrupt
      'service routines can be present in any of them, but a prototype of the ISR, MUST be present or
      'included in the file that contains the function main.
      '*************************************************************************************
    
    
    
       If (r.v("MAIN_COMPILER") = "SDCC") Then
             
          Print #f, FormatComments(0, 0, "", "", "ISR prototype declaration for SDCC.")
          Print #f, ""
          If (r.v("IR_USE_IsrNMI") = 1) Then
              Print #f, "void INT_viNmiIsr(void) interrupt 14;"
          End If
          
          If (r.v("IR_USE_IsrExt0") = 1) Then
              Print #f, "void INT_viExt0(void) interrupt 0;"
          End If
        
          If (r.v("IR_USE_IsrExt1") = 1) Then
              Print #f, "void INT_viExt1(void) interrupt 2;"
          End If
          
          If (r.v("SSC_USE_ISR") = 1) Then
              Print #f, "void SSC_vIsr(void) interrupt 7;"
          End If
            
          If (r.v("T01_USE_IsrTmr0") = 1) Then
              Print #f, "void T01_viTmr0(void) interrupt 1;"
          End If
             
          If (r.v("T01_USE_IsrTmr1") = 1) Then
              Print #f, "void T01_viTmr1(void) interrupt 3;"
          End If
              
          If (r.v("UART_USE_Isr") = 1) Then
              Print #f, "void UART_viIsr(void) interrupt 4;"
          End If
             
               
          If IsCpuTypeOK(r) Then
             
             If (((r.v("ADC_USE_Isr") = 1) And (r.v("ADC_USE_Init") = 1)) Or (((r.v("CAN_USE_SRN1") = 1) Or (r.v("CAN_USE_SRN2") = 1)) And (r.v("CAN_USE_Init") = 1))) Then
                Print #f, "void SHINT_viXINTR6Isr(void) interrupt XINTR6INT;"
             End If
               
             If (((r.v("T21_USE_IsrTmr") = 1) And (r.v("T21_USE_Init") = 1)) Or ((r.v("IR_USE_IsrExt2") = 1) And (r.v("IR_USE_Init") = 1)) Or ((r.v("UART1_USE_Isr") = 1) And (r.v("UART1_USE_Init") = 1)) Or ((r.v("BRG1_Interrupt_Select") = 1) And (r.v("UART1_USE_Init") = 1))) Then
                 Print #f, "void SHINT_viXINTR8Isr(void) interrupt XINTR8INT;"
             End If
               
             If (((r.v("IR_USE_IsrExt3") = 1) And (r.v("IR_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN3") = 1) And (r.v("CAN_USE_Init") = 1))) Then
                 Print #f, "void SHINT_viXINTR9Isr(void) interrupt XINTR9INT;"
             End If
               
             If (((r.v("T2_USE_IsrTmr") = 1) And (r.v("T2_USE_Init") = 1)) Or ((r.v("BRG_Interrupt_Select") = 1) And (r.v("UART_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN0") = 1) And (r.v("CAN_USE_Init") = 1))) Then
                 Print #f, "void SHINT_viXINTR5Isr(void) interrupt XINTR5INT;"
             End If
               
             If (((r.v(CORE_PREFIX & "IEN1_ECCIP0") = 1) And (r.v("CCU6_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN4") = 1) And (r.v("CAN_USE_Init") = 1))) Then
                 Print #f, "void SHINT_viXINTR10Isr(void) interrupt XINTR10INT;"
             End If
                
             If (((r.v(CORE_PREFIX & "IEN1_ECCIP1") = 1) And (r.v("CCU6_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN5") = 1) And (r.v("CAN_USE_Init") = 1))) Then
                 Print #f, "void SHINT_viXINTR11Isr(void) interrupt XINTR11INT;"
             End If
                
             If (((r.v(CORE_PREFIX & "IEN1_ECCIP2") = 1) And (r.v("CCU6_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN6") = 1) And (r.v("CAN_USE_Init") = 1))) Then
                Print #f, "void SHINT_viXINTR12Isr(void) interrupt XINTR12INT;"
             End If
                
             If (((r.v(CORE_PREFIX & "IEN1_ECCIP3") = 1) And (r.v("CCU6_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN7") = 1) And (r.v("CAN_USE_Init") = 1))) Then
                 Print #f, "void SHINT_viXINTR13Isr(void) interrupt XINTR13INT;"
             End If
               
          Else
             If (r.v("ADC_USE_Isr") = 1) Then
                 Print #f, "void ADC_vIsr(void) interrupt 6;"
             End If
             
            
             
             If (r.v("IR_USE_IsrExt2") = 1) Then
                 Print #f, "void INT_viExt2(void) interrupt 8;"
             End If
             
             If (r.v("IR_USE_IsrExt3") = 1) Then
                 Print #f, "void INT_viExt3(void) interrupt 9;"
             End If
             
             If (r.v("T2_USE_IsrTmr") = 1) Then
                 Print #f, "void T2_viTmr(void) interrupt 5;"
             End If
             
             If ((r.v("BRG_Interrupt_Select") = 1) And (r.v(CORE_PREFIX & "IEN0_ET2") <> 1)) Then
                 Print #f, "void BRG_viIsr(void) interrupt 5;"
             End If
             
             If (r.v("CCU6_USE_NodeI0") = 1) Then
                 Print #f, "void CC6_viNodeI0(void) interrupt 10;"
             End If
             
             If (r.v("CCU6_USE_NodeI1") = 1) Then
                 Print #f, "void CC6_viNodeI1(void) interrupt 11;"
             End If
             
             If (r.v("CCU6_USE_NodeI2") = 1) Then
                 Print #f, "void CC6_viNodeI2(void) interrupt 12;"
             End If
             
             If (r.v("CCU6_USE_NodeI3") = 1) Then
                 Print #f, "void CC6_viNodeI3(void) interrupt 13;"
             End If
          End If
             
       End If
       Print #f, ""
       Print #f, ""
       Print #f, ""
     
       Print #f, ""
       Print #f, uc.GetUserCode("(MAIN_Header,11)", 0)
       Print #f, ""
       Print #f, ""
       
       Print #f, "#endif  // ifndef _" & StripExtension(r.v("MAIN_HFILE")) & "_H_"
    
    'close file
    Close f
    
    MainGenerateH = True
    Exit Function
    
MainGenerateHError:
    Call Errorbox("Can not generate main headerfile", Error$)
    Close f
    Exit Function
End Function


Function MainGenerateStartupFile(r As ProjectData) As Boolean
    On Error GoTo MainGenerateStartupErr
   
    Dim f As Variant
    
    MainGenerateStartupFile = False
    
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & "START_XC.A51" For Output As f
  
   
    Print #f, "$NOMOD51"
    Print #f, ";------------------------------------------------------------------------------"
    Print #f, ";  This file is part of the C51 Compiler package"
    Print #f, ";  Startup Code for the Infineon XC8xx devices "
    Print #f, ";  Copyright (c) 1988-2005 Keil Elektronik GmbH and Keil Software, Inc."
    Print #f, ";  Version 1.00"
    Print #f, ";"
    Print #f, ";  *** <<< Use Configuration Wizard in Context Menu >>> ***"
    Print #f, ";------------------------------------------------------------------------------"
    Print #f, ";  STARTUP.A51:  This code is executed after processor reset."
    Print #f, ";"
    Print #f, ";  To translate this file use A51 with the following invocation:"
    Print #f, ";"
    Print #f, ";     A51 STARTUP.A51"
    Print #f, ";"
    Print #f, ";  To link the modified STARTUP.OBJ file to your application use the following"
    Print #f, ";  Lx51 invocation:"
    Print #f, ";"
    Print #f, ";     Lx51 your object file list, STARTUP.OBJ  controls"
    Print #f, ";"
    Print #f, ";------------------------------------------------------------------------------"
    Print #f, ";"
    Print #f, ";<e> Use off-chip XTAL"
    Print #f, ";<i> XC8xx series runs by default from on-chip osciallator."
    Print #f, ";<i> optionally you may use a off-chip XTAL"
    Print #f, ";"
    If r.v("OSC_CON_OSCSS") = 1 Then
        Print #f, "XTAL        EQU     1   ; set to 1 External oscillator"
    Else
        Print #f, "XTAL        EQU     0   ; set to 0 On-chip oscillator"
    End If
    Print #f, ";"
    Print #f, ";---------------------------------------------------------------------"
    Print #f, ";resulting in fsys = 80MHz for XC866 and fsys = 96MHz for XC88x unless otherwise mentioned"
    Print #f, ";"
    Print #f, ";<e> Device = "" XC866 """
    
    'added by venk / changed by sheshadri
    If (Left(r.v("MAIN_CPU_TYPE"), 5) = "XC866") Then
      Print #f, "XC866_CHIP    EQU   1"
    Else
      Print #f, "XC866_CHIP    EQU   0"
    End If
    Print #f, ";"
    Print #f, "; <o> PLL N-Divider"
    Print #f, "; <0=>  N=14"
    Print #f, "; <1=>  N=15"
    Print #f, "; <2=>  N=16 (10 MHz XTAL)"
    Print #f, "; <3=>  N=17"
    Print #f, "; <4=>  N=18"
    Print #f, "; <5=>  N=19"
    Print #f, "; <6=>  N=20 (8 MHz XTAL)"
    Print #f, "; <7=>  N=21"
    Print #f, "; <8=>  N=24"
    Print #f, "; <9=>  N=28"
    Print #f, "; <10=> N=30"
    Print #f, "; <11=> N=32 (5 MHz XTAL)"
    Print #f, "; <12=> N=40"
    Print #f, "; <13=> N=42"
    Print #f, "; <14=> N=45"
    Print #f, "; <15=> N=50"
    Print #f, ";"
	If (Left(r.v("MAIN_CPU_TYPE"), 5) = "XC866") Then
		Print #f, "NDIV_XC866        EQU     " & r.v("PLL_CON_NDIV") & "   ; default 2"
	else
		Print #f, "NDIV_XC866        EQU       2   ; default 2"
	End If
    Print #f, ";</e>    "
    Print #f, ""
    Print #f, ";---------------------------------------------------------------------"
      ' If r.v("CPU_TYPE") = XC88x Then
    Print #f, ";<e> Device = "" XC88x """

    If (Left(r.v("MAIN_CPU_TYPE"), 4) = "XC88") Then
      Print #f, "XC88x_CHIP    EQU   1"
    Else
      Print #f, "XC88x_CHIP    EQU   0"
    End If
    Print #f, ";"
    Print #f, "; <o> PLL N-Divider"
    Print #f, "; <0=>  N=10"
    Print #f, "; <1=>  N=12"
    Print #f, "; <2=>  N=13"
    Print #f, "; <3=>  N=14"
    Print #f, "; <4=>  N=15"
    Print #f, "; <5=>  N=16"
    Print #f, "; <6=>  N=17"
    Print #f, "; <7=>  N=18"
    Print #f, "; <8=>  N=19"
    Print #f, "; <9=>  N=20 (9.6 MHz On-Chip XTAL)"
    Print #f, "; <10=> N=24 (8 MHz Ext XTAL)"
    Print #f, "; <11=> N=30"
    Print #f, "; <12=> N=32(6 MHz  XTAL)"
    Print #f, "; <13=> N=36"
    Print #f, "; <14=> N=40"
    Print #f, "; <15=> N=48(4 MHz  XTAL)"
    Print #f, ";"
    
    If (Left(r.v("MAIN_CPU_TYPE"), 4) = "XC88") Then
       Print #f, "NDIV_XC88x    EQU     " & r.v("PLL_CON_NDIV") & "   ; default 9"
	else
       Print #f, "NDIV_XC88x    EQU       9   ; default 9"
    End If

    Print #f, ";</e>"
    Print #f, ";</e>"
    Print #f, ";------------------------------------------------------------------------------"
    Print #f, ";"
    Print #f, ";  User-defined <h> Power-On Initialization of Memory"
    Print #f, ";"
    Print #f, ";  With the following EQU statements the initialization of memory"
    Print #f, ";  at processor reset can be defined:"
    Print #f, ";"
    Print #f, ";<o> IDATA memory length <0x0-0x100>"
    Print #f, ";<i> Note: The absolute start-address of IDATA memory is always 0"
    Print #f, ";<i>       The IDATA space overlaps physically the DATA and BIT areas."
    Print #f, "IDATALEN        EQU     0x80"
    Print #f, ";"
    Print #f, "; <o> XDATA memory start address <0x0-0xFFFF> "
    Print #f, "; <i> absolute start-address of XDATA memory"
    Print #f, "XDATASTART      EQU     0xF000     "
    Print #f, ";"
    Print #f, "; <o> XDATA memory length <0x0-0xFFFF> "
    Print #f, "; <i> length of XDATA memory in bytes."
    Print #f, "XDATALEN        EQU     0x200      "
    Print #f, ";"
    Print #f, "; <o> PDATA memory start address <0x0-0xFFFF> "
    Print #f, "; <i> absolute start-address of PDATA memory"
    Print #f, "PDATASTART      EQU     0xF000"
    Print #f, ";"
    Print #f, "; <o> PDATA memory length <0x0-0xFF> "
    Print #f, "; <i> length of PDATA memory in bytes."
    Print #f, "PDATALEN        EQU     0"
    Print #f, ";"
    Print #f, "; </h>"
    Print #f, ";------------------------------------------------------------------------------"
    Print #f, ";"
    Print #f, "; <h> Reentrant Stack Initilization"
    Print #f, ";"
    Print #f, ";  The following EQU statements define the stack pointer for reentrant"
    Print #f, ";  functions and initialized it:"
    Print #f, ";"
    Print #f, "; Stack Space for reentrant functions in the SMALL model."
    Print #f, "; <e> Activate reentrant Stack (SMALL model)"
    Print #f, "IBPSTACK        EQU     0       ; set to 1 if small reentrant is used."
    Print #f, "; <o> top of stack <0x0-0xFF> "
    Print #f, "; <i> set top of stack to highest location+1 "
    Print #f, "IBPSTACKTOP     EQU     0xFF +1     ; default 0FFH+1  "
    Print #f, "; </e>"
    Print #f, ";"
    Print #f, ";  Stack Space for reentrant functions in the LARGE model.      "
    Print #f, "; <e> Activate reentrant Stack (LARGE model)"
    Print #f, "XBPSTACK        EQU     0       ; set to 1 if large reentrant is used."
    Print #f, "; <o> top of stack <0x0-0xFFFF> "
    Print #f, "; <i> set top of stack to highest location+1."
    Print #f, "XBPSTACKTOP     EQU     0xFFFF +1   ; default 0FFFFH+1 "
    Print #f, "; </e>"
    Print #f, ";"
    Print #f, ";  Stack Space for reentrant functions in the COMPACT model.    "
    Print #f, "; <e> Activate reentrant Stack (COMPACT model)"
    Print #f, "PBPSTACK        EQU     0       ; set to 1 if compact reentrant is used."
    Print #f, ";"
    Print #f, "; <o> top of stack <0x0-0xFF> "
    Print #f, "; <i> set top of stack to highest location+1."
    Print #f, "PBPSTACKTOP     EQU     0xFF +1     ; default 0FFH+1  "
    Print #f, "; </e>"
    Print #f, "; </h>"
    Print #f, ";------------------------------------------------------------------------------"
    Print #f, ";"
    Print #f, ";  <e>Set Memory Page for Using the Compact Model with 64 KByte xdata RAM"
    Print #f, ";"
    Print #f, ";  <i>Define the xdata page used for pdata variables. "
    Print #f, ";  <i>PPAGE must conform with the PPAGE set in the linker invocation."
    Print #f, ";"
    Print #f, "; Enable pdata memory page initalization"
    Print #f, "PPAGEENABLE     EQU     0       ; set to 1 if pdata object are used."
    Print #f, ";"
    Print #f, "; <o> PPAGE number <0x0-0xFF> "
    Print #f, "; <i> uppermost 256-byte address of the page used for pdata variables."
    Print #f, "PPAGE           EQU     0xF0"
    Print #f, ";"
    Print #f, "; </e>"
    Print #f, ";------------------------------------------------------------------------------"
    Print #f, ";Check the chip selection"
    Print #f, "IF (XTAL <> 0)"
    Print #f, "   IF ((XC866_CHIP <> 0)&& (XC88x_CHIP <> 0))"
    Print #f, "    __ERROR__  ""XC866 and XC88x chip are both selected.Please select only one"""
    Print #f, "    NDIV EQU NDIV_XC866   ;Default"
    Print #f, "    ELSEIF (XC866_CHIP <> 0)"
    Print #f, "       NDIV EQU NDIV_XC866"
    Print #f, "   ELSEIF (XC88x_CHIP <> 0)"
    Print #f, "       NDIV EQU NDIV_XC88x"
    Print #f, "   ELSE"
    Print #f, "    __WARNING__  ""Default NDIV selection is XC866"""
    Print #f, "    NDIV EQU NDIV_XC866  ;Default"
    Print #f, "    ENDIF"
   Print #f, " ENDIF                   ;End of XTAL selection"
    Print #f, ""
    Print #f, "; Standard SFR Symbols "
    Print #f, "ACC     DATA    0E0H"
    Print #f, "B       DATA    0F0H"
    Print #f, "SP      DATA    81H"
    Print #f, "DPL     DATA    82H"
    Print #f, "DPH     DATA    83H"
    Print #f, ""
    Print #f, "; XC8xx specific SFR Symbols used in STARTUP code"
    Print #f, "sfr  SCU_PAGE = 0xBF"
    Print #f, "sfr  PLL_CON  = 0xB7"
    Print #f, "sfr  OSC_CON  = 0xB6"
    Print #f, "sfr  PASSWD   = 0xBB"
    Print #f, "sfr  XADDRH   = 0xB3"
    Print #f, ""
    Print #f, ""
    Print #f, "                NAME    ?C_STARTUP"
    Print #f, ""
    Print #f, ""
    Print #f, "?C_C51STARTUP   SEGMENT   CODE"
    Print #f, "?STACK          SEGMENT   IDATA"
    Print #f, ""
    Print #f, "                RSEG    ?STACK"
    Print #f, "                DS      1"
    Print #f, ""
    Print #f, "                EXTRN CODE (?C_START)"
    Print #f, "                PUBLIC  ?C_STARTUP"
    Print #f, ""
    Print #f, "                CSEG    AT      0"
    Print #f, "?C_STARTUP:     LJMP    STARTUP1"
    Print #f, ""
    Print #f, "                RSEG    ?C_C51STARTUP"
    Print #f, ""
    Print #f, "STARTUP1:"
    Print #f, ""
    Print #f, "IF XTAL <> 0"
    Print #f, "                                    ; switch to external XTAL"
    Print #f, "                MOV     SCU_PAGE,#1"
    Print #f, "                ORL     PLL_CON, #0x08  ;VCOBYP = 1 "
    Print #f, "                ORL     PLL_CON, #0x04  ; OSCDISC = 1   _ _ _ NDIV, VCOBYP, OSCDISC, RESLD, LOCK"
    Print #f, "                ORL     OSC_CON, #0x04  ; OSCSS = 1     0, 0, 0, OSCPD, XPD, OSCSS, ORDRES, OSCR"
    Print #f, "                ANL     OSC_CON, #0xF7  ; XPD = 0       power xtal"
    Print #f, "                                "
    Print #f, "                MOV     R0,#10          ; delay around 1.5 ms"
    Print #f, "DelayXTAL:"
    Print #f, "                DJNZ    R1,$"
    Print #f, "                DJNZ    R0,DelayXTAL"
    Print #f, "                                        ; redetection of osc"
    Print #f, "OSCR_NOTSET:"
    Print #f, "                ORL     OSC_CON, #0x02  ; ORDRES = 1"
    Print #f, "WAIT_ORDRES_0:"
    Print #f, "                MOV     A,OSC_CON"
    Print #f, "                JB      ACC.1,WAIT_ORDRES_0  "
    Print #f, "                JNB     ACC.0,OSCR_NOTSET    "
    Print #f, "        "
    Print #f, "                                            ; VCOBYP to change N-Divider"
    Print #f, "                ORL     PLL_CON, #0x08      ; VCOBYP = 1"
    Print #f, "                ANL     PLL_CON, #0xFB      ; OSCDISC = 0"
    Print #f, "                MOV     PASSWD,  #0x98      ; open access to writing protected bit"
    Print #f, "                ANL     PLL_CON, #0x0F"
    Print #f, "                ORL     PLL_CON, #NDIV*16"
    Print #f, ""
    Print #f, "                ORL     PLL_CON, #0x02     ; detect PLL lock"
    Print #f, "WAIT_LOCK:"
    Print #f, "                MOV     A, PLL_CON"
    Print #f, "                JNB     ACC.0, WAIT_LOCK"
    Print #f, "                     ; reconnect to PLL"
    Print #f, "                ANL     PLL_CON, #0xF7   ; VCOBYP = 0"
    Print #f, "                MOV     SCU_PAGE,#0"
    Print #f, "ENDIF"
    Print #f, ""
    Print #f, "IF IDATALEN <> 0"
    Print #f, "                MOV     R0,#IDATALEN - 1"
    Print #f, "                CLR     A"
    Print #f, "IDATALOOP:      MOV     @R0,A"
    Print #f, "                DJNZ    R0,IDATALOOP"
    Print #f, "ENDIF"
    Print #f, ""
    Print #f, "IF XDATALEN <> 0"
    Print #f, "                MOV     DPTR,#XDATASTART"
    Print #f, "                MOV     R7,#LOW (XDATALEN)"
    Print #f, "  IF (LOW (XDATALEN)) <> 0"
    Print #f, "                MOV     R6,#(HIGH (XDATALEN)) +1"
    Print #f, "  ELSE"
    Print #f, "                MOV     R6,#HIGH (XDATALEN)"
    Print #f, "  ENDIF"
    Print #f, "                CLR     A"
    Print #f, "XDATALOOP:      MOVX    @DPTR,A"
    Print #f, "                INC     DPTR"
    Print #f, "                DJNZ    R7,XDATALOOP"
    Print #f, "                DJNZ    R6,XDATALOOP"
    Print #f, "ENDIF"
    Print #f, ""
    Print #f, "IF PPAGEENABLE <> 0"
    Print #f, "                MOV     SCU_PAGE,#3 "
    Print #f, "                MOV     XADDRH,#PPAGE    ;Page register change in DAvE!!          "
    Print #f, "                MOV     SCU_PAGE,#0 "
    Print #f, "ENDIF"
    Print #f, ""
    Print #f, "IF PDATALEN <> 0"
    Print #f, "                MOV     R0,#LOW (PDATASTART)"
    Print #f, "                MOV     R7,#LOW (PDATALEN)"
    Print #f, "                CLR     A"
    Print #f, "PDATALOOP:      MOVX    @R0,A"
    Print #f, "                INC     R0"
    Print #f, "                DJNZ    R7,PDATALOOP"
    Print #f, "ENDIF"
    Print #f, ""
    Print #f, "IF IBPSTACK <> 0"
    Print #f, "EXTRN DATA (?C_IBP)"
    Print #f, ""
    Print #f, "                MOV     ?C_IBP,#LOW IBPSTACKTOP"
    Print #f, "ENDIF"
    Print #f, ""
    Print #f, "IF XBPSTACK <> 0"
    Print #f, "EXTRN DATA (?C_XBP)"
    Print #f, ""
    Print #f, "                MOV     ?C_XBP,#HIGH XBPSTACKTOP"
    Print #f, "                MOV     ?C_XBP+1,#LOW XBPSTACKTOP"
    Print #f, "ENDIF"
    Print #f, ""
    Print #f, "IF PBPSTACK <> 0"
    Print #f, "EXTRN DATA (?C_PBP)"
    Print #f, "                MOV     ?C_PBP,#LOW PBPSTACKTOP"
    Print #f, "ENDIF"
    Print #f, ""
    Print #f, "                MOV     SP,#?STACK-1"
    Print #f, ""
    Print #f, "                LJMP    ?C_START"
    Print #f, ""
    Print #f, "; Overwrite ?C?DPSEL address for XC866 Device"
    Print #f, "PUBLIC ?C?DPSEL"
    Print #f, "?C?DPSEL DATA 0A2H   ; DPSEL address for Mentor M8051EW"
    Print #f, ""
    Print #f, " END"
    Print #f, ""

    
    Close f
    
    MainGenerateStartupFile = True
    Exit Function
    

MainGenerateStartupErr:
    Call Errorbox("Can not generate Startup file", Error$)
    Close f
    Exit Function
End Function

Function MainGenerateStartupFile_SDCC(r As ProjectData) As Boolean
    On Error GoTo MainGenerateStartup_SDCCErr
   
    Dim f As Variant
    
    MainGenerateStartupFile_SDCC = False
    
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & "startupxc886.s" For Output As f
     
    Print #f, ";------------------------------------------------------------------------------"
    Print #f, ";  This file is part of the DAvE SDCC Compiler package"
    Print #f, ";  Startup Code for the Infineon XC8xx devices "
    Print #f, ";  Copyright (c) ."
    Print #f, ";  Version 1.00"
    Print #f, ";"
    Print #f, ";------------------------------------------------------------------------------"
    Print #f, ";  startupxc886.s:  This code is executed after processor reset."
    Print #f, ";"
    Print #f, ";------------------------------------------------------------------------------"
    Print #f, ";"
    Print #f, "; XC8xx series runs by default from on-chip osciallator."
    Print #f, ";------------------------------------------------------------------------------"
    Print #f, ""
    Print #f, ".area startup_code(code)"
    Print #f, ""
    Print #f, ".globl __sdcc_external_startup"
    Print #f, "__sdcc_external_startup:"
    Print #f, ""
'    Print #f, "UART = 1        ; set for UART Initialisation"

    If r.v("OSC_CON_OSCSS") = 1 Then
        Print #f, "XTAL = 1   ; set to 1 External oscillator"
    Else
        Print #f, "XTAL = 0   ; set to 0 On-chip oscillator"
    End If
    
	Print #f, ""
    If (Left(r.v("MAIN_CPU_TYPE"), 5) = "XC866") Then
		Print #f, "; <o> PLL N-Divider"
		Print #f, "; <0=>  N=14"
		Print #f, "; <1=>  N=15"
		Print #f, "; <2=>  N=16(10MHz XTAL)"
		Print #f, "; <3=>  N=17"
		Print #f, "; <4=>  N=18"
		Print #f, "; <5=>  N=19"
		Print #f, "; <6=>  N=20(8MHz  XTAL)"
		Print #f, "; <7=>  N=21"
		Print #f, "; <8=>  N=24"
		Print #f, "; <9=>  N=28 "
		Print #f, "; <10=> N=30 "
		Print #f, "; <11=> N=32(5MHz  XTAL)"
		Print #f, "; <12=> N=40"
		Print #f, "; <13=> N=42"
		Print #f, "; <14=> N=45"
		Print #f, "; <15=> N=50"
		Print #f, ""
		Print #f, "     NDIV = " & r.v("PLL_CON_NDIV") & "   ; default 2"
	ElseIf (Left(r.v("MAIN_CPU_TYPE"), 4) = "XC88") Then
		Print #f, "; <o> PLL N-Divider"
		Print #f, "; <0=>  N=10"
		Print #f, "; <1=>  N=12"
		Print #f, "; <2=>  N=13"
		Print #f, "; <3=>  N=14"
		Print #f, "; <4=>  N=15"
		Print #f, "; <5=>  N=16"
		Print #f, "; <6=>  N=17"
		Print #f, "; <7=>  N=18"
		Print #f, "; <8=>  N=19"
		Print #f, "; <9=>  N=20 (9.6 MHz On-Chip XTAL)"
		Print #f, "; <10=> N=24 (8 MHz Ext XTAL)"
		Print #f, "; <11=> N=30"
		Print #f, "; <12=> N=32(6 MHz  XTAL)"
		Print #f, "; <13=> N=36"
		Print #f, "; <14=> N=40"
		Print #f, "; <15=> N=48(4 MHz  XTAL)"
		Print #f, ";"
    	Print #f, "		NDIV  = " & r.v("PLL_CON_NDIV") & "   ; default 9"
	End If
	Print #f, ""
    Print #f, "; XC8xx specific SFR Symbols used in STARTUP code"
    Print #f, "     SCU_PAGE = 0x00BF"
    Print #f, "     PLL_CON  = 0x00B7"
    Print #f, "     OSC_CON  = 0x00B6"
    Print #f, "     PASSWD   = 0x00BB"
    Print #f, ""
    Print #f, ".if XTAL                                         ; switch to external XTAL"
    Print #f, "     MOV     SCU_PAGE,#1"
    Print #f, "     ORL     PLL_CON, #0x08  ; VCOBYP=1"
    Print #f, "     ORL     PLL_CON, #0x04  ; OSCDISC = 1   _ _ _ NDIV, VCOBYP, OSCDISC, RESLD, LOCK"
    Print #f, "     ORL     OSC_CON, #0x04  ; OSCSS = 1     0, 0, 0, OSCPD, XPD, OSCSS, ORDRES, OSCR"
    Print #f, "     ANL     OSC_CON, #0xF7  ; XPD = 0       power xtal"
    Print #f, ""
    Print #f, "     MOV     R0,#10          ; delay around 1.5 ms"
    Print #f, ""
    Print #f, "DelayXTAL:"
    Print #f, "Delay1: DJNZ R1, Delay1"
    Print #f, "        DJNZ R0, DelayXTAL"
    Print #f, "                "
    Print #f, "OSCR_NOTSET:"
    Print #f, "     ORL     OSC_CON, #0x02  ; ORDRES = 1"
    Print #f, "WAIT_ORDRES_0:"
    Print #f, "     MOV A, OSC_CON"
    Print #f, "     JB      ACC.1,WAIT_ORDRES_0"
    Print #f, "     JNB     ACC.0,OSCR_NOTSET"
    Print #f, "                "
    Print #f, "     ; VCOBYP to change N-Divider"
    Print #f, "     ORL     PLL_CON, #0x08  ; VCOBYP = 1"
    Print #f, "     ANL     PLL_CON, #0xFB  ; OSCDISC = 0"
    Print #f, "     MOV     PASSWD,  #0x98  ; open access to writing protected bit"
    Print #f, "     ANL     PLL_CON, #0x0F"
    Print #f, "     ORL     PLL_CON, #NDIV*16"
    Print #f, ""
    Print #f, "     ORL     PLL_CON, #0x02  ; detect PLL lock"
    Print #f, "WAIT_LOCK:"
    Print #f, "     MOV A, PLL_CON"
    Print #f, "     JNB     ACC.0, WAIT_LOCK"
    Print #f, "                    ; reconnect to PLL"
    Print #f, "     ANL     PLL_CON, #0xF7  ; VCOBYP = 0"
    Print #f, "     MOV     SCU_PAGE,#0"
    Print #f, ".endif"
    Print #f, ""
    Print #f, ""
    Print #f, ""
    Print #f, "ret         ;End of __sdcc_external_startup"
    Print #f, ""
    Close f
    
    MainGenerateStartupFile_SDCC = True
    Exit Function

MainGenerateStartup_SDCCErr:
    Call Errorbox("Can not generate Startup file", Error$)
    Close f
    Exit Function
End Function

'************************************************************************************************************
Function SharedIntGenerateC(r As ProjectData) As Boolean
  On Error GoTo SharedIntGenerateCErr
    Dim f As Variant
    Dim uc As UserCode
    Set uc = New UserCode
    Dim func As FunctionClass
    Set func = New FunctionClass
    Dim d  As String
    Dim v As Variant
    SharedIntGenerateC = False
    'refresh the decriptions of the functions and macros
    Call RefreshFunctionDescriptions(func, r)
    'read usercode
    uc.ReadFile ((StripFilenameAndExtension(r.LoadedFile)) & "\" & "SHARED_INT.C")
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & "SHARED_INT.C" For Output As f
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = Rtf.RtfDescriptionFirstLine & "This file contains the Shared interrupt routines."
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), "Shared Interrupt Routines", "SHARED_INT.C", r.v("MAIN_CPU_TYPE"), (r.v("MAIN_COMPILER")), d)
     Print #f, uc.GetUserCode("(SHARED_INT_General,1)", 0)   'USER CODE for application specific descriptions'
     Print #f, ""
     Print #f, Rtf.GenerateNote((""))
     Print #f, ""
     Print #f, SECTION_PROJECT_INCLUDES
     Print #f, ""  'DAvE includes'
     Print #f, "#include """ & "SHARED_INT.H" & """"
     Print #f, ""
     Print #f, uc.GetUserCode("(SHARED_INT_General,2)", 0)
     Print #f, ""
     Print #f, ""
     Print #f, SECTION_MACROS
     Print #f, ""  'DAvE local macro definitions'
     Print #f, uc.GetUserCode("(SHARED_INT_General,3)", 0)
     Print #f, ""
     Print #f, ""
     Print #f, SECTION_DEFINES
     Print #f, ""  'DAvE local #define definitions'
     Print #f, uc.GetUserCode("(SHARED_INT_General,4)", 0)
     Print #f, ""
     Print #f, ""
     Print #f, SECTION_TYPEDEFS
     Print #f, ""  'DAvE type defintions'
     Print #f, uc.GetUserCode("(SHARED_INT_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("(SHARED_INT_General,6)", 0)
     Print #f, ""
     Print #f, ""
     Print #f, SECTION_GLOBAL_VARIABLES
     Print #f, ""  'DAvE definitions of global variables'
     Print #f, uc.GetUserCode("(SHARED_INT_General,7)", 0)
     Print #f, ""
     Print #f, ""
     Print #f, SECTION_EXTERNAL_PROTOTYPES
     Print #f, ""
     Print #f, ""  'DAvE external declarations of imported functions'
     Print #f, uc.GetUserCode("(SHARED_INT_General,8)", 0)
     Print #f, ""
     Print #f, ""
     Print #f, SECTION_LOCAL_PROTOTYPES
     Print #f, ""  'DAvE declarations of internal local functions'
     Print #f, uc.GetUserCode("(SHARED_INT_General,9)", 0)
     Print #f, ""
     Print #f, ""
     Print #f, FormatFunctionHeader(r, func, "SHINT_NAME_Init")
     Print #f, uc.GetUserCode("(SHINT_Init,1)", 0)     'USER CODE for application specific descriptions'
     Print #f, ""
                    '<--                     max. 78 characters                                -->'
     Print #f, FormatFunctionDeclaration(r, func, "SHINT_NAME_Init")
     Print #f, "{"
     Print #f, uc.GetUserCode("(SHINT_Init,2)", 2)
     Print #f, ""
     '------------------------------------------------------------------------------------------------
     ' Configuration of the Shared Interrupts:
     '------------------------------------------------------------------------------------------------
     Print #f, FormatSectionHeader(2, 1, "Configuration of the Shared  Interrupts:")
     If (r.v(CORE_PREFIX & "IEN0_ET2") = 1) Then
         d = "Timer 2 Interrupt is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     If (r.v("BRG_Interrupt_Select") = 1) Then
         d = "UART BRG Interrupt is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     If (r.v("CAN_USE_SRN0") = 1) Then
         d = "MultiCAN Node 0 Interrupt is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     v = 0
     If ((r.v(CORE_PREFIX & "IEN0_ET2") = 1) Or (r.v("BRG_Interrupt_Select") = 1) Or (r.v("CAN_USE_SRN0") = 1)) Then
         v = v + 2 ^ 5
         Print #f, ""
         Print #f, ""
         Print #f, FormatCodeStatemente(2, CORE_PREFIX & "IEN0", "|=", _
                FormatTextOutput(v, "H2"), _
               "load interrupt enable register 0")
         Print #f, ""
     End If
     If ((r.v("ADC_USE_Isr") = 1)) Then
         d = "ADC Interrupt is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     If ((r.v("CAN_USE_SRN1") = 1)) Then
         d = "MultiCAN Node 1 Interrupt is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     If ((r.v("CAN_USE_SRN2") = 1)) Then
         d = "MultiCAN Node 2 Interrupt is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     If ((r.v("IR_USE_IsrExt2") = 1)) Then
         d = "External Interrupt 2 is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     If ((r.v("T21_USE_IsrTmr") = 1)) Then
         d = "Timer21 Interrupt is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     If ((r.v("UART1_USE_Isr") = 1)) Then
         d = "UART1 Interrupt is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     If ((r.v("BRG1_Interrupt_Select") = 1)) Then
         d = "UART1 BRG Interrupt is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     If ((r.v("IR_USE_IsrExt3") = 1)) Then
         d = "External Interrupt 3-6 is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     If ((r.v("CAN_USE_SRN3") = 1)) Then
         d = "MultiCAN Node 3 Interrupt is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     If ((r.v(CORE_PREFIX & "IEN1_ECCIP0") = 1)) Then
         d = "CCU6 Node 0 Interrupt is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     If ((r.v("CAN_USE_SRN4") = 1)) Then
         d = "MultiCAN Node 4 Interrupt is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     If ((r.v(CORE_PREFIX & "IEN1_ECCIP1") = 1)) Then
         d = "CCU6 Node 1 Interrupt is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     If ((r.v("CAN_USE_SRN5") = 1)) Then
         d = "MultiCAN Node 5 Interrupt is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     If ((r.v(CORE_PREFIX & "IEN1_ECCIP2") = 1)) Then
         d = "CCU6 Node 2 Interrupt is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     If ((r.v("CAN_USE_SRN6") = 1)) Then
         d = "MultiCAN Node 6 Interrupt is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     If ((r.v(CORE_PREFIX & "IEN1_ECCIP3") = 1)) Then
         d = "CCU6 Node 3 Interrupt is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     If ((r.v("CAN_USE_SRN7") = 1)) Then
         d = "MultiCAN Node 7 Interrupt is Selected"
         Print #f, FormatCommentsForInit(1, d)
     End If
     v = 0
     If ((r.v("CAN_USE_SRN1") = 1) Or (r.v("CAN_USE_SRN2") = 1) Or (r.v("ADC_USE_Isr") = 1)) Then
        v = v + 2 ^ 0
     End If
     If ((r.v("T21_USE_IsrTmr") = 1) Or (r.v("IR_USE_IsrExt2") = 1) Or (r.v("UART1_USE_Isr") = 1) Or (r.v("BRG1_Interrupt_Select") = 1)) Then
        v = v + 2 ^ 2
     End If
     If ((r.v("CAN_USE_SRN3") = 1) Or (r.v("IR_USE_IsrExt3") = 1)) Then
        v = v + 2 ^ 3
     End If
     If ((r.v("CAN_USE_SRN4") = 1) Or (r.v(CORE_PREFIX & "IEN1_ECCIP0") = 1)) Then
        v = v + 2 ^ 4
     End If
     If ((r.v("CAN_USE_SRN5") = 1) Or (r.v(CORE_PREFIX & "IEN1_ECCIP1") = 1)) Then
        v = v + 2 ^ 5
     End If
     If ((r.v("CAN_USE_SRN6") = 1) Or (r.v(CORE_PREFIX & "IEN1_ECCIP2") = 1)) Then
        v = v + 2 ^ 6
     End If
     If ((r.v("CAN_USE_SRN7") = 1) Or (r.v(CORE_PREFIX & "IEN1_ECCIP3") = 1)) Then
        v = v + 2 ^ 7
     End If
     Print #f, ""
     Print #f, ""
     Print #f, FormatCodeStatemente(2, CORE_PREFIX & "IEN1", "|=", _
                FormatTextOutput(v, "H2"), _
               "load interrupt enable register 1")
     Print #f, ""
     Print #f, ""
     Print #f, uc.GetUserCode("(SHINT_Init,3)", 2)
     Print #f, ""
     Print #f, ""
     Print #f, "} " & END_OF_FUNCTION & r.v("SHINT_NAME_Init")
     Print #f, ""
     Print #f, ""
     '************************* To Generate SHARED ISRs in MAIN.C ***************************
     If (((r.v("T2_USE_IsrTmr") = 1) And (r.v("T2_USE_Init") = 1)) Or ((r.v("BRG_Interrupt_Select") = 1) And (r.v("UART_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN0") = 1) And (r.v("CAN_USE_Init") = 1))) Then
        If (Not Main_vXINTR5Isr(r, f, func, uc)) Then Exit Function
     End If
     If (((r.v("ADC_USE_Isr") = 1) And (r.v("ADC_USE_Init") = 1)) Or (((r.v("CAN_USE_SRN1") = 1) Or (r.v("CAN_USE_SRN2") = 1)) And (r.v("CAN_USE_Init") = 1))) Then
        If (Not Main_vXINTR6Isr(r, f, func, uc)) Then Exit Function
     End If
     If (((r.v("T21_USE_IsrTmr") = 1) And (r.v("T21_USE_Init") = 1)) Or ((r.v("IR_USE_IsrExt2") = 1) And (r.v("IR_USE_Init") = 1)) Or ((r.v("UART1_USE_Isr") = 1) And (r.v("UART1_USE_Init") = 1)) Or ((r.v("BRG1_Interrupt_Select") = 1) And (r.v("UART1_USE_Init") = 1))) Then
        If (Not Main_vXINTR8Isr(r, f, func, uc)) Then Exit Function
     End If
     If (((r.v("IR_USE_IsrExt3") = 1) And (r.v("IR_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN3") = 1) And (r.v("CAN_USE_Init") = 1))) Then
        If (Not ((r.v(SCU_PREFIX & "EXICON0_EXINT3") = 3) And (r.v(SCU_PREFIX & "EXICON1_EXINT4") = 3) And (r.v(SCU_PREFIX & "EXICON1_EXINT5") = 3) And (r.v(SCU_PREFIX & "EXICON1_EXINT6") = 3))) Or ((r.v("CAN_USE_SRN3") = 1) And (r.v("CAN_USE_Init") = 1)) Then
           If (Not Main_vXINTR9Isr(r, f, func, uc)) Then Exit Function
        End If
     End If
     If (((r.v(CORE_PREFIX & "IEN1_ECCIP0") = 1) And (r.v("CCU6_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN4") = 1) And (r.v("CAN_USE_Init") = 1))) Then
        If (Not Main_vXINTR10Isr(r, f, func, uc)) Then Exit Function
     End If
     If (((r.v(CORE_PREFIX & "IEN1_ECCIP1") = 1) And (r.v("CCU6_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN5") = 1) And (r.v("CAN_USE_Init") = 1))) Then
        If (Not Main_vXINTR11Isr(r, f, func, uc)) Then Exit Function
     End If
     If (((r.v(CORE_PREFIX & "IEN1_ECCIP2") = 1) And (r.v("CCU6_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN6") = 1) And (r.v("CAN_USE_Init") = 1))) Then
        If (Not Main_vXINTR12Isr(r, f, func, uc)) Then Exit Function
     End If
     If (((r.v(CORE_PREFIX & "IEN1_ECCIP3") = 1) And (r.v("CCU6_USE_Init") = 1)) Or ((r.v("CAN_USE_SRN7") = 1) And (r.v("CAN_USE_Init") = 1))) Then
        If (Not Main_vXINTR13Isr(r, f, func, uc)) Then Exit Function
     End If
    '************************* To Generate SHARED ISRs in MAIN.C ***************************
     Print #f, uc.GetUserCode("(SHARED_INT_General,10)", 0)
     Print #f, ""
     Print #f, ""
    Close f
    SharedIntGenerateC = True
    Exit Function

SharedIntGenerateCErr:
    Call Errorbox("Can not generate shared interrupt c file", Error$)
    Close f
    Exit Function
End Function

'*****************************************************************************
'Description:
'        This function generates the H-File for the shared interrupts.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the Project data object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function SharedIntGenerateH(r As ProjectData) As Boolean
  On Error GoTo SharedIntGenerateHErr
   Dim f As Variant
   Dim uc As UserCode
   Set uc = New UserCode
   Dim func As FunctionClass
   Set func = New FunctionClass
   Dim d  As String
   SharedIntGenerateH = False
   'refresh the decriptions of the functions and macros
    Call RefreshFunctionDescriptions(func, r)
    'read usercode
    uc.ReadFile ((StripFilenameAndExtension(r.LoadedFile)) & "\" & "SHARED_INT.H")
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & "SHARED_INT.H" For Output As f
           '------------------------------------------------------------------------------------------------
           ' module header
           '------------------------------------------------------------------------------------------------
           d = Rtf.RtfDescriptionFirstLine & "This is the include header file for shared interrupts."
           Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), "Shared Interrupt Routines", "SHARED_INT.H", r.v("MAIN_CPU_TYPE"), (r.v("MAIN_COMPILER")), d)

            Print #f, uc.GetUserCode("(SHARED_Header,1)", 0)
            Print #f, ""
            Print #f, Rtf.GenerateNote((""))
            Print #f, ""
            Print #f, "#ifndef _" & StripExtension("SHARED_INT.H") & "_H_"
            Print #f, "#define _" & StripExtension("SHARED_INT.H") & "_H_"
            Print #f, ""
            Print #f, SECTION_PROJECT_INCLUDES
            Print #f, ""
            Print #f, uc.GetUserCode("(SHARED_INT_Header,2)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_MACROS
            Print #f, ""
            Print #f, uc.GetUserCode("(SHARED_INT_Header,3)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_DEFINES
            Print #f, ""  'DAvE global #define definitions'
            Print #f, uc.GetUserCode("(SHARED_INT_Header,4)", 0)
            Print #f, ""
            Print #f, SECTION_GLOBAL_PROTOTYPES
            Print #f, ""  'DAvE declarations of functions which should become global'
            Print #f, "" & "void" & " SHINT_vInit(void)" & ";"
            Print #f, ""
            Print #f, ""
            Print #f, uc.GetUserCode("(SHARED_INT_Header,5)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_TYPEDEFS
            Print #f, ""  'DAvE type definitions'
            Print #f, uc.GetUserCode("(SHARED_INT_Header,6)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_IMPORTED_GLOBAL_VARIABLES
            Print #f, ""  'DAvE external declarations of imported global variables'
            Print #f, uc.GetUserCode("(SHARED_INT_Header,7)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_GLOBAL_VARIABLES
            Print #f, ""  'DAvE definition of global variables'
            Print #f, uc.GetUserCode("(SHARED_INT_Header,8)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_INTERRUPT_VECTORS
            Print #f, ""  'DAvE interrupt vector definitions
            If ((r.v("T2_USE_IsrTmr") = 1) Or (r.v("BRG_Interrupt_Select") = 1) Or (r.v("CAN_USE_SRN0") = 1)) Then
               Print #f, "#define  XINTR5INT   " & IntNumber(&H2B)
               Print #f, ""
            End If
            If ((r.v("ADC_USE_Isr") = 1) Or (r.v("CAN_USE_SRN1") = 1) Or (r.v("CAN_USE_SRN2") = 1)) Then
               Print #f, "#define  XINTR6INT   " & IntNumber(&H33)
               Print #f, ""
            End If
            If ((r.v("T21_USE_IsrTmr") = 1) Or (r.v(CORE_PREFIX & "IEN1_EX2") = 1) Or (r.v("UART1_USE_Isr") = 1) Or (r.v("BRG1_Interrupt_Select") = 1)) Then
                Print #f, "#define  XINTR8INT   " & IntNumber(&H43)
                Print #f, ""
            End If
            If ((r.v(CORE_PREFIX & "IEN1_EXM") = 1) Or (r.v("CAN_USE_SRN3") = 1)) Then
               Print #f, "#define  XINTR9INT   " & IntNumber(&H4B)
               Print #f, ""
            End If
           If ((r.v("CCU6_USE_NodeI0") = 1) Or (r.v("CAN_USE_SRN4") = 1)) Then
              Print #f, "#define  XINTR10INT  " & IntNumber(&H53)
              Print #f, ""
           End If
           If ((r.v("CCU6_USE_NodeI1") = 1) Or (r.v("CAN_USE_SRN5") = 1)) Then
              Print #f, "#define  XINTR11INT  " & IntNumber(&H5B)
              Print #f, ""
           End If
           If ((r.v("CCU6_USE_NodeI2") = 1) Or (r.v("CAN_USE_SRN6") = 1)) Then
              Print #f, "#define  XINTR12INT  " & IntNumber(&H63)
              Print #f, ""
           End If
           If ((r.v("CCU6_USE_NodeI3") = 1) Or (r.v("CAN_USE_SRN7") = 1)) Then
              Print #f, "#define  XINTR13INT  " & IntNumber(&H6B)
              Print #f, ""
           End If
            Print #f, uc.GetUserCode("(SHARED_INT_Header,9)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_PROJECT_INCLUDES
            Print #f, ""
            Print #f, ""
            Print #f, ""
            Print #f, ""
            Print #f, ""
            Print #f, ""  'DAvE includes'
            Print #f, "#include """ & r.v("MAIN_HFile") & """"
            Print #f, ""
            Print #f, ""
            Print #f, uc.GetUserCode("(SHARED_INT_Header,10)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, "#endif  // ifndef _" & StripExtension("SHARED_INT.H") & "_H_"
    Close f
    SharedIntGenerateH = True
    Exit Function

SharedIntGenerateHErr:
    Call Errorbox("Can not generate shared interrupt h file", Error$)
    Close f
    Exit Function
End Function

