PS2SDK
PS2 Homebrew Libraries
timer.h
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 */
10 
16 #ifndef __TIMER_H__
17 #define __TIMER_H__
18 
19 #include <tamtypes.h>
20 
21 // EE Timers
22 #define T0_COUNT ((volatile unsigned int *)0x10000000)
23 #define T0_MODE ((volatile unsigned int *)0x10000010)
24 #define T0_COMP ((volatile unsigned int *)0x10000020)
25 #define T0_HOLD ((volatile unsigned int *)0x10000030)
26 
27 #define T1_COUNT ((volatile unsigned int *)0x10000800)
28 #define T1_MODE ((volatile unsigned int *)0x10000810)
29 #define T1_COMP ((volatile unsigned int *)0x10000820)
30 #define T1_HOLD ((volatile unsigned int *)0x10000830)
31 
32 // Note! T2 and T3 don't have a Tn_HOLD register!
33 // ----------------------------------------------
34 #define T2_COUNT ((volatile unsigned int *)0x10001000)
35 #define T2_MODE ((volatile unsigned int *)0x10001010)
36 #define T2_COMP ((volatile unsigned int *)0x10001020)
37 
38 #define T3_COUNT ((volatile unsigned int *)0x10001800)
39 #define T3_MODE ((volatile unsigned int *)0x10001810)
40 #define T3_COMP ((volatile unsigned int *)0x10001820)
41 
42 // Pointers to the kernel segment (uncached)
43 #define K_T2_COUNT ((volatile unsigned int *)0xB0001000)
44 #define K_T2_MODE ((volatile unsigned int *)0xB0001010)
45 #define K_T2_COMP ((volatile unsigned int *)0xB0001020)
46 
47 #define Tn_MODE(CLKS, GATE, GATS, GATM, ZRET, CUE, CMPE, OVFE, EQUF, OVFF) \
48  (u32)((u32)(CLKS) | ((u32)(GATE) << 2) | \
49  ((u32)(GATS) << 3) | ((u32)(GATM) << 4) | \
50  ((u32)(ZRET) << 6) | ((u32)(CUE) << 7) | \
51  ((u32)(CMPE) << 8) | ((u32)(OVFE) << 9) | \
52  ((u32)(EQUF) << 10) | ((u32)(OVFF) << 11))
53 
54 // Available rates
55 #define kBUSCLK (147456000)
56 #define kBUSCLKBY16 (kBUSCLK / 16)
57 #define kBUSCLKBY256 (kBUSCLK / 256)
58 #define kHBLNK_NTSC (15734)
59 #define kHBLNK_PAL (15625)
60 #define kHBLNK_DTV480p (31469)
61 #define kHBLNK_DTV1080i (33750)
62 
63 typedef u64 (*timer_alarm_handler_t)(s32 id, u64 scheduled_time, u64 actual_time, void *arg, void *pc_value);
64 
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68 
69 extern void _ps2sdk_init_timer_impl(void);
70 extern void _ps2sdk_init_timer(void);
71 extern void _ps2sdk_deinit_timer_impl(void);
72 extern void _ps2sdk_deinit_timer(void);
73 
74 extern s32 InitTimer(s32 in_mode);
75 extern s32 EndTimer(void);
76 extern s32 GetTimerPreScaleFactor(void);
77 extern s32 StartTimerSystemTime(void);
78 extern s32 StopTimerSystemTime(void);
79 extern void SetNextComp(u64 time);
80 extern u64 iGetTimerSystemTime(void);
81 extern u64 GetTimerSystemTime(void);
82 extern s32 iAllocTimerCounter(void);
83 extern s32 AllocTimerCounter(void);
84 extern s32 iFreeTimerCounter(s32 id);
85 extern s32 FreeTimerCounter(s32 id);
86 extern s32 iGetTimerUsedUnusedCounters(u32 *used_counters, u32 *unused_counters);
87 extern s32 GetTimerUsedUnusedCounters(u32 *used_counters, u32 *unused_counters);
88 extern s32 iStartTimerCounter(s32 id);
89 extern s32 StartTimerCounter(s32 id);
90 extern s32 iStopTimerCounter(s32 id);
91 extern s32 StopTimerCounter(s32 id);
92 extern u64 SetTimerCount(s32 id, u64 timer_count);
93 extern u64 iGetTimerBaseTime(s32 id);
94 extern u64 GetTimerBaseTime(s32 id);
95 extern u64 iGetTimerCount(s32 id);
96 extern u64 GetTimerCount(s32 id);
97 extern s32 iSetTimerHandler(s32 id, u64 scheduled_time, timer_alarm_handler_t callback_handler, void *arg);
98 extern s32 SetTimerHandler(s32 id, u64 scheduled_time, timer_alarm_handler_t callback_handler, void *arg);
99 extern void TimerBusClock2USec(u64 clocks, u32 *seconds_result, u32 *microseconds_result);
100 extern u64 TimerUSec2BusClock(u32 seconds, u32 microseconds);
101 extern float TimerBusClock2Freq(s64 clocks);
102 extern u64 TimerFreq2BusClock(float timer_frequency);
103 extern u32 cpu_ticks(void);
104 
105 // Additional conversion functions, from time to bus cycles
106 static inline u64 NSec2TimerBusClock(u64 usec) {
107  // Approximate, avoid 64 bit division
108  return ((((kBUSCLK / 1000) * 65536ULL) / 1000000) * usec) >> 16;
109 }
110 
111 static inline u64 USec2TimerBusClock(u64 usec) {
112  // Approximate, avoid 64 bit division
113  return ((((kBUSCLK / 1000) * 1024) / 1000) * usec) >> 10;
114 }
115 
116 static inline u64 MSec2TimerBusClock(u64 msec) {
117  return (kBUSCLK / 1000) * msec;
118 }
119 
120 static inline u64 Sec2TimerBusClock(u64 sec) {
121  return kBUSCLK * sec;
122 }
123 
124 #ifdef __cplusplus
125 }
126 #endif
127 
128 #endif /* __TIMER_H__ */
tamtypes.h