/* G32A10XX GNU ld file */

MEMORY {
    flash_boot_region (rx)     : ORIGIN = 0x08000000, LENGTH = 0x110
    flash_rsvd_fls_region (rx) : ORIGIN = 0x08000110, LENGTH = 0x80
    flash_memory_region (rx)   : ORIGIN = 0x08000190, LENGTH = 0x10000 - 0x190

    iram_main_region (rwx)     : ORIGIN = 0x20000000, LENGTH = 0x1750
    iram_stack_region (rw)     : ORIGIN = 0x20001750, LENGTH = 0x800
    iram_rsvd_fls_region (rw)  : ORIGIN = 0x20001F60, LENGTH = 0x60
}

ENTRY(Reset_Handler)

__STACK_SIZE = DEFINED(__STACK_SIZE) ? __STACK_SIZE : 0x800;

SECTIONS {
    AppFlashBoot : ALIGN(4) {
        KEEP(*(.intc_vector))
        . = ALIGN(4);
        KEEP(*(.flash_config))
        . = ALIGN(4);
    } > flash_boot_region

    AppACFLSCode : {
        KEEP(*(.acfls_code_rom));
    } > flash_rsvd_fls_region

    AppCode : {
        *(.mcal_text*);
        *(.text*);
        . = ALIGN(4);
    } > flash_memory_region

    AppROData : {
        *(.mcal_const_cfg*);
        *(.mcal_const*);
        *(.rodata*);
        . = ALIGN(4);
    } > flash_memory_region

    AppStartup : {
        KEEP(*(.startup));
        KEEP(*(.systeminit));
        KEEP(*(.text.Reset_Handler));
    } > flash_memory_region

    AppData : ALIGN(4) {
        _ramappdata = .;
        KEEP(*(.mcal_data_init))
        KEEP(*(.data_init))
        *(.mcal_data*);
        *(.data*);
        . = ALIGN(4);
        _edata = .;
    } > iram_main_region AT > flash_memory_region
    _flashappdata = LOADADDR(AppData);

    AppRAMCode_Init : {
        KEEP(*(.ramcode_init))
    } > flash_memory_region
    _flashramcode = LOADADDR(AppRAMCode_Init);
    _endramcode = _flashramcode + SIZEOF(AppRAMCode);

    AppRAMCode : {
        _sramcode = .;
        *(.ramcode*);
        . = ALIGN(4);
        _eramcode = .;
    } > iram_main_region AT > flash_memory_region
    _ramcode = ADDR(AppRAMCode);

    AppBSS_NO_CACHE (NOLOAD) : {
        _sbss_nc = .;
        *(.mcal_bss_no_cacheable*);
        . = ALIGN(4);
        _ebss_nc = .;
    } > iram_main_region

    AppData_NO_CACHE_Init : {
        _sidata_nc = .;
        KEEP(*(.mcal_data_no_cacheable_init));
    } > flash_memory_region
    _flashappdatanocache = LOADADDR(AppData_NO_CACHE_Init);
    _endappdatanocache = _flashappdatanocache + SIZEOF(AppData_NO_CACHE);

    AppData_NO_CACHE : {
        _sdata_nc = .;
        *(.mcal_data_no_cacheable*);
        . = ALIGN(4);
        _edata_nc = .;
    } > iram_main_region AT > flash_memory_region
    _ramappdatanocache = ADDR(AppData_NO_CACHE);

    AppBSS (NOLOAD) : {
        _sbss = .;
        __bss_start__ = .;
        *(.mcal_bss*);
        *(.bss*);
        *(COMMON);
        . = ALIGN(4);
        _ebss = .;
        __bss_end__ = .;
    } > iram_main_region

    AppSTACK (NOLOAD) : {
        . = ALIGN(16);
        . += __STACK_SIZE;
        *(.stack_main*);
        . = ALIGN(16);
        _estack = .;
    } > iram_stack_region

    .heap (NOLOAD) : {
        . = ALIGN(8);
        end = .;
        _end = .;
    } > iram_main_region
}

ASSERT(SIZEOF(AppFlashBoot) <= LENGTH(flash_boot_region), "AppFlashBoot exceeds flash_boot_region 0x410");

_Stack_end = ORIGIN(iram_stack_region);
_Stack_start = ORIGIN(iram_stack_region) + LENGTH(iram_stack_region);

INT_SRAM_START = ORIGIN(iram_main_region);
INT_SRAM_END   = 0x20002000;
INT_SRAM_SIZE  = (INT_SRAM_END - INT_SRAM_START + 3) / 4;

Fls_ACEraseRomStart = LOADADDR(AppACFLSCode);
Fls_ACEraseRomEnd = LOADADDR(AppACFLSCode) + SIZEOF(AppACFLSCode);
Fls_ACEraseSize = (SIZEOF(AppACFLSCode) + 3) / 4;

Fls_ACWriteRomStart = LOADADDR(AppACFLSCode);
Fls_ACWriteRomEnd = LOADADDR(AppACFLSCode) + SIZEOF(AppACFLSCode);
Fls_ACWriteSize = (SIZEOF(AppACFLSCode) + 3) / 4;

_ERASE_FUNC_ADDRESS_ = ORIGIN(iram_rsvd_fls_region);
_WRITE_FUNC_ADDRESS_ = ORIGIN(iram_rsvd_fls_region);

PROVIDE(_sbss_no_cache = _sbss_nc);
PROVIDE(_ebss_no_cache = _ebss_nc);
PROVIDE(_sdata_no_cache = _sdata_nc);
PROVIDE(_edata_no_cache = _edata_nc);
PROVIDE(__bss_start__ = _sbss);
PROVIDE(__bss_end__ = _ebss);

ASSERT(SIZEOF(AppACFLSCode) <= LENGTH(flash_rsvd_fls_region), "acfls_code_rom exceeds flash_rsvd_fls_region");
ASSERT(SIZEOF(AppACFLSCode) <= LENGTH(iram_rsvd_fls_region), "acfls_code_rom exceeds iram_rsvd_fls_region");
ASSERT(SIZEOF(AppBSS_NO_CACHE) <= (LENGTH(iram_main_region) - (_ebss - _sbss)), "AppBSS_NO_CACHE exceeds iram_main_region");
