//============================================================================
//  Name:                                                                     
//    startup.cmm 
//
//  Description:                                                              
//    Common startup script that calls the processor specific init scripts 
//                                                                            
// Copyright (c) 2012 - 2018 by Qualcomm Technologies, Incorporated.  All Rights Reserved.        
//
//
//
//
//                      EDIT HISTORY FOR FILE
//  This section contains comments describing changes made to the module.
//  Notice that changes are listed in reverse chronological order.
//
// when       who           what, where, why
// --------   ---           ---------------------------------------------------------
// 03/31/2018 NAKKALA       Search metabuild path for PRESIL to force rumi setting
// 03/21/2018 RVENNAM       Add TRST WA for SDM855 shortcuts
// 01/31/2018 PKARUNAK      Changes for sdx50v2/sdx55
// 01/17/2018 JBILLING      Changes for directory update
// 01/02/2018 JBILLING      Refactor/Cleanup
// 04/06/2017 JBILLING      Updates for sdm845
// 01/16/2016 JBILLING      Changed for 9645 automotive
// 03/10/2016 JBILLING      Updated temp path for host OS
// 10/26/2015 JBILLING      Changed location of gen_buildflavor.cmm
// 12/03/2013 AJCheriyan    Added MPQ support
// 05/02/2013 AJCheriyan    Added support for History
// 04/18/2013 AJCheriyan    Added default access specifier option
// 04/04/2013 AJCheriyan    Added option for SWD connectivity
// 07/10/2012 AJCheriyan    Created for B-family
//

// T32PROP     - T32 connection property (DAP,EUD)
// CHIPSET     - Chipset Name (SDM845,SDM855...)
// SUBSYS      - Sub-system Name (APPS,MPSS...)
// CORENUM     - Core Number (0,1,2..)
// CONNECT     - Connectivity option (SWD/JTAG)
// fusion_arg  - Fusion platform option (Fusion2,Fusion3)
// rumi_option - RUMI option 

LOCAL &Subroutine &ArgumentLine &remainder &argline
ENTRY %LINE &ArgumentLine
ENTRY &Subroutine %LINE &remainder

&Subroutine=STR.UPR("&Subroutine")

//**************************************************
//                  Select Subroutine 
//**************************************************
    
    IF ("&ArgumentLine"=="")
    (
        PRINT %ERROR "Error! Arguments expected for startup script (T32PROP, CHIPSET, etc. or Subroutine)."
    )
    ELSE IF (("&Subroutine"=="GET_FUSION_OPTIONS")||("&Subroutine"=="GET_CHIPTYPE")||("&Subroutine"=="GET_RUMI_OPTIONS")||("&Subroutine"=="CALL_ATTACH_CONFIG_SCRIPT")||("&Subroutine"=="GET_HOSTOS_INFO")||("&Subroutine"=="CALL_GENERATED_BUILDINFO")||("&Subroutine"=="FUSION_SETTINGS_OVERWRITE_HLOS")||("&Subroutine"=="CALL_TOOLSCONFIG")||("&Subroutine"=="CALL_BUILD_FLAVOR_SCRIPT")||("&Subroutine"=="CALL_SESSION_CFG_SCRIPT"))
    (
        //&Subroutine="&Subroutine"
        &argline="&remainder"
    )
    ELSE
    (
        &Subroutine="MAIN"
        &argline="&ArgumentLine"
    )
    
    GOSUB &Subroutine &argline
    LOCAL &rvalue
    ENTRY %LINE &rvalue 

    GOSUB EXIT &rvalue

