/*
 * main.c
 *
 *  Created on: 26 Jan 2017
 *      Author: mr589
 */
#include "NoC_lib_V5.h"
#include "xil_printf.h"
#include "rtc.h"
#include "stdlib.h"


int main()
{

	/*******************************************************/
	/*** Start off by performing some set-up on the host ***/
	/*******************************************************/

	/* Enable the cache */
	microblaze_enable_dcache();
	microblaze_enable_icache();

	/* Reset the NoC, assign node ID's, and setup the RTC */
	xil_printf("V5 Many-Core Test \r\n");
	NoC_reset();
	NoC_Assign_Node_IDs();
	RTC_Set_Prescaler(100-1); // Set the pre-scaler to 99 (F=1MHz)

	/* Read and store the current TRC time */
	Xuint32 startTime = RTC_Read_Value();
	xil_printf("RTC: %d\r\n", startTime);


	/**********************************************/
	/***  Now we're setup lets test the system  ***/
	/**********************************************/

	/* Host will send the first message */
/*
	// Create the message:
	Xuint32 data;
	Xuint32 dataSent = 1;
	Xuint32 node = 4;

	NoC_Write_Sys_Packet((int)node, (Xuint8 *)(&dataSent), 4, 0);
*/

	NoC_Broadcast_Command(0xFF);

	Xuint32 data;
	Xuint32 dataSent = 1;
	Xuint32 node = 4;

	while(1){

		/* Get the data from the NoC */
		unpack_int(&node);
		unpack_int(&data);
		NoC_Sink_EOP();

		xil_printf("Rx Data = %x || From = %x \r\n", data, node);

		/* Check the returned data */
		/*if (data != dataSent) {
			xil_printf("Packet returned is different, msg = %x \r\n", dataSent);
		} else {*/
			/* Increment and send it back */
			dataSent++;
			NoC_Write_Sys_Packet(node, (Xuint8 *)(&dataSent), 4, 0);
		//}

	}
}
