/*---------------------------------------------------------------------*/
/*                                                                     */
/* Copyright (C) 1992/93 Cyclades Corporation.                         */
/*                                                                     */
/* TY.EXE	V_1.0.0 11/19/92 First release			       */
/* 		V_2.0.0 05/21/93 Complete revision		       */
/*		V_2.0.1 07/07/93 Alterations in lib_y		       */
/*                                                                     */
/* This program was compiled and tested in Microsoft C V 5.0 and is    */ 
/* provided by Cyclades as an example.				       */ 
/*---------------------------------------------------------------------*/

/* include files */

#include <stdio.h>
#include <string.h>
#include <graph.h>

#include "..\library\cyclom.h"

#define VERSION	"V_2.0.1"

/* global variables */

ushort			irq;
uchar			*address;
ushort			nchan,ntest;
ushort			option;
ushort			channel,chn;
uchar			keystroke;
struct ChannelParam	ch_par;
ushort			cont;


/*--------------------------------------------------------------------------*/
/* main routine - display a menu, call tests			            */
/*--------------------------------------------------------------------------*/

main(argc,argv)
int argc;
char *argv[];
{

	/* sets Cyclom address and irq */

	if (argc != 3) {
		printf("TY : Error - Bad syntax.\n");
		printf("Sintax  :   ty <address> <irq>.\n");
		printf("Example :   ty D4 10.\n");
		printf("            ty E0 09.\n\n");
		exit(1);
	}
	address = NULL;
	
	if ( strcmp(argv[1],"A0") == 0 )	address = A0;
	if ( strcmp(argv[1],"A2") == 0 )	address = A2;
	if ( strcmp(argv[1],"A4") == 0 )	address = A4;
	if ( strcmp(argv[1],"A6") == 0 )	address = A6;
	if ( strcmp(argv[1],"A8") == 0 )	address = A8;
	if ( strcmp(argv[1],"AA") == 0 )	address = AA;
	if ( strcmp(argv[1],"AC") == 0 )	address = AC;
	if ( strcmp(argv[1],"AE") == 0 )	address = AE;

	if ( strcmp(argv[1],"B0") == 0 )	address = B0;
	if ( strcmp(argv[1],"B2") == 0 )	address = B2;
	if ( strcmp(argv[1],"B4") == 0 )	address = B4;
	if ( strcmp(argv[1],"B6") == 0 )	address = B6;
	if ( strcmp(argv[1],"B8") == 0 )	address = B8;
	if ( strcmp(argv[1],"BA") == 0 )	address = BA;
	if ( strcmp(argv[1],"BC") == 0 )	address = BC;
	if ( strcmp(argv[1],"BE") == 0 )	address = BE;

	if ( strcmp(argv[1],"C0") == 0 )	address = C0;
	if ( strcmp(argv[1],"C2") == 0 )	address = C2;
	if ( strcmp(argv[1],"C4") == 0 )	address = C4;
	if ( strcmp(argv[1],"C6") == 0 )	address = C6;
	if ( strcmp(argv[1],"C8") == 0 )	address = C8;
	if ( strcmp(argv[1],"CA") == 0 )	address = CA;
	if ( strcmp(argv[1],"CC") == 0 )	address = CC;
	if ( strcmp(argv[1],"CE") == 0 )	address = CE;
	
	if ( strcmp(argv[1],"D0") == 0 )	address = D0;
	if ( strcmp(argv[1],"D2") == 0 )	address = D2;
	if ( strcmp(argv[1],"D4") == 0 )	address = D4;
	if ( strcmp(argv[1],"D6") == 0 )	address = D6;
	if ( strcmp(argv[1],"D8") == 0 )	address = D8;
	if ( strcmp(argv[1],"DA") == 0 )	address = DA;
	if ( strcmp(argv[1],"DC") == 0 )	address = DC;
	if ( strcmp(argv[1],"DE") == 0 )	address = DE;

	if ( strcmp(argv[1],"E0") == 0 )	address = E0;
	if ( strcmp(argv[1],"E2") == 0 )	address = E2;
	if ( strcmp(argv[1],"E4") == 0 )	address = E4;
	if ( strcmp(argv[1],"E6") == 0 )	address = E6;
	if ( strcmp(argv[1],"E8") == 0 )	address = E8;
	if ( strcmp(argv[1],"EA") == 0 )	address = EA;
	if ( strcmp(argv[1],"EC") == 0 )	address = EC;
	if ( strcmp(argv[1],"EE") == 0 )	address = EE;

	if (address == NULL) {
		printf("TY : Error - Bad syntax.\n");
		printf("Sintax   :  ty <address> <irq>.\n");
		printf("Examples :  ty D4 10.\n");
		printf("            ty E0 09.\n\n");
		exit(1);
	}

	irq = 0;

	if ( strcmp(argv[2],"09") == 0 )	irq = IRQ9;
	if ( strcmp(argv[2],"10") == 0 )	irq = IRQ10;
	if ( strcmp(argv[2],"11") == 0 )	irq = IRQ11;
	if ( strcmp(argv[2],"12") == 0 )	irq = IRQ12;
	if ( strcmp(argv[2],"15") == 0 )	irq = IRQ15;

	if (irq == 0) {
		printf("TY : Error - Bad syntax.\n");
		printf("Sintax   :  ty <address> <irq>.\n");
		printf("Examples :  ty D4 10.\n");
		printf("            ty E0 09.\n\n");
		exit(1);
	}
	
	cls();
	printf ("Looking for Cyclom-Y at 0x%x segment...",
		(ushort)( (ushort)((ulong)address>>16) + (ushort)((ulong)address>>4) ) );

	nchan = (ushort) Cyc_init(address,irq);

	switch(nchan) {
		case 0:
		case -1:
			printf("\n\nTY : Error - board not found.\n");
			printf("-> Board not present or\n");
			printf("-> Wrong dip-switch settings or\n");
			printf("-> Problem in bus interface: GAL, 244, 521 or\n");
			printf("-> Problem on the first CD1400.\n");
			exit(1);
			break;
		case 4: printf("\n\nChannells found : 4.\n\n");
			printf("-> if you are testing:\n");
			printf("	- Cyclom-4Y,ok.\n");
			printf("	- Cyclom-8Y,16Y, problem on the second CD1400.\n");
			break;
		case 8: printf("\n\nChannells found : 8.\n\n");
			printf("-> if you are testing:\n");
			printf("	- Cyclom-4Y, bus interface problem (GAL,244, 521).\n");
			printf("	- Cyclom-8Y, ok.\n");
			printf("	- Cyclom-16Y, problem on the third CD1400.\n");
			break;
		case 12:printf("\n\nChannells found : 12.\n\n");
			printf("-> if you are testing:\n");
			printf("	- Cyclom-4Y,8Y, bus interface problem (GAL,244, 521).\n");
			printf("	- Cyclom-16Y, problem on the fourth CD1400.\n");
			break;
		case 16:printf("\n\nChannells found : 16.\n\n");
			printf("-> if you are testing:\n");
			printf("	- Cyclom-4Y,8Y, bus interface problem (GAL,244, 521).\n");
			printf("	- Cyclom-16Y, ok.\n");
			break;
	}			

	printf("\n%d-channel card found and initialized.\n\n",nchan);
	printf("Press <Enter> to continue... ");
	getch();
	
	/* open all channels */ 

	cls();

	printf("Opening channel");
	for ( channel = 1 ; channel <= nchan ; channel++ ) { 
		ch_par.channel = channel;
		ch_par.char_len = CHAR_8;
		ch_par.parity_check = PAR_CHECK_OFF;
		ch_par.parity = EVEN_PARITY;
		ch_par.stopbits = ONE_STOP_BIT;
		ch_par.baudrate = BAUD_9600;
		if ( Cyc_open(&ch_par) == -1 ) {
			printf("\nError : cannot open channel %d.\n",channel);
			fatal(1);
		}
			printf(" %d,",channel);
	}
	printf("\n\n");
	printf("%d channels openned (9600,8,1,N)...\n\n",nchan);
	printf("Press <Enter> to continue. ");
	getch();

			
	while(1) {
		cls();
		printf ("Test menu:\n\n");
		printf ("	1   ) Communication Channel Test - 4 ports\n");
		printf ("	2   ) Communication Channel Test - 8 ports\n");
		printf ("	3   ) Communication Channel Test - 16 ports\n");
		printf ("	Esc ) Exit to DOS\n\n\n");
		printf ("Option: ");
		option = getch();

		cls();
				
		switch (option) {
			case '1':	ntest = 4;  break;
			case '2':	ntest = 8;  break;
			case '3':	ntest = 16; break;
			case 0x1b:	fatal(0); break;
			default:	continue; break;
		}
		if (ntest > nchan)	ntest = nchan;

		printf("Sending/Receiving characters to/from %d channels.\n",
			ntest);
		printf("'i>' message means that you have interrupts.\n\n");
		printf("-> if you don't get 'i>' (the machine hangs):\n");
		printf("	Wrong interrupt dip-switch setting or\n");
		printf("	Problem in interrupt generation circuit.\n\n");
		printf("-> if you don't get characters at the ports:\n");
		printf("	Problem in the Line-Drivers.\n\n");
		printf("-> if you don't get report of received characters:\n");
		printf("	Problem in the Line-Receivers.\n\n");
		
		printf("Press any key to cancel.\n\n");

		cont = 0;
		while (1) {
			if(kbhit()) {
				getch();
				break;
			}
			chn = 0;
			if ( Cyc_read_ch(&chn,&keystroke) != -1 ) {
				printf ("\n%c (0x%x) received from channel %d\n",
					keystroke,keystroke,chn);
			}
			for ( channel = 1 ; channel <= ntest ; channel++ ) {
				Cyc_write_ch(channel,(uchar)(0x30+channel));
			}
			if ( ((cont++)%2000) == 200 ) printf("i>"); 
		}
	}
}


cls()
{
void far _clearscreen(short);

_clearscreen(_GCLEARSCREEN);
printf("                       -----------------------------------\n");
printf("                              Cyclades Corporation\n");
printf("                         TY %s - Cyclom-Y test.\n",VERSION);
printf("                       -----------------------------------\n");
printf("\n\n");

}



fatal(x)
ushort x;
{
ushort	i;

	for (i = 0 ; i < nchan ; i++) {
		Cyc_close(i,CL_HANGUP);
	}
	Cyc_reset(irq);
	exit(x);
}