//////////////////////////////////////////////////
///
/// Main function
/// Primary entry point for T32 sessions. Performs all basic configurations.
///
//////////////////////////////////////////////////
MAIN:
    GLOBAL &T32PROP &CHIPSET &SUBSYS &CORENUM &CONNECT &METABUILDPATH
    LOCAL &fusion_arg &rumi_option
    ENTRY &T32PROP &CHIPSET &SUBSYS &CORENUM &CONNECT &fusion_arg &rumi_option

    //Sanitize arguments
    &CHIPSET=STR.LWR("&CHIPSET")
    &CORENUM=STR.LWR("&CORENUM")
    &CONNECT=STR.LWR("&CONNECT")
    IF "&rumi_option"==""
    (
        &METABUILDPATH=os.pwd()
        IF STRING.SCAN(STR.LWR("&METABUILDPATH"),"presil",0)!=-1
        (
            &rumi_option="TRUE"
        )
        ELSE
        (
            &rumi_option="NULL"
        )
    )
    
    
    GOSUB GET_CHIPTYPE &CHIPSET
    GLOBAL &CHIPTYPE &CHIPFAMILY
    ENTRY  &CHIPTYPE &CHIPFAMILY

    GOSUB GET_FUSION_OPTIONS &fusion_arg
    GLOBAL &FUSION_PLATFORM &FUSION_TYPE
    ENTRY  &FUSION_TYPE &FUSION_PLATFORM

    GOSUB GET_RUMI_OPTIONS &CHIPSET &rumi_option
    GLOBAL &RUMI &VI_DIRECTORY &VI_SCRATCH &ENCLAVE_FLAG
    ENTRY  &RUMI &VI_DIRECTORY &VI_SCRATCH &ENCLAVE_FLAG

    GOSUB CALL_ATTACH_CONFIG_SCRIPT &T32PROP &SUBSYS &CHIPSET &CORENUM &CONNECT
    GLOBAL &BOOTPROCESSOR
    ENTRY  &BOOTPROCESSOR

    GOSUB GET_HOSTOS_INFO
    GLOBAL &TEMPDIR &TEMP &HOSTOS
    ENTRY  &TEMPDIR &TEMP &HOSTOS

    GOSUB CALL_GENERATED_BUILDINFO &TEMPDIR
    GLOBAL &BUILDINFOHASRUN
    ENTRY  &BUILDINFOHASRUN

    GOSUB CALL_TOOLSCONFIG &CHIPSET &SUBSYS

    GOSUB CALL_BUILD_FLAVOR_SCRIPT &FUSION_ARG 
    GLOBAL &BUILD_FLAVOR_SCRIPT &PRODUCT_FLAVOR &HLOS
    ENTRY  &BUILD_FLAVOR_SCRIPT &PRODUCT_FLAVOR &HLOS

    GOSUB FUSION_SETTINGS_OVERWRITE_HLOS &FUSION_ARG &HLOS
    GLOBAL &HLOS
    ENTRY &HLOS

    // Perform the intercom initialization
    do std_intercom_init NEWSESSION

	// Perform chip specific global workarounds
	IF  ("&CHIPSET"=="sdm855"&&"&BOOTPROCESSOR"=="FALSE"&&"&SUBSYS"!="RPM")||("&CHIPSET"=="sdm1000"&&"&BOOTPROCESSOR"=="FALSE"&&"&SUBSYS"!="RPM")
    (
		GLOBALON SYSUP DO &METASCRIPTSDIR/common/&CHIPSET/std_trst_wa.cmm
	)
	
    GOSUB CALL_SESSION_CFG_SCRIPT &CHIPSET &SUBSYS

    // Other various variables and settings.
    GLOBAL &ACCESS 
    &ACCESS="AXI"

    GLOBAL &STDRESETPMIC
    &STDRESETPMIC="TRUE"

    ON ERROR DEFAULT 
    GLOBALON ERROR DEFAULT

    GLOBAL &CURRENT_T32_VERSION
    &CURRENT_T32_VERSION=SOFTWARE.VERSION()

    //If we're using RUMI, show HW version dialog to user.
    IF "&RUMI"=="TRUE"&&"&BOOTPROCESSOR"=="TRUE"
    (
        do &METASCRIPTSDIR/debug/&CHIPSET/std_debug_gui GET_HW_VERSION_GUI
    )

    RETURN SUCCESS
//////////////////==== End main function ====///////////////////////





//#####======---------------------------------------------=====######
//#####======               Subroutines                   =====######
//#####======---------------------------------------------=====######


/////////////////////////////////////////////
///
/// Fusion Options
///
/////////////////////////////////////////////
GET_FUSION_OPTIONS:
    LOCAL &fusion_arg
    ENTRY &fusion_arg

    LOCAL &rvalue_fusion_type &rvalue_fusion_platform 

    IF STR.LWR("&fusion_arg")=="fusion2"
    (
        //Fusion2 setup is two distinct boards but with one t32 instance opening them
        //i.e., not really a fusion setup.
        &rvalue_fusion_platform="FUSION_MDM"
        &rvalue_fusion_type="FUSION_MDM"

    )
    ELSE IF STR.LWR("&fusion_arg")=="fusion3"
    (
        IF ("&CHIPTYPE"=="MSM")
        (
            &rvalue_fusion_platform="FUSION_MSM"
            &rvalue_fusion_type="NONE"
        )
        ELSE
        (
            &rvalue_fusion_platform="TRUE"
            &rvalue_fusion_type="FUSION3"
        )
    )
    ELSE IF STR.LWR("&fusion_arg")=="konafusion"
    (
        &rvalue_fusion_platform="TRUE"
        &rvalue_fusion_type="KONAFUSION"
    )    
    ELSE IF STR.LWR("&fusion_arg")=="fusion"
    (
        &rvalue_fusion_platform="TRUE"
        &rvalue_fusion_type="FUSION1"
    )
    ELSE
    (
        &rvalue_fusion_platform="FALSE"
        &rvalue_fusion_type="NONE"
    )
    RETURN &rvalue_fusion_type &rvalue_fusion_platform

