#include <stdio.h>
#include <string.h>
#include <system.h>
#include <unistd.h>
#include "io.h"

#define SW_CONTROL 		 0x00
#define SW_STATUS  	     0x01
#define SW_OUTPUT        0x02
#define SW_DOUT_CTRL	 0x03


int main()
{
  int i;
  int h;
  const int offset = 98304; //Anzahl der Bytes pro Frame
  const int size = 786432; //Anzahl der Pixel


  printf("Program running! Wait! \n");

  char read_data[4];
  char ausgabe[4];
  int help;


	     for(i=0; i<size; i++){

			 //Erstellen einer Testmaske
	    	 read_data[0]=255;  //R
	    	 read_data[1]=10;  //G
	    	 read_data[2]=11;  //B
	    	 read_data[3]=12;    // don't care, wird verworfen beim Auslesen durch FrameReader
	    	                     // er liest 24bit und verwirft die letzten 8bit, da kein
	    	                     // SingleCycleColorPattern mehr komplett hineinpassen würde

	    	 //Ausgabe in Speicher als 4*1Byte
 			 IOWR_8DIRECT((DDR_SDRAM_BASE  ), ((i)*32/8), read_data[0]);
 			 IOWR_8DIRECT((DDR_SDRAM_BASE+1), ((i)*32/8), read_data[1]);
 			 IOWR_8DIRECT((DDR_SDRAM_BASE+2), ((i)*32/8), read_data[2]);
 			 IOWR_8DIRECT((DDR_SDRAM_BASE+3), ((i)*32/8), read_data[3]);
		  }

	     printf("Kontrolle: Lese erste 10*32bit aus DDR_SDRAM zurueck: \n");
	     printf("Soll: 255, 10, 11, 12  Beachte: 255=-1, wegen unsigned2signed \n");
	     for(h=0; h<10; h++){
	    ausgabe[0] = IORD_8DIRECT(DDR_SDRAM_BASE,(h*32/8));
	    printf("%i 1=>%i",h,ausgabe[0]);
	    ausgabe[1] = IORD_8DIRECT(DDR_SDRAM_BASE+1,(h*32/8));
	    printf(" 2=>%i",ausgabe[1]);
	    ausgabe[2] = IORD_8DIRECT(DDR_SDRAM_BASE+2,(h*32/8));
	    printf(" 3=>%i",ausgabe[2]);
	    ausgabe[3] = IORD_8DIRECT(DDR_SDRAM_BASE+3,(h*32/8));
	    printf(" 4=>%i \n",ausgabe[3]);
  }



	     	     IOWR(ALT_VIP_VFR_0_BASE, 3, 0); // Frame Select
	     	     IOWR(ALT_VIP_VFR_0_BASE, 4, DDR_SDRAM_BASE); // Frame 0 Base Address
	     	     IOWR(ALT_VIP_VFR_0_BASE, 5, 384000); // Frame 0 Words => 800*480
	     	     IOWR(ALT_VIP_VFR_0_BASE, 6, 384000); // Frame 0 Single Cycle Color Patterns
	     	     // => 800*480*1 (in 32bit passt nur jeweils genau 1 vollständiges SCCP)
	     	     IOWR(ALT_VIP_VFR_0_BASE, 7, 0); // Frame 0 Reserved Bit
	     	     IOWR(ALT_VIP_VFR_0_BASE, 8, 800); // Frame 0 Width
	     	     IOWR(ALT_VIP_VFR_0_BASE, 9, 480); // Frame 0 Height
	     	     IOWR(ALT_VIP_VFR_0_BASE, 10, 0); // Frame 0 Interlaced => 3 = 0011 bedeutet progressive output
	     	     IOWR(ALT_VIP_VFR_0_BASE, 0, 1); // start Frame Reader

	     	     //Video clocked output
	     	     IOWR(ALT_VIP_ITC_0_BASE, 3, 384000); //FIFO SIZE
	     	     IOWR(ALT_VIP_ITC_0_BASE, 5, 0); //Progressive output
	     	     IOWR(ALT_VIP_ITC_0_BASE, 26, 0); //MODE1 Invalid
	     	     IOWR(ALT_VIP_ITC_0_BASE, 6, 800); // width
	     	     IOWR(ALT_VIP_ITC_0_BASE, 7, 480); // height
	     	     IOWR(ALT_VIP_ITC_0_BASE, 9, 40); // Horizontal front porch
	     	     IOWR(ALT_VIP_ITC_0_BASE, 10, 5); // horizontal sync
	     	     IOWR(ALT_VIP_ITC_0_BASE, 11 ,261); // HORIZONTAL BLANK
	     	     IOWR(ALT_VIP_ITC_0_BASE, 12 , 10); // vertical front porch
	     	     IOWR(ALT_VIP_ITC_0_BASE, 13 , 5); // vertical sync
	     	     IOWR(ALT_VIP_ITC_0_BASE, 14 , 50); // vertical blank
	     	     IOWR(ALT_VIP_ITC_0_BASE, 18, 0); //Active picture line
	     	     IOWR(ALT_VIP_ITC_0_BASE, 26, 1); //MODE1 valid
	     	     IOWR(ALT_VIP_ITC_0_BASE, 1, 4); //FIFO Underflow
	     	     IOWR(ALT_VIP_ITC_0_BASE, 0, 1); //START VCO


  printf("Kontrolle: Lese FrameReader Register aus: \n");


  	    printf("soll 0     =>ist %u \n",(IORD(ALT_VIP_VFR_0_BASE,3)));

  	    printf("soll 0     =>ist %u \n",(IORD(ALT_VIP_VFR_0_BASE,4)));

  	   	printf("soll 384000=>ist %u \n",(IORD(ALT_VIP_VFR_0_BASE,5)));

  	   	printf("soll 384000=>ist %u \n",(IORD(ALT_VIP_VFR_0_BASE,6)));

  	    printf("soll 800  =>ist %u \n",(IORD(ALT_VIP_VFR_0_BASE,8)));

  	    printf("soll 480   =>ist %u \n",(IORD(ALT_VIP_VFR_0_BASE,9)));

  	   	printf("soll 3     =>ist %u \n",(IORD(ALT_VIP_VFR_0_BASE,10)));

  	   	printf("FR STATUS     =>ist %u \n\n",(IORD(ALT_VIP_VFR_0_BASE,1)));

  	   	printf("VCO STATUS     =>ist %u \n",(IORD(ALT_VIP_ITC_0_BASE,1)));
  	   	printf("VCO CONTROL     =>ist %u \n",(IORD(ALT_VIP_ITC_0_BASE,0)));

  	   	printf("VCO INTERRUPTS     =>ist %u \n",(IORD(ALT_VIP_ITC_0_BASE,2)));
  	   	printf("VCO MODE MATCH  =>ist  %u \n", (IORD(ALT_VIP_ITC_0_BASE, 4)) );


  printf("Program terminated! \n");
  while(1){}
  return 0;

}
