-- Intime.mesa
-- Maureen Stone June 3, 1982 10:51 am
-- reduced size of DeltaDeltaTime to make room for penPosition ActionKind
-- Dan Swinehart April 14, 1982 11:31 am --

-- Paul Rovner July 20, 1983 1:27 pm --
DIRECTORY
Basics USING [LowHalf, HighHalf, DIVMOD],
Process USING [Milliseconds, Ticks];

Intime: DEFINITIONS IMPORTS Basics =
BEGIN OPEN Basics;

MsTicks: TYPE = Process.Milliseconds; -- expressed in milliseconds --
MSTicks: TYPE = LONG CARDINAL; -- long milliseconds --

DeltaTicks: TYPE = Process.Ticks;
-- grain of resolution for Increek events --
DeltaTime: TYPE = DeltaTicks [0..256);
-- expresses time differences in tick units --
maxDeltaTime: DeltaTime = LAST[DeltaTime];
-- fits in a short event value cell --
msPerDeltaTick: MsTicks;
deltaTicksPerSecond: DeltaTicks;

DeltaDeltaTime: TYPE = DeltaTime [0..16); -- must fit in Action first word --
maxDeltaDeltaTime: DeltaDeltaTime = LAST[DeltaDeltaTime];

Overlap: TYPE = {loShort, hiShort};
EventTime: TYPE = PRIVATE MACHINE DEPENDENT RECORD -- what client sees
-- milliseconds since 1901
[
SELECT OVERLAID Overlap FROM
loShort => [lo: MsTicks, hi: LONG CARDINAL],
hiShort => [lower: MSTicks, higher: CARDINAL],
ENDCASE];

-- Procedures --

ReadEventTime: PROCEDURE RETURNS [EventTime];
AdjustEventTime: PROC[initialize: BOOLEANFALSE];
EventTimeDifference: PROCEDURE [t1, t2: LONG POINTER TO READONLY EventTime]
RETURNS [MsTicks] = INLINE {
RETURN[IF t1.hi = t2.hi THEN t1.lo - t2.lo ELSE BigDifference[t1, t2]]; };

IsLaterTime: PROCEDURE [t1, t2: EventTime] RETURNS [BOOLEAN] = INLINE
BEGIN RETURN[IF t1.hi = t2.hi THEN t1.lo > t2.lo ELSE t1.hi > t2.hi]; END;

AddDeltaTimeToEventTime: PROCEDURE [eT: EventTime, dT: DeltaTime]
RETURNS [rT: EventTime] = INLINE {
lo: MSTicks ← LONG[eT.lo] + (dT*msPerDeltaTick);
RETURN[[loShort[hi: eT.hi + HighHalf[lo], lo: LowHalf[lo]]]];
};

SubtractMsTicksFromEventTime: PROCEDURE [
eT: LONG POINTER TO EventTime, ticks: MsTicks] = INLINE {
IF ticks > eT.lower THEN eT.higher ← eT.higher - 1;
eT.lower ← eT.lower - ticks;
};

MsTicksToDeltaTime: PROCEDURE [s: MsTicks]
RETURNS [dT: DeltaTicks, rem: MsTicks] = INLINE {
[dT, rem] ← DIVMOD[s, msPerDeltaTick]; };

BigDifference: PRIVATE PROCEDURE [t1, t2: LONG POINTER TO READONLY EventTime]
RETURNS [MsTicks];
END.

-- DCS, July 2, 1980 2:39 PM, birth, extracted from Increeks
-- July 2, 1980 2:56 PM, more
-- July 17, 1980 3:17 PM, remove AddTimeToPosTime, involves Positions
-- July 21, 1980 8:16 AM, add StoredTimeToEventTime
-- August 7, 1980 9:35 AM, change EventTimeDifference to StoredEvent...ce, add EventTimeDiff..ce
-- August 12, 1980 12:37 AM, change time representation again, all milliseconds
-- August 12, 1980 4:23 PM, add SubtractMsTicksFromEventTime
-- August 19, 1980 4:45 PM, difference operates on READONLYs
-- September 9, 1980 2:57 PM, long pointers
-- September 12, 1980 5:47 AM, modify DeltaTime a little
-- October 2, 1980 3:36 PM, make EventTime internals PRIVATE
-- 29-Jan-81 13:49:38, narrow DeltaDeltaTime to widen Action kind
--