Attribute VB_Name = "Module1"
'Uses "..\classes\FunctionClass.cls
'#Uses "..\classes\ModuleClass.cls"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Pages.inc"
'#Uses "..\main\Include.inc
'#Uses "..\include\PORT.inc"

Option Explicit
'*****************************************************************************
' Module:
'        MAIN
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions which are create controls for the
'        module 'MAIN'.
'        Furthermore it contains functions for the event handling of
'        these controls.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        0.1   ALS           15-09-04    Initial version
'        0.2   ALS           06-12-04    Unused variables removed
'                                        System Clock page is modified
'        0.3  radhish        27-12-04    Module name appended before register names
'        0.4  Sunil          24-01-05    Register ADC_LCBR is initialized to 0xB7
'        0.5  Sunil          04-02-05    Added missing NDIV values
'
'        0.1  Sheshadri      04-02-05    Converting to XC886C
'        0.2  Sheshadri      04-05-05    XC886C-Startup, XC886-8FF
'
'
'*****************************************************************************

'*****************************************************************************
'Description:
'        This function is called if a new project is created.
'
'        Only the dialog script of the MAIN module
'        contains this type of function.
'        The project variables will be initialized as follows:
'        1. Initialization of the "project settings" with default values
'        2. Calling of the Update-Functions of all peripheral modules
'           (The "project settings" must be valid at this time.)
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        vIndex          parameter for the module
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function Initialize(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo InitializeError

    Initialize = False
            
    ' Reset Values of the Special Function Registers (values <> 0)
    r.v(CORE_PREFIX & "IEN0_EA") = 1
    r.v(SCU_PREFIX & "PLL_CON_NDIV") = 9
    r.v(SCU_PREFIX & "CMCON_FCCFG") = 1 '-->ashish
    ' SCU registers
    r.sfr(SCU_PREFIX & "OSC_CON") = &H8
    
    ' Port registers
    r.sfr("P0_PUDSEL") = &HFF
    r.sfr("P0_PUDEN") = &HC4
    r.sfr("P1_PUDSEL") = &HFF
    r.sfr("P1_PUDEN") = &HFF
    r.sfr("P2_PUDSEL") = &HFF
    r.sfr("P3_PUDSEL") = &HBF
    r.sfr("P3_PUDEN") = &H0
    r.sfr("P4_PUDSEL") = &HFF
    r.sfr("P4_PUDEN") = &H4
    
    ' SSC registers
'    r.v("SSC_CONL_P_BM") = 1
    
    ' ADC registers
    r.sfr(ADC_PREFIX & "LCBR") = &HB7
    r.sfr(ADC_PREFIX & "GLOBCTR") = &H30
    r.v("SHINT_NAME_Init") = "SHINT_vInit"
    ' General Project Variables:
    r.v("MAIN_CFile") = "MAIN.C"
    r.v("MAIN_MEMORY") = "SMALL"
    r.v("MAIN_COMPILER") = "Keil"
    r.v("MAIN_HFile") = "MAIN.H"
    r.v("MAIN_NAME_Init") = "MAIN_vInit"
    r.v("MAIN_NAME_Main") = "main"
    r.v("MAIN_Notes") = ""
r.v("MAIN_KeilStartup") = 1
    ' Controller Type dependent Project Variables:
    Call SetControllerType(r, "XC886CLM-8FF")                ' set controller type-specific project variables

    'Initialize the variables/registers of all peripheral modules.
    'This has to be executed after initialization of the main variables/registers,
    'because some of these settings (i.e. CPU clock) are
    'also used as input values for the peripherals:
    
    If (Not UpdatePeripherals(r, 0)) Then Exit Function

        r.v("MAIN_FSYS") = GetSystemClock(r)
        r.v("MAIN_PCLK") = GetPeriClock(r)
        r.v("MAIN_CCLK") = GetCoreClock(r)
        
    Initialize = True
    Exit Function

InitializeError:
    MsgBox "Error in Initialize"
    Exit Function
End Function

'*****************************************************************************
'Description:
'        This function is called from DAvE just before the Project Settings
'        window is shown.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function OnLoad(ps As Peripherals) As Boolean
    On Error GoTo OnLoadError

    OnLoad = False

    If (Not OnLoadPageGeneral(ps)) Then Exit Function
    '-------------------------------------------------------------------------
    ' page: SystemClock
    '-------------------------------------------------------------------------
    If (Not OnLoadPageSystemClock(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: GlobalSettings
    '-------------------------------------------------------------------------
    If (Not OnLoadPageGlobalSettings(ps)) Then Exit Function
   
    If (Not OnLoadPageNotes(ps)) Then Exit Function
    
    ' call function to initialize the menu
    If (Not InitMenu(ps, "Main\Dialog")) Then Exit Function

    OnLoad = True
    Exit Function

OnLoadError:
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This function is called from DAvE just before a page
'        is shown.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Page            no. of the page (starts with 1)
'        Pagename        name of the page
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function OnChangingPage(ps As Peripherals, page As Integer, Pagename As String) As Boolean
    On Error GoTo OnChangingPageError

    OnChangingPage = False
    
    If (Not OnChangingPageGeneral(ps, Pagename)) Then Exit Function
    '-------------------------------------------------------------------------
    'page: SystemClock
    '-------------------------------------------------------------------------
    If (Not OnChangingPageSystemClock(ps, Pagename)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: GlobalSettings
    '-------------------------------------------------------------------------
    If (Not OnChangingPageGlobalSettings(ps, Pagename)) Then Exit Function

    If (Not OnChangingPageNotes(ps, Pagename, "MAIN")) Then Exit Function

    OnChangingPage = True
    Exit Function

OnChangingPageError:
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This function is called from DAvE in two cases:
'
'        1.
'            The contents of the peripherals window must be refreshed.
'            In this case the 'Controlname' parameter is a empty string
'            and the values array 'v()' contains nothing.
'
'        2.
'            A control on the peripherals has been changed.
'            In this case the 'Controlname' parameter contains the name
'            of the changed control and the values array 'v()' contains the
'            changed value(s).
'
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     name of the changed control (for case 2)
'        v()             array with the changed values (for case 2)
'        Page            no. of the page (starts with 1)
'        Pagename        name of the page
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function OnShow(ps As Peripherals, Controlname As String, v() As Variant, page As Integer, Pagename As String) As Boolean
    On Error Resume Next

    OnShow = False

    If (Not OnShowPageGeneral(ps, Controlname, Pagename, v)) Then Exit Function
    '-------------------------------------------------------------------------
    ' page: SystemClock
    '-------------------------------------------------------------------------
    If (Not OnShowPageSystemClock(ps, Controlname, Pagename, v)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: GlobalSettings
    '-------------------------------------------------------------------------
    If (Not OnShowPageGlobalSettings(ps, Controlname, Pagename, v)) Then Exit Function

    If (Not OnShowPageNotes(ps, Controlname, Pagename, "MAIN", v)) Then Exit Function

    If (Controlname <> "") Then
        'Some variables/registers of the peripheral modules depend on the
        'project settings, i.e. the CPU clock.
        'Therefore these variables/registers are always updated when the
        'user changes any of the project settings:
        If (Not UpdatePeripherals(ps.ProjectData, 1)) Then Exit Function
    End If

    OnShow = True
End Function


'*****************************************************************************
'Description:
'        This function is called from DAvE if the user clicks on a button.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     name of the button
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function OnClick(ps As Peripherals, Controlname As String) As Boolean
    On Error GoTo OnClickError

    OnClick = True
    Exit Function

OnClickError:
    errorbox "Can not process the click", Err.Description
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This function is called from DAvE if the user clicks on a menu entry.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Toolname        name of the entry
'        ToolId          id of the entry
'        Tooltext        additional text of the entry
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function OnMenuClick(ps As Peripherals, Toolname As String, ToolId As Long, Tooltext As String) As Boolean
    On Error GoTo OnMenuClickError

    OnMenuClick = False

    If (Not MenuClick(ps, Toolname, ToolId, Tooltext, "Main\Dialog")) Then Exit Function

    OnMenuClick = True
    Exit Function

OnMenuClickError:
    Exit Function
End Function
    

'-----------------------------------------------------------------------------
' local functions
'-----------------------------------------------------------------------------


'*****************************************************************************
'Description:
'        This function is called from the OnLoad function of the associated
'        module in order to add a page.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageGeneral(ps As Peripherals) As Boolean
  On Error GoTo ExitLabel
  OnLoadPageGeneral = False

  If (Not ps.AddPage("General")) Then Exit Function

  OnLoadPageGeneral = True
ExitLabel:
End Function

'*****************************************************************************
'Description:
'        This function is called from the OnChangingPage function of the
'        associated module in order load all controls for the requested page
'        if they are not already loaded.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'        Pagename         requested page name
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Public Function OnChangingPageGeneral(ps As Peripherals, Pagename As String) As Boolean
  On Error GoTo ExitLabel
  OnChangingPageGeneral = False

  Dim e As Collection

  Set e = ps.ExtenderCollection

  If (Not e("General").Loaded) And (Pagename = "General") Then
        e("General").Loaded = True
    If (Not ps.AddFrame("fraControllerDetails", 100, 100, 8450, 1750, "Controller Details")) Then Exit Function

    If (Not ps.AddLabel("lblDeviceName", 0, 200, 450, 600, 390, "Device")) Then Exit Function
    If (Not ps.AddCombo("cboDeviceName", 900, 400, 1900, 315)) Then Exit Function

   ' With ps.ControlCollection("cboDeviceName")
   '     .AddItem "XC886CLM-8FF"
   '     .AddItem "XC886CLM-6FF"
   '     .ListIndex = 0
   ' End With
    
    If (Not ps.AddLabel("lblDeviceTypeLabel", 0, 3600, 400, 1100, 390, "Device Type")) Then Exit Function
    If (Not ps.AddLabel("lblDeviceType", 1, 5100, 400, 850, 325, "")) Then Exit Function
    
    If (Not ps.AddLabel("lblFlashSizeLabel", 0, 6100, 400, 1100, 390, "Flash Memory Size")) Then Exit Function
    If (Not ps.AddLabel("lblFlashSize", 1, 7300, 400, 850, 325, "")) Then Exit Function

    If (Not ps.AddLabel("lblROMSizeLabel", 0, 6100, 1000, 1100, 390, "ROM Size")) Then Exit Function
    If (Not ps.AddLabel("lblROMSize", 1, 7300, 1000, 850, 325, "")) Then Exit Function
    
    If (Not ps.AddLabel("lblMax_Clock_Frequency_Label", 0, 3600, 1000, 1600, 390, "Max. System Clock Frequency")) Then Exit Function
    If (Not ps.AddLabel("lblMax_Clock_Frequency", 1, 5100, 1000, 850, 325, "")) Then Exit Function

    If (Not ps.AddFrame("fraMainCFile", 100, 2000, 4175, 1000, "Main Source File")) Then Exit Function
    
    If (Not ps.AddText("txtCFile", 1500, 320, 2400, 315)) Then Exit Function
    If (Not ps.AddLabel("lblCFile", 0, 200, 320, 1150, 390, "File name")) Then Exit Function

    If (Not ps.AddFrame("fraMainHFile", 4350, 2000, 4200, 1000, "Main Header File")) Then Exit Function
    
    If (Not ps.AddText("txtHFile", 1500, 320, 2400, 315)) Then Exit Function
    If (Not ps.AddLabel("lblHFile", 0, 200, 320, 1150, 390, "File name")) Then Exit Function

    If (Not ps.AddFrame("fraCompilerSettings", 100, 3300, 8450, 1600, "Compiler Settings")) Then Exit Function
    
    If (Not ps.AddOption("optKeil", 200, 400, 700, 380, "Keil")) Then Exit Function
    
    If (Not ps.AddCheck("chkKeilStartup", 2000, 400, 2500, 380, "Enable Startup File Generation")) Then Exit Function
    If (Not ps.AddOption("optTask", 200, 800, 900, 380, "Tasking")) Then Exit Function
    If (Not ps.AddOption("optSDCC", 200, 1200, 900, 380, "SDCC")) Then Exit Function

    If (Not ps.AddLabel("lblMemoryModelName", 0, 5000, 450, 1200, 390, "Memory Model:")) Then Exit Function
    If (Not ps.AddCombo("cboMemoryModelName", 6300, 400, 1500, 315)) Then Exit Function
  End If

  OnChangingPageGeneral = True
ExitLabel:
End Function

'*****************************************************************************
'Description:
'        This function is called from the OnShow function of the associated
'        module. See the description for the OnShow function of the
'        associated module for details.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     control name
'        Pagename        current page name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Public Function OnShowPageGeneral(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowError
  OnShowPageGeneral = False
     
  Dim r As ProjectData
  Dim c As Collection
  Dim e As Collection
  Dim bDisabled As Boolean
  Dim lKFactor As Long

  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection

  If (Pagename = "General") Then
    '-------------------------------------------------------------------------
    ' page: General (action)
    '-------------------------------------------------------------------------
    Select Case (Controlname)

        Case "cboDeviceName"
            If (Ask(MSG_CHANGE_CONTROLLER)) Then
                Call SetControllerType(r, v(1))
            End If
            
        Case "txtCFile"
            If (IsInputFormatOK(v(1), FORMAT_FILES, c("lblCFile").Caption)) Then
                r.v("MAIN_CFile") = v(1)
            Else
                Exit Function
            End If

        Case "txtHFile"
            If (IsInputFormatOK(v(1), FORMAT_FILES, c("lblHFile").Caption)) Then
                r.v("MAIN_HFile") = v(1)
            Else
                Exit Function
            End If

        Case "optKeil"
            r.v("MAIN_COMPILER") = "Keil"
             r.v("MAIN_MEMORY") = "SMALL"
'                UpdateMemoryModel (r)   ' choose suitable compiler memory model
        
        Case "chkKeilStartup"
            r.v("MAIN_KeilStartup") = v(1)
            If (r.v("MAIN_KeilStartup") = 0) Then
                If (Ask(MSG_KEIL_STARTUP)) Then
                   r.v("MAIN_KeilStartup") = 0
                Else
                   r.v("MAIN_KeilStartup") = 1
                End If
            End If
        
        Case "optTask"
            r.v("MAIN_COMPILER") = "TASKING"
            r.v(SCU_PREFIX & "OSC_CON_OSCSS") = 0

        Case "optSDCC"
            r.v("MAIN_COMPILER") = "SDCC"
            r.v(SCU_PREFIX & "OSC_CON_OSCSS") = 0
             r.v("MAIN_MEMORY") = "SMALL"
'            r.v("MAIN_KeilStartup") = 0

'                UpdateMemoryModel (r)   ' choose suitable compiler memory model

    Case "cboMemoryModelName"
             
             If (v(1) = "SMALL") Then
                r.v("MAIN_MEMORY") = "SMALL"
             End If
             If (v(1) = "LARGE") Then
                r.v("MAIN_MEMORY") = "LARGE"
             End If
             If (v(1) = "COMPACT") Then
                r.v("MAIN_MEMORY") = "COMPACT"
             End If
    End Select     ' case Controlname
    '-------------------------------------------------------------------------
    ' page: General (show)
    '-------------------------------------------------------------------------
    
    With c("lblFlashSize")
       .Caption = r.v("MAIN_MEM_Size")
    End With

    With c("lblDeviceType")
       .Caption = r.v("MAIN_MEM_Type")
    End With
    With ps.ControlCollection("cboDeviceName")
        .Enabled = True
        .Clear
        .AddItem "XC886CLM-8FF"
        .AddItem "XC886CLM-6FF"
        .ListIndex = 0
        If (r.v("MAIN_CPU_TYPE") = "XC886CLM-8FF") Then
           .ListIndex = 0
        ElseIf (r.v("MAIN_CPU_TYPE") = "XC886CLM-6FF") Then
           .ListIndex = 1
        End If
    End With
    With c("lblROMSize")
        .Caption = r.v("MAIN_ROM_Size")
    End With
    
    With c("lblMax_Clock_Frequency")
       .Caption = (r.v("MAIN_FSYS_MAX") & " MHz")
    End With

    With c("txtCFile")
       .Text = FormatTextOutput(r.v("MAIN_CFile"), "S")
    End With

    With c("txtHFile")
       .Text = FormatTextOutput(r.v("MAIN_HFile"), "S")
    End With

    With c("optKeil")
    bDisabled = False
       .Enabled = Not (bDisabled)
       .Value = r.v("MAIN_COMPILER") = "Keil"
    End With

    c("chkKeilStartup").Value = -CInt(r.v("MAIN_KeilStartup") = 1)
        bDisabled = r.v("MAIN_COMPILER") = "TASKING"
    c("chkKeilStartup").Enabled = Not (bDisabled)
    
    With e("chkKeilStartup")
        If (bDisabled) Then
            .DisabledToolTipText = TOOLTIP_DISABLED_IF
            .DisabledToolTipText = .DisabledToolTipText & FormatDisabledToolTip("startup doesn't support's for this compiler option")
        End If
    End With
    With c("optTask")
        bDisabled = True
       .Enabled = Not (bDisabled)
       .Value = r.v("MAIN_COMPILER") = "TASKING"
    End With

    With e("optTask")
      If (bDisabled) Then
          .DisabledToolTipText = TOOLTIP_DISABLED_IF
          .DisabledToolTipText = .DisabledToolTipText & FormatDisabledToolTip("this version of DAvE doesn't support TASKING")
       End If
    End With

    With c("optSDCC")
    bDisabled = False
       .Enabled = Not (bDisabled)
       .Value = r.v("MAIN_COMPILER") = "SDCC"
    End With

    With e("optSDCC")
      If (bDisabled) Then
          .DisabledToolTipText = TOOLTIP_DISABLED_IF
          .DisabledToolTipText = .DisabledToolTipText & FormatDisabledToolTip("this version of DAvE doesn't support SDCC")
       End If
    End With
    '-------------------------------------------------------------------------
    ' control: cboMemoryModelName (show)
    '-------------------------------------------------------------------------
    With c("cboMemoryModelName")
      .Enabled = True
      .Clear
      If (r.v("MAIN_COMPILER") = "Keil") Then
          .AddItem "SMALL"
          .AddItem "LARGE"
          .AddItem "COMPACT"
      Else
          .AddItem "SMALL"
          .AddItem "LARGE"
      End If
        If (r.v("MAIN_MEMORY") = "SMALL") Then
          .ListIndex = 0
        End If
        If (r.v("MAIN_MEMORY") = "LARGE") Then
          .ListIndex = 1
        End If
      If (r.v("MAIN_COMPILER") = "Keil") Then
          If (r.v("MAIN_MEMORY") = "COMPACT") Then
            .ListIndex = 2
          End If
      End If
   End With
  End If         ' if Pagename

  OnShowPageGeneral = True
  Exit Function

OnShowError:
    errorbox "Can not show page", Err.Description
End Function


'*****************************************************************************
'Description:
'        This function is called from the OnLoad function of the associated
'        module in order to add the page 'SystemClock'.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageSystemClock(ps As Object) As Boolean
  On Error Resume Next

  OnLoadPageSystemClock = False

  If (Not ps.AddPage("System Clock")) Then Exit Function

  OnLoadPageSystemClock = True
End Function



'*****************************************************************************
'Description:
'        This function is called from the OnChangingPage function of the
'        associated module in order load all controls for the requested page
'        if they are not already loaded.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'        Pagename         requested page name
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnChangingPageSystemClock(ps As Object, Pagename As String) As Boolean
  On Error Resume Next

  OnChangingPageSystemClock = False
  
  If (Not ps.ExtenderCollection("System Clock").Loaded) And (Pagename = "System Clock") Then
    ps.ExtenderCollection("System Clock").Loaded = True

    If (Not ps.AddFrame("frmClkSrcCTRL", 100, 100, 4150, 1400, "Clock Source Control")) Then Exit Function
        If (Not ps.AddOption("optClkSrcCTRL_3", 200, 350, 2500, 400, "Prescalar Mode (VCO Bypass)")) Then Exit Function
        If (Not ps.AddOption("optClkSrcCTRL_4", 200, 800, 1400, 400, "PLL Mode")) Then Exit Function

    If (Not ps.AddFrame("frmClkSystem", 100, 1600, 4150, 1000, " Clock System")) Then Exit Function
      If (Not ps.AddLabel("lblClkSys", 0, 200, 500, 1950, 315, "")) Then Exit Function
      If (Not ps.AddCombo("cboPLLN", 2250, 500, 1000, 380)) Then Exit Function
      If (Not ps.AddLabel("lblP", 1, 3350, 250, 550, 300, "P = 1")) Then Exit Function
      If (Not ps.AddLabel("lblK", 1, 3350, 600, 550, 300, "K = 2")) Then Exit Function

'    If (Not ps.AddFrame("frmOSCout", 100, 2750, 4150, 1450, "System Clock")) Then Exit Function
'        If (Not ps.AddCheck("chk0SCout", 200, 600, 2400, 300, " Use pin P0.0 as CLKOUT ")) Then Exit Function
    
    If (Not ps.AddFrame("frmOSCselect", 4400, 100, 4150, 1400, "Clock Source Selection ")) Then Exit Function
        If (Not ps.AddOption("optClkSource_I", 250, 350, 1400, 400, "OnChip OSC")) Then Exit Function
        If (Not ps.AddOption("optClkSource_E", 2500, 350, 1400, 400, "External OSC")) Then Exit Function
        If (Not ps.AddLabel("lblOSC", 0, 2000, 960, 1000, 385, "fOSC [MHz]")) Then Exit Function
        If (Not ps.AddText("txtOSC", 3050, 960, 800, 315)) Then Exit Function

    If (Not ps.AddFrame("frmSysFrq", 4400, 1600, 4150, 1000, "System Frequency fSYS")) Then Exit Function
        If (Not ps.AddLabel("lblfsysMHz", 0, 2000, 450, 850, 335, "fSYS [MHz]")) Then Exit Function
        If (Not ps.AddLabel("lblfsys", 1, 3050, 400, 800, 315, "")) Then Exit Function

    If (Not ps.AddFrame("frmClkManagement", 4400, 2650, 4150, 2600, "Clock Management")) Then Exit Function
        If (Not ps.AddLabel("lblClkDivider", 0, 200, 300, 2300, 315, "Clock divider (CMCON.CLKREL)")) Then Exit Function
        If (Not ps.AddCombo("cboClkDIV", 2550, 250, 1400, 380)) Then Exit Function
        
        If (Not ps.AddLabel("lblpClk", 0, 200, 700, 2500, 315, "PCLK, SCLK, CCLK [MHz]")) Then Exit Function
        If (Not ps.AddLabel("lblpClk_cal", 1, 3050, 700, 800, 315, "")) Then Exit Function
        
        If (Not ps.AddLabel("lblfClk", 0, 200, 1050, 1800, 315, "FCLK [MHz]")) Then Exit Function
        If (Not ps.AddLabel("lblfClk_cal", 1, 3050, 1050, 800, 315, "")) Then Exit Function
  
  If (Not ps.AddFrame("frmClkOutputCntrl", 100, 2650, 4150, 2600, "Clock Output Control")) Then Exit Function
        If (Not ps.AddCheck("chk0SCout", 200, 200, 2200, 350, "Use Pin P0.0 as CLKOUT")) Then Exit Function
        If (Not ps.AddCheck("chk0SCout1", 200, 500, 2200, 350, "Use Pin P0.7 as CLKOUT")) Then Exit Function
        If (Not ps.AddOption("optClkFOSCSel", 200, 800, 1500, 350, "CLKOUT= fOSC")) Then Exit Function
        If (Not ps.AddOption("optClkDivSel", 200, 1100, 3700, 350, "CLKOUT is Selected by COREL and TLEN bits")) Then Exit Function
        If (Not ps.AddLabel("lblClkOutputDiv", 0, 200, 1500, 2300, 315, "Clock Divider (COCON.COREL)")) Then Exit Function
        If (Not ps.AddCombo("cboClkOutputDiv", 2550, 1500, 1400, 380)) Then Exit Function
        If (Not ps.AddCheck("chkTLEN", 200, 1800, 3500, 400, "Enable Toggle Latch(COCON.TLEN)")) Then Exit Function
        If (Not ps.AddLabel("lblCLKOUT", 0, 200, 2200, 1800, 315, "CLKOUT[MHz]")) Then Exit Function
        If (Not ps.AddLabel("lblfClk_calCntrl", 1, 3050, 2200, 800, 315, "")) Then Exit Function
  End If
  OnChangingPageSystemClock = True
End Function


'*****************************************************************************
'Description:
'        This function is called from the OnShow function of the associated
'        module. See the description for the OnShow function of the
'        associated module for details.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     control name
'        Pagename        current page name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnShowPageSystemClock(ps As Object, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowPageSystemClockError

  OnShowPageSystemClock = False

  Dim r           As ProjectData
  Dim c           As Object
  Dim e           As Object
  Dim i           As Integer
  Dim j           As Integer
  Dim vTemp       As Variant
  Dim vTempValue  As Variant
  Dim bDisabled   As Boolean

  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection

  If (Pagename = "System Clock") Then
  
      Select Case (Controlname)
         
        Case "optClkSrcCTRL_3"
            r.v("PLL_CTRL") = 3
            r.v(SCU_PREFIX & "PLL_CON_NDIV") = 9
 '           r.sfr(SCU_PREFIX & "CMCON") = 0
           r.v(SCU_PREFIX & "CMCON_CLKREL") = 0
        
        Case "optClkSrcCTRL_4"
            r.v("PLL_CTRL") = 4
'            r.sfr(SCU_PREFIX & "CMCON") = 0 '
           r.v(SCU_PREFIX & "CMCON_CLKREL") = 0

        Case "optClkSource_I"
            r.v(SCU_PREFIX & "OSC_CON_OSCSS") = 0
            r.v(SCU_PREFIX & "PLL_CON_NDIV") = 9
'            r.sfr(SCU_PREFIX & "CMCON") = 0 '
            r.v(SCU_PREFIX & "CMCON_CLKREL") = 0
            r.v("MAIN_FOSC") = r.v("MAIN_ONCHIPOSC")
        
        Case "optClkSource_E"
            r.v(SCU_PREFIX & "OSC_CON_OSCSS") = 1
            r.v(SCU_PREFIX & "PLL_CON_NDIV") = 10
           '' r.v("SCU_USE_ClockOut") = 0'ashish
'                r.sfr(SCU_PREFIX & "CMCON") = 0 '
           r.v(SCU_PREFIX & "CMCON_CLKREL") = 0
           ''  Call ReleaseAlternateOutputPin(r, "CLKOUT")'ashish
                r.v("MAIN_FOSC") = r.v("MAIN_EXTOSC")
        
        Case "txtOSC"
            vTemp = r.v("MAIN_FOSC")
            If (IsInputFormatOK(v(1), "F", c("lblOSC").Caption, r.v("MAIN_EXTOSC_MIN"), r.v("MAIN_EXTOSC_MAX"))) Then
                r.v("MAIN_EXTOSC") = v(1)
                r.v("MAIN_FOSC") = r.v("MAIN_EXTOSC")
            Else
                Exit Function
            End If
            
        Case "cboPLLN"
            If (Ask(MSG_CHANGE_SYSTEMCLOCK)) Then
                r.v(SCU_PREFIX & "PLL_CON_NDIV") = c("cboPLLN").ListIndex
            End If
            
        Case "cboClkDIV"
            If (Ask(MSG_CHANGE_CLKREL)) Then
                r.v(SCU_PREFIX & "CMCON_CLKREL") = c("cboClkDIV").ListIndex
            End If
           
        Case "chk0SCout"
             If (v(1) = 1) Then
                 If (r.v("SCU_USE_ClockOut") = 0) Then
                     r.v("SCU_USE_ClockOut") = 1
                  ElseIf (r.v("SCU_USE_ClockOut") = 2) Then
                     r.v("SCU_USE_ClockOut") = 3
                End If
             Else
                 If (r.v("SCU_USE_ClockOut") = 3) Then
                     r.v("SCU_USE_ClockOut") = 2
                  Else
                     r.v("SCU_USE_ClockOut") = 0
                 End If
            End If
              If (r.v("SCU_USE_ClockOut") = 1 Or r.v("SCU_USE_ClockOut") = 3) Then
                  Call RequestAlternateOutputPin(r, "CLKOUT", ALT_FUN1)
              ElseIf (r.v("SCU_USE_ClockOut") = 2) Then
                   Call ReleaseAlternateOutputPin(r, "CLKOUT")
              ElseIf (r.v("SCU_USE_ClockOut") = 0) Then
                   Call ReleaseAlternateOutputPin(r, "CLKOUT")
                   r.v(SCU_PREFIX & "COCON_COREL") = 0
                   r.v(SCU_PREFIX & "COCON_TLEN") = 0
             End If
                 
      Case "chk0SCout1"
            If v(1) Then
               If (r.v("SCU_USE_ClockOut") = 0) Then
                 r.v("SCU_USE_ClockOut") = 2
                  ElseIf (r.v("SCU_USE_ClockOut") = 1) Then
                r.v("SCU_USE_ClockOut") = 3
                End If
             Else
                If (r.v("SCU_USE_ClockOut") = 3) Then
                     r.v("SCU_USE_ClockOut") = 1
                 Else
                     r.v("SCU_USE_ClockOut") = 0
                 End If
            End If
           If (r.v("SCU_USE_ClockOut") = 2 Or r.v("SCU_USE_ClockOut") = 3) Then
                Call RequestAlternateOutputPin(r, "CLKOUT_1", ALT_FUN1)
           ElseIf (r.v("SCU_USE_ClockOut") = 1) Then
               Call ReleaseAlternateOutputPin(r, "CLKOUT_1")
           ElseIf (r.v("SCU_USE_ClockOut") = 0) Then
                  Call ReleaseAlternateOutputPin(r, "CLKOUT_1")
                  r.v(SCU_PREFIX & "COCON_COREL") = 0
                  r.v(SCU_PREFIX & "COCON_TLEN") = 0
           End If

       Case "cboClkOutputDiv"
         r.v(SCU_PREFIX & "COCON_COREL") = c("cboClkOutputDiv").ListIndex
        
        Case "chkTLEN"
        If (v(1)) Then
            r.v(SCU_PREFIX & "COCON_TLEN") = 1
         Else
          r.v(SCU_PREFIX & "COCON_TLEN") = 0
        End If
            
        Case "optClkFOSCSel"
           r.v(SCU_PREFIX & "COCON_COREL") = 0
           r.v(SCU_PREFIX & "COCON_COUTS") = 0
           r.v(SCU_PREFIX & "COCON_TLEN") = 0
      
        Case "optClkDivSel"
           r.v(SCU_PREFIX & "COCON_COUTS") = 1
End Select
            
        r.v("MAIN_FSYS") = GetSystemClock(r)
        r.v("MAIN_PCLK") = GetPeriClock(r)
       
        With c("optClkSrcCTRL_3")
            bDisabled = False
            .Enabled = Not (bDisabled)
            .Value = -CInt(r.v("PLL_CTRL") = 3)
        End With
                
        With c("optClkSrcCTRL_4")
            bDisabled = False
            .Enabled = Not (bDisabled)
            .Value = -CInt(r.v("PLL_CTRL") = 4)
        End With
        
        With c("lblClkSys")
            If (r.v("PLL_CTRL") = 3) Then
                .Caption = " fSYS = fOSC x 1 /( P x K )"
            ElseIf (r.v("PLL_CTRL") = 4) Then
                .Caption = " fSYS = fOSC x N /( P x K )"
            End If
        End With
        
        With c("cboPLLN")
             If (r.v("PLL_CTRL") = 3) Then
                .Visible = False
            ElseIf (r.v("PLL_CTRL") = 4) Then
                .Visible = True
            End If
             .Clear
             .AddItem " N = 10"
             For i = 12 To 20
              .AddItem " N = " & i
             Next i
             .AddItem " N = 24"
             .AddItem " N = 30"
             .AddItem " N = 32"
             .AddItem " N = 36"
             .AddItem " N = 40"
             .AddItem " N = 48"
             .ListIndex = r.v(SCU_PREFIX & "PLL_CON_NDIV")
        End With
        
        With c("lblP")
            .Visible = True
        End With
        
        With c("lblK")
            .Visible = True
        End With
        
        With c("chk0SCout")
            bDisabled = (Not (IsAlternatePinFree(r, "CLKOUT"))) ' Or r.v("OSC_CON_OSCSS")
            .Enabled = Not bDisabled
            If (r.v("SCU_USE_ClockOut") = 1 Or r.v("SCU_USE_ClockOut") = 3) Then
            .Value = 1
            Else
            .Value = 0
            End If
            
        End With

         With e("chk0SCout")
          If (bDisabled) Then
           .DisabledToolTipText = TOOLTIP_DISABLED_IF
           .DisabledToolTipText = .DisabledToolTipText & GetDisabledAlternatePinTooltip(r, "CLKOUT")
          End If
        End With
        
      With c("chk0SCout1")
            bDisabled = (Not (IsAlternatePinFree(r, "CLKOUT_1"))) ' Or r.v("OSC_CON_OSCSS")
            .Enabled = Not bDisabled
            '.Value = r.v("SCU_USE_ClockOut") = 2
            If (r.v("SCU_USE_ClockOut") = 2 Or r.v("SCU_USE_ClockOut") = 3) Then
            .Value = 1
            Else
            .Value = 0
            End If
        End With

         With e("chk0SCout1")
          If (bDisabled) Then
            .DisabledToolTipText = TOOLTIP_DISABLED_IF
             .DisabledToolTipText = .DisabledToolTipText & GetDisabledAlternatePinTooltip(r, "CLKOUT_1")
          End If
        End With
        
        With c("optClkSource_I")
            bDisabled = False
            .Enabled = Not (bDisabled)
            .Value = -CInt(r.v(SCU_PREFIX & "OSC_CON_OSCSS") = 0)
        End With
            
        With c("optClkSource_E")
            bDisabled = False '(r.v("MAIN_COMPILER") = "SDCC")
            .Enabled = Not (bDisabled)
            .Value = -CInt(r.v(SCU_PREFIX & "OSC_CON_OSCSS") = 1)
        End With
        
        With e("optClkSource_E")
            If bDisabled Then
                .DisabledToolTipText = TOOLTIP_DISABLED_IF & FormatDisabledToolTip("this is not supported for SDCC")
            End If
        End With
            
        With c("txtOSC")
            bDisabled = Not (r.v(SCU_PREFIX & "OSC_CON_OSCSS") = 1)
            .Enabled = Not (bDisabled)
            If (r.v(SCU_PREFIX & "OSC_CON_OSCSS") = 0) Then
                r.v("MAIN_FOSC") = r.v("MAIN_ONCHIPOSC")
                .Text = Format(r.v("MAIN_FOSC"), "0.00")
            Else
                .Text = Get_fextOSC(r)
            End If
        End With
        
        With c("lblfsys")
            .Caption = Get_FSYS(r)
        End With
        
        With c("cboClkDIV")
            .Clear
            .AddItem " fSYS/ " & 4
            .AddItem " fSYS/ " & 6
            .AddItem " fSYS/ " & 8
            .AddItem " fSYS/ " & 12
            .AddItem " fSYS/ " & 16
            .AddItem " fSYS/ " & 24
            .AddItem " fSYS/ " & 32
            .AddItem " fSYS/ " & 48
            .AddItem " fSYS/ " & 64
            .AddItem " fSYS/ " & 96
            .AddItem " fSYS/ " & 128
            .AddItem " fSYS/ " & 192
            .AddItem " fSYS/ " & 256
            .AddItem " fSYS/ " & 384
            .AddItem " fSYS/ " & 512
            .AddItem " fSYS/ " & 768
                .ListIndex = r.v(SCU_PREFIX & "CMCON_CLKREL")
        End With
    
        With c("lblpClk_cal")
            .Caption = Get_fPCLK(r)
        End With
    
        With c("lblfClk_cal")
            .Caption = Get_fFCLK(r)
        End With
      
        With c("optClkFOSCSel")
        If (r.v("SCU_USE_ClockOut") = 0) Then
        bDisabled = True
        r.v(SCU_PREFIX & "COCON_COUTS") = 0
        Else
        bDisabled = False
        End If
        .Enabled = Not bDisabled
        If (bDisabled) Then
        .Value = 1 ' r.v("SCU_USE_ClockOut")
'         r.v(SCU_PREFIX & "COCON_COUTS") = 0
       Else
           .Value = -CInt(r.v(SCU_PREFIX & "COCON_COUTS") = 0)
       
        End If
        End With
       
        With e("optClkFOSCSel")
           If (bDisabled) Then
           .DisabledToolTipText = TOOLTIP_DISABLED_IF
           .DisabledToolTipText = TOOLTIP_DISABLED_IF & FormatDisabledToolTip("Pin P0.0 is not selected as CLKOUT")
          End If
      End With
      
         With c("optClkDivSel")
        bDisabled = r.v("SCU_USE_ClockOut") = 0
       .Enabled = Not bDisabled
       .Value = r.v(SCU_PREFIX & "COCON_COUTS")
        End With
        
        With e("optClkDivSel")
            If (bDisabled) Then
            .DisabledToolTipText = TOOLTIP_DISABLED_IF
            .DisabledToolTipText = TOOLTIP_DISABLED_IF & FormatDisabledToolTip("Pin P0.0 is not selected as CLKOUT")
            End If
        End With
        
        With c("cboClkOutputDiv")
        .Clear
            .AddItem " fSYS/ " & 2
            .AddItem " fSYS/ " & 3
            .AddItem " fSYS/ " & 4
            .AddItem " fSYS/ " & 5
            .AddItem " fSYS/ " & 6
            .AddItem " fSYS/ " & 8
            .AddItem " fSYS/ " & 9
            .AddItem " fSYS/ " & 10
            .AddItem " fSYS/ " & 12
            .AddItem " fSYS/ " & 16
            .AddItem " fSYS/ " & 18
            .AddItem " fSYS/ " & 20
            .AddItem " fSYS/ " & 24
            .AddItem " fSYS/ " & 32
            .AddItem " fSYS/ " & 36
            .AddItem " fSYS/ " & 40
        .ListIndex = r.v(SCU_PREFIX & "COCON_COREL")
         bDisabled = (Not r.v("SCU_USE_ClockOut") <> 0) Or (r.v(SCU_PREFIX & "COCON_COUTS") = 0)
        .Enabled = Not bDisabled
       End With
        
        With c("cboClkOutputDiv")
        bDisabled = Not ((r.v("SCU_USE_ClockOut") <> 0) And (r.v(SCU_PREFIX & "COCON_COUTS") = 1))
        .Enabled = Not bDisabled
        End With
        
        With e("cboClkOutputDiv")
        If (bDisabled) Then
        .DisabledToolTipText = TOOLTIP_DISABLED_IF
            If (r.v("SCU_USE_ClockOut") = 0) Then
            .DisabledToolTipText = TOOLTIP_DISABLED_IF & FormatDisabledToolTip("CLKOUT pin is not selected")
             Else
            .DisabledToolTipText = TOOLTIP_DISABLED_IF & FormatDisabledToolTip("CLKOUT is not selected by bit field COREL")
            End If
        End If
        End With
        
        With c("lblClkOutputDiv")
            bDisabled = Not ((r.v("SCU_USE_ClockOut") <> 0) And (r.v(SCU_PREFIX & "COCON_COUTS") = 1))
           .Enabled = Not bDisabled
        End With
        
        With e("lblClkOutputDiv")
          If (bDisabled) Then
            .DisabledToolTipText = TOOLTIP_DISABLED_IF
            .DisabledToolTipText = TOOLTIP_DISABLED_IF & FormatDisabledToolTip("CLKOUT pin is not selected")
        End If
        End With
      
        With c("lblfClk_calCntrl")
            If (r.v("SCU_USE_ClockOut") = 0) Then
            .Caption = "n.a"
            ElseIf (r.v(SCU_PREFIX & "COCON_COUTS") = 0) Then
                 .Caption = r.v("MAIN_FOSC")
            ElseIf (r.v(SCU_PREFIX & "COCON_COUTS") = 1) And (r.v(SCU_PREFIX & "COCON_TLEN") = 1) Then
                 .Caption = Get_fsysCLKOUT(r) 'function to measure fsys/2
            ElseIf (r.v(SCU_PREFIX & "COCON_COUTS") = 1) Then
                 .Caption = Get_fsysCLKOUT(r)
        End If
       End With
       
        With c("chkTLEN")
            bDisabled = Not ((r.v("SCU_USE_ClockOut") <> 0) And (r.v(SCU_PREFIX & "COCON_COUTS") = 1))
            .Enabled = Not bDisabled
            If (r.v("SCU_USE_ClockOut") = 0 Or r.v(SCU_PREFIX & "COCON_COUTS") = 0) Then
            .Value = 0
            ElseIf ((r.v(SCU_PREFIX & "COCON_COUTS") = 1) And (r.v(SCU_PREFIX & "COCON_TLEN") = 1)) Then
            .Value = 1
            End If
        End With
        
        With e("chkTLEN")
        If (bDisabled) Then
        .DisabledToolTipText = TOOLTIP_DISABLED_IF
            .DisabledToolTipText = TOOLTIP_DISABLED_IF & FormatDisabledToolTip("CLKOUT pin is not selected")
        End If
        End With
        
         
         c("lblCLKOUT").Enabled = (r.v("SCU_USE_ClockOut"))
         c("lblfClk_calCntrl").Enabled = (r.v("SCU_USE_ClockOut"))
  End If
 
  OnShowPageSystemClock = True
  Exit Function

OnShowPageSystemClockError:
  errorbox "Can not show page.", Err.Description
  Exit Function
End Function

'*****************************************************************************
'Description:
'        This function is called from the OnLoad function of the associated
'        module in order to add the page 'GlobalSettings'.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageGlobalSettings(ps As Object) As Boolean
  On Error Resume Next

  OnLoadPageGlobalSettings = False

  If (Not ps.AddPage("Global Settings")) Then Exit Function

  OnLoadPageGlobalSettings = True
End Function


'*****************************************************************************
'Description:
'        This function is called from the OnChangingPage function of the
'        associated module in order load all controls for the requested page
'        if they are not already loaded.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'        Pagename         requested page name
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnChangingPageGlobalSettings(ps As Object, Pagename As String) As Boolean
  On Error Resume Next

  OnChangingPageGlobalSettings = False
  
  If (Not ps.ExtenderCollection("Global Settings").Loaded) And (Pagename = "Global Settings") Then
    ps.ExtenderCollection("Global Settings").Loaded = True
    '-------------------------------
    ' frame: frmIEN
    '-------------------------------
    If (Not ps.AddFrame("frmIEN", 100, 100, 4100, 1000, "Global Interrupt Enable")) Then Exit Function
        If (Not ps.AddCheck("chkIEN", 200, 400, 2700, 300, "Enable global interrupt (IEN0.EA)")) Then Exit Function

    '-------------------------------
    ' frame: frmIMODE
    '-------------------------------
    If (Not ps.AddFrame("frmIMODE", 4300, 100, 4100, 1000, "Interrupt Structure 2 Mode Select")) Then Exit Function
        If (Not ps.AddOption("optImode0", 200, 240, 1300, 380, "IMODE = 0")) Then Exit Function
        If (Not ps.AddOption("optImode1", 200, 600, 1300, 380, "IMODE = 1")) Then Exit Function
    '-------------------------------
    ' frame: frmISRCHOICE
    '-------------------------------
    If (Not ps.AddFrame("frmISRCHOICE", 100, 1200, 8300, 1000, "Interrupt Service Routine Type Select")) Then Exit Function
        If (Not ps.AddOption("optChoice1", 200, 240, 8000, 380, "CHOICE1" & "  " & "ISRs Push and Pop SYSCON to prevent register mapping (RMAP) problems")) Then Exit Function
        If (Not ps.AddOption("optChoice2", 200, 600, 8000, 380, "CHOICE2" & "  " & "Global Interrupts (EA) are disabled in Functions/Macros that modify RMAP to avoid mapping issue")) Then Exit Function
    '-------------------------------
    ' frame: frmMSGEN
    '-------------------------------
    If (Not ps.AddFrame("frmMSGEN", 100, 2300, 4100, 1000, "Shared Interrupt Message Box Enable")) Then Exit Function
        If (Not ps.AddCheck("chkMSGEN", 200, 400, 3000, 300, "Enable shared interrupt message box")) Then Exit Function
  End If
  OnChangingPageGlobalSettings = True
End Function



'*****************************************************************************
'Description:
'        This function is called from the OnShow function of the associated
'        module. See the description for the OnShow function of the
'        associated module for details.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     control name
'        Pagename        current page name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnShowPageGlobalSettings(ps As Object, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowPageGlobalSettingsError

  OnShowPageGlobalSettings = False

  Dim r As ProjectData
  Dim c As Object
  Dim e As Object
  Dim bDisabled As Boolean

  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection

  If (Pagename = "Global Settings") Then
    Select Case (Controlname)
        
     Case "chkIEN"
            If v(1) = 0 Then
                If (Ask(MSG_CHANGE_GLOBAL_INTERRUPT)) Then
                    r.v(CORE_PREFIX & "IEN0_EA") = v(1)
                End If
            Else
                r.v(CORE_PREFIX & "IEN0_EA") = 1
            End If
            
     Case "optImode0"
            r.v(SCU_PREFIX & "SYSCON0_IMODE") = 0

     Case "optImode1"
            r.v(SCU_PREFIX & "SYSCON0_IMODE") = 1
    
     Case "optChoice1"
            r.v("ISR_CHOICE") = 1
     Case "optChoice2"
            r.v("ISR_CHOICE") = 2
     Case "chkMSGEN"
            If v(1) = 0 Then
                If (Ask(MSG_CHANGE_SHARED_INTERRUPT)) Then
                    r.v("INTR_MSGEN") = v(1)
                End If
            Else
                 r.v("INTR_MSGEN") = 1
            End If
    End Select

    With c("chkIEN")
      bDisabled = False
      .Enabled = Not (bDisabled)
      .Value = -CInt(r.v(CORE_PREFIX & "IEN0_EA") = 1)
    End With

    With c("optImode0")
    bDisabled = False
       .Enabled = Not (bDisabled)
       .Value = -CInt(r.v(SCU_PREFIX & "SYSCON0_IMODE") = 0)
    End With

    With c("optImode1")
    bDisabled = False
       .Enabled = Not (bDisabled)
       .Value = -CInt(r.v(SCU_PREFIX & "SYSCON0_IMODE") = 1)
    End With
    With c("optChoice1")
    bDisabled = False
       .Enabled = Not (bDisabled)
       .Value = -CInt(r.v("ISR_CHOICE") = 1)
    End With

    With c("optChoice2")
    bDisabled = False
       .Enabled = Not (bDisabled)
       .Value = -CInt(r.v("ISR_CHOICE") = 2)
    End With
    With c("chkMSGEN")
      bDisabled = False
      .Enabled = Not (bDisabled)
      .Value = -CInt(r.v("INTR_MSGEN") = 1)
    End With
  End If

  OnShowPageGlobalSettings = True
  Exit Function

OnShowPageGlobalSettingsError:
  errorbox "Can not show page.", Err.Description
  Exit Function
End Function

'*****************************************************************************
'Description:
'        The "UpdateProjectData" functions of all peripheral modules are
'        called with parameter "lngUpdateDescriptor" being passed down.
'
'        Restrictions:
'        - The first module in collection "MODULENAMES" is the main module
'          (which has no "UpdateProjectData" function implemented)
'        - All other modules in collection "MODULENAMES" contain a file
'          "Ini.dave" with a "UpdateProjectData" function
'-----------------------------------------------------------------------------
'Parameters:
'        r                      reference to the project data
'        lngUpdateDescriptor    identification of the update context (input)
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function UpdatePeripherals(r As ProjectData, lngUpdateDescriptor As Long) As Boolean
  On Error GoTo UpdatePeripheralsError

  Dim lngIndex  As Long
  Dim cp        As DAvECollection
  Dim sc        As Script

  UpdatePeripherals = False
  Set cp = ObjectCollection("MODULENAMES")

  For lngIndex = 2 To cp.Count  ' skip the first module "main"
    Set sc = New Script
    sc.Load (DefGetPath("Scripts") & cp(lngIndex).ModuleDirectory & "\Ini.dave")
    If (Not sc.Functions.UpdateProjectData(r, cp(lngIndex).ParameterValue, lngUpdateDescriptor)) Then Exit Function
  Next lngIndex

  UpdatePeripherals = True
  Exit Function

UpdatePeripheralsError:
  Exit Function
End Function


'*****************************************************************************
'Description:
'        The project variables for a specific controller type are set.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the project data (input)
'        sTypeName       Name of the controller type
'                        (as displayed in the combo box)
'*****************************************************************************
Sub SetControllerType(r As ProjectData, sTypeName As String)
    On Error Resume Next

    Select Case sTypeName
            '--------------------------------------------------------------------------------
        Case "XC886CLM"
            r.v("MAIN_CPU_TYPE") = "XC886CLM"
            r.v("MAIN_MEM_Type") = "FLASH"
            r.v("MAIN_MEM_Size") = "32 K"
            r.v("MAIN_ROM_Size") = " - "

        Case "XC886CLM-8FF"
            r.v("MAIN_CPU_TYPE") = "XC886CLM-8FF"
            r.v("MAIN_MEM_Type") = "FLASH"
            r.v("MAIN_MEM_Size") = "32 K"
            r.v("MAIN_ROM_Size") = " - "

        Case "XC886CLM-6FF"
            r.v("MAIN_CPU_TYPE") = "XC886CLM-6FF"
            r.v("MAIN_MEM_Type") = "FLASH"
            r.v("MAIN_MEM_Size") = "24 K"
            r.v("MAIN_ROM_Size") = " - "
    End Select
    
    r.v("MAIN_ONCHIPOSC") = 9.6
    r.v("MAIN_FOSC") = r.v("MAIN_ONCHIPOSC")      'default fosc = OnchipOsc
    r.v("MAIN_EXTOSC") = 8
    r.v("MAIN_EXTOSC_MIN") = 3
    r.v("MAIN_EXTOSC_MAX") = 12
    
    r.v("MAIN_FSYS_MIN") = 80
    r.v("MAIN_FSYS_MAX") = 96
        
End Sub

    

