'#Uses "..\include\Const.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\classes\FunctionClass.cls"
'#Uses "..\classes\InterruptClass.cls"
'#Uses "Include.inc"
'#Uses "..\include\Port.inc"

Option Explicit

'*****************************************************************************
' Module:
'        INT
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions for generating code for the
'        module 'INT'.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        1.0  ahe            27.04.00     final version for C165UTAH
'
'        ----------------------------------------------------------------------
'        2.0  ahe            15.06.00     final version for C167CS
'
'        ----------------------------------------------------------------------
'        2.1  ahe            20.10.00     final version for C167CS
'                                         - changes the USER-Gode for
'                                           # INT_viNMITrap
'                                           # INT_viSTKOFTrap
'                                           # INT_viSTKUFTrap
'                                           # INT_viClassBTrap
'             
'        ----------------------------------------------------------------------
'             ahe            26.10.00     final version for 
'                                           C167CS
'                                           C161PI
'                                           C167CR/SR r2
'
'                                         - check EXISEL
'
'        ----------------------------------------------------------------------
'        7.0  ahe            24.04.01     final version for Pegasus
'
'        7.1  ahe            08.08.02     no changes
'        7.2  ste            20.12.02     XC167CI added
'        ----------------------------------------------------------------------
'             ste            24.11.03     new function _viSOFTBRKTrap added
'                                         function _viClassBTrap changed
'
'*****************************************************************************




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


    ' write files INT.h
    If (r.v(sVarPre & "_USE_Init") = 1) Then
      If (Not INTGenerateH(r, vIndex)) Then Exit Function
	 	   If (Not r.AddFile(r.s(sVarPre & "_HFILE"))) Then Exit Function
    End if

    ' write file INT.c
    If (r.v(sVarPre & "_USE_Init") = 1) Then
      If (Not INTGenerateC(r, vIndex)) Then Exit Function
	 	   If (Not r.AddFile(r.s(sVarPre & "_CFILE"))) Then Exit Function
    End if

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