/////////////////////////////////////////////
///
/// Chip type Options
///
/////////////////////////////////////////////
GET_CHIPTYPE:
    LOCAL &chipset
    ENTRY &chipset
    
    LOCAL &rvalue_chiptype &rvalue_chipfamily
    LOCAL &chipset_upr
    
    &chipset_upr=STR.UPR("&CHIPSET")
    IF (STRING.SCAN("&chipset_upr","MDM",0)!=-1)||(STRING.SCAN("&chipset_upr","SDX",0)!=-1)
    (
        &rvalue_chiptype="SDX"
    )
    ELSE
    (
        &rvalue_chiptype="SDM"
    )
    
    IF (STRING.SCAN("&chipset_upr","8998",0)!=-1)||(STRING.SCAN("&chipset_upr","9X45",0)!=-1)||(STRING.SCAN("&chipset_upr","9X55",0)!=-1)
    (
        &rvalue_chipfamily="HONEYBADGER"
    )
    ELSE IF (STRING.SCAN("&chipset_upr","X20",0)!=-1)||(STRING.SCAN("&chipset_upr","X50",0)!=-1)
    (
        &rvalue_chipfamily="BEAR"
    )
    ELSE
    (
        &rvalue_chipfamily="HOYA"
    )
    RETURN &rvalue_chiptype &rvalue_chipfamily
    
/////////////////////////////////////////////
///
///  RUMI Presilicon options
///
/////////////////////////////////////////////
GET_RUMI_OPTIONS:
    LOCAL &chipset &rumi_option &bootprocessor
    ENTRY &chipset &rumi_option &bootprocessor
    
    LOCAL &rvalue_rumi &rvalue_vi_directory &rvalue_enclave_option &rvalue_vi_scratch_dir
    LOCAL &enclave_vi_directory &enclave_vi_scratch_dir
    &rvalue_rumi="FALSE"
    &rvalue_enclave_option="FALSE"
    IF ("&rumi_option"=="TRUE")
    (
        &rvalue_rumi="TRUE"
        &rvalue_enclave_option="FALSE"
    )
    
    IF ("&rvalue_rumi"=="TRUE")&&(("&chipset"!="sdx50")||("&chipset"!="sdx55"))
    (
        //Timescale
        system.config.debugtimescale 4
        &rvalue_vi_directory="\\qctdfsrt\prj\vlsi\vlsi_verify"
        &rvalue_vi_scratch_dir="&rvalue_vi_directory"


            
    )
    
    RETURN &rvalue_rumi &rvalue_vi_directory &rvalue_vi_scratch_dir &rvalue_enclave_option

/////////////////////////////////////////////
///
///  Attach configuration script. 
///  This script (e.g.  DAP_APPS) contains 
///  all configurations required for attaching 
///  to a particular core.
///
/////////////////////////////////////////////
CALL_ATTACH_CONFIG_SCRIPT:
    LOCAL &t32prop &subsys &chipset &corenum &connect
    ENTRY &t32prop &subsys &chipset &corenum &connect
    
    GLOBAL &BOOTPROCESSOR
    &subsys=STR.UPR("&subsys")
    IF ("&subsys"=="APPS")&&("&corenum"=="0")
    (
        &BOOTPROCESSOR="TRUE"
    )
    ELSE
    (
        &BOOTPROCESSOR="FALSE"
    )

	GLOBAL &EUD
	LOCAL &configprefix
	IF "&t32prop"=="EUD"
	(
		do config/EUD_CONFIG &chipset &corenum &connect 
		&configprefix="DAP"
	)
	ELSE
	(
		&configprefix="DAP"
	)
	
    LOCAL &subscript
    &subscript="&configprefix"+"_"+"&subsys"
    PRINT "Calling script: &subscript with args: &chipset &corenum &connect"
    do config/&subscript &chipset &corenum &connect
    
    RETURN &BOOTPROCESSOR    
    

