blob: 42c63f6a3e517e78b601aeaed74f68e71741af22 [file] [log] [blame]
Nico Huber5e9b1b52016-10-08 22:09:33 +02001--
2-- Copyright (C) 2015-2016 secunet Security Networks AG
3--
4-- This program is free software; you can redistribute it and/or modify
5-- it under the terms of the GNU General Public License as published by
Nico Huberaab715f2016-10-18 00:22:25 +02006-- the Free Software Foundation; either version 2 of the License, or
7-- (at your option) any later version.
Nico Huber5e9b1b52016-10-08 22:09:33 +02008--
9-- This program is distributed in the hope that it will be useful,
10-- but WITHOUT ANY WARRANTY; without even the implied warranty of
11-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-- GNU General Public License for more details.
13--
14
Nico Huber5e9b1b52016-10-08 22:09:33 +020015with HW.Time.Timer;
16
17use type HW.Word64;
18
19package body HW.Time
20 with Refined_State => (State => (Timer.Timer_State, Timer.Abstract_Time))
21is
22
23 function Now return T
24 with
25 Refined_Global => (Input => Timer.Abstract_Time)
26 is
27 Current : constant T := Timer.Raw_Value_Min;
28 begin
29 return Current;
30 end Now;
31
32 function US_From_Now (US : Natural) return T
33 with
34 Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State))
35 is
Nico Hubera6a49092018-05-18 15:04:12 +020036 Hz : constant T := Timer.Hz;
Nico Huber5e9b1b52016-10-08 22:09:33 +020037 Current : constant T := Timer.Raw_Value_Max;
38 begin
Nico Hubera6a49092018-05-18 15:04:12 +020039 return Current + (T (US) * Hz + 999_999) / 1_000_000;
Nico Huber5e9b1b52016-10-08 22:09:33 +020040 end US_From_Now;
41
42 function MS_From_Now (MS : Natural) return T
43 with
44 Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State))
45 is
Nico Hubera6a49092018-05-18 15:04:12 +020046 Hz : constant T := Timer.Hz;
Nico Huber5e9b1b52016-10-08 22:09:33 +020047 Current : constant T := Timer.Raw_Value_Max;
48 begin
Nico Hubera6a49092018-05-18 15:04:12 +020049 return Current + (T (MS) * Hz + 999) / 1_000;
Nico Huber5e9b1b52016-10-08 22:09:33 +020050 end MS_From_Now;
51
52 function Now_US return Int64
53 with
54 Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State))
55 is
Nico Hubera6a49092018-05-18 15:04:12 +020056 Hz : constant T := Timer.Hz;
57 MHz : constant T := Hz / 1_000_000;
Nico Huber5e9b1b52016-10-08 22:09:33 +020058 Current : constant T := Timer.Raw_Value_Min;
59 begin
Nico Huber874113d2017-06-18 02:40:51 +020060 return Int64 (Current and (2 ** 63 - 1))
61 / Int64 (if MHz = 0 then T'(1) else MHz);
Nico Huber5e9b1b52016-10-08 22:09:33 +020062 end Now_US;
63
64 ----------------------------------------------------------------------------
65
66 procedure Delay_Until (Deadline : T)
67 with
68 Refined_Global => (Input => (Timer.Abstract_Time))
69 is
Nico Hubera6a49092018-05-18 15:04:12 +020070 Current : T;
Nico Huber5e9b1b52016-10-08 22:09:33 +020071 begin
72 loop
73 Current := Timer.Raw_Value_Min;
74 exit when Current >= Deadline;
75 end loop;
76 end Delay_Until;
77
78 procedure U_Delay (US : Natural)
79 with
80 Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State))
81 is
82 Deadline : constant T := US_From_Now (US);
83 begin
84 Delay_Until (Deadline);
85 end U_Delay;
86
87 procedure M_Delay (MS : Natural)
88 with
89 Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State))
90 is
91 Deadline : constant T := MS_From_Now (MS);
92 begin
93 Delay_Until (Deadline);
94 end M_Delay;
95
96 ----------------------------------------------------------------------------
97
98 function Timed_Out (Deadline : T) return Boolean
99 with
100 Refined_Global => (Input => (Timer.Abstract_Time))
101 is
102 Current : constant T := Timer.Raw_Value_Min;
103 begin
104 return Current >= Deadline;
105 end Timed_Out;
106
107end HW.Time;
108
109-- vim: set ts=8 sts=3 sw=3 et: