blob: c1666a6ee958c661701ec06a1e60ac0f1736042c [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
15with HW;
16with HW.Time.Timer;
17
18use type HW.Word64;
19
20package body HW.Time
21 with Refined_State => (State => (Timer.Timer_State, Timer.Abstract_Time))
22is
23
24 function Now return T
25 with
26 Refined_Global => (Input => Timer.Abstract_Time)
27 is
28 Current : constant T := Timer.Raw_Value_Min;
29 begin
30 return Current;
31 end Now;
32
33 function US_From_Now (US : Natural) return T
34 with
35 Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State))
36 is
37 Current : constant T := Timer.Raw_Value_Max;
38 begin
39 return Current + (T (US) * Timer.Hz + 999_999) / 1_000_000;
40 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
46 Current : constant T := Timer.Raw_Value_Max;
47 begin
48 return Current + (T (MS) * Timer.Hz + 999) / 1_000;
49 end MS_From_Now;
50
51 function Now_US return Int64
52 with
53 Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State))
54 is
55 MHz : constant T := Timer.Hz / 1_000_000;
56 Current : constant T := Timer.Raw_Value_Min;
57 begin
58 return Int64 ((Current and (2 ** 63 - 1)) / (if MHz = 0 then 1 else MHz));
59 end Now_US;
60
61 ----------------------------------------------------------------------------
62
63 procedure Delay_Until (Deadline : T)
64 with
65 Refined_Global => (Input => (Timer.Abstract_Time))
66 is
67 Current: T;
68 begin
69 loop
70 Current := Timer.Raw_Value_Min;
71 exit when Current >= Deadline;
72 end loop;
73 end Delay_Until;
74
75 procedure U_Delay (US : Natural)
76 with
77 Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State))
78 is
79 Deadline : constant T := US_From_Now (US);
80 begin
81 Delay_Until (Deadline);
82 end U_Delay;
83
84 procedure M_Delay (MS : Natural)
85 with
86 Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State))
87 is
88 Deadline : constant T := MS_From_Now (MS);
89 begin
90 Delay_Until (Deadline);
91 end M_Delay;
92
93 ----------------------------------------------------------------------------
94
95 function Timed_Out (Deadline : T) return Boolean
96 with
97 Refined_Global => (Input => (Timer.Abstract_Time))
98 is
99 Current : constant T := Timer.Raw_Value_Min;
100 begin
101 return Current >= Deadline;
102 end Timed_Out;
103
104end HW.Time;
105
106-- vim: set ts=8 sts=3 sw=3 et: