/*
 * Copyright (c) 2009 Xilinx, Inc.  All rights reserved.
 *
 * Xilinx, Inc.
 * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
 * COURTESY TO YOU.  BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
 * ONE POSSIBLE   IMPLEMENTATION OF THIS FEATURE, APPLICATION OR
 * STANDARD, XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION
 * IS FREE FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE
 * FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
 * XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
 * THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO
 * ANY WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE
 * FROM CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

/*
 * helloworld.c: simple test application
 */

#include <stdio.h>
#include "platform.h"
#include "xparameters.h"
#include "xparameters.h"
#include "xstatus.h"
#include "../../hello_world_bsp/microblaze_0/include/fsl.h"
//#include "xtmrctr.h"


void print(char *str);

int main()
{
    init_platform();

    print("Hello World\n\r");
    print("In main\n\r");


    int imag, real, send;
    int recv;
    int indata[256];
    int outdata[16];

    print("init indata");

    int i;
    for (i=0; i<256; ++i) {
    	indata[i] = i;
    }

    print(", done\r\n");


    while (1) {
		//print("begining of loop");
    	int i;
		for (i=0; i<16; ++i) {

			int temp = outdata[(16-i)%16] >> 24;
			send = outdata[(16-i)%16] << 8;
			send += temp;


			putfslx(send, 0, FSL_DEFAULT);
			//xil_printf("sent: %d\n\r", send);
		}


		for (i=0; i<16; ++i) {
//			outdata[i] = i*0x11111111;		// shows colors
			outdata[i] = 0;
		}

//		outdata[1] = 0x01234567;
//		outdata[2] = 0x89abcdef;
//		outdata[3] = 0x99aabbcc;
//		outdata[4] = 0xddeeff00;

//		outdata[5] = 0x76543210;
//
//		outdata[6] = 0xefcdab89;


//		print("step1 of loop");

		for (i=0; i<256; ++i) {
			send = indata[i] << 24;
			putfslx(send, 1, FSL_DEFAULT);
//			xil_printf("sent(%d): %d\n\r", i, send);
		}
//		print("step2 of loop");

		for(i=0; i<128; ++i) {
			getfslx(recv, 1, FSL_DEFAULT);
			real = recv >> 16;
			imag = (recv << 16) >> 16;

			if (i%8 == 0) { outdata[i/8] = 0; indata[i/8] = 0; }

			int temp = (real*real+imag*imag);
			int t2 = temp >> 14;
			t2 = t2>15? 15: t2;
			outdata[i/8] += (t2 << ((7-(i%8))*4));
			indata[i/8] += (t2 << ((7-(i%8))*4));
//			xil_printf("got(%d): %d + %d i; became: abs: %d, shift: %d\n\r", i, real, imag, temp, t2);
//			if (i%8 == 7) {
//				xil_printf("sum(%d): %x\r\n", i, outdata[i/8]);
//			}
		}
		//print("end of loop");
    }
    return 0;


}
