/*	TERM3.C		Subroutines for TERM.C, Part 3
			Functions to select comm.parameter alternatives
	14 Feb 1988	A.Herrmann
	09 Mar 1988	added binary file transfer calls tx, rx
*/

#include "stdio.h"
#include "term.h"
#include "term.e"

/* SIO register masks	*/
#define WR3_Rx	0x3F
#define WR4_x	0x3F
#define WR4_ST	0xF3
#define WR4_PA	0xFE
#define WR5_Tx	0x9F

/* SIO register bits	*/
#define WR3Rx5	0x00
#define WR3Rx6	0x80
#define WR3Rx7	0x40
#define WR3Rx8	0xC0

#define WR4x1	0x00
#define WR4x16	0x40
#define WR4x32	0x80
#define WR4x64	0xC0

#define WR4ST1	0x04
#define WR4ST2	0x0C
#define WR4ST3	0x08

#define WR4NOP	0x00
#define WR4ODD	0x01
#define WR4EVN	0x03

#define WR5Tx5	0x00
#define WR5Tx6	0x40
#define WR5Tx7	0x20
#define WR5Tx8	0x60

#define	WR5RTS	0x02
#define	WR5DTR	0x80

static int bits, bits2;

void baud(i)	/* selects a new baud rate	*/
int i;
{
	switch (i) {
	case 0:					/* 300 bd */
		bits = WR4x64;
		pio_b0 = 1;
		break;
	case 1:					/* 1200 bd */
		bits = WR4x16;
		pio_b0 = 1;
		break;
	case 2:					/* 2400 bd */
		bits = WR4x64;
		pio_b0 = 0;
		break;
	case 3:					/* 9600 bd */
		bits = WR4x16;
		pio_b0 = 0;
		break;
#ifdef UNUSED			/* Some leftovers, currently not used	*/
	case 4:					/*19200 bd */
		bits = WR4x1;
		pio_b0 = 1;
		break;
	case 5:					/* 600 bd */
		bits = WR4x32;
		pio_b0 = 1;
		break;
	case 6:					/* 4800 bd */
		bits = WR4x32;
		pio_b0 = 0;
		break;
#endif
	}
	sio_reg[4] &= WR4_x;
	sio_reg[4] |= bits;
	i_rs232();
}


void bpch(i)	/* selects new bits per character	*/
int i;
{
	switch(i) {
	case 0:					/* 5 bits */
		bits = WR3Rx5;
		bits2= WR5Tx5;
		mask = C5MASK;
		break;
	case 1:					/* 6 bits */
		bits = WR3Rx6;
		bits2= WR5Tx6;
		mask = C6MASK;
		break;
	case 2:					/* 7 bits */
		bits = WR3Rx7;
		bits2= WR5Tx7;
		mask = C7MASK;
		break;
	case 3:					/* 8 bits */
		bits = WR3Rx8;
		bits2= WR5Tx8;
		mask = C8MASK;
		break;
	}
	sio_reg[3] &= WR3_Rx;
	sio_reg[3] |= bits;
	sio_reg[5] &= WR5_Tx;
	sio_reg[5] |= bits2;
	i_rs232();
}


void stop(i)	/* selects new stop bits	*/
int i;
{
	switch(i) {
	case 0:					/* 1 Stopbit */
		bits = WR4ST1;
		break;
	case 1:					/* 2 Stopbits */
		bits = WR4ST2;
		break;
	}
	sio_reg[4] &= WR4_ST;
	sio_reg[4] |= bits;
	i_rs232();
}


void parity(i)	/* select new parity	*/
int i;
{
	switch (i) {
	case 0:					/* no parity	*/
		bits = WR4NOP;
		break;
	case 1:					/* even parity	*/
		bits = WR4EVN;
		break;
	case 2:					/* odd parity	*/
		bits = WR4ODD;
		break;
	}
	sio_reg[4] &= WR4_PA;
	sio_reg[4] |= bits;
	i_rs232();
}


void proto(i)	/* select a new protocol	*/
int i;
{
	switch (i) {
	case 0:					/* no protocol	*/
		mode = NOP;
		break;
	case 1:
		mode = RTS;
		break;
	case 2:
		mode = DTR;
		break;
	case 3:
		mode = XONOFF;
		break;
	}
	sio_reg[5] |= WR5RTS;		/* in any case, set RTS */
	sio_reg[5] |= WR5DTR;		/* and DTR pin 		*/
	i_rs232();
}


void xonsw(i)	/* toggles Xon/Xoff enable/disable	*/
int i;
{
	xonkey = i;
}


void lfsw(i)	/* toggles passing of LF after CR on/off	*/
int i;
{
	passlf = i;
}


void echosw(i)	/* toggles local echo mode on/off	*/
int i;
{
	echo = i;
}


void ctlcsw(i)	/* toggles between Interpret and Display Control chars	*/
int i;
{
	dspctl = i;
}


void emulsw(i)	/* toggles VT100 emulation on/off	*/
int i;
{
	vtemul = vtinit = i;
}


void csetsw(i)	/* toggles character set (0=ASCII, 1=German)	*/
int i;
{
	cset = i;
}

void prntsw(i)	/* toggles printer output via Centronics port	*/
int i;
{
	prtlog = i;
}


void fform(i)	/* selects file format (ASCII, binary)	*/
int i;
{
	switch(i) {
	case 0:					/* ASCII file	*/
		format = ASCII;
		break;
	case 1:					/* Binary file	*/
		format = BINARY;
		break;
	}
}


void infil(name)	/* defines a new input file	*/
char *name;
{
	if (format==ASCII)
		newfile(&input,name,IFITEM, "rb");
	else
		tx(name,IFITEM);
}


void outfil(name)	/* defines a new output file	*/
char *name;
{
	if (format==ASCII)
		newfile(&output,name,OFITEM, "wb");
	else
		rx(name,OFITEM);
}


void newfile(fp,name,item,mode)	/* defines a new input or output file	*/
FILE **fp;			/* File pointer	*/
char *name;			/* File name	*/
int item;			/* Corresp. status line item to be updated */
char *mode;			/* Mode in which file is to be opened	   */
{
	if (!*name) return;			/* skip if file name empty */
	if (*fp) iofclose(fp,item);		/* close old file before...*/
	if (isalnum(*name) && (*fp = iofopen(name,mode)))
		cpitem(name,item);		/*...opening the new one   */
	else	cpitem(par[item].alt[0],item);
	ptitem(item);
}


void closefile(fp,item)	/* closes IO file fp and updates item in status line */
FILE **fp;
int item;
{
	iofclose(fp,item);
	cpitem(par[item].alt[0],item);
	selwdw(STAWDW);
	selwdw(TERWDW);
}

