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

  1. /* TIMER - Amiga CIA Timer Control Software 0.1a
  2.  
  3.   originally by Paul Higginbottom, Public Domain
  4.  
  5.   hacked on by karl to produce a monotonically increasing microsecond
  6.   clock, 12/30/88, Public Domain
  7.  
  8.   ciafinder is a companion program to ciatimer.  When ciatimer is running,
  9.   it has a timer interrupt installed for a CIA timer, ciafinder will locate
  10.   it and get the time.  ciafinder contains the code you need for your
  11.   application to find the microsecond-resolution CIA timer time.
  12.  
  13.   cc +p ciafinder.c
  14.   ln ciafinder.o -lcl32
  15.  
  16. */
  17.  
  18. #include <exec/types.h>
  19. #include <exec/tasks.h>
  20. #include <functions.h>
  21. #include <exec/interrupts.h>
  22. #include <hardware/cia.h>
  23. #include <hardware/custom.h>
  24. #include <hardware/intbits.h>
  25. #include <resources/cia.h>
  26. #include <stdio.h>
  27.  
  28. #include "ciatimer.h"
  29.  
  30. #define MATCH 0
  31.  
  32. static struct Interrupt
  33.    CIATimerInterrupt,
  34.    *OldCIAInterrupt = (struct Interrupt *)-1;
  35.  
  36. static struct Library *CIAResource = NULL;
  37.  
  38. #define ciatlo ciaa.ciatalo
  39. #define ciathi ciaa.ciatahi
  40. #define ciacr ciaa.ciacra
  41. #define CIAINTBIT CIAICRB_TA
  42. #define CLEAR 0
  43.  
  44. void DummyCIAInterrupt()
  45. {
  46. }
  47.  
  48. struct CIA_Time *LocateCIATimerData()
  49. {
  50.     /* Open the CIA resource */
  51.     if ((CIAResource = (struct Library *)OpenResource(CIAANAME)) == NULL)
  52.     {
  53.         fprintf(stderr,"timer couldn't open cia resource\n");
  54.         return(NULL);
  55.     }
  56.  
  57.     CIATimerInterrupt.is_Node.ln_Type = NT_INTERRUPT;
  58.     CIATimerInterrupt.is_Node.ln_Pri = 127;
  59.     CIATimerInterrupt.is_Code = DummyCIAInterrupt;
  60.     CIATimerInterrupt.is_Node.ln_Name = "ciafinder dummy interrupt";
  61.  
  62.     /* install interrupt */
  63.     if ((OldCIAInterrupt = AddICRVector(CIAResource,CIAINTBIT,&CIATimerInterrupt)) == NULL)
  64.     {
  65.         RemICRVector(CIAResource, CIAINTBIT, &CIATimerInterrupt);
  66.         fprintf(stderr,"no CIA timer currently installed!\n");
  67.         return(NULL);
  68.     }
  69.  
  70.     if (strcmp(OldCIAInterrupt->is_Node.ln_Name,CIATIMER_INTERRUPT_NAME) != MATCH)
  71.     {
  72.         fprintf(stderr,"CIA interrupt routine is '%s' rather than '%s'\n",OldCIAInterrupt->is_Node.ln_Name,CIATIMER_INTERRUPT_NAME);
  73.         return(NULL);
  74.     }
  75.  
  76.     return((struct CIA_Time *)OldCIAInterrupt->is_Data);
  77. }
  78.  
  79. /* return the elapsed real time in seconds and microseconds since the
  80.  * cia timer interrupt handler was installed.
  81.  *
  82.  * ElapsedTime(&secs,µsecs);
  83.  *
  84.  */
  85. void ElapsedTime(tickdata_ptr,sec_ptr,usec_ptr)
  86. struct CIA_Time *tickdata_ptr;
  87. int *sec_ptr,*usec_ptr;
  88. {
  89.     register long seconds, microseconds;
  90.     register long ciahi, cialo;
  91.  
  92.     Disable();
  93.     ciahi = ciathi;
  94.     cialo = ciatlo;
  95.     seconds = tickdata_ptr->CIA_Seconds;
  96.     microseconds = tickdata_ptr->CIA_Microseconds;
  97.     Enable();
  98.     /* total microseconds is CIA_BigTicks * 65536 + timerval * 1.397 */
  99.     /* to multiply the timer ticks * 1.397, you can multiply by 1430
  100.      * and divide by 1024 (or shift right by 10, get it?) 
  101.      */
  102.     ciahi = CIA_TIME_SLICE - ((ciahi << 8) + cialo);
  103.     ciahi = ((ciahi * 1430) >> 10) & 0xffff;
  104.  
  105.     microseconds += ciahi;
  106.     if (microseconds > 1000000)
  107.     {
  108.         microseconds -= 1000000;
  109.         seconds++;
  110.     }
  111.  
  112.     *sec_ptr = seconds;
  113.     *usec_ptr = microseconds;
  114.     return;
  115. }
  116.  
  117. main()
  118. {
  119.     struct CIA_Time *CIA_CurrentTime_ptr;
  120.     long secs, microsecs;
  121.  
  122.     CIA_CurrentTime_ptr = LocateCIATimerData();
  123.     if (CIA_CurrentTime_ptr == NULL)
  124.         exit(1);
  125.  
  126.     ElapsedTime(CIA_CurrentTime_ptr,&secs,µsecs);
  127.  
  128.     printf("secs %d microsecs %d\n",secs,microsecs);
  129. }
  130.