/*----------------------------------------------------------------------------*/
/* sys_link.cmd                                                               */
/*                                                                            */
/* 
* Copyright (C) 2009-2018 Texas Instruments Incorporated - www.ti.com 
* 
* 
*  Redistribution and use in source and binary forms, with or without 
*  modification, are permitted provided that the following conditions 
*  are met:
*
*    Redistributions of source code must retain the above copyright 
*    notice, this list of conditions and the following disclaimer.
*
*    Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the 
*    documentation and/or other materials provided with the   
*    distribution.
*
*    Neither the name of Texas Instruments Incorporated nor the names of
*    its contributors may be used to endorse or promote products derived
*    from this software without specific prior written permission.
*
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
*  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
*  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
*  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
*  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
*  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

/*                                                                            */
/*----------------------------------------------------------------------------*/
/* USER CODE BEGIN (0) */

#define BOOT_START_ADDRESS 	0x00000000
#define BOOT_INFO_SIZE   	0x8
#define ABORT_STACK_SIZE	0x1800
#define STACK_SIZE 			0x3000
#define RAM_SIZE 			0x40000 	/* Total CPU internal RAM size */

#define PROFILE_SIZE 		0x8000
#define LOG_SIZE 			0x2000

--verbose_diagnostics
-c                                 		/* Link using ROM autoinitialization conventions */
-heap   0x0000                     		/* Heap area size */
-stack  STACK_SIZE                 		/* Main stack area */

--cinit_compression=off
--copy_compression=off


/* USER CODE END */


/*----------------------------------------------------------------------------*/
/* Linker Settings                                                            */

--retain="*(.intvecs)"

/* USER CODE BEGIN (1) */


/*----------------------------------------------------------------------------*/
/* Memory Map - Redefined for BootLoader                                      */
MEMORY
{
    VECTORS 		 (X)	: origin=BOOT_START_ADDRESS 				length=0x00000040															fill =0xFFFFFFFF
    BOOTLOADERFLASH  (RX) 	: origin=(end(VECTORS)) 					length=(0x0060000 - (BOOT_START_ADDRESS + size(VECTORS) + BOOT_INFO_SIZE)) 	vfill=0xFFFFFFFF // 384K
    BOOTLINFO        (RX) 	: origin=(start(BOOTLOADERFLASH)+size(BOOTLOADERFLASH)) 	length=BOOT_INFO_SIZE	                                	vfill=0xFFFFFFFF // 8Bytes

    /* Not used.
    FLASH_NVM_SECT1  (RW)	: origin=0xF0200000                       	length=0x4000																vfill=0xFFFFFFFF // 16K
    FLASH_NVM_SECT2  (RW)	: origin=0xF0204000                       	length=0x4000																vfill=0xFFFFFFFF // 16K
    FLASH_NVM_SECT3  (RW)	: origin=0xF0208000                       	length=0x4000																vfill=0xFFFFFFFF // 16K
    FLASH_NVM_SECT4  (RW)	: origin=0xF020C000                       	length=0x4000																vfill=0xFFFFFFFF // 16K									vfill=0xFFFFFFFF //16K * 4 sectors
    */

    ABORT_STACKS  	 (RW)	: origin=0x08000000 						length=ABORT_STACK_SIZE														vfill=0xFFFFFFFF

    STACKS  		 (RW)	: origin=(end(ABORT_STACKS))				length=STACK_SIZE															vfill=0xFFFFFFFF

    PROFILE			 (RW)	: origin=(end(STACKS))  					length=PROFILE_SIZE															vfill=0x0       /* Added for profiling purposes */

    LOG 			 (RW)	: origin=(end(PROFILE)) 					length=LOG_SIZE					    										vfill=0xDEADBEEF/* Added for logging purposes */

	RAM     		 (RWX)	: origin=(end(LOG)) 						length=(RAM_SIZE - (STACK_SIZE + PROFILE_SIZE +LOG_SIZE)) 					vfill=0x0

    ECC_VEC  		 (R) 	: origin=(0xf0400000 + (BOOT_START_ADDRESS >> 3))
    						  length=(0x00000020 >> 3)
    						  ECC={ algorithm=algoL2R5F021, input_range=VECTORS, fill=false }

	ECC_BOOTLOADERFLASH (R) : origin=(0xf0400000 + size(ECC_VEC)) // ECC BOOTLOADERFLASH include BOOTLINFO section
							  length=((0x0060000 - (BOOT_START_ADDRESS + size(VECTORS) + size(BOOTLINFO)))>> 3) // + BOOT_INFO_SIZE)) >> 3)
							  ECC={ algorithm=algoL2R5F021, input_range=BOOTLOADERFLASH, fill=false }

	ECC_BOOTLINFO (R) : origin=(0xf0400000 + size(ECC_BOOTLOADERFLASH) + size(ECC_VEC))
							  length=(size(BOOTLINFO) >>3 )
							  ECC={ algorithm=algoL2R5F021, input_range=BOOTLINFO, fill=false }

	/*  Not used.
	ECC_FLASH_NVM_SECT1 (R) : origin= 0xF0100000
							  length=(size(FLASH_NVM_SECT1) >> 3)
							  ECC={ algorithm=algoL2R5F021, input_range=FLASH_NVM_SECT1 }

	ECC_FLASH_NVM_SECT2 (R) : origin=(0xf0100000 + size(ECC_FLASH_NVM_SECT1))
							  length=(size(FLASH_NVM_SECT2) >> 3)
							  ECC={ algorithm=algoL2R5F021, input_range=FLASH_NVM_SECT2 }

	ECC_FLASH_NVM_SECT3 (R) : origin=(0xf0100000 + size(ECC_FLASH_NVM_SECT1) + size(ECC_FLASH_NVM_SECT2))
							  length=(size(FLASH_NVM_SECT3) >> 3)
							  ECC={ algorithm=algoL2R5F021, input_range=FLASH_NVM_SECT3 }

	ECC_FLASH_NVM_SECT4 (R) : origin=(0xf0100000 + size(ECC_FLASH_NVM_SECT1) + size(ECC_FLASH_NVM_SECT2) + size(ECC_FLASH_NVM_SECT3))
							  length=(size(FLASH_NVM_SECT4) >> 3)
							  ECC={ algorithm=algoL2R5F021, input_range=FLASH_NVM_SECT4 }
	*/
}