'*****************************************************************************
'Description:
'        Generates the code for the INT.c file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function INTGenerateC(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo INTGenerateCError
    
    Dim i         As Integer
    Dim f         As Variant
    Dim v         As Variant
    Dim d         As String
    Dim dc        As DAvECollection
    Dim uc        As UserCode
    Dim func      As FunctionClass
    Dim n         As String
    Dim n1        As String
    Dim sPinName  As String
    Dim iReg      As Integer
    Dim sVarPre   As String
    Dim sAlternatePinName(1 To 8) As String
    Dim saSource(0 To 7) As CourceControl

    sVarPre = "INT" & vIndex

    For i = 0 To 7
        saSource(i).saCon(0) = GetPinName("EX" & i & "IN")
    Next i

    saSource(0).saCon(1) = "P1H.3"
    saSource(0).saCon(2) = "P1H.0"

    If (DefGetValue("Name") = "XC161CJ" Or DefGetValue("Name") = "XC167CI") Then
        saSource(1).saCon(1) = "P3.1"
        saSource(1).saCon(2) = "P3.0"
    Else
        saSource(1).saCon(1) = ""
        saSource(1).saCon(2) = ""
    End If

    saSource(2).saCon(1) = "P3.11"
    saSource(2).saCon(2) = "P3.10"

    saSource(3).saCon(1) = "P3.13"
    saSource(3).saCon(2) = "P3.12"

    saSource(4).saCon(1) = "P4.7"
    saSource(4).saCon(2) = "P4.5"

    saSource(5).saCon(1) = "P4.6"
    saSource(5).saCon(2) = "P4.4"

    If (DefGetValue("Name") = "XC161CJ" Or DefGetValue("Name") = "XC167CI") Then
        saSource(6).saCon(1) = "P9.3/P7.7"
        saSource(6).saCon(2) = "P9.1/P7.5"
    Else
        saSource(6).saCon(1) = ""
        saSource(6).saCon(2) = ""
    End If

    saSource(7).saCon(1) = "P9.2/P7.6"
    saSource(7).saCon(2) = "P9.0/P7.4"

    
    INTGenerateC = False

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

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

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

    'open INT.c
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v(sVarPre & "_CFILE") For Output As f
    
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = FormatModuleDescription(GetModuleHeaderC(sVarPre))
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc(sVarPre).ModuleDescription, r.v(sVarPre & "_CFILE"), r.v("MAIN_CPU_TYPE"), r.v("MAIN_COMPILER"), d)
    
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,1)",0)    'USER CODE for application specific descriptions'
    Print #f, ""
    Print #f, Rtf.GenerateNote((r.v(sVarPre & "_Notes")))
    Print #f, ""
    Print #f, SECTION_PROJECT_INCLUDES
    Print #f, ""  'DAvE includes'
    Print #f, "#include """ & r.v("MAIN_HFile") & """"
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,2)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_MACROS
    Print #f, ""  'DAvE local macro definitions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,3)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_DEFINES
    Print #f, ""  'DAvE local #define definitions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,4)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_TYPEDEFS
    Print #f, ""  'DAvE type defintions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,5)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_IMPORTED_GLOBAL_VARIABLES
    Print #f, ""  'DAvE external declarations of imported global variables'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,6)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_GLOBAL_VARIABLES
    Print #f, ""  'DAvE definitions of global variables'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,7)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_EXTERNAL_PROTOTYPES
    Print #f, ""  'DAvE external declarations of imported functions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,8)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_LOCAL_PROTOTYPES
    Print #f, ""  'DAvE declarations of internal local functions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,9)",0)
    Print #f, ""
    Print #f, ""
    
    '------------------------------------------------------------------------------------------------
    ' INT_vInit function
    '------------------------------------------------------------------------------------------------
    n = "Init"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
      Print #f, "{"
      Print #f, uc.GetUserCode("(" & n & ",2)",2) 
      Print #f, ""
            
      For i = 0 To 7
        If (i < 4) Then
            iReg = 0
        Else
            iReg = 1
        End If
        If (r.v(sVarPre & "_Ex" & i & "_Use") = 1) Then ' external interrupt i
          '------------------------------------------------------------------------------------------------
          ' Configuration of the external interrupt i:
          '------------------------------------------------------------------------------------------------
          Print #f, FormatSectionHeader(2, 1, "Configuration of the external interrupt " & i & ":")

          
          'sPinName = " (" & GetPinName("EX" & i & "IN") & ")"
          sPinName = ""

          '----------------------------------------- EXICON_EXIxES
          Select Case (r.v("EXICON_EXI" & i & "ES"))
            Case 0
              d = "interrupt " & i & " is disabled"
              Print #f, FormatCommentsForInit(1, d)
              Print #f, ""
            Case 1
              d = "interrupt on positive edge" & sPinName
              Print #f, FormatCommentsForInit(1, d)
            Case 2
              d = "interrupt on negative edge" & sPinName
              Print #f, FormatCommentsForInit(1, d)
            Case 3
              d = "interrupt on any edge" & sPinName
              Print #f, FormatCommentsForInit(1, d)
          End Select

          If (r.v("EXICON_EXI" & i & "ES") > 0) Then
              '----------------------------------------- EXISEL_EXIiSS
              If (r.v("EXISEL" & iReg & "_EXI" & i & "SS") = 0) Then
                d = "input from pin " & saSource(i).saCon(0) & " (EX" & i & "IN)"
                Print #f, FormatCommentsForInit(1, d)
              ElseIf (r.v("EXISEL" & iReg & "_EXI" & i & "SS") = 1) Then
                d = "input from pin " & saSource(i).saCon(1)
                Print #f, FormatCommentsForInit(1, d)
              ElseIf (r.v("EXISEL" & iReg & "_EXI" & i & "SS") = 2) Then
                d = "input from pin " & saSource(i).saCon(2)
                Print #f, FormatCommentsForInit(1, d)
              ElseIf (r.v("EXISEL" & iReg & "_EXI" & i & "SS") = 3) Then
                d = "input from pin " & saSource(i).saCon(0) & " (EX" & i & "IN) ORed " & _
                    "with pin " & saSource(i).saCon(1)
                Print #f, FormatCommentsForInit(1, d)
              ElseIf (r.v("EXISEL" & iReg & "_EXI" & i & "SS") = 4) Then
                d = "input from pin " & saSource(i).saCon(0) & " (EX" & i & "IN) ANDed " & _
                    "with pin " & saSource(i).saCon(1)
                Print #f, FormatCommentsForInit(1, d)
              ElseIf (r.v("EXISEL" & iReg & "_EXI" & i & "SS") = 5) Then
                d = "input from pin " & saSource(i).saCon(1) & " ORed " & _
                    "with pin " & saSource(i).saCon(2)
                Print #f, FormatCommentsForInit(1, d)
              ElseIf (r.v("EXISEL" & iReg & "_EXI" & i & "SS") = 6) Then
                d = "input from pin " & saSource(i).saCon(1) & " ANDed " & _
                    "with pin " & saSource(i).saCon(2)
                Print #f, FormatCommentsForInit(1, d)
              ElseIf (r.v("EXISEL" & iReg & "_EXI" & i & "SS") = 7) Then
                d = "input from pin " & saSource(i).saCon(0) & " (EX" & i & "IN) ORed " & _
                    "with  pin " & saSource(i).saCon(1) & " ORed " & _
                    "with pin " & saSource(i).saCon(2)
                Print #f, FormatCommentsForInit(1, d)
              End If

          End If
          Print #f, ""
        End If
      
      Next i
          
      Print #f, "  " & r.v("MAIN_NAME_UnlockProtecReg") & "();       // unlock EXISEL0"
      v = 0
      v = v + r.sfr("EXISEL0")
      Print #f, FormatCodeStatemente(2, "EXISEL0", "=", FormatTextOutput(v, "H4"), _
        "load the external Interrupt  select register 0")            
      Print #f, ""

      Print #f, "  " & r.v("MAIN_NAME_UnlockProtecReg") & "();       // unlock EXISEL1"
      v = 0
      v = v + r.sfr("EXISEL1")
      Print #f, FormatCodeStatemente(2, "EXISEL1", "=", FormatTextOutput(v, "H4"), _
        "load the external Interrupt  select register 1")            
      Print #f, ""

      Print #f, "  " & r.v("MAIN_NAME_UnlockProtecReg") & "();       // unlock EXICON"
      v = 0
      v = v + r.sfr("EXICON")
      Print #f, FormatCodeStatemente(2, "EXICON", "=", FormatTextOutput(v, "H4"), _
        "load the external Interrupt control register")            
      Print #f, ""
      
      If (IsISRSelected(r, sVarPre, "EOPIC", n)) Then
          v = 0
          Print #f, FormatCodeStatemente(2, "PECISNC", "=", FormatTextOutput(v, "H4"), _
            "clear PEC interrupt sub node control register")            
          Print #f, ""

          v = 0
          v = v + r.sfr("PECISNC")
          Print #f, FormatCodeStatemente(2, "PECISNC", "=", FormatTextOutput(v, "H4"), _
            "load PEC interrupt sub node control register")            
          Print #f, ""

      End If
      
      '------------------------------------------------------------------------------------------------
      ' Configuration of the used INT Port Pins:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of the used " & sVarPre & " Port Pins:")
      
      For i = 0 To 7
        sAlternatePinName(i + 1) = "EX" & i & "IN"
      Next i

      Call SetAlternatePinStatements(f, r, sAlternatePinName())


      '------------------------------------------------------------------------------------------------
      ' Configuration of the used INT Interrupts:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of the used " & sVarPre & " Interrupts:")
      For i = 0 To 7
        Call GenerateIR(f, r, "CC1_CC" & i + 8 & "IC", "Ext. " & i & " _ CC1 Ch. " & i + 8)
      Next i

      Call GenerateIR(f, r, "EOPIC", "EOP")

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

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' INT_viExx function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    For i = 0 To 7  
      n = "Ex" & i
      If (IsISRSelected(r, sVarPre, "CC1_CC" & i + 8 & "IC", n)) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
        Print #f, ""
        Print #f, GenerateIsrDeclaration(r, r.v(sVarPre & "_NAME_" & n), "CC1_CC" & i + 8 & "INT")
        Print #f, "{"
        Print #f, uc.GetUserCode("(" & n & ",4)",2) 
        Print #f, ""
        Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
        Print #f, ""
        Print #f, ""
      End If
    Next i

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' INT_viExx function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "EOP"
    If (IsISRSelected(r, sVarPre, "EOPIC", n)) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, GenerateIsrDeclaration(r, r.v(sVarPre & "_NAME_" & n), "EOPINT")
      Print #f, "{"
      Print #f, uc.GetUserCode("(" & n & ",2)",2) 
      Print #f, ""

      For i = 0 To 7
          If (r.v("PECISNC_C" & i & "IE") = 1) Then
              Print #f, "  if(PECISNC_C" & i & "IR)   // end of PEC channel " & i
              Print #f, "  {"
              Print #f, "    PECISNC_C" & i & "IR = 0;"
              Print #f, "  "
              Print #f, uc.GetUserCode("(" & n & "," & 3 + i & ")",4)
              Print #f, ""
              Print #f, "  }"
              Print #f, "  "
          End If
      Next i

      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

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

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

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

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

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' INT_viClassBTrap function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ClassBTrap"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, GenerateIsrDeclaration(r, r.v(sVarPre & "_NAME_" & n), "BTRAP")
      Print #f, "{"
      Print #f, uc.GetUserCode("(" & n & ",2)",2) 
      Print #f, ""

      n1 = "UNDOPCTrap"
      Print #f, "  if(TFR_UNDOPC)    // Undefined Opcode"
      Print #f, "  {"
      Print #f, uc.GetUserCode("(" & n1 & ",1)",4) 
      Print #f, ""
      Print #f, "    TFR_UNDOPC = 0;"
      Print #f, ""
      Print #f, uc.GetUserCode("(" & n1 & ",2)",4) 
      Print #f, "  }                 // end of if Undefined Opcode"
      Print #f, ""

      n1 = "PACERRTrap"
      Print #f, "  if(TFR_PACER)     // Programm Memory Interface Access Error"
      Print #f, "  {"
      Print #f, uc.GetUserCode("(" & n1 & ",1)",4) 
      Print #f, ""
      Print #f, "    TFR_PACER = 0;"
      Print #f, ""
      Print #f, uc.GetUserCode("(" & n1 & ",2)",4) 
      Print #f, "  }                 // end of if Programm Memory Interface Access Error"
      Print #f, ""

      n1 = "PRTFLTTrap"
      Print #f, "  if(TFR_PRTFLT)    // Protection Fault"
      Print #f, "  {"
      Print #f, uc.GetUserCode("(" & n1 & ",1)",4) 
      Print #f, ""
      Print #f, "    TFR_PRTFLT = 0;"
      Print #f, ""
      Print #f, uc.GetUserCode("(" & n1 & ",2)",4) 
      Print #f, "  }                 // end of if Protection Fault"
      Print #f, ""

      n1 = "ILLOPATrap"
      Print #f, "  if(TFR_ILLOPA)    // Illegal Word Operand Access"
      Print #f, "  {"
      Print #f, uc.GetUserCode("(" & n1 & ",1)",4) 
      Print #f, ""
      Print #f, "    TFR_ILLOPA = 0;"
      Print #f, ""
      Print #f, uc.GetUserCode("(" & n1 & ",2)",4) 
      Print #f, "  }             // end of if Illegal Word Operand Access"
      Print #f, ""

      Print #f, uc.GetUserCode("(" & n & ",3)",2) 
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' INT_vConfigureFastISR function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ConfigureFastISR"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
      Print #f, "{"
      Print #f, "  ubyte ubLevel, ubGPX, ubGLVL;"
      Print #f, ""
      Print #f, "  if(ubIntLevel > 11)"
      Print #f, "  {"
      Print #f, "    ubLevel = ubIntLevel - 12;"
      Print #f, ""
      Print #f, "    if(ubGroupLevel > 3)"
      Print #f, "    {"
      Print #f, "      ubGPX = 1;"
      Print #f, "      ubGLVL = ubGroupLevel - 4;"
      Print #f, "    }"
      Print #f, "    else"
      Print #f, "    {"
      Print #f, "      ubGPX = 0;"
      Print #f, "      ubGLVL = ubGroupLevel;"
      Print #f, "    }"
      Print #f, ""
      Print #f, "    if(ubFastISR == 0)"
      Print #f, "    {"
      Print #f, "      FINT0CSP = (ubGPX << 12) + (ubLevel << 10) + (ubGLVL << 8) + ubSEG;"
      Print #f, "      FINT0ADDR = uwAddress;"
      Print #f, "    }"
      Print #f, ""
      Print #f, "    if(ubFastISR == 1)"
      Print #f, "    {"
      Print #f, "      FINT1CSP = (ubGPX << 12) + (ubLevel << 10) + (ubGLVL << 8) + ubSEG;"
      Print #f, "      FINT1ADDR = uwAddress;"
      Print #f, "    }"
      Print #f, ""
      Print #f, "  }"
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

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

    Print #f, ""
    Print #f, ""
    Print #f, uc.GetUserCode("(INT_General,10)",0)
    Print #f, ""
    
    'close file
    Close f
    
    INTGenerateC = True
    Exit Function
    
