/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */

#include <ios.h>
#include <hw.h>
#include <iostypes.h>
#include <ioslibc.h>

#define STACK_SIZE 1024
const u8 _initStack[STACK_SIZE];
const u32 _initStackSize = sizeof(_initStack);
const u32 _initPriority = 10;

/* Thread created by idle process */
u8 testthrStack[1024]; /* 1024 is hardwired as stack size for this thread
                        * when the idle thread starts it. Please change
                        * it if you change this.
                        */


static
__attribute__((aligned(64))) u32 SHARefData[16] = {
        0x61626380, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x00000018 };

/*
static
__attribute__((aligned(64))) u32 SHARefHash[5] = {
        0xa9993e36, 0x4706816a, 0xba3e2571, 0x7850c26c, 0x9cd0d89d };
*/


void 
sha_intr_test(void)
{
	IOSMessageQueueId mqid;
	IOSMessage array;
	u32 i, reg;
	u32 numBlocks;
    u32 srcAddr = (u32) SHARefData;
  

    /* Register handler for AES event */
    /* Create a message queue and register for timer events */
    if ((mqid = IOS_CreateMessageQueue(&array, 1)) < 0) {
	    printf("Unable to create timer message queue: %d\n", mqid);
	    goto done;
    }
    if (IOS_HandleEvent(IOS_EVENT_SHA, mqid, 0) != IOS_ERROR_OK) {
	    printf("Unable to register SHA event handler: %d\n");
	    goto done;
    }


    /* Enable SHA access to SRAM */
    reg = busRd32(ACRSRNPROT_REG) | ACRSRNPROT_SHAEN_MASK;
    busWrt32(ACRSRNPROT_REG, reg);

    /* Init source data */
    numBlocks = 1;
    for (i = 0; i < (numBlocks*16); i++) {
	    IO_WRITE(srcAddr+(4*i), SHARefData[i]);
    }
    
  while(1) {
    printf("Will setup SHA for 1 block at 0x%x \n", srcAddr);
    /* Clear interrupt and stop any operation */
    IO_WRITE(SHA_CTRL_REG, 0);

    /* Setup initial hash value */
    IO_WRITE(SHA_BUF0_REG, 0x67452301);  /* Data is in big endian */
    IO_WRITE(SHA_BUF1_REG, 0xefcdab89);
    IO_WRITE(SHA_BUF2_REG, 0x98badcfe);
    IO_WRITE(SHA_BUF3_REG, 0x10325476);
    IO_WRITE(SHA_BUF4_REG, 0xc3d2e1f0);

    /* Address must be 64-byte (bit 5:0) aligned */
    IO_WRITE(SHA_RPTR_REG, srcAddr);

    /* Start hash operation */
    reg = SHA_CTRL_EXEC | SHA_CTRL_INTR | 
          ((numBlocks - 1) & SHA_CTRL_SIZE_MASK);
    IO_WRITE(SHA_CTRL_REG, reg);
	  
    if (IOS_ReceiveMessage(mqid, NULL, IOS_MESSAGE_BLOCK) == IOS_ERROR_OK) {
		  printf("Received SHA completion event\n");
	  }
  }
done:
    while(1);
}

void
main(u32 arg)
{ 
    sha_intr_test(); 
}
