/*
 * Copyright (c) 2024 HPMicro
 * SPDX-License-Identifier: BSD-3-Clause
 */

define exported symbol _link_file_version_2 = 1;
define symbol UF2_BOOTLOADER_RESERVED_LENGTH = 0x20000;

define memory with size = 4G;

/* Sizes */
if (isdefinedsymbol(CSTACK_SIZE)) {
  define symbol __size_cstack__        = CSTACK_SIZE;
} else {
  define symbol __size_cstack__        = 0x4000;
}

if (isdefinedsymbol(HEAP_SIZE)) {
  define symbol __size_heap__          = HEAP_SIZE;
} else {
  define symbol __size_heap__          = 0x4000;
}

if (isdefinedsymbol(_flash_size)) {
  define symbol __size_flash__          = _flash_size;
} else {
  define symbol __size_flash__          = 8M;
}

if (isdefinedsymbol(_extram_size)) {
  define symbol __size_exram__          = _extram_size;
} else {
  define symbol __size_exram__          = 256M;
}

define symbol __size_safestack__        = 256;

/* Regions */
define region XPI0 = mem:[from 0x80000000 + UF2_BOOTLOADER_RESERVED_LENGTH size (__size_flash__ - UF2_BOOTLOADER_RESERVED_LENGTH) ];   /* XPI0 */
define region ILM = mem:[from 0x00000000 size 256k];         /* ILM */
define region DLM = mem:[from 0x00080000 size 256k];         /* DLM */
define region AXI_SRAM = mem:[from 0x01200000 size 512k];    /* AXI SRAM */
define region SDRAM  = mem:[from 0x40000000 size __size_exram__ - 32M];
define region NONCACHEABLE_RAM = mem:[from 0x40000000 + __size_exram__ - 32M size 32M];
define region AHB_SRAM = mem:[from 0xF0400000 size 32k];
define region APB_SRAM = mem:[from 0xF4130000 size 16k];

/* Blocks */
define block vectors with fixed order                       { section .vector_table, section .isr_vector };
define block vectors_s with fixed order                     { section .vector_s_table, section .isr_s_vector };

define block HEAP  with size = __size_heap__,  alignment = 8 { };
define block CSTACK with size = __size_cstack__, alignment = 8 { };
define block SAFESTACK with size = __size_safestack__, alignment = 8 { };
define block RW_DATA with static base GPREL { rw data };

define block cherryusb_usbh_class_info                       { section .usbh_class_info };
define block framebuffer                                     { section .framebuffer };

define block rtthread_FSymTab                                { section FSymTab };
define block rtthread_VSymTab                                { section VSymTab };
define block rtthread_rti_fn with alphabetical order         { section .rti_fn* };
define block rtthread_RTMSymTab                              { section RTMSymTab };

/* Symbols */
define exported symbol __noncacheable_start__ = start(NONCACHEABLE_RAM);
define exported symbol __noncacheable_end__ = end(NONCACHEABLE_RAM) + 1;

/* Initialization */
do not initialize                           { section .noncacheable };
do not initialize                           { section .non_init, section .non_init.*, section .*.non_init, section .*.non_init.* };
do not initialize                           { section .no_init, section .no_init.*, section .*.no_init, section .*.no_init.* };   // Legacy sections, kept for backwards compatibility
do not initialize                           { section .noinit, section .noinit.*, section .*.noinit, section .*.noinit.* };       // Legacy sections, used by some SDKs/HALs
do not initialize                           { section .backup_sram};

initialize by copy with packing=auto        { section .noncacheable.init };
initialize by copy with packing=auto        { section .fast, section .fast.*, section .*.fast, section .*.fast.* };               // "RAM Code" sections
initialize by copy with packing=none        { section .vector_table };
initialize by copy with packing=none        { section .isr_vector };
initialize by copy with packing=none        {  section .vector_s_table };
initialize by copy with packing=none        {  section .isr_s_vector };
initialize by copy with packing=auto        { section .usbh_class_info };
initialize by copy                          { readwrite };

/* Placement */
place at start of XPI0                      { section .uf2_signature };
place in XPI0                               { symbol _start};
place in XPI0                               { block rtthread_FSymTab,
                                              block rtthread_VSymTab,
                                              block rtthread_rti_fn,
                                              block rtthread_RTMSymTab,
                                              readonly
                                            };
place at start of ILM                       { block vectors, block vectors_s };
place in ILM                                {
                                             section .fast, section .fast.*,                       // "ramfunc" section
                                            };
place in SDRAM                              { block cherryusb_usbh_class_info };
place in SDRAM                              { block framebuffer };
place in AXI_SRAM | SDRAM                   {
                                              block RW_DATA
                                            };
place in NONCACHEABLE_RAM                   { section .noncacheable, section .noncacheable.init, section .noncacheable.bss };  // Noncacheable
place in AHB_SRAM                           { section .ahb_sram};                                   // AHB SRAM memory
place in APB_SRAM                           { section .backup_sram};                                // Backup SRAM memory
place in DLM                                { section .fast_ram};                                   // Fast access memory
place in SDRAM                              { block HEAP };                                         // Heap reserved block
place at end of DLM                         { block CSTACK, block SAFESTACK };                      // Stack reserved block, Safe Stack reserved block

/* Keep */
keep                                        {
                                              section .uf2_signature
                                            };
keep                                        { section .usbh_class_info,
                                              section FSymTab,
                                              section VSymTab,
                                              section .rti_fn*,
                                              section RTMSymTab
                                            };
keep symbol __iar_cstart_init_gp;
