| Nico Huber | 5e9b1b5 | 2016-10-08 22:09:33 +0200 | [diff] [blame] | 1 | -- |
| 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 Huber | aab715f | 2016-10-18 00:22:25 +0200 | [diff] [blame] | 6 | -- the Free Software Foundation; either version 2 of the License, or |
| 7 | -- (at your option) any later version. |
| Nico Huber | 5e9b1b5 | 2016-10-08 22:09:33 +0200 | [diff] [blame] | 8 | -- |
| 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 | |
| 15 | with HW; |
| 16 | with HW.Time.Timer; |
| 17 | |
| 18 | use type HW.Word64; |
| 19 | |
| 20 | package body HW.Time |
| 21 | with Refined_State => (State => (Timer.Timer_State, Timer.Abstract_Time)) |
| 22 | is |
| 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 Huber | a6a4909 | 2018-05-18 15:04:12 +0200 | [diff] [blame] | 37 | Hz : constant T := Timer.Hz; |
| Nico Huber | 5e9b1b5 | 2016-10-08 22:09:33 +0200 | [diff] [blame] | 38 | Current : constant T := Timer.Raw_Value_Max; |
| 39 | begin |
| Nico Huber | a6a4909 | 2018-05-18 15:04:12 +0200 | [diff] [blame] | 40 | return Current + (T (US) * Hz + 999_999) / 1_000_000; |
| Nico Huber | 5e9b1b5 | 2016-10-08 22:09:33 +0200 | [diff] [blame] | 41 | 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 Huber | a6a4909 | 2018-05-18 15:04:12 +0200 | [diff] [blame] | 47 | Hz : constant T := Timer.Hz; |
| Nico Huber | 5e9b1b5 | 2016-10-08 22:09:33 +0200 | [diff] [blame] | 48 | Current : constant T := Timer.Raw_Value_Max; |
| 49 | begin |
| Nico Huber | a6a4909 | 2018-05-18 15:04:12 +0200 | [diff] [blame] | 50 | return Current + (T (MS) * Hz + 999) / 1_000; |
| Nico Huber | 5e9b1b5 | 2016-10-08 22:09:33 +0200 | [diff] [blame] | 51 | 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 Huber | a6a4909 | 2018-05-18 15:04:12 +0200 | [diff] [blame] | 57 | Hz : constant T := Timer.Hz; |
| 58 | MHz : constant T := Hz / 1_000_000; |
| Nico Huber | 5e9b1b5 | 2016-10-08 22:09:33 +0200 | [diff] [blame] | 59 | Current : constant T := Timer.Raw_Value_Min; |
| 60 | begin |
| Nico Huber | 874113d | 2017-06-18 02:40:51 +0200 | [diff] [blame] | 61 | return Int64 (Current and (2 ** 63 - 1)) |
| 62 | / Int64 (if MHz = 0 then T'(1) else MHz); |
| Nico Huber | 5e9b1b5 | 2016-10-08 22:09:33 +0200 | [diff] [blame] | 63 | end Now_US; |
| 64 | |
| 65 | ---------------------------------------------------------------------------- |
| 66 | |
| 67 | procedure Delay_Until (Deadline : T) |
| 68 | with |
| 69 | Refined_Global => (Input => (Timer.Abstract_Time)) |
| 70 | is |
| Nico Huber | a6a4909 | 2018-05-18 15:04:12 +0200 | [diff] [blame] | 71 | Current : T; |
| Nico Huber | 5e9b1b5 | 2016-10-08 22:09:33 +0200 | [diff] [blame] | 72 | 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 | |
| 108 | end HW.Time; |
| 109 | |
| 110 | -- vim: set ts=8 sts=3 sw=3 et: |