| 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 | |
| Nico Huber | 5e9b1b5 | 2016-10-08 22:09:33 +0200 | [diff] [blame] | 15 | with HW.Time.Timer; |
| 16 | |
| 17 | use type HW.Word64; |
| 18 | |
| 19 | package body HW.Time |
| 20 | with Refined_State => (State => (Timer.Timer_State, Timer.Abstract_Time)) |
| 21 | is |
| 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 |
| Nico Huber | a6a4909 | 2018-05-18 15:04:12 +0200 | [diff] [blame] | 36 | Hz : constant T := Timer.Hz; |
| Nico Huber | 5e9b1b5 | 2016-10-08 22:09:33 +0200 | [diff] [blame] | 37 | Current : constant T := Timer.Raw_Value_Max; |
| 38 | begin |
| Nico Huber | a6a4909 | 2018-05-18 15:04:12 +0200 | [diff] [blame] | 39 | return Current + (T (US) * Hz + 999_999) / 1_000_000; |
| Nico Huber | 5e9b1b5 | 2016-10-08 22:09:33 +0200 | [diff] [blame] | 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 |
| Nico Huber | a6a4909 | 2018-05-18 15:04:12 +0200 | [diff] [blame] | 46 | Hz : constant T := Timer.Hz; |
| Nico Huber | 5e9b1b5 | 2016-10-08 22:09:33 +0200 | [diff] [blame] | 47 | Current : constant T := Timer.Raw_Value_Max; |
| 48 | begin |
| Nico Huber | a6a4909 | 2018-05-18 15:04:12 +0200 | [diff] [blame] | 49 | return Current + (T (MS) * Hz + 999) / 1_000; |
| Nico Huber | 5e9b1b5 | 2016-10-08 22:09:33 +0200 | [diff] [blame] | 50 | end MS_From_Now; |
| 51 | |
| 52 | function Now_US return Int64 |
| 53 | with |
| 54 | Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State)) |
| 55 | is |
| Nico Huber | a6a4909 | 2018-05-18 15:04:12 +0200 | [diff] [blame] | 56 | Hz : constant T := Timer.Hz; |
| 57 | MHz : constant T := Hz / 1_000_000; |
| Nico Huber | 5e9b1b5 | 2016-10-08 22:09:33 +0200 | [diff] [blame] | 58 | Current : constant T := Timer.Raw_Value_Min; |
| 59 | begin |
| Nico Huber | 874113d | 2017-06-18 02:40:51 +0200 | [diff] [blame] | 60 | return Int64 (Current and (2 ** 63 - 1)) |
| 61 | / Int64 (if MHz = 0 then T'(1) else MHz); |
| Nico Huber | 5e9b1b5 | 2016-10-08 22:09:33 +0200 | [diff] [blame] | 62 | end Now_US; |
| 63 | |
| 64 | ---------------------------------------------------------------------------- |
| 65 | |
| 66 | procedure Delay_Until (Deadline : T) |
| 67 | with |
| 68 | Refined_Global => (Input => (Timer.Abstract_Time)) |
| 69 | is |
| Nico Huber | a6a4909 | 2018-05-18 15:04:12 +0200 | [diff] [blame] | 70 | Current : T; |
| Nico Huber | 5e9b1b5 | 2016-10-08 22:09:33 +0200 | [diff] [blame] | 71 | begin |
| 72 | loop |
| 73 | Current := Timer.Raw_Value_Min; |
| 74 | exit when Current >= Deadline; |
| 75 | end loop; |
| 76 | end Delay_Until; |
| 77 | |
| 78 | procedure U_Delay (US : Natural) |
| 79 | with |
| 80 | Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State)) |
| 81 | is |
| 82 | Deadline : constant T := US_From_Now (US); |
| 83 | begin |
| 84 | Delay_Until (Deadline); |
| 85 | end U_Delay; |
| 86 | |
| 87 | procedure M_Delay (MS : Natural) |
| 88 | with |
| 89 | Refined_Global => (Input => (Timer.Abstract_Time, Timer.Timer_State)) |
| 90 | is |
| 91 | Deadline : constant T := MS_From_Now (MS); |
| 92 | begin |
| 93 | Delay_Until (Deadline); |
| 94 | end M_Delay; |
| 95 | |
| 96 | ---------------------------------------------------------------------------- |
| 97 | |
| 98 | function Timed_Out (Deadline : T) return Boolean |
| 99 | with |
| 100 | Refined_Global => (Input => (Timer.Abstract_Time)) |
| 101 | is |
| 102 | Current : constant T := Timer.Raw_Value_Min; |
| 103 | begin |
| 104 | return Current >= Deadline; |
| 105 | end Timed_Out; |
| 106 | |
| 107 | end HW.Time; |
| 108 | |
| 109 | -- vim: set ts=8 sts=3 sw=3 et: |