/*
	INT2.C
	
   Sample program showing the use of INT2 of the Z180 as IRQ3 for the PC104
	bus. Use the strobe line (stb) of the printer port to drive the interrupt.

	Jumper J20-2 to J20-3 for IRQ3 configuration. (default)

	Connect pin1(stb) of the printer port (H3) to position B25 of the PC104
	header (H4). "pin1" has a square pad and B25 is opposite jumper J20.
	
   While in the watch window, execute the function by typing "testint2()".
   The status of the test of INT2 will be indicated in the stdio window.
*/

#use "default.h"

#if (BOARD_TYPE!=Z104ISA)
#fatal "This program only runs on the Z104 or ZISA boards."
#endif

int testcnt2;

main()
{
   testint2();

   while(1) 
   {
      runwatch();   // execute testint2() from the watch window
      hitwd();
   }
}

int testint2()
{
int timeout;
int oldtest;
   
      outport(PTRCNTR,  0x08);              // make sure strobe line is low

      testcnt2 = 0;
      ISET(ITC, 2);                         // set interrupt on

      if( testcnt2 != 0) 
      {
         printf("Strobe line not connected to IRQ3.\n");
         return 3;
      }

      timeout = 50;
      testcnt2 = 0;

      while(--timeout)
      {
            if(testcnt2==15)
               break;         // end the test
            if(oldtest == testcnt2)
            {
                  outport(PTRCNTR, 0x00);       // strobe output high
                  ISET(ITC, 2);
            }
            else
            {
                  oldtest = testcnt2;
            }
       }

       IRES(ITC, 2);
       outport(PTRCNTR, 0x08);                  // strobe low


      if(!timeout)
      {
         printf("Test Time Out. Make sure J20-2 is connected to J20-3\n");
         return 1;      // timeout
      }

      if(testcnt2 == 15)
      {
         printf("Succesful test for IRQ3 via INT2.\n");
         return 0;
      }

      return 2;
}

#INT_VEC INT2_VEC int2test

interrupt reti int2test()
{
        outport(PTRCNTR, 0x08);     // set strobe line back to low
        testcnt2++;                 // increment test counter
        IRES(ITC,2);
}