/*******************************************************************************
 * Copyright (c) 2018-2021 Renesas Electronics Corporation. All rights reserved.
 *
 *     RENESAS ELECTRONICS CONFIDENTIAL AND PROPRIETARY
 *
 * This software is provided as reference/sample code under the license
 * agreement between Renesas Electronics Corporation and licensee (the
 * "License Agreement") and shall be treated as specified in the License
 * Agreement.
 * These instructions, statements, and software are the confidential
 * information of Renesas Electronics Corporation. They must be used and
 * modified solely for the purpose for which it was furnished by Renesas
 * Electronics Corporation. All or part of these instructions, statements and
 * software must not be reproduced nor disclosed to any third party in any
 * form, unless permitted by the License Agreement.
 *
 * THIS SOFTWARE IS PROVIDED BY RENESAS ELEOCTRONICS CORPORATION "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
 * SATISFACTORY QUALITY, ACCURACY, TITLE AND NON-INFRINGEMENT ARE DISCLAIMED.
 * IN NO EVENT SHALL RENESAS ELECTRONICS CORPORATION BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, PUNITIVE, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * 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.
 ******************************************************************************/

/*******************************************************************************
 * DESCRIPTION   : Loader start code
 ******************************************************************************/

    .global code_start
    .global _loader_main
    .global _wdt_init
    .global ___ghsend_ICU_stack /* LRAM stack Hardware end address  */
    .global ___ghsend_RT_stack  /* RT-SRAM stack Logical end address    */

    .section ".reset", "ax"
    .align 2
code_start:
//; initialize registers
//#
//#_start_icu:
    mov code_start_2, r1
    jmp r1
code_start_2:
    mov r0, r1
    mov r0, r2
    mov r0, r3
    mov r0, r4
    mov r0, r5
    mov r0, r6
    mov r0, r7
    mov r0, r8
    mov r0, r9
    mov r0, r10
    mov r0, r11
    mov r0, r12
    mov r0, r13
    mov r0, r14
    mov r0, r15
    mov r0, r16
    mov r0, r17
    mov r0, r18
    mov r0, r19
    mov r0, r20
    mov r0, r21
    mov r0, r22
    mov r0, r23
    mov r0, r24
    mov r0, r25
    mov r0, r26
    mov r0, r27
    mov r0, r28
    mov r0, r29
    ldsr    r0, 0, 0
    ldsr    r0, 16, 0

//clear stack area of BootROM
    mov ___ghsbegin_top_stack, r6
    mov ___ghsbegin_end_stack, r7
stack_clear:
    st.dw   r0, 0[r6]
    addi    8, r6, r6
    cmp r7, r6
    bl  stack_clear

//RAM clear
    mov ___ghsbegin_top_local_ram, r6
    mov ___ghsbegin_end_local_ram, r7
    mov r0, r1
loop_clear:
    st.dw   r0, 0[r6]
    addi    8, r6, r6
    cmp r7, r6
    bl  loop_clear

//set global pointer
    mov ___ghsbegin_sdabase, gp
//set stack pointer
    mov ___ghsend_RT_stack, sp

// ***************
// RAM CODE COPY
// ***************
    mov ___ghsbegin_dst_local_ram_start, r6
    mov ___ghsend_dst_local_ram_end, r7
    mov ___ghsbegin_src_local_ram_start, r8
    mov 0xEA000000, r9
    sub r9, r8
loop_code_copy:
    ld.dw   0[r8], r10
    st.dw   r10, 0[r6]
    addi    8, r6, r6
    addi    8, r8, r8
    cmp r7, r6
    bl  loop_code_copy

//ICUMXA Watchdog Timer initialize
    jarl    _wdt_init, lp

    mov local_ram_main, r1
    mov _loader_main, r2
    jmp r1
.section ".text"
local_ram_main:


    jarl    [r2], lp

    jmp [r10]
    nop
    halt




