1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use std::fmt;
use std::ops::{Add, AddAssign, Sub, SubAssign};

/// Like [`std::time::Duration`], but in AVR cycles; somewhat approximate¹.
///
/// ¹ <https://github.com/buserror/simavr/blob/b3ea4f93e18fa5059f76060ff718dc544ca48009/simavr/sim/sim_core.c#L621>
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AvrDuration {
    clock_frequency: u32,
    cycles: u64,
}

impl AvrDuration {
    /// Creates a new duration for given clock frequency and number of cycles:
    ///
    /// ```
    /// # use avr_simulator::AvrDuration;
    /// #
    /// AvrDuration::new(
    ///     16_000_000, /* 16 MHz */
    ///     8_000_000, /* 8 million clock cycles (=500ms here) */
    /// );
    /// ```
    ///
    /// If you're using AvrTester, you might find `AvrDurationExt` more
    /// convenient than this constructor.
    pub const fn new(clock_frequency: u32, cycles: u64) -> Self {
        Self {
            clock_frequency,
            cycles,
        }
    }

    /// Returns a new duration with increased number of cycles.
    ///
    /// # Examples
    ///
    /// ```
    /// # use avr_simulator::AvrDuration;
    /// #
    /// let tt = AvrDuration::new(16_000_000, 8_000_000);
    ///
    /// assert_eq!(500, tt.as_millis());
    /// assert_eq!(750, tt.add_cycles(4_000_000).as_millis());
    /// ```
    pub const fn add_cycles(mut self, n: u64) -> Self {
        self.cycles += n;
        self
    }

    /// Returns a new duration with increased number of microseconds.
    ///
    /// # Examples
    ///
    /// ```
    /// # use avr_simulator::AvrDuration;
    /// #
    /// let tt = AvrDuration::new(16_000_000, 8_000_000);
    ///
    /// assert_eq!(500, tt.as_millis());
    /// assert_eq!(501, tt.add_micros(1_000).as_millis());
    /// ```
    pub const fn add_micros(self, n: u64) -> Self {
        self.add_cycles(n * (self.clock_frequency as u64 / 1_000_000))
    }

    /// Returns a new duration with increased number of milliseconds.
    ///
    /// # Examples
    ///
    /// ```
    /// # use avr_simulator::AvrDuration;
    /// #
    /// let tt = AvrDuration::new(16_000_000, 8_000_000);
    ///
    /// assert_eq!(500, tt.as_millis());
    /// assert_eq!(515, tt.add_millis(15).as_millis());
    /// ```
    pub const fn add_millis(self, millis: u64) -> Self {
        self.add_cycles(millis * (self.clock_frequency as u64) / 1_000)
    }

    /// Returns a new duration with increased number of seconds.
    ///
    /// # Examples
    ///
    /// ```
    /// # use avr_simulator::AvrDuration;
    /// #
    /// let tt = AvrDuration::new(16_000_000, 8_000_000);
    ///
    /// assert_eq!(500, tt.as_millis());
    /// assert_eq!(2500, tt.add_secs(2).as_millis());
    /// ```
    pub const fn add_secs(self, secs: u64) -> Self {
        self.add_cycles(secs * (self.clock_frequency as u64))
    }

    /// Returns a new duration with specified number of cycles.
    ///
    /// # Examples
    ///
    /// ```
    /// # use avr_simulator::AvrDuration;
    /// #
    /// let tt = AvrDuration::new(16_000_000, 8_000_000);
    ///
    /// assert_eq!(250, tt.with_cycles(4_000_000).as_millis());
    /// ```
    pub const fn with_cycles(mut self, n: u64) -> Self {
        self.cycles = n;
        self
    }

    /// Returns a new duration with specified number of microseconds.
    ///
    /// # Examples
    ///
    /// ```
    /// # use avr_simulator::AvrDuration;
    /// #
    /// let tt = AvrDuration::new(16_000_000, 8_000_000);
    ///
    /// assert_eq!(1, tt.with_micros(1_000).as_millis());
    /// ```
    pub const fn with_micros(self, n: u64) -> Self {
        self.with_cycles(0).add_micros(n)
    }

    /// Returns a new duration with specified number of milliseconds.
    ///
    /// # Examples
    ///
    /// ```
    /// # use avr_simulator::AvrDuration;
    /// #
    /// let tt = AvrDuration::new(16_000_000, 8_000_000);
    ///
    /// assert_eq!(15, tt.with_millis(15).as_millis());
    /// ```
    pub const fn with_millis(self, n: u64) -> Self {
        self.with_cycles(0).add_millis(n)
    }

    /// Returns a new duration with specified number of seconds.
    ///
    /// # Examples
    ///
    /// ```
    /// # use avr_simulator::AvrDuration;
    /// #
    /// let tt = AvrDuration::new(16_000_000, 8_000_000);
    ///
    /// assert_eq!(2000, tt.with_secs(2).as_millis());
    /// ```
    pub const fn with_secs(self, n: u64) -> Self {
        self.with_cycles(0).add_secs(n)
    }

    /// Returns the clock frequency associated with this duration (e.g. 16 MHz).
    ///
    /// # Examples
    ///
    /// ```
    /// # use avr_simulator::AvrDuration;
    /// #
    /// let tt = AvrDuration::new(16_000_000, 8_000_000);
    ///
    /// assert_eq!(16_000_000, tt.clock_frequency());
    /// ```
    pub const fn clock_frequency(self) -> u32 {
        self.clock_frequency
    }

    /// Returns the number of cycles contained by this duration.
    ///
    /// # Examples
    ///
    /// ```
    /// # use avr_simulator::AvrDuration;
    /// #
    /// let tt = AvrDuration::new(16_000_000, 8_000_000);
    ///
    /// assert_eq!(8_000_000, tt.as_cycles());
    /// ```
    ///
    /// ```
    /// # use avr_simulator::AvrDuration;
    /// #
    /// let tt = AvrDuration::new(16_000_000, 0).add_secs(3);
    ///
    /// assert_eq!(48_000_000, tt.as_cycles());
    /// ```
    pub const fn as_cycles(self) -> u64 {
        self.cycles
    }

    /// Returns the number of microseconds contained by this duration.
    ///
    /// # Examples
    ///
    /// ```
    /// # use avr_simulator::AvrDuration;
    /// #
    /// let tt = AvrDuration::new(16_000_000, 40);
    ///
    /// assert_eq!(3, tt.as_micros());
    /// ```
    pub fn as_micros(self) -> u64 {
        self.as_micros_f64().round() as _
    }

    /// Returns the number of microseconds contained by this duration as `f64`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use avr_simulator::AvrDuration;
    /// #
    /// let tt = AvrDuration::new(16_000_000, 40);
    ///
    /// assert_eq!(2.5, tt.as_micros_f64());
    /// ```
    pub fn as_micros_f64(self) -> f64 {
        (self.cycles as f64) / (self.clock_frequency as f64 / 1_000_000.0)
    }

    /// Returns the number of milliseconds contained by this duration.
    ///
    /// # Examples
    ///
    /// ```
    /// # use avr_simulator::AvrDuration;
    /// #
    /// let tt = AvrDuration::new(16_000_000, 40_000);
    ///
    /// assert_eq!(3, tt.as_millis());
    /// ```
    pub fn as_millis(self) -> u64 {
        self.as_millis_f64().round() as _
    }

    /// Returns the number of milliseconds contained by this duration as `f64`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use avr_simulator::AvrDuration;
    /// #
    /// let tt = AvrDuration::new(16_000_000, 40_000);
    ///
    /// assert_eq!(2.5, tt.as_millis_f64());
    /// ```
    pub fn as_millis_f64(self) -> f64 {
        (self.cycles as f64) / (self.clock_frequency as f64 / 1_000.0)
    }

    /// Returns the number of seconds contained by this duration.
    ///
    /// # Examples
    ///
    /// ```
    /// # use avr_simulator::AvrDuration;
    /// #
    /// let tt = AvrDuration::new(16_000_000, 40_000_000);
    ///
    /// assert_eq!(3, tt.as_secs());
    /// ```
    pub fn as_secs(self) -> u64 {
        self.as_secs_f64().round() as _
    }

