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

	Jumper J18-2 to J19-2 for IRQ4 configuration. (default)

	Connect pin1 (stb) of the printer port (H3) to position B24 of the PC104
	header (H4). "pin1" has a square pad and B24 is opposite jumper J19.

   While in the watch window, execute the function by typing "testint0()".
   The status of the test of INT0 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 testcnt0;

main()
{
   testint0();

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

int testint0()
{
int timeout;
int oldtest;

      outport(PTRCNTR,  0x08);         // make sure strobe line is low

#asm xmemok
      IM    1                 ; interrupt mode 1 for INT0
#endasm

      testcnt0 = 0;
      ISET(ITC, 0);
      if( testcnt0 != 0) 
      {
         printf("Strobe line not connected to IRQ4.\n");
         return 3;
      }

      timeout = 50;
      testcnt0 = 0;

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


#asm xmemok
      IM    2           ; return to mode 2 after usage
#endasm

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

      if(testcnt0 == 15)
      {
         printf("Succesful test for IRQ4 via INT0.\n");
         return 0;
      }

      return 2;
}

#JUMP_VEC   RST38_VEC   int0test

interrupt reti int0test()
{
         outport(PTRCNTR, 0x08);    // set strobe line to low
         testcnt0++;                // increment counter value
         IRES(ITC, 0);
}



