blob: b81d343e8f2c3f569fd7a970a27ca0e98b7e0da6 [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
Nico Hubera6a49092018-05-18 15:04:12 +020037 Hz : constant T := Timer.Hz;
Nico Huber5e9b1b52016-10-08 22:09:33 +020038 Current : constant T := Timer.Raw_Value_Max;
39 begin
Nico Hubera6a49092018-05-18 15:04:12 +020040 return Current + (T (US) * Hz + 999_999) / 1_000_000;
Nico Huber5e9b1b52016-10-08 22:09:33 +020041 end US_From_Now;
42
43 function MS_From_Now (MS : Natural) return T
44 with
45 Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State))
46 is
Nico Hubera6a49092018-05-18 15:04:12 +020047 Hz : constant T := Timer.Hz;
Nico Huber5e9b1b52016-10-08 22:09:33 +020048 Current : constant T := Timer.Raw_Value_Max;
49 begin
Nico Hubera6a49092018-05-18 15:04:12 +020050 return Current + (T (MS) * Hz + 999) / 1_000;
Nico Huber5e9b1b52016-10-08 22:09:33 +020051 end MS_From_Now;
52
53 function Now_US return Int64
54 with
55 Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State))
56 is
Nico Hubera6a49092018-05-18 15:04:12 +020057 Hz : constant T := Timer.Hz;
58 MHz : constant T := Hz / 1_000_000;
Nico Huber5e9b1b52016-10-08 22:09:33 +020059 Current : constant T := Timer.Raw_Value_Min;
60 begin
Nico Huber874113d2017-06-18 02:40:51 +020061 return Int64 (Current and (2 ** 63 - 1))
62 / Int64 (if MHz = 0 then T'(1) else MHz);
Nico Huber5e9b1b52016-10-08 22:09:33 +020063 end Now_US;
64
65 ----------------------------------------------------------------------------
66
67 procedure Delay_Until (Deadline : T)
68 with
69 Refined_Global => (Input => (Timer.Abstract_Time))
70 is
Nico Hubera6a49092018-05-18 15:04:12 +020071 Current : T;
Nico Huber5e9b1b52016-10-08 22:09:33 +020072 begin
73 loop
74 Current := Timer.Raw_Value_Min;
75 exit when Current >= Deadline;
76 end loop;
77 end Delay_Until;
78
79 procedure U_Delay (US : Natural)
80 with
81 Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State))
82 is
83 Deadline : constant T := US_From_Now (US);
84 begin
85 Delay_Until (Deadline);
86 end U_Delay;
87
88 procedure M_Delay (MS : Natural)
89 with
90 Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State))
91 is
92 Deadline : constant T := MS_From_Now (MS);
93 begin
94 Delay_Until (Deadline);
95 end M_Delay;
96
97 ----------------------------------------------------------------------------
98
99 function Timed_Out (Deadline : T) return Boolean
100 with
101 Refined_Global => (Input => (Timer.Abstract_Time))
102 is
103 Current : constant T := Timer.Raw_Value_Min;
104 begin
105 return Current >= Deadline;
106 end Timed_Out;
107
108end HW.Time;
109
110-- vim: set ts=8 sts=3 sw=3 et: