maquette 0.1.0

A simulated HDMI sink for tests: SCDC and EDID registers, a DDC bus, a PHY and a clock under deterministic time, with faults and conformance checks for hdmi-hal's traits
Documentation
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
//! The PHY check: hdmi-hal's `TmdsPhy`, `PhyPower` and `FrlPhy`, with a probe for what the
//! traits cannot show.

use core::fmt;

use hdmi_hal::HdmiForumFrl;
use hdmi_hal::phy::TmdsPhyConfig;

use super::Report;
use crate::bench::Bench;
use crate::line::{Line, RATIO_40_ABOVE_KHZ};

/// What a PHY transmits: what the traits cannot show.
///
/// The probe is handed the PHY, so a PHY that knows its own output is probed by a type that
/// reads it.
pub trait OutputProbe<P: ?Sized> {
    /// What `phy` transmits: `Some(Line::Off)` when nothing, `None` when the probe cannot
    /// tell; the points that need it are then skipped.
    fn line(&self, phy: &P) -> Option<Line>;
}

/// The bench's line, whichever PHY drives it.
impl<P: ?Sized, const LOG: usize, const FAULTS: usize> OutputProbe<P> for Bench<'_, LOG, FAULTS> {
    fn line(&self, _phy: &P) -> Option<Line> {
        Some(self.line())
    }
}

/// A probe that cannot tell: the points that need one are skipped.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct NoProbe;

impl<P: ?Sized> OutputProbe<P> for NoProbe {
    fn line(&self, _phy: &P) -> Option<Line> {
        None
    }
}

/// A point of the [`PhyCheck`]: a call, in the state the check put the PHY in.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum PhyPoint {
    /// `stop_output` on a fresh PHY.
    StopFresh,
    /// `start_output` before any `configure`.
    StartUnconfigured,
    /// `configure`, with the output stopped.
    Configure,
    /// `set_scrambling(false)` with the output stopped.
    ScramblerOffStopped,
    /// `set_scrambling(true)` with the output stopped.
    ScramblerOnStopped,
    /// `start_output` after `configure`.
    Start,
    /// `set_scrambling(false)` with the output running.
    ScramblerOffRunning,
    /// `set_scrambling(true)` with the output running.
    ScramblerOnRunning,
    /// `configure` with the output running: it fails, or stops the output.
    ConfigureRunning,
    /// `stop_output` after the output ran.
    Stop,
    /// `stop_output` on a stopped output.
    StopStopped,
    /// `power_off`.
    PowerOff,
    /// `stop_output` while powered off.
    StopPoweredOff,
    /// `configure` while powered off.
    ConfigurePoweredOff,
    /// `power_on`.
    PowerOn,
    /// `power_on` on a PHY that is on.
    PowerOnAgain,
    /// `start_output` after a power-on, before `configure`.
    StartAfterPowerOn,
    /// `set_frl_rate` with a rate.
    EnterFrl,
    /// `start_output` in FRL, before `configure`.
    StartInFrl,
    /// `configure` in FRL: it leaves FRL.
    ConfigureLeavesFrl,
    /// `stop_output` in FRL: it leaves FRL.
    StopLeavesFrl,
    /// `set_frl_rate(NotSupported)`.
    FrlOff,
}

/// A failed point of the [`PhyCheck`]. A PHY's errors are only `Debug`, so a failure says
/// which point failed and how, not why.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum PhyFailure {
    /// The call failed where the contract says it succeeds.
    Failed {
        /// The point.
        point: PhyPoint,
    },
    /// The call succeeded where the contract says it fails.
    Succeeded {
        /// The point.
        point: PhyPoint,
    },
    /// After the call, the probe saw the output running when it should not, or not when it
    /// should.
    Running {
        /// The point.
        point: PhyPoint,
        /// Whether it should run.
        expected: bool,
        /// Whether it ran.
        found: bool,
    },
    /// After the call, the output ran but not as the configuration and the calls ask: TMDS
    /// at another rate or ratio, scrambled or not against `set_scrambling`, or FRL at
    /// another rate.
    Line {
        /// The point.
        point: PhyPoint,
        /// What the probe saw.
        found: Line,
    },
}

impl fmt::Display for PhyFailure {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Failed { point } => write!(f, "{point:?}: the call failed"),
            Self::Succeeded { point } => write!(f, "{point:?}: the call should have failed"),
            Self::Running {
                point,
                expected,
                found,
            } => {
                let state = |running: &bool| if *running { "running" } else { "stopped" };
                write!(
                    f,
                    "{point:?}: the output was {}, not {}",
                    state(found),
                    state(expected)
                )
            }
            Self::Line { point, found } => write!(
                f,
                "{point:?}: transmits {found:?}, not what the configuration and the calls ask"
            ),
        }
    }
}

/// The PHY check: the points of hdmi-hal's PHY contracts, with a configuration the PHY can
/// do and a probe for what it transmits: whether its output runs, and, once it runs, TMDS at
/// the configuration's rate and ratio, scrambled as `set_scrambling` last said, or FRL at
/// the rate entered.
///
/// - **`run_tmds`**, `TmdsPhy` alone, on a PHY fresh from creation: `stop_output` before
///   anything; `start_output` refused before `configure`; `configure` leaving the output
///   stopped; `set_scrambling` with the output stopped and running (`true` only where the
///   capabilities report a scrambler for the rate, and refused without one); `start_output`
///   running the output; `configure` with the output running failing or stopping it;
///   `stop_output`, twice.
/// - **`run_powered`**, with `PhyPower`: a power cycle first, so any PHY is checked from the
///   contract's known state, then the TMDS points, then `power_off` stopping the output,
///   `stop_output` and a refused `configure` while off, `power_on` leaving the output stopped
///   and changing nothing a second time, and `configure` needed again before `start_output`.
/// - **`run_frl`**, with `FrlPhy` too and [`with_frl_rate`](Self::with_frl_rate): the mode
///   rule — a rate enters FRL and leaves TMDS (`start_output` refused until `configure`),
///   `configure` leaves FRL, `stop_output` leaves FRL, `NotSupported` enters nothing.
pub struct PhyCheck<'p, R: ?Sized> {
    config: TmdsPhyConfig,
    probe: &'p R,
    frl_rate: Option<HdmiForumFrl>,
}

impl<R: ?Sized> Clone for PhyCheck<'_, R> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<R: ?Sized> Copy for PhyCheck<'_, R> {}

impl<R: ?Sized> fmt::Debug for PhyCheck<'_, R> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PhyCheck")
            .field("config", &self.config)
            .field("frl_rate", &self.frl_rate)
            .finish_non_exhaustive()
    }
}

impl<'p, R: ?Sized> PhyCheck<'p, R> {
    /// The check with `config`, one the PHY can do, and `probe` for what it transmits
    /// ([`NoProbe`] when nothing can tell).
    pub const fn new(config: TmdsPhyConfig, probe: &'p R) -> Self {
        Self {
            config,
            probe,
            frl_rate: None,
        }
    }

    /// The check entering FRL at `rate`, one the PHY can do, for the mode rule.
    #[must_use]
    pub const fn with_frl_rate(self, rate: HdmiForumFrl) -> Self {
        Self {
            frl_rate: Some(rate),
            ..self
        }
    }
}

/// Records the check's points.
struct Points<'r> {
    report: &'r mut Report<PhyFailure>,
}

impl Points<'_> {
    /// A call the contract says succeeds.
    fn ok<E>(&mut self, point: PhyPoint, result: Result<(), E>) -> bool {
        let ok = result.is_ok();
        self.report
            .record((!ok).then_some(PhyFailure::Failed { point }));
        ok
    }

    /// A call the contract says fails.
    fn refused<E>(&mut self, point: PhyPoint, result: Result<(), E>) {
        self.report
            .record(result.is_ok().then_some(PhyFailure::Succeeded { point }));
    }

    /// Whether the output runs after `point`, as the probe sees `line`.
    fn running(&mut self, point: PhyPoint, line: Option<Line>, expected: bool) {
        if let Some(line) = line {
            let found = line != Line::Off;
            self.report
                .record((found != expected).then_some(PhyFailure::Running {
                    point,
                    expected,
                    found,
                }));
        }
    }

    /// Whether the output runs TMDS at `config`'s rate and ratio after `point`, scrambled
    /// as `scrambled` says where it says.
    fn tmds(
        &mut self,
        point: PhyPoint,
        line: Option<Line>,
        config: TmdsPhyConfig,
        scrambled: Option<bool>,
    ) {
        let Some(line) = line else { return };
        self.report.record(match line {
            Line::Off => Some(PhyFailure::Running {
                point,
                expected: true,
                found: false,
            }),
            Line::Tmds {
                character_rate_khz,
                ratio,
                scrambled: found,
            } if character_rate_khz == config.character_rate_khz
                && ratio == config.clock_ratio
                && scrambled.is_none_or(|expected| expected == found) =>
            {
                None
            }
            found => Some(PhyFailure::Line { point, found }),
        });
    }

    /// Whether the output runs FRL at `rate` after `point`.
    fn frl(&mut self, point: PhyPoint, line: Option<Line>, rate: HdmiForumFrl) {
        let Some(line) = line else { return };
        self.report.record(match line {
            Line::Off => Some(PhyFailure::Running {
                point,
                expected: true,
                found: false,
            }),
            Line::Frl { rate: found, .. } if found == rate => None,
            found => Some(PhyFailure::Line { point, found }),
        });
    }
}

