1 /***************************************************************************
2 
3   timer.c
4 
5   Functions needed to generate timing and synchronization between several
6   CPUs.
7 
8 ***************************************************************************/
9 
10 #ifndef __TIMER_H__
11 #define __TIMER_H__
12 
13 typedef INT32 timer_tm;
14 
15 extern timer_tm cycles_to_sec[];
16 extern timer_tm sec_to_cycles[];
17 
18 #define TIME_NOW              (0)
19 #define TIME_NEVER            (2147483647)
20 
21 /* nanoseconds mode
22 #define TIME_ONE_SEC 1000000000
23 #define TIME_ONE_MSEC 1000000
24 #define TIME_ONE_USEC 1000
25 #define TIME_IN_HZ(hz)        (TIME_ONE_SEC / ((timer_tm)(hz)))
26 #define TIME_IN_CYCLES(c,cpu) ((c) * cycles_to_sec[(cpu)])
27 #define TIME_IN_SEC(s)        ((timer_tm)((s) * (TIME_ONE_SEC)))
28 #define TIME_IN_MSEC(ms)      ((timer_tm)((ms) * (TIME_ONE_MSEC)))
29 #define TIME_IN_USEC(us)      ((timer_tm)((us) * (TIME_ONE_USEC)))
30 #define TIME_IN_NSEC(ns)      ((timer_tm)(ns)) */
31 
32 // 2^30 ticks per second mode
33 #define TIME_ONE_SEC (1<<30)
34 #define TIME_IN_HZ(hz)        (TIME_ONE_SEC / ((timer_tm)(hz)))
35 #define TIME_IN_CYCLES(c,cpu) ((c) * cycles_to_sec[(cpu)])
36 #define TIME_IN_SEC(s)        ((timer_tm)((s) * (TIME_ONE_SEC)))
37 #define TIME_IN_MSEC(ms)      ((timer_tm)(((float)(ms)*(float)(TIME_ONE_SEC))/(float)1000))
38 #define TIME_IN_USEC(us)      ((timer_tm)(((float)(us)*(float)(TIME_ONE_SEC))/(float)1000000))
39 #define TIME_IN_NSEC(ns)      (((INT64)(ns)*(INT64)(TIME_ONE_SEC))/(INT64)1000000000)
40 
41 #define CYCLES_CALC(t,cycles) ((((INT64)(t))*((INT64)(cycles)))/((INT64)TIME_ONE_SEC))
42 #define TIME_TO_CYCLES(cpu,t) CYCLES_CALC((t),sec_to_cycles[(cpu)])
43 
44 #define SUSPEND_REASON_HALT		0x0001
45 #define SUSPEND_REASON_RESET	0x0002
46 #define SUSPEND_REASON_SPIN		0x0004
47 #define SUSPEND_REASON_TRIGGER	0x0008
48 #define SUSPEND_REASON_DISABLE	0x0010
49 #define SUSPEND_ANY_REASON		((UINT32)-1)
50 
51 void timer_init(void);
52 void *timer_pulse(timer_tm period, int param, void(*callback)(int));
53 void *timer_set(timer_tm duration, int param, void(*callback)(int));
54 void timer_reset(void *which, timer_tm duration);
55 void timer_remove(void *which);
56 int timer_enable(void *which, int enable);
57 timer_tm timer_timeelapsed(void *which);
58 timer_tm timer_timeleft(void *which);
59 float timer_get_time(void);
60 int timer_schedule_cpu(int *cpu, int *cycles);
61 void timer_update_cpu(int cpu, int ran);
62 void timer_suspendcpu(int cpu, int suspend, int reason);
63 void timer_holdcpu(int cpu, int hold, int reason);
64 int timer_iscpususpended(int cpu, int reason);
65 int timer_iscpuheld(int cpunum, int reason);
66 void timer_suspendcpu_trigger(int cpu, int trigger);
67 void timer_holdcpu_trigger(int cpu, int trigger);
68 void timer_trigger(int trigger);
69 float timer_get_overclock(int cpunum);
70 void timer_set_overclock(int cpunum, float overclock);
71 
72 timer_tm getabsolutetime(void);
73 typedef struct timer_entry
74 {
75 	struct timer_entry *next;
76 	struct timer_entry *prev;
77 	void (*callback)(int);
78 	int callback_param;
79 	int enabled;
80 	timer_tm period;
81 	timer_tm start;
82 	timer_tm expire;
83 } timer_entry;
84 
85 #endif
86