//////////////////////////////////////////////////
///
/// Retrieve Host machine OS info such as OS name, temp directory.
///
//////////////////////////////////////////////////
GET_HOSTOS_INFO:
    
    //Get host os type
    LOCAL &rvalue_tempdir &rvalue_hostos 
    
    IF (VERSION.BUILD()<65625.)
    (
        &rvalue_hostos="Windows"
    )
    ELSE
    (
        &rvalue_hostos=OS.NAME()
    )
    IF ("&rvalue_hostos"=="Windows")
    (
        &rvalue_tempdir=OS.ENV(TEMP)
    )
    ELSE
    (
        &rvalue_tempdir="~~~"
    )
    RETURN &rvalue_tempdir &rvalue_tempdir &rvalue_hostos
    
//////////////////////////////////////////////////
///
/// Call buildinfo script, which was generated via  
/// metabuild scripts from t32start.py  call.
///
//////////////////////////////////////////////////
CALL_GENERATED_BUILDINFO:
    LOCAL &tempdir
    ENTRY &tempdir
        
    LOCAL &rvalue_buildinfohasrun
    &rvalue_buildinfohasrun="FALSE"
    // At this point we have the location of the scripts for 
    // this session. Call the script to populate the build information
    PRINT "Calling script: gen_buildinfo"
    IF FILE.EXIST("&tempdir/gen_buildinfo.cmm")
    (
        &rvalue_buildinfohasrun="TRUE"
        do &tempdir/gen_buildinfo.cmm
    )
    ELSE
    (
        PRINT "Warning - Could not access gen_buildinfo.cmm at &TEMPDIR"
    )
    
    //APPS_BUILDROOT used interchangeably with  APSS_BUILDROOT.
    //Ensure here  that they match.
    IF ((STRING.SCAN("&APPS_BUILDROOT","APPS_BUILDROOT",0)!=-1)&&(STRING.SCAN("&APSS_BUILDROOT","APSS_BUILDROOT",0)==-1))
    (
        GLOBAL &APPS_BUILDROOT
        &APPS_BUILDROOT="&APSS_BUILDROOT"
    )


    RETURN &rvalue_buildinfohasrun
    
//////////////////////////////////////////////////
///
/// Overwrite HLOS name if fusion2 or fusion3. 
/// Not the most elegant way to set this global variable.
/// Would be better to retrieve a separate variable from contents.xml
///
//////////////////////////////////////////////////
FUSION_SETTINGS_OVERWRITE_HLOS:
    LOCAL &fusion_arg &hlos
    ENTRY &fusion_arg &hlos
    
    IF STR.LWR("&fusion_arg")=="fusion2"
    (
        //Fusion2 setup is two distinct boards but with one t32 instance opening them
        //Rarely used / will be deprecated as a feature soon.
        &hlos="LE"
        
        &APSS_BUILDROOT="&APSS_BUILDROOT_MDM"
        GLOBAL &APPS_BUILDROOT
        &APPS_BUILDROOT="&APSS_BUILDROOT"
        &MPSS_BUILDROOT="&MPSS_BUILDROOT_MDM"
        &TZ_BUILDROOT="&TZ_BUILDROOT_MDM"
        &BOOT_BUILDROOT="&BOOT_BUILDROOT_MDM"
        &BTFM_BUILDROOT="&BTFM_BUILDROOT_MDM"
        &RPM_BUILDROOT="&RPM_BUILDROOT_MDM"
    )
    ELSE IF STR.LWR("&fusion_arg")=="fusion3"
    (
        &hlos="MN"
    )
    
    RETURN &hlos

//////////////////////////////////////////////////
///
/// Toolsconfig script call.
/// This script mainly deals with paths and additional global variables.
///
//////////////////////////////////////////////////
CALL_TOOLSCONFIG:
    LOCAL &chipset &subsys
    ENTRY &chipset &subsys
    
    LOCAL &currdir
    &currdir=OS.PPD()
    IF FILE.EXIST("&currdir/std_toolsconfig.cmm")
    (
        PRINT "Calling script: std_toolsconfig with args: &chipset &subsys"
        do &currdir/std_toolsconfig &chipset &subsys
    )
    ELSE IF FILE.EXIST("&currdir/../std_toolsconfig.cmm")
    (
        PRINT "Calling script: std_toolsconfig with args: &chipset &subsys"
        do &currdir/../std_toolsconfig &chipset &subsys
    )
    ELSE
    (
        WINPOS 0. 0. 50. 5.
        AREA
        PRINT %ERROR "Error! Could not navigate to std_toolsconfig. Make sure to use 'cd.do' to run simulator script"
        WINPOS 0. 6. 50. 10.
        PLIST
        PSTEP
    )
    RETURN 

