Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions crates/celox/tests/frontends/systemverilog/timing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,156 @@ fn event_expressions_with_effects_are_rejected() {
assert!(error.contains(construct), "{event}: {error}");
}
}

/// Timing features Celox does not implement yet are rejected with a message
/// naming the construct and its tracking issue, instead of being ignored.
/// The comments name the issue that implements a feature when the message
/// still points to the roadmap.
#[test]
fn unimplemented_timing_features_are_rejected() {
for (body, construct, issue) in [
("bit a; initial @(a iff a);", "iff-qualified event", 1444),
("initial fork #1; join", "fork-join block", 88),
("initial fork #1; join_none", "fork-join block", 88),
// #1711 names the construct.
(
"event e; initial -> e;",
"unsupported internal data type",
88,
),
(
"bit a, b; initial a = #1 b;",
"intra-assignment timing control",
88,
),
(
"bit a, b; initial a <= #1 b;",
"intra-assignment timing control",
88,
),
(
"bit a, b, c; initial a = @(posedge c) b;",
"intra-assignment timing control",
88,
),
("initial wait fork;", "wait fork statement", 88),
// #1711 names the construct.
("initial disable fork;", "disable statement", 88),
(
"bit a; task t(input bit x); wait (a ^ x); endtask initial t(0);",
"wait condition reading a static task argument",
88,
),
(
"bit a; task t(input bit x); @(a ^ x); endtask initial t(0);",
"event expression reading a static task argument",
88,
),
(
"bit a; function automatic bit f(); a = ~a; return a; endfunction initial @(f());",
"impure call `f` in an event expression",
88,
),
(
"bit q; task t(); int s = 1; #1 q = s; endtask initial t();",
"static local in a timed task",
88,
),
(
"bit a, y; assign #1 y = a;",
"delayed continuous assignment",
444,
),
// #1708: a plain `always` that is not a flip-flop runs as a process.
(
"bit [3:0] v; int n; always @(posedge v[0]) n++;",
"always_ff event expression",
464,
),
(
"bit [3:0] v; int n; always @(posedge v) n++;",
"multi-bit always_ff event signal",
88,
),
(
"bit a, b; int n; always @(posedge a or posedge b) n <= n + 1;",
"always_ff event control",
88,
),
// #1709
(
"bit [7:0] m[4]; initial #1 m[1] <= 5;",
"nonblocking assignment to an unpacked array in a process",
88,
),
(
"bit [3:0] q; int i = 2; initial #1 q[i] <= 1;",
"nonblocking assignment with a run-time position in a process",
88,
),
(
"bit [3:0] q; initial #1 q[5] <= 1;",
"nonblocking assignment outside its target",
88,
),
// #1710
(
"bit q; task t(); #1 q = 1; endtask always @* t();",
"timed task call in a combinational process",
88,
),
] {
let source = format!("module Top; {body} endmodule");
let error = celox::Simulation::from_sv_sources(
vec![(source.as_str(), std::path::Path::new("unimplemented.sv"))],
"Top",
)
.build()
.err()
.unwrap_or_else(|| panic!("`{body}` must be rejected"))
.to_string();
// The report wraps long lines behind a `│` gutter.
let error = error
.split(|c: char| c.is_whitespace() || c == '│')
.filter(|word| !word.is_empty())
.collect::<Vec<_>>()
.join(" ");
assert!(
error.contains(&format!("[tracking issue #{issue}]"))
&& error.contains(&format!("Unsupported SystemVerilog construct: {construct}")),
"{body}: {error}"
);
}
}

/// Only `initial` and `always` processes may suspend (IEEE 1800-2023
/// 9.2.2.2, 9.2.2.4 and 13.4); a timing control elsewhere is an error.
#[test]
fn timing_controls_outside_suspendable_processes_are_rejected() {
for (body, construct) in [
(
"bit a, y; always_comb begin #1 y = a; end",
"delay control in an always_comb or always_ff body",
),
(
"bit clk, a, q; always_ff @(posedge clk) begin @(a); q <= a; end",
"event control in an always_comb or always_ff body",
),
// #1711 reports this as illegal rather than unsupported.
(
"bit a; function automatic bit f(); #1; return a; endfunction",
"delay control in a subroutine",
),
] {
let source = format!("module Top; {body} endmodule");
let error = celox::Simulation::from_sv_sources(
vec![(source.as_str(), std::path::Path::new("outside.sv"))],
"Top",
)
.build()
.err()
.unwrap_or_else(|| panic!("`{body}` must be rejected"))
.to_string();
assert!(error.contains(construct), "{body}: {error}");
}
}
62 changes: 54 additions & 8 deletions docs/guide/systemverilog-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,30 @@ synthesis and is tested at the design level.
| Statements | `if` / `else`, `case`, `casez`, `casex`, `case ... inside`, `unique` / `priority`, `for`, `while`, `do ... while`, `repeat`, `forever` and `foreach` (unrolled when the trip count is constant, otherwise executed at run time), `break` / `continue` / `return`, immediate assertions |
| Functions | `function` and `task` with `input`, `output` and `inout` arguments, `return` or assignment to the function name, local variables and `localparam`s, selected and composite assignments; calls are inlined. Calls with constant arguments in constant expressions (parameters, ranges) are evaluated during elaboration |

### Timing controls {#timing-controls}

In a timed `Simulation`, `initial` and plain `always` processes can suspend:

