1 
2 /*
3  *   O2EM Free Odyssey2 / Videopac+ Emulator
4  *
5  *   Created by Daniel Boris <dboris@comcast.net>  (c) 1997,1998
6  *
7  *   Developed by Andre de la Rocha <adlroc@users.sourceforge.net>
8  *
9  *   http://o2em.sourceforge.net
10  *
11  *
12  *
13  *   Timing functions
14  */
15 
16 
17 #include <time.h>
18 #include "allegro.h"
19 #ifdef ALLEGRO_WINDOWS
20 #include "winalleg.h"
21 #elif defined(ALLEGRO_UNIX) || defined(ALLEGRO_LINUX)
22 #include <sys/time.h>
23 #include <sys/times.h>
24 #endif
25 #include "timefunc.h"
26 
27 
28 
29 #ifdef ALLEGRO_WINDOWS
30 
31 /* Windows */
gettimeticks(void)32 long gettimeticks(void){
33 	LARGE_INTEGER counter, freq;
34 	static LONG_LONG first=-1;
35 	if (QueryPerformanceCounter(&counter))
36 		if (QueryPerformanceFrequency(&freq)) {
37 			if (first<0) first = counter.QuadPart;
38 			return (long)(((counter.QuadPart-first)*TICKSPERSEC)/freq.QuadPart);
39 		}
40 	return (long)((((LONG_LONG)clock())*TICKSPERSEC)/CLOCKS_PER_SEC);
41 }
42 
43 #elif defined(ALLEGRO_UNIX) || defined(ALLEGRO_LINUX)
44 
45 #ifdef _BSD_SOURCE
46 
47 /* Unix with gettimeofday */
gettimeticks(void)48 long gettimeticks(void){
49 	struct timeval tv;
50 	static LONG_LONG first=-1;
51 	LONG_LONG ll;
52 	if (first<0) {
53 		if (gettimeofday(&tv, NULL) != 0)
54 			return (long)((((LONG_LONG)clock())*TICKSPERSEC)/CLOCKS_PER_SEC);
55 		first = ((LONG_LONG)tv.tv_sec)*1000000 + tv.tv_usec;
56 	}
57 	if (gettimeofday(&tv, NULL) == 0) {
58 		ll = ((LONG_LONG)tv.tv_sec)*1000000 + tv.tv_usec;
59 		return (long) (((ll-first)*TICKSPERSEC)/1000000);
60 	}
61 	return (long)((((LONG_LONG)clock())*TICKSPERSEC)/CLOCKS_PER_SEC);
62 }
63 
64 #else
65 
66 /* Unix without gettimeofday */
gettimeticks(void)67 long gettimeticks(void){
68 	struct tms t;
69 	return (long)((((LONG_LONG)times(&t))*TICKSPERSEC)/sysconf(_SC_CLK_TCK));
70 }
71 
72 #endif
73 
74 #else
75 
76 /* Default (use Allegro timers) */
77 
78 volatile long ticks_counter = 0;
79 static int timer_init = 0;
80 
inc_ticks_counter(void)81 void inc_ticks_counter(void){
82    ticks_counter++;
83 }
84 
85 END_OF_FUNCTION(inc_ticks_counter);
86 
gettimeticks(void)87 long gettimeticks(void){
88 	if (timer_init==0) {
89 		ticks_counter=0;
90 		LOCK_VARIABLE(ticks_counter);
91 		LOCK_FUNCTION(inc_ticks_counter);
92    		install_int(inc_ticks_counter, 1);
93    		timer_init=1;
94 	}
95 	return (long)(((LONG_LONG)ticks_counter*TICKSPERSEC)/1000);
96 }
97 
98 #endif
99 
100