    /// Returns the number of seconds contained by this duration as `f64`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use avr_simulator::AvrDuration;
    /// #
    /// let tt = AvrDuration::new(16_000_000, 40_000_000);
    ///
    /// assert_eq!(2.5, tt.as_secs_f64());
    /// ```
    pub fn as_secs_f64(self) -> f64 {
        (self.cycles as f64) / (self.clock_frequency as f64)
    }

    /// Returns whether the number of cycles contained by this duration is zero.
    ///
    /// # Examples
    ///
    /// ```
    /// # use avr_simulator::AvrDuration;
    /// #
    /// let tt1 = AvrDuration::new(16_000_000, 0);
    /// let tt2 = AvrDuration::new(16_000_000, 10_000);
    ///
    /// assert!(tt1.is_zero());
    /// assert!(!tt2.is_zero());
    /// ```
    pub fn is_zero(self) -> bool {
        self.as_cycles() == 0
    }
}

/// # Examples
///
/// ```
/// # use avr_simulator::AvrDuration;
/// #
/// let a = AvrDuration::new(16_000_000, 1_000);
/// let b = AvrDuration::new(16_000_000, 2_000);
///
/// assert_eq!(
///     AvrDuration::new(16_000_000, 3_000),
///     a + b,
/// );
/// ```
impl Add for AvrDuration {
    type Output = Self;

    fn add(mut self, rhs: Self) -> Self::Output {
        self += rhs;
        self
    }
}

/// # Examples
///
/// ```
/// # use avr_simulator::AvrDuration;
/// #
/// let mut a = AvrDuration::new(16_000_000, 1_000);
///
/// a += AvrDuration::new(16_000_000, 2_000);
///
/// assert_eq!(AvrDuration::new(16_000_000, 3_000), a);
/// ```
impl AddAssign for AvrDuration {
    fn add_assign(&mut self, rhs: Self) {
        assert_eq!(
            self.clock_frequency, rhs.clock_frequency,
            "Cannot add durations with different clock frequencies ({} vs {})",
            self.clock_frequency, rhs.clock_frequency
        );

        self.cycles += rhs.cycles;
    }
}

/// # Examples
///
/// ```
/// # use avr_simulator::AvrDuration;
/// #
/// let a = AvrDuration::new(16_000_000, 3_000);
/// let b = AvrDuration::new(16_000_000, 2_000);
///
/// assert_eq!(
///     AvrDuration::new(16_000_000, 1_000),
///     a - b,
/// );
/// ```
///
/// ```
/// # use avr_simulator::AvrDuration;
/// #
/// let a = AvrDuration::new(16_000_000, 3_000);
/// let b = AvrDuration::new(16_000_000, 4_000);
///
/// assert_eq!(
///     AvrDuration::new(16_000_000, 0),
///     a - b,
/// );
/// ```
impl Sub for AvrDuration {
    type Output = Self;

    fn sub(mut self, rhs: Self) -> Self::Output {
        self -= rhs;
        self
    }
}

/// # Examples
///
/// ```
/// # use avr_simulator::AvrDuration;
/// #
/// let mut a = AvrDuration::new(16_000_000, 3_000);
///
/// a -= AvrDuration::new(16_000_000, 2_000);
///
/// assert_eq!(AvrDuration::new(16_000_000, 1_000), a);
/// ```
///
/// ```
/// # use avr_simulator::AvrDuration;
/// #
/// let mut a = AvrDuration::new(16_000_000, 3_000);
///
/// a -= AvrDuration::new(16_000_000, 4_000);
///
/// assert_eq!(AvrDuration::new(16_000_000, 0), a);
/// ```
impl SubAssign for AvrDuration {
    fn sub_assign(&mut self, rhs: Self) {
        assert_eq!(
            self.clock_frequency, rhs.clock_frequency,
            "Cannot subtract durations with different clock frequencies ({} vs {})",
            self.clock_frequency, rhs.clock_frequency
        );

        self.cycles = self.cycles.saturating_sub(rhs.cycles);
    }
}

/// # Examples
///
/// ```rust
/// # use avr_simulator::AvrDuration;
/// #
/// let tt = AvrDuration::new(16_000_000, 0).add_millis(123);
///
/// assert_eq!("123000 µs", tt.to_string());
/// ```
impl fmt::Display for AvrDuration {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} µs", self.as_micros())
    }
}