//////////////////////////////////////////////////
///
/// Build flavor script
/// This script is generated at metabuild compile time and 
/// should be located in one of the directories specified below.
/// It contains global variables for various elf and build directory
/// subpaths.
///
//////////////////////////////////////////////////
CALL_BUILD_FLAVOR_SCRIPT:
    LOCAL &fusion_arg
    ENTRY &fusion_arg
    
    LOCAL &build_flavor_script &product_flavor
    
    IF FILE.EXIST(&METASCRIPTSDIR/../../../build/app/gen_buildflavor.cmm)
    (
        &build_flavor_script="&METASCRIPTSDIR/../../../build/app/gen_buildflavor.cmm"
    )
    ELSE IF (FILE.EXIST(&METASCRIPTSDIR/gen/gen_buildflavor.cmm))
    (
        &build_flavor_script="&METASCRIPTSDIR/gen/gen_buildflavor.cmm"
    )
    ELSE
    (

        WINPOS 0. 0. 50. 10.
        area.create gen_buildflavor
        AREA.SELECT gen_buildflavor
        area.view gen_buildflavor
        PRINT %ERROR "   Warning! gen_buildflavor.cmm not found!" 
        PRINT " "
        PRINT %ERROR "   Metabuild binary paths, "
        PRINT %ERROR "   ELF file paths and build "
        PRINT %ERROR "   flavors will be missing."
        PRINT " "
        PRINT " "
        PRINT " "
        area.select A000
        wait.3s

        &build_flavor_script="NULL"
    )
    
    
    //FIX ME - Don't think this does anything. 
    //IF ("&PRODUCT_FLAVORS"=="")
    //(
    //    PRINT %ERROR "Product Flavor invalid. Invalid meta build configuration"
    //    GOTO FATALEXIT
    //)
    
    // Execute flavor script, taking into account multiple flavors case. 
    IF "&build_flavor_script"!="NULL"
    (
        IF (STRING.SCAN("&PRODUCT_FLAVORS",",",0)==-1)
        (
            IF "&PRODUCT_FLAVORS"==""
            (
                do &build_flavor_script asic
            )
            ELSE
            (
                // This means there is just one product flavor
                do &build_flavor_script &PRODUCT_FLAVORS
                &product_flavor="&PRODUCT_FLAVORS"
            )
        )
        ELSE
        (
            // Populate the data using the first one and make it the default
            &DEFAULT_FLAVOR=STR.MID("&PRODUCT_FLAVORS",0,STR.SCAN("&PRODUCT_FLAVORS",",",0))
            do &build_flavor_script &DEFAULT_FLAVOR
            &product_flavor="&DEFAULT_FLAVOR"
        )
    )
    
    &hlos="&HLOS_TYPE"
    
    RETURN &build_flavor_script &product_flavor &hlos
    
//////////////////////////////////////////////////
///
/// Call session cfg script
/// This script  configures T32 menus and session for the user
///
//////////////////////////////////////////////////
CALL_SESSION_CFG_SCRIPT:
    LOCAL &chipset &subsys
    ENTRY &chipset &subsys
    
    PRINT "Calling script: std_sessioncfg with args: &subsys"
    GLOBAL &SESSIONSCRIPT
    &SESSIONSCRIPT="std_sessioncfg_"+STR.LWR("&subsys")
    ON ERROR GOTO USE_DIRECT_PATH
    do &SESSIONSCRIPT INIT &chipset &subsys
    GOTO CONTINUESCRIPT
USE_DIRECT_PATH:

    ON ERROR DEFAULT

    IF OS.FILE("&METASCRIPTSDIR/sessioncfg/&SESSIONSCRIPT.cmm")
    (
        do &METASCRIPTSDIR/sessioncfg/&SESSIONSCRIPT INIT &chipset &subsys
    )
    ELSE
    (
        PRINT "Warning: Was not able to find &SESSIONSCRIPT"
    )
CONTINUESCRIPT:
    ON ERROR DEFAULT
    
    
    RETURN 
    
EXIT:
 ENTRY %LINE &ArgumentLine
 ENDDO &ArgumentLine

FATALEXIT:
    END
