| 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 |
| 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 |
| Nico Huber | 874113d | 2017-06-18 02:40:51 +0200 | [diff] [blame^] | 58 | return Int64 (Current and (2 ** 63 - 1)) |
| 59 | / Int64 (if MHz = 0 then T'(1) else MHz); |
| Nico Huber | 5e9b1b5 | 2016-10-08 22:09:33 +0200 | [diff] [blame] | 60 | end Now_US; |
| 61 | |
| 62 | ---------------------------------------------------------------------------- |
| 63 | |
| 64 | procedure Delay_Until (Deadline : T) |
| 65 | with |
| 66 | Refined_Global => (Input => (Timer.Abstract_Time)) |
| 67 | is |
| 68 | Current: T; |
| 69 | begin |
| 70 | loop |
| 71 | Current := Timer.Raw_Value_Min; |
| 72 | exit when Current >= Deadline; |
| 73 | end loop; |
| 74 | end Delay_Until; |
| 75 | |
| 76 | procedure U_Delay (US : Natural) |
| 77 | with |
| 78 | Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State)) |
| 79 | is |
| 80 | Deadline : constant T := US_From_Now (US); |
| 81 | begin |
| 82 | Delay_Until (Deadline); |
| 83 | end U_Delay; |
| 84 | |
| 85 | procedure M_Delay (MS : Natural) |
| 86 | with |
| 87 | Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State)) |
| 88 | is |
| 89 | Deadline : constant T := MS_From_Now (MS); |
| 90 | begin |
| 91 | Delay_Until (Deadline); |
| 92 | end M_Delay; |
| 93 | |
| 94 | ---------------------------------------------------------------------------- |
| 95 | |
| 96 | function Timed_Out (Deadline : T) return Boolean |
| 97 | with |
| 98 | Refined_Global => (Input => (Timer.Abstract_Time)) |
| 99 | is |
| 100 | Current : constant T := Timer.Raw_Value_Min; |
| 101 | begin |
| 102 | return Current >= Deadline; |
| 103 | end Timed_Out; |
| 104 | |
| 105 | end HW.Time; |
| 106 | |
| 107 | -- vim: set ts=8 sts=3 sw=3 et: |