Skip to content
Closed
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/celox-test-suite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ name = "inspect-mismatch-ir"
required-features = ["emit"]

[dependencies]
sv-parser.workspace = true
veryl-analyzer = { workspace = true, optional = true }
veryl-emitter = { workspace = true, optional = true }
veryl-parser = { workspace = true, optional = true }
Expand Down
41 changes: 41 additions & 0 deletions crates/celox-test-suite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ behavioral model and verifies that incorrect results are rejected.
changes. `read` observes settled values, including after a direct write.
`tick` executes the named event once and settles its consequences. Event names
can refer to clocks or asynchronous resets; use the declaration's polarity.
- A design with `Design::timed` is driven by `run_until` and `run_to_finish`
instead of `tick`: the simulator runs the design's own processes, delays
and clocks. `run_until` advances to the given time in the design's time
units and fails when the design has finished by then; `run_to_finish` rejects
an already-finished design, runs until a process calls `$finish`, and fails
when the events run out first. A
`write` in a timed case takes effect at the current time, and `eval_comb`
also resumes the processes it woke, as a simulator's settle step does. A
time later than `RUN_UNTIL_LIMIT` (`i64::MAX / 1000`) is out of range in
every adapter. The optional methods' defaults return an
unsupported-operation error.
- Start each design with fresh storage: zero for two-state signals and X for
four-state signals. Initial combinational outputs must be readable before an
input is written. Generic clock/reset types use Veryl's defaults (positive
Expand Down Expand Up @@ -237,6 +248,36 @@ static). Script values are integers of unbounded width, so an expectation never
depends on the simulator's width or X rules; arithmetic on unknown bits is an
error.

A case whose design has processes with delays or event controls, or a clock
it generates itself, advances time with `run_until` and `run_to_finish` and
checks the design's output with `expect_output`. Design delays count time
units with the injected 1 ns / 1 ps scale. Explicit `timescale`, `timeunit`
and `timeprecision` declarations, and `resetall`, are rejected consistently by
the Celox and external adapters, with each simulator's predefined macros
applied. A timed design has no `final` procedures: they would run in an
arbitrary order relative to the checks after `run_to_finish`. `tick` cannot be
mixed with timed operations.

A timed script begins with `run_until` or `run_to_finish`; the parser rejects
any other first statement. Time zero starts the design's `initial` and
`always` processes, `always_ff` listeners and variable initializers in an order
the standard leaves open relative to the testbench, so a timed case never
depends on host inputs written before them: every input keeps its fresh value
(zero, or X with four-state) until time zero has been drained. Drive stimulus
after the first `run_until`, or generate it in the design. For the same
reason the design does not call `$finish` at time zero, and the target of the
first `run_until` does not read the design.

After that, reads, output collection and `eval` settle processes due at the
current timestamp without advancing time. External scripted adapters use an
Icarus read/write synchronization callback or a Verilator clocking event
sampled in the observed region; they compile the design sources unchanged.
`run_until` drains its target timestamp before subsequent writes. Only checks
may follow `run_to_finish`; they run in a `final` block that sees the script's
top-level bindings. They run only when the script reached the operation and
the design called `$finish`; a design that idles or is still running at
`RUN_UNTIL_LIMIT` fails the case.

For an invalid design, write `(expect reject)` and no statements.
`TestCase::expectation` lets consumers select these separately. For additional
SV expectations, add `(tags evaluation_order)` (or other `TestTag` names).
Expand Down
25 changes: 25 additions & 0 deletions crates/celox-test-suite/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ pub struct Design {
pub four_state: bool,
/// Values of the top module's parameters, overriding their defaults.
pub parameters: Vec<(String, u64)>,
/// Whether the case advances simulation time (`run_until`,
/// `run_to_finish`), so the design's own processes, delays and clocks
/// must run. An adapter builds a timed simulation for such a design.
pub timed: bool,
}

impl Design {
Expand All @@ -30,6 +34,7 @@ impl Design {
top: top.into(),
four_state: false,
parameters: Vec::new(),
timed: false,
}
}

Expand Down Expand Up @@ -89,6 +94,18 @@ pub trait Backend {
fn take_output(&mut self) -> Result<Vec<u8>> {
Err("this adapter does not capture design output".into())
}
/// Advance simulation time to `time` (inclusive), running the design's
/// processes, delays and clocks. Fails when `time` is in the past or
/// when a process ended the simulation at or before `time`. Only a
/// timed design (`Design::timed`) receives this call.
fn run_until(&mut self, _time: u64) -> Result<()> {
Err("this adapter does not run timed simulations".into())
}
/// Run until a process ends the simulation with `$finish`. Fails when
/// the simulation runs out of events first.
fn run_to_finish(&mut self) -> Result<()> {
Err("this adapter does not run timed simulations".into())
}
}

/// Compiler callback used by every test. Capturing closures allow the caller to
Expand Down Expand Up @@ -200,6 +217,14 @@ impl Simulator {
self.backend.take_output()
}

pub fn run_until(&mut self, time: u64) -> Result<()> {
self.backend.run_until(time)
}

pub fn run_to_finish(&mut self) -> Result<()> {
self.backend.run_to_finish()
}

pub fn tick(&mut self, event: Event) -> Result<()> {
self.backend.tick(&self.events.borrow()[event.0])
}
Expand Down
16 changes: 15 additions & 1 deletion crates/celox-test-suite/src/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::process::write_if_changed;
use crate::script::ScriptCase;
use crate::script::sv::{DesignInfo, TESTBENCH_TOP, testbench as generate_testbench};
use crate::script::timed::validate_timed_design_with_defines;
use crate::verification::{EmissionError, panic_message};
use crate::{Design, Expectation, Result};
use std::collections::BTreeMap;
Expand All @@ -12,6 +13,11 @@ use std::path::{Path, PathBuf};
/// The module that instantiates the top with a rejection case's parameters.
const PARAMETERS_TOP: &str = "celox_suite_parameters";

/// The time scale a timed design is compiled with: a script time unit is
/// 1 ns, and the generated testbench settles without advancing time. The
/// directive persists across the files that follow it in one compilation.
const TIMESCALE: &str = "`timescale 1ns/1ps\n";

/// A design translated to SystemVerilog for an external simulator.
pub struct PreparedDesign {
/// SystemVerilog sources to compile, in order.
Expand Down Expand Up @@ -45,13 +51,16 @@ pub(crate) struct Staged {
}

/// Write the original and translated sources of `design`, and for a script
/// case that simulates, the generated testbench, into `directory`.
/// case that simulates, the generated testbench, into `directory`. `defines`
/// are the macros the adapter's compiler predefines.
pub(crate) fn stage(
frontend: &dyn Frontend,
design: &Design,
directory: &Path,
script: Option<&ScriptCase>,
defines: &[&str],
) -> Result<Staged> {
validate_timed_design_with_defines(design, defines)?;
fs::create_dir_all(directory)?;
let directory = fs::canonicalize(directory)?;
for (index, source) in design.sources.iter().enumerate() {
Expand All @@ -64,6 +73,11 @@ pub(crate) fn stage(
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| frontend.prepare(design)))
.map_err(|error| EmissionError(panic_message(error.as_ref())))??;
let mut paths = Vec::new();
if design.timed {
let timescale = directory.join("timescale.sv");
write_if_changed(&timescale, TIMESCALE.as_bytes())?;
paths.push(timescale);
}
for (index, source) in prepared.sources.iter().enumerate() {
let path = directory.join(format!("source_{index}.sv"));
write_if_changed(&path, source.as_bytes())?;
Expand Down
19 changes: 19 additions & 0 deletions crates/celox-test-suite/src/icarus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,26 @@ static PLI_INT32 start(p_cb_data) {
return commands(nullptr);
}

static PLI_INT32 resume_settle(p_cb_data cb) {
s_vpi_value value{}; value.format = vpiIntVal; value.value.integer = 1;
vpi_put_value(reinterpret_cast<vpiHandle>(cb->user_data), &value, nullptr, vpiNoDelay);
return 0;
}
static PLI_INT32 request_settle(PLI_BYTE8*) {
vpiHandle call = vpi_handle(vpiSysTfCall, nullptr);
vpiHandle args = vpi_iterate(vpiArgument, call);
vpiHandle ready = vpi_scan(args);
s_vpi_time time{}; time.type = vpiSimTime;
s_cb_data cb{}; cb.reason = cbReadWriteSynch; cb.cb_rtn = resume_settle;
cb.time = &time; cb.user_data = reinterpret_cast<PLI_BYTE8*>(ready);
vpi_register_cb(&cb);
return 0;
}
static void register_suite() {
s_vpi_systf_data settle{}; settle.type = vpiSysTask;
settle.tfname = const_cast<char*>("$celox_suite_settle"); settle.calltf = request_settle;
vpi_register_systf(&settle);

s_vpi_systf_data finish{};
finish.type = vpiSysTask;
finish.tfname = const_cast<char*>("$celox_suite_finish");
Expand Down
9 changes: 8 additions & 1 deletion crates/celox-test-suite/src/icarus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ impl Icarus {
top,
testbench,
edges,
} = stage(frontend, design, directory, script)?;
} = stage(
frontend,
design,
directory,
script,
// `__ICARUS__` is predefined by Icarus.
&["CELOX_SUITE_ICARUS", "__ICARUS__"],
)?;
write_if_changed(
&directory.join("vpi_bits.hpp"),
include_bytes!("vpi_bits.hpp"),
Expand Down
2 changes: 2 additions & 0 deletions crates/celox-test-suite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub use backend::{
pub use frontend::{Frontend, PreparedDesign};
pub use num_bigint::BigUint;
pub use scalar::Scalar;
pub use script::validate_timed_design;

/// Errors from a compiler or simulator adapter. Assertions fail by panicking,
/// just like ordinary Rust tests; adapter errors are included in that failure.
Expand Down Expand Up @@ -233,6 +234,7 @@ impl script::ScriptCase {
top: self.top.clone(),
four_state: self.four_state,
parameters: self.parameters.clone(),
timed: self.is_timed(),
}
}
}
Expand Down
Loading
Loading