/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2024. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */
/**
 * @file Startup.c
 *
 * @brief This file provides GCC DATA, RAM code and BSS initialization.
 *
 */

/*==============================================INCLUDE FILES=======================================*/
/*PRQA S 1503 EOF #init_data_bss was used at GCC startup.s */
/*PRQA S 3684 EOF #Variables are defined at linker file    */
/*PRQA S 3408 EOF #init_data_bss was used at GCC startup.s */
/*PRQA S 3387 EOF #i++ was used to iterating over arrays   */

#include <stdint.h>

/*=====================================SOURCE FILE VERSION INFORMATION==============================*/

/*============================================FILE VERSION CHECKS===================================*/

/*=================================================CONSTANTS========================================*/

/*============================================DEFINES AND MACROS====================================*/

/*===================================================ENUMS==========================================*/

/*=======================================STRUCTURES AND OTHER TYPEDEFS==============================*/
/**
 * @brief The type used to copy from source to destination.
 */
typedef struct
{
    uint8_t *dst_start;  /* Start address of destination */
    uint8_t *src_start;  /* Start address of source */
    uint8_t *src_end;    /* End address of source */
} Sys_CopyType;

/**
 * @brief The type used to zero initialization.
 */
typedef struct
{
    uint8_t *dst_start; /* Start address of destination */
    uint8_t *dst_end;   /* End address of destination */
} Sys_ZeroType;

/*===========================================VARIABLE DECLARATIONS==================================*/

/*===========================================VARIABLE INITED========================================*/

/*============================================FUNCTION PROTOTYPES===================================*/
/**
 * @brief     This service initializes DATA, RAM code and BSS.
 * @note      Function ID: [DES_BOOT_API_003]
 * @return    void
 */
void init_data_bss(void)
{
    extern uint32_t __DATA_ROM[];
    extern uint32_t __DATA_RAM[];
    extern uint32_t __DATA_END[];

    extern uint32_t __CODE_RAM[];
    extern uint32_t __CODE_ROM[];
    extern uint32_t __CODE_END[];

    extern uint32_t __BSS_START[];
    extern uint32_t __BSS_END[];

    const Sys_CopyType Copy_Table[] =
    {
        {(uint8_t *)__DATA_RAM, (uint8_t *)__DATA_ROM, (uint8_t *)__DATA_END},
        {(uint8_t *)__CODE_RAM, (uint8_t *)__CODE_ROM, (uint8_t *)__CODE_END},

    };

    const Sys_ZeroType Zero_Table[] =
    {
        {(uint8_t *)__BSS_START, (uint8_t *)__BSS_END},
    };

    volatile uint8_t i;
    volatile const uint8_t *s;
    volatile uint8_t *d;

    /* Copy initialized data from ROM to RAM */
    for (i = 0; i < (sizeof (Copy_Table) / sizeof(Copy_Table[0])); i++)
    {
        s = Copy_Table[i].src_start;
        d = Copy_Table[i].dst_start;

        /* Simple, byte oriented memcpy. */
        while (s != Copy_Table[i].src_end)
        {
            *d = *s;
            d++;
            s++;
        }
    }

    /* Initialize bss to zero */
    for (i = 0; i < (sizeof (Zero_Table) / sizeof(Zero_Table[0])); i++)
    {
        d = Zero_Table[i].dst_start;

        while (d != Zero_Table[i].dst_end)
        {
            *d = 0x00U;
            d++;
        }
    }
}
