/**
*   @copyright       © 2026 Microchip Technology Inc.
    Microchip Technology Inc. and its subsidiaries. You may use this
    software and any derivatives exclusively with Microchip products.

    THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
    EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
    WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
    PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
    WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.

    IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
    INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
    WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
    BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
    FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
    ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
    THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.

    MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
    TERMS. 
*
*************************************************************************/

/**
*  @file        diag_cpu_regresetstate_test.c
*  @version     1.0
*  @brief       This file has the implementation for the CPU
                Register reset state test diagnostic. @see diag_cpu.h
*
*************************************************************************/
/*Function headers*/
#include "diag_cpu.h"
#include "diag_cpu_shared.h"

uint32_t DIAG_CPU_RegResetStateTest(void)
{
    uint32_t result;
    static const diagCpuResetValueLookup resetValueLookup[DIAG_CPU_TEST_REGS] = {
                                            {0x00000000U,0xFFFFFFFFU},/*RCOUNT*/
                                            {0x00000000U,0x00000007U},/*DISIIPL*/
                                            {0x00000020U,0x000017F3U},/*CORCON*/
                                            {0x000000FFU,0x0000C0FFU},/*MODCON*/
                                            {0x00000000U,0x00FFFFFFU},/*XMODSRT*/
                                            {0x00000001U,0x00FFFFFFU},/*XMODEND*/
                                            {0x00000000U,0x00FFFFFFU},/*YMODSRT*/
                                            {0x00000001U,0x00FFFFFFU},/*YMODEND*/
                                            {0x00000000U,0x00007FFFU},/*XBREV*/
                                            {0x00000000U,0x00FFFFFFU},/*PCTRAP*/
                                            {0x00000000U,0xFFFFFFFFU},/*FEX*/
                                            {0x00000000U,0xFFFFFFFFU},/*FEX2*/
                                            {0x00000000U,0xFFFFFFFFU},/*PCHOLD*/
                                            {0x00000000U,0x00000000U},/*VFA - mask 0: skipped, value is linker-dependent*/
    };
    /* LDRA_EXCLUDE 606 S <justification start>Pointer to array wrongly identified as function pointer. Violation Ignored.<justification end> */
    cpuRegMap_t cpuRegMapPtr = (cpuRegMap_t)CPU_RCOUNT_REG_BASE;
    uint32_t diagCpuRegResetFailCount = 0U;
    uint32_t loopCount = 0U;
   
    while(loopCount < DIAG_CPU_TEST_REGS)   
    {
        if(((*cpuRegMapPtr)[loopCount] & resetValueLookup[loopCount].resetMask)!= resetValueLookup[loopCount].resetValue)
        {
            diagCpuRegResetFailCount += 1U;
            (*cpuRegMapPtr)[loopCount] = ((resetValueLookup[loopCount].resetValue) | (~resetValueLookup[loopCount].resetMask));        
        }
        
        loopCount += 1U;
    }   
    if(diagCpuRegResetFailCount == 0U)
    {
        DIAG_CPU_SetRetValue(DIAG_PASS);
    }
    else
    {
        DIAG_CPU_SetRetValue(DIAG_FAIL);
    }
    result = DIAG_CPU_GetRetValue();
    return result;
/*LDRA_EXCLUDE 88 S <justification start> functionally implemented to test assembly instruction behavior, hence violation ignored <justification end> */    
}
