/*  TEK 4010 support

    03Dec2002 .. initial version started .. K.Metzger
    22Feb2004 .. updated for EECS 452 .. K.Metzger
    
    
   Tektronix 4010/4014 Graphics Display support code.

   These routines work with the TeraTerm Tektronix 4010/4014 emulator.
   
   The primitive operations derive from pen plotter routines.  penup moves
   the plotting "pen" to a given position in the up (non-printing) position.
   pendn does the same but with the pen in contact with the drawing surface.
   pendot moves pen up and then puts the pen down.  This is like doing a
   penup placing the pen in contact with the drawing surface at the end.

   The Tektronix coordinate system has origin in lower left corner of the
   screen.  The full x extent is 1024 points.  The full y extent is 780
   visible points.  These routines work in "raw" coordinates.

*/


#define ESC 0x1B
#define GS  0x1D
#define FF  0x0C
#define US  0x1F        
         
unsigned volatile flag = 0;
unsigned volatile value;

void TEK4010init() 
{
	unsigned long idx;
	
//	for(idx =0; idx<600000; idx++) if(flag == 1) value = flag;
    TX_Put(ESC);   // ESC
    TX_Put(FF);    // clear screen..go text mode  
    return;
}   

/*

Hi Y     Lo Y    Hi X    LSBXY   Characters sent (Lo-X always sent)
 ----     ----    ----    -----   ----------------------------------
 Same     Same    Same    Same                           Lo-X
 Same     Same    Diff    Same               Lo-Y, Hi-X, Lo-X
 Same     Diff    Same    Same               Lo-Y,       Lo-X
 Same     Diff    Diff    Same               Lo-Y, Hi-X, Lo-X
 Diff     Same    Same    Same    Hi-Y,                  Lo-X
 Diff     Same    Diff    Same    Hi-Y,      Lo-Y, Hi-X, Lo-X
 Diff     Diff    Same    Same    Hi-Y,      Lo-Y,       Lo-X
 Diff     Diff    Diff    Same    Hi-y,      Lo-Y, Hi-X, Lo-X

*/

int HiY, LoY, HiX;

void Draw(int x, int y) 
{      
	int hiy, loy, hix, lox;
	
	hiy = (y>>5)&0x1F;
	loy = y&0x1F;
	hix = (x>>5)&0x1F;
	lox = x&0x1F;
	
	if (hiy != HiY) {
		TX_Put(0x20+hiy);
		HiY = hiy;
	}
	if ((loy != LoY)||(hix != HiX)) {
		LoY = loy;
		TX_Put(0x60+loy);
		if (hix != HiX) {
			TX_Put(0x20+hix);
			HiX = hix;
		}
	}
    TX_Put(0x40+lox);       
    return;
}         
 
void GoTo(int x, int y) 
{
    TX_Put(GS);
    Draw(x, y);
    return;
}
 
void TextOut(char* str) 
{   
    TX_Put(US);    
    TX_Put(ESC); TX_Put(':');
    while(*str!= 0) TX_Put(*str++);
}

void DrawDot(unsigned int x, unsigned int y) 
{
  GoTo(x, y);                 // go to point to draw
  TX_Put(0x40+(x&0x1F));      // repeats point with pen down 
}
  
void GraphicsOff()
{
    TX_Put(ESC);
    TX_Put('[');
    TX_Put('?');
    TX_Put('3');
    TX_Put('8');
    TX_Put('l');
}
     
void DrawRectangle(unsigned int x, unsigned int y, unsigned int x1, unsigned int y1)
{
    GoTo(x,y);    
    Draw(x1,y);
    Draw(x1,y1);
    Draw(x,y1);
    Draw(x,y);
}

int xoffset = 0;
int yoffset = 50;
int sf = 8;

ScanOut(int *dptr, int N)
{      
	unsigned idx;
	int *ptr, y;
	              
	xoffset = 0;                       
	yoffset = 50;
	
	
	ptr = dptr; 
	GoTo(xoffset, yoffset);
	for (idx = 0; idx<N; idx++) {
		y = ((~*ptr++)^0x8000)>>sf;
		Draw(idx+idx+xoffset, y+yoffset);
	}
}


void XGridOut(int y)
{
	y = ((~y)^0x8000)>>sf;
	GoTo(xoffset, y+yoffset);
	Draw(1023,y+yoffset);
	return;
}
