summaryrefslogtreecommitdiff
path: root/packages/extra/amunits/units/realtime.pas
blob: edc42cbf7ebd890d48c1e39142d3afe001680e6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
{
    This file is part of the Free Pascal run time library.

    A file in Amiga system run time library.
    Copyright (c) 1998-2003 by Nils Sjoholm
    member of the Amiga RTL development team.

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}

{
    History:

    Added functions and procedures with array of const.
    For use with fpc 1.0.7. They are in systemvartags.
    11 Nov 2002.

    Added the defines use_amiga_smartlink and
    use_auto_openlib. Implemented autoopening of the
    library.
    14 Jan 2003.

    Changed integer > smallint,
            cardinal > longword.
    Changed startcode for unit.
    09 Feb 2003.

    nils.sjoholm@mailbox.swipnet.se
}

{$I useamigasmartlink.inc}
{$ifdef use_amiga_smartlink}
   {$smartlink on}
{$endif use_amiga_smartlink}

UNIT realtime;

INTERFACE
USES exec, utility;

{***************************************************************************}

const
{ realtime.library's idea of time is based on a clock which emits a pulse
 * 1200 times a second (1.2kHz). All time values maintained by realtime.library
 * are based on this number. For example, the field RealTimeBase->rtb_Time
 * expresses an amount of time equivalent to (RealTimeBase->rtb_Time/TICK_FREQ)
 * seconds.
 }
 TICK_FREQ = 1200;


{***************************************************************************}


{ Each Conductor represents a group of applications which wish to remain
 * synchronized together.
 *
 * This structure must only be allocated by realtime.library and is
 * READ-ONLY!
 }
