Skip to content

fix(sim): judge four-state edges of internally driven clocks by IEEE 1800 Table 9-2 - #1414

Merged
mergify[bot] merged 8 commits into
masterfrom
fix/four-state-edge-table
Oct 18, 2026
Merged

mergify[bot] merged 8 commits into
masterfrom
fix/four-state-edge-table

Conversation

@tignear

@tignear tignear commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Bug

In four-state designs, edges of clocks and asynchronous resets that the design drives itself (a register, combinational logic, a process) were judged on the value bit only. So 1 -> x, z -> 0, 0 -> z and x -> 1 never fired. x -> 0, 0 -> x and z -> 1 fired only because of how x (value 1) and z (value 0) are encoded. The same applied to a clock or reset that starts at x/z and is first driven through Simulation::schedule.

IEEE 1800-2023 9.4.2, Table 9-2:

  • posedge: 0->1, 0->x, 0->z, x->1, z->1
  • negedge: 1->0, 1->x, 1->z, x->0, z->0
  • no edge between x and z

Process event-control waits (timing.rs) and celox-vpi (external writes) already followed the table.

Fix

  • Edge rules (celox_design::edges, introduced by refactor(runtime)!: derive the schedule model once per program #1417): scheduled_edge and settled_edge now take edge levels (0, 1, x, z) and follow Table 9-2, with a unit test of every cell. The VPI layer's write-edge check now uses settled_edge instead of its own copy of the table.
  • Timed scheduler (celox-runtime/src/simulation/ after refactor(runtime)!: split the simulation scheduler into modules #1408 and refactor(runtime)!: derive the schedule model once per program #1417: batch.rs, edges.rs, round.rs, persist.rs, mod.rs): each event's edge baseline is now an edge level: 0, 1, or unknown plus the value bit. The baseline is kept in two bitsets, high and unknown. stable_edge follows Table 9-2. This covers every place the scheduler compares levels: scheduled values, process-driven transitions, publication pulses, cascaded and gated (clk & en) clocks, host-write detection, and both the batch round and the process round.
  • Derived tick plans (derived_tick.rs): the round flags keep the same level. The settled gate path moves an x or z lever to its inactive level before the edge.
  • Per-mode specialization: the edge loops are compiled once per state mode (FOUR_STATE const generic), so two-state designs run value-bit comparisons only. The four-state branches are out of line, so two-state callers keep their previous size and inlining. Four-state designs read the mask plane of edge-sensed signals only.
  • Checkpoints and state files carry the unknown baselines. State files record them under a new flag that is set only when some event is x or z, so existing files keep their encoding.
  • JIT and interpreter trigger code (Cranelift, WASM, x86, arm64, interpreter): unchanged. In the timed simulation those trigger bits are only hints: the scheduler replaces them with edges judged from its own baseline after each settle.
  • Explicit edges (feat(sim): add explicit clock edges to the simulator #1391, celox-runtime/src/edge.rs): the level a moved signal had is its edge level, mask included. Plain events already treated an x or z start as "not at the level". Gated clocks were judged from the level opposite to the edge's, so x & x rising to 1 & x fired; they are now judged from the lever's real level. The settled gate path moves an x or z lever back to that level before the edge.
  • WASM edge runner (packages/celox/src/wasm-edges.ts, also used by the playground): levels are 0, 1, x and z with the Table 9-2 rules, and levers move to their real level. Edge plans now say per signal whether it has a mask (fourState).
  • TS waitForCycles counts rising edges through x and z: a clock starting at x counts its first rise.

Tests

  • Shared suite, Veryl (four_state.vtest): a register clock, a clock computed from a register (e ? a & b : 'z), and register-driven async resets of both polarities. Each walks all 12 changes between 0/1/x/z. They run on every backend through all_backends! (native, parallel, cranelift, wasm, interp, SV emission).
  • Shared suite, SV (new edges group): the same three designs, a gated clock ticked from x/z levels, and a two-state variant.
  • Icarus: 0 mismatches (SV 5/5, Veryl 3/3).
  • Verilator: the two-state case passes. It reports the four-state cases unsupported because it cannot represent x/z.
  • Veryl's reference simulator misses edges through x/z, so its runs of the new cases are ignored.
  • Timed Simulation tests on every backend (interp, cranelift, wasm, tiered, native) in crates/celox/src/simulation.rs:
    • process-driven clocks
    • register, combinational and reset clocks
    • @(posedge c) / @(negedge c) waits on a register clock that walks through x/z, checked alongside flip-flop domains on the same clock
    • unknown baselines across checkpoint/restore and state files
  • Explicit posedge/negedge/cycle/edges of four-state clocks starting at x or z, on plain events, gated clocks and register clocks: on every backend (tests/four_state_explicit_edges.rs), through the native addon (simulator-edges-addon.test.ts), the WASM edge runner (wasm-edges.test.ts) and the playground's WASM build (playground-simulator-wasm.test.ts).
  • both_clock_edges::a_reset_at_both_edges: in four-state, rst rises from its initial unknown level. That is a posedge but no negedge, so low keeps its initial value until the first clock edge, matching Icarus. The test previously expected the negedge-reset register to run.
  • packages/celox: new waitForCycles x->1 test.

Performance (callgrind, measured loop of benches/overhead.rs)

Instruction counts of one benchmark iteration (10,000 ticks or 20,000 steps), native backend, Rust 1.99.1 from nix develop. The before build uses the runtime, celox_design::edges and the edge plans from origin/master after #1391 and #1431. Wall-clock runs were not usable on the shared host: a load average of 20-50 moved even unchanged code by ±30%.

Benchmark (two-state) before after change
simulator_tick_x10000 37,670,440 37,670,440 0.00%
simulator_cycle_x10000 40,420,438 40,320,438 -0.25%
simulator_tick_gated_x10000 46,580,472 46,450,472 -0.28%
simulator_cycle_gated_x10000 66,450,279 65,950,279 -0.75%
simulator_tick_divided_x10000 43,425,297 43,395,297 -0.07%
simulator_cycle_divided_x10000 50,455,295 50,590,295 +0.27%
simulation_step_x20000 58,831,156 58,641,156 -0.32%
simulation_step_divided_x20000 87,325,512 86,995,476 -0.38%

Four-state cost: these numbers were measured before the rebases onto #1361, #1408, #1417, #1391 and #1431; the two-state numbers above were remeasured after them.

Benchmark (four-state) before after change
simulator_tick_gated_4state 150,290,281 151,820,497 +1.0%
simulator_tick_divided_4state 112,395,296 113,555,296 +1.0%
simulation_step_4state 172,559,707 178,661,351 +3.5%
simulation_step_divided_4state 214,276,703 225,650,906 +5.3%

The four-state cost is the mask read on edge-sensed signals. The _4state variants and simulation_step_divided are new in benches/overhead.rs; existing benchmark names and designs are unchanged.

Validation

  • cargo fmt --all -- --check
  • CI's cargo clippy --locked -p celox-test-suite -p celox … --all-targets --features celox/systemverilog,celox-test-suite/verilator,celox-test-suite/icarus --no-deps, plus celox-runtime
  • RUSTDOCFLAGS=-D warnings cargo doc -p celox-runtime -p celox-design -p celox --no-deps
  • cargo nextest run -p celox-design -p celox-runtime -p celox -p celox-test-suite -p celox-vpi -p celox-napi -p lydite-celox --features celox/systemverilog with Rust 1.99.1: 11,668 passed. This includes refactor(runtime)!: split the simulation scheduler into modules #1408's golden executor call-trace tests, whose traces are unchanged.
  • Full workspace nextest before the rebases: 13,846 passed
  • packages/celox: vitest (415 tests); pnpm run typecheck and pnpm run lint for packages/celox and the playground; the playground's test:wasm on the WASI build (16 tests)

@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-17T06:49:39.488734Z 1db09d9 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 the merge-ready Request merging through Mergify after required checks label Oct 17, 2026
@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 07:37 UTC · Rule: changes · triggered by rule Queue explicitly requested merges
  • 🚫 Left the queue — 2026-10-17 07:41 UTC · at 1db09d9b026a340d261bc03c4f0a744da1f4c4bd

This pull request spent 3 minutes 49 seconds in the queue, with no time running CI.

Reason

The pull request conflicts with the base branch

Hint

You should update or rebase your pull request.

If you want to requeue this pull request, you can post a @mergifyio queue comment.

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

@mergify mergify Bot added dequeued Mergify stopped this queue entry; investigate before retrying and removed queued Mergify queue entry labels Oct 17, 2026
@tignear tignear removed the dequeued Mergify stopped this queue entry; investigate before retrying label Oct 17, 2026
@mergify mergify Bot added the dequeued Mergify stopped this queue entry; investigate before retrying label Oct 17, 2026
@tignear tignear removed the dequeued Mergify stopped this queue entry; investigate before retrying label Oct 17, 2026
@mergify mergify Bot added the dequeued Mergify stopped this queue entry; investigate before retrying label Oct 17, 2026
@tignear
tignear force-pushed the fix/four-state-edge-table branch from 1db09d9 to 5102458 Compare October 17, 2026 21:02
@mergify mergify Bot removed the dequeued Mergify stopped this queue entry; investigate before retrying label Oct 17, 2026
@tignear
tignear force-pushed the fix/four-state-edge-table branch 2 times, most recently from 3ed0159 to 66c9b62 Compare October 18, 2026 01:13
@mergify mergify Bot added the queued Mergify queue entry label Oct 18, 2026
@mergify

mergify Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-18 01:49 UTC · Rule: changes · triggered by rule Queue explicitly requested merges
  • 🚫 Left the queue — 2026-10-18 01:59 UTC · at 66c9b62c922d548252f9ec5a5b801bed980a9e5e

This pull request spent 9 minutes 30 seconds in the queue, with no time running CI.

Reason

The pull request conflicts with the base branch

The pull request was held in the queue, conflicting with at least one of 2 pull requests ahead of it. The conflict became a base branch one after #1402 merged.

Automatic conflict resolution can merge a conflict like this one instead of stopping on it, but only in a queue using the merge-batch or fast-forward merge method.

Hint

You should update or rebase your pull request.

If you want to requeue this pull request, you can post a @mergifyio queue comment.

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

@mergify mergify Bot added dequeued Mergify stopped this queue entry; investigate before retrying and removed queued Mergify queue entry labels Oct 18, 2026
Edges of clocks and asynchronous resets driven inside a four-state design
were judged on the value bit alone, so 1 -> x, z -> 0, 0 -> z and x -> 1
did not fire, while x -> 0, 0 -> x and z -> 1 did only by the accident of
the x and z encodings. IEEE 1800-2023 9.4.2, Table 9-2 makes a posedge any
change that leaves 0 or reaches 1 and a negedge any change that leaves 1 or
reaches 0, with no edge between x and z.

The edge rules of celox_design::edges now take edge levels (0, 1, x, z)
and follow the table; the VPI layer's write edges use the same rule. The
timed scheduler keeps an unknown bit next to each event's high bit and
judges scheduled, process-driven, published, cascaded and gated edges from
both. Derived tick plans keep the same level in their round flags, and the
settled gate path moves an x or z lever to its inactive level before the
edge. Explicit edges read the moved signal's level with its mask, so a
gated clock is judged from the lever's real level, x or z included,
rather than from the level opposite to the edge's. Checkpoints and state
files carry the unknown baselines; a state file
records them under a new flag only when there are any, so other files keep
their encoding.

The edge loops are instantiated per state mode and the four-state paths
stay out of line: two-state designs read only value bits, with the code
and inlining they had before, and four-state designs read the mask only of
edge-sensed signals.
Shared Veryl and SystemVerilog cases walk a register clock, a clock
computed from a register and register-driven asynchronous resets through
all twelve changes between 0, 1, x and z, and tick a gated clock from x and
z levels. Icarus agrees with every case; Verilator runs the two-state
variant only. Veryl's simulator misses edges through x and z, so its
reference runs are ignored.
A four-state clock that starts at x now counts its first rise to 1, and
rises from 0 to x or z count too (IEEE 1800-2023 Table 9-2). Two-state
clocks read only their value bit as before.
Add four-state variants of the derived-clock tick benchmarks and of
Simulation::step, and a Simulation::step benchmark with a register-driven
divider, so the cost of four-state edge detection is tracked. Existing
benchmark names and designs are unchanged.
In four-state simulation a reset that rises from its initial unknown level gives a posedge but no negedge, so a register on its negedge keeps its initial value until the first clock edge.
Posedges and negedges of plain events, gated clocks and register clocks
that start at x or z follow IEEE 1800-2023 Table 9-2 on every backend and
through the native addon: a gated clock is judged from the clock's real
level, so `x & x` rising to `1 & x` is no edge.
The WASM edge runner read event levels from the value bit and moved a
lever from x or z as if from the opposite level, so a gated clock rising
from `x & x` to `1 & x` fired. Levels are now 0, 1, x and z, with the
edge rules of IEEE 1800-2023 Table 9-2, and a lever is moved to its real
level before the edge. Edge plans say per signal whether it keeps an
unknown mask.
@mergify mergify Bot added queued Mergify queue entry and removed dequeued Mergify stopped this queue entry; investigate before retrying labels Oct 18, 2026
@mergify

mergify Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

This pull request spent 43 minutes 26 seconds in the queue, including 24 minutes 17 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 merged commit d53ecde into master Oct 18, 2026
29 checks passed
@mergify
mergify Bot deleted the fix/four-state-edge-table branch October 18, 2026 03:54
@mergify mergify Bot removed the queued Mergify queue entry label Oct 18, 2026
tignear added a commit that referenced this pull request Oct 18, 2026
Change-Id: I7f47a736828bf84dec906eca161314b39fcd1760
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