/***
 *
 *  Trade secret of ATI Technologies Inc.
 *  Unpublished work, Copyright 2005 ATI Technologies Inc.
 *
 *  Unpublished work, Copyright 2005 BroadOn Communications Corp.

 *  All rights reserved.  This notice is intended as a precaution against
 *  inadvertent publication and does not imply publication or any waiver
 *  of confidentiality.  The year included in the foregoing notice is the
 *  year of creation of the work.
 *
 ***/

//#include <stdio.h>
#include <diag.h>
#include <sys.h>
#include <rvldv.h>
#include "iop.h"
#include "initvar.h"  // Initialization values for crt0.s; remove soon

/* 
 * This is a simple register test of the AES
 * All registers are read and compared to default values
 */
int aesRegTest()
{
    int err = 0;
    u32 d = 0;
    u32 expected = 0;

    /* Read AES controller reg: 
       Bits 9:0=0 */
    expected = 0;
    d = busRd32((u32)(AES_CTRL_REG));
    busPrintf("%s: Reading AESCTRL(0x%08x) = 0x%x\n", __FILE__, 
        AES_CTRL_REG, d);
    if ((d & 0xFFFFFFC0) != expected) {
        err = 1;
        busPrintf(">>> %s: AESCTRL failed, got=0x%x, exp=0x%x!\n", 
                  __FILE__, d, expected);
    }

    /* Read/Write/Read AES read pointer
       Bits: 3:0=0 */
    expected = 0;
    d = busRd32((u32)(AES_RPTR_REG));
    busPrintf("%s: Reading AESRDPTR(0x%08x) = 0x%x\n", __FILE__, 
        AES_RPTR_REG, d);
    if ((d & 0x0000000F) != expected) {
        err = 1;
        busPrintf(">>> %s: AESRDPTR failed, got=0x%x, exp=0x%x!\n", 
                  __FILE__, d, expected);
    }
    expected = 0xbabecafe;
    busPrintf("%s: Writing AESRDPTR(0x%08x) with 0x%x\n", __FILE__, 
        AES_RPTR_REG, expected);
    busWrt32((u32)(AES_RPTR_REG), expected);
    d = busRd32((u32)(AES_RPTR_REG));
    if (d != (expected & ~0xF)) {
        err = 1;
        busPrintf(">>> %s: AESRDPTR failed, got=0x%x, exp=0x%x!\n", 
                  __FILE__, d, (expected & ~0xF));
    }

    /* Read/Write/Read AES write pointer
       Bits: 3:0=0 */
    expected = 0;
    d = busRd32((u32)(AES_WPTR_REG));
    busPrintf("%s: Reading AESWRPTR(0x%08x) = 0x%x\n", __FILE__,
        AES_WPTR_REG, d);
    if ((d & 0x0000000F) != expected) {
        err = 1;
        busPrintf(">>> %s: AESWRPTR failed, got=0x%x, exp=0x%x!\n", 
                  __FILE__, d, expected);
    }
    expected = 0xbabecafe;
    busPrintf("%s: Writing AESWRPTR(0x%08x) with 0x%x\n", __FILE__, 
        AES_WPTR_REG, expected);
    busWrt32((u32)(AES_WPTR_REG), expected);
    d = busRd32((u32)(AES_WPTR_REG));
    if (d != (expected & ~0xF)) {
        err = 1;
        busPrintf(">>> %s: AESWRPTR failed, got=0x%x, exp=0x%x!\n", 
                  __FILE__, d, (expected & ~0xF));
    }

    /* Read/Write/Read AES write key
       Bits: 3:0=0 
       This register can be written to but not read from */
    expected = 0;
    d = busRd32((u32)(AES_KEY_REG));
    busPrintf("%s: Reading AESWKEY(0x%08x) = 0x%x\n", __FILE__,
        AES_KEY_REG, d);
    if (d != expected) {
        err = 1;
        busPrintf(">>> %s: AESWKEY failed, got=0x%x, exp=0x%x!\n", 
                  __FILE__, d, expected);
    }
    expected = 0xbabecafe;
    busPrintf("%s: Writing AESWKEY(0x%08x) with 0x%x\n", __FILE__, 
        AES_KEY_REG, expected);
    busWrt32((u32)(AES_KEY_REG), expected);
    d = busRd32((u32)(AES_KEY_REG));
    expected = 0;
    if (d != expected) {
        err = 1;
        busPrintf(">>> %s: AESWKEY failed, got=0x%x, exp=0x%x!\n", 
                  __FILE__, d, expected);
    }

    /* Read/Write/Read AES write IV
       Bits: 3:0=0 
       This register can be written to but not read from */
    expected = 0;
    d = busRd32((u32)(AES_IVPTR_REG));
    busPrintf("%s: Reading AESWIV(0x%08x) = 0x%x\n", __FILE__,
        AES_IVPTR_REG, d);
    if (d != expected) {
        err = 1;
        busPrintf(">>> %s: AESWIV failed, got=0x%x, exp=0x%x!\n", 
                  __FILE__, d, expected);
    }
    expected = 0xbabecafe;
    busPrintf("%s: Writing AESWIV(0x%08x) with 0x%x\n", __FILE__, 
        AES_IVPTR_REG, expected);
    busWrt32((u32)(AES_IVPTR_REG), expected);
    expected = 0;
    d = busRd32((u32)(AES_IVPTR_REG));
    if (d != expected) {
        err = 1;
        busPrintf(">>> %s: AESWIV failed, got=0x%x, exp=0x%x!\n", 
                  __FILE__, d, expected);
    }

    return(err);
}


int main()
{
    int err = 0;

    busInit(NULL, NULL, NULL);

    err = aesRegTest();

    if (err)
        simFail(-1);
    else
        simPass(0);

    busClose();

    return 0;

}
