/**
*   @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_pc_programcountertest.c
 *  @version    1.0
 *  @brief      This file has the implementation for the CPU PC test
 *              
 *************************************************************************/

#include <stddef.h>
#include <stdint.h>
#include "diag_pc.h"

#define DIAG_PC_MAXFUNC 3U
#define DIAG_PC_EXPECTEDNUMCOUNT 7U

/*declaration for helper functions*/
/*LDRA_EXCLUDE 62 X <justification start> The function here is intended to return a void and the usage is as per the design. Hence violation ignored <justification end> */
static void DIAG_PC_HelperFunc1(void);
/*LDRA_EXCLUDE 62 X <justification start> The function here is intended to return a void and the usage is as per the design. Hence violation ignored <justification end> */
static void DIAG_PC_HelperFunc2(void);
/*LDRA_EXCLUDE 62 X <justification start> The function here is intended to return a void and the usage is as per the design. Hence violation ignored <justification end> */
static void DIAG_PC_HelperFunc3(void);

typedef void (*AppFunctionPtr)(void);
static const AppFunctionPtr funcArray[DIAG_PC_MAXFUNC] = {DIAG_PC_HelperFunc1, DIAG_PC_HelperFunc2, DIAG_PC_HelperFunc3};

static uint16_t numFuncCounter = 0U;

static void __attribute__((optimize("-O0"))) DIAG_PC_HelperFunc1(void)
{
	numFuncCounter |= (1U << 0U);
}

uint32_t DIAG_PC_ProgramCounterTest(void)
{
    uint16_t index = 0;
    uint32_t result = 0;
    while(index < DIAG_PC_MAXFUNC)
    {
/*LDRA_EXCLUDE 151 D <justification start> The value is incremented in every function and compared against a fixed value. Violation ignored<justification end>*/
        (*funcArray[index])();
        index += 1U;
    }
    /*The fail case condition cannot be achieved by any fault injection mechanism
      programmatically. There is no way we can make the program counter NOT 
      access the called functions. Hence this block of code is put under LDRA
      annotation*/
    /*LDRA_NOANALYSIS*/
    if(numFuncCounter == DIAG_PC_EXPECTEDNUMCOUNT)
    {
        result = DIAG_PASS;
    }
    else
    {
        result = DIAG_FAIL;
    }
    /*LDRA_ANALYSIS*/
    /*Reset the counter value*/
    numFuncCounter = 0U;
    return result;
}

static void __attribute__((optimize("-O0"))) DIAG_PC_HelperFunc2(void)
{
   numFuncCounter |= (1U << 1U);
}

static void __attribute__((optimize("-O0"))) DIAG_PC_HelperFunc3(void)
{
    numFuncCounter |= (1U << 2U);
}
