
/********************************************************/
/*							*/
/*	Utility Routines for MX-80 Bit-Plot Graphics	*/
/*							*/
/*	Don Brittain		3 January 1983		*/
/*							*/
/********************************************************/

#define HSIZE 320
#define VSIZE 328
#define NO 0
#define YES 1
#define LIST 5
#define ESC 27

short grafbuf[HSIZE][VSIZE/8];		/* MUST be EXTERNAL */
extern int horsize, versize, flag;
extern int pixplot(), unpixplot();


init(h,v)	/* Clears or fills the user area of the graph matrix */
		/* depending on the status of the external var "flag" */
int h,v;

{
    register i, j;
    int fill=0;

    if(flag==NO) fill=255;
    v=(v+7)/8;
    for(j=1; j<v; j++)
	for(i=0; i<h; i++)
	    grafbuf[i][j]=fill;
    if(flag)
	for(i=0; i<h; i++)
	    grafbuf[i][0]=0;
    else			/* Cluge resulting because the graph is */
	{			/* defined bit-wise, but initializing */
	switch(versize%8) {	/* is much quicker byte-wise */
	    case 1 :
		fill=1;
		break;
	    case 2 :
		fill=3;
		break;
	    case 3 :
		fill=7;
		break;
	    case 4 :
	        fill=15;
	        break;
	    case 5 :
		fill=31;
		break;
	    case 6 :
		fill=63;
		break;
	    case 7 :
		fill=127;
		break;
	    case 0 :
		fill=255;	}
	for(i=0; i<h; i++)
	    grafbuf[i][0]=fill;
	}
    flag=YES;		/* functions that use flag always reset it */
}


grafprnt(n)	/* routine to send the graph RAM image to the MX-80 */

int n;

{
    register i, j;
    int v;
    
    setline(8);
    clmar(n);
    v=(versize+7)/8;
    for(j=0; j<v; j++)	{
	bitmode(horsize);
	for(i=0; i<horsize; i++)
	    bdos(LIST, grafbuf[i][j]);
	clmar(n);	}
    setline(12);
}

border()	/* puts a border around the graph */
{
    register i;
    int rside, top;
    
    rside=horsize-1;
    top=versize-1;
    
    if(flag)
	{
	for(i=0; i<horsize; i++)	{
	    pixplot(i,0);
	    pixplot(i,top);		}
	for(i=0; i<versize; i++)	{
	    pixplot(0,i);
	    pixplot(rside,i);		}
	}   
 else
	{
	for(i=0; i<horsize; i++)	{
	    unpixplot(i,0);
	    unpixplot(i,top);		}
	for(i=0; i<versize; i++)	{
	    unpixplot(0,i);
	    unpixplot(rside,i);		}
	}
    flag=YES;
}


setline(n)	/* routine to set the line-spacing on the MX-80 */
int n;
{
    bdos(LIST,ESC);
    bdos(LIST,'A');
    bdos(LIST,n);
}

bitmode(n)	/* sets n chars of dot graphics (480 dots/line) on MX-80 */
int n;
{
    bdos(LIST,ESC);
    bdos(LIST,'K');
    if(n<256)
	{
	bdos(LIST,n);
	bdos(LIST,0);
	}
    else
	{
	bdos(LIST,n-256);
	bdos(LIST,1);
	}
}


crlf(n)		/* printer carriage-return-linefeed */
int n;
{
int i;
for(i=0; i<n; i++)
    {
    bdos(LIST,10);
    bdos(LIST,13);
    }
}


margin(n)	/* printer left-margin */
int n;
{
int i;
for(i=0; i<n; i++)
    bdos(LIST,' ');
}


clmar(n)	/* single CRLF then n-space margin to printer */
int n;
{
    crlf(1);
    margin(n);
}
    

