/* Copyright 2023 Vulcalien
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

/* Based on lnkscript v1.3 by Jeff Frohwein */

OUTPUT_FORMAT(elf32-littlearm)
ENTRY(_start)

MEMORY
{
    ROM   : ORIGIN = 0x08000000, LENGTH = 32M
    IWRAM : ORIGIN = 0x03000000, LENGTH = 32K
    EWRAM : ORIGIN = 0x02000000, LENGTH = 256K
}

__sp_usr = ORIGIN(IWRAM) + LENGTH(IWRAM) - 0x100;
__sp_irq = ORIGIN(IWRAM) + LENGTH(IWRAM) - 0x60;

SECTIONS
{
    /* --------------- */
    /* ----- ROM ----- */
    /* --------------- */

    .text : ALIGN(4)
    {
        __text_start = ABSOLUTE(.);

        *(.text)
        *(.text.*)

        *(.glue_7)
        *(.glue_7t)

        . = ALIGN(4);
        __text_end = ABSOLUTE(.);
    } >ROM =0xff
    __text_size = SIZEOF(.text);

    .rodata : ALIGN(4)
    {
        __rodata_start = ABSOLUTE(.);

        *(.rodata)
        *(.rodata.*)

        . = ALIGN(4);
        __rodata_end = ABSOLUTE(.);
    } >ROM =0xff
    __rodata_size = SIZEOF(.rodata);

    /* ----------------- */
    /* ----- IWRAM ----- */
    /* ----------------- */

    .iwram : ALIGN(4)
    {
        __iwram_start = ABSOLUTE(.);

        *(.iwram)
        *(.iwram.*)

        . = ALIGN(4);
        __iwram_end = ABSOLUTE(.);
    } >IWRAM AT>ROM =0xff
    __iwram_size = SIZEOF(.iwram);
    __iwram_lma = LOADADDR(.iwram);

    .bss : ALIGN(4)
    {
        __bss_start = ABSOLUTE(.);

        *(.bss)
        *(.bss.*)

        *(COMMON)

        . = ALIGN(4);
        __bss_end = ABSOLUTE(.);
    } >IWRAM
    __bss_size = SIZEOF(.bss);

    .data : ALIGN(4)
    {
        __data_start = ABSOLUTE(.);

        *(.data)
        *(.data.*)

        . = ALIGN(4);
        __data_end = ABSOLUTE(.);
    } >IWRAM AT>ROM =0xff
    __data_size = SIZEOF(.data);
    __data_lma = LOADADDR(.data);

    /* ----------------- */
    /* ----- EWRAM ----- */
    /* ----------------- */

    .ewram : ALIGN(4)
    {
        __ewram_start = ABSOLUTE(.);

        *(.ewram)
        *(.ewram.*)

        . = ALIGN(4);
        __ewram_end = ABSOLUTE(.);
    } >EWRAM AT>ROM =0xff
    __ewram_size = SIZEOF(.ewram);
    __ewram_lma = LOADADDR(.ewram);

    .sbss : ALIGN(4)
    {
        __sbss_start = ABSOLUTE(.);

        *(.sbss)
        *(.sbss.*)

        . = ALIGN(4);
        __sbss_end = ABSOLUTE(.);
    } >EWRAM
    __sbss_size = SIZEOF(.sbss);
}