INTGenerateCError:
    Close f
    Exit Function
End Function


'*****************************************************************************
'Description:
'        Generates the code for the INT.h file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function INTGenerateH(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo INTGenerateHError

    Const MAX_CHARACTER_INT_DEFINES = 25
    
    Dim i       As Integer
    Dim f       As Variant
    Dim d       As String
    Dim dc      As DAvECollection
    Dim uc      As UserCode
    Dim func    As FunctionClass
    Dim n       As String
    Dim sVarPre As String
    Dim l       As Long
    Dim v       As Variant
    Dim cp      As DAvECollection
    Dim ir      As InterruptClass

    Dim sLeftCharacters   As String
    Dim sRightCharacters  As String
    Dim iPosition         As Integer
    Dim sKomp_Sufix       As String

    sVarPre = "INT" & vIndex
   
    INTGenerateH = False

    If (InStr(r.v("MAIN_COMPILER"), "Tasking")) Then
       sKomp_Sufix = "(uword)"
    End If
    If (InStr(r.v("MAIN_COMPILER"), "Keil")) Then
       sKomp_Sufix = "_sof_"
    End If


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

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

    Print #f, FormatSectionHeader(0, 0, "Defines for the parameter IntName")
    Print #f, ""

    ' for each interrupt source which was insert in the install.inc file
    For Each ir In cp  
      v = StrConv(ir.IntName, vbUpperCase)
      iPosition = InStr(v, " ") ' search for spaces
      While (iPosition > 0)  
        ' cut all characters until the space beginning left
        sLeftCharacters = Left(v, iPosition - 1)
        ' cut all characters until the space beginning right
        sRightCharacters = Right(v, Len(v) - iPosition)
        ' replace the space with a underscore
        v = sLeftCharacters & "_" & sRightCharacters
        ' find the next space
        iPosition = InStr(v, " ")
      Wend 
      ' calculate the required spaces between the text output
      If (Len(v) >= MAX_CHARACTER_INT_DEFINES) Then
        i = 1
      Else
        i = MAX_CHARACTER_INT_DEFINES - Len(v)
      End If

      Print #f, "#define " & v & Space(i) & ir.RegName
    Next ir

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

    ' for each function in this 'func' object
    For l = 1 To func.FunctionCount
      ' get the function name which was insert in 'Include.inc' with
      'AddFunction()'
      d = func.FunctionName(l)
      ' get only the third part of the function name (e.g "Init")
      n = Right(d, Len(d) - Instr(d, "NAME") - Len("NAME"))
      ' only if the use variable of the function is selected and the function is a really function
      ' not a macro or a ISR
      If (r.v(sVarPre & "_USE_" & n) = 1 AND func.FunctionSort(d) = 1) Then
            Print #f, FormatFunctionDeclaration(r, func, d) & ";"
      End If
    Next l
    Print #f, ""
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,8)",0)
    Print #f, ""
    Print #f, ""
                          'DAvE declarations of global macros'
    

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' INT_USE_EnableInterrupt macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "EnableInterrupt"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " IntName = 1" 
      Print #f, ""                                                                                    
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' INT_USE_DisableInterrupt macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "DisableInterrupt"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " IntName = 0" 
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' INT_USE_SetPecCount macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetPecCount"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " PECC##Nr = (PECC##Nr & 0xFF00) | Value" 
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' INT_USE_SetPecSrcP macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetPecSrcP"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        

      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " SRCP##Nr = (uword)((ulong)(Address)); \" 
      Print #f, "         PECSEG##Nr = (PECSEG##Nr & 0x00FF) | (uword)(((ulong)(Address) >> 16) << 8);" 

      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' INT_USE_SetPecDesP macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetPecDesP"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        

      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " DSTP##Nr = (uword)((ulong)(Address)); \" 
      Print #f, "         PECSEG##Nr = (PECSEG##Nr & 0xFF00) | (uword)((ulong)(Address) >> 16);" 

      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' INT_USE_EnableFastISR macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "EnableFastISR"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " _" & r.v(sVarPre & "_NAME_" & n) & "_##ubFastISR()"
      Print #f, "#define _" & r.v(sVarPre & "_NAME_" & n) & "_0() FINT0CSP |= 0x8000"
      Print #f, "#define _" & r.v(sVarPre & "_NAME_" & n) & "_1() FINT1CSP |= 0x8000"
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' INT_USE_DisableFastISR macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "DisableFastISR"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " _" & r.v(sVarPre & "_NAME_" & n) & "_##ubFastISR()"
      Print #f, "#define _" & r.v(sVarPre & "_NAME_" & n) & "_0() FINT0CSP &= 0x7FFF"
      Print #f, "#define _" & r.v(sVarPre & "_NAME_" & n) & "_1() FINT1CSP &= 0x7FFF"
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' Interrupt Vectors
    '------------------------------------------------------------------------------------------------
    Print #f, SECTION_INTERRUPT_VECTORS
    Print #f, ""  'DAvE interrupt vector definitions
    For i = 0 To 7
      n = "Ex" & i
      If (IsISRSelected(r, sVarPre, "CC1_CC" & i + 8 & "IC", n)) Then
        v = 24 + i
        Print #f, "#define CC1_CC" & i + 8 & "INT    " & FormatTextOutput(v, "H2")
      End If
    Next i
    
    n = "EOP"
    If (IsISRSelected(r, sVarPre, "EOPIC", n)) Then
      v = 76
      Print #f, "#define EOPINT    " & FormatTextOutput(v, "H2")
    End If

    n = "NMITrap"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, "#define NMITRAP    " & FormatTextOutput(2, "H2")
    End If

    n = "STKOFTrap"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, "#define STOTRAP    " & FormatTextOutput(4, "H2")
    End If

    n = "STKUFTrap"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, "#define STUTRAP    " & FormatTextOutput(6, "H2")
    End If

    n = "SOFTBRKTrap"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, "#define SBRKTRAP   " & FormatTextOutput(8, "H2")
    End If

    n = "ClassBTrap"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, "#define BTRAP      " & FormatTextOutput(10, "H2")
    End If

    Print #f, ""
    Print #f, ""
    Print #f, uc.GetUserCode("(INT_Header,9)",0)
    Print #f, ""
    Print #f, ""
    Print #f, "#endif  // ifndef _" & StripExtension(r.v(sVarPre & "_HFILE")) & "_H_"

    'close file
    Close f
    
    INTGenerateH = True
    Exit Function
    
INTGenerateHError:
    Close f
    Exit Function
End Function

