/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

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