|
24 | 24 | * Included Files |
25 | 25 | ****************************************************************************/ |
26 | 26 |
|
27 | | -#include <stdint.h> |
| 27 | +#include <errno.h> |
28 | 28 | #include <stdbool.h> |
| 29 | +#include <stdint.h> |
29 | 30 | #include <windows.h> |
30 | 31 |
|
| 32 | +#include "sim_internal.h" |
| 33 | + |
31 | 34 | /**************************************************************************** |
32 | 35 | * Pre-processor Definitions |
33 | 36 | ****************************************************************************/ |
34 | 37 |
|
35 | 38 | #define POW10_9 (1000000000ull) |
36 | 39 |
|
| 40 | +/**************************************************************************** |
| 41 | + * Private Data |
| 42 | + ****************************************************************************/ |
| 43 | + |
| 44 | +/* Ratio of simulated time to real time in percent. 100 means real-time |
| 45 | + * (default). Values > 100 speed up simulated time; values < 100 slow it |
| 46 | + * down. Overridable at runtime via --sim-rt-ratio=<percent>. |
| 47 | + */ |
| 48 | + |
| 49 | +static int g_time_ratio = CONFIG_SIM_WALLTIME_RATIO; |
| 50 | + |
37 | 51 | /* Number of 100ns-seconds between the beginning of the Windows epoch |
38 | 52 | * (Jan. 1, 1601) and the Unix epoch (Jan. 1, 1970) |
39 | 53 | */ |
@@ -89,7 +103,9 @@ uint64_t host_gettime(bool rtc) |
89 | 103 | start = current; |
90 | 104 | } |
91 | 105 |
|
92 | | - return current - start; |
| 106 | + /* Apply time ratio: simulated_time = real_elapsed * ratio / 100 */ |
| 107 | + |
| 108 | + return ((current - start) * g_time_ratio) / 100; |
93 | 109 | } |
94 | 110 |
|
95 | 111 | /**************************************************************************** |
@@ -125,9 +141,33 @@ void host_sleepuntil(uint64_t nsec) |
125 | 141 | uint64_t now; |
126 | 142 |
|
127 | 143 | now = host_gettime(false); |
128 | | - if (nsec > now) |
| 144 | + if (nsec > now + 1000) |
| 145 | + { |
| 146 | + /* nsec is in simulated time; convert back to real duration to sleep */ |
| 147 | + |
| 148 | + host_sleep((((nsec - now) * 100) / g_time_ratio)); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +/**************************************************************************** |
| 153 | + * Name: host_set_timeratio |
| 154 | + * |
| 155 | + * Description: |
| 156 | + * Set the ratio of simulated time to real time in percent. 100 (default) |
| 157 | + * means simulated time advances at the same rate as real time. Values |
| 158 | + * greater than 100 speed up simulated time; values less than 100 slow it |
| 159 | + * down. |
| 160 | + * |
| 161 | + * Input Parameters: |
| 162 | + * ratio - The new time ratio in percent (must be > 0) |
| 163 | + * |
| 164 | + ****************************************************************************/ |
| 165 | + |
| 166 | +void host_set_timeratio(int ratio) |
| 167 | +{ |
| 168 | + if (ratio > 0) |
129 | 169 | { |
130 | | - host_sleep(nsec - now); |
| 170 | + g_time_ratio = ratio; |
131 | 171 | } |
132 | 172 | } |
133 | 173 |
|
|
0 commit comments