/*
  Program       : Event trigger IRQ test program.

  Description   : You should be input trigger signal to counter 3 clk3. 
		  the on-board controller will acknowledge PC by the IRQ signal.

  Hardware setting: The Jumpers should be set as following :
		    JP1 : Dont'care (or default)
		    JP2 : IRQ 5
		    JP3 : EVTIRQ

  Note          : This program can be compiled both in Turbo C 2.0 or
		  or Turbo C++ 3.0 and running under DOS environment.
*/
#include        <stdio.h>
#include        <conio.h>
#include        <stdlib.h>
#include        <dos.h>

#pragma inline

#ifdef __cplusplus
   #define __CPPARGS            ...
#else
   #define __CPPARGS
#endif

#define BASE                    0x2A0

int irq_gen;

void interrupt ( *old_irq5)(__CPPARGS);

void set_8254(unsigned int value)
{
   outp(BASE+0x0B,0x30);
   outp(BASE+0x08,value & 0xff);
   outp(BASE+0x08,(value & 0xff00) >> 8);
}

void disable_8254()
{
  outp(BASE+0x0B,0x30);
  outp(BASE+0x08,0x00);
  outp(BASE+0x08,0x00);
}

void interrupt irq5_isr(__CPPARGS)
{
    disable();
    irq_gen = 1;
    /*  outp(0xa0,0x20);   re-enable 8259  */
    outp(0x20,0x20);
    enable();
}

void toggle_pc0(void)
{
  outp(BASE+0,0xfe);                    /* toggle bit 0 only */
  outp(BASE+0,0xff);
}


void main(void)
{
  unsigned long i;
  disable_8254();
  old_irq5 = getvect(0x0D);
  setvect(0x0D, irq5_isr);
  outp(0x21, inp(0x21) & 0xDF);        /* enable 8259 */
  /*  outp(0xa1, inp(0xa1) & 0xfb);  */
  do {
    set_8254(0x3);
    irq_gen = 0;
    for(i=0;i<0x5;i++) toggle_pc0();
    if(irq_gen == 1)  printf("\n Event trigger IRQ 5 generate..... !!");
    else    printf("\n No IRQ generated !!");
  } while((getch()|32)!='q');
  setvect(0x0D, old_irq5);
  outp(0x21, inp(0x21) | 0x20);         /*  disable irq 5 interrupt  */
  getch();
}