/// Whether `set_scrambling(true)` is the contract's to accept for `config` on a PHY with these
/// capabilities: a scrambler, and one that works at the rate.
fn scrambles(config: TmdsPhyConfig, caps: hdmi_hal::phy::TmdsPhyCapabilities) -> bool {
    caps.scrambling
        && (config.character_rate_khz > RATIO_40_ABOVE_KHZ || caps.scrambles_at_low_rate())
}

/// The TMDS points, the power points and the FRL points, once for the sync traits and once,
/// with `.await`, for the async ones.
macro_rules! tmds {
    ($check:ident, $points:ident, $phy:ident, $($await:tt)*) => {{
        use PhyPoint as P;
        let config = $check.config;
        let caps = $phy.capabilities();
        $points.ok(P::StopFresh, $phy.stop_output() $($await)*);
        $points.running(P::StopFresh, $check.probe.line(&*$phy), false);
        $points.refused(P::StartUnconfigured, $phy.start_output() $($await)*);
        $points.running(P::StartUnconfigured, $check.probe.line(&*$phy), false);
        if $points.ok(P::Configure, $phy.configure(config) $($await)*) {
            $points.running(P::Configure, $check.probe.line(&*$phy), false);
            $points.ok(P::ScramblerOffStopped, $phy.set_scrambling(false) $($await)*);
            if scrambles(config, caps) {
                $points.ok(P::ScramblerOnStopped, $phy.set_scrambling(true) $($await)*);
            } else if !caps.scrambling {
                $points.refused(P::ScramblerOnStopped, $phy.set_scrambling(true) $($await)*);
            }
            if $points.ok(P::Start, $phy.start_output() $($await)*) {
                $points.tmds(P::Start, $check.probe.line(&*$phy), config, None);
                if $points.ok(P::ScramblerOffRunning, $phy.set_scrambling(false) $($await)*) {
                    let line = $check.probe.line(&*$phy);
                    $points.tmds(P::ScramblerOffRunning, line, config, Some(false));
                }
                if scrambles(config, caps)
                    && $points.ok(P::ScramblerOnRunning, $phy.set_scrambling(true) $($await)*)
                {
                    let line = $check.probe.line(&*$phy);
                    $points.tmds(P::ScramblerOnRunning, line, config, Some(true));
                }
                // Either behaviour is the contract's; a success stops the output.
                if $phy.configure(config) $($await)*.is_ok() {
                    $points.running(P::ConfigureRunning, $check.probe.line(&*$phy), false);
                }
            }
        }
        $points.ok(P::Stop, $phy.stop_output() $($await)*);
        $points.running(P::Stop, $check.probe.line(&*$phy), false);
        $points.ok(P::StopStopped, $phy.stop_output() $($await)*);
    }};
}

macro_rules! power {
    ($check:ident, $points:ident, $phy:ident, $($await:tt)*) => {{
        use PhyPoint as P;
        let config = $check.config;
        if $points.ok(P::Configure, $phy.configure(config) $($await)*)
            && $points.ok(P::Start, $phy.start_output() $($await)*)
        {
            $points.tmds(P::Start, $check.probe.line(&*$phy), config, None);
            $points.ok(P::PowerOnAgain, $phy.power_on() $($await)*);
            $points.tmds(P::PowerOnAgain, $check.probe.line(&*$phy), config, None);
        }
        $points.ok(P::PowerOff, $phy.power_off() $($await)*);
        $points.running(P::PowerOff, $check.probe.line(&*$phy), false);
        $points.ok(P::StopPoweredOff, $phy.stop_output() $($await)*);
        $points.refused(P::ConfigurePoweredOff, $phy.configure(config) $($await)*);
        $points.ok(P::PowerOn, $phy.power_on() $($await)*);
        $points.running(P::PowerOn, $check.probe.line(&*$phy), false);
        $points.refused(P::StartAfterPowerOn, $phy.start_output() $($await)*);
    }};
}