Type
 pConductor = ^tConductor;
 tConductor = record
    cdt_Link        : tNode;
    cdt_Reserved0   : WORD;
    cdt_Players     : tMinList;          { this conductor's players      }
    cdt_ClockTime,                       { current time of this sequence }
    cdt_StartTime,                       { start time of this sequence   }
    cdt_ExternalTime,                    { time from external unit       }
    cdt_MaxExternalTime,                 { upper limit on sync'd time    }
    cdt_Metronome   : ULONG;             { MetricTime highest pri node   }
    cdt_Reserved1   : WORD;
    cdt_Flags       : WORD;              { conductor flags               }
    cdt_State       : Byte;              { playing or stopped            }
 end;

const
{ Flag bits for Conductor.cdt_Flags }
 CONDUCTF_EXTERNAL = 1;   { clock is externally driven }
 CONDUCTF_GOTTICK  = 2;   { received 1st external tick }
 CONDUCTF_METROSET = 4;   { cdt_Metronome filled in    }
 CONDUCTF_PRIVATE  = 8;   { conductor is private       }

 CONDUCTB_EXTERNAL = 0;
 CONDUCTB_GOTTICK  = 1;
 CONDUCTB_METROSET = 2;
 CONDUCTB_PRIVATE  = 3;

{ constants for Conductor.cdt_State and SetConductorState() }
 CONDSTATE_STOPPED    = 0;   { clock is stopped              }
 CONDSTATE_PAUSED     = 1;   { clock is paused               }
 CONDSTATE_LOCATE     = 2;   { go to 'running' when ready    }
 CONDSTATE_RUNNING    = 3;   { run clock NOW                 }

{ These do not actually exist as Conductor states, but are used as additional
 * arguments to SetConductorState()
 }
 CONDSTATE_METRIC     = -1;   { ask high node to locate       }
 CONDSTATE_SHUTTLE    = -2;   { time changing but not running }
 CONDSTATE_LOCATE_SET = -3;   { maestro done locating         }


{***************************************************************************}


{ The Player is the connection between a Conductor and an application.
 *
 * This structure must only be allocated by realtime.library and is
 * READ-ONLY!
 }
Type
 pPlayer = ^tPlayer;
 tPlayer = record
    pl_Link             : tNode;
    pl_Reserved0,
    pl_Reserved1        : Shortint;
    pl_Hook             : pHook;         { player's hook function       }
    pl_Source           : pConductor;    { pointer to parent context    }
    pl_Task             : pTask;         { task to signal for alarm     }
    pl_MetricTime       : Longint;       { current time in app's metric }
    pl_AlarmTime        : Longint;       { time to wake up              }
    pl_UserData         : Pointer;       { for application use  }
    pl_PlayerID         : WORD;          { for application use  }
    pl_Flags            : WORD;          { general Player flags         }
 end;

const
{ Flag bits for Player.pl_Flags }
 PLAYERF_READY     = 1;   { player is ready to go!        }
 PLAYERF_ALARMSET  = 2;   { alarm is set                  }
 PLAYERF_QUIET     = 3;   { a dummy player, used for sync }
 PLAYERF_CONDUCTED = 8;   { give me metered time          }
 PLAYERF_EXTSYNC   = 16;   { granted external sync         }

 PLAYERB_READY     = 0;
 PLAYERB_ALARMSET  = 1;
 PLAYERB_QUIET     = 2;
 PLAYERB_CONDUCTED = 3;
 PLAYERB_EXTSYNC   = 4;


{***************************************************************************}


{ Tags for CreatePlayer(), SetPlayerAttrs(), and GetPlayerAttrs() }
 PLAYER_Base         = (TAG_USER+64)   ;
 PLAYER_Hook         = (PLAYER_Base+1) ;  { set address of hook function }
 PLAYER_Name         = (PLAYER_Base+2) ;  { name of player       }
 PLAYER_Priority     = (PLAYER_Base+3) ;  { priority of player           }
 PLAYER_Conductor    = (PLAYER_Base+4) ;  { set conductor for player     }
 PLAYER_Ready        = (PLAYER_Base+5) ;  { the "ready" flag             }
 PLAYER_AlarmTime    = (PLAYER_Base+12);  { alarm time (sets PLAYERF_ALARMSET) }
 PLAYER_Alarm        = (PLAYER_Base+13);  { sets/clears PLAYERF_ALARMSET flag  }
 PLAYER_AlarmSigTask = (PLAYER_Base+6) ;  { task to signal for alarm/notify    }
 PLAYER_AlarmSigBit  = (PLAYER_Base+8) ;  { signal bit for alarm (or -1) }
 PLAYER_Conducted    = (PLAYER_Base+7) ;  { sets/clears PLAYERF_CONDUCTED flag   }
 PLAYER_Quiet        = (PLAYER_Base+9) ;  { don't process time thru this }
 PLAYER_UserData     = (PLAYER_Base+10);
 PLAYER_ID           = (PLAYER_Base+11);
 PLAYER_ExtSync      = (PLAYER_Base+14);  { attempt/release to ext sync  }
 PLAYER_ErrorCode    = (PLAYER_Base+15);  { error return value           }


{***************************************************************************}


{ Method types for messages sent via a Player's hook }
 PM_TICK     = 0;
 PM_STATE    = 1;
 PM_POSITION = 2;
 PM_SHUTTLE  = 3;

Type
{ used for PM_TICK, PM_POSITION and PM_SHUTTLE methods }
 ppmTime = ^tpmTime;
 tpmTime = record
    pmt_Method  : ULONG;        { PM_TICK, PM_POSITION, or PM_SHUTTLE }
    pmt_Time    : ULONG;
 end;

{ used for the PM_STATE method }
 ppmState = ^tpmState;
 tpmState = record
    pms_Method  : ULONG;        { PM_STATE }
    pms_OldState: ULONG;
 end;


{***************************************************************************}

const
{ Possible lock types for LockRealTime() }
 RT_CONDUCTORS = 0;   { conductor list }


{***************************************************************************}


{ realtime.library error codes }
 RTE_NOMEMORY    = 801;   { memory allocation failed      }
 RTE_NOCONDUCTOR = 802;   { player needs a conductor      }
 RTE_NOTIMER     = 803;   { timer (CIA) allocation failed }
 RTE_PLAYING     = 804;   { can't shuttle while playing   }


{***************************************************************************}


{ OpenLibrary("realtime.library",0) returns a pointer to this structure.
 * All fields are READ-ONLY.
 }
Type
 pRealTimeBase = ^tRealTimeBase;
 tRealTimeBase = record
    rtb_LibNode     : tLibrary;
    rtb_Reserved0   : Array[0..1] of Byte;

    rtb_Time,                      { current time                         }
    rtb_TimeFrac    : ULONG;       { fixed-point fraction part of time    }
    rtb_Reserved1   : WORD;
    rtb_TickErr     : smallint;     { nanosecond error from ideal Tick     }
 end;                              { length to real tick length           }

{ Actual tick length is: 1/TICK_FREQ + rtb_TickErr/1e9 }

const
 RealTime_TickErr_Min = -705;
 RealTime_TickErr_Max =  705;

{*--- functions in V37 or higher (Release 2.04) ---*}

VAR RealTimeBase : pRealTimeBase;

const
    REALTIMENAME : PChar = 'realtime.library';

FUNCTION CreatePlayerA(const tagList : pTagItem) : pPlayer;
PROCEDURE DeletePlayer(player : pPlayer);
FUNCTION ExternalSync(player : pPlayer; minTime : LONGINT; maxTime : LONGINT) : BOOLEAN;
FUNCTION FindConductor(const name : pCHAR) : pConductor;
FUNCTION GetPlayerAttrsA(const player : pPlayer;const tagList : pTagItem) : ULONG;
FUNCTION LockRealTime(lockType : ULONG) : POINTER;
FUNCTION NextConductor(const previousConductor : pConductor) : pConductor;
FUNCTION SetConductorState(player : pPlayer; state : ULONG; time : LONGINT) : LONGINT;
FUNCTION SetPlayerAttrsA(player : pPlayer;const tagList : pTagItem) : BOOLEAN;
PROCEDURE UnlockRealTime(lock : POINTER);

{You can remove this include and use a define instead}
{$I useautoopenlib.inc}
{$ifdef use_init_openlib}
procedure InitREALTIMELibrary;
{$endif use_init_openlib}

{This is a variable that knows how the unit is compiled}
var
    REALTIMEIsCompiledHow : longint;

IMPLEMENTATION

{$ifndef dont_use_openlib}
uses msgbox;
{$endif dont_use_openlib}

FUNCTION CreatePlayerA(const tagList : pTagItem) : pPlayer;
BEGIN
  ASM
    MOVE.L  A6,-(A7)
    MOVEA.L tagList,A0
    MOVEA.L RealTimeBase,A6
    JSR -042(A6)
    MOVEA.L (A7)+,A6
    MOVE.L  D0,@RESULT
  END;
END;

PROCEDURE DeletePlayer(player : pPlayer);
BEGIN
  ASM
    MOVE.L  A6,-(A7)
    MOVEA.L player,A0
    MOVEA.L RealTimeBase,A6
    JSR -048(A6)
    MOVEA.L (A7)+,A6
  END;
END;

FUNCTION ExternalSync(player : pPlayer; minTime : LONGINT; maxTime : LONGINT) : BOOLEAN;
BEGIN
  ASM
    MOVE.L  A6,-(A7)
    MOVEA.L player,A0
    MOVE.L  minTime,D0
    MOVE.L  maxTime,D1
    MOVEA.L RealTimeBase,A6
    JSR -066(A6)
    MOVEA.L (A7)+,A6
    TST.W   D0
    BEQ.B   @end
    MOVEQ   #1,D0
  @end: MOVE.B  D0,@RESULT
  END;
END;

FUNCTION FindConductor(const name : pCHAR) : pConductor;
BEGIN
  ASM
    MOVE.L  A6,-(A7)
    MOVEA.L name,A0
    MOVEA.L RealTimeBase,A6
    JSR -078(A6)
    MOVEA.L (A7)+,A6
    MOVE.L  D0,@RESULT
  END;
END;

FUNCTION GetPlayerAttrsA(const player : pPlayer;const tagList : pTagItem) : ULONG;
BEGIN
  ASM
    MOVE.L  A6,-(A7)
    MOVEA.L player,A0
    MOVEA.L tagList,A1
    MOVEA.L RealTimeBase,A6
    JSR -084(A6)
    MOVEA.L (A7)+,A6
    MOVE.L  D0,@RESULT
  END;
END;

FUNCTION LockRealTime(lockType : ULONG) : POINTER;
BEGIN
  ASM
    MOVE.L  A6,-(A7)
    MOVE.L  lockType,D0
    MOVEA.L RealTimeBase,A6
    JSR -030(A6)
    MOVEA.L (A7)+,A6
    MOVE.L  D0,@RESULT
  END;
END;

FUNCTION NextConductor(const previousConductor : pConductor) : pConductor;
BEGIN
  ASM
    MOVE.L  A6,-(A7)
    MOVEA.L previousConductor,A0
    MOVEA.L RealTimeBase,A6
    JSR -072(A6)
    MOVEA.L (A7)+,A6
    MOVE.L  D0,@RESULT
  END;
END;

FUNCTION SetConductorState(player : pPlayer; state : ULONG; time : LONGINT) : LONGINT;
BEGIN
  ASM
    MOVE.L  A6,-(A7)
    MOVEA.L player,A0
    MOVE.L  state,D0
    MOVE.L  time,D1
    MOVEA.L RealTimeBase,A6
    JSR -060(A6)
    MOVEA.L (A7)+,A6
    MOVE.L  D0,@RESULT
  END;
END;

FUNCTION SetPlayerAttrsA(player : pPlayer;const tagList : pTagItem) : BOOLEAN;
BEGIN
  ASM
    MOVE.L  A6,-(A7)
    MOVEA.L player,A0
    MOVEA.L tagList,A1
    MOVEA.L RealTimeBase,A6
    JSR -054(A6)
    MOVEA.L (A7)+,A6
    TST.W   D0
    BEQ.B   @end
    MOVEQ   #1,D0
  @end: MOVE.B  D0,@RESULT
  END;
END;

PROCEDURE UnlockRealTime(lock : POINTER);
BEGIN
  ASM
    MOVE.L  A6,-(A7)
    MOVEA.L lock,A0
    MOVEA.L RealTimeBase,A6
    JSR -036(A6)
    MOVEA.L (A7)+,A6
  END;
END;

const
    { Change VERSION and LIBVERSION to proper values }

    VERSION : string[2] = '0';
    LIBVERSION : longword = 0;

{$ifdef use_init_openlib}
  {$Info Compiling initopening of realtime.library}
  {$Info don't forget to use InitREALTIMELibrary in the beginning of your program}

var
    realtime_exit : Pointer;

procedure CloserealtimeLibrary;
begin
    ExitProc := realtime_exit;
    if RealTimeBase <> nil then begin
        CloseLibrary(pLibrary(RealTimeBase));
        RealTimeBase := nil;
    end;
end;

procedure InitREALTIMELibrary;
begin
    RealTimeBase := nil;
    RealTimeBase := pRealTimeBase(OpenLibrary(REALTIMENAME,LIBVERSION));
    if RealTimeBase <> nil then begin
        realtime_exit := ExitProc;
        ExitProc := @CloserealtimeLibrary;
    end else begin
        MessageBox('FPC Pascal Error',
        'Can''t open realtime.library version ' + VERSION + #10 +
        'Deallocating resources and closing down',
        'Oops');
        halt(20);
    end;
end;

begin
    REALTIMEIsCompiledHow := 2;
{$endif use_init_openlib}

{$ifdef use_auto_openlib}
  {$Info Compiling autoopening of realtime.library}

var
    realtime_exit : Pointer;

procedure CloserealtimeLibrary;
begin
    ExitProc := realtime_exit;
    if RealTimeBase <> nil then begin
        CloseLibrary(pLibrary(RealTimeBase));
        RealTimeBase := nil;
    end;
end;

begin
    RealTimeBase := nil;
    RealTimeBase := pRealTimeBase(OpenLibrary(REALTIMENAME,LIBVERSION));
    if RealTimeBase <> nil then begin
        realtime_exit := ExitProc;
        ExitProc := @CloserealtimeLibrary;
        REALTIMEIsCompiledHow := 1;
    end else begin
        MessageBox('FPC Pascal Error',
        'Can''t open realtime.library version ' + VERSION + #10 +
        'Deallocating resources and closing down',
        'Oops');
        halt(20);
    end;

{$endif use_auto_openlib}

{$ifdef dont_use_openlib}
begin
    REALTIMEIsCompiledHow := 3;
   {$Warning No autoopening of realtime.library compiled}
   {$Warning Make sure you open realtime.library yourself}
{$endif dont_use_openlib}


END. (* UNIT REALTIME *)