/* NAME:  HOSTINTERFACE */

/* This is some sample code fragments that can be used to right directly
   to the HOST interface of you Pepper graphics board.  You can use this
   procedure to transfre data to the graphics board very quickly.  You must
   be very carefull of where you transfer the data to.  If you transfer to
   the wrong locations you could lock up your system or the distroy NNios
   operating enviroment.  Refer to the section of your Programmers
   supplement for your Pepper board on HOST Interface before using this
   code.  You should also refer to the TI 34010 user guide for information
   on the Host Interface and the Host Control Register before using this
   code.
*/

#include "nnios.h"                      /* Required for all NNIOS programs */
#include "nnioslib.h"                   /* Language binding header file    */
#include "time.h"

DEV      device;                        /* Info about the current device   */
DEVCON   dev;                           /* Current device configuration    */
BITMAP   bitmap;                        /* Data for the app's one bitmap   */
WINDOW   window;                        /* And for the app's one window    */
WORD     devCount;                      /* Number of NNIOS devices present */
POINT    line[10];                      /* polyline structure              */

main (argc, argv)
int argc;
char *argv[];
{
   DWORD   host;
   UDWORD   i,j;
   WORD    startaddressHigh,startaddressLow;
   WORD    screenpitch,screenheight,count;
   WORD    tdata;
   time_t  starttime,stoptime;

   if (CheckNNIOS ())                   /* Is NNIOS present  */
      {
      printf ("This application requires the NNIOS device driver.\n\r");
      exit (-1);
      }
      
   if ((devCount = DeviceCount ()) == 0)
      {
      printf ("No Pepper Graphics System products are installed.\n\r");
      exit (-1);
      }
   DeviceInfo (&device, devCount);


   OpenCommChannel (&device, "NNIOS Program");
   if(argc >1) {
      SetDeviceConfig(atoi(argv[1]));
      GetDeviceConfig (&dev);

      BitCreate (&bitmap, atoi(argv[2]),atoi(argv[3]), dev.maxDepth,
                 TRUE, 0, "Template");
      BMClear(bitmap.handle,(CINDEX)0);        

      WinCreate (&window, bitmap.handle, 0, 0, atoi(argv[2]),
                 atoi(argv[3]), 0, 0, dev.maxDepth, 0, 0, 0);
      printf("bitmap x = %d  y = %d \n window x = %d  y = %d\n",atoi(argv[2]),
         atoi(argv[3]),atoi(argv[2]),atoi(argv[3]));
      
      testpattern();

      flip_screen(bitmap.size.extents.x,bitmap.size.extents.y,bitmap.depth,TRUE);

      /* switch bitmap orgin */


      
      getch();
      WinDiscard (window.windowHandle);
      BitDiscard (bitmap.handle);
      CloseCommChannel ();
      exit(0);
      }
}

flip_screen(bitmap_x,bitmap_y,bitmap_depth,dir)
int bitmap_x;
int bitmap_y;
int bitmap_depth;
BOOL dir;
{
   UDWORD   address,address1,address2;
   UDWORD   flag,flag1;
   long    address3;


      flag = 0x00000000;
      address = 0x00000000;
      address1 = 0x00000000;
      address2 = 0x00000000;
      GetData((DWORD)0xc0000080,(BYTE FAR *)&flag,(WORD)1);
      printf("flag = %8lx\n",flag);
      flag1 = flag|0x0400;
      printf("flag1 = %8lx\n",flag1);
      printf("Press any key to switch\n");
      getch();
      SendData((BYTE FAR *)&flag1,(DWORD)0xc0000080,(WORD)1);
      GetData((DWORD)0xc0000090,(BYTE FAR *)&address1,(WORD)1);
      address3 = (bitmap_x*(bitmap_y -1L)*bitmap_depth)/256;
      address2 = (address1 ^0x0000fffc)+address3;
                        /*      ^  */
                        /*      |  */
                        /*      |  */
                        /* This is a magic number */
      SendData((BYTE FAR *)&address2,(DWORD)0xc0000090,(WORD)1);
      printf("/* now switch back */\n");
      getch();
      SendData((BYTE FAR *)&flag,(DWORD)0xc0000080,(WORD)1);
      SendData((BYTE FAR *)&address1,(DWORD)0xc0000090,(WORD)1);


      do {
         SendData((BYTE FAR *)&flag1,(DWORD)0xc0000080,(WORD)1);
         SendData((BYTE FAR *)&address2,(DWORD)0xc0000090,(WORD)1);
         stall((float)1);  
         BMClear(bitmap.handle,(CINDEX)33);        
         testpattern();
         stall((float)1);  
         SendData((BYTE FAR *)&flag,(DWORD)0xc0000080,(WORD)1);
         SendData((BYTE FAR *)&address1,(DWORD)0xc0000090,(WORD)1);
         stall((float)1);  
         BMClear(bitmap.handle,(CINDEX)0);        
         testpattern();
         stall((float)1);  
         }while (!kbhit());

}










stall(amount)
float   amount;
{
   time_t         starttime,stoptime;
   

   starttime = time(NULL);
   do {
      stoptime = time(NULL);
      } while(difftime(stoptime,starttime) < amount);
}

testpattern()
{
   register int i, j, k, lutColor, flag;

/* Drawing line pattern */
      SetColor (bitmap.handle, (CINDEX) 127,(CINDEX) 0);			 
/* 3 horizontal lines each 4 lines wide */

   	for (i = 0; i < 4; i++)
         {
         line[0].x = 0;
         line[0].y = i;
         line[1].x = (dev.displaySize.x)-1;
         line[1].y = i;
         PolyLine (bitmap.handle, 2,line);
         }
   	for (i = 0; i < 4; i++)
         {
         line[0].x = 0;
         line[0].y = ((dev.displaySize.y /2)-2) + i;
         line[1].x = (dev.displaySize.x)-1;
         line[1].y = ((dev.displaySize.y /2)-2) + i;
         PolyLine (bitmap.handle, 2,line);
         }

   	for (i = 0; i < 4; i++)
         {
         line[0].x = 0;
         line[0].y = (dev.displaySize.y - 5) + i;
         line[1].x =  dev.displaySize.x - 1;
         line[1].y = (dev.displaySize.y -5) + i;
         PolyLine (bitmap.handle, 2,line);
         }
/* 5 vertical lines each 4 lines wide */

   	for (i = 0; i < 4; i++)
         {
         line[0].x = i;
         line[0].y = 0;
         line[1].x = i;
         line[1].y = (dev.displaySize.y) - 1;
         PolyLine (bitmap.handle, 2,line);
         }

   	for (i = 0; i < 4; i++)
         {
         line[0].x = ((dev.displaySize.x /4)-2) + i;
         line[0].y = 0;
         line[1].x = ((dev.displaySize.x /4)-2) + i;
         line[1].y = dev.displaySize.y -1;
         PolyLine (bitmap.handle, 2,line);
         }

   	for (i = 0; i < 4; i++)
         {
         line[0].x = ((dev.displaySize.x /2)-2) + i;
         line[0].y = 0;
         line[1].x = ((dev.displaySize.x /2)-2) + i;
         line[1].y = dev.displaySize.y -1;
         PolyLine (bitmap.handle, 2,line);
         }

   	for (i = 0; i < 4; i++)
         {
         line[0].x = (((dev.displaySize.x /2) +
                       (dev.displaySize.x /4))-2) + i;
         line[0].y = 0;
         line[1].x = (((dev.displaySize.x /2) +
                       (dev.displaySize.x /4))-2) + i;                                                    
         line[1].y = dev.displaySize.y - 1;
         PolyLine (bitmap.handle, 2,line);
         }

   	for (i = 0; i < 4; i++)
         {
         line[0].x = (dev.displaySize.x - 4) + i;
         line[0].y = 0;
         line[1].x = (dev.displaySize.x -4) + i;
         line[1].y = dev.displaySize.y -1;
         PolyLine (bitmap.handle, 2,line);
         }
/* Drawing 2 short lines */
      for (i = 0; i < 4; i++)
         {
         line[0].x = dev.displaySize.x/4;
         line[0].y = ((dev.displaySize.y /4)-2) + i;
         line[1].x = ((dev.displaySize.x /2) +
                       ((dev.displaySize.x /4)-2));
         line[1].y = ((dev.displaySize.y /4)-2) + i;
         PolyLine (bitmap.handle, 2,line);
         }
      for (i = 0; i < 4; i++)
         {
         line[0].x = ((dev.displaySize.x /4)-2);
         line[0].y = (((dev.displaySize.y /2) +
                       (dev.displaySize.y /4)-2)) + i;
         line[1].x = ((dev.displaySize.x /2) +
                       ((dev.displaySize.x /4)-2));
         line[1].y = (((dev.displaySize.y /2) +
                       (dev.displaySize.y /4)-2)) + i;
         PolyLine (bitmap.handle, 2,line);
			}

/* Drawing przekatne on the screen */
/* 1 */
         line[0].x = 0;
         line[0].y = 0;
         line[1].x = dev.displaySize.x - 1;
         line[1].y = dev.displaySize.y - 1;
         line[2].x = 1;
         line[2].y = 0;
         line[3].x = dev.displaySize.x - 1;
         line[3].y = dev.displaySize.y - 1;
         line[4].x = 0;
         line[4].y = 1;
         line[5].x = dev.displaySize.x - 2;
         line[5].y = dev.displaySize.y - 1;
         PolyLine (bitmap.handle, 6,line);

/* 2 */
         line[0].x = dev.displaySize.x - 1;
         line[0].y = 0;
         line[1].x = 0;
         line[1].y = dev.displaySize.y - 1;
         line[2].x = dev.displaySize.x - 2;
         line[2].y = 0;
         line[3].x = 0;
         line[3].y = dev.displaySize.y - 2;
         line[4].x = dev.displaySize.x - 1;
         line[4].y = 1;
         line[5].x = 1;
         line[5].y = dev.displaySize.y - 1;
         PolyLine (bitmap.handle, 6,line);

/* Drawing elipsse */

         DrawEllipse (bitmap.handle, dev.displaySize.x/4, dev.displaySize.y/4,
                       dev.displaySize.x/2, (dev.displaySize.y/2) - 3);                                                      
         DrawEllipse (bitmap.handle, dev.displaySize.x/4, dev.displaySize.y/4, 
                       dev.displaySize.x/2, (dev.displaySize.y/2) - 2);
         DrawEllipse (bitmap.handle, dev.displaySize.x/4, dev.displaySize.y/4,
                       dev.displaySize.x/2, (dev.displaySize.y/2) - 1);


/* draw a mesure bar */
	DrawLine(bitmap.handle,0,dev.displaySize.y/4,dev.displaySize.x,dev.displaySize.y/4);
	for (i=0;i<dev.displaySize.x;i+=10) {
	  DrawLine(bitmap.handle,i,dev.displaySize.y/4,i,(dev.displaySize.y/4)+25);
	  if(i%100 == 0)
	     DrawLine(bitmap.handle,i,dev.displaySize.y/4,i,(dev.displaySize.y/4)+75);
	  }


}