| Control | Supported |
| --- | --- |
| `#delay` | integral delays, `#(min:typ:max)` (the typical value) and [time literals](#time-literal-delays); an X or Z delay counts as zero |
| `@(...)` | variables, `posedge` / `negedge` / `edge`, items joined by `or` or `,`, bit, part and run-time index selects, any expression including calls of pure functions, and `@*` / `@(*)`, which waits on the variables its statement reads |
| `wait (condition)` | any condition; an X or Z condition counts as false |
| Assignments | blocking and nonblocking; nonblocking updates take effect in the NBA region of the time slot |

An `always` with a sensitivity list other than `@*`, such as
`always @(a or b)`, runs as a process too. A `Simulator` runs no processes,
so such a design simulates without them there; use `Simulation`, or write
`always @*` or `always_comb` ([#1712](https://github.com/celox-sim/celox/issues/1712)).

### Timed task calls

An `initial` or ordinary `always` process can call tasks containing `#delay`,
`@(event)` and `wait (condition)`, including through nested tasks. Calls are
inlined into the caller's resumable state machine. A normal edge-sensitive
`always` that calls a timed task waits for that task to return before rearming
its event control. Functions, `always_comb`, `always @*` and `always_ff` cannot
call a timed task.
its event control. Functions, `always_comb` and `always_ff` cannot call a timed
task, and `always @*` cannot yet ([#1710](https://github.com/celox-sim/celox/issues/1710)).

Automatic task arguments and locals have separate storage for each activation
and survive suspension. Module-local static task arguments remain shared. Inputs and inouts
Expand All @@ -74,12 +90,12 @@ stream (IEEE 1800-2023 18.12).
Constraints (`with`), class methods, enum variables, unpacked arrays, nets,
constants, selects and concatenation arguments are unsupported and rejected.

### Time literal delays
### Time literal delays {#time-literal-delays}

Standalone delay controls accept time literals such as `#5ns`, `#(2.75ns)` and
`#40ps`, with all six units from `fs` to `s`. Values round at the declaring
scope's precision before conversion to scheduler ticks (5.8, 3.14.1). Real-valued
arithmetic expressions remain unsupported.
delays are not supported yet ([#1707](https://github.com/celox-sim/celox/issues/1707)).

### Built-in functions and tasks

Expand All @@ -103,10 +119,17 @@ it. The error carries the number of the issue that tracks the construct;
constructs without a dedicated issue point to the frontend roadmap, [#88](https://github.com/celox-sim/celox/issues/88).

- Classes. Timed tasks with static local declarations, recursive calls and `ref` arguments remain unsupported.
- Behavioral and verification constructs: `final`, intra-assignment timing controls,
- Behavioral and verification constructs: `final`, intra-assignment timing controls (`a = #1 b`),
delayed continuous assignments ([#444](https://github.com/celox-sim/celox/issues/444)),
named events, `fork` / `join`, concurrent assertions, `force` / `release`.
- Nonblocking assignments inside `always_latch` ([#431](https://github.com/celox-sim/celox/issues/431)); use blocking assignments for the supported latch subset. Level-sensitive sensitivity lists other than `@*` are unsupported.
named events and `->`, `fork` / `join`, `wait fork` / `disable fork`, concurrent assertions, `force` / `release`.
- Timing controls in processes: `iff` qualifiers ([#1444](https://github.com/celox-sim/celox/issues/1444)),
real-valued delays ([#1707](https://github.com/celox-sim/celox/issues/1707)), package-scoped delay values
([#1710](https://github.com/celox-sim/celox/issues/1710)), impure function calls in event expressions, and
`wait` or event expressions reading a static task argument. Nonblocking
assignments in a process cannot yet target an unpacked array element, a
run-time position or a constant position outside the target
([#1709](https://github.com/celox-sim/celox/issues/1709)).
- Nonblocking assignments inside `always_latch` ([#431](https://github.com/celox-sim/celox/issues/431)); use blocking assignments for the supported latch subset.
- Inferred latches inside `always_comb` are rejected by default. Set `inferred_latch = "warn"` or `"allow"` in `celox.toml` under `[diagnostics]` to permit retained blocking assignments. Ordinary `always @*` and explicit `always_latch` do not require this option.
- Ports and instances: non-ANSI port declarations ([#426](https://github.com/celox-sim/celox/issues/426)), `ref` ports
([#427](https://github.com/celox-sim/celox/issues/427)), wildcard `.*` connections ([#442](https://github.com/celox-sim/celox/issues/442)), gate primitives ([#457](https://github.com/celox-sim/celox/issues/457)),
Expand All @@ -115,7 +138,9 @@ constructs without a dedicated issue point to the frontend roadmap, [#88](https:
array, ANSI port default values, tagged unions, soft packed unions, packed union assignment patterns, internal nets without a driver ([#460](https://github.com/celox-sim/celox/issues/460)),
unpacked structs, strings and `real`.
- Sequential processes: `iff` qualifiers ([#452](https://github.com/celox-sim/celox/issues/452)) and edge operands other than a
plain signal ([#464](https://github.com/celox-sim/celox/issues/464)) in event lists.
plain signal ([#464](https://github.com/celox-sim/celox/issues/464)) in event lists. A plain `always` with
such an edge, a multi-bit edge signal, or several edges without a reset is
rejected too, until it runs as a process ([#1708](https://github.com/celox-sim/celox/issues/1708)).
- Combinational processes: nonblocking assignments inside `always_comb`
([#453](https://github.com/celox-sim/celox/issues/453)).
- Loops: a procedural loop unrolls at most 10,000 iterations; a combinational
Expand Down Expand Up @@ -290,6 +315,27 @@ their argument expressions (IEEE 1800-2023 9.4.2.2 and 9.2.2.2.1).
Nonblocking latch assignments (`<=`) require NBA scheduling and remain
unsupported, including when the inference option is enabled.

### Event and level waits {#timing-semantics}

- An event control waits for a change that happens after it begins
(IEEE 1800-2023 9.4.2). In `a = 1; @(a ^ b);` the write to `a` is not an
event of the wait; the process resumes when `a` or `b` changes later.
Icarus 13.0 wakes on the earlier write; Verilator 5.052 agrees with Celox.
- `posedge` and `negedge` of a multi-bit expression follow its least
significant bit (Table 9-2): `@(posedge v)` ignores `v[3]`, and
`@(posedge v[3])` follows it. Transitions through X and Z count as in that
table, so `0` to `x` is a `posedge`.
- Continuous assignments, combinational processes and event expressions see
a process's writes when that process reaches a timing control or ends.
After `b = 1; a = 1;`, `a ^ b` has not changed, and a pulse
`a = 1; a = 0;` wakes `@(a)` but not `@(a ^ b)`; write `#0` between the
writes to make the pulse visible. IEEE 1800-2023 4.7 permits this order;
Icarus evaluates the expression at each write.
- A waiter does not yet see a value that one evaluation of `always_comb`, an
edge-triggered `always` or `always_ff` writes and then overwrites, as in
`y = 0; if (a) y = 1;` ([#1706](https://github.com/celox-sim/celox/issues/1706)). `always @*` already
reports that write.

### Variable lifetimes

Variable lifetimes follow IEEE 1800-2023 6.21. A variable declared in a
Expand Down
18 changes: 12 additions & 6 deletions docs/guide/systemverilog.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ compilation unit; choose the top by its SystemVerilog module name.
| What your test needs | API |
| --- | --- |
| Set inputs, evaluate logic and advance clock events from Rust | `Simulator::from_sv_sources` |
| Advance simulation time and run supported `initial` processes | `Simulation::from_sv_sources` |
| Advance simulation time and run `initial` and `always` processes with timing controls | `Simulation::from_sv_sources` |
| Include both Veryl and SystemVerilog sources | `Simulator::from_mixed_sources` |

`Simulator::tick` drives the rising edge of the selected event. `Simulation`
tracks time and runs scheduled edges. Constant initialization applies to both;
`initial` blocks that read design state or call system tasks need `Simulation`
to execute as processes. Timing controls within SV `initial` blocks remain
restricted; see [unsupported constructs](./systemverilog-reference#what-is-not-supported).
tracks time and runs scheduled edges. Constant initialization applies to both.
`initial` blocks that read design state or call system tasks, processes with
timing controls and `always` blocks with a sensitivity list other than `@*` run
as processes and need `Simulation`; a `Simulator` does not run them
([#1712](https://github.com/celox-sim/celox/issues/1712)).

Use `.four_state(true)` on the builder when your test needs X/Z values. The
[4-state guide](./four-state) explains the state model. For time units,
Expand All @@ -78,6 +79,7 @@ The main supported areas are:
| Design structure | Modules, hierarchy, parameters, generate blocks, packages, interfaces and modports |
| Data | Packed vectors, multidimensional packed arrays, structs, enums and unpacked memories |
| RTL processes | `assign`, `always_comb`, `always @*`, blocking `always_latch`, edge-triggered `always` and `always_ff` |
| Timed processes | `initial` and `always` with `#delay` (also time literals such as `#5ns`), `@(...)` on variables, edges, selects, expressions and `@*`, `wait (...)`, nonblocking assignments and task calls with timing controls |
| Computation | Arithmetic, selects, assignment patterns, branches, loops and functions |
| Simulation helpers | Immediate assertions, display tasks, `$readmemh` / `$readmemb`, time queries and random numbers |

Expand All @@ -89,7 +91,10 @@ ports, types, statements or system functions before porting a design.

Common reasons a design needs changes are:

- Class-based verification, concurrent assertions and tasks with timing controls.
- Class-based verification and concurrent assertions.
- `fork` / `join`, named events, intra-assignment timing such as `a = #1 b`,
`iff` in event controls ([#1444](https://github.com/celox-sim/celox/issues/1444)) and real-valued delays
([#1707](https://github.com/celox-sim/celox/issues/1707)).
- Nonblocking latch assignments, multiple drivers and tri-state buses. Inferred latches in `always_comb` require `inferred_latch = "warn"` or `"allow"` in the common `[diagnostics]` configuration.
- Non-ANSI module ports, wildcard `.*` connections, gate primitives and `bind`.
- Strings, `real`, unpacked structs and several interface features.
Expand Down Expand Up @@ -120,6 +125,7 @@ and the supported types and call sites.
## Find a specific behavior {#semantics-worth-knowing}

- [Initialization, scopes, variable lifetimes and memory files](./systemverilog-reference#semantics-worth-knowing)
- [Event and level waits in processes](./systemverilog-reference#timing-semantics)
- [Time units, precision and time queries](./systemverilog-reference#time-and-precision)
- [Random seeds and reproducibility](./systemverilog-reference#random-numbers)

Expand Down
Loading
Loading