/**
*   @copyright       © 2025 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. 
*
*************************************************************************/

#include "test.h"
#include "diag_sram.h"
#include "diag_sram_shared.h"
#include <stddef.h>
#include <stdint.h>
#include <xc.h>

static uint32_t testResult;
static uint32_t status;

#if DEMO_SRAM_AVAILABLE

uint32_t    dupsrc[5] __attribute__((address(0x4180))) = { 0x0102, 0x0304, 0x0506, 0x0708, 0x090A };
uint32_t    dupdest[5] __attribute__((address(0x41C0))) = { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 };

uint32_t __attribute__ ((address(0x6000))) singlebitErrorVar = 0;
uint32_t __attribute__ ((address(0x6100))) doublebitErrorVar = 0;
uint32_t __attribute__ ((address(0xD000))) singlebitErrorVarYMem = 0;
uint32_t __attribute__ ((address(0xD100))) doublebitErrorVarYMem = 0;

void __attribute__ ( ( interrupt, no_auto_psv ) ) _XRAMECCInterrupt(void)
{
    DIAG_SRAM_SetSramSingleBitIsrEntryStatus(DIAG_SRAM_SINGLE_BIT_ERROR_ISR_TRIGGERED);
    IFS0bits.XRAMECCIF = 0;
}

void __attribute__ ( ( interrupt, no_auto_psv ) ) _YRAMECCInterrupt(void)
{
    DIAG_SRAM_SetSramSingleBitIsrEntryStatus(DIAG_SRAM_SINGLE_BIT_ERROR_ISR_TRIGGERED);
    IFS0bits.YRAMECCIF = 0;
}

uint32_t DEMO_SRAM_TestTask()
{
    status = DEMO_TEST_PASS;
    
    testResult = DIAG_SRAM_SingleDoubleErrorDetectionTest(SINGLE_BIT_ERROR_EVENT,&singlebitErrorVar,22);
    if(testResult == DIAG_FAIL)
    {
        status = DEMO_TEST_FAIL;
    }
   
    testResult = DIAG_SRAM_SingleDoubleErrorDetectionTest(DOUBLE_BIT_ERROR_EVENT,&doublebitErrorVar,28);
    if(testResult == DIAG_FAIL)
    {
        status = DEMO_TEST_FAIL;
    }
   
    testResult = DIAG_SRAM_SingleDoubleErrorDetectionTest(SINGLE_BIT_ERROR_EVENT,&singlebitErrorVarYMem,22);
    if(testResult == DIAG_FAIL)
    {
        status = DEMO_TEST_FAIL;
    }

    
    testResult = DIAG_SRAM_SingleDoubleErrorDetectionTest(DOUBLE_BIT_ERROR_EVENT,&doublebitErrorVarYMem,28);
    if(testResult == DIAG_FAIL)
    {
        status = DEMO_TEST_FAIL;
    }

    testResult = DIAG_SRAM_ReplicationWrite(dupsrc, dupdest, 5);
    testResult = DIAG_SRAM_IsBackedUpDataValid(dupsrc, dupdest, 5);
    if(testResult == DIAG_FAIL)
    {
        status = DEMO_TEST_FAIL;
    }
 
    return status;
}
#endif
