Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions conformance/sv-tests/expected.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ chapter-8/8.8--typed_constructor.sv fail
chapter-8/8.8--typed_constructor_param.sv fail
chapter-8/8.9--static_properties.sv fail
chapter-9/9.2.1--initial.sv pass
chapter-9/9.2.2.1--always.sv fail
chapter-9/9.2.2.1--always.sv pass
chapter-9/9.2.2.2--always_comb.sv pass
chapter-9/9.2.2.3--always_latch.sv fail
chapter-9/9.2.2.4--always_ff.sv pass
Expand All @@ -732,8 +732,8 @@ chapter-9/9.4.2--event_control_negedge.sv pass
chapter-9/9.4.2--event_control_posedge.sv pass
chapter-9/9.4.2--event_control_sim.sv fail
chapter-9/9.4.2--event_control_sim_minimal.sv fail
chapter-9/9.4.2.1--event_comma_op.sv fail
chapter-9/9.4.2.1--event_or_op.sv fail
chapter-9/9.4.2.1--event_comma_op.sv pass
chapter-9/9.4.2.1--event_or_op.sv pass
chapter-9/9.4.2.2--event_implicit.sv pass
chapter-9/9.4.2.3--event_conditional.sv fail
chapter-9/9.4.2.4--event_sequence.sv fail
Expand Down
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
6 changes: 6 additions & 0 deletions crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2807,6 +2807,12 @@ fn lower_initial_processes(
.get(&(1, index))
.copied()
.unwrap_or(0x4d59_5df4);
// An `always` process never finishes, so it is never folded into
// initial state.
if process.repeats() {
runtime.push((process, random_seed));
continue;
}
let mut comb = comb::Comb::new(&mut pm, &mut arena);
match comb.lower_initial(process.body(), process.is_initializer(), &seed) {
Ok((written, store)) => {
Expand Down
Loading
Loading