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
7 changes: 7 additions & 0 deletions crates/celox-design/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,12 @@ pub struct EventTopology<A> {
/// a signal needs a settle before its domains evaluate.
#[serde(default)]
pub combinationally_read: BTreeSet<A>,
/// Canonical events whose own domain reads no level of their signal: its
/// flip-flops do not sample the signal as data. With
/// [`Self::combinationally_read`] it says whether a run of the domain
/// observes the signal's level. Absent means it may.
#[serde(default)]
pub level_unread_by_domain: BTreeSet<A>,
}

/// A clock computed combinationally from an event's signal.
Expand Down Expand Up @@ -1149,6 +1155,7 @@ impl<A> Default for EventTopology<A> {
combinational_derived: BTreeMap::new(),
independent_derived: BTreeSet::new(),
combinationally_read: BTreeSet::new(),
level_unread_by_domain: BTreeSet::new(),
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions crates/celox-frontend-core/src/symbolic/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,7 @@ pub fn schedule_symbolic_rtl_reporting_loops(
combinational_derived,
independent_derived,
combinationally_read,
level_unread_by_domain,
} = timed_sub!(
"analyze_clock_dependencies",
analyze_clock_dependencies(
Expand Down Expand Up @@ -1802,6 +1803,7 @@ pub fn schedule_symbolic_rtl_reporting_loops(
.collect(),
independent_derived: independent_derived.into_iter().map(project).collect(),
combinationally_read: combinationally_read.into_iter().map(project).collect(),
level_unread_by_domain: level_unread_by_domain.into_iter().map(project).collect(),
combinational_derived: combinational_derived
.into_iter()
.map(|(event, clocks)| {
Expand Down Expand Up @@ -4629,6 +4631,8 @@ struct ClockAnalysis {
independent_derived: BTreeSet<AbsoluteAddr>,
/// Events whose signal combinational logic reads.
combinationally_read: BTreeSet<AbsoluteAddr>,
/// Events whose own domain does not read their signal.
level_unread_by_domain: BTreeSet<AbsoluteAddr>,
}

fn analyze_clock_dependencies(
Expand Down Expand Up @@ -5023,6 +5027,27 @@ fn analyze_clock_dependencies(
}
Some((reads, writes))
};
// A domain reads its own clock's level when a flip-flop samples it as
// data. An external call may read anything.
let canonical = |address: AbsoluteAddr| clock_domains.get(&address).copied().unwrap_or(address);
let level_unread_by_domain: BTreeSet<AbsoluteAddr> = eval_apply_ffs
.iter()
.filter(|(domain, units)| {
let domain = canonical(**domain);
!units
.iter()
.flat_map(|unit| unit.blocks.values())
.flat_map(|block| &block.instructions)
.any(|instruction| match instruction {
SIRInstruction::Load(_, address, ..) | SIRInstruction::Commit(address, ..) => {
canonical(address.absolute_addr()) == domain
}
SIRInstruction::ExternCall { .. } => true,
_ => false,
})
})
.map(|(domain, _)| canonical(*domain))
.collect();
let mut independent_derived = BTreeSet::new();
let combinationally_read: BTreeSet<AbsoluteAddr> = comb_deps
.values()
Expand Down Expand Up @@ -5070,6 +5095,7 @@ fn analyze_clock_dependencies(
combinational_derived,
independent_derived,
combinationally_read,
level_unread_by_domain,
}
}

Expand Down
206 changes: 201 additions & 5 deletions crates/celox-runtime/src/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//! so the inactive edge of a clock that only clocks flip-flops costs no
//! settle on the next active edge.

use celox_design::{DomainKind, EventTopology, edges::LEVEL_UNKNOWN};
use celox_design::{DomainKind, EventTopology, GateOp, edges::LEVEL_UNKNOWN};

use crate::{
AbsoluteAddr, SignalRef,
Expand Down Expand Up @@ -60,6 +60,15 @@ pub struct EventEdge {
pub kind: Option<DomainKind>,
/// Combinational logic reads the signal.
pub comb_read: bool,
/// A run of the event's own domain can observe the signal's level:
/// combinational logic or one of the domain's flip-flops reads it.
pub level_read: bool,
/// The edge opposite to [`Self::active_edge`] can run a domain: the
/// event's own, or that of a clock computed combinationally from the
/// signal that this edge can move to its active edge. When it cannot,
/// nothing commits on that edge, so no domain clocked from state runs
/// either, and a host only needs to store the level.
pub inactive_runs: bool,
}

impl EventEdge {
Expand Down Expand Up @@ -108,15 +117,130 @@ pub fn build_event_edges<B: SimBackend>(
.iter()
.map(|address| {
let canonical = events.canonical(*address);
EventEdge {
let kind = kinds.get(&canonical).copied();
let comb_read = events.combinationally_read.contains(&canonical);
let mut info = EventEdge {
signal: backend.resolve_signal(&canonical),
kind: kinds.get(&canonical).copied(),
comb_read: events.combinationally_read.contains(&canonical),
}
kind,
comb_read,
level_read: comb_read || !events.level_unread_by_domain.contains(&canonical),
inactive_runs: false,
};
let inactive = info.active_edge().opposite();
info.inactive_runs = info.can_fire(inactive)
|| events
.combinational_derived
.get(&canonical)
.into_iter()
.flatten()
// A clock without a domain of its own runs nothing.
.filter(|clock| backend.resolve_event_opt(&clock.clock).is_some())
.any(|clock| {
let moves = clock
.gate
.as_deref()
.and_then(dependence)
.unwrap_or(Dependence::Mixed);
moves.can_reach(inactive, kinds.get(&clock.clock).copied())
});
info
})
.collect()
}

/// How a gate program's value depends on its source's level, bitwise:
/// whether raising the source can only raise each bit, only lower it, or
/// either. The operations it treats as monotone also are over the order
/// 0 < x < 1 of four-state values, so it holds for four-state designs.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Dependence {
Independent,
Positive,
Negative,
Mixed,
}

impl Dependence {
fn invert(self) -> Self {
match self {
Dependence::Positive => Dependence::Negative,
Dependence::Negative => Dependence::Positive,
other => other,
}
}

/// A monotone function of both operands, such as `&` or `|`.
fn join(self, other: Self) -> Self {
match (self, other) {
(Dependence::Independent, other) | (other, Dependence::Independent) => other,
(lhs, rhs) if lhs == rhs => lhs,
_ => Dependence::Mixed,
}
}

/// A function of the operands that is not monotone, such as `^`.
fn scramble(self, other: Self) -> Self {
if self == Dependence::Independent && other == Dependence::Independent {
Dependence::Independent
} else {
Dependence::Mixed
}
}

/// Whether `edge` of the source can be an edge of a clock of `kind`
/// computed with this dependence.
fn can_reach(self, edge: Edge, kind: Option<DomainKind>) -> bool {
let moves = match self {
Dependence::Independent => return false,
Dependence::Mixed => return true,
Dependence::Positive => edge,
Dependence::Negative => edge.opposite(),
};
match kind {
Some(DomainKind::ClockPosedge | DomainKind::ResetAsyncHigh) => moves == Edge::Rising,
Some(DomainKind::ClockNegedge | DomainKind::ResetAsyncLow) => moves == Edge::Falling,
_ => true,
}
}
}

/// The [`Dependence`] of a gate program's value on its source, or `None`
/// for a malformed program.
fn dependence<A>(ops: &[GateOp<A>]) -> Option<Dependence> {
let mut stack: Vec<Dependence> = Vec::new();
for op in ops {
let value = match op {
GateOp::Source => Dependence::Positive,
GateOp::Load { .. } | GateOp::Const(_) => Dependence::Independent,
GateOp::Not { .. } | GateOp::LogicNot => stack.pop()?.invert(),
GateOp::ReduceAnd { .. } | GateOp::ReduceOr | GateOp::Slice { .. } => stack.pop()?,
GateOp::ReduceXor => stack.pop()?.scramble(Dependence::Independent),
GateOp::And | GateOp::Or | GateOp::LogicAnd | GateOp::LogicOr => {
let rhs = stack.pop()?;
stack.pop()?.join(rhs)
}
GateOp::Xor | GateOp::Eq | GateOp::Ne => {
let rhs = stack.pop()?;
stack.pop()?.scramble(rhs)
}
GateOp::Mux => {
let otherwise = stack.pop()?;
let then = stack.pop()?;
match stack.pop()? {
// A fixed condition selects one of two monotone values.
Dependence::Independent => then.join(otherwise),
_ => Dependence::Mixed,
}
}
};
stack.push(value);
}
match stack.as_slice() {
[value] => Some(*value),
_ => None,
}
}

/// What an edge did.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum EdgeOutcome {
Expand Down Expand Up @@ -174,6 +298,12 @@ pub fn set_level<B: SimBackend>(backend: &mut B, signal: SignalRef, level: u8) {
backend.set(signal, level);
}

/// Whether `info`'s signal is at the known level `edge` leaves it at.
#[inline]
pub fn at_level<B: SimBackend>(backend: &B, info: &EventEdge, edge: Edge) -> bool {
level(backend, info.signal) == edge.level()
}

/// Move `info`'s signal to the level of `edge` through `write`. Returns the
/// level it had (`None` while unknown), or `None` when it already was at
/// that level.
Expand Down Expand Up @@ -398,3 +528,69 @@ where
}
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

type Op = GateOp<()>;

const LOAD: Op = GateOp::Load {
address: (),
lsb: 0,
width: 1,
};

#[test]
fn gates_follow_their_source_through_monotone_operations() {
use Dependence::*;
let cases: [(&[Op], Dependence); 8] = [
(&[GateOp::Source, LOAD, GateOp::And], Positive),
(&[GateOp::Source, GateOp::Not { width: 1 }], Negative),
(
&[GateOp::Source, GateOp::LogicNot, LOAD, GateOp::LogicOr],
Negative,
),
(&[LOAD, GateOp::Const(1), GateOp::Or], Independent),
(&[GateOp::Source, LOAD, GateOp::Xor], Mixed),
(
&[
GateOp::Source,
GateOp::Source,
GateOp::Not { width: 1 },
GateOp::Or,
],
Mixed,
),
(
&[LOAD, GateOp::Source, GateOp::Const(0), GateOp::Mux],
Positive,
),
(
&[GateOp::Source, LOAD, GateOp::Const(0), GateOp::Mux],
Mixed,
),
];
for (ops, expected) in cases {
assert_eq!(dependence(ops), Some(expected), "{ops:?}");
}
assert_eq!(dependence::<()>(&[GateOp::And]), None);
assert_eq!(dependence::<()>(&[GateOp::Source, GateOp::Source]), None);
}

#[test]
fn a_clock_is_reached_by_the_edges_its_dependence_allows() {
use DomainKind::*;
let posedge = Some(ClockPosedge);
let negedge = Some(ClockNegedge);
// The falling edge of a positive-edge clock's source.
let edge = Edge::Falling;
assert!(!Dependence::Positive.can_reach(edge, posedge));
assert!(Dependence::Positive.can_reach(edge, negedge));
assert!(Dependence::Negative.can_reach(edge, posedge));
assert!(!Dependence::Negative.can_reach(edge, Some(ResetAsyncLow)));
assert!(Dependence::Positive.can_reach(edge, None));
assert!(!Dependence::Independent.can_reach(edge, None));
assert!(Dependence::Mixed.can_reach(edge, posedge));
}
}
4 changes: 2 additions & 2 deletions crates/celox-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub use derived_tick::{
EventDomain, Gate, GatedDomain, TickExecutor, build_derived_tick_plans,
};
pub use edge::{
Edge, EdgeOutcome, EventEdge, EventEdges, SignalWrite, build_event_edges, move_signal,
run_edge, run_edges, run_tick, set_level,
Edge, EdgeOutcome, EventEdge, EventEdges, SignalWrite, at_level, build_event_edges,
move_signal, run_edge, run_edges, run_tick, set_level,
};
pub use error::{SimulatorErrorCode, comb_fatal_code, comb_fatal_site};
pub use event_buffer::RuntimeEventBuffer;
Expand Down
Loading
Loading