/********************************************/
/*71V30 Dual Port RAM Configuration Code    */
/*PIC32MX795F512L-80I/PF                    */
/********************************************/

#include "xc.h"
#include "REN70V05.h"
#include "Delay.h"
 
void REN70V05_Init(void)
{    
   //RD3 = /CS1
    TRISDbits.TRISD3 = 0;
    PORTDbits.RD3 = 1;
    
    //RG15 = /SEM
    TRISGbits.TRISG15 = 0;
    PORTGbits.RG15 = 1;
    
    //INT1 - INTR
    TRISEbits.TRISE8 = 1;
    
    //BUSY - BUSYR
    TRISEbits.TRISE9 = 1;
    
    //RA7 = M/S
    TRISAbits.TRISA7 = 1;
    
    return;
}

void REN70V05_WR(unsigned address_70V05, uint8_t mdata_70V05)
{        
    //save previous address
    //this fixes a hang issue
    int d = PMADDR;
    
    //Save the RD/WR Wait State Value
    int wait_m = PMMODEbits.WAITM;
    //Set new wait state = 0;
    PMMODEbits.WAITM = 0;
    
    PMADDR = address_70V05;
    
    // /CS1 
    PORTDbits.RD3 = 0;
    
    PMDIN = mdata_70V05;
    while(PMMODEbits.BUSY == 1);

    // /CS1
    PORTDbits.RD3 = 1;
    
    //restore previous address
    PMADDR = d;

    //Restore the RD/WR Wait State Value
    PMMODEbits.WAITM = wait_m;    
       
    return;
}

void REN70V05_RD(unsigned address_70V05)
{        
    //save previous address
    //this fixes a hang issue
    int d = PMADDR;
    
    //Save the RD/WR Wait State Value
    int wait_m = PMMODEbits.WAITM;
    //Set new wait state = 0;
    PMMODEbits.WAITM = 0;
    
    PMADDR = address_70V05;
    
    // /CS1
    PORTDbits.RD3 = 0;
        
    //dummy read
    mdata_70V05 = PMDIN;
    while(PMMODEbits.BUSY == 1);
   
    mdata_70V05 = PMDIN;
    while(PMMODEbits.BUSY == 1);

    // /CS1
    PORTDbits.RD3 = 1;
    
    //restore previous address
    PMADDR = d;
    
    //Restore the RD/WR Wait State Value
    PMMODEbits.WAITM = wait_m;    
    
    return;
}

void memtest_70V05(void)
{
    int a;
    int b;
    
    //Save the RD/WR Wait State Value
    int wait_m = PMMODEbits.WAITM;
    //Set new wait state = 0;
    PMMODEbits.WAITM = 0;
    
    /////////////////////////////
    //SRAM write all zeros
    ////////////////////////////
    b = 0;
    for(a=0;a<0x2000;a++)
    {
        PMADDR = a;
        // /CS1 
        PORTDbits.RD3 = 0;

        mdata_70V05 = b;
        PMDIN = mdata_70V05;
        while(PMMODEbits.BUSY == 1);

        // /CS1
        PORTDbits.RD3 = 1;
    }
    /////////////////////////////
    
    //SRAM read and verify 
    for(a=0;a<0x2000;a++)
    {
        PMADDR = a;
        address_70V05 = a;
        mdata_70V05 = 0;
        // /CS1
        PORTDbits.RD3 = 0;

        //dummy read
        mdata_70V05 = PMDIN;
        while(PMMODEbits.BUSY == 1);

        mdata_70V05 = PMDIN;
        while(PMMODEbits.BUSY == 1);

        if(mdata_70V05 != 0)
        {
            sram_test = 0;
            sram_test_data[a] = 0;
        }
        else
        {
            sram_test = 1;
            sram_test_data[a] = 1;
        }
        // /CS1
        PORTDbits.RD3 = 1;
    }

    //Restore the RD/WR Wait State Value
    PMMODEbits.WAITM = wait_m;    
    
    return;
}
