/**************************************************************************/
/*  Program	:INTCOM12.C						  */
/*  Description	:Demo program for ADAM-4500             		  */
/*		 Use both COM1 and COM2 interrupt functions               */
/*		 Receive 'A' from COM1 then send '1' back                 */
/*               Receive 'A' from COM2 then send '2' back                 */
/**************************************************************************/

#include	<stdio.h>
#include	<math.h>
#include	<dos.h>
#include	<conio.h>

static	int base0=0x3f8;
static	int base1=0x2f8;

void interrupt com1(void)
{
	char com1data;

	disable();

	com1data=inportb(base0);
	if (com1data=='A')
	 outportb(base0,'1');

	enable();
	EOI();
}

void interrupt com2(void)
{
	char com2data;

	disable();

	com2data=inportb(base1);
	if (com2data=='A')
	 outportb(base1,'2');

	enable();
	EOI();
}

void set_com1_vect(void)
{
	setvect(0xc, com1);
}

void set_com2_vect(void)
{
	setvect(0xe, com2);
}

int EOI(void)
{
	outport(0xff22,0x8FFC);	     /*	EOI  INT 0*/
}

void enable_com1_irq(void)
{
	outport(0xff28,inport(0xff28) &	0xffef); /* INT0 interrupt enable*/
}
void enable_com2_irq(void)
{
	outport(0xff28,inport(0xff28) &	0xffbf); /* INT2 interrupt enable*/
}

void disable_com1_irq(void)
{
	outport(0xff28,inport(0xff28) |	0x0010); /* INT0 interrupt disable */
}

void disable_com2_irq(void)
{
	outport(0xff28,inport(0xff28) |	0x0040); /* INT2 interrupt disable */
}

void main(void)
{
	int i,j;
	char temp,ch;

	disable(); /* disable interrupt	*/

	set_com1_vect();
	set_com2_vect();

	outportb((0x3f8+2),0x03);	 /*	    enable COM1	FIFO */
	outportb((0x2f8+2),0x03);	 /*	    enable COM2	FIFO */

	outportb(0x3f8+3,0x80);			    /* set DLAB=1 */
	outportb(0x3f8	,0x0C);	outportb(0x3f8+1,0x0);	/* set buad = 9600   */
	outportb(0x3f8+3,0x03);			    /* set data=8 stop=1 no parity */
	outportb(0x3f8+1,0x01);			    /* enable COM1 interrupt */

	outportb(0x2f8+3,0x80);			    /* set DLAB=1 */
	outportb(0x2f8	,0x0C);	outportb(0x2f8+1,0x0);	/* set buad = 9600   */
	outportb(0x2f8+3,0x03);			    /* set data=8 stop=1 no parity */
	outportb(0x2f8+1,0x01);			    /* enable COM2 interrupt */

	 enable(); /* enable interrupt */
	 enable_com1_irq();
	 enable_com2_irq();

	 while(1)
	 {
	   if(kbhit()) {
	     ch=getch();
	     if((ch=='Q')||(ch=='q')) {
	       exit(0);
	     }
	   }
	   printf("\nPress 'Q' or 'q' to Quit...");
	 }

}
