PageRenderTime 69ms CodeModel.GetById 24ms app.highlight 40ms RepoModel.GetById 1ms app.codeStats 0ms

/packages/libogcfpc/src/ogc/lwp_watchdog.inc

https://github.com/slibre/freepascal
Pascal | 259 lines | 176 code | 52 blank | 31 comment | 3 complexity | dbd732df23c3238d5c7f530318a2f476 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, LGPL-3.0
  1
  2{$IFDEF OGC_INTERFACE}
  3//{$if defined(HW_RVL)}
  4//const
  5//  TB_BUS_CLOCK = 243000000;
  6//  TB_CORE_CLOCK = 729000000;
  7//{$elif defined(HW_DOL)}
  8//const
  9//  TB_BUS_CLOCK = 162000000;
 10//  TB_CORE_CLOCK = 486000000;
 11//{$endif}
 12
 13//const
 14//  TB_TIMER_CLOCK =  (TB_BUS_CLOCK div 4000)  ;  //4th of the bus frequency
 15//  TB_SECSPERMIN = 60;
 16//  TB_MINSPERHR = 60;
 17//  TB_MONSPERYR = 12;
 18//  TB_DAYSPERYR = 365;
 19//  TB_HRSPERDAY = 24;
 20//  TB_SECSPERDAY = ( TB_SECSPERMIN * TB_MINSPERHR * TB_HRSPERDAY );
 21//  TB_SECSPERNYR = ( 365 * TB_SECSPERDAY );
 22//  TB_MSPERSEC = 1000;
 23//  TB_USPERSEC = 1000000;
 24//  TB_NSPERSEC = 1000000000;
 25//  TB_NSPERMS = 1000000;
 26//  TB_NSPERUS = 1000;
 27//  TB_USPERTICK = 10000;
 28
 29function ticks_to_cycles(ticks: cuint64): cuint64;	inline;
 30function ticks_to_secs(ticks: cuint64): cuint64;	inline;
 31function ticks_to_millisecs(ticks: cuint64): cuint64;	inline;
 32function ticks_to_microsecs(ticks: cuint64): cuint64;	inline;
 33function ticks_to_nanosecs(ticks: cuint64): cuint64;	inline;
 34function tick_microsecs(ticks: cuint64): cuint64;	inline;
 35function tick_nanosecs(ticks: cuint64): cuint64;	inline;
 36function secs_to_ticks(sec: cuint64): cuint64;	inline;
 37function millisecs_to_ticks(msec: cuint64): cuint64;	inline;
 38function microsecs_to_ticks(usec: cuint64): cuint64;	inline;
 39function nanosecs_to_ticks(nsec: cuint64): cuint64;	inline;
 40function diff_ticks(tick0, tick1: cuint64): cuint64;	inline;
 41function LWP_WD_ABS(x: cint64): cint64; inline;
 42
 43const
 44  LWP_WD_INACTIVE = 0;
 45  LWP_WD_INSERTED = 1;
 46  LWP_WD_ACTIVE = 2;
 47  LWP_WD_REMOVE = 3;
 48  LWP_WD_FORWARD = 0;
 49  LWP_WD_BACKWARD = 1;
 50  LWP_WD_NOTIMEOUT = 0;
 51
 52
 53var
 54  _wd_sync_level : cuint32; external;
 55  _wd_sync_count : cuint32; external;
 56  _wd_ticks_since_boot : cuint32; external;
 57  _wd_ticks_queue : lwp_queue; external;
 58
 59
 60function gettick: cuint32; cdecl; external;
 61function gettime: cuint64; cdecl; external;
 62procedure settime(par0: cuint64); cdecl; external;
 63function diff_sec(start, end_: cuint64): cuint32; cdecl; external;
 64function diff_msec(start, end_: cuint64): cuint32; cdecl; external;
 65function diff_usec(start, end_: cuint64): cuint32; cdecl; external;
 66function diff_nsec(start, end_: cuint64): cuint32; cdecl; external;
 67
 68
 69type
 70  wd_service_routine = procedure(par0: pointer); cdecl;
 71
 72  _wdcntrl = record
 73    node : lwp_node;
 74    start : cuint64;
 75    id : cuint32;
 76    state : cuint32;
 77    fire : cuint64;
 78    routine : wd_service_routine;
 79    usr_data : pointer;
 80  end;
 81  wd_cntrl = _wdcntrl;
 82  pwd_cntrl = ^_wdcntrl;
 83
 84
 85procedure __lwp_watchdog_init; cdecl; external;
 86procedure __lwp_watchdog_settimer(wd: Pwd_cntrl); cdecl; external;
 87procedure __lwp_wd_insert(header: Plwp_queue; wd: Pwd_cntrl); cdecl; external;
 88function __lwp_wd_remove(header: Plwp_queue; wd: Pwd_cntrl): cuint32; cdecl; external;
 89procedure __lwp_wd_tickle(queue: Plwp_queue); cdecl; external;
 90procedure __lwp_wd_adjust(queue: Plwp_queue; dir: cuint32; interval: cint64); cdecl; external;
 91
 92
 93//#ifdef LIBOGC_INTERNAL
 94//#include <libogc/lwp_watchdog.inl>
 95//#endif
 96{$ENDIF}
 97
 98{$IFDEF OGC_IMPLEMENTATION}
 99
