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
651
652
653
654
655
656
657
658
659
660
661
662
//! The chosen link: `TmdsLinkParams`, its `choose` function, and why the link is or is
//! not scrambled (`ScramblingDecision`, `ScramblingOff`).
use crate::error::ParamsError;
use crate::types::{
ClockRatio, ScramblingThreshold, SinkTmdsConfig, SinkTmdsFacts, SourceTmdsFacts,
TmdsConfigGate, TmdsPolicy,
};
/// The rate, in kHz, that the threshold is measured against: 340 Mcsc.
const THRESHOLD_KHZ: u32 = 340_000;
/// Why the link is or is not scrambled.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ScramblingDecision {
/// The rate is above the threshold, so the link is scrambled at 1/40.
Required,
/// The rate is at or below the threshold, and the source, the sink and the policy
/// all allow scrambling.
LowRate,
/// The link is 1/10 and not scrambled: the rate is at or below the threshold, or it
/// is exactly 340 000 kHz under [`ScramblingThreshold::AtOrAbove340`] on a sink
/// without SCDC or scrambling support or a source without a scrambler (then
/// `no_scdc`, `no_scrambling_support` or `source_cannot` is set).
Off(ScramblingOff),
}
/// Every condition that keeps a 1/10 link unscrambled.
///
/// At least one flag is set. Each is judged on its own fact, independently of the
/// others: a sink without SCDC that also does not declare `LTE_340Mcsc_scramble` has
/// both flags set.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ScramblingOff {
/// The sink has no SCDC, so it cannot be told to expect scrambling.
pub no_scdc: bool,
/// The sink does not support scrambling
/// ([`SinkTmdsFacts::scrambling_supported`] is clear).
pub no_scrambling_support: bool,
/// The sink does not declare `LTE_340Mcsc_scramble`.
pub sink_does_not_declare: bool,
/// The source cannot scramble at or below 340 Mcsc: it has no scrambler, or cannot use
/// it at low rates.
pub source_cannot: bool,
/// The policy does not permit scrambling at or below the threshold.
pub policy_off: bool,
}
/// The chosen TMDS link: clock ratio, scrambling, and the sink's `TMDS_Config`.
///
/// Built only by [`choose`](Self::choose), so the sink's value always matches the
/// link the source runs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TmdsLinkParams {
rate_khz: u32,
clock_ratio: ClockRatio,
decision: ScramblingDecision,
sink_config: Option<SinkTmdsConfig>,
sink: SinkTmdsFacts,
source: SourceTmdsFacts,
policy: TmdsPolicy,
}
impl TmdsLinkParams {
/// Chooses the link for a TMDS character rate.
///
/// `rate_khz` is the TMDS character rate in kHz, rounded up: truncating would put a
/// rate just above 340 000 kHz on the wrong side of the threshold.
///
/// - Above the policy's threshold, the link is 1/40 and scrambled, on a sink with
/// SCDC and scrambling support.
/// - At or below it, the link is 1/10, and scrambled when the source, the sink
/// (SCDC, scrambling support and `LTE_340Mcsc_scramble`) and the policy all allow
/// it; otherwise every reason is reported.
/// - At exactly 340 000 kHz with [`ScramblingThreshold::AtOrAbove340`], a sink
/// without SCDC or scrambling support, or a source without a scrambler, gets the
/// plain 1/10 link, and the flag that says why.
/// - A source without a scrambler is treated as a sink that cannot take scrambling:
/// refused above 340 000 kHz, unscrambled at or below.
/// - A sink without SCDC gets no `TMDS_Config` value, nor does a plain link on a sink
/// without scrambling support under [`TmdsConfigGate::ScramblingSink`].
///
/// # Errors
///
/// [`ParamsError::AboveSourceMaximum`] for a rate above the source's own maximum,
/// checked first. [`ParamsError::ScramblingUnsupported`] for a rate above 340 000 kHz
/// on a sink without SCDC or scrambling support, or from a source without a scrambler.
///
/// # Example
///
/// ```
/// use spiritlevel::{
/// ClockRatio, ScramblingDecision, SinkTmdsFacts, SourceTmdsFacts, TmdsLinkParams,
/// TmdsPolicy,
/// };
///
/// // 4K at 60 Hz, 8 bpc RGB: 594 MHz.
/// let sink = SinkTmdsFacts::new(true, true, false);
/// let source = SourceTmdsFacts::new(false);
/// let link = TmdsLinkParams::choose(594_000, sink, source, TmdsPolicy::DEFAULT)?;
///
/// assert_eq!(link.clock_ratio(), ClockRatio::Div40);
/// assert_eq!(link.scrambling_decision(), ScramblingDecision::Required);
/// let config = link.sink_config().expect("the sink has SCDC");
/// assert!(config.scrambling_enable && config.high_tmds_clock_ratio);
/// # Ok::<(), spiritlevel::ParamsError>(())
/// ```
pub const fn choose(
rate_khz: u32,
sink: SinkTmdsFacts,
source: SourceTmdsFacts,
policy: TmdsPolicy,
) -> Result<Self, ParamsError> {
if rate_khz > source.max_character_rate_khz {
// Before anything else: a link the source cannot drive gets no sink configuration.
return Err(ParamsError::AboveSourceMaximum {
rate_khz,
max_khz: source.max_character_rate_khz,
});
}
let above_threshold = match policy.threshold {
ScramblingThreshold::Above340 => rate_khz > THRESHOLD_KHZ,
ScramblingThreshold::AtOrAbove340 => rate_khz >= THRESHOLD_KHZ,
};
let sink_can_scramble = sink.scdc_present && sink.scrambling_supported;
if above_threshold && sink_can_scramble && source.scrambling {
return Ok(Self {
rate_khz,
clock_ratio: ClockRatio::Div40,
decision: ScramblingDecision::Required,
sink_config: Some(SinkTmdsConfig::new(true, true)),
sink,
source,
policy,
});
}
if rate_khz > THRESHOLD_KHZ {
// Above the threshold under either setting, and the sink cannot take it or the
// source cannot scramble.
return Err(ParamsError::ScramblingUnsupported {
rate_khz,
no_scdc: !sink.scdc_present,
no_scrambling_support: !sink.scrambling_supported,
source_cannot_scramble: !source.scrambling,
});
}
// At or below the threshold, or exactly 340 000 kHz on a sink without SCDC or
// scrambling support or a source without a scrambler (one of those flags is then
// set).
let off = ScramblingOff {
no_scdc: !sink.scdc_present,
no_scrambling_support: !sink.scrambling_supported,
sink_does_not_declare: !sink.lte_340mcsc_scramble,
source_cannot: !(source.scrambling && source.low_rate_scrambling),
policy_off: !policy.low_rate_scrambling,
};
let scrambled = !(off.no_scdc
|| off.no_scrambling_support
|| off.sink_does_not_declare
|| off.source_cannot
|| off.policy_off);
let decision = if scrambled {
ScramblingDecision::LowRate
} else {
ScramblingDecision::Off(off)
};
// A scrambled link always writes; a plain one as the policy's gate says.
let write_due = sink.scdc_present
&& (scrambled
|| match policy.tmds_config_gate {
TmdsConfigGate::ScramblingSink => sink.scrambling_supported,
_ => true,
});
let sink_config = if write_due {
Some(SinkTmdsConfig::new(scrambled, false))
} else {
None
};
Ok(Self {
rate_khz,
clock_ratio: ClockRatio::Div10,
decision,
sink_config,
sink,
source,
policy,
})
}
/// The TMDS character rate the link was chosen for, in kHz.
#[must_use]
pub const fn rate_khz(&self) -> u32 {
self.rate_khz
}
/// The TMDS bit clock ratio.
#[must_use]
pub const fn clock_ratio(&self) -> ClockRatio {
self.clock_ratio
}
/// Whether the link is scrambled: true for [`ScramblingDecision::Required`] and
/// [`ScramblingDecision::LowRate`].
#[must_use]
pub const fn scrambling(&self) -> bool {
matches!(
self.decision,
ScramblingDecision::Required | ScramblingDecision::LowRate
)
}
/// Why the link is or is not scrambled.
#[must_use]
pub const fn scrambling_decision(&self) -> ScramblingDecision {
self.decision
}
/// What the sink's `TMDS_Config` must hold; `None` exactly when no write is due: a sink
/// without SCDC, or a plain link the policy's [`TmdsConfigGate`] leaves unwritten.
#[must_use]
pub const fn sink_config(&self) -> Option<SinkTmdsConfig> {
self.sink_config
}
/// The sink facts the link was chosen from: what the link assumes about the sink.
#[must_use]
pub const fn sink(&self) -> SinkTmdsFacts {
self.sink
}
/// Whether the link still fits a sink with the facts `sink`: [`choose`](Self::choose),
/// with the source facts and the policy this link was chosen with, gives the same clock
/// ratio, scrambling and `TMDS_Config` value. A fact the link does not depend on (such
/// as [`SinkTmdsFacts::frl_capable`], or `LTE_340Mcsc_scramble` on a 1/40 link) may
/// differ.
#[must_use]
pub fn fits(&self, sink: SinkTmdsFacts) -> bool {
match Self::choose(self.rate_khz, sink, self.source, self.policy) {
Ok(other) => {
other.clock_ratio == self.clock_ratio
&& other.scrambling() == self.scrambling()
&& other.sink_config == self.sink_config
}
Err(_) => false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_link_fits_facts_that_choose_the_same_link() {
let sink = SinkTmdsFacts::new(true, true, false);
let source = SourceTmdsFacts::new(false);
let link = TmdsLinkParams::choose(594_000, sink, source, TmdsPolicy::DEFAULT).unwrap();
assert!(link.fits(sink));
assert!(link.fits(sink.with_frl_capable(false)));
assert!(link.fits(SinkTmdsFacts::new(true, true, true)));
// No scrambling support: refused at this rate.
assert!(!link.fits(SinkTmdsFacts::new(true, false, false)));
// A plain link on a sink that lost SCDC: no write any more.
let plain = TmdsLinkParams::choose(148_500, sink, source, TmdsPolicy::DEFAULT).unwrap();
assert!(!plain.fits(SinkTmdsFacts::NONE));
// The link compares its source facts and policy too.
let other = TmdsLinkParams::choose(
148_500,
sink,
source,
TmdsPolicy::DEFAULT.with_low_rate_scrambling(false),
)
.unwrap();
assert_ne!(plain, other);
}
const RATES: [u32; 9] = [
0,
25_175,
165_000,
339_999,
340_000,
340_001,
594_000,
600_000,
u32::MAX,
];
const THRESHOLDS: [ScramblingThreshold; 2] = [
ScramblingThreshold::Above340,
ScramblingThreshold::AtOrAbove340,
];
const BOOLS: [bool; 2] = [false, true];
const GATES: [TmdsConfigGate; 2] =
[TmdsConfigGate::AnyScdcSink, TmdsConfigGate::ScramblingSink];
/// Calls `f` with every combination of inputs over [`RATES`].
fn for_all(mut f: impl FnMut(u32, SinkTmdsFacts, SourceTmdsFacts, TmdsPolicy)) {
for rate in RATES {
for scdc in BOOLS {
for supported in BOOLS {
for lte in BOOLS {
for scr in BOOLS {
for src in BOOLS {
for pol in BOOLS {
for threshold in THRESHOLDS {
for gate in GATES {
f(
rate,
SinkTmdsFacts::new(scdc, supported, lte),
SourceTmdsFacts::new(src).with_scrambling(scr),
TmdsPolicy::DEFAULT
.with_low_rate_scrambling(pol)
.with_threshold(threshold)
.with_tmds_config_gate(gate),
);
}
}
}
}
}
}
}
}
}
}
fn is_above(rate: u32, threshold: ScramblingThreshold) -> bool {
match threshold {
ScramblingThreshold::Above340 => rate > 340_000,
ScramblingThreshold::AtOrAbove340 => rate >= 340_000,
}
}
const ALL: SinkTmdsFacts = SinkTmdsFacts::new(true, true, true);
const CAN: SourceTmdsFacts = SourceTmdsFacts::new(true);
const AT_OR_ABOVE: TmdsPolicy =
TmdsPolicy::DEFAULT.with_threshold(ScramblingThreshold::AtOrAbove340);
// ---- Invariants over every combination ----
#[test]
fn errors_exactly_above_340_on_an_incapable_sink() {
for_all(|rate, sink, source, policy| {
let incapable = !(sink.scdc_present && sink.scrambling_supported && source.scrambling);
let result = TmdsLinkParams::choose(rate, sink, source, policy);
if rate > 340_000 && incapable {
assert_eq!(
result,
Err(ParamsError::ScramblingUnsupported {
rate_khz: rate,
no_scdc: !sink.scdc_present,
no_scrambling_support: !sink.scrambling_supported,
source_cannot_scramble: !source.scrambling,
})
);
} else {
assert!(result.is_ok(), "{rate} {sink:?} {source:?} {policy:?}");
}
});
}
#[test]
fn link_is_consistent() {
for_all(|rate, sink, source, policy| {
let Ok(link) = TmdsLinkParams::choose(rate, sink, source, policy) else {
return;
};
assert_eq!(link.rate_khz(), rate);
let required = link.scrambling_decision() == ScramblingDecision::Required;
assert_eq!(link.clock_ratio() == ClockRatio::Div40, required);
assert_eq!(
required,
is_above(rate, policy.threshold)
&& sink.scdc_present
&& sink.scrambling_supported
&& source.scrambling
);
assert_eq!(
link.scrambling(),
!matches!(link.scrambling_decision(), ScramblingDecision::Off(_))
);
match link.sink_config() {
// `None` exactly when no write is due.
None => assert!(
!sink.scdc_present
|| (!link.scrambling()
&& policy.tmds_config_gate == TmdsConfigGate::ScramblingSink
&& !sink.scrambling_supported)
),
Some(config) => {
assert!(sink.scdc_present);
assert_eq!(config.scrambling_enable, link.scrambling());
assert_eq!(
config.high_tmds_clock_ratio,
link.clock_ratio() == ClockRatio::Div40
);
}
}
});
}
#[test]
fn every_reason_is_set_exactly_when_its_fact_blocks() {
for_all(|rate, sink, source, policy| {
let Ok(link) = TmdsLinkParams::choose(rate, sink, source, policy) else {
return;
};
if link.scrambling_decision() == ScramblingDecision::Required {
return;
}
let all_allow = sink.scdc_present
&& sink.scrambling_supported
&& sink.lte_340mcsc_scramble
&& source.scrambling
&& source.low_rate_scrambling
&& policy.low_rate_scrambling;
match link.scrambling_decision() {
ScramblingDecision::LowRate => assert!(all_allow),
ScramblingDecision::Off(off) => {
assert!(!all_allow);
assert_eq!(off.no_scdc, !sink.scdc_present);
assert_eq!(off.no_scrambling_support, !sink.scrambling_supported);
assert_eq!(off.sink_does_not_declare, !sink.lte_340mcsc_scramble);
assert_eq!(
off.source_cannot,
!(source.scrambling && source.low_rate_scrambling)
);
assert_eq!(off.policy_off, !policy.low_rate_scrambling);
}
ScramblingDecision::Required => unreachable!(),
}
});
}
// ---- The boundary ----
#[test]
fn exactly_340_000_follows_the_threshold() {
let link = TmdsLinkParams::choose(340_000, ALL, CAN, TmdsPolicy::DEFAULT).unwrap();
assert_eq!(link.clock_ratio(), ClockRatio::Div10);
assert_eq!(link.scrambling_decision(), ScramblingDecision::LowRate);
let link = TmdsLinkParams::choose(340_000, ALL, CAN, AT_OR_ABOVE).unwrap();
assert_eq!(link.clock_ratio(), ClockRatio::Div40);
assert_eq!(link.scrambling_decision(), ScramblingDecision::Required);
}
#[test]
fn rate_340_001_is_above_under_both_thresholds() {
for policy in [TmdsPolicy::DEFAULT, AT_OR_ABOVE] {
let link = TmdsLinkParams::choose(340_001, ALL, CAN, policy).unwrap();
assert_eq!(link.clock_ratio(), ClockRatio::Div40);
assert_eq!(link.sink_config(), Some(SinkTmdsConfig::new(true, true)));
}
}
#[test]
fn exactly_340_000_at_or_above_on_an_incapable_sink_falls_back() {
// No SCDC: the plain 1/10 link, nothing written, and the reason.
let link = TmdsLinkParams::choose(340_000, SinkTmdsFacts::NONE, CAN, AT_OR_ABOVE).unwrap();
assert_eq!(link.clock_ratio(), ClockRatio::Div10);
assert_eq!(link.sink_config(), None);
assert!(matches!(
link.scrambling_decision(),
ScramblingDecision::Off(off) if off.no_scdc
));
// SCDC but no scrambling support, even with LTE_340Mcsc_scramble declared: the
// plain 1/10 link, the ratio cleared, and the reason.
let sink = SinkTmdsFacts::new(true, false, true);
let link = TmdsLinkParams::choose(340_000, sink, CAN, AT_OR_ABOVE).unwrap();
assert_eq!(link.clock_ratio(), ClockRatio::Div10);
assert_eq!(link.sink_config(), Some(SinkTmdsConfig::new(false, false)));
assert_eq!(
link.scrambling_decision(),
ScramblingDecision::Off(ScramblingOff {
no_scdc: false,
no_scrambling_support: true,
sink_does_not_declare: false,
source_cannot: false,
policy_off: false,
})
);
}
#[test]
fn low_rate_scrambling_needs_scrambling_support() {
// LTE_340Mcsc_scramble without a non-zero Max_TMDS_Character_Rate does not count:
// the sources take the low-rate fact from Linux's gated `scrambling.low_rates`.
let sink = SinkTmdsFacts::new(true, false, true);
let link = TmdsLinkParams::choose(297_000, sink, CAN, TmdsPolicy::DEFAULT).unwrap();
assert!(!link.scrambling());
assert_eq!(link.sink_config(), Some(SinkTmdsConfig::new(false, false)));
}
// ---- Named cases ----
#[test]
fn dvi_sink_gets_the_plain_link_with_every_reason() {
let link = TmdsLinkParams::choose(
148_500,
SinkTmdsFacts::NONE,
SourceTmdsFacts::new(false),
TmdsPolicy::DEFAULT.with_low_rate_scrambling(false),
)
.unwrap();
assert_eq!(link.clock_ratio(), ClockRatio::Div10);
assert_eq!(link.sink_config(), None);
assert_eq!(
link.scrambling_decision(),
ScramblingDecision::Off(ScramblingOff {
no_scdc: true,
no_scrambling_support: true,
sink_does_not_declare: true,
source_cannot: true,
policy_off: true,
})
);
}
#[test]
fn scdc_sink_below_threshold_gets_the_ratio_cleared() {
let sink = SinkTmdsFacts::new(true, true, false);
let link = TmdsLinkParams::choose(297_000, sink, CAN, TmdsPolicy::DEFAULT).unwrap();
assert_eq!(link.sink_config(), Some(SinkTmdsConfig::new(false, false)));
}
#[test]
fn low_rate_scrambling_writes_scrambling_enable_only() {
let link = TmdsLinkParams::choose(297_000, ALL, CAN, TmdsPolicy::DEFAULT).unwrap();
assert_eq!(link.scrambling_decision(), ScramblingDecision::LowRate);
assert_eq!(link.sink_config(), Some(SinkTmdsConfig::new(true, false)));
}
// ---- A source without a scrambler ----
const NO_SCRAMBLER: SourceTmdsFacts = SourceTmdsFacts::new(true).with_scrambling(false);
#[test]
fn source_without_scrambler_is_refused_above_340() {
assert_eq!(
TmdsLinkParams::choose(594_000, ALL, NO_SCRAMBLER, TmdsPolicy::DEFAULT),
Err(ParamsError::ScramblingUnsupported {
rate_khz: 594_000,
no_scdc: false,
no_scrambling_support: false,
source_cannot_scramble: true,
})
);
}
#[test]
fn source_without_scrambler_at_340_000_at_or_above_gets_the_plain_link() {
let link = TmdsLinkParams::choose(340_000, ALL, NO_SCRAMBLER, AT_OR_ABOVE).unwrap();
assert_eq!(link.clock_ratio(), ClockRatio::Div10);
assert!(matches!(
link.scrambling_decision(),
ScramblingDecision::Off(ScramblingOff {
source_cannot: true,
no_scdc: false,
no_scrambling_support: false,
..
})
));
}
#[test]
fn low_rate_scrambling_needs_the_scrambler_too() {
let link = TmdsLinkParams::choose(297_000, ALL, NO_SCRAMBLER, TmdsPolicy::DEFAULT).unwrap();
assert!(!link.scrambling());
assert!(matches!(
link.scrambling_decision(),
ScramblingDecision::Off(ScramblingOff {
source_cannot: true,
..
})
));
}
#[test]
fn source_facts_come_from_the_phy_capabilities() {
use hdmi_hal::phy::TmdsPhyCapabilities;
let none = SourceTmdsFacts::from(&TmdsPhyCapabilities::new(600_000));
assert!(!none.scrambling && !none.low_rate_scrambling);
// Low-rate scrambling without a scrambler counts as false.
let odd = SourceTmdsFacts::from(
&TmdsPhyCapabilities::new(600_000).with_low_rate_scrambling(true),
);
assert!(!odd.scrambling && !odd.low_rate_scrambling);
let full = SourceTmdsFacts::from(
&TmdsPhyCapabilities::new(600_000)
.with_scrambling(true)
.with_low_rate_scrambling(true),
);
assert_eq!(
full,
SourceTmdsFacts::new(true).with_max_character_rate_khz(600_000)
);
}
#[test]
fn new_keeps_a_scrambler_as_in_0_1() {
assert!(SourceTmdsFacts::new(false).scrambling);
assert_eq!(SourceTmdsFacts::new(false).max_character_rate_khz, u32::MAX);
}
// ---- The source's own maximum ----
#[test]
fn rate_above_the_source_maximum_is_refused_first() {
let source = CAN.with_max_character_rate_khz(340_000);
assert!(TmdsLinkParams::choose(340_000, ALL, source, TmdsPolicy::DEFAULT).is_ok());
// Refused even where the sink could not take the rate either: the source's limit
// comes first.
assert_eq!(
TmdsLinkParams::choose(594_000, SinkTmdsFacts::NONE, source, TmdsPolicy::DEFAULT),
Err(ParamsError::AboveSourceMaximum {
rate_khz: 594_000,
max_khz: 340_000,
})
);
}
#[test]
fn source_maximum_comes_from_the_phy_capabilities() {
use hdmi_hal::phy::TmdsPhyCapabilities;
let facts = SourceTmdsFacts::from(&TmdsPhyCapabilities::new(297_000));
assert_eq!(facts.max_character_rate_khz, 297_000);
}
#[test]
fn the_gate_decides_a_plain_links_write_on_a_sink_without_scrambling_support() {
// SCDC, but no scrambling support (Max_TMDS_Character_Rate 0).
let sink = SinkTmdsFacts::new(true, false, false);
let any = TmdsLinkParams::choose(148_500, sink, CAN, TmdsPolicy::DEFAULT).unwrap();
assert_eq!(any.sink_config(), Some(SinkTmdsConfig::new(false, false)));
let gated = TmdsPolicy::DEFAULT.with_tmds_config_gate(TmdsConfigGate::ScramblingSink);
let plain = TmdsLinkParams::choose(148_500, sink, CAN, gated).unwrap();
assert_eq!(plain.sink_config(), None);
// A sink that supports scrambling is written under either gate.
let supported = TmdsLinkParams::choose(148_500, ALL, CAN, gated).unwrap();
assert!(supported.sink_config().is_some());
assert_eq!(TmdsConfigGate::default(), TmdsConfigGate::AnyScdcSink);
}
#[test]
fn choose_is_const() {
const LINK: Result<TmdsLinkParams, ParamsError> =
TmdsLinkParams::choose(594_000, ALL, CAN, TmdsPolicy::DEFAULT);
const RATIO_IS_DIV40: bool = match LINK {
Ok(link) => matches!(link.clock_ratio(), ClockRatio::Div40),
Err(_) => false,
};
const { assert!(RATIO_IS_DIV40) };
}
}