/* timer.c */
#include <exec/types.h>
#include <exec/lists.h>
#include <exec/nodes.h>
#include <exec/ports.h>
#include <exec/io.h>
#include <exec/devices.h>
#include <devices/timer.h>

extern short debug;
struct Device *TimerBase;
struct timeRequest {
   struct IORequest tr_node;
   struct timeval tr_time;
};
struct MsgPort *timerport;
struct timeRequest timermsg;
long timerbit;
inittimer()
{
   short error;
   char tportname[20];

   timerport = CreatePort(0L,0L);
   if(timerport == NULL) {
      printf("cannot createport timer\n");
      return(1);
   }
   error = OpenDevice((UBYTE *)TIMERNAME,UNIT_VBLANK,
                                      (struct IORequest *)&timermsg,0L);
   if(error) {
    /*  DeletePort(timerport);*/
      printf("can't open timer device\n");
      return(1);
   }
   timermsg.tr_node.io_Message.mn_ReplyPort = timerport;
   timermsg.tr_node.io_Command = TR_ADDREQUEST;
   timermsg.tr_node.io_Flags = 0;
   timermsg.tr_node.io_Error = 0;
   TimerBase = timermsg.tr_node.io_Device;
   timerbit = 1L << timerport->mp_SigBit;
   return(0);
}

endtimer()
{
   CloseDevice((struct IORequest *)&timermsg);
  /* DeletePort(timerport); */
}
struct timeval tval;
settimer(secs)
short secs;
{
   if(secs <= 0)secs = 1;
   timermsg.tr_time.tv_secs = secs;
   timermsg.tr_time.tv_micro = 0L;
   timermsg.tr_node.io_Command = TR_ADDREQUEST;
   SendIO((struct IORequest *)&timermsg);
}

cantimer()
{
   AbortIO((struct IORequest *)&timermsg);
   Wait(timerbit);
   WaitIO((struct IORequest *)&timermsg);
}
/* This version works. The main Wait loop must have a WaitIO((struct IORequest *)&timermsg)
*/
