Skip to content

feat(sim): add explicit clock edges to the simulator - #1391

Merged
mergify[bot] merged 17 commits into
masterfrom
feat/simulator-edge-api
Oct 17, 2026
Merged

mergify[bot] merged 17 commits into
masterfrom
feat/simulator-edge-api

Conversation

@tignear

@tignear tignear commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Simulator::tick(event) runs only the event's active edge and never moves the signal. There was no way to drive a falling edge, a cycle, or several coincident edges on the event-based simulator. This adds explicit edges in Rust, N-API, TypeScript and the browser/WASI bridge.

API

  • Simulator::posedge, negedge, edge(event, Edge), active_edge, inactive_edge, cycle (active then inactive edge), and edges(&[(event, Edge)]) for coincident edges.
  • An edge moves the event's signal to the new level, which stays there, and runs every domain sensitive to that transition:
    • the event's own domain on its active edge;
    • gated clocks computed from the signal, judged from the state before the edge;
    • gates that the commit moves, and domains clocked from state, transitively, as a timed step does.
  • An edge to the level the signal already has does nothing. An unknown (X) level counts as either level.
  • active_edge follows the event's DomainKind. Veryl's abstract clock follows the build's clock_type.
  • tick keeps its current behavior: the active edge, without moving the signal. Making it an alias of active_edge would leave clocks and resets at their active level, which would be a breaking change.
  • N-API adds edges(ids, rising, dirty?) and activeEdgeIsRising(id).
  • TypeScript adds posedge, negedge, activeEdge, inactiveEdge, cycle and edges on Simulator, and the ClockEdge type.
  • The WASM bridge now runs ticks and edges from build-time plans (edgePlansJson()) with split-phase kernels (eventPhaseWasmBytes(name, phase)). Derived clocks therefore also work in the browser/WASI runtime.
  • The runner is also exported as @celox-sim/celox/wasm-edges. The playground now uses it:
    • its Simulator has posedge, negedge, activeEdge, inactiveEdge, cycle and edges;
    • its ticks and timed Simulation steps fire derived and gated clocks as the native simulator does;
    • Monaco now gets every declaration file of the package, so the new methods type-check in testbenches.
  • The wasm32 handle now numbers events as the native simulator does. Before, a clock that only drives a gated clock (no always_ff of its own) was no event in the browser.

Performance

  • An edge of a clock with no derived domains moves the signal and goes through the tick path. The inactive edge of a clock that combinational logic does not read leaves combinational logic settled, so the next active edge does not settle again.
  • cycle() reads everything it needs before the domain runs, then stores the inactive level. An inactive edge of a known-polarity event stores its level without reading it, since it can run nothing.
  • criterion medians in ms, taskset, tick and cycle paired per round, 6 rounds, load 1–2.5:
design tick cycle cycle / tick
plain 1.586 1.729 1.09
gated (clk & en) 3.342 4.702 1.42
divided 3.159 3.514 1.12
  • In the gated design, combinational logic reads clk, so its falling edge really has to evaluate the gate.
  • Separate posedge()/negedge() calls right after a kernel can still hit the aliasing stall in some builds (up to about +0.5 tick). cycle() avoids it.