macro_rules! frl {
    ($check:ident, $points:ident, $phy:ident, $($await:tt)*) => {{
        use PhyPoint as P;
        if let Some(rate) = $check.frl_rate {
            let config = $check.config;
            if $points.ok(P::EnterFrl, $phy.set_frl_rate(rate) $($await)*) {
                $points.frl(P::EnterFrl, $check.probe.line(&*$phy), rate);
                $points.refused(P::StartInFrl, $phy.start_output() $($await)*);
                $points.ok(P::ConfigureLeavesFrl, $phy.configure(config) $($await)*);
                $points.running(P::ConfigureLeavesFrl, $check.probe.line(&*$phy), false);
            }
            if $points.ok(P::EnterFrl, $phy.set_frl_rate(rate) $($await)*) {
                $points.ok(P::StopLeavesFrl, $phy.stop_output() $($await)*);
                $points.running(P::StopLeavesFrl, $check.probe.line(&*$phy), false);
            }
            if $points.ok(P::EnterFrl, $phy.set_frl_rate(rate) $($await)*) {
                $points.ok(P::FrlOff, $phy.set_frl_rate(HdmiForumFrl::NotSupported) $($await)*);
                $points.running(P::FrlOff, $check.probe.line(&*$phy), false);
                $points.refused(P::StartInFrl, $phy.start_output() $($await)*);
            }
        }
    }};
}

impl<R: ?Sized> PhyCheck<'_, R> {
    /// The TMDS points, against a PHY fresh from creation (`start_output` is refused before
    /// its first `configure`).
    pub fn run_tmds<P>(&self, phy: &mut P) -> Report<PhyFailure>
    where
        P: hdmi_hal::phy::TmdsPhy + ?Sized,
        R: OutputProbe<P>,
    {
        let mut report = Report::new();
        let mut points = Points {
            report: &mut report,
        };
        let check = self;
        tmds!(check, points, phy,);
        report
    }

    /// A power cycle, the TMDS points and the power points, against any PHY.
    pub fn run_powered<P>(&self, phy: &mut P) -> Report<PhyFailure>
    where
        P: hdmi_hal::phy::TmdsPhy + hdmi_hal::phy::PhyPower + ?Sized,
        R: OutputProbe<P>,
    {
        let mut report = Report::new();
        let mut points = Points {
            report: &mut report,
        };
        let check = self;
        points.ok(PhyPoint::PowerOff, phy.power_off());
        points.ok(PhyPoint::PowerOn, phy.power_on());
        tmds!(check, points, phy,);
        power!(check, points, phy,);
        report
    }

    /// The powered points and the mode rule (with [`with_frl_rate`](Self::with_frl_rate)).
    pub fn run_frl<P>(&self, phy: &mut P) -> Report<PhyFailure>
    where
        P: hdmi_hal::phy::TmdsPhy + hdmi_hal::phy::PhyPower + hdmi_hal::phy::FrlPhy + ?Sized,
        R: OutputProbe<P>,
    {
        let mut report = self.run_powered(phy);
        let mut points = Points {
            report: &mut report,
        };
        let check = self;
        frl!(check, points, phy,);
        report
    }

    /// [`run_tmds`](Self::run_tmds) over hdmi-hal-async's `TmdsPhy`.
    #[cfg(feature = "async")]
    pub async fn run_tmds_async<P>(&self, phy: &mut P) -> Report<PhyFailure>
    where
        P: hdmi_hal_async::phy::TmdsPhy + ?Sized,
        R: OutputProbe<P>,
    {
        let mut report = Report::new();
        let mut points = Points {
            report: &mut report,
        };
        let check = self;
        tmds!(check, points, phy, .await);
        report
    }

    /// [`run_powered`](Self::run_powered) over hdmi-hal-async's traits.
    #[cfg(feature = "async")]
    pub async fn run_powered_async<P>(&self, phy: &mut P) -> Report<PhyFailure>
    where
        P: hdmi_hal_async::phy::TmdsPhy + hdmi_hal_async::phy::PhyPower + ?Sized,
        R: OutputProbe<P>,
    {
        let mut report = Report::new();
        let mut points = Points {
            report: &mut report,
        };
        let check = self;
        points.ok(PhyPoint::PowerOff, phy.power_off().await);
        points.ok(PhyPoint::PowerOn, phy.power_on().await);
        tmds!(check, points, phy, .await);
        power!(check, points, phy, .await);
        report
    }

    /// [`run_frl`](Self::run_frl) over hdmi-hal-async's traits.
    #[cfg(feature = "async")]
    pub async fn run_frl_async<P>(&self, phy: &mut P) -> Report<PhyFailure>
    where
        P: hdmi_hal_async::phy::TmdsPhy
            + hdmi_hal_async::phy::PhyPower
            + hdmi_hal_async::phy::FrlPhy
            + ?Sized,
        R: OutputProbe<P>,
    {
        let mut report = self.run_powered_async(phy).await;
        let mut points = Points {
            report: &mut report,
        };
        let check = self;
        frl!(check, points, phy, .await);
        report
    }
}