2
0

realtime.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. {
  2. This file is part of the Free Pascal run time library.
  3. A file in Amiga system run time library.
  4. Copyright (c) 1998-2003 by Nils Sjoholm
  5. member of the Amiga RTL development team.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {
  13. History:
  14. Added functions and procedures with array of const.
  15. For use with fpc 1.0.7. They are in systemvartags.
  16. 11 Nov 2002.
  17. Added the defines use_amiga_smartlink and
  18. use_auto_openlib. Implemented autoopening of the
  19. library.
  20. 14 Jan 2003.
  21. Changed integer > smallint,
  22. cardinal > longword.
  23. Changed startcode for unit.
  24. 09 Feb 2003.
  25. [email protected]
  26. }
  27. {$I useamigasmartlink.inc}
  28. {$ifdef use_amiga_smartlink}
  29. {$smartlink on}
  30. {$endif use_amiga_smartlink}
  31. UNIT realtime;
  32. INTERFACE
  33. USES exec, utility;
  34. {***************************************************************************}
  35. const
  36. { realtime.library's idea of time is based on a clock which emits a pulse
  37. * 1200 times a second (1.2kHz). All time values maintained by realtime.library
  38. * are based on this number. For example, the field RealTimeBase->rtb_Time
  39. * expresses an amount of time equivalent to (RealTimeBase->rtb_Time/TICK_FREQ)
  40. * seconds.
  41. }
  42. TICK_FREQ = 1200;
  43. {***************************************************************************}
  44. { Each Conductor represents a group of applications which wish to remain
  45. * synchronized together.
  46. *
  47. * This structure must only be allocated by realtime.library and is
  48. * READ-ONLY!
  49. }
  50. Type
  51. pConductor = ^tConductor;
  52. tConductor = record
  53. cdt_Link : tNode;
  54. cdt_Reserved0 : WORD;
  55. cdt_Players : tMinList; { this conductor's players }
  56. cdt_ClockTime, { current time of this sequence }
  57. cdt_StartTime, { start time of this sequence }
  58. cdt_ExternalTime, { time from external unit }
  59. cdt_MaxExternalTime, { upper limit on sync'd time }
  60. cdt_Metronome : ULONG; { MetricTime highest pri node }
  61. cdt_Reserved1 : WORD;
  62. cdt_Flags : WORD; { conductor flags }
  63. cdt_State : Byte; { playing or stopped }
  64. end;
  65. const
  66. { Flag bits for Conductor.cdt_Flags }
  67. CONDUCTF_EXTERNAL = 1; { clock is externally driven }
  68. CONDUCTF_GOTTICK = 2; { received 1st external tick }
  69. CONDUCTF_METROSET = 4; { cdt_Metronome filled in }
  70. CONDUCTF_PRIVATE = 8; { conductor is private }
  71. CONDUCTB_EXTERNAL = 0;
  72. CONDUCTB_GOTTICK = 1;
  73. CONDUCTB_METROSET = 2;
  74. CONDUCTB_PRIVATE = 3;
  75. { constants for Conductor.cdt_State and SetConductorState() }
  76. CONDSTATE_STOPPED = 0; { clock is stopped }
  77. CONDSTATE_PAUSED = 1; { clock is paused }
  78. CONDSTATE_LOCATE = 2; { go to 'running' when ready }
  79. CONDSTATE_RUNNING = 3; { run clock NOW }
  80. { These do not actually exist as Conductor states, but are used as additional
  81. * arguments to SetConductorState()
  82. }
  83. CONDSTATE_METRIC = -1; { ask high node to locate }
  84. CONDSTATE_SHUTTLE = -2; { time changing but not running }
  85. CONDSTATE_LOCATE_SET = -3; { maestro done locating }
  86. {***************************************************************************}
  87. { The Player is the connection between a Conductor and an application.
  88. *
  89. * This structure must only be allocated by realtime.library and is
  90. * READ-ONLY!
  91. }
  92. Type
  93. pPlayer = ^tPlayer;
  94. tPlayer = record
  95. pl_Link : tNode;
  96. pl_Reserved0,
  97. pl_Reserved1 : Shortint;
  98. pl_Hook : pHook; { player's hook function }
  99. pl_Source : pConductor; { pointer to parent context }
  100. pl_Task : pTask; { task to signal for alarm }
  101. pl_MetricTime : Longint; { current time in app's metric }
  102. pl_AlarmTime : Longint; { time to wake up }
  103. pl_UserData : Pointer; { for application use }
  104. pl_PlayerID : WORD; { for application use }
  105. pl_Flags : WORD; { general Player flags }
  106. end;
  107. const
  108. { Flag bits for Player.pl_Flags }
  109. PLAYERF_READY = 1; { player is ready to go! }
  110. PLAYERF_ALARMSET = 2; { alarm is set }
  111. PLAYERF_QUIET = 3; { a dummy player, used for sync }
  112. PLAYERF_CONDUCTED = 8; { give me metered time }
  113. PLAYERF_EXTSYNC = 16; { granted external sync }
  114. PLAYERB_READY = 0;
  115. PLAYERB_ALARMSET = 1;
  116. PLAYERB_QUIET = 2;
  117. PLAYERB_CONDUCTED = 3;
  118. PLAYERB_EXTSYNC = 4;
  119. {***************************************************************************}
  120. { Tags for CreatePlayer(), SetPlayerAttrs(), and GetPlayerAttrs() }
  121. PLAYER_Base = (TAG_USER+64) ;
  122. PLAYER_Hook = (PLAYER_Base+1) ; { set address of hook function }
  123. PLAYER_Name = (PLAYER_Base+2) ; { name of player }
  124. PLAYER_Priority = (PLAYER_Base+3) ; { priority of player }
  125. PLAYER_Conductor = (PLAYER_Base+4) ; { set conductor for player }
  126. PLAYER_Ready = (PLAYER_Base+5) ; { the "ready" flag }
  127. PLAYER_AlarmTime = (PLAYER_Base+12); { alarm time (sets PLAYERF_ALARMSET) }
  128. PLAYER_Alarm = (PLAYER_Base+13); { sets/clears PLAYERF_ALARMSET flag }
  129. PLAYER_AlarmSigTask = (PLAYER_Base+6) ; { task to signal for alarm/notify }
  130. PLAYER_AlarmSigBit = (PLAYER_Base+8) ; { signal bit for alarm (or -1) }
  131. PLAYER_Conducted = (PLAYER_Base+7) ; { sets/clears PLAYERF_CONDUCTED flag }
  132. PLAYER_Quiet = (PLAYER_Base+9) ; { don't process time thru this }
  133. PLAYER_UserData = (PLAYER_Base+10);
  134. PLAYER_ID = (PLAYER_Base+11);
  135. PLAYER_ExtSync = (PLAYER_Base+14); { attempt/release to ext sync }
  136. PLAYER_ErrorCode = (PLAYER_Base+15); { error return value }
  137. {***************************************************************************}
  138. { Method types for messages sent via a Player's hook }
  139. PM_TICK = 0;
  140. PM_STATE = 1;
  141. PM_POSITION = 2;
  142. PM_SHUTTLE = 3;
  143. Type
  144. { used for PM_TICK, PM_POSITION and PM_SHUTTLE methods }
  145. ppmTime = ^tpmTime;
  146. tpmTime = record
  147. pmt_Method : ULONG; { PM_TICK, PM_POSITION, or PM_SHUTTLE }
  148. pmt_Time : ULONG;
  149. end;
  150. { used for the PM_STATE method }
  151. ppmState = ^tpmState;
  152. tpmState = record
  153. pms_Method : ULONG; { PM_STATE }
  154. pms_OldState: ULONG;
  155. end;
  156. {***************************************************************************}
  157. const
  158. { Possible lock types for LockRealTime() }
  159. RT_CONDUCTORS = 0; { conductor list }
  160. {***************************************************************************}
  161. { realtime.library error codes }
  162. RTE_NOMEMORY = 801; { memory allocation failed }
  163. RTE_NOCONDUCTOR = 802; { player needs a conductor }
  164. RTE_NOTIMER = 803; { timer (CIA) allocation failed }
  165. RTE_PLAYING = 804; { can't shuttle while playing }
  166. {***************************************************************************}
  167. { OpenLibrary("realtime.library",0) returns a pointer to this structure.
  168. * All fields are READ-ONLY.
  169. }
  170. Type
  171. pRealTimeBase = ^tRealTimeBase;
  172. tRealTimeBase = record
  173. rtb_LibNode : tLibrary;
  174. rtb_Reserved0 : Array[0..1] of Byte;
  175. rtb_Time, { current time }
  176. rtb_TimeFrac : ULONG; { fixed-point fraction part of time }
  177. rtb_Reserved1 : WORD;
  178. rtb_TickErr : smallint; { nanosecond error from ideal Tick }
  179. end; { length to real tick length }
  180. { Actual tick length is: 1/TICK_FREQ + rtb_TickErr/1e9 }
  181. const
  182. RealTime_TickErr_Min = -705;
  183. RealTime_TickErr_Max = 705;
  184. {*--- functions in V37 or higher (Release 2.04) ---*}
  185. VAR RealTimeBase : pRealTimeBase;
  186. const
  187. REALTIMENAME : PChar = 'realtime.library';
  188. FUNCTION CreatePlayerA(const tagList : pTagItem) : pPlayer;
  189. PROCEDURE DeletePlayer(player : pPlayer);
  190. FUNCTION ExternalSync(player : pPlayer; minTime : LONGINT; maxTime : LONGINT) : BOOLEAN;
  191. FUNCTION FindConductor(const name : pCHAR) : pConductor;
  192. FUNCTION GetPlayerAttrsA(const player : pPlayer;const tagList : pTagItem) : ULONG;
  193. FUNCTION LockRealTime(lockType : ULONG) : POINTER;
  194. FUNCTION NextConductor(const previousConductor : pConductor) : pConductor;
  195. FUNCTION SetConductorState(player : pPlayer; state : ULONG; time : LONGINT) : LONGINT;
  196. FUNCTION SetPlayerAttrsA(player : pPlayer;const tagList : pTagItem) : BOOLEAN;
  197. PROCEDURE UnlockRealTime(lock : POINTER);
  198. {You can remove this include and use a define instead}
  199. {$I useautoopenlib.inc}
  200. {$ifdef use_init_openlib}
  201. procedure InitREALTIMELibrary;
  202. {$endif use_init_openlib}
  203. {This is a variable that knows how the unit is compiled}
  204. var
  205. REALTIMEIsCompiledHow : longint;
  206. IMPLEMENTATION
  207. {$ifndef dont_use_openlib}
  208. uses msgbox;
  209. {$endif dont_use_openlib}
  210. FUNCTION CreatePlayerA(const tagList : pTagItem) : pPlayer;
  211. BEGIN
  212. ASM
  213. MOVE.L A6,-(A7)
  214. MOVEA.L tagList,A0
  215. MOVEA.L RealTimeBase,A6
  216. JSR -042(A6)
  217. MOVEA.L (A7)+,A6
  218. MOVE.L D0,@RESULT
  219. END;
  220. END;
  221. PROCEDURE DeletePlayer(player : pPlayer);
  222. BEGIN
  223. ASM
  224. MOVE.L A6,-(A7)
  225. MOVEA.L player,A0
  226. MOVEA.L RealTimeBase,A6
  227. JSR -048(A6)
  228. MOVEA.L (A7)+,A6
  229. END;
  230. END;
  231. FUNCTION ExternalSync(player : pPlayer; minTime : LONGINT; maxTime : LONGINT) : BOOLEAN;
  232. BEGIN
  233. ASM
  234. MOVE.L A6,-(A7)
  235. MOVEA.L player,A0
  236. MOVE.L minTime,D0
  237. MOVE.L maxTime,D1
  238. MOVEA.L RealTimeBase,A6
  239. JSR -066(A6)
  240. MOVEA.L (A7)+,A6
  241. TST.W D0
  242. BEQ.B @end
  243. MOVEQ #1,D0
  244. @end: MOVE.B D0,@RESULT
  245. END;
  246. END;
  247. FUNCTION FindConductor(const name : pCHAR) : pConductor;
  248. BEGIN
  249. ASM
  250. MOVE.L A6,-(A7)
  251. MOVEA.L name,A0
  252. MOVEA.L RealTimeBase,A6
  253. JSR -078(A6)
  254. MOVEA.L (A7)+,A6
  255. MOVE.L D0,@RESULT
  256. END;
  257. END;
  258. FUNCTION GetPlayerAttrsA(const player : pPlayer;const tagList : pTagItem) : ULONG;
  259. BEGIN
  260. ASM
  261. MOVE.L A6,-(A7)
  262. MOVEA.L player,A0
  263. MOVEA.L tagList,A1
  264. MOVEA.L RealTimeBase,A6
  265. JSR -084(A6)
  266. MOVEA.L (A7)+,A6
  267. MOVE.L D0,@RESULT
  268. END;
  269. END;
  270. FUNCTION LockRealTime(lockType : ULONG) : POINTER;
  271. BEGIN
  272. ASM
  273. MOVE.L A6,-(A7)
  274. MOVE.L lockType,D0
  275. MOVEA.L RealTimeBase,A6
  276. JSR -030(A6)
  277. MOVEA.L (A7)+,A6
  278. MOVE.L D0,@RESULT
  279. END;
  280. END;
  281. FUNCTION NextConductor(const previousConductor : pConductor) : pConductor;
  282. BEGIN
  283. ASM
  284. MOVE.L A6,-(A7)
  285. MOVEA.L previousConductor,A0
  286. MOVEA.L RealTimeBase,A6
  287. JSR -072(A6)
  288. MOVEA.L (A7)+,A6
  289. MOVE.L D0,@RESULT
  290. END;
  291. END;
  292. FUNCTION SetConductorState(player : pPlayer; state : ULONG; time : LONGINT) : LONGINT;
  293. BEGIN
  294. ASM
  295. MOVE.L A6,-(A7)
  296. MOVEA.L player,A0
  297. MOVE.L state,D0
  298. MOVE.L time,D1
  299. MOVEA.L RealTimeBase,A6
  300. JSR -060(A6)
  301. MOVEA.L (A7)+,A6
  302. MOVE.L D0,@RESULT
  303. END;
  304. END;
  305. FUNCTION SetPlayerAttrsA(player : pPlayer;const tagList : pTagItem) : BOOLEAN;
  306. BEGIN
  307. ASM
  308. MOVE.L A6,-(A7)
  309. MOVEA.L player,A0
  310. MOVEA.L tagList,A1
  311. MOVEA.L RealTimeBase,A6
  312. JSR -054(A6)
  313. MOVEA.L (A7)+,A6
  314. TST.W D0
  315. BEQ.B @end
  316. MOVEQ #1,D0
  317. @end: MOVE.B D0,@RESULT
  318. END;
  319. END;
  320. PROCEDURE UnlockRealTime(lock : POINTER);
  321. BEGIN
  322. ASM
  323. MOVE.L A6,-(A7)
  324. MOVEA.L lock,A0
  325. MOVEA.L RealTimeBase,A6
  326. JSR -036(A6)
  327. MOVEA.L (A7)+,A6
  328. END;
  329. END;
  330. const
  331. { Change VERSION and LIBVERSION to proper values }
  332. VERSION : string[2] = '0';
  333. LIBVERSION : longword = 0;
  334. {$ifdef use_init_openlib}
  335. {$Info Compiling initopening of realtime.library}
  336. {$Info don't forget to use InitREALTIMELibrary in the beginning of your program}
  337. var
  338. realtime_exit : Pointer;
  339. procedure CloserealtimeLibrary;
  340. begin
  341. ExitProc := realtime_exit;
  342. if RealTimeBase <> nil then begin
  343. CloseLibrary(pLibrary(RealTimeBase));
  344. RealTimeBase := nil;
  345. end;
  346. end;
  347. procedure InitREALTIMELibrary;
  348. begin
  349. RealTimeBase := nil;
  350. RealTimeBase := pRealTimeBase(OpenLibrary(REALTIMENAME,LIBVERSION));
  351. if RealTimeBase <> nil then begin
  352. realtime_exit := ExitProc;
  353. ExitProc := @CloserealtimeLibrary;
  354. end else begin
  355. MessageBox('FPC Pascal Error',
  356. 'Can''t open realtime.library version ' + VERSION + #10 +
  357. 'Deallocating resources and closing down',
  358. 'Oops');
  359. halt(20);
  360. end;
  361. end;
  362. begin
  363. REALTIMEIsCompiledHow := 2;
  364. {$endif use_init_openlib}
  365. {$ifdef use_auto_openlib}
  366. {$Info Compiling autoopening of realtime.library}
  367. var
  368. realtime_exit : Pointer;
  369. procedure CloserealtimeLibrary;
  370. begin
  371. ExitProc := realtime_exit;
  372. if RealTimeBase <> nil then begin
  373. CloseLibrary(pLibrary(RealTimeBase));
  374. RealTimeBase := nil;
  375. end;
  376. end;
  377. begin
  378. RealTimeBase := nil;
  379. RealTimeBase := pRealTimeBase(OpenLibrary(REALTIMENAME,LIBVERSION));
  380. if RealTimeBase <> nil then begin
  381. realtime_exit := ExitProc;
  382. ExitProc := @CloserealtimeLibrary;
  383. REALTIMEIsCompiledHow := 1;
  384. end else begin
  385. MessageBox('FPC Pascal Error',
  386. 'Can''t open realtime.library version ' + VERSION + #10 +
  387. 'Deallocating resources and closing down',
  388. 'Oops');
  389. halt(20);
  390. end;
  391. {$endif use_auto_openlib}
  392. {$ifdef dont_use_openlib}
  393. begin
  394. REALTIMEIsCompiledHow := 3;
  395. {$Warning No autoopening of realtime.library compiled}
  396. {$Warning Make sure you open realtime.library yourself}
  397. {$endif dont_use_openlib}
  398. END. (* UNIT REALTIME *)