home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d178 / ciatimer.lha / CIATimer / CIATimer2 / ciatimer.c < prev    next >
C/C++ Source or Header  |  1989-02-04  |  3KB  |  134 lines

  1. /* TIMER - Amiga CIA Timer Control Software 0.1a
  2.  
  3.   originally by Paul Higginbottom, Public Domain
  4.  
  5.   hacked on by Karl Lehenbauer to produce a monotonically increasing microsecond
  6.   clock, 12/30/88, Public Domain
  7.  
  8.   further hacking by Karl to provide arbitrary tasks the ability to locate
  9.   the CIA interrupt's time data, 1/5/88, Public Domain
  10.  
  11.   cc +p ciatimer.c
  12.   ln ciatimer.o -lcl32
  13.  
  14. To start up the timer, execute ciatimer.  To kill it, control-C.  The task
  15. doesn't do anything except wait for the control-C and clean up afterwards,
  16. removing the interrupt.
  17.  
  18. ciafinder may then be used to locate the is_Data section of ciatimer's
  19. interrupt, where ciatimer writes its time data.
  20. */
  21.  
  22. #include <exec/types.h>
  23. #include <exec/tasks.h>
  24. #include <functions.h>
  25. #include <exec/interrupts.h>
  26. #include <hardware/cia.h>
  27. #include <hardware/custom.h>
  28. #include <hardware/intbits.h>
  29. #include <resources/cia.h>
  30. #include <stdio.h>
  31. #include <libraries/dos.h>
  32.  
  33. #include "ciatimer.h"
  34.  
  35. struct CIA_Time CIA_CurrentTime = {0, 0};
  36.  
  37. static struct Interrupt
  38.    CIATimerInterrupt,
  39.    *OldCIAInterrupt = (struct Interrupt *)-1;
  40.  
  41. static struct Library *CIAResource = NULL;
  42.  
  43. #define ciatlo ciaa.ciatalo
  44. #define ciathi ciaa.ciatahi
  45. #define ciacr ciaa.ciacra
  46. #define CIAINTBIT CIAICRB_TA
  47. #define CLEAR 0
  48.  
  49. void CIAInterrupt()
  50. {
  51.     CIA_CurrentTime.CIA_Microseconds += 65536;
  52.     if (CIA_CurrentTime.CIA_Microseconds > 1000000)
  53.     {
  54.         CIA_CurrentTime.CIA_Seconds++;
  55.         CIA_CurrentTime.CIA_Microseconds -= 1000000;
  56.     }
  57. }
  58.  
  59. /* start the timer, clear pending interrupts, and enable timer A
  60.  * Interrupts */
  61. StartCIATimer()
  62. {
  63.     ciacr &= ~(CIACRAF_RUNMODE);    /* set it to reload on overflow */
  64.     ciacr |= (CIACRAF_LOAD | CIAICRF_TA);
  65.     SetICR(CIAResource,CLEAR|CIAICRF_TA);
  66.     AbleICR(CIAResource, CIAICRF_SETCLR | CIAICRF_TA);
  67. }
  68.  
  69. void StopCIATimer()
  70. {
  71.     AbleICR(CIAResource, CLEAR | CIAICRF_TA);
  72.     ciacr &= ~CIACRAF_START;
  73. }
  74.  
  75. /* set period between timer increments */
  76. void SetCIATimer(micros)
  77. unsigned short micros;
  78. {
  79.     ciatlo = micros & 0xff;
  80.     ciathi = micros >> 8;
  81. }
  82.  
  83. /* stop the timer and remove its interrupt vector */
  84. EndCIATimer()
  85. {
  86.     if (OldCIAInterrupt == NULL)
  87.     {
  88.         StopCIATimer();
  89.         RemICRVector(CIAResource, CIAINTBIT, &CIATimerInterrupt);
  90.     }
  91. }
  92.  
  93. BOOL BeginCIATimer()
  94. {
  95.     /* Open the CIA resource */
  96.     if ((CIAResource = (struct Library *)OpenResource(CIAANAME)) == NULL)
  97.     {
  98.         fprintf(stderr,"cia periodic timer startup couldn't open cia resource\n");
  99.         return(0);
  100.     }
  101.  
  102.     CIATimerInterrupt.is_Node.ln_Type = NT_INTERRUPT;
  103.     CIATimerInterrupt.is_Node.ln_Pri = 127;
  104.     CIATimerInterrupt.is_Node.ln_Name =  CIATIMER_INTERRUPT_NAME;
  105.     CIATimerInterrupt.is_Code = CIAInterrupt;
  106.     CIATimerInterrupt.is_Data = (APTR)&CIA_CurrentTime;
  107.  
  108.     /* install interrupt */
  109.     if ((OldCIAInterrupt = AddICRVector(CIAResource,CIAINTBIT,&CIATimerInterrupt)) != NULL)
  110.     {
  111.         fprintf(stderr,"cia timer interrupt already in use by '%s'",OldCIAInterrupt->is_Node.ln_Name);
  112.         EndCIATimer();
  113.         return(0);
  114.     }
  115.  
  116.     SetCIATimer(CIA_TIME_SLICE);
  117.  
  118.     StartCIATimer();
  119.     return(TRUE);
  120. }
  121.  
  122. main()
  123. {
  124.     if (BeginCIATimer())
  125.     {
  126.         Wait(SIGBREAKF_CTRL_C);
  127.     }
  128.     else 
  129.         exit(1);
  130.  
  131.     EndCIATimer();
  132.     exit(0);
  133. }
  134.