home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / info / uforce.lzh / timer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-25  |  2.0 KB  |  100 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.  * timer.h added March 1992 by Ethan Dicks <erd@kumiss.UUCP>
  11.  */
  12.  
  13. #include <exec/types.h>
  14. #include <exec/ports.h>
  15. #include <exec/memory.h>
  16. #include <exec/interrupts.h>
  17. #include <hardware/cia.h>
  18. #include <resources/cia.h>
  19.  
  20. #include <proto/exec.h>
  21.  
  22. #include "timer.h"
  23.  
  24. static volatile int times_up;
  25. extern struct CIA ciab;
  26. static struct Library *CIAResource;
  27. static struct Interrupt interrupt;
  28. static UBYTE interrupting;
  29.  
  30. void closetimer(void);
  31.  
  32. void
  33. fatal(char *s)
  34. {
  35.     printf("fatal error: %s\n", s);
  36.     closetimer();
  37.     exit(1);
  38. }
  39.  
  40. void
  41. timerhandler()
  42. {
  43.     /* hardware timer interrupt */
  44.     times_up = 1;
  45. }
  46.  
  47. void
  48. timersleep(long ticks)
  49. {
  50.     times_up = 0;
  51.  
  52.     ticks -= 8; if (ticks <= 0) { times_up=1; return; } /* yucko */
  53.  
  54.     /* 1.397 usec * n */
  55.     ciab.ciatblo = ticks & 0xff;
  56.     ciab.ciatbhi = (ticks>>8) & 0xff;
  57.  
  58.     /* one-shot */
  59.     ciab.ciacrb |= CIACRBF_RUNMODE | CIACRBF_LOAD | CIACRBF_START;
  60.  
  61.     while (!times_up) {}
  62. }
  63.  
  64. void
  65. opentimer()
  66. {
  67.     static char* timername = "glove.timer";
  68.  
  69.     /* install interrupt handler */
  70.     CIAResource = OpenResource(CIABNAME);
  71.     if (CIAResource == NULL) fatal("can't get timer resource");
  72.  
  73.     interrupt.is_Node.ln_Type = NT_INTERRUPT;
  74.     interrupt.is_Node.ln_Pri = 127;
  75.     interrupt.is_Node.ln_Name = timername;
  76.     interrupt.is_Data = NULL;
  77.     interrupt.is_Code = timerhandler;
  78.     if (AddICRVector(CIAResource, CIAICRB_TB, &interrupt) != NULL) {
  79.         fatal("can't use CIA B Timer B");
  80.     }
  81.  
  82.     /* this timer is all mine */
  83.     interrupting = 1;
  84.  
  85.     SetICR(CIAResource, CIAICRF_TB);
  86.     AbleICR(CIAResource, CIAICRF_SETCLR | CIAICRF_TB);
  87.     times_up = 0;
  88. }
  89.  
  90. void
  91. closetimer()
  92. {
  93.     if (interrupting) {
  94.         ciab.ciacrb &= ~CIACRBF_START;
  95.         AbleICR(CIAResource, CIAICRF_TB);
  96.         RemICRVector(CIAResource, CIAICRB_TB, &interrupt);
  97.         interrupting = 0;
  98.     }
  99. }
  100.