Skip to content
Merged
100 changes: 80 additions & 20 deletions crates/celox-design/src/edges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,58 @@ pub const fn polarity(kind: DomainKind) -> Option<bool> {
}
}

/// Whether a level change from `was` to `is` that the scheduler applies to
/// an event signal, or that a process drives, is an active edge of a domain
/// of `kind`. A domain of unknown kind reacts to a rising edge.
/// The bit of an edge level that marks an unknown (x or z) value; the value
/// bit then tells x (1) from z (0), as in the state image. An edge level is
/// 0, 1, x (`LEVEL_UNKNOWN | 1`) or z (`LEVEL_UNKNOWN`).
pub const LEVEL_UNKNOWN: u8 = 2;

/// Whether a change between the edge levels `was` and `is` is a rising
/// (`rising`) or a falling edge (IEEE 1800-2023 9.4.2, Table 9-2): a posedge
/// leaves 0 or reaches 1, a negedge leaves 1 or reaches 0, and a change
/// between x and z is neither. With `FOUR_STATE` false the levels are 0 or
/// 1 and this is a rise or a fall.
#[inline]
pub const fn level_edge<const FOUR_STATE: bool>(rising: bool, was: u8, is: u8) -> bool {
let between_unknowns = FOUR_STATE && was & is & LEVEL_UNKNOWN != 0;
let (from, to) = if rising { (0, 1) } else { (1, 0) };
was != to && is != from && !between_unknowns
}

/// Whether a change from edge level `was` to `is` that the scheduler
/// applies to an event signal, or that a process drives, is an active edge
/// of a domain of `kind`. A domain of unknown kind reacts to a rising edge.
///
/// This differs from [`settled_edge`] for domains of unknown kind; the two
/// rules stay separate.
pub const fn scheduled_edge(kind: Option<DomainKind>, was: bool, is: bool) -> bool {
#[inline]
pub const fn scheduled_edge<const FOUR_STATE: bool>(
kind: Option<DomainKind>,
was: u8,
is: u8,
) -> bool {
match kind {
Some(DomainKind::ClockPosedge | DomainKind::ResetAsyncHigh) => !was && is,
Some(DomainKind::ClockNegedge | DomainKind::ResetAsyncLow) => was && !is,
_ => !was && is,
Some(DomainKind::ClockNegedge | DomainKind::ResetAsyncLow) => {
level_edge::<FOUR_STATE>(false, was, is)
}
_ => level_edge::<FOUR_STATE>(true, was, is),
}
}

/// Whether a settled level change from `was` to `is` is an active edge of a
/// domain of `kind`. A domain of unknown kind reacts to any change.
pub const fn settled_edge(kind: Option<DomainKind>, was: bool, is: bool) -> bool {
/// Whether a settled change from edge level `was` to `is` is an active edge
/// of a domain of `kind`. A domain of unknown kind reacts to any change.
#[inline]
pub const fn settled_edge<const FOUR_STATE: bool>(
kind: Option<DomainKind>,
was: u8,
is: u8,
) -> bool {
match kind {
Some(DomainKind::ClockPosedge | DomainKind::ResetAsyncHigh) => !was && is,
Some(DomainKind::ClockNegedge | DomainKind::ResetAsyncLow) => was && !is,
Some(DomainKind::ClockPosedge | DomainKind::ResetAsyncHigh) => {
level_edge::<FOUR_STATE>(true, was, is)
}
Some(DomainKind::ClockNegedge | DomainKind::ResetAsyncLow) => {
level_edge::<FOUR_STATE>(false, was, is)
}
_ => was != is,
}
}
Expand All @@ -54,10 +86,32 @@ mod tests {
#[test]
fn domains_of_unknown_kind_differ_between_the_rules() {
for kind in [None, Some(DomainKind::Other)] {
assert!(scheduled_edge(kind, false, true));
assert!(!scheduled_edge(kind, true, false));
assert!(settled_edge(kind, false, true));
assert!(settled_edge(kind, true, false));
assert!(scheduled_edge::<false>(kind, 0, 1));
assert!(!scheduled_edge::<false>(kind, 1, 0));
assert!(settled_edge::<false>(kind, 0, 1));
assert!(settled_edge::<false>(kind, 1, 0));
}
}

/// IEEE 1800-2023 Table 9-2, rows `from` and columns `to` in the order
/// 0, 1, x, z: `P` posedge, `N` negedge, `-` no edge.
#[test]
fn four_state_levels_follow_table_9_2() {
const LEVELS: [u8; 4] = [0, 1, LEVEL_UNKNOWN | 1, LEVEL_UNKNOWN];
const TABLE: [&str; 4] = ["-PPP", "N-NN", "NP--", "NP--"];
for (from, row) in LEVELS.iter().zip(TABLE) {
for (to, cell) in LEVELS.iter().zip(row.chars()) {
for (kind, mark) in [
(DomainKind::ClockPosedge, 'P'),
(DomainKind::ResetAsyncHigh, 'P'),
(DomainKind::ClockNegedge, 'N'),
(DomainKind::ResetAsyncLow, 'N'),
] {
let edge = cell == mark;
assert_eq!(settled_edge::<true>(Some(kind), *from, *to), edge);
assert_eq!(scheduled_edge::<true>(Some(kind), *from, *to), edge);
}
}
}
}

Expand All @@ -70,10 +124,16 @@ mod tests {
DomainKind::ResetAsyncLow,
] {
let rising = polarity(kind) == Some(true);
for (was, is) in [(false, true), (true, false), (false, false), (true, true)] {
let edge = if rising { !was && is } else { was && !is };
assert_eq!(scheduled_edge(Some(kind), was, is), edge);
assert_eq!(settled_edge(Some(kind), was, is), edge);
for (was, is) in [(0, 1), (1, 0), (0, 0), (1, 1)] {
let edge = if rising {
was == 0 && is == 1
} else {
was == 1 && is == 0
};
assert_eq!(scheduled_edge::<false>(Some(kind), was, is), edge);
assert_eq!(settled_edge::<false>(Some(kind), was, is), edge);
assert_eq!(scheduled_edge::<true>(Some(kind), was, is), edge);
assert_eq!(settled_edge::<true>(Some(kind), was, is), edge);
}
assert_eq!(active_level(Some(kind)), u8::from(rising));
}
Expand Down
Loading
Loading