From 26b6ed57e19af5261c05af483f3d3731a3f54423 Mon Sep 17 00:00:00 2001 From: tignear Date: Sat, 17 Oct 2026 04:06:24 +0900 Subject: [PATCH 1/3] fix(sim): keep clock copies sensed at the opposite edge apart A whole-variable copy of a clock joined its source's domain even when it was sensed at the opposite edge, so always_ff @(negedge c) with assign c = clk ran at clk's rising edge. Such a copy is now a clock derived from its source. --- .../src/symbolic/assembly.rs | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/celox-frontend-core/src/symbolic/assembly.rs b/crates/celox-frontend-core/src/symbolic/assembly.rs index d7d5900c148..a5010c44be8 100644 --- a/crates/celox-frontend-core/src/symbolic/assembly.rs +++ b/crates/celox-frontend-core/src/symbolic/assembly.rs @@ -1820,6 +1820,20 @@ fn unify_clock_domains( .get(&var) .map(|variable| variable.metadata.width) }; + // A copy sensed at the opposite edge of its source is a clock of its + // own: `always_ff @(negedge c)` with `assign c = clk` runs when `clk` + // falls, not with `clk`'s rising domain. + let polarity = |module: &SimModule, var: SourceVarId| { + module + .variables + .get(&var) + .and_then(|variable| match variable.metadata.kind { + DomainKind::ClockPosedge | DomainKind::ResetAsyncHigh => Some(true), + DomainKind::ClockNegedge | DomainKind::ResetAsyncLow => Some(false), + _ => None, + }) + }; + let same_edge = |a: Option, b: Option| a.is_none() || b.is_none() || a == b; // Internal aliases (e.g. `assign clk_internal = clk_port;`) for logic_path in &sim_module.comb_blocks { @@ -1831,7 +1845,8 @@ fn unify_clock_domains( logic_path, |target| width(sim_module, target), |source| width(sim_module, source), - ) { + ) && same_edge(polarity(sim_module, target), polarity(sim_module, source)) + { drive_graph .entry(bindings.locate(*id, source)) .or_default() @@ -1853,6 +1868,10 @@ fn unify_clock_domains( for (_, logic_path) in &glue_block.input_ports { if let Some((GlueAddr::Child(child_var), GlueAddr::Parent(parent_var))) = whole_variable_copy(&glue_block.arena, logic_path, glue_width, glue_width) + && same_edge( + polarity(child_module, child_var), + polarity(sim_module, parent_var), + ) { drive_graph .entry(bindings.locate(*id, parent_var)) @@ -1871,6 +1890,9 @@ fn unify_clock_domains( logic_path, |_| parent_width, glue_width, + ) && same_edge( + polarity(child_module, child_var), + polarity(sim_module, *parent_var), ) { drive_graph .entry(bindings.locate(child_id, child_var)) From acbed0e5d845ec6e84d7d85195229deb93d87c62 Mon Sep 17 00:00:00 2001 From: tignear Date: Sat, 17 Oct 2026 05:26:43 +0900 Subject: [PATCH 2/3] test(sim): cover clock copies sensed at the falling edge --- crates/celox/tests/derived_register_clock.rs | 101 +++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/crates/celox/tests/derived_register_clock.rs b/crates/celox/tests/derived_register_clock.rs index 5951c5c167f..2a1edb00c63 100644 --- a/crates/celox/tests/derived_register_clock.rs +++ b/crates/celox/tests/derived_register_clock.rs @@ -687,3 +687,104 @@ 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(), + ); +} From 315eab017e6060fdddf6a2bd7834cfd82c35f566 Mon Sep 17 00:00:00 2001 From: tignear Date: Sat, 17 Oct 2026 05:52:42 +0900 Subject: [PATCH 3/3] fix(sim): keep chains of clock copies sensed at opposite edges apart --- .../src/symbolic/assembly.rs | 106 ++++++++++++++---- crates/celox/tests/derived_register_clock.rs | 74 ++++++++++++ 2 files changed, 158 insertions(+), 22 deletions(-) diff --git a/crates/celox-frontend-core/src/symbolic/assembly.rs b/crates/celox-frontend-core/src/symbolic/assembly.rs index a5010c44be8..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,20 +1822,20 @@ fn unify_clock_domains( .get(&var) .map(|variable| variable.metadata.width) }; - // A copy sensed at the opposite edge of its source is a clock of its - // own: `always_ff @(negedge c)` with `assign c = clk` runs when `clk` - // falls, not with `clk`'s rising domain. - let polarity = |module: &SimModule, var: SourceVarId| { - module - .variables - .get(&var) - .and_then(|variable| match variable.metadata.kind { - DomainKind::ClockPosedge | DomainKind::ResetAsyncHigh => Some(true), - DomainKind::ClockNegedge | DomainKind::ResetAsyncLow => Some(false), - _ => None, - }) + 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); + } }; - let same_edge = |a: Option, b: Option| a.is_none() || b.is_none() || a == b; // Internal aliases (e.g. `assign clk_internal = clk_port;`) for logic_path in &sim_module.comb_blocks { @@ -1845,8 +1847,9 @@ fn unify_clock_domains( logic_path, |target| width(sim_module, target), |source| width(sim_module, source), - ) && same_edge(polarity(sim_module, target), polarity(sim_module, source)) - { + ) { + record_polarity(sim_module, *id, target); + record_polarity(sim_module, *id, source); drive_graph .entry(bindings.locate(*id, source)) .or_default() @@ -1868,11 +1871,9 @@ fn unify_clock_domains( for (_, logic_path) in &glue_block.input_ports { if let Some((GlueAddr::Child(child_var), GlueAddr::Parent(parent_var))) = whole_variable_copy(&glue_block.arena, logic_path, glue_width, glue_width) - && same_edge( - polarity(child_module, child_var), - polarity(sim_module, parent_var), - ) { + 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() @@ -1890,10 +1891,9 @@ fn unify_clock_domains( logic_path, |_| parent_width, glue_width, - ) && same_edge( - polarity(child_module, child_var), - polarity(sim_module, *parent_var), ) { + 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() @@ -1904,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 2a1edb00c63..b7cf39ab08a 100644 --- a/crates/celox/tests/derived_register_clock.rs +++ b/crates/celox/tests/derived_register_clock.rs @@ -788,3 +788,77 @@ fn a_systemverilog_clock_copy_sensed_at_the_falling_edge() { .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), + )); + } +}