blob: 3162de1daa1e9e129845f9459f2f2e23d4a9d9e2 [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
6-- the Free Software Foundation; version 2 of the License.
7--
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. See the
11-- GNU General Public License for more details.
12--
13
14with HW;
15with 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
36 Current : constant T := Timer.Raw_Value_Max;
37 begin
38 return Current + (T (US) * Timer.Hz + 999_999) / 1_000_000;
39 end US_From_Now;
40
41 function MS_From_Now (MS : Natural) return T
42 with
43 Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State))
44 is
45 Current : constant T := Timer.Raw_Value_Max;
46 begin
47 return Current + (T (MS) * Timer.Hz + 999) / 1_000;
48 end MS_From_Now;
49
50 function Now_US return Int64
51 with
52 Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State))
53 is
54 MHz : constant T := Timer.Hz / 1_000_000;
55 Current : constant T := Timer.Raw_Value_Min;
56 begin
57 return Int64 ((Current and (2 ** 63 - 1)) / (if MHz = 0 then 1 else MHz));
58 end Now_US;
59
60 ----------------------------------------------------------------------------
61
62 procedure Delay_Until (Deadline : T)
63 with
64 Refined_Global => (Input => (Timer.Abstract_Time))
65 is
66 Current: T;
67 begin
68 loop
69 Current := Timer.Raw_Value_Min;
70 exit when Current >= Deadline;
71 end loop;
72 end Delay_Until;
73
74 procedure U_Delay (US : Natural)
75 with
76 Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State))
77 is
78 Deadline : constant T := US_From_Now (US);
79 begin
80 Delay_Until (Deadline);
81 end U_Delay;
82
83 procedure M_Delay (MS : Natural)
84 with
85 Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State))
86 is
87 Deadline : constant T := MS_From_Now (MS);
88 begin
89 Delay_Until (Deadline);
90 end M_Delay;
91
92 ----------------------------------------------------------------------------
93
94 function Timed_Out (Deadline : T) return Boolean
95 with
96 Refined_Global => (Input => (Timer.Abstract_Time))
97 is
98 Current : constant T := Timer.Raw_Value_Min;
99 begin
100 return Current >= Deadline;
101 end Timed_Out;
102
103end HW.Time;
104
105-- vim: set ts=8 sts=3 sw=3 et: