diff --git a/crates/celox/src/derived_edge.rs b/crates/celox/src/derived_edge.rs new file mode 100644 index 00000000000..2debba1c98c --- /dev/null +++ b/crates/celox/src/derived_edge.rs @@ -0,0 +1,934 @@ +//! Derived clock domains of an event edge as SIR. +//! +//! An edge of an event's signal runs the event's own domain and the domains +//! of the clocks derived from it, with the rules of `celox_runtime`'s +//! `DerivedTickPlan`: clocks computed combinationally from the signal +//! (`clk & en`, `~clk`) are judged from their gate programs at the signal's +//! old and new level and evaluate together with the event's domain; after +//! the commit and a settle, the domains whose clocks moved to their active +//! edge run together, in rounds, until none does, each at most once. +//! +//! [`EdgePlan`] is that plan over the program's units, and +//! [`EdgePlan::emit`] emits one edge as SIR, so that a host can run it +//! without walking the plan in Rust. It covers the plans the Rust walk runs +//! without settling for gates: two-state designs whose gated clocks all have +//! gate programs. + +use celox_design::{DomainKind, GateOp, STABLE_REGION, schedule::ScheduleModel}; +use celox_sir::{BinaryOp, BlockId, SIRBuilder, SIRTerminator, SIRValue, UnaryOp}; + +use crate::ir::{ + AbsoluteAddr, ExecutionUnit, LaidOutProgram, RegionedAbsoluteAddr, RegisterId, SIRInstruction, + SIROffset, +}; + +type Unit = ExecutionUnit; +type Builder = SIRBuilder; + +/// Width of the values a gate program computes with. +const GATE_WIDTH: usize = 64; + +/// The deepest stack a gate program may use, as the Rust walk allows. +const GATE_STACK: usize = 16; + +/// The most domains one edge can fire: one bit each in a fire mask. +const MAX_ENTRIES: usize = 64; + +/// The units of one event domain. +#[derive(Clone, Debug)] +pub(crate) struct DomainUnits { + eval_apply: Vec, + /// Evaluation without commit; without it, the domain evaluates and + /// commits at once when it fires with others, as the Rust walk does. + eval_only: Option>, + apply: Option>, +} + +impl DomainUnits { + fn of(program: &LaidOutProgram, address: AbsoluteAddr) -> Self { + let sir = &program.sir; + Self { + eval_apply: sir + .eval_apply_ffs + .get(&address) + .cloned() + .unwrap_or_default(), + eval_only: sir.eval_only_ffs.get(&address).cloned(), + apply: sir.apply_ffs.get(&address).cloned(), + } + } +} + +/// A clock whose edges an edge of the event can cause. +#[derive(Clone, Debug)] +struct Derived { + signal: AbsoluteAddr, + width: usize, + kind: Option, + units: DomainUnits, +} + +/// A clock computed combinationally from the event's signal. +#[derive(Clone, Debug)] +struct Gated { + derived: Derived, + gate: Vec>, +} + +/// The derived domains an edge of one event runs. +#[derive(Clone, Debug)] +pub(crate) struct EdgePlan { + /// The event's own domain. + source: DomainUnits, + /// Domains clocked from state the event's domain writes. + domains: Vec, + /// Clocks computed combinationally from the event's signal. + gated: Vec, + /// The gated domains read and write no state the event's domain writes, + /// nor one another's: at the edge they run first, one at a time. + independent: bool, +} + +impl EdgePlan { + /// The plan of the canonical event `event`: `Ok(None)` when no clock + /// derives from it, an error when the plan is one this module does not + /// emit. + pub(crate) fn new( + program: &LaidOutProgram, + model: &ScheduleModel, + event: AbsoluteAddr, + ) -> Result, &'static str> { + let events = &model.events; + let domains = events + .derived_events + .get(&event) + .map_or(&[][..], Vec::as_slice); + let gated = events + .combinational_derived + .get(&event) + .map_or(&[][..], Vec::as_slice); + if domains.is_empty() && gated.is_empty() { + return Ok(None); + } + let layout = program.layout(); + if layout.four_state { + return Err("the design is four-state"); + } + if domains.len() + gated.len() + 1 > MAX_ENTRIES { + return Err("an edge reaches too many derived domains"); + } + // A derived clock without a domain of its own is left out, as the + // Rust walk leaves out a clock without an event. + let derived = |address: AbsoluteAddr| -> Result, &'static str> { + if !program.sir.eval_apply_ffs.contains_key(&address) { + return Ok(None); + } + Ok(Some(Derived { + signal: address, + width: *layout + .widths + .get(&address) + .ok_or("a derived clock has no storage")?, + kind: model.kind(&address), + units: DomainUnits::of(program, address), + })) + }; + let loadable = |address: &AbsoluteAddr, lsb: u32, width: u32| { + layout + .widths + .get(address) + .is_some_and(|&stored| stored <= GATE_WIDTH && (lsb + width) as usize <= stored) + && !layout.unpacked_arrays.contains_key(address) + }; + let mut gated_domains = Vec::with_capacity(gated.len()); + for clock in gated { + let Some(derived) = derived(clock.clock)? else { + continue; + }; + let gate = clock + .gate + .clone() + .ok_or("a gated clock has no gate program")?; + if !gate_fits(&gate, loadable) { + return Err("a gated clock has no gate program"); + } + gated_domains.push(Gated { derived, gate }); + } + let mut state_domains = Vec::with_capacity(domains.len()); + for &address in domains { + if let Some(derived) = derived(address)? { + state_domains.push(derived); + } + } + if state_domains.is_empty() && gated_domains.is_empty() { + return Ok(None); + } + Ok(Some(Self { + source: DomainUnits::of(program, event), + domains: state_domains, + gated: gated_domains, + independent: events.independent_derived.contains(&event), + })) + } + + /// The entries of a fire mask: the event's domain, then the + /// state-derived domains, then the gated ones. + fn entries(&self) -> impl Iterator + '_ { + std::iter::once(&self.source) + .chain(self.domains.iter().map(|domain| &domain.units)) + .chain(self.gated.iter().map(|gated| &gated.derived.units)) + } + + fn gated_bit(&self, index: usize) -> usize { + 1 + self.domains.len() + index + } + + /// Emit one edge of the event's signal, which has already moved from + /// level `from` to `to` (one-bit registers). `fires` says whether the + /// event's own domain sees its active edge, `settle` whether + /// combinational logic must settle before the domains evaluate. The code + /// continues at `settled` after the domains ran and logic settled, and + /// at `unsettled` when nothing ran (no gated clock and no own edge). + #[allow(clippy::too_many_arguments)] + pub(crate) fn emit( + &self, + b: &mut Builder, + comb: &[Unit], + from: RegisterId, + to: RegisterId, + fires: RegisterId, + settle: RegisterId, + settled: BlockId, + unsettled: BlockId, + ) { + let mut e = Emitter { + b, + after: Vec::new(), + }; + // The state-derived clocks' levels before the edge, the baseline of + // their edges for the whole phase. + let was: Vec = self + .domains + .iter() + .map(|domain| e.level(domain.signal, domain.width)) + .collect(); + // The fire section's parameters: the domains to fire, those fired so + // far, whether to settle first, and whether the gated domains run + // first one at a time (`fire_with_gated`). + let mask = e.b.alloc_bit(MAX_ENTRIES, false); + let fired = e.b.alloc_bit(MAX_ENTRIES, false); + let settle_first = e.b.alloc_bit(1, false); + let alone = e.b.alloc_bit(1, false); + let fire = e.b.new_block_with(vec![mask, fired, settle_first, alone]); + let source = e.bit(fires, 0); + if self.gated.is_empty() { + let zero = e.constant(0, MAX_ENTRIES); + let none = e.binary(1, source, BinaryOp::Eq, zero); + let no = e.constant(0, 1); + let start = e.b.new_block(); + e.b.seal_block(SIRTerminator::Branch { + cond: none, + true_block: (unsettled, Vec::new()), + false_block: (start, Vec::new()), + }); + e.b.switch_to_block(start); + e.b.seal_block(SIRTerminator::Jump(fire, vec![source, zero, settle, no])); + } else { + // The edge: each gated clock judged from its gate program with + // the signal at its old and new level. + let mut ready = source; + let mut gated_fired = e.constant(0, MAX_ENTRIES); + let mut after = Vec::with_capacity(self.gated.len()); + for (index, gated) in self.gated.iter().enumerate() { + let before = e.gate(&gated.gate, from); + let level = e.gate(&gated.gate, to); + let edge = e.edge(gated.derived.kind, before, level); + let bit = e.bit(edge, self.gated_bit(index)); + ready = e.binary(MAX_ENTRIES, ready, BinaryOp::Or, bit); + gated_fired = e.binary(MAX_ENTRIES, gated_fired, BinaryOp::Or, bit); + after.push(level); + } + let alone = e.constant(u64::from(self.independent), 1); + e.b.seal_block(SIRTerminator::Jump( + fire, + vec![ready, gated_fired, settle, alone], + )); + e.after = after; + } + + // The fire section, shared by the edge and the rounds. + e.b.switch_to_block(fire); + let fire_end = e.b.new_block(); + e.settle_if(settle_first, comb); + let zero = e.constant(0, MAX_ENTRIES); + let gated_mask = self + .gated + .iter() + .enumerate() + .fold(0u64, |mask, (index, _)| mask | 1 << self.gated_bit(index)); + let gated_bits = e.constant(gated_mask, MAX_ENTRIES); + let gated_ready = e.binary(MAX_ENTRIES, mask, BinaryOp::And, gated_bits); + let any_gated = e.binary(1, gated_ready, BinaryOp::Ne, zero); + let in_turn = e.binary(1, alone, BinaryOp::And, any_gated); + let one_at_a_time = e.b.new_block(); + let together = e.b.new_block(); + e.b.seal_block(SIRTerminator::Branch { + cond: in_turn, + true_block: (one_at_a_time, Vec::new()), + false_block: (together, Vec::new()), + }); + + // Independent gated domains, then the event's own, each fused. + e.b.switch_to_block(one_at_a_time); + let order: Vec = (0..self.gated.len()) + .map(|index| self.gated_bit(index)) + .chain(std::iter::once(0)) + .collect(); + for entry in order { + let units = self.entries().nth(entry).expect("an entry of the mask"); + e.if_bit(mask, entry, |e| e.inline_all(&units.eval_apply)); + } + e.b.seal_block(SIRTerminator::Jump(fire_end, Vec::new())); + + // The domains together: one evaluates and commits at once, several + // evaluate, then commit, in entry order. + e.b.switch_to_block(together); + let one = e.constant(1, MAX_ENTRIES); + let lower = e.binary(MAX_ENTRIES, mask, BinaryOp::Sub, one); + let others = e.binary(MAX_ENTRIES, mask, BinaryOp::And, lower); + let single = e.binary(1, others, BinaryOp::Eq, zero); + let one_domain = e.b.new_block(); + let several = e.b.new_block(); + e.b.seal_block(SIRTerminator::Branch { + cond: single, + true_block: (one_domain, Vec::new()), + false_block: (several, Vec::new()), + }); + e.b.switch_to_block(one_domain); + for (entry, units) in self.entries().enumerate() { + e.if_bit(mask, entry, |e| e.inline_all(&units.eval_apply)); + } + e.b.seal_block(SIRTerminator::Jump(fire_end, Vec::new())); + e.b.switch_to_block(several); + for (entry, units) in self.entries().enumerate() { + e.if_bit(mask, entry, |e| match &units.eval_only { + Some(eval_only) => e.inline_all(eval_only), + None => e.inline_all(&units.eval_apply), + }); + } + for (entry, units) in self.entries().enumerate() { + if let Some(apply) = &units.apply { + e.if_bit(mask, entry, |e| e.inline_all(apply)); + } + } + e.b.seal_block(SIRTerminator::Jump(fire_end, Vec::new())); + + // Combinational logic settles after every fire; then a round: the + // domains not fired yet whose clocks moved to their active edge. + e.b.switch_to_block(fire_end); + e.inline_all(comb); + let mut ready = e.constant(0, MAX_ENTRIES); + for (index, domain) in self.domains.iter().enumerate() { + let is = e.level(domain.signal, domain.width); + let edge = e.edge(domain.kind, was[index], is); + let bit = e.bit(edge, 1 + index); + ready = e.binary(MAX_ENTRIES, ready, BinaryOp::Or, bit); + } + let after = std::mem::take(&mut e.after); + for (index, gated) in self.gated.iter().enumerate() { + let is = e.gate(&gated.gate, to); + let edge = e.edge(gated.derived.kind, after[index], is); + let bit = e.bit(edge, self.gated_bit(index)); + ready = e.binary(MAX_ENTRIES, ready, BinaryOp::Or, bit); + } + let all = e.constant(u64::MAX, MAX_ENTRIES); + let unfired = e.binary(MAX_ENTRIES, fired, BinaryOp::Xor, all); + let ready = e.binary(MAX_ENTRIES, ready, BinaryOp::And, unfired); + let quiet = e.binary(1, ready, BinaryOp::Eq, zero); + let round = e.b.new_block(); + e.b.seal_block(SIRTerminator::Branch { + cond: quiet, + true_block: (settled, Vec::new()), + false_block: (round, Vec::new()), + }); + e.b.switch_to_block(round); + let now_fired = e.binary(MAX_ENTRIES, fired, BinaryOp::Or, ready); + let no = e.constant(0, 1); + e.b.seal_block(SIRTerminator::Jump(fire, vec![ready, now_fired, no, no])); + } +} + +/// Whether `gate` is a program the emitter runs: its loads read state of at +/// most 64 bits, and its stack stays within the Rust walk's bound and ends +/// with one value. +fn gate_fits( + gate: &[GateOp], + loadable: impl Fn(&AbsoluteAddr, u32, u32) -> bool, +) -> bool { + let mut depth = 0usize; + for op in gate { + let pops = match op { + GateOp::Load { + address, + lsb, + width, + } => { + if !loadable(address, *lsb, *width) { + return false; + } + 0 + } + GateOp::Source | GateOp::Const(_) => 0, + GateOp::Not { .. } + | GateOp::LogicNot + | GateOp::ReduceAnd { .. } + | GateOp::ReduceOr + | GateOp::ReduceXor + | GateOp::Slice { .. } => 1, + GateOp::And + | GateOp::Or + | GateOp::Xor + | GateOp::Eq + | GateOp::Ne + | GateOp::LogicAnd + | GateOp::LogicOr => 2, + GateOp::Mux => 3, + }; + let Some(next) = depth.checked_sub(pops) else { + return false; + }; + depth = next + 1; + if depth > GATE_STACK { + return false; + } + } + depth == 1 +} + +fn mask(width: u32) -> u64 { + if width >= 64 { + u64::MAX + } else { + (1u64 << width) - 1 + } +} + +struct Emitter<'b> { + b: &'b mut Builder, + /// The gated clocks' levels just after the edge, the baseline of their + /// further edges in the rounds. + after: Vec, +} + +impl Emitter<'_> { + fn constant(&mut self, value: u64, width: usize) -> RegisterId { + let register = self.b.alloc_bit(width, false); + self.b + .emit(SIRInstruction::Imm(register, SIRValue::new(value))); + register + } + + fn binary( + &mut self, + width: usize, + lhs: RegisterId, + op: BinaryOp, + rhs: RegisterId, + ) -> RegisterId { + let register = self.b.alloc_bit(width, false); + self.b.emit(SIRInstruction::Binary(register, lhs, op, rhs)); + register + } + + fn unary(&mut self, width: usize, op: UnaryOp, value: RegisterId) -> RegisterId { + let register = self.b.alloc_bit(width, false); + self.b.emit(SIRInstruction::Unary(register, op, value)); + register + } + + /// `value` as a gate value, zero-extended. + fn widen(&mut self, value: RegisterId) -> RegisterId { + self.unary(GATE_WIDTH, UnaryOp::Ident, value) + } + + fn load(&mut self, address: AbsoluteAddr, lsb: usize, width: usize) -> RegisterId { + let register = self.b.alloc_bit(width, false); + self.b.emit(SIRInstruction::Load( + register, + RegionedAbsoluteAddr::from_absolute_addr(STABLE_REGION, address), + SIROffset::Static(lsb), + width, + )); + register + } + + /// The edge level of a signal as the runtime reads it: whether its low + /// byte, within its width, is not zero. + fn level(&mut self, signal: AbsoluteAddr, width: usize) -> RegisterId { + let width = width.min(8); + let value = self.load(signal, 0, width); + let zero = self.constant(0, width); + self.binary(1, value, BinaryOp::Ne, zero) + } + + /// Whether a change of a domain's clock from `was` to `is` (one-bit + /// levels) is its active edge, as `settled_edge` judges two-state levels. + fn edge(&mut self, kind: Option, was: RegisterId, is: RegisterId) -> RegisterId { + let changed = self.binary(1, was, BinaryOp::Ne, is); + match kind { + Some(DomainKind::ClockPosedge | DomainKind::ResetAsyncHigh) => { + self.binary(1, changed, BinaryOp::And, is) + } + Some(DomainKind::ClockNegedge | DomainKind::ResetAsyncLow) => { + self.binary(1, changed, BinaryOp::And, was) + } + _ => changed, + } + } + + /// Bit `index` of a fire mask, set when `condition` holds. + fn bit(&mut self, condition: RegisterId, index: usize) -> RegisterId { + let set = self.constant(1 << index, MAX_ENTRIES); + let unset = self.constant(0, MAX_ENTRIES); + let bit = self.b.alloc_bit(MAX_ENTRIES, false); + self.b.emit(SIRInstruction::Mux(bit, condition, set, unset)); + bit + } + + /// The level of a gated clock from its program, with the event's signal + /// at `source` (a one-bit register). + fn gate(&mut self, gate: &[GateOp], source: RegisterId) -> RegisterId { + let mut stack: Vec = Vec::with_capacity(GATE_STACK); + for op in gate { + let value = match *op { + GateOp::Source => self.widen(source), + GateOp::Load { + address, + lsb, + width, + } => { + let value = self.load(address, lsb as usize, width as usize); + self.widen(value) + } + GateOp::Const(value) => self.constant(value, GATE_WIDTH), + GateOp::Not { width } => { + let value = stack.pop().expect("checked by gate_fits"); + let mask = self.constant(mask(width), GATE_WIDTH); + self.binary(GATE_WIDTH, value, BinaryOp::Xor, mask) + } + GateOp::LogicNot => { + let value = stack.pop().expect("checked by gate_fits"); + let zero = self.constant(0, GATE_WIDTH); + let is = self.binary(1, value, BinaryOp::Eq, zero); + self.widen(is) + } + GateOp::ReduceAnd { width } => { + let value = stack.pop().expect("checked by gate_fits"); + let mask = self.constant(mask(width), GATE_WIDTH); + let bits = self.binary(GATE_WIDTH, value, BinaryOp::And, mask); + let is = self.binary(1, bits, BinaryOp::Eq, mask); + self.widen(is) + } + GateOp::ReduceOr => { + let value = stack.pop().expect("checked by gate_fits"); + let zero = self.constant(0, GATE_WIDTH); + let is = self.binary(1, value, BinaryOp::Ne, zero); + self.widen(is) + } + GateOp::ReduceXor => { + let value = stack.pop().expect("checked by gate_fits"); + let parity = self.unary(1, UnaryOp::Xor, value); + self.widen(parity) + } + GateOp::Slice { lsb, width } => { + let value = stack.pop().expect("checked by gate_fits"); + let shift = self.constant(u64::from(lsb), GATE_WIDTH); + let shifted = self.binary(GATE_WIDTH, value, BinaryOp::Shr, shift); + let mask = self.constant(mask(width), GATE_WIDTH); + self.binary(GATE_WIDTH, shifted, BinaryOp::And, mask) + } + GateOp::Mux => { + let otherwise = stack.pop().expect("checked by gate_fits"); + let then = stack.pop().expect("checked by gate_fits"); + let condition = stack.pop().expect("checked by gate_fits"); + let zero = self.constant(0, GATE_WIDTH); + let holds = self.binary(1, condition, BinaryOp::Ne, zero); + let value = self.b.alloc_bit(GATE_WIDTH, false); + self.b + .emit(SIRInstruction::Mux(value, holds, then, otherwise)); + value + } + GateOp::And + | GateOp::Or + | GateOp::Xor + | GateOp::Eq + | GateOp::Ne + | GateOp::LogicAnd + | GateOp::LogicOr => { + let rhs = stack.pop().expect("checked by gate_fits"); + let lhs = stack.pop().expect("checked by gate_fits"); + match *op { + GateOp::And => self.binary(GATE_WIDTH, lhs, BinaryOp::And, rhs), + GateOp::Or => self.binary(GATE_WIDTH, lhs, BinaryOp::Or, rhs), + GateOp::Xor => self.binary(GATE_WIDTH, lhs, BinaryOp::Xor, rhs), + GateOp::Eq => { + let is = self.binary(1, lhs, BinaryOp::Eq, rhs); + self.widen(is) + } + GateOp::Ne => { + let is = self.binary(1, lhs, BinaryOp::Ne, rhs); + self.widen(is) + } + GateOp::LogicAnd => { + let is = self.binary(1, lhs, BinaryOp::LogicAnd, rhs); + self.widen(is) + } + _ => { + let is = self.binary(1, lhs, BinaryOp::LogicOr, rhs); + self.widen(is) + } + } + } + }; + stack.push(value); + } + let value = stack.pop().expect("checked by gate_fits"); + let one = self.constant(1, GATE_WIDTH); + let low = self.binary(GATE_WIDTH, value, BinaryOp::And, one); + let zero = self.constant(0, GATE_WIDTH); + self.binary(1, low, BinaryOp::Ne, zero) + } + + fn inline_all(&mut self, units: &[Unit]) { + for unit in units { + self.b.inline_execution_unit(unit); + } + } + + /// Settle combinational logic when the one-bit `condition` holds. + fn settle_if(&mut self, condition: RegisterId, comb: &[Unit]) { + let settle = self.b.new_block(); + let next = self.b.new_block(); + self.b.seal_block(SIRTerminator::Branch { + cond: condition, + true_block: (settle, Vec::new()), + false_block: (next, Vec::new()), + }); + self.b.switch_to_block(settle); + self.inline_all(comb); + self.b.seal_block(SIRTerminator::Jump(next, Vec::new())); + self.b.switch_to_block(next); + } + + /// Run `body` when bit `index` of `mask` is set. + fn if_bit(&mut self, mask: RegisterId, index: usize, body: impl FnOnce(&mut Self)) { + let selector = self.constant(1 << index, MAX_ENTRIES); + let selected = self.binary(MAX_ENTRIES, mask, BinaryOp::And, selector); + let zero = self.constant(0, MAX_ENTRIES); + let set = self.binary(1, selected, BinaryOp::Ne, zero); + let run = self.b.new_block(); + let next = self.b.new_block(); + self.b.seal_block(SIRTerminator::Branch { + cond: set, + true_block: (run, Vec::new()), + false_block: (next, Vec::new()), + }); + self.b.switch_to_block(run); + body(self); + self.b.seal_block(SIRTerminator::Jump(next, Vec::new())); + self.b.switch_to_block(next); + } +} + +impl EdgePlan { + /// A unit that runs one edge of the event whose signal its caller + /// already moved, rising or falling, with `settle` and `fires` fixed. + #[cfg(all(test, feature = "systemverilog"))] + fn unit(&self, comb: &[Unit], rising: bool, settle: bool, fires: bool) -> Unit { + let mut b = Builder::new(); + let constant = |b: &mut Builder, value: bool| { + let register = b.alloc_bit(1, false); + b.emit(SIRInstruction::Imm( + register, + SIRValue::new(u64::from(value)), + )); + register + }; + let from = constant(&mut b, !rising); + let to = constant(&mut b, rising); + let fires = constant(&mut b, fires); + let settle = constant(&mut b, settle); + let done = b.new_block(); + self.emit(&mut b, comb, from, to, fires, settle, done, done); + b.switch_to_block(done); + b.seal_block(SIRTerminator::Return); + let (blocks, register_map, _) = b.drain(); + Unit { + entry_block_id: BlockId(0), + blocks, + register_map, + } + } +} + +/// A generated edge unit and the edge it runs, for the tests. +#[cfg(all(test, feature = "systemverilog"))] +#[derive(Clone, Copy, Debug)] +pub(crate) struct TestEdgeUnit { + pub(crate) event: AbsoluteAddr, + pub(crate) rising: bool, + pub(crate) settle: bool, + pub(crate) fires: bool, + pub(crate) index: usize, +} + +#[cfg(all(test, feature = "systemverilog"))] +pub(crate) type TestEdgeUnits = std::sync::Arc>>; + +/// Append an edge unit of every event with a plan this module emits, for +/// each edge, settle and own-edge case, and record them in `sink`. +#[cfg(all(test, feature = "systemverilog"))] +pub(crate) fn attach_test_units(program: &mut LaidOutProgram, sink: &TestEdgeUnits) { + let model = ScheduleModel::derive(&program.design, &program.runtime.runtime_schema); + for event in crate::edge_plans::event_addresses(program) { + let Ok(Some(plan)) = EdgePlan::new(program, &model, event) else { + continue; + }; + for rising in [false, true] { + for settle in [false, true] { + for fires in [false, true] { + let unit = plan.unit(&program.sir.eval_comb, rising, settle, fires); + let index = program.sir.processes.len(); + program.sir.processes.push(unit); + sink.lock().expect("unpoisoned").push(TestEdgeUnit { + event, + rising, + settle, + fires, + index, + }); + } + } + } + } +} + +#[cfg(all(test, feature = "systemverilog"))] +mod tests { + use std::path::Path; + + use celox_runtime::{ + BackendExecutor, DerivedTickScratch, Edge, EdgeOutcome, run_edge, set_level, + }; + + use super::TestEdgeUnits; + use crate::{ + Simulator, + backend::{EventHandle, SimBackend}, + }; + + /// The design state: the stable region after its header. + fn state(backend: &B) -> Vec { + let (ptr, len) = backend.memory_as_ptr(); + let size = backend.layout().total_size; + assert!(size <= len); + // Safety: the backend exposes `len` readable bytes at `ptr`. + let memory = unsafe { std::slice::from_raw_parts(ptr, len) }; + memory[celox_state_layout::STATE_HEADER_SIZE..size].to_vec() + } + + /// Whether combinational logic is stale after an edge with `outcome`, + /// as `run_tick` keeps it. + fn stale(dirty: bool, outcome: EdgeOutcome) -> bool { + match outcome { + EdgeOutcome::Unchanged | EdgeOutcome::Stored => dirty, + EdgeOutcome::Moved => true, + EdgeOutcome::Settled => false, + } + } + + /// Run edges of `clk` and host writes of `en`, when the design has it, + /// through the Rust plan walk and through the generated units, and + /// compare the state after each. + fn agrees(source: &str) { + let build = || Simulator::from_sv_sources(vec![(source, Path::new("top.sv"))], "Top"); + // The Cranelift backend runs extra units without an image to check. + let mut walked = build().build_cranelift().unwrap(); + let sink = TestEdgeUnits::default(); + let mut generated = build() + .derived_edge_units(sink.clone()) + .build_cranelift() + .unwrap(); + let units = sink.lock().unwrap().clone(); + assert!(!units.is_empty(), "no edge unit was generated"); + let clk = walked.event("clk"); + let address = walked.backend.id_to_addr_slice()[clk.id()]; + let plans = walked.derived_tick_plans(); + let plan = plans.get(clk.id()).and_then(Option::as_ref); + assert!(plan.is_some(), "clk has no derived plan"); + let gated = plan.is_some_and(|plan| plan.lever_signals().next().is_some()); + let info = walked.event_edge(clk); + let en = walked.try_signal("en").ok(); + let mut scratch = DerivedTickScratch::default(); + assert!( + state(&generated.backend) == state(&walked.backend), + "the simulators start in other states" + ); + let (mut walked_dirty, mut generated_dirty) = (false, false); + let edges = [ + Edge::Rising, + Edge::Falling, + Edge::Falling, + Edge::Rising, + Edge::Falling, + ]; + for step in 0..60usize { + if let Some(en) = en + && step % 7 < 2 + { + let value = u8::from(step % 3 != 0); + walked.backend.set(en, value); + generated.backend.set(en, value); + walked_dirty = true; + generated_dirty = true; + } + let edge = edges[step % edges.len()]; + let outcome = run_edge( + &mut BackendExecutor(&mut walked.backend), + plan, + info, + &mut scratch, + clk, + edge, + walked_dirty, + set_level, + ) + .unwrap(); + walked_dirty = stale(walked_dirty, outcome); + + let from = u8::from(generated.backend.get_as::(info.signal) & 1 != 0); + let outcome = if from == edge.level() { + EdgeOutcome::Unchanged + } else { + generated.backend.set(info.signal, edge.level()); + let fires = info.fires(Some(from != 0), edge); + let settle = generated_dirty || info.comb_read; + let unit = units + .iter() + .find(|unit| { + unit.event == address + && unit.rising == (edge == Edge::Rising) + && unit.settle == settle + && unit.fires == fires + }) + .expect("a unit for every case"); + generated.backend.run_process(unit.index).unwrap(); + if gated || fires { + EdgeOutcome::Settled + } else if info.comb_read { + EdgeOutcome::Moved + } else { + EdgeOutcome::Stored + } + }; + generated_dirty = stale(generated_dirty, outcome); + assert_eq!(generated_dirty, walked_dirty, "step {step}"); + let (left, right) = (state(&generated.backend), state(&walked.backend)); + if let Some(offset) = left.iter().zip(&right).position(|(a, b)| a != b) { + let offset = offset + celox_state_layout::STATE_HEADER_SIZE; + let name = walked + .named_signals() + .into_iter() + .find(|named| { + let start = named.signal.offset; + (start..start + named.signal.width.div_ceil(8)).contains(&offset) + }) + .map_or_else(|| "no named signal".to_string(), |named| named.name); + panic!( + "step {step} ({edge:?}): the generated edge left state byte {offset} ({name}) otherwise" + ); + } + } + } + + #[test] + fn gated_clocks_run_as_the_plan_walk() { + agrees( + "module Top(input logic clk, input logic en, output logic [7:0] q); + logic gclk; + logic [7:0] r = 0; + logic [7:0] g = 0; + assign gclk = clk & en; + always_ff @(posedge clk) r <= r + 8'd1; + always_ff @(posedge gclk) g <= g + r; + assign q = g; + endmodule", + ); + } + + #[test] + fn inverted_and_falling_gated_clocks_run_as_the_plan_walk() { + agrees( + "module Top(input logic clk, input logic en, output logic [7:0] q); + logic nclk; + logic gclk; + logic [7:0] r = 0; + logic [7:0] n = 0; + logic [7:0] g = 0; + assign nclk = ~clk; + assign gclk = clk & en; + always_ff @(posedge clk) r <= r + 8'd1; + always_ff @(posedge nclk) n <= n + r; + always_ff @(negedge gclk) g <= g ^ n; + assign q = n + g; + endmodule", + ); + } + + #[test] + fn dividers_run_as_the_plan_walk() { + agrees( + "module Top(input logic clk, output logic [7:0] q); + logic half = 0; + logic quarter = 0; + logic [7:0] h = 0; + logic [7:0] w = 0; + always_ff @(posedge clk) half <= ~half; + always_ff @(posedge half) quarter <= ~quarter; + always_ff @(posedge half) h <= h + 8'd1; + always_ff @(posedge quarter) w <= w + h; + assign q = h + w; + endmodule", + ); + } + + #[test] + fn enables_the_commit_raises_run_as_the_plan_walk() { + agrees( + "module Top(input logic clk, output logic [7:0] q); + logic en = 0; + logic gclk; + logic [7:0] g = 0; + assign gclk = clk & en; + always_ff @(posedge clk) en <= ~en; + always_ff @(posedge gclk) g <= g + 8'd1; + assign q = g; + endmodule", + ); + } + + #[test] + fn independent_gated_domains_run_as_the_plan_walk() { + agrees( + "module Top(input logic clk, input logic en, input logic [7:0] d, output logic [7:0] q); + logic gclk; + logic [7:0] r = 0; + logic [7:0] g = 0; + assign gclk = clk & en; + always_ff @(posedge clk) r <= r + d; + always_ff @(posedge gclk) g <= g + 8'd3; + assign q = r ^ g; + endmodule", + ); + } +} diff --git a/crates/celox/src/lib.rs b/crates/celox/src/lib.rs index f8c7072b4f3..ae4fb81ffe3 100644 --- a/crates/celox/src/lib.rs +++ b/crates/celox/src/lib.rs @@ -19,6 +19,10 @@ pub use component::{ register_static_component_manifest, }; mod debug; +// Only its tests run the generator until a host does. +#[cfg(feature = "host-runtime")] +#[cfg_attr(not(all(test, feature = "systemverilog")), allow(dead_code))] +mod derived_edge; mod edge_plans; pub use edge_plans::{ EdgePlanDomain, EdgePlanEvent, EdgePlanSignal, EdgePlans, edge_plans, event_addresses, diff --git a/crates/celox/src/simulator.rs b/crates/celox/src/simulator.rs index 4579bc5414e..2ce6b6afe92 100644 --- a/crates/celox/src/simulator.rs +++ b/crates/celox/src/simulator.rs @@ -2200,6 +2200,12 @@ mod host { Ok(()) } + /// The edge facts of `event`. + #[cfg(all(test, feature = "systemverilog"))] + pub(crate) fn event_edge(&self, event: B::Event) -> celox_runtime::EventEdge { + self.event_edges[event.id()] + } + /// Whether a period of `event`'s signal, a rise and a fall, runs /// domains only at its active edge: the event's own and those derived /// from it, while the inactive edge runs nothing. diff --git a/crates/celox/src/simulator/builder.rs b/crates/celox/src/simulator/builder.rs index 0df87c12b46..68400436bc8 100644 --- a/crates/celox/src/simulator/builder.rs +++ b/crates/celox/src/simulator/builder.rs @@ -1679,6 +1679,9 @@ mod host { /// When true, JIT-compiled functions emit trigger detection code for /// edge-based event discovery. Only needed by [`crate::Simulation`]. pub emit_triggers: bool, + /// Generated derived-edge units the tests run, recorded here. + #[cfg(all(test, feature = "host-runtime", feature = "systemverilog"))] + pub(crate) derived_edge_units: Option, /// Emit the additional combinational entries needed to reapply foreign /// force overrides between procedural store boundaries. pub native_force_support: bool, @@ -1924,6 +1927,8 @@ mod host { diagnostic_policy: Default::default(), sv_timescale: None, emit_triggers: false, + #[cfg(all(test, feature = "host-runtime", feature = "systemverilog"))] + derived_edge_units: None, native_force_support: false, signal_access: crate::SignalAccessOptions::default(), dead_store_policy: DeadStorePolicy::Off, @@ -2080,6 +2085,17 @@ mod host { self } + /// Append the generated derived-edge units of every event and record + /// them in `sink`. + #[cfg(all(test, feature = "host-runtime", feature = "systemverilog"))] + pub(crate) fn derived_edge_units( + mut self, + sink: crate::derived_edge::TestEdgeUnits, + ) -> Self { + self.options.derived_edge_units = Some(sink); + self + } + /// Set the ordered arguments queried by `$test$plusargs` and /// `$value$plusargs`. A leading `+` is optional on each argument. /// Queries currently require a timed procedural process, such as an @@ -2832,6 +2848,10 @@ mod host { { crate::scheduler::attach(&mut laid_out); } + #[cfg(all(test, feature = "host-runtime", feature = "systemverilog"))] + if let Some(sink) = &self.options.derived_edge_units { + crate::derived_edge::attach_test_units(&mut laid_out, sink); + } // Link DPI-C imports before code generation, so a missing C // function fails the build early; the backends reuse the result.