diff --git a/crates/celox-frontend-core/src/symbolic/assembly.rs b/crates/celox-frontend-core/src/symbolic/assembly.rs index d7d5900c148..44298ea22e2 100644 --- a/crates/celox-frontend-core/src/symbolic/assembly.rs +++ b/crates/celox-frontend-core/src/symbolic/assembly.rs @@ -1809,6 +1809,8 @@ fn unify_clock_domains( modules: &HashMap, ) -> HashMap { let mut drive_graph: HashMap> = HashMap::default(); + // The edge each clock is sensed at: `true` rising, `false` falling. + let mut polarities: HashMap = HashMap::default(); for (path, id) in expanded { let module_id = &instance_modules[id]; @@ -1820,6 +1822,20 @@ fn unify_clock_domains( .get(&var) .map(|variable| variable.metadata.width) }; + let mut record_polarity = |module: &SimModule, instance: InstanceId, var: SourceVarId| { + let polarity = + module + .variables + .get(&var) + .and_then(|variable| match variable.metadata.kind { + DomainKind::ClockPosedge | DomainKind::ResetAsyncHigh => Some(true), + DomainKind::ClockNegedge | DomainKind::ResetAsyncLow => Some(false), + _ => None, + }); + if let Some(polarity) = polarity { + polarities.insert(bindings.locate(instance, var), polarity); + } + }; // Internal aliases (e.g. `assign clk_internal = clk_port;`) for logic_path in &sim_module.comb_blocks { @@ -1832,6 +1848,8 @@ fn unify_clock_domains( |target| width(sim_module, target), |source| width(sim_module, source), ) { + record_polarity(sim_module, *id, target); + record_polarity(sim_module, *id, source); drive_graph .entry(bindings.locate(*id, source)) .or_default() @@ -1854,6 +1872,8 @@ fn unify_clock_domains( if let Some((GlueAddr::Child(child_var), GlueAddr::Parent(parent_var))) = whole_variable_copy(&glue_block.arena, logic_path, glue_width, glue_width) { + record_polarity(child_module, child_id, child_var); + record_polarity(sim_module, *id, parent_var); drive_graph .entry(bindings.locate(*id, parent_var)) .or_default() @@ -1872,6 +1892,8 @@ fn unify_clock_domains( |_| parent_width, glue_width, ) { + record_polarity(child_module, child_id, child_var); + record_polarity(sim_module, *id, *parent_var); drive_graph .entry(bindings.locate(child_id, child_var)) .or_default() @@ -1882,6 +1904,68 @@ fn unify_clock_domains( } } + // A copy sensed at the opposite edge of its source is a clock of its own: + // `always_ff @(negedge d)` with `assign c = clk; assign d = c;` runs when + // `clk` falls, not in `clk`'s rising domain. A copy sensed at no edge + // takes the edge of its drivers, and a root sensed at no edge the edge of + // its first copy sensed at one. Copies that join edges of both kinds are + // cut apart. + let drivers = { + let mut drivers: HashMap> = HashMap::default(); + for (src, sinks) in &drive_graph { + for sink in sinks { + drivers.entry(*sink).or_default().push(*src); + } + } + drivers + }; + let root_polarity = |root: AbsoluteAddr| { + polarities.get(&root).copied().or_else(|| { + let mut stack = vec![root]; + let mut visited = HashSet::default(); + while let Some(addr) = stack.pop() { + if !visited.insert(addr) { + continue; + } + if let Some(&polarity) = polarities.get(&addr) { + return Some(polarity); + } + if let Some(sinks) = drive_graph.get(&addr) { + stack.extend(sinks.iter().rev()); + } + } + None + }) + }; + let effective_polarity = |addr: AbsoluteAddr| { + let mut current = addr; + let mut visited = HashSet::default(); + loop { + if let Some(&polarity) = polarities.get(¤t) { + return Some(polarity); + } + match drivers.get(¤t).and_then(|sources| sources.first()) { + Some(&next) if visited.insert(next) => current = next, + _ => return root_polarity(current), + } + } + }; + let drive_graph: HashMap> = drive_graph + .iter() + .map(|(src, sinks)| { + let src_polarity = effective_polarity(*src); + let sinks = sinks + .iter() + .copied() + .filter(|sink| match (src_polarity, effective_polarity(*sink)) { + (Some(a), Some(b)) => a == b, + _ => true, + }) + .collect(); + (*src, sinks) + }) + .collect(); + // Resolve Canonical Clock Domains: Find the root driver for each connected component let mut clock_domains: HashMap = HashMap::default(); diff --git a/crates/celox/tests/derived_register_clock.rs b/crates/celox/tests/derived_register_clock.rs index 5951c5c167f..b7cf39ab08a 100644 --- a/crates/celox/tests/derived_register_clock.rs +++ b/crates/celox/tests/derived_register_clock.rs @@ -687,3 +687,178 @@ fn a_systemverilog_child_clock_into_a_vector_bit() { check_sv(&source, 8, &want); } } + +/// Drives `clk` low and high four times and checks that `cnt` counts the +/// falling edges only. +fn check_falling_edges(mut timed: Simulation) { + let cnt = timed.signal("cnt"); + for (port, time, value) in [("rst", 0, 1), ("clk", 0, 1), ("rst", 1, 0), ("rst", 2, 1)] { + timed.schedule(port, time, value).unwrap(); + timed.step().unwrap(); + } + for cycle in 0..4u64 { + timed.schedule("clk", 10 + cycle * 20, 0).unwrap(); + timed.step().unwrap(); + assert_eq!(timed.get(cnt), (cycle + 1).into(), "falling edge {cycle}"); + timed.schedule("clk", 20 + cycle * 20, 1).unwrap(); + timed.step().unwrap(); + assert_eq!(timed.get(cnt), (cycle + 1).into(), "rising edge {cycle}"); + } +} + +/// A whole copy of `clk` sensed at its falling edge does not join `clk`'s +/// rising domain. +#[test] +fn a_clock_copy_sensed_at_the_falling_edge_runs_on_the_falling_edge() { + check_falling_edges( + Simulation::builder( + r#" +module Top ( + clk: input '_ clock, + rst: input '_ reset_async_low, + cnt: output logic<8>, +) { + var c: '_ clock_negedge; + var b: logic<8>; + assign c = clk; + always_ff (c, rst) { + if_reset { b = 0; } else { b = b + 8'd1; } + } + assign cnt = b; +} +"#, + "Top", + ) + .build() + .unwrap(), + ); +} + +/// The same for a child clock port sensed at the falling edge. +#[test] +fn a_falling_edge_child_clock_port_runs_on_the_falling_edge() { + check_falling_edges( + Simulation::builder( + r#" +module Child ( + clk: input '_ clock_negedge, + rst: input '_ reset_async_low, + cnt: output logic<8>, +) { + var b: logic<8>; + always_ff (clk, rst) { + if_reset { b = 0; } else { b = b + 8'd1; } + } + assign cnt = b; +} + +module Top ( + clk: input '_ clock, + rst: input '_ reset_async_low, + cnt: output logic<8>, +) { + var a: logic<8>; + always_ff (clk, rst) { + if_reset { a = 0; } else { a = a + 8'd1; } + } + inst u: Child (clk, rst, cnt); +} +"#, + "Top", + ) + .build() + .unwrap(), + ); +} + +#[cfg(feature = "systemverilog")] +#[test] +fn a_systemverilog_clock_copy_sensed_at_the_falling_edge() { + let source = "module Top(input logic clk, input logic rst, output logic [7:0] cnt); + logic c; + logic [7:0] a; + assign c = clk; + always_ff @(posedge clk or negedge rst) if (!rst) a <= 0; else a <= a + 1; + always_ff @(negedge c or negedge rst) if (!rst) cnt <= 0; else cnt <= cnt + 1; + endmodule"; + let path = std::path::Path::new("derived_register_clock.sv"); + check_falling_edges( + Simulation::from_sv_sources(vec![(source, path)], "Top") + .build() + .unwrap(), + ); +} + +#[cfg(feature = "systemverilog")] +fn check_sv_falling_edges(source: &str) { + let path = std::path::Path::new("derived_register_clock.sv"); + check_falling_edges( + Simulation::from_sv_sources(vec![(source, path)], "Top") + .build() + .unwrap(), + ); +} + +/// A copy of a copy takes its edge from the end of the chain that is sensed. +#[cfg(feature = "systemverilog")] +#[test] +fn a_systemverilog_chain_of_clock_copies_sensed_at_the_falling_edge() { + check_sv_falling_edges( + "module Top(input logic clk, input logic rst, output logic [7:0] cnt); + logic c, d; + logic [7:0] a; + assign c = clk; + assign d = c; + always_ff @(posedge clk or negedge rst) if (!rst) a <= 0; else a <= a + 1; + always_ff @(negedge d or negedge rst) if (!rst) cnt <= 0; else cnt <= cnt + 1; + endmodule", + ); +} + +#[cfg(feature = "systemverilog")] +#[test] +fn a_systemverilog_chain_of_clock_copies_through_a_child_port() { + check_sv_falling_edges( + "module Child(input logic k, input logic rst, output logic [7:0] cnt); + logic j; + assign j = k; + always_ff @(negedge j or negedge rst) if (!rst) cnt <= 0; else cnt <= cnt + 1; + endmodule + module Top(input logic clk, input logic rst, output logic [7:0] cnt); + logic c; + logic [7:0] a; + assign c = clk; + always_ff @(posedge clk or negedge rst) if (!rst) a <= 0; else a <= a + 1; + Child u(.k(c), .rst(rst), .cnt(cnt)); + endmodule", + ); +} + +/// Copies of a clock that is itself sensed at no edge, one at each edge. +#[cfg(feature = "systemverilog")] +#[test] +fn a_systemverilog_unsensed_clock_copied_to_both_edges() { + for (first, second) in [("posedge c", "negedge d"), ("negedge d", "posedge c")] { + let block = |edge: &str| { + if edge.ends_with('c') { + format!("always_ff @({edge} or negedge rst) if (!rst) a <= 0; else a <= a + 1;") + } else { + format!( + "always_ff @({edge} or negedge rst) if (!rst) cnt <= 0; else cnt <= cnt + 1;" + ) + } + }; + check_sv_falling_edges(&format!( + "module Top(input logic clk, input logic rst, output logic [7:0] cnt); + logic c, d; + logic [7:0] a; + assign c = clk; + assign d = clk; + {} + {} + endmodule", + block(first), + block(second), + )); + } +}