/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** ThreadX Component                                                     */
/**                                                                       */
/**   Module                                                              */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

    IMPORT      __use_two_region_memory
    IMPORT      __scatterload
    IMPORT      txm_heap

    AREA ||.text||, CODE, READONLY
/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _txm_module_initialize                            Cortex-Mx/AC5     */
/*                                                           6.1.9        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Scott Larson, Microsoft Corporation                                 */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function initializes the module c runtime.                     */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    __scatterload                         Initialize C runtime          */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _txm_module_thread_shell_entry        Start module thread           */
/**************************************************************************/
// VOID   _txm_module_initialize(VOID)

    EXPORT  _txm_module_initialize
_txm_module_initialize
    PUSH    {r4-r12,lr}                         // Save dregs and LR

    B       __scatterload                       // Call ARM func to initialize variables


/* Override __rt_exit function. */

    EXPORT  __rt_exit
__rt_exit

    POP     {r4-r12,lr}                         // Restore dregs and LR
    BX      lr                                  // Return to caller

    EXPORT __user_setup_stackheap
    // returns heap start address in  R0
    // returns heap end address in    R2
    // does not touch SP, it is already set up before the module runs

__user_setup_stackheap
    LDR     r1, _tx_heap_offset                 // load heap offset
    ADD     r0, r9, r1                          // calculate heap base address
    MOV     r2, #TXM_MODULE_HEAP_SIZE           // load heap size
    ADD     r2, r2, r0                          // calculate heap end address
    BX      lr

    ALIGN 4
_tx_heap_offset
    DCDO        txm_heap
    AREA ||.arm_vfe_header||, DATA, READONLY, NOALLOC, ALIGN=2

    IMPORT txm_heap [DATA]


// Dummy main function

    AREA section_main, CODE, READONLY, ALIGN=2
    EXPORT main
main
    BX lr

    END
