From 2504f6b6ec5d6fa2aaed28c4a4d3e2d605d4202c Mon Sep 17 00:00:00 2001 From: tignear Date: Fri, 23 Oct 2026 09:22:39 +0900 Subject: [PATCH 1/2] fix(sv): run waiters of blocking edge writes before the NBA region A domain whose blocking results are observed (active_commits) committed them in the active region only when a zero delay was pending. In an ordinary round the process phase returned once the active region was empty and the domain published its blocking and nonblocking results together, so a process the blocking write woke ran after the edge's nonblocking updates (IEEE 1800-2023 4.5). When nothing is ready or inactive and a marked trigger evaluates such a domain, the process phase now evaluates it before the NBA region as it does before an inactive region: the processes its blocking results wake run in the active region, and the nonblocking results publish with the NBA region. Fixes #1721. Change-Id: I620f6bf29af4d7d5cba5e1782aa6ef7fb6d3eb1b --- .../celox-runtime/src/simulation/process.rs | 10 ++++++++++ crates/celox-runtime/src/simulation/round.rs | 19 +++++++++++++++++++ docs/guide/external-frontends.md | 2 ++ docs/guide/systemverilog-reference.md | 6 ++++++ 4 files changed, 37 insertions(+) diff --git a/crates/celox-runtime/src/simulation/process.rs b/crates/celox-runtime/src/simulation/process.rs index cd8536a049a..3faece18b35 100644 --- a/crates/celox-runtime/src/simulation/process.rs +++ b/crates/celox-runtime/src/simulation/process.rs @@ -294,6 +294,16 @@ impl SimulationState { continue; } if self.process_state.inactive.is_empty() { + // A domain whose blocking results are observed commits them + // in the active region: the processes they wake run before + // the NBA region publishes its nonblocking results (IEEE + // 1800-2023 4.5). + if self.active_commit_triggered(executor.backend(), round) + && self.evaluate_before_inactive(executor, current_time, round)? + { + self.observe_driven(executor.backend()); + continue; + } return Ok(()); } // The active region is empty: the domains it triggered evaluate diff --git a/crates/celox-runtime/src/simulation/round.rs b/crates/celox-runtime/src/simulation/round.rs index f5dcb5ffac9..bccba803ea5 100644 --- a/crates/celox-runtime/src/simulation/round.rs +++ b/crates/celox-runtime/src/simulation/round.rs @@ -221,6 +221,25 @@ impl SimulationState { } } + /// A marked trigger evaluates a domain that commits its blocking + /// results in the active region (the filter of + /// [`Self::evaluate_before_inactive`]). + pub(super) fn active_commit_triggered(&self, backend: &B, round: &Round) -> bool { + if self.model.active_commits.is_empty() { + return false; + } + backend.get_triggered_bits().iter().any(|id| { + let info = self.model.event_info[id]; + info.canonical_id != usize::MAX + && info.eval_only_event.is_some() + && self.model.active_commits.contains(info.canonical_id) + && (!info.is_cascaded + || round.counted_triggered.contains(id) + || round.edge_approved.contains(id) + || self.stable_edge_triggered(backend, id)) + }) + } + /// Evaluate the triggered domains before an inactive region runs. Their /// nonblocking assignments stay in the working state until the NBA /// region. A domain that commits its blocking results does so now, in diff --git a/docs/guide/external-frontends.md b/docs/guide/external-frontends.md index d3a9f60ee95..64a0d0f9947 100644 --- a/docs/guide/external-frontends.md +++ b/docs/guide/external-frontends.md @@ -173,6 +173,8 @@ declaration order. A time slot follows the regions of IEEE 1800-2023 4.5: processes, including those an edge wakes, run in the active region before the registers of that edge publish their nonblocking assignments, so a process woken by `@(posedge clk)` reads the register outputs from before the edge. +A process woken by a register domain's blocking write also runs in the +active region, before that domain's nonblocking assignments publish. Zero-delay `InactiveDelay` processes run after the active region, and nonblocking assignments publish only when both are empty. A process that drives a register's input at the same edge races with the register, as in diff --git a/docs/guide/systemverilog-reference.md b/docs/guide/systemverilog-reference.md index 61c4efccee5..ae2d1b91d03 100644 --- a/docs/guide/systemverilog-reference.md +++ b/docs/guide/systemverilog-reference.md @@ -391,6 +391,12 @@ unsupported, including when the inference option is enabled. in the parent sees only the settled value, as in the previous item. Verilator 5.052 sees only the final value of an evaluation; Icarus 13.0 also sees the pulse through the port. +- A blocking write of an edge-triggered `always` is an update of the active + region: the processes it wakes run before the same edge's nonblocking + updates. With `y = ~y; q <= q + 1;`, a waiter on `@(y)` reads `q` from + before the edge and waits again before the nonblocking updates + (IEEE 1800-2023 4.5). Icarus 13.0 gives the same values; Verilator 5.052 + resumes the waiter after the nonblocking updates. ### Variable lifetimes From 51942d8f9652922797ec443a0037d1d36bdaca27 Mon Sep 17 00:00:00 2001 From: tignear Date: Fri, 23 Oct 2026 09:22:39 +0900 Subject: [PATCH 2/2] test(sv): cover waiters of blocking edge writes before nonblocking updates Change-Id: I076b9f122c74f6ba75e8e635c8643c981cb88b2b --- crates/celox-test-suite/LIMITATIONS.md | 1 + .../src/sv/cases/timing.vtest | 68 +++++++++++++++++++ .../sv_active_region_waiters_verilator.json | 52 ++++++++++++++ .../verification/sv/icarus/timing.json | 33 +++++++++ .../verification/sv/limitations.json | 18 +++++ .../verification/sv/verilator/timing.json | 57 ++++++++++++++++ .../tests/frontends/systemverilog/timing.rs | 3 + 7 files changed, 232 insertions(+) create mode 100644 crates/celox-test-suite/verification/repros/sv_active_region_waiters_verilator.json diff --git a/crates/celox-test-suite/LIMITATIONS.md b/crates/celox-test-suite/LIMITATIONS.md index 666569db647..4cb4d453e62 100644 --- a/crates/celox-test-suite/LIMITATIONS.md +++ b/crates/celox-test-suite/LIMITATIONS.md @@ -544,6 +544,7 @@ four-state expectations; the assertions remain enabled on all Celox variants. | `sv_verilator_time_in_combinational_logic` | execute | verilator | 1 | Verilator 5.052 evaluates an always_comb that reads $time again when the clocking event of the suite testbench fires, although no variable it reads has changed: at 3 ns y is 3 instead of 0. IEEE 1800-2023 9.2.2.2.1 makes always_comb sensitive to the variables it reads, and $time (20.3.1) is none. A standalone probe prints y=0 in Icarus 13.0 and Verilator without a clocking block, and y=3 in Verilator with one. | | `sv_verilator_intermediate_writes` | execute | verilator | 6 | Verilator 5.052 wakes a process waiting on a variable only for the value an evaluation leaves: an always_comb or edge-triggered body that rewrites a variable 1 -> 0 -> 1, and nonblocking updates `y <= 0; y <= 1;` from 1, wake no waiter, nor does a pulse that one always_ff writes to some bits of a variable while other processes write its other bits, or a nonblocking update that rises from the final blocking value. IEEE 1800-2023 4.3 and 9.4.2 make every store an update event that triggers the waiter, and 10.4.2 applies nonblocking updates in issue order, each its own update. [Standalone probes](verification/repros/sv_intermediate_writes_verilator.json) print the spec counts in Icarus 13.0 and 1 or 0 in Verilator. | | `sv_icarus_port_pulses` | execute | icarus | 1 | Icarus 13.0 collapses a child's output port with the parent's net, so a waiter in the parent sees each value the child's always_comb writes inside one evaluation and counts 3 instead of 1. Both are conforming: the port is a continuous assignment (IEEE 1800-2023 23.3.3), evaluated after the child's process yields, and 4.7 leaves that order open. The case asserts the settled count, which Verilator 5.052 and Celox give. The [standalone probe](verification/repros/sv_port_pulses_icarus.json) retains both outputs. | +| `sv_verilator_active_region_waiters` | execute | verilator | 2 | Verilator 5.052 resumes a process woken by a blocking write of an edge-triggered process only after the same edge's nonblocking updates: with `y = ~y; q <= q + 1;` a waiter on `@(y)` reads the updated q, and with `y = 1; y <= 0; y <= 1;` from 0 it does not wait again before the nonblocking updates and counts 1 instead of 3. IEEE 1800-2023 4.5 makes the blocking write an update event of the active region and enters the NBA region only when the active and inactive regions are empty, so the waiter runs first (9.4.2). [Standalone probes](verification/repros/sv_active_region_waiters_verilator.json) print the spec values in Icarus 13.0 and the post-NBA values in Verilator. | The [system-function edge-case evidence](verification/repros/system_function_edge_cases.json) retains the failed Icarus runs and minimal sources. Cover comparisons enable diff --git a/crates/celox-test-suite/src/sv/cases/timing.vtest b/crates/celox-test-suite/src/sv/cases/timing.vtest index 912ecf270c5..d696896ac9a 100644 --- a/crates/celox-test-suite/src/sv/cases/timing.vtest +++ b/crates/celox-test-suite/src/sv/cases/timing.vtest @@ -2014,3 +2014,71 @@ module Top(input bit clk,output bit[7:0]seen);bit q;Child c(clk,q);initial forev (top Top) (run_to_finish) (expect_output "y=1001 n=3\np=10 q=01\n")) + +; A blocking write of an edge process is an update of the active region: +; the process waiting on it runs there, before the NBA region applies the +; same edge's nonblocking update (IEEE 1800-2023 4.5, 9.4.2). +(case blocking_write_waiters_run_before_nonblocking_updates + (source "active.sv" #" + module Top; + bit clk, y; + int q; + always @(posedge clk) begin y = ~y; q <= q + 1; end + initial forever begin @(y); $display("y=%0d q=%0d", y, q); end + initial begin + #1 clk = 1; + #1 clk = 0; + #1 clk = 1; + #1 $finish; + end + endmodule + "#) + (top Top) + (run_to_finish) + (expect_output "y=1 q=0\ny=0 q=1\n")) + +; The waiter the blocking write wakes waits again before the nonblocking +; updates, so they wake it once more. +(case blocking_write_waiters_rearm_before_nonblocking_updates + (source "rearm.sv" #" + module Top; + bit clk, y; + int n, r; + always @(posedge clk) begin y = 1; y <= 0; y <= 1; end + initial forever begin @(y); n++; end + initial forever begin @(posedge y); r++; end + initial begin + #1 clk = 1; + #1 clk = 0; + #1 clk = 1; + #1 $display("n=%0d r=%0d", n, r); + $finish; + end + endmodule + "#) + (top Top) + (run_to_finish) + (expect_output "n=3 r=3\n")) + +; Processes and edge processes woken by the blocking write read the +; variables the same edge updates nonblocking before the update. +(case blocking_write_edges_read_values_before_nonblocking_updates + (source "derived.sv" #" + module Top; + bit clk, g; + int q, r, s; + always @(posedge clk) begin g = ~g; q <= q + 1; end + always @(g) r <= q; + initial forever begin @(g); s = q; end + initial begin + #1 clk = 1; + #1 clk = 0; + #1 clk = 1; + #1 $display("q=%0d r=%0d s=%0d", q, r, s); + $finish; + end + endmodule + "#) + (top Top) + (run_to_finish) + (expect_output "q=2 r=1 s=1\n")) diff --git a/crates/celox-test-suite/verification/repros/sv_active_region_waiters_verilator.json b/crates/celox-test-suite/verification/repros/sv_active_region_waiters_verilator.json new file mode 100644 index 00000000000..2d4d602fd30 --- /dev/null +++ b/crates/celox-test-suite/verification/repros/sv_active_region_waiters_verilator.json @@ -0,0 +1,52 @@ +{ + "standard": "IEEE 1800-2023 4.5 and 9.4.2", + "expected": "A blocking write of an edge-triggered process is an update event of the active region; the process waiting on it is resumed in the active region, and the NBA region is entered only when the active and inactive regions are empty. The waiter therefore reads the values before the same edge's nonblocking updates, and waits again before those updates.", + "probes": [ + { + "name": "active_waiter", + "note": "The suite case blocking_write_waiters_run_before_nonblocking_updates.", + "source": "module Top;\n bit clk, y;\n int q;\n always @(posedge clk) begin y = ~y; q <= q + 1; end\n initial forever begin @(y); $display(\"y=%0d q=%0d\", y, q); end\n initial begin #1 clk = 1; #1 clk = 0; #1 clk = 1; #1 $finish; end\nendmodule\n", + "verilator_compile": { + "command": "verilator --binary --timing -Wno-fatal --top-module Top --Mdir /obj /probe.sv", + "exit_code": 0 + }, + "verilator_run": { + "command": "/obj/VTop", + "exit_code": 0, + "output": "y=1 q=1\ny=0 q=2\n- /probe.sv:6: Verilog $finish\n- S i m u l a t i o n R e p o r t: Verilator 5.052 2026-09-05\n- Verilator: $finish at 4ps; walltime 0.000 s; speed 44.186 ns/s\n- Verilator: cpu 0.000 s on 1 threads; allocated 4 MB\n" + }, + "icarus_compile": { + "command": "iverilog -g2012 -s Top -o /probe.vvp /probe.sv", + "exit_code": 0 + }, + "icarus_run": { + "command": "vvp /probe.vvp", + "exit_code": 0, + "output": "y=1 q=0\ny=0 q=1\n/probe.sv:6: $finish called at 4 (1s)\n" + } + }, + { + "name": "rearm", + "note": "The suite case blocking_write_waiters_rearm_before_nonblocking_updates.", + "source": "module Top;\n bit clk, y;\n int n, r;\n always @(posedge clk) begin y = 1; y <= 0; y <= 1; end\n initial forever begin @(y); n++; end\n initial forever begin @(posedge y); r++; end\n initial begin #1 clk = 1; #1 clk = 0; #1 clk = 1; #1 $display(\"n=%0d r=%0d\", n, r); $finish; end\nendmodule\n", + "verilator_compile": { + "command": "verilator --binary --timing -Wno-fatal --top-module Top --Mdir /obj /probe.sv", + "exit_code": 0 + }, + "verilator_run": { + "command": "/obj/VTop", + "exit_code": 0, + "output": "n=1 r=1\n- /probe.sv:7: Verilog $finish\n- S i m u l a t i o n R e p o r t: Verilator 5.052 2026-09-05\n- Verilator: $finish at 4ps; walltime 0.000 s; speed 143.266 ns/s\n- Verilator: cpu 0.000 s on 1 threads; allocated 4 MB\n" + }, + "icarus_compile": { + "command": "iverilog -g2012 -s Top -o /probe.vvp /probe.sv", + "exit_code": 0 + }, + "icarus_run": { + "command": "vvp /probe.vvp", + "exit_code": 0, + "output": "n=3 r=3\n/probe.sv:7: $finish called at 4 (1s)\n" + } + } + ] +} diff --git a/crates/celox-test-suite/verification/sv/icarus/timing.json b/crates/celox-test-suite/verification/sv/icarus/timing.json index ec43a64d592..8c54f5c6013 100644 --- a/crates/celox-test-suite/verification/sv/icarus/timing.json +++ b/crates/celox-test-suite/verification/sv/icarus/timing.json @@ -475,6 +475,39 @@ "tag_reasons": {}, "tags": [] }, + { + "category": "Sequential", + "detail": "", + "expectation": "Simulation", + "name": "timing::blocking_write_waiters_run_before_nonblocking_updates", + "phase": "execute", + "status": "passed", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] + }, + { + "category": "Sequential", + "detail": "", + "expectation": "Simulation", + "name": "timing::blocking_write_waiters_rearm_before_nonblocking_updates", + "phase": "execute", + "status": "passed", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] + }, + { + "category": "Sequential", + "detail": "", + "expectation": "Simulation", + "name": "timing::blocking_write_edges_read_values_before_nonblocking_updates", + "phase": "execute", + "status": "passed", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] + }, { "category": "Sequential", "detail": "", diff --git a/crates/celox-test-suite/verification/sv/limitations.json b/crates/celox-test-suite/verification/sv/limitations.json index cc74a052a47..4b2c5655c63 100644 --- a/crates/celox-test-suite/verification/sv/limitations.json +++ b/crates/celox-test-suite/verification/sv/limitations.json @@ -1973,5 +1973,23 @@ "timing::child_port_pulses_reach_waiters_settled" ] } + }, + { + "id": "sv_verilator_active_region_waiters", + "category": "simulator_disagreement", + "phase": "execute", + "observed_version": "Verilator 5.052", + "reason": "Verilator 5.052 resumes a process woken by a blocking write of an edge-triggered process only after the same edge's nonblocking updates: with `y = ~y; q <= q + 1;` a waiter on `@(y)` reads the updated q, and with `y = 1; y <= 0; y <= 1;` from 0 it does not wait again before the nonblocking updates and counts 1 instead of 3. IEEE 1800-2023 4.5 makes the blocking write an update event of the active region and enters the NBA region only when the active and inactive regions are empty, so the waiter runs first (9.4.2). Standalone probes print the spec values in Icarus 13.0.", + "upstream": [], + "evidence": [ + "verification/repros/sv_active_region_waiters_verilator.json", + "LIMITATIONS.md#systemverilog-suite" + ], + "cases": { + "verilator": [ + "timing::blocking_write_waiters_run_before_nonblocking_updates", + "timing::blocking_write_waiters_rearm_before_nonblocking_updates" + ] + } } ] diff --git a/crates/celox-test-suite/verification/sv/verilator/timing.json b/crates/celox-test-suite/verification/sv/verilator/timing.json index 717dc6e5563..07c3f4d070c 100644 --- a/crates/celox-test-suite/verification/sv/verilator/timing.json +++ b/crates/celox-test-suite/verification/sv/verilator/timing.json @@ -499,6 +499,63 @@ "tag_reasons": {}, "tags": [] }, + { + "category": "Sequential", + "detail": "Verilator 5.052 resumes a process woken by a blocking write of an edge-triggered process only after the same edge's nonblocking updates: with `y = ~y; q <= q + 1;` a waiter on `@(y)` reads the updated q, and with `y = 1; y <= 0; y <= 1;` from 0 it does not wait again before the nonblocking updates and counts 1 instead of 3. IEEE 1800-2023 4.5 makes the blocking write an update event of the active region and enters the NBA region only when the active and inactive regions are empty, so the waiter runs first (9.4.2). Standalone probes print the spec values in Icarus 13.0.", + "expectation": "Simulation", + "known_issue": { + "category": "simulator_disagreement", + "evidence": [ + "verification/repros/sv_active_region_waiters_verilator.json", + "LIMITATIONS.md#systemverilog-suite" + ], + "id": "sv_verilator_active_region_waiters", + "observed_version": "Verilator 5.052", + "phase": "execute", + "reason": "Verilator 5.052 resumes a process woken by a blocking write of an edge-triggered process only after the same edge's nonblocking updates: with `y = ~y; q <= q + 1;` a waiter on `@(y)` reads the updated q, and with `y = 1; y <= 0; y <= 1;` from 0 it does not wait again before the nonblocking updates and counts 1 instead of 3. IEEE 1800-2023 4.5 makes the blocking write an update event of the active region and enters the NBA region only when the active and inactive regions are empty, so the waiter runs first (9.4.2). Standalone probes print the spec values in Icarus 13.0.", + "upstream": [] + }, + "name": "timing::blocking_write_waiters_run_before_nonblocking_updates", + "phase": "skip", + "status": "ignored", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] + }, + { + "category": "Sequential", + "detail": "Verilator 5.052 resumes a process woken by a blocking write of an edge-triggered process only after the same edge's nonblocking updates: with `y = ~y; q <= q + 1;` a waiter on `@(y)` reads the updated q, and with `y = 1; y <= 0; y <= 1;` from 0 it does not wait again before the nonblocking updates and counts 1 instead of 3. IEEE 1800-2023 4.5 makes the blocking write an update event of the active region and enters the NBA region only when the active and inactive regions are empty, so the waiter runs first (9.4.2). Standalone probes print the spec values in Icarus 13.0.", + "expectation": "Simulation", + "known_issue": { + "category": "simulator_disagreement", + "evidence": [ + "verification/repros/sv_active_region_waiters_verilator.json", + "LIMITATIONS.md#systemverilog-suite" + ], + "id": "sv_verilator_active_region_waiters", + "observed_version": "Verilator 5.052", + "phase": "execute", + "reason": "Verilator 5.052 resumes a process woken by a blocking write of an edge-triggered process only after the same edge's nonblocking updates: with `y = ~y; q <= q + 1;` a waiter on `@(y)` reads the updated q, and with `y = 1; y <= 0; y <= 1;` from 0 it does not wait again before the nonblocking updates and counts 1 instead of 3. IEEE 1800-2023 4.5 makes the blocking write an update event of the active region and enters the NBA region only when the active and inactive regions are empty, so the waiter runs first (9.4.2). Standalone probes print the spec values in Icarus 13.0.", + "upstream": [] + }, + "name": "timing::blocking_write_waiters_rearm_before_nonblocking_updates", + "phase": "skip", + "status": "ignored", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] + }, + { + "category": "Sequential", + "detail": "", + "expectation": "Simulation", + "name": "timing::blocking_write_edges_read_values_before_nonblocking_updates", + "phase": "execute", + "status": "passed", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [] + }, { "category": "Sequential", "detail": "", diff --git a/crates/celox/tests/frontends/systemverilog/timing.rs b/crates/celox/tests/frontends/systemverilog/timing.rs index 9394797babc..204f36c4992 100644 --- a/crates/celox/tests/frontends/systemverilog/timing.rs +++ b/crates/celox/tests/frontends/systemverilog/timing.rs @@ -157,6 +157,9 @@ timed_cases! { fn staged_nonblocking_writes_follow_later_blocking_writes = "timing::staged_nonblocking_writes_follow_later_blocking_writes"; fn intermediate_combinational_writes_replace_split_definitions = "timing::intermediate_combinational_writes_replace_split_definitions"; fn intermediate_combinational_writes_reach_concatenated_ports = "timing::intermediate_combinational_writes_reach_concatenated_ports"; + fn blocking_write_waiters_run_before_nonblocking_updates = "timing::blocking_write_waiters_run_before_nonblocking_updates"; + fn blocking_write_waiters_rearm_before_nonblocking_updates = "timing::blocking_write_waiters_rearm_before_nonblocking_updates"; + fn blocking_write_edges_read_values_before_nonblocking_updates = "timing::blocking_write_edges_read_values_before_nonblocking_updates"; } #[test]