100function ticks_to_cycles(ticks: cuint64): cuint64;	inline;
101begin
102  result :=(((cuint64(ticks)*cuint64((TB_CORE_CLOCK*2) div TB_TIMER_CLOCK)) div 2));
103end;
104
105function ticks_to_secs(ticks: cuint64): cuint64;	inline;
106begin
107  result := ((cuint64(ticks) div cuint64(TB_TIMER_CLOCK*1000)));
108end;
109
110function ticks_to_millisecs(ticks: cuint64): cuint64;	inline;
111begin
112  result  := ((cuint64(ticks) div cuint64(TB_TIMER_CLOCK)));
113end;
114
115function ticks_to_microsecs(ticks: cuint64): cuint64;	inline;
116begin
117  result :=	(((cuint64(ticks)*8) div cuint64(TB_TIMER_CLOCK div 125)));
118end;
119
120function ticks_to_nanosecs(ticks: cuint64): cuint64;	inline;
121begin
122  result 	:= (((cuint64(ticks)*8000) div cuint64(TB_TIMER_CLOCK div 125)));
123end;
124
125
126function tick_microsecs(ticks: cuint64): cuint64;	inline;
127begin
128  result 	:=	(((cuint64(ticks)*8) mod cuint64(TB_TIMER_CLOCK div 125)));
129end;
130
131function tick_nanosecs(ticks: cuint64): cuint64;	inline;
132begin
133  result 	:= (((cuint64(ticks)*8000) mod cuint64(TB_TIMER_CLOCK div 125)));
134end;
135
136
137
138function secs_to_ticks(sec: cuint64): cuint64;	inline;
139begin
140  result 		:=	(cuint64(sec)*(TB_TIMER_CLOCK*1000));
141end;
142
143function millisecs_to_ticks(msec: cuint64): cuint64;	inline;
144begin
145  result  :=	(cuint64(msec)*(TB_TIMER_CLOCK));
146end;
147
148function microsecs_to_ticks(usec: cuint64): cuint64;	inline;
149begin
150  result 	:= ((cuint64(usec)*(TB_TIMER_CLOCK div 125)) div 8);
151end;
152
153function nanosecs_to_ticks(nsec: cuint64): cuint64;	inline;
154begin
155  result 	:=	((cuint64(nsec)*(TB_TIMER_CLOCK div 125)) div 8000);
156end;
157
158
159function diff_ticks(tick0, tick1: cuint64): cuint64;	inline;
160begin
161  if tick1 < tick0 then
162    result := -1 - tick0 + tick1
163  else
164    result := tick1 - tick0;
165end;
166
167
168
169function LWP_WD_ABS(x: cint64): cint64; inline;
170begin
171  if x > 0 then result := (x)
172  else
173    result := -x;
174end;
175
176
177procedure __lwp_wd_initialize(wd: pwd_cntrl; routine: wd_service_routine; id: cuint32; usr_data: pointer);
178begin
179	wd^.state := LWP_WD_INACTIVE;
180	wd^.id := id;
181	wd^.routine := routine;
182	wd^.usr_data := usr_data;
183end;
184
185function __lwp_wd_first(queue: plwp_queue): pwd_cntrl;
186begin
187	result := pwd_cntrl(@(queue^.first));
188end;
189
190function __lwp_wd_last(queue: plwp_queue): pwd_cntrl;
191begin
192	result := pwd_cntrl(@(queue^.last));
193end;
194
195function __lwp_wd_next(wd: pwd_cntrl): pwd_cntrl;
196begin
197	result := pwd_cntrl(@(wd^.node.next));
198end;
199
200function __lwp_wd_prev(wd: pwd_cntrl): pwd_cntrl;
201begin
202	result := pwd_cntrl(@(wd^.node.prev));
203end;
204
205procedure __lwp_wd_activate(wd: pwd_cntrl);
206begin
207	wd^.state := LWP_WD_ACTIVE;
208end;
209
210procedure __lwp_wd_deactivate(wd: pwd_cntrl);
211begin
212	wd^.state := LWP_WD_REMOVE;
213end;
214
215function __lwp_wd_isactive(wd: pwd_cntrl): cuint32;
216begin
217	result := 0;
218  if (wd^.state = LWP_WD_ACTIVE) then result := 1;
219end;
220
221function __lwp_wd_calc_ticks(const time: ptimespec): cuint64;
222var
223	ticks: cuint64;
224begin
225	ticks := secs_to_ticks(time^.tv_sec);
226	ticks := ticks + nanosecs_to_ticks(time^.tv_nsec);
227	result := ticks;
228end;
229
230procedure __lwp_wd_tickle_ticks();
231begin
232	__lwp_wd_tickle(@_wd_ticks_queue);
233end;
234
235procedure __lwp_wd_insert_ticks(wd: pwd_cntrl; interval: cint64);
236begin
237	wd^.start := gettime();
238	wd^.fire := (wd^.start + LWP_WD_ABS(interval));
239	__lwp_wd_insert(@_wd_ticks_queue, wd);
240end;
241
242procedure __lwp_wd_adjust_ticks(dir: cuint32; interval: cint64);
243begin
244	__lwp_wd_adjust(@_wd_ticks_queue,dir,interval);
245end;
246
247procedure __lwp_wd_remove_ticks(wd: pwd_cntrl);
248begin
249	__lwp_wd_remove(@_wd_ticks_queue,wd);
250end;
251
252procedure __lwp_wd_reset(wd: pwd_cntrl);
253begin
254	__lwp_wd_remove(@_wd_ticks_queue,wd);
255	__lwp_wd_insert(@_wd_ticks_queue,wd);
256end;
257
258
259{$ENDIF}