use core::cell::RefCell;
use core::fmt;
use core::time::Duration;
use critical_section::Mutex;
use hdmi_hal::ddc::{DdcCapabilities, DdcErrorKind, NoAcknowledgeSource};
use hdmi_hal::phy::TmdsPhyCapabilities;
use hdmi_hal::time::{Clock, Delay, Timestamp};
use crate::action::{Action, When};
use crate::clock::SimClock;
use crate::config::SinkConfig;
use crate::ddc::{BusTiming, Ddc};
use crate::eddc::Eddc;
use crate::fault::{Effect, Fault, FaultId, Moment, Schedule, ScheduleFull, Strike};
use crate::hook::ScdcHook;
use crate::line::{ErrorRates, Line, LinkLanes};
use crate::phy::{Phy, PhyCall, PhyErrorKind, PhyFault, PhySettings, PhyState};
use crate::scdc::Scdc;
use crate::target::{Env, Operation, Target};
use crate::transcript::{BusRecord, Event, Record, Transcript};
fn routed<'s>(
targets: &'s mut Targets<'_>,
eddc: &'s mut Eddc<'_>,
scdc: &'s mut Option<Scdc>,
) -> impl Iterator<Item = &'s mut dyn Target> {
targets
.iter_mut()
.map(|target| &mut **target as &mut dyn Target)
.chain(core::iter::once(eddc as &mut dyn Target))
.chain(scdc.as_mut().map(|scdc| scdc as &mut dyn Target))
}
type Targets<'a> = &'a mut [&'a mut (dyn Target + Send)];
const MAX_STORE: usize = 256 * 128;
pub const DEFAULT_BUDGET: u64 = 1_000_000;
pub const DEFAULT_PHY_CAPABILITIES: TmdsPhyCapabilities = TmdsPhyCapabilities::new(600_000)
.with_scrambling(true)
.with_low_rate_scrambling(true);
const fn sink_line(plugged: bool, line: Line) -> Line {
if plugged { line } else { Line::Off }
}
const fn sink_errors(plugged: bool, errors: ErrorRates) -> ErrorRates {
if plugged { errors } else { ErrorRates::NONE }
}
fn check_stores(edid: &[u8], displayid: Option<&[u8]>) -> Result<(), BenchError> {
let whole_blocks = |len: usize| len % 128 == 0 && len <= MAX_STORE;
if !whole_blocks(edid.len()) {
return Err(BenchError::EdidLength(edid.len()));
}
match displayid {
Some(store) if !whole_blocks(store.len()) => Err(BenchError::DisplayIdLength(store.len())),
_ => Ok(()),
}
}
pub struct Bench<'a, const LOG: usize = 256, const FAULTS: usize = 16> {
clock: SimClock,
capabilities: DdcCapabilities,
timing: BusTiming,
budget: Option<u64>,
state: Mutex<RefCell<State<'a, LOG, FAULTS>>>,
}
struct State<'a, const LOG: usize, const FAULTS: usize> {
config: SinkConfig,
eddc: Eddc<'a>,
targets: Targets<'a>,
scdc: Option<Scdc>,
scdc_hook: Option<Hook<'a>>,
line: Line,
errors: ErrorRates,
lanes: LinkLanes,
hpd: bool,
plugged: bool,
attached: bool,
spent: u64,
transcript: Transcript<LOG>,
faults: Schedule<'a, FAULTS>,
armed: Option<FaultId>,
phy: PhyState,
phy_line: Line,
}
impl<'a, const LOG: usize, const FAULTS: usize> State<'a, LOG, FAULTS> {
fn record(&mut self, at: Timestamp, event: Event) {
self.transcript.push(Record { at, event });
}
fn update(&mut self, at: Timestamp) {
let State {
targets,
eddc,
scdc,
transcript,
hpd,
line,
errors,
lanes,
plugged,
..
} = self;
let mut env = Env::new(at, *hpd, sink_line(*plugged, *line), transcript)
.with_errors(sink_errors(*plugged, *errors))
.with_lanes(*lanes);
for target in routed(targets, eddc, scdc) {
target.update(&mut env);
}
self.faults.time_actions(at);
self.run_actions(at, Moment::Time);
}
fn strike(&mut self, at: Timestamp, record: &BusRecord) -> Option<Strike> {
self.faults.time_actions(at);
if self.attached {
self.faults.strike(record, at)
} else {
None
}
}
fn struck_before(
&mut self,
at: Timestamp,
record: BusRecord,
strike: Strike,
kind: DdcErrorKind,
) {
self.record(
at,
Event::Bus(BusRecord {
result: Err(kind),
..record
}),
);
self.record(at, fault_event(strike));
}
fn transaction(
&mut self,
at: Timestamp,
per_byte: Duration,
record: BusRecord,
ops: &mut [Operation<'_>],
fault: Option<Strike>,
) -> (Result<(), DdcErrorKind>, Reach) {
if !self.attached {
let result = Err(DdcErrorKind::Detached);
self.record(at, Event::Bus(BusRecord { result, ..record }));
return (result, Reach::Detached);
}
let index = self.transcript.push(Record {
at,
event: Event::Bus(record),
});
let unanswered = (
Err(DdcErrorKind::NoAcknowledge(NoAcknowledgeSource::Address)),
Reach::Unanswered,
);
let (result, reach) = if self.plugged {
let State {
targets,
eddc,
scdc,
scdc_hook,
transcript,
hpd,
line,
errors,
lanes,
..
} = self;
let addr = ops.first().map_or(0, Operation::addr);
let mut env = Env::new(at, *hpd, *line, transcript)
.with_errors(*errors)
.with_lanes(*lanes)
.with_byte_time(per_byte);
let first = targets
.iter_mut()
.map(|target| &mut **target as &mut dyn Target)
.chain(core::iter::once(eddc as &mut dyn Target))
.find(|t| t.answers(addr, &env));
match (first, scdc) {
(Some(target), _) => (target.transaction(ops, &mut env), Reach::Answered),
(None, Some(scdc)) if scdc.answers(addr, &env) => (
scdc.hooked_transaction(
ops,
&mut env,
scdc_hook
.as_deref_mut()
.map(|hook| hook as &mut (dyn ScdcHook + Send)),
),
Reach::Answered,
),
_ => unanswered,
}
} else {
unanswered
};
let result = match fault {
Some(Strike {
effect: Effect::Fail(kind),
..
}) => Err(kind),
_ => result,
};
self.transcript.set_result(index, result);
if let Some(strike) = fault {
self.record(at, fault_event(strike));
}
(result, reach)
}
fn phy_call(&mut self, call: PhyCall, clock: &SimClock) -> (Result<(), PhyErrorKind>, bool) {
let strike = self.faults.strike_phy(&call, clock.now());
self.faults.phy_actions(&call);
let was_running = self.phy.is_running();
let (result, took_effect) = match strike {
Some((_, false)) => (Err(PhyErrorKind::Fault), false),
Some((_, true)) => match self.phy.apply(call) {
Ok(()) => (Err(PhyErrorKind::Fault), true),
Err(kind) => (Err(kind), false),
},
None => (self.phy.apply(call), false),
};
if !was_running && self.phy.is_running() {
clock.advance(self.phy.start_latency());
}
let at = clock.now();
self.record(at, Event::Phy { call, result });
let line = self.phy.line();
if line != self.phy_line {
self.phy_line = line;
if line != self.line {
self.line = line;
self.record(at, Event::Line(line));
}
}
self.update(at);
self.run_actions(at, Moment::After);
(result, took_effect)
}
fn unplug(&mut self, at: Timestamp) {
if !self.plugged {
return;
}
self.plugged = false;
self.record(at, Event::Plug { plugged: false });
if self.hpd {
self.hpd = false;
self.record(at, Event::Hpd { high: false });
}
self.update(at);
}
fn plug(
&mut self,
at: Timestamp,
edid: &'a [u8],
displayid: Option<&'a [u8]>,
config: SinkConfig,
) {
self.unplug(at);
self.config = config;
self.eddc = Eddc::new(&config, edid, displayid);
self.scdc = Scdc::new(&config);
self.plugged = true;
self.record(at, Event::Plug { plugged: true });
self.hpd = true;
self.record(at, Event::Hpd { high: true });
self.update(at);
}
fn set_line(&mut self, at: Timestamp, line: Line) {
if self.line != line {
self.line = line;
self.record(at, Event::Line(line));
self.update(at);
}
}
fn set_character_errors(&mut self, at: Timestamp, errors: ErrorRates) {
if self.errors != errors {
self.errors = errors;
self.record(at, Event::CharacterErrors(errors));
self.update(at);
}
}
fn set_lane_limits(&mut self, at: Timestamp, lanes: LinkLanes) {
if self.lanes != lanes {
self.lanes = lanes;
self.record(at, Event::LaneLimits(lanes));
self.update(at);
}
}
fn set_hpd(&mut self, at: Timestamp, high: bool) -> bool {
if high && !self.plugged {
return false;
}
if self.hpd != high {
self.hpd = high;
self.record(at, Event::Hpd { high });
self.update(at);
}
true
}
fn set_attached(&mut self, at: Timestamp, attached: bool) {
if self.attached != attached {
self.attached = attached;
self.record(at, Event::Adapter { attached });
self.update(at);
}
}
fn preset(&mut self, at: Timestamp, reg: u8, value: u8) -> bool {
let State {
scdc,
transcript,
hpd,
line,
errors,
lanes,
plugged,
..
} = self;
let Some(scdc) = scdc else {
return false;
};
let line = sink_line(*plugged, *line);
let mut env = Env::new(at, *hpd, line, transcript)
.with_errors(sink_errors(*plugged, *errors))
.with_lanes(*lanes);
scdc.preset(reg, value, &mut env);
self.update(at);
true
}
fn run_actions(&mut self, at: Timestamp, moment: Moment) {
while let Some((id, action)) = self.faults.take_due(moment) {
self.record(at, Event::Action { id });
match action {
Action::Preset { reg, value } => {
self.preset(at, reg, value);
}
Action::Hpd(high) => {
self.set_hpd(at, high);
}
Action::Unplug => self.unplug(at),
Action::Plug {
edid,
displayid,
config,
} => self.plug(at, edid, displayid, config),
Action::Line(line) => self.set_line(at, line),
Action::CharacterErrors(errors) => self.set_character_errors(at, errors),
Action::LaneLimits(lanes) => self.set_lane_limits(at, lanes),
Action::Detach => self.set_attached(at, false),
Action::Reattach => self.set_attached(at, true),
}
}
}
}
fn fault_event(strike: Strike) -> Event {
Event::Fault {
id: strike.id,
effect: strike.effect,
point: strike.point,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Reach {
Answered,
Unanswered,
Detached,
}
impl<'a> Bench<'a> {
pub fn new(config: SinkConfig, edid: &'a [u8]) -> Result<Self, BenchError> {
Self::builder(edid).sink(config).build()
}
pub fn builder(edid: &'a [u8]) -> BenchBuilder<'a> {
BenchBuilder {
edid,
displayid: None,
config: SinkConfig::new(),
capabilities: DdcCapabilities::general(),
timing: BusTiming::STANDARD_MODE,
clock: SimClock::new(),
budget: Some(DEFAULT_BUDGET),
targets: Default::default(),
scdc_hook: None,
phy_capabilities: DEFAULT_PHY_CAPABILITIES,
phy_settings: PhySettings::DEFAULT,
keep_newest: false,
}
}
}
impl<'a, const LOG: usize, const FAULTS: usize> Bench<'a, LOG, FAULTS> {
fn locked<R>(&self, f: impl FnOnce(&mut State<'a, LOG, FAULTS>, Timestamp) -> R) -> R {
critical_section::with(|cs| {
let Ok(mut state) = self.state.borrow(cs).try_borrow_mut() else {
panic!(
"the bench is locked: a target or hook used the bench's handles or \
controls while the bench ran it (use Env instead)"
);
};
f(&mut state, self.clock.now())
})
}
pub fn ddc(&self) -> Ddc<'_> {
Ddc::new(self)
}
pub fn phy(&self) -> Phy<'_> {
Phy::new(self)
}
pub fn time(&self) -> Time<'_> {
Time { core: self }
}
pub fn sink(&self) -> Sink<'_> {
Sink { core: self }
}
pub fn advance(&self, duration: Duration) {
self.clock.advance(duration);
self.locked(|state, at| state.update(at));
}
pub fn now(&self) -> Timestamp {
self.clock.now()
}
pub fn set_line(&self, line: Line) {
self.locked(|state, at| state.set_line(at, line));
}
pub fn line(&self) -> Line {
self.locked(|state, _| state.line)
}
pub fn set_character_errors(&self, errors: ErrorRates) {
self.locked(|state, at| state.set_character_errors(at, errors));
}
pub fn character_errors(&self) -> ErrorRates {
self.locked(|state, _| state.errors)
}
pub fn set_lane_limits(&self, lanes: LinkLanes) {
self.locked(|state, at| state.set_lane_limits(at, lanes));
}
pub fn lane_limits(&self) -> LinkLanes {
self.locked(|state, _| state.lanes)
}
pub fn set_hpd(&self, high: bool) {
let unplugged = !self.locked(|state, at| state.set_hpd(at, high));
if unplugged {
panic!("HPD raised on an unplugged sink: plug it first");
}
}
pub fn hpd_pulse(&self, low_for: Duration) {
self.set_hpd(false);
self.advance(low_for);
self.locked(|state, at| state.set_hpd(at, true));
}
pub fn unplug(&self) {
self.locked(|state, at| state.unplug(at));
}
pub fn plug(
&self,
edid: &'a [u8],
displayid: Option<&'a [u8]>,
config: SinkConfig,
) -> Result<(), BenchError> {
check_stores(edid, displayid)?;
self.locked(|state, at| state.plug(at, edid, displayid, config));
Ok(())
}
pub fn detach(&self) {
self.set_attached(false);
}
pub fn reattach(&self) {
self.set_attached(true);
}
fn set_attached(&self, attached: bool) {
self.locked(|state, at| state.set_attached(at, attached));
}
pub fn is_attached(&self) -> bool {
self.locked(|state, _| state.attached)
}
pub fn operations(&self) -> u64 {
self.locked(|state, _| state.spent)
}
pub fn schedule(&self, fault: Fault) -> Result<FaultId, ScheduleFull> {
self.locked(|state, _| state.faults.add(fault))
}
pub fn schedule_phy(&self, fault: PhyFault) -> Result<FaultId, ScheduleFull> {
self.locked(|state, _| state.faults.add_phy(fault))
}
pub fn schedule_action(&self, action: Action<'a>, when: When) -> Result<FaultId, ScheduleFull> {
if let Action::Plug {
edid, displayid, ..
} = action
{
if let Err(error) = check_stores(edid, displayid) {
panic!("a scheduled plug's stores: {error}");
}
}
self.locked(|state, _| state.faults.add_action(action, when))
}
pub fn clear_fault(&self, id: FaultId) -> bool {
self.locked(|state, _| state.faults.remove(id))
}
pub(crate) fn arm(&self, fault: Fault) -> bool {
self.locked(|state, _| {
if let Some(id) = state.armed.take() {
state.faults.remove(id);
}
state.armed = state.faults.add(fault).ok();
state.armed.is_some()
})
}
pub(crate) fn withdraw_armed(&self) {
self.locked(|state, _| {
if let Some(id) = state.armed.take() {
state.faults.remove(id);
}
});
}
pub fn clear_faults(&self) {
self.locked(|state, _| state.faults.clear());
}
pub fn scheduled_faults(&self) -> usize {
self.locked(|state, _| state.faults.len())
}
pub fn preset_scdc(&self, reg: u8, value: u8) {
let missing = !self.locked(|state, at| state.preset(at, reg, value));
if missing {
panic!("preset of SCDC register {reg:#04x} on a sink without SCDC");
}
}
pub fn transcript(&self) -> impl Iterator<Item = Record> + '_ {
let mut index = 0;
core::iter::from_fn(move || {
let record = self.locked(|state, _| state.transcript.get(index));
index += 1;
record
})
}
pub fn transcript_len(&self) -> usize {
self.locked(|state, _| state.transcript.len())
}
pub fn transcript_overflow(&self) -> usize {
self.locked(|state, _| state.transcript.overflow())
}
pub fn clear_transcript(&self) {
self.locked(|state, _| state.transcript.clear());
}
}
impl<const LOG: usize, const FAULTS: usize> fmt::Debug for Bench<'_, LOG, FAULTS> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.locked(|state, now| {
f.debug_struct("Bench")
.field("now", &now)
.field("config", &state.config)
.field("edid_len", &state.eddc.edid_len())
.field("displayid_len", &state.eddc.displayid_len())
.field("targets", &state.targets.len())
.field("scdc", &state.scdc.is_some())
.field("line", &state.line)
.field("hpd", &state.hpd)
.field("plugged", &state.plugged)
.field("attached", &state.attached)
.field("operations", &state.spent)
.field("faults", &state.faults.len())
.field("phy", &state.phy)
.field("budget", &self.budget)
.field("transcript_len", &state.transcript.len())
.field("transcript_overflow", &state.transcript.overflow())
.finish_non_exhaustive()
})
}
}
pub(crate) trait Core {
fn serialized(&self, f: &mut dyn FnMut());
fn capabilities(&self) -> DdcCapabilities;
fn timing(&self) -> BusTiming;
fn clock(&self) -> &SimClock;
fn spend(&self);
fn strike(&self, record: &BusRecord) -> Option<Strike>;
fn struck_before(&self, record: BusRecord, strike: Strike, kind: DdcErrorKind);
fn transaction(
&self,
record: BusRecord,
ops: &mut [Operation<'_>],
fault: Option<Strike>,
) -> (Result<(), DdcErrorKind>, Reach);
fn record(&self, record: BusRecord);
fn run_actions(&self, before: bool);
fn after_delay(&self);
fn scdc(&self, reg: u8) -> Option<u8>;
fn hpd(&self) -> (bool, bool);
fn phy_capabilities(&self) -> TmdsPhyCapabilities;
fn phy_call(&self, call: PhyCall) -> (Result<(), PhyErrorKind>, bool);
fn coupled_call(&self, call: PhyCall, result: Result<(), PhyErrorKind>, line: Option<Line>);
}
impl<const LOG: usize, const FAULTS: usize> Core for Bench<'_, LOG, FAULTS> {
fn capabilities(&self) -> DdcCapabilities {
self.capabilities
}
fn serialized(&self, f: &mut dyn FnMut()) {
critical_section::with(|_| f());
}
fn timing(&self) -> BusTiming {
self.timing
}
fn clock(&self) -> &SimClock {
&self.clock
}
fn spend(&self) {
let over = self.locked(|state, _| {
state.spent = state.spent.saturating_add(1);
self.budget.filter(|&budget| state.spent > budget)
});
if let Some(budget) = over {
panic!(
"the bench's operation budget of {budget} operations is spent: the code under \
test may be looping (BenchBuilder::budget sets the budget)"
);
}
}
fn strike(&self, record: &BusRecord) -> Option<Strike> {
self.locked(|state, at| state.strike(at, record))
}
fn struck_before(&self, record: BusRecord, strike: Strike, kind: DdcErrorKind) {
self.locked(|state, at| state.struck_before(at, record, strike, kind));
}
fn transaction(
&self,
record: BusRecord,
ops: &mut [Operation<'_>],
fault: Option<Strike>,
) -> (Result<(), DdcErrorKind>, Reach) {
let per_byte = self.timing.per_byte;
self.locked(|state, at| state.transaction(at, per_byte, record, ops, fault))
}
fn record(&self, record: BusRecord) {
self.locked(|state, at| state.record(at, Event::Bus(record)));
}
fn run_actions(&self, before: bool) {
let moment = if before {
Moment::Before
} else {
Moment::After
};
self.locked(|state, at| state.run_actions(at, moment));
}
fn after_delay(&self) {
self.locked(|state, at| state.update(at));
}
fn scdc(&self, reg: u8) -> Option<u8> {
self.locked(|state, _| state.scdc.as_ref().map(|scdc| scdc.get(reg)))
}
fn hpd(&self) -> (bool, bool) {
self.locked(|state, _| (state.hpd, state.plugged))
}
fn phy_capabilities(&self) -> TmdsPhyCapabilities {
self.locked(|state, _| state.phy.capabilities())
}
fn phy_call(&self, call: PhyCall) -> (Result<(), PhyErrorKind>, bool) {
self.locked(|state, _| state.phy_call(call, &self.clock))
}
fn coupled_call(&self, call: PhyCall, result: Result<(), PhyErrorKind>, line: Option<Line>) {
self.locked(|state, at| {
state.faults.phy_actions(&call);
state.record(at, Event::Phy { call, result });
if let Some(line) = line {
if line != state.line {
state.line = line;
state.record(at, Event::Line(line));
}
}
state.update(at);
state.run_actions(at, Moment::After);
});
}
}
#[must_use]
pub struct BenchBuilder<'a, const LOG: usize = 256, const FAULTS: usize = 16> {
edid: &'a [u8],
displayid: Option<&'a [u8]>,
config: SinkConfig,
capabilities: DdcCapabilities,
timing: BusTiming,
clock: SimClock,
budget: Option<u64>,
targets: Targets<'a>,
scdc_hook: Option<Hook<'a>>,
phy_capabilities: TmdsPhyCapabilities,
phy_settings: PhySettings,
keep_newest: bool,
}
type Hook<'a> = &'a mut (dyn ScdcHook + Send);
impl<'a, const LOG: usize, const FAULTS: usize> BenchBuilder<'a, LOG, FAULTS> {
pub fn sink(self, config: SinkConfig) -> Self {
Self { config, ..self }
}
pub fn displayid(self, store: &'a [u8]) -> Self {
Self {
displayid: Some(store),
..self
}
}
pub fn ddc_capabilities(self, capabilities: DdcCapabilities) -> Self {
Self {
capabilities,
..self
}
}
pub fn clock(self, clock: SimClock) -> Self {
Self { clock, ..self }
}
pub fn bus_timing(self, timing: BusTiming) -> Self {
Self { timing, ..self }
}
pub fn budget(self, budget: Option<u64>) -> Self {
Self { budget, ..self }
}
pub fn phy_capabilities(self, phy_capabilities: TmdsPhyCapabilities) -> Self {
Self {
phy_capabilities,
..self
}
}
pub fn phy(self, phy_settings: PhySettings) -> Self {
Self {
phy_settings,
..self
}
}
pub fn targets(self, targets: &'a mut [&'a mut (dyn Target + Send)]) -> Self {
Self { targets, ..self }
}
pub fn keep_newest_records(self) -> Self {
Self {
keep_newest: true,
..self
}
}
pub fn scdc_hook(self, hook: &'a mut (dyn ScdcHook + Send)) -> Self {
Self {
scdc_hook: Some(hook),
..self
}
}
pub fn capacities<const L: usize, const F: usize>(self) -> BenchBuilder<'a, L, F> {
BenchBuilder {
edid: self.edid,
displayid: self.displayid,
config: self.config,
capabilities: self.capabilities,
timing: self.timing,
clock: self.clock,
budget: self.budget,
targets: self.targets,
scdc_hook: self.scdc_hook,
phy_capabilities: self.phy_capabilities,
phy_settings: self.phy_settings,
keep_newest: self.keep_newest,
}
}
pub fn build(self) -> Result<Bench<'a, LOG, FAULTS>, BenchError> {
check_stores(self.edid, self.displayid)?;
Ok(Bench {
clock: self.clock,
capabilities: self.capabilities,
timing: self.timing,
budget: self.budget,
state: Mutex::new(RefCell::new(State {
config: self.config,
eddc: Eddc::new(&self.config, self.edid, self.displayid),
scdc: Scdc::new(&self.config),
targets: self.targets,
scdc_hook: self.scdc_hook,
line: Line::Off,
errors: ErrorRates::NONE,
lanes: LinkLanes::NONE,
hpd: true,
plugged: true,
attached: true,
spent: 0,
transcript: if self.keep_newest {
Transcript::new().keeping_newest()
} else {
Transcript::new()
},
faults: Schedule::new(),
armed: None,
phy: PhyState::new(self.phy_settings, self.phy_capabilities),
phy_line: Line::Off,
})),
})
}
}
impl<const LOG: usize, const FAULTS: usize> fmt::Debug for BenchBuilder<'_, LOG, FAULTS> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BenchBuilder")
.field("edid_len", &self.edid.len())
.field("displayid_len", &self.displayid.map(<[u8]>::len))
.field("config", &self.config)
.field("capabilities", &self.capabilities)
.field("timing", &self.timing)
.field("clock", &self.clock)
.field("budget", &self.budget)
.field("targets", &self.targets.len())
.field("scdc_hook", &self.scdc_hook.is_some())
.field("phy_capabilities", &self.phy_capabilities)
.field("phy_settings", &self.phy_settings)
.field("keep_newest", &self.keep_newest)
.finish()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum BenchError {
EdidLength(usize),
DisplayIdLength(usize),
}
impl fmt::Display for BenchError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (store, len) = match self {
Self::EdidLength(len) => ("EDID", len),
Self::DisplayIdLength(len) => ("DisplayID", len),
};
write!(
f,
"an {store} store of {len} bytes: a store is a whole number of 128-byte blocks, \
at most 256 blocks"
)
}
}
impl core::error::Error for BenchError {}
#[derive(Clone, Copy)]
pub struct Time<'b> {
core: &'b (dyn Core + Sync + 'b),
}
impl Clock for Time<'_> {
fn now(&self) -> Timestamp {
self.core.spend();
self.core.clock().now()
}
}
impl Delay for Time<'_> {
fn delay(&mut self, duration: Duration) {
self.core.spend();
let core = self.core;
core.serialized(&mut || {
let mut clock = core.clock();
Delay::delay(&mut clock, duration);
core.after_delay();
});
}
}
impl fmt::Debug for Time<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Time")
.field("clock", self.core.clock())
.finish()
}
}
#[derive(Clone, Copy)]
pub struct Sink<'b> {
core: &'b (dyn Core + Sync + 'b),
}
impl Sink<'_> {
pub fn hpd(&self) -> bool {
self.core.hpd().0
}
pub fn is_plugged(&self) -> bool {
self.core.hpd().1
}
pub fn scdc(&self, reg: u8) -> Option<u8> {
self.core.scdc(reg)
}
pub fn tmds_config(&self) -> Option<u8> {
self.scdc(crate::scdc::TMDS_CONFIG)
}
pub fn scrambling_status(&self) -> Option<u8> {
self.scdc(crate::scdc::SCRAMBLING_STATUS)
}
pub fn sink_version(&self) -> Option<u8> {
self.scdc(crate::scdc::SINK_VERSION)
}
pub fn source_version(&self) -> Option<u8> {
self.scdc(crate::scdc::SOURCE_VERSION)
}
pub fn update_0(&self) -> Option<u8> {
self.scdc(crate::scdc::UPDATE_0)
}
pub fn status_flags_0(&self) -> Option<u8> {
self.scdc(crate::scdc::STATUS_FLAGS_0)
}
pub fn config_0(&self) -> Option<u8> {
self.scdc(crate::scdc::CONFIG_0)
}
pub fn config_1(&self) -> Option<u8> {
self.scdc(crate::scdc::CONFIG_1)
}
}
impl fmt::Debug for Sink<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Sink")
.field("tmds_config", &self.tmds_config())
.field("scrambling_status", &self.scrambling_status())
.finish_non_exhaustive()
}
}