-- File: AMCUCool.mesa,  Last Edit: HGM  January 15, 1981  10:13 PM

DIRECTORY
  ProcessOps USING [TimerGrain],
  PupDefs USING [],
  PupStream USING [Tocks];

AMCUCool: PROGRAM EXPORTS PupDefs, PupStream =
  BEGIN

  TockConversionTroubles: PUBLIC ERROR = CODE;

  msPerTock: CARDINAL = ProcessOps.TimerGrain;

  SecondsToTocks: PUBLIC PROCEDURE [t: CARDINAL] RETURNS [PupStream.Tocks] =
    BEGIN
    IF t ~IN [1..(177777B/(1000/msPerTock))) THEN ERROR TockConversionTroubles;
    RETURN[[t*(1000/msPerTock)]];
    END;

  MsToTocks: PUBLIC PROCEDURE [t: CARDINAL] RETURNS [PupStream.Tocks] =
    BEGIN
    IF t = 0 THEN ERROR TockConversionTroubles;
    IF (t + msPerTock) > t THEN t ← t + (msPerTock - 1);
    -- roundup if won't overflow
    RETURN[[t/msPerTock]];
    END;

  END.