Runtime events (#1361)

  • An edge's move of its clock is a host write. It goes through the host watch, as IOContext writes do, so a timed simulation's waits and counters see it.
  • The edge runtime takes the write as a callback (SignalWrite; set_level for hosts that observe nothing).
  • Edges on a simulation that a $finish froze change nothing. A $finish during an edge is left to the host, as a tick does.
  • Rounds judge gates with their lever at its new level. So an active edge uses the active level, as Phase::Active did, and an inactive edge uses the inactive level.

Tests

  • crates/celox/tests/edge_api.rs covers polarity, coincident edges, derived and gated clocks, X levels, and the dirty flag after an inactive edge.
  • The shared-suite harness now pulses events with explicit edges, as the Verilator and Icarus testbenches do. This re-enables the ~clk case on every Celox backend.
  • TypeScript: native addon and WASM bridge tests (simulator-edges-addon.test.ts, wasm-edges.test.ts).
  • Playground: playground-simulator-wasm.test.ts drives the playground runtime over modules from the wasm32 addon (expected counts are the native simulator's), and playground-dts.test.ts checks the Monaco declarations against the built dist.
  • cargo nextest run --workspace --features celox/systemverilog: 13811 passed. CI's clippy (--all-targets) and fmt are clean. pnpm test:js passes (celox: 410 tests), and typecheck and biome are clean.
  • After the rebase onto feat(runtime)!: wake processes on events in front of the batch step #1361 and the port, all of these pass:
    • nextest on the affected crates with --features celox/systemverilog: 11850 tests;
    • CI's clippy command and cargo fmt --check;
    • pnpm test:js, pnpm typecheck and pnpm lint;
    • CI's napi-wasm job, including the playground's test:wasm (15 tests).
  • Before the rebase (WASM build and playground changes), all of these passed:
    • nextest on the affected crates with --features celox/systemverilog: 11778 tests;
    • CI's clippy command and cargo fmt --check;
    • pnpm test:js, pnpm typecheck and pnpm lint;
    • CI's napi-wasm job, including the playground's test:wasm (15 tests).

Not covered

  • Nothing from the earlier list. The wasm32 addon now builds locally:
    • The dev shell lacked the wasm32-wasip1-threads target; the flake adds it.
    • zstd-sys needs WASI SDK clang as CC_wasm32_wasip1_threads, as CI sets it.
    • CI's napi-wasm job (addon build, TS build, playground test:wasm) passes locally.

Follow-up: #1384 (both edges of one SystemVerilog clock) gets Simulator tests that drive the falling edge directly once this lands.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-17T00:25:40.762165Z 7d93220 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@tignear tignear added merge-ready Request merging through Mergify after required checks and removed merge-ready Request merging through Mergify after required checks labels Oct 17, 2026
@mergify

mergify Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Queued — the merge queue status continues in this comment ↓.

@tignear
tignear force-pushed the feat/simulator-edge-api branch from 439b3ae to 193197f Compare October 17, 2026 04:21
@tignear tignear added the merge-ready Request merging through Mergify after required checks label Oct 17, 2026
@tignear
tignear force-pushed the feat/simulator-edge-api branch from 193197f to 2679010 Compare October 17, 2026 20:49
Simulator::edge, posedge, negedge, active_edge, inactive_edge, cycle and
the coincident edges move an event signal's level and run every domain
sensitive to the transition, as a timed step does: the event's own domain
on its active edge, gated clocks judged from the pre-edge state, gates the
commit moves and state-derived cascades. tick keeps its behaviour: the
active edge without moving the signal.

Change-Id: I21a0510f1955ce1a3f9f047c9f55c4f1344b3e81
An edge that moves an event signal without running a domain marked
combinational logic stale, so a cycle's falling edge forced a full settle
before the next rising edge even when only flip-flops see the clock.
Report such moves as Stored when combinational logic does not read the
signal and keep the pending state as it was, in the simulator, the N-API
handle (which now takes the caller's dirty flag) and the WASM edge runner.
An edge of an event without derived domains now moves the signal and runs
the event's domain through tick itself. The bench binary's generic edge
path ran the same flip-flop kernel about 1.8 times slower per cycle than
tick did, with equal instruction counts; going through tick keeps cycle
within a few percent of tick and records tick timing diagnostics for
edges too.
CI builds the N-API addon for wasm32-wasip1-threads; the dev shell's
toolchain only had wasm32-unknown-unknown, so the same build could not run
locally. With the target and WASI SDK clang as CC_wasm32_wasip1_threads,
the CI napi-wasm job runs as is.
The wasm32 handle listed only events with flip-flops of their own, so a
clock that only drives a gated clock was no event in the browser and its
edges never reached the gated domain. Events, their kernels and the edge
plans now follow the native event order (celox::event_addresses), and
kernels are found through the canonical event.
The runner has no other imports, so browser hosts that instantiate the
kernels themselves can use it without the addon loader. cachedEdgeKernels
holds the kernel lookup both hosts need, and the runner and bridge accept
events without a kernel of their own.
…unner

The playground's simulators now expose posedge, negedge, activeEdge,
inactiveEdge, cycle and edges, and their ticks and timed steps fire
derived and gated clocks as the native simulator does: the worker passes
the edge plans and split-phase kernels, and the runtime moved from main.ts
into playground-simulator.ts around the package's WASM edge runner. Monaco
now gets every declaration file of the package, so the new methods and
subpaths resolve in testbenches.
… $finish

Since the timed runtime wakes processes on host writes (#1361), an edge's
move of its clock signal is such a write: it now goes through the host
watch, as IOContext writes do, so waits and counters a timed simulation
keeps see it. The edge runtime takes the write as a callback (SignalWrite;
set_level for hosts that observe nothing). Edges on a simulation a
$finish froze change nothing, and a $finish during an edge is left to
the host, as a tick does.

Change-Id: If100c01610ac1c9d5169c0e64d37f7cc62b08390
After the rebase onto the process-waking runtime, a cycle on the plain
overhead bench cost 1.75 ticks with the same instructions as before: loads
of host tables in the edge path right after the flip-flop kernel waited
behind the kernel's stores (4 KiB aliasing; which loads alias depends on
the heap layout). cycle() now reads everything before the domain runs,
then stores the inactive level, and an inactive edge of a known-polarity
event stores its level without reading it first, as it can run nothing.
cycle is 1.09 ticks again on the bench.

Change-Id: I672bb62499cbecdc7fce913ad48ff7d08053a637
@tignear
tignear force-pushed the feat/simulator-edge-api branch from 2679010 to b32c10b Compare October 17, 2026 21:33
@mergify mergify Bot added the queued Mergify queue entry label Oct 17, 2026
@mergify

mergify Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-17 22:07 UTC · Rule: changes · triggered by rule Queue explicitly requested merges
  • ✅ Checks passed · on draft merge queue: checking #1391 on master (c96fc06) #1433
  • ✅ Merged — 2026-10-17 23:00 UTC · at 358d3468c7948375242fd3e3a09af2b9cc6855c1 · merge

This pull request spent 52 minutes 18 seconds in the queue, including 50 minutes 37 seconds running CI.

Required conditions to merge
  • check-success = Conventional Commit title
  • check-success = Queue validation
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/Lint & Format
    • check-neutral = @github-actions/Lint & Format
    • check-skipped = @github-actions/Lint & Format
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/NAPI Build (Linux ARM64 GNU)
    • check-neutral = @github-actions/NAPI Build (Linux ARM64 GNU)
    • check-skipped = @github-actions/NAPI Build (Linux ARM64 GNU)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/NAPI Build (Windows)
    • check-neutral = @github-actions/NAPI Build (Windows)
    • check-skipped = @github-actions/NAPI Build (Windows)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/Rust Test & NAPI Build
    • check-neutral = @github-actions/Rust Test & NAPI Build
    • check-skipped = @github-actions/Rust Test & NAPI Build
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/JS Test (Ubuntu)
    • check-neutral = @github-actions/JS Test (Ubuntu)
    • check-skipped = @github-actions/JS Test (Ubuntu)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/JS Test (Windows)
    • check-neutral = @github-actions/JS Test (Windows)
    • check-skipped = @github-actions/JS Test (Windows)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/Conventional Commit title
    • check-neutral = @github-actions/Conventional Commit title
    • check-skipped = @github-actions/Conventional Commit title

@mergify mergify Bot mentioned this pull request Oct 17, 2026
39 of 70 tasks
@mergify
mergify Bot merged commit 358d346 into master Oct 17, 2026
29 checks passed
@mergify
mergify Bot deleted the feat/simulator-edge-api branch October 17, 2026 23:00
@mergify mergify Bot removed the queued Mergify queue entry label Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-ready Request merging through Mergify after required checks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant