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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
use super::{
    Decoder, DecoderError, DecoderErrorInt, MalformedPacket, TimestampDataRelation, TracePacket,
};

use std::io::Read;
use std::time::Duration;

pub use crate::cortex_m::LocalTimestampOptions;

/// Iterator that yield [`TracePacket`](TracePacket).
pub struct Singles<R>
where
    R: Read,
{
    decoder: Decoder<R>,
}

impl<R> Singles<R>
where
    R: Read,
{
    pub(super) fn new(decoder: Decoder<R>) -> Self {
        Self { decoder }
    }
}

impl<R> Iterator for Singles<R>
where
    R: Read,
{
    type Item = Result<TracePacket, DecoderError>;

    fn next(&mut self) -> Option<Self::Item> {
        let trace = self.decoder.next_single();

        match trace {
            Err(DecoderErrorInt::Eof) => None,
            Err(DecoderErrorInt::Io(io)) => Some(Err(DecoderError::Io(io))),
            Err(DecoderErrorInt::MalformedPacket(m)) => Some(Err(DecoderError::MalformedPacket(m))),
            Ok(trace) => Some(Ok(trace)),
        }
    }
}

/// [`Timestamps`](Timestamps) configuration.
#[derive(Clone)]
pub struct TimestampsConfiguration {
    /// Frequency of the ITM timestamp clock. Necessary to calculate a
    /// relative timestamp from global and local timestamp packets.
    pub clock_frequency: u32,

    /// Prescaler used for the ITM timestamp clock. Necessary to
    /// calculate a relative timestamp from global and local timestamp
    /// packets.
    pub lts_prescaler: LocalTimestampOptions,

    /// When set, pushes [`MalformedPacket`](MalformedPacket)s to
    /// [`TimestampedTracePackets::malformed_packets`](TimestampedTracePackets::malformed_packets)
    /// instead of returning it as an `Result::Err`.
    pub expect_malformed: bool,
}

/// A set of timestamped [`TracePacket`](TracePacket)s.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TimestampedTracePackets {
    /// Timestamp of [`packets`](Self::packets) and
    /// [`malformed_packets`](Self::malformed_packets).
    pub timestamp: Timestamp,

    /// Packets that the target generated during
    /// [`timestamp`](Self::timestamp).
    pub packets: Vec<TracePacket>,

    /// Malformed packets that the target generated during
    /// [`timestamp`](Self::timestamp).
    pub malformed_packets: Vec<MalformedPacket>,

    /// The number of [`TracePacket`](TracePacket)s consumed to generate
    /// this structure.
    pub consumed_packets: usize,
}

/// Timestamp relative to trace clock start with quality
/// descriptions. In order of decreasing quality:
/// - [`Sync`](Timestamp::Sync);
/// - [`UnknownDelay`](Timestamp::UnknownDelay);
/// - [`AssocEventDelay`](Timestamp::AssocEventDelay);
/// - [`UnknownAssocEventDelay`](Timestamp::UnknownAssocEventDelay).
///
/// A decrease in timestamp quality indicates an insufficient
/// exfiltration rate of trace packets. A decrease in timestamp
/// quality may herald an overflow event.
///
/// See also (Appendix D4.2.4).
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Timestamp {
    /// The timestamp is synchronous to the ITM/DWT data and event.
    Sync(Duration),
    /// The synchronous timestamp to the ITM/DWT data is unknown, but
    /// must be between the previous and current timestamp provided
    /// here.
    UnknownDelay {
        /// The previous timestamp.
        prev: Duration,
        /// The current timestamp.
        curr: Duration,
    },
    /// The timestamp is synchronous to the ITM/DWT data packet
    /// generation, but the packet itself was delayed relative to the
    /// corresponding event due to other trace output packets.
    AssocEventDelay(Duration),
    /// The synchronous timestamp to the ITM/DWT data is unknown and
    /// the generation of the associated packet itself was delayed
    /// relative to the corresponding event due to other trace output
    /// packets. This is a combination of
    /// [`UnknownDelay`](Timestamp::UnknownDelay) and
    /// [`AssocEventDelay`](Timestamp::AssocEventDelay); the packet
    /// was generated, and the associated event occured some time
    /// between the previous and current timestamp provided here.
    UnknownAssocEventDelay {
        /// The previous timestamp.
        prev: Duration,
        /// The current timestamp.
        curr: Duration,
    },
}

/// Iterator that yield [`TimestampedTracePackets`](TimestampedTracePackets).
pub struct Timestamps<R>
where
    R: Read,
{
    decoder: Decoder<R>,
    options: TimestampsConfiguration,
    current_offset: Duration,
    gts: Gts,
    prev_lts: Duration,
}

#[cfg_attr(test, derive(Clone, Debug))]
struct Gts {
    pub lower: Option<u64>,
    pub upper: Option<u64>,
}
impl Gts {
    const GTS2_SHIFT: u32 = 26; // see (Appendix D4.2.5).

    pub fn replace_lower(&mut self, new: u64) {
        self.lower = match self.lower {
            None => Some(new),
            Some(old) => {
                let shift = 64 - new.leading_zeros();
                Some(((old >> shift) << shift) | new)
            }
        }
    }

    pub fn reset(&mut self) {
        self.lower = None;
        self.upper = None;
    }

    pub fn merge(&self) -> Option<u64> {
        if let (Some(lower), Some(upper)) = (self.lower, self.upper) {
            Some(
                upper
                    .checked_shl(Self::GTS2_SHIFT)
                    .expect("GTS merge overflow")
                    | lower,
            )
        } else {
            None
        }
    }

    #[cfg(test)]
    #[allow(dead_code)]
    pub fn from_one(gts: TracePacket) -> Self {
        match gts {
            TracePacket::GlobalTimestamp1 { ts: lower, .. } => Self {
                lower: Some(lower),
                upper: None,
            },
            TracePacket::GlobalTimestamp2 { ts: upper, .. } => Self {
                lower: None,
                upper: Some(upper),
            },
            _ => unreachable!(),
        }
    }
}

impl<R> Timestamps<R>
where
    R: Read,
{
    pub(super) fn new(decoder: Decoder<R>, options: TimestampsConfiguration) -> Self {
        if options.lts_prescaler == LocalTimestampOptions::Disabled {
            unimplemented!("Generating approximate absolute timestamps from global timestamps alone is not yet supported");
        }

        Self {
            current_offset: Duration::from_nanos(0),
            decoder,
            options,
            gts: Gts {
                lower: None,
                upper: None,
            },
            // NOTE: required because GTS resets current_offset. GTS
            // -> LTS, would yield incorrect prev timestamp if this
            // field, upon which only local timestamps are applied, is
            // not used.
            prev_lts: Duration::from_nanos(0),
        }
    }

    fn next_timestamped(
        &mut self,
        options: TimestampsConfiguration,
    ) -> Result<TimestampedTracePackets, DecoderErrorInt> {
        use std::ops::Add;

        let mut packets: Vec<TracePacket> = vec![];
        let mut malformed_packets: Vec<MalformedPacket> = vec![];
        let mut consumed_packets: usize = 0;

        fn apply_lts(
            prev_offset: &mut Duration,
            lts: u64,
            data_relation: TimestampDataRelation,
            current_offset: &mut Duration,
            options: &TimestampsConfiguration,
        ) -> Timestamp {
            let offset = calc_offset(lts, Some(options.lts_prescaler), options.clock_frequency);
            *current_offset = current_offset.add(offset);

            let lts = match data_relation {
                TimestampDataRelation::Sync => Timestamp::Sync(*current_offset),
                TimestampDataRelation::UnknownDelay => Timestamp::UnknownDelay {
                    prev: *prev_offset,
                    curr: *current_offset,
                },
                TimestampDataRelation::AssocEventDelay => {
                    Timestamp::AssocEventDelay(*current_offset)
                }
                TimestampDataRelation::UnknownAssocEventDelay => {
                    Timestamp::UnknownAssocEventDelay {
                        prev: *prev_offset,
                        curr: *current_offset,
                    }
                }
            };
            *prev_offset = *current_offset;
            lts
        }

        fn apply_gts(gts: &Gts, current_offset: &mut Duration, options: &TimestampsConfiguration) {
            if let Some(gts) = gts.merge() {
                let offset = calc_offset(gts, None, options.clock_frequency);
                *current_offset = offset;
            }
        }

        loop {
            consumed_packets += 1;
            match self.decoder.next_single() {
                Err(DecoderErrorInt::MalformedPacket(m)) if options.expect_malformed => {
                    malformed_packets.push(m);
                }
                Err(e) => return Err(e),
                Ok(packet) => match packet {
                    // A local timestamp: packets received up to this point
                    // relate to this local timestamp. Return these.
                    TracePacket::LocalTimestamp1 { ts, data_relation } => {
                        return Ok(TimestampedTracePackets {
                            timestamp: apply_lts(
                                &mut self.prev_lts,
                                ts.into(),
                                data_relation,
                                &mut self.current_offset,
                                &self.options,
                            ),
                            packets,
                            malformed_packets,
                            consumed_packets,
                        });
                    }
                    TracePacket::LocalTimestamp2 { ts } => {
                        return Ok(TimestampedTracePackets {
                            timestamp: apply_lts(
                                &mut self.prev_lts,
                                ts.into(),
                                TimestampDataRelation::Sync,
                                &mut self.current_offset,
                                &self.options,
                            ),
                            packets,
                            malformed_packets,
                            consumed_packets,
                        });
                    }

                    // A global timestamp: store until we have both the
                    // upper (GTS2) and lower (GTS1) bits.
                    TracePacket::GlobalTimestamp1 { ts, wrap, clkch } => {
                        self.gts.replace_lower(ts);

                        if wrap {
                            // upper bits have changed; GTS2 incoming
                            self.gts.upper = None;
                        } else if clkch {
                            // system has asserted clock change input; full GTS incoming
                            //
                            // A clock change signal that the system
                            // asserts if there is a change in the ratio
                            // between the global timestamp clock
                            // frequency and the processor clock
                            // frequency. Implementation and use of the
                            // clock change signal is optional and
                            // deprecated.
                            self.gts.reset();
                        } else {
                            apply_gts(&self.gts, &mut self.current_offset, &options);
                        }
                    }
                    TracePacket::GlobalTimestamp2 { ts } => {
                        self.gts.upper = Some(ts);
                        apply_gts(&self.gts, &mut self.current_offset, &options);
                    }

                    packet => packets.push(packet),
                },
            }
        }
    }
}