/*----------------------------------------------------------------------------*/
/* Section Configuration                                                      */

SECTIONS
{
	.intvecs : START( ulFlashStartAddr ) {} palign=8, fill=0xff, crc_table(_boot_crc_table, algorithm=TMS570_CRC64_ISO) > VECTORS

	//.intvecs : {} > VECTORS
	//.Os_ExceptionVectors: {}, > 0x004
	//.Os_IRQ : {}, palign=4 > VECTORS
	//.Os_SWI : {}, palign=4 > VECTORS
	//.Os_ExceptionHandlers : {}, palign=4 > VECTORS

	flashAPI_Text:
   	{
    	.\MainSources\Source\Ios_Flash.obj (.text)
    	.\MainSources\Source\F021_Flash\Fapi_UserDefinedFunctions.obj (.text)
    	--library ..\Library\F021_API_CortexR4_BE_V3D16.lib (.text)
    } palign=8, load = BOOTLOADERFLASH, run = RAM, LOAD_START(FlsApiLoadStart), RUN_START(FlsApiRunStart), SIZE(FlsApiLoadSize), crc_table(_boot_crc_table, algorithm=TMS570_CRC64_ISO)

    flashAPI_Const:
   	{
    	.\MainSources\Source\Ios_Flash.obj (.const)
    	//.\MainSources\Source\F021_Flash\Fapi_UserDefinedFunctions.obj (.const)
    	//--library ..\Library\F021_API_CortexR4_BE_V3D16.lib (.const)
    } palign=8, load = BOOTLOADERFLASH, run = RAM,LOAD_START(FlsApiconstLoadStart), RUN_START(FlsApiconstRunStart), SIZE(FlsApiconstLoadSize), crc_table(_boot_crc_table, algorithm=TMS570_CRC64_ISO)

	// Addition of section for safety library self tests needing to work from RAM
	.sl_stflash_SRAM : {} palign=8, load = BOOTLOADERFLASH, run = RAM, LOAD_START(SelfTestpApiLoadStart), LOAD_END(SelfTestpApiLoadEnd), RUN_START(SelfTestApiRunStart),RUN_END(SelfTestApiRunEnd), SIZE(SelfTestApiLoadSize)

    .text    : {} palign=8, crc_table(_boot_crc_table, algorithm=TMS570_CRC64_ISO)  > BOOTLOADERFLASH

    //FEE_TEXT_SECTION : 	{} palign=8, crc_table(_boot_crc_table, algorithm=TMS570_CRC64_ISO) > BOOTLOADERFLASH - Not used

	.const   : END( ulFlashEndAddr ) {} palign=8, crc_table(_boot_crc_table, algorithm=TMS570_CRC64_ISO) > BOOTLOADERFLASH  //palign=8, load = BOOTLOADERFLASH, run = RAM, LOAD_START(constLoadStart), RUN_START(constRunStart), SIZE(constLoadSize), crc_table(_my_crc_table, algorithm=TMS570_CRC64_ISO)

	//FEE_CONST_SECTION : {} palign=8, crc_table(_boot_crc_table, algorithm=TMS570_CRC64_ISO) > BOOTLOADERFLASH - Not used

	.cinit   : {} palign=8, crc_table(_boot_crc_table, algorithm=TMS570_CRC64_ISO) > BOOTLOADERFLASH

    //.pinit   : {} palign=8 //.pinit is an empty section

	.boot_info_section : {} palign=8, load = BOOTLINFO, run = RAM, LOAD_START(BootLInfoLoadStart), RUN_START(BootLInfoRunStart), SIZE(BootLInfoLoadSize),crc_table(_boot_crc_table, algorithm=TMS570_CRC64_ISO)

    .TI.crctab : {} palign=8 > BOOTLOADERFLASH

	.STACK_DATA_svc     : {. += 1024;} palign=8 > ABORT_STACKS, RUN_START(StackModeSVC)
    .STACK_DATA_fiq     : {. += 1024;} palign=8 > ABORT_STACKS, RUN_START(StackModeFIQ)
    .STACK_DATA_irq     : {. += 1024;} palign=8 > ABORT_STACKS, RUN_START(StackModeIRQ)
    .STACK_DATA_abt     : {. += 1024;} palign=8 > ABORT_STACKS, RUN_START(StackModeABT)
   	.STACK_DATA_und     : {. += 1024;} palign=8 > ABORT_STACKS, RUN_START(StackModeUND)
    .STACK_DATA_sys     : {. += 1024;} palign=8 > ABORT_STACKS, RUN_START(StackModeSYS)

	.stack:         (NOLOAD) { . = ALIGN(8);
      							_sstack = .;
							    . = . + STACK_SIZE;
							    . = ALIGN(8);
							    _estack = .;
  							 } > STACKS

    .bss     : {} > RAM

    .data    : {} > RAM

    FEE_DATA_SECTION : 	{} > RAM

	.sysmem  : {} > RAM

	LOG_DATA 		: START( ulLOGStartAddr ), END( ulLOGEndAddr ) > LOG

    PROFILE_DATA    : START( ulPROFILEStartAddr ), END( ulPROFILEEndAddr ) > PROFILE

}

