use core::time::Duration;
use hdmi_hal::ddc::DdcErrorKind;
use hdmi_hal::time::Timestamp;
use crate::line::{ErrorRates, Line, LinkLanes};
use crate::transcript::{Event, Record, Transcript};
pub trait Target {
fn answers(&self, addr: u8, env: &Env<'_>) -> bool;
fn transaction(
&mut self,
ops: &mut [Operation<'_>],
env: &mut Env<'_>,
) -> Result<(), DdcErrorKind>;
fn update(&mut self, env: &mut Env<'_>) {
let _ = env;
}
}
#[derive(Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Operation<'b> {
Write {
addr: u8,
bytes: &'b [u8],
},
Read {
addr: u8,
buf: &'b mut [u8],
},
}
impl Operation<'_> {
pub fn addr(&self) -> u8 {
match self {
Self::Write { addr, .. } | Self::Read { addr, .. } => *addr,
}
}
pub(crate) fn len(&self) -> usize {
match self {
Self::Write { bytes, .. } => bytes.len(),
Self::Read { buf, .. } => buf.len(),
}
}
}
pub struct Env<'e> {
now: Timestamp,
hpd: bool,
line: Line,
errors: ErrorRates,
lanes: LinkLanes,
byte_time: Duration,
log: &'e mut dyn NoteSink,
}
impl<'e> Env<'e> {
pub(crate) fn new(now: Timestamp, hpd: bool, line: Line, log: &'e mut dyn NoteSink) -> Self {
Self {
now,
hpd,
line,
errors: ErrorRates::NONE,
lanes: LinkLanes::NONE,
byte_time: Duration::ZERO,
log,
}
}
pub(crate) fn with_errors(self, errors: ErrorRates) -> Self {
Self { errors, ..self }
}
pub fn now(&self) -> Timestamp {
self.now
}
pub fn hpd(&self) -> bool {
self.hpd
}
pub fn line(&self) -> Line {
self.line
}
pub(crate) fn with_lanes(self, lanes: LinkLanes) -> Self {
Self { lanes, ..self }
}
pub(crate) fn with_byte_time(self, byte_time: Duration) -> Self {
Self { byte_time, ..self }
}
pub(crate) fn byte_time(&self) -> Duration {
self.byte_time
}
pub fn lane_limits(&self) -> LinkLanes {
self.lanes
}
pub fn character_errors(&self) -> ErrorRates {
self.errors
}
pub(crate) fn record(&mut self, event: Event) {
self.log.note(Record {
at: self.now,
event,
});
}
pub fn note(&mut self, addr: u8, code: u32, value: u32) {
self.log.note(Record {
at: self.now,
event: Event::Note { addr, code, value },
});
}
}
impl core::fmt::Debug for Env<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Env")
.field("now", &self.now)
.field("hpd", &self.hpd)
.field("line", &self.line)
.finish_non_exhaustive()
}
}
pub(crate) trait NoteSink {
fn note(&mut self, record: Record);
}
impl<const LOG: usize> NoteSink for Transcript<LOG> {
fn note(&mut self, record: Record) {
self.push(record);
}
}