impl<R> Iterator for Timestamps<R>
where
    R: Read,
{
    type Item = Result<TimestampedTracePackets, DecoderError>;

    fn next(&mut self) -> Option<Self::Item> {
        let trace = self.next_timestamped(self.options.clone());

        match trace {
            Err(DecoderErrorInt::Eof) => None,
            Err(DecoderErrorInt::Io(io)) => Some(Err(DecoderError::Io(io))),
            Err(DecoderErrorInt::MalformedPacket(m)) => Some(Err(DecoderError::MalformedPacket(m))),
            Ok(trace) => Some(Ok(trace)),
        }
    }
}

fn calc_offset(ts: u64, prescaler: Option<LocalTimestampOptions>, freq: u32) -> Duration {
    let prescale = match prescaler {
        None | Some(LocalTimestampOptions::Enabled) => 1,
        Some(LocalTimestampOptions::EnabledDiv4) => 4,
        Some(LocalTimestampOptions::EnabledDiv16) => 16,
        Some(LocalTimestampOptions::EnabledDiv64) => 64,
        Some(LocalTimestampOptions::Disabled) => unreachable!(), // checked in `Timestamps::new`
    };
    let ticks = ts * prescale;
    let seconds = ticks as f64 / freq as f64;

    // NOTE(ceil) we rount up so as to not report an event before it
    // occurs on hardware.
    Duration::from_nanos((seconds * 1e9).ceil() as u64)
}

#[cfg(test)]
mod timestamp_utils {
    use super::*;

    #[test]
    fn gts() {
        let mut gts = Gts {
            lower: Some(1), // bit 1
            upper: Some(1), // bit 26
        };
        assert_eq!(gts.merge(), Some(67108865));

        gts.replace_lower(127);
        assert_eq!(gts.merge(), Some(67108991));

        let gts = Gts {
            lower: None,
            upper: None,
        };
        assert_eq!(gts.merge(), None, "noop merge");

        let mut gts = Gts {
            lower: Some(42),
            upper: Some(42),
        };
        assert_eq!(
            gts.merge(),
            Some((42 << Gts::GTS2_SHIFT) | 42),
            "(42, 42) merge"
        );

        gts.replace_lower(0b1101011);
        assert_eq!(
            gts.merge(),
            Some((42 << Gts::GTS2_SHIFT) | 0b1101011),
            "replace whole merge"
        );

        let mut gts = Gts {
            lower: Some(42),
            upper: Some(42),
        };
        gts.replace_lower(1);
        assert_eq!(
            gts.merge(),
            Some((42 << Gts::GTS2_SHIFT) | 43),
            "replace partial merge"
        );
    }

    #[test]
    fn offset() {
        assert_eq!(
            calc_offset(1000, Some(LocalTimestampOptions::EnabledDiv4), 16_000_000),
            Duration::from_micros(250),
        );
    }
}

#[cfg(test)]
mod timestamps {
    use super::Duration;
    use crate::*;

    const FREQ: u32 = 16_000_000;

    /// Check whether timestamps are correctly generated by effectively
    /// comparing `Timestamps::next_timestamps` and [outer_calc_offset].
    #[test]
    fn check_timestamps() {
        #[rustfmt::skip]
        let stream: &[u8] = &[
            // PC sample (sleeping)
            0b0001_0101,
            0b0000_0000,

            // PC sample (sleeping)
            0b0001_0101,
            0b0000_0000,

            // PC sample (sleeping)
            0b0001_0101,
            0b0000_0000,

            // GTS1
            0b1001_0100,
            0b1000_0000,
            0b1010_0000,
            0b1000_0100,
            0b0000_0000,

            // GTS2 (48-bit)
            0b1011_0100,
            0b1011_1101,
            0b1111_0100,
            0b1001_0001,
            0b0000_0001,

            // LTS1
            0b1100_0000,
            0b1100_1001,
            0b0000_0001,

            // Pull!

            // PC sample (sleeping)
            0b0001_0101,
            0b0000_0000,

            // LTS1
            0b1100_0000,
            0b1100_1001,
            0b0000_0001,

            // Pull!

            // Overflow
            0b0111_0000,

            // LTS1
            0b1100_0000,
            0b1100_1001,
            0b0000_0001,

            // Pull!

            // GTS1
            0b1001_0100,
            0b1000_0000,
            0b1010_0000,
            0b1000_0100,
            0b0000_0000,

            // GTS2 (48-bit)
            0b1011_0100,
            0b1011_1101,
            0b1111_0100,
            0b1001_0001,
            0b0000_0001,

            // LTS1
            0b1111_0000,
            0b1100_1001,
            0b0000_0001,

            // LTS2
            0b0110_0000,
        ];

        let decoder = Decoder::new(stream.clone(), DecoderOptions { ignore_eof: false });
        let mut it = decoder.timestamps(TimestampsConfiguration {
            clock_frequency: FREQ,
            lts_prescaler: LocalTimestampOptions::Enabled,
            expect_malformed: false,
        });

        for set in [
            TimestampedTracePackets {
                packets: [
                    TracePacket::PCSample { pc: None },
                    TracePacket::PCSample { pc: None },
                    TracePacket::PCSample { pc: None },
                ]
                .into(),
                malformed_packets: [].into(),
                timestamp: Timestamp::Sync(Duration::from_nanos(10026857009420563)),
                consumed_packets: 6,
            },
            TimestampedTracePackets {
                packets: [TracePacket::PCSample { pc: None }].into(),
                malformed_packets: [].into(),
                timestamp: Timestamp::Sync(Duration::from_nanos(10026857009433126)),
                consumed_packets: 2,
            },
            TimestampedTracePackets {
                packets: [TracePacket::Overflow].into(),
                malformed_packets: [].into(),
                timestamp: Timestamp::Sync(Duration::from_nanos(10026857009445689)),
                consumed_packets: 2,
            },
            TimestampedTracePackets {
                packets: [].into(),
                malformed_packets: [].into(),
                timestamp: Timestamp::UnknownAssocEventDelay {
                    prev: Duration::from_nanos(10026857009445689),
                    curr: Duration::from_nanos(10026857009420563),
                },
                consumed_packets: 3,
            },
            TimestampedTracePackets {
                packets: [].into(),
                malformed_packets: [].into(),
                timestamp: Timestamp::Sync(Duration::from_nanos(10026857009420938)),
                consumed_packets: 1,
            },
        ]
        .iter()
        {
            assert_eq!(it.next().unwrap().unwrap(), *set);
        }
    }

    /// Test cases where a GTS2 applied to two GTS1; 64-bit GTS2; and
    /// compares timestamps to precalculated [Duration] offsets.
    #[test]
    fn gts_compression() {
        #[rustfmt::skip]
        let stream: &[u8] = &[
            // LTS2
            0b0110_0000,

            // GTS1 (bit 1 set)
            0b1001_0100,
            0b1000_0001,
            0b1000_0000,
            0b1000_0000,
            0b0000_0000,

            // GTS2 (64-bit, bit 26 set)
            0b1011_0100,
            0b1000_0001,
            0b1000_0000,
            0b1000_0000,
            0b1000_0000,
            0b1000_0000,
            0b0000_0000,

            // LTS2
            0b0110_0000,

            // GTS1 (compressed)
            0b1001_0100,
            0b1111_1111,
            0b0000_0000,

            // LTS2
            0b0110_0000,

            // TODO: add a section where a GTS1 must merge with the
            // previous GTS1
        ];

        let decoder = Decoder::new(stream.clone(), DecoderOptions { ignore_eof: false });
        let mut it = decoder.timestamps(TimestampsConfiguration {
            clock_frequency: FREQ,
            lts_prescaler: LocalTimestampOptions::Enabled,
            expect_malformed: false,
        });

        for set in [
            TimestampedTracePackets {
                packets: [].into(),
                malformed_packets: [].into(),
                timestamp: Timestamp::Sync(Duration::from_nanos(375)),
                consumed_packets: 1,
            },
            TimestampedTracePackets {
                packets: [].into(),
                malformed_packets: [].into(),
                timestamp: Timestamp::Sync(Duration::from_nanos(4194304438)),
                consumed_packets: 3,
            },
            TimestampedTracePackets {
                packets: [].into(),
                malformed_packets: [].into(),
                timestamp: Timestamp::Sync(Duration::from_nanos(4194312313)),
                consumed_packets: 2,
            },
        ]
        .iter()
        {
            let ttp = it.next().unwrap().unwrap();
            assert_eq!(ttp, *set);
        }
    }
}