PS2SDK
PS2 Homebrew Libraries
timer.c
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright 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 
11 #include "actimer_internal.h"
12 
13 static struct timer_softc Timerc;
14 
15 int acTimerAdd(acTimerT timer, acTimerDone done, void *arg, unsigned int us)
16 {
17  acQueueT q_prev;
19  acSpl state;
20 
21  if ( !Timerc.tick )
22  return -6;
23  if ( timer == 0 )
24  {
25  return -22;
26  }
27  if ( done == 0 )
28  {
29  return -22;
30  }
31  timer->t_done = done;
32  timer->t_arg = arg;
33  USec2SysClock(us, &t);
34  timer->t_deadline = t.lo | ((acUint64)(t.hi) << 32);
35  GetSystemTime(&t);
36  timer->t_deadline += t.lo | ((acUint64)(t.hi) << 32);
37  CpuSuspendIntr(&state);
38  q_prev = Timerc.waitq.q_prev;
39  timer->t_chain.q_next = (acQueueT)&Timerc;
40  timer->t_chain.q_prev = q_prev;
41  q_prev->q_next = &timer->t_chain;
42  Timerc.waitq.q_prev = &timer->t_chain;
43  CpuResumeIntr(state);
44  return 0;
45 }
46 
47 int acTimerRemove(acTimerT timer)
48 {
49  acQueueT _next_;
50  acQueueT _prev_;
51  acSpl state;
52 
53  if ( !Timerc.tick )
54  return -6;
55  if ( timer == 0 )
56  {
57  return -22;
58  }
59  CpuSuspendIntr(&state);
60  if ( timer->t_deadline )
61  {
62  _next_ = timer->t_chain.q_next;
63  _prev_ = timer->t_chain.q_prev;
64  _prev_->q_next = _next_;
65  _next_->q_prev = _prev_;
66  }
67  CpuResumeIntr(state);
68  return 0;
69 }
70 
71 static unsigned int timer_handler(void *arg)
72 {
73  acTime current;
74  acTimerT timer;
75  acTimerData *q_next;
76  acQueueT q_prev;
77  acTimerDone t_done;
78  void *t_arg;
79  iop_sys_clock_t v9;
80  struct timer_softc *argt;
81 
82  argt = (struct timer_softc *)arg;
83  GetSystemTime(&v9);
84  memcpy(&current, &v9, sizeof(v9));
85  if ( argt->tick == 0 )
86  {
87  return 0;
88  }
89  timer = (acTimerT)argt->waitq.q_next;
90  while ( argt != (struct timer_softc *)timer )
91  {
92  q_next = (acTimerData *)timer->t_chain.q_next;
93  if ( current >= timer->t_deadline )
94  {
95  q_prev = timer->t_chain.q_prev;
96  q_prev->q_next = &q_next->t_chain;
97  q_next->t_chain.q_prev = q_prev;
98  t_done = timer->t_done;
99  t_arg = timer->t_arg;
100  timer->t_deadline = 0LL;
101  t_done(timer, t_arg);
102  }
103  timer = q_next;
104  }
105  return argt->tick;
106 }
107 
108 int acTimerModuleStart(int argc, char **argv)
109 {
110  int tick;
111  int v5;
112  int v6;
113  iop_sys_clock_t t;
114  char *next;
115 
116  tick = 1000000;
117  if ( Timerc.tick )
118  return -16;
119  if ( argc >= 2 )
120  {
121  tick = strtol(argv[1], &next, 10);
122  if ( next == argv[1] )
123  {
124  tick = 1000000;
125  }
126  else if ( tick < 1000 )
127  {
128  tick = 1000;
129  }
130  }
131  Timerc.tick = 0LL;
132  Timerc.waitq.q_prev = (acQueueT)&Timerc;
133  Timerc.waitq.q_next = (acQueueT)&Timerc;
134  USec2SysClock(tick, &t);
135  v5 = SetAlarm(&t, timer_handler, &Timerc);
136  if ( v5 == -400 )
137  return -12;
138  v6 = 0;
139  if ( v5 == -104 )
140  {
141  CancelAlarm(timer_handler, &Timerc);
142  SetAlarm(&t, timer_handler, &Timerc);
143  v6 = 0;
144  }
145  memcpy(&(Timerc.tick), &t, sizeof(t));
146  return v6;
147 }
148 
149 int acTimerModuleStop()
150 {
151  if ( Timerc.tick )
152  {
153  CancelAlarm(timer_handler, &Timerc);
154  Timerc.tick = 0LL;
155  }
156  return 0;
157 }
158 
159 int acTimerModuleRestart(int argc, char **argv)
160 {
161  (void)argc;
162  (void)argv;
163 
164  return -88;
165 }
166 
167 int acTimerModuleStatus()
168 {
169  return Timerc.tick != 0;
170 }
ac_queue
Definition: accore.h:52
CpuSuspendIntr
int CpuSuspendIntr(int *state)
Definition: intrman.c:195
ac_timer
Definition: actimer.h:21
CpuResumeIntr
int CpuResumeIntr(int state)
Definition: intrman.c:217
timer_softc
Definition: actimer_internal.h:17
_iop_sys_clock
Definition: thbase.h:87