
/*   
 *  sdram test
 *  
 * :set expandtab
 * :set shiftwidth=4
 * :set tabstop=4
 */

#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include "dv.h"

void
fdCheck(unsigned int addr, unsigned int exp)
{
    unsigned int d;

    d = IO_READ(addr);
    printf("%s Read 0x%x get 0x%x exp 0x%x\n", 
           (d == exp) ? "_/" : "X ", addr, d, exp);
}

int 
testProc(void *p)
{
    int sc, dummy, i;
    unsigned int pdata[100], r[32];

    for (i=0; i<100; i++) pdata[i] = random();

    /*  Setup Page table */
    DV_SetMemAccess(0, PT_ACCESS_RW, 0x7f, MEM0_START, DV_CpuSdramSize());

    /* Invalidate all L2 cache */
    DV_GetCacheRamSizes(&dummy, &sc, &dummy);
    DV_CpuBdIoBlockFill(SC_START, sc, 0);

    /* check for non-L2 access */ 
    DV_CpuBdL2Write(DA_START, 4, pdata+2);
    DV_CpuBdL2Read(DA_START, 4, r);
    if (r[0] != pdata[2]) 
        printf("X DA access read word failed\n"); 

    DV_CpuBdL2Write(DA_START, 32, pdata+2);
    DV_CpuBdL2Read(DA_START+4, 20, r);
    for (i=0; i<5; i++)
        if (r[i] != pdata[3+i])
            printf("X Da block access failed @%d\n", i);

    /* Backdoor Write sdram */
    /* Cache invalid, write to sdram directly */
    DV_CpuBdL2Write(MEM0_START+0x80400, 4, pdata);
    DV_CpuBdL2Write(MEM0_START+0x80408, 4, pdata+7);

    /* Check data from front door access 
       Also bring data into cacheline.
       Tag should be valid and clean
     */
    fdCheck(MEM0_START+0x80400, pdata[0]);
    
    /* Backdoor write: Valid and clean */      
    DV_CpuBdL2Write(MEM0_START+0x80404, 4, pdata+2);

    /* Valid and clean, read from L2 cache */
    fdCheck(MEM0_START+0x80408, pdata[7]);
      

    /* Invalid case */    
    DV_CpuBdL2Write(MEM0_START, 4, pdata+3);
    fdCheck(MEM0_START, pdata[3]);
    

    IO_WRITE(MEM0_START+0x8040c, pdata[10]);
    /* Valid and dirty case */
    DV_CpuBdL2Write(MEM0_START+0x80400, 4, pdata+12);
    fdCheck(MEM0_START+0x80400, pdata[0]);
    fdCheck(MEM0_START+0x8040c, pdata[10]);
    
    
    /* Miss and valid dirty case */
    DV_CpuBdL2Write(MEM0_START+0x803f4, 32, pdata+20);
    fdCheck(MEM0_START+0x803f4, pdata[20]);
    fdCheck(MEM0_START+0x803fc, pdata[22]);
    /* Invalid L1 to make sure not L1 hit */
    DV_CpuBdL1DCacheInv(IPC_SOCK_CPU0, MEM0_START+0x80400, 16);
    DV_CpuBdL1DCacheInv(IPC_SOCK_CPU1, MEM0_START+0x80400, 16);
    fdCheck(MEM0_START+0x80404, pdata[24]);
    fdCheck(MEM0_START+0x80410, pdata[27]); 
     

    /* L2 read invalid case */
    BD_IO_WRITE(MEM0_START + 0x7c4, 0xbabecafe);
    DV_CpuBdL2Read(MEM0_START + 0x7c4, 4, r);
    if (r[0] != 0xbabecafe) 
        printf("X at 0x%x \n", MEM0_START + 0x7c4);


    /* valid dirty */
    IO_WRITE(MEM0_START+0x728, 0xdeadbeef);
    DV_CpuBdL2Read(MEM0_START + 0x728, 4, r);
    if (r[0] != 0xdeadbeef) 
        printf("X at 0x%x \n", MEM0_START + 0x728);
    
   
    /* valid clean */
    IO_READ(MEM0_START + 0x7c4);
    DV_CpuBdL2Read(MEM0_START + 0x7c4, 4, r);
    if (r[0] != 0xbabecafe) 
        printf("X at 0x%x \n", MEM0_START + 0x7c4);

    
    /* Cache missing */
    BD_IO_WRITE(MEM0_START + 0x80744, 0xbabecafe);
    DV_CpuBdL2Read(MEM0_START + 0x80744, 4, r);
    if (r[0] != 0xbabecafe) 
        printf("X at 0x%x \n", MEM0_START + 0x80744);

    
    /* block read */
    DV_CpuBdL2Read(MEM0_START + 0x807b8, 32, r);

    return 0;
}
