home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / info / powerglo.lzh / glovehack / timer.c < prev   
Encoding:
C/C++ Source or Header  |  1991-10-12  |  1.9 KB  |  97 lines

  1. /*
  2.  * This file implements a running millisecond timer using one of
  3.  * the Amiga's hardware timers.
  4.  *
  5.  * This file must be compiled with Lattice -b0 and -v options.
  6.  *
  7.  * Alan Bland, August 1988
  8.  * Updated June 1990 to follow Amiga Mail guidelines.
  9.  * Ugly hacks added October 1991 for power glove stuff.
  10.  */
  11.  
  12. #include <exec/types.h>
  13. #include <exec/ports.h>
  14. #include <exec/memory.h>
  15. #include <exec/interrupts.h>
  16. #include <hardware/cia.h>
  17. #include <resources/cia.h>
  18.  
  19. #include <proto/exec.h>
  20.  
  21. static volatile int times_up;
  22. extern struct CIA ciab;
  23. static struct Library *CIAResource;
  24. static struct Interrupt interrupt;
  25. static UBYTE interrupting;
  26.  
  27. void closetimer(void);
  28.  
  29. void
  30. fatal(char *s)
  31. {
  32.     printf("fatal error: %s\n", s);
  33.     closetimer();
  34.     exit(1);
  35. }
  36.  
  37. void
  38. timerhandler()
  39. {
  40.     /* hardware timer interrupt */
  41.     times_up = 1;
  42. }
  43.  
  44. void
  45. timersleep(long ticks)
  46. {
  47.     times_up = 0;
  48.  
  49.     ticks -= 8; if (ticks <= 0) { times_up=1; return; } /* yucko */
  50.  
  51.     /* 1.397 usec * n */
  52.     ciab.ciatblo = ticks & 0xff;
  53.     ciab.ciatbhi = (ticks>>8) & 0xff;
  54.  
  55.     /* one-shot */
  56.     ciab.ciacrb |= CIACRBF_RUNMODE | CIACRBF_LOAD | CIACRBF_START;
  57.  
  58.     while (!times_up) {}
  59. }
  60.  
  61. void
  62. opentimer()
  63. {
  64.     static char* timername = "glove.timer";
  65.  
  66.     /* install interrupt handler */
  67.     CIAResource = OpenResource(CIABNAME);
  68.     if (CIAResource == NULL) fatal("can't get timer resource");
  69.  
  70.     interrupt.is_Node.ln_Type = NT_INTERRUPT;
  71.     interrupt.is_Node.ln_Pri = 127;
  72.     interrupt.is_Node.ln_Name = timername;
  73.     interrupt.is_Data = NULL;
  74.     interrupt.is_Code = timerhandler;
  75.     if (AddICRVector(CIAResource, CIAICRB_TB, &interrupt) != NULL) {
  76.         fatal("can't use CIA B Timer B");
  77.     }
  78.  
  79.     /* this timer is all mine */
  80.     interrupting = 1;
  81.  
  82.     SetICR(CIAResource, CIAICRF_TB);
  83.     AbleICR(CIAResource, CIAICRF_SETCLR | CIAICRF_TB);
  84.     times_up = 0;
  85. }
  86.  
  87. void
  88. closetimer()
  89. {
  90.     if (interrupting) {
  91.         ciab.ciacrb &= ~CIACRBF_START;
  92.         AbleICR(CIAResource, CIAICRF_TB);
  93.         RemICRVector(CIAResource, CIAICRB_TB, &interrupt);
  94.         interrupting = 0;
  95.     }
  96. }
  97.