#ifndef lint
static char *sccsid = "@(#)bbcplot.c	1.8 (UKC) 28/4/87";
#endif  lint
#include <stdio.h>
#include "bbcplot.h"

/* 
 * Library of primitive graphics functions for BBC micro with
 * UKC terminal emulator ROM.
 * Translated from dcw's original pascal
 */

/*
 *	bbcodd reflects whether this is the first byte of a pair to be
 *	massaged into three characters for transmission.
 */
static int bbcodd = 1;

/* If the 2 bytes of data are 'a' and 'b' then the bits will be sent as
 * follows:
 *
 *    a7 a6 a5 a4 a3 a2
 *       a1 a0 b7 b6 b5
 *       b4 b3 b2 b1 b0
 *    
 * These are added onto a base of 0x30 - in case any problem is experienced
 * with sending spaces.
 *
 * gewt - (UKC)
 */

/*
 *	bbcbyte: transmit a byte to be printed to the bbc;
 *	packs 2 bytes into 3 for transmission
 *	As this function takes about a third of dbbc's time, it has been
 *	optimised for the VAX-11 C compiler, hence some strange constructs.
 */

bbcbyte(c)
register char c;
{
	static char bbcbuff;

	if (bbcodd) {
		bbcbuff = c;
		bbcodd = 0;
		return;
	}
	(void) putchar( (bbcbuff + (48<<2)) >> 2 );
	(void) putchar( ( ( (bbcbuff&3)<<3 ) | (c>>5) ) + 48 );
	(void) putchar( (c&31) + 48 );
	bbcodd=1;
}

bbcword(x)
register short x;
{
	bbcbyte((char) (x & 0377));
	bbcbyte((char) ( (x & (0377<<8)) >> 8 ));
}

bbcopen()
{
	(void) putchar(1);			/* encoded mode on */
}

bbcclose()
{
	bbcbyte(27); /* code 27 reverts to normal mode */
	bbcflush();
}

/*
 *	Flush all pending commands to the terminal
 */
bbcflush()
{
	if (!bbcodd) bbcbyte(0);
	(void) fflush(stdout);
}

bbcplot(k,x,y)
char k;
short x,y;
{
	bbcbyte(25);
	bbcbyte(k);
	bbcword(x);
	bbcword(y);
	/* delay for triangle fill goes here */
}

bbcmode(m)
char m;
{
	bbcbyte(22);
	bbcbyte(m);
}

#if 0
bbcdchar(ch,g)
char ch;
grid g;
{
	register int i;

	bbcbyte(23);
	bbcbyte(ch);
	for (i=0;i<8;i++)
		bbcbyte(g[i]);
}
#endif

bbcgcol(style, colour)
int style, colour;
{
	bbcbyte(18);
	bbcbyte((char) style);
	bbcbyte((char) colour);
}
