/*

MIT License

Copyright (c) 2019 PCSX-Redux authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

OUTPUT_FORMAT("binary")
EXTERN(_ZN10JabyEngine5startEv)
ENTRY(_ZN10JabyEngine5startEv)

TLOAD_ADDR = DEFINED(TLOAD_ADDR) ? TLOAD_ADDR : 0x80010000;

MEMORY {
    zero         : ORIGIN = 0x0                   LENGTH = 0x100
    bios         : ORIGIN = 0x100,                LENGTH = 0x500
    loader       : ORIGIN = (TLOAD_ADDR - 0x800), LENGTH = 2048
    ram(rwx)     : ORIGIN = 0x80010000,           LENGTH = 2M - 0x10000
    ram_alt(rwx) : ORIGIN = 0x80010000,           LENGTH = 2M - 0x10000
    dcache       : ORIGIN = 0x1f800000,           LENGTH = 0x400
}

__ram_top       = ORIGIN(ram) + LENGTH(ram);
__sp            = __ram_top - 0x100;

__dcache        = ORIGIN(dcache);
__dcache_top    = ORIGIN(dcache) + LENGTH(dcache);

__bss_len             = (__bss_end             - __bss_start);
__ftext_len           = (__ftext_end           - __ftext_start);
__fdata_len           = (__planschi_end        - __fdata_start);
__persistent_lbas_len = (__persistent_lbas_end - __persistent_lbas_start);

__stack_start   = ORIGIN(ram) + LENGTH(ram);


SECTIONS {
    .zero (NOLOAD) : {
        _ZN10JabyEngine3SPU5voiceE = .;
    } > zero
    
    .bios (NOLOAD) : {
        _ZN10JabyEngine15table_of_tablesE = .;
    } > bios

    /DISCARD/ : { *(.MIPS.abiflags) }

    /* Everything is statically linked, so discard PLTs. */
    /DISCARD/ : { *(.rel.iplt) *(.rela.iplt) *(.rel.plt) *(.rela.plt) *(.plt) *(.iplt) }

    /* Discard things that the standard link script drops, too. */
    /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) *(.gnu.*) *(.comment) }
}

/*Overlay sections created by mkoverlay*/
INCLUDE Overlays.ld
SECTIONS {
    .planschi __engine_bss_end : SUBALIGN(4)
	{
		__planschi_start    = .;
		__boot_loader_start = .;

		*libJabyEngine_*.a:*_boot.o(.text.startup._GLOBAL__*)
		*_boot.o(.text.startup._GLOBAL__*)
		*libJabyEngine_*.a:*_boot.o(.ctors)
		*_boot.o(.ctors)

		*libJabyEngine_*.a:*_boot.o(.text.*)
		*_boot.o(.text.*)
		*libJabyEngine_*.a:*_boot.o(.rodata*)
		*_boot.o(.rodata*)
		*libJabyEngine_*.a:*_boot.o(.sdata*)
		*_boot.o(.sdata*)
		*libJabyEngine_*.a:*_boot.o(.data*)
		*_boot.o(.data*)
		*libJabyEngine_*.a:*_boot.o(.sbss*)
		*_boot.o(.sbss*)
		*libJabyEngine_*.a:*_boot.o(.bss*)
		*_boot.o(.bss*)
		*libJabyEngine_*.a:*_boot.o(*)
		*_boot.o(*)

		. = ALIGN(4);
		__boot_loader_end = .;
		/*Only needed for the PSX BIOS to load the entire game*/
		. = ALIGN(2048);
        __planschi_end = .;
	} > ram
}