#if 0
/* USER CODE END */

/*----------------------------------------------------------------------------*/
/* Memory Map                                                                 */

MEMORY
{
    VECTORS (X)  : origin=0x00000000 length=0x00000020
    FLASH0  (RX) : origin=0x00000020 length=0x0017FFE0
    FLASH1  (RX) : origin=0x00180000 length=0x00180000
    STACKS  (RW) : origin=0x08000000 length=0x00001500
    RAM     (RW) : origin=0x08001500 length=0x0003EB00

/* USER CODE BEGIN (2) */
/* USER CODE END */
}

/* USER CODE BEGIN (3) */
/* USER CODE END */


/*----------------------------------------------------------------------------*/
/* Section Configuration                                                      */

SECTIONS
{
    .intvecs : {} > VECTORS
    .text    : {} > FLASH0 | FLASH1
    .const   : {} > FLASH0 | FLASH1
    .cinit   : {} > FLASH0 | FLASH1
    .pinit   : {} > FLASH0 | FLASH1
    .bss     : {} > RAM
    .data    : {} > RAM
	.sysmem  : {} > RAM
	
    FEE_TEXT_SECTION : {} > FLASH0 | FLASH1
    FEE_CONST_SECTION : {} > FLASH0 | FLASH1
    FEE_DATA_SECTION : {} > RAM

/* USER CODE BEGIN (4) */
/* USER CODE END */
}

/* USER CODE BEGIN (5) */
#endif

/* USER CODE END */


/*----------------------------------------------------------------------------*/
/* Misc                                                                       */

/* USER CODE BEGIN (6) */

ECC {
	algoL2R5F021 : address_mask = 0xfffffff8 	/*Address Bits 31:3 */
	hamming_mask             = R4 				/*Use R4/R5 build in Mask */
	parity_mask              = 0x0c 			/*Set which ECC bits are Even & Odd parity */
	mirroring                = F021				/* RM57Lx and TMS570LCx are build in F021 */
}

/* USER CODE END */
/*----------------------------------------------------------------------------*/