SECTIONS {
    .PSX_EXE_Header : {
        /*
            0x0000 - 0x0007 : "PS-X EXE"
        */
        BYTE(80); BYTE(83); BYTE(45); BYTE(88); BYTE(32); BYTE(69); BYTE(88); BYTE(69);

        /* 0x0008 - 0x000F : skip text_off and data_off since they're not supported by the PS1 BIOS */
        LONG(0); LONG(0);

        /* 0x0010 - 0x0013 : entry point */
        LONG(ABSOLUTE(_ZN10JabyEngine5startEv));

        /* 0x0014 - 0x0017 : initial value of $gp */
        LONG(0);

        /* 0x0018 - 0x001B : Memory address to load "text" section to. */
        /*
            NOTE: The "text" section is actually all of the "load"
            sections of the file including .text, .rodata, .data.
            etc.
        */
        LONG(TLOAD_ADDR);

        /* 0x001C - 0x001F :  size, in bytes, of the "text" section. */
        LONG(__persistent_lbas_len + __ftext_len + __fdata_len);

        /* 0x0020 - 0x002F :
            Skip "data_addr", "data_size", "bss_addr" and "bss_size".
            None of these are supported by retail PS1 BIOS.
        */
        LONG(0); LONG(0);
        LONG(0); LONG(0);

        /* 0x0030 - 0x0033 :  Initial stack address. */
        LONG(DEFINED(_sp) ? ABSOLUTE(_sp) : 0x801FFF00);

        /* 0x0034 - 0x0037 : Initial stack size, set it to 0. */
        LONG(0);

        /* Skip the remaining fields as they're not supported by the BIOS */
        /* e.g. 2048 header bytes minus whatever we've actually used */
        . = . + 1992;
    } > loader

    .persistent_lbas TLOAD_ADDR : {
        __persistent_lbas_start = .;
            __persistent_lbas = .;
            KEEP(*(.header.lbas))
        . = ALIGN(4);
        __persistent_lbas_end = .;
    } > ram

    __ftext_start = ABSOLUTE(.);
    .text : {
        __text_start = .;
            *(.start)
            *(.init)
            KEEP (*(SORT_NONE(.fini)))
            *(.text.unlikely .text.*_unlikely .text.unlikely.*)
            *(.text.exit .text.exit.*)
            *(.text.startup .text.startup.*)
            *(.text.hot .text.hot.*)
            *(.text .stub .text.* .gnu.linkonce.t.*)

            . = ALIGN(16);
            KEEP(*(.init))
            . = ALIGN(16);
            KEEP(*(.fini))
        . = ALIGN(16);
        __text_end = .;
    } > ram
    __ftext_end = ABSOLUTE(.);

    __fdata_start = ABSOLUTE(.);
    .rodata : {
        *(.rodata .rodata.* .rdata .rdata.* .gnu.linkonce.r.*)
        . = ALIGN(16);
        __preinit_array_start = .;
        KEEP (*(.preinit_array))
        __preinit_array_end = .;

        . = ALIGN(16);
        __init_array_start = .;
        KEEP (*(SORT(.init_array.*)))
        KEEP (*(.init_array))

        . = ALIGN(16);
        KEEP (*crtbegin.o(.ctors))
        KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors))
        KEEP (*(SORT(.ctors.*)))
        KEEP (*crtend.o(.ctors))
        __init_array_end = .;

        . = ALIGN(16);
        __fini_array_start = .;
        KEEP (*(.fini_array))
        KEEP (*(SORT(.fini_array.*)))

        KEEP (*crtbegin.o(.dtors))
        KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors))
        KEEP (*(SORT(.dtors.*)))
        KEEP (*crtend.o(.dtors))
        __fini_array_end = .;
        __build_id = .;
        *(.note.gnu.build-id)
        __build_id_end = .;
    } > ram

    .rodata1 : {
        *(.rodata1)
    } > ram

    .data : {
        __data_start = .;
            *(.a0table)
            *(.data .data.* .gnu.linkonce.d.*)
            *(.data1)
            *(.sdata .sdata.* .gnu.linkonce.s.*)
            *(.sdata2 .sdata2.* .gnu.linkonce.s2.*)
            *(.got.plt)
            *(.got)
        __data_end  = .;
    } > ram

    .engine_bss : {
        __engine_bss_start = .;
            *libJabyEngine_*.a:*(.dynsbss)
            *libJabyEngine_*.a:*(.sbss .sbss.* .gnu.linkonce.sb.*)
            *libJabyEngine_*.a:*(.scommon)
            *libJabyEngine_*.a:*(.dynbss)
            *libJabyEngine_*.a:*(.bss .bss.* .gnu.linkonce.b.*)
            *libJabyEngine_*.a:*(COMMON)
        . = ALIGN(4);
        __engine_bss_end = .;
    } > ram
    __fdata_end = .;

    .bss __persistent_overlay_end (NOLOAD) : {
        __bss_start = .;
        *(.dynsbss)
        *(.sbss .sbss.* .gnu.linkonce.sb.*)
        *(.scommon)
        *(.dynbss)
        *(.bss .bss.* .gnu.linkonce.b.*)
        *(COMMON)
        . = ALIGN(4);
        __bss_end = .;
    } > ram

    __heap_start = __bss_end;
    __end = .;
}