From 354a8713be8ac788ff9a60b677f99ccb24238f79 Mon Sep 17 00:00:00 2001 From: tignear Date: Sat, 24 Oct 2026 19:41:46 +0900 Subject: [PATCH 1/2] feat(sv): simulate program blocks The analyzer accepts program declarations and their instances (IEEE 1800-2023 24.3). It rejects always procedures, module instances and nested units inside a program, and reports a program nested in a module as unsupported. A program's initial procedures are not folded into initial state: they lower to kernels that name the program instance, so they run in the reactive region set, and $exit in a program ends its initial procedures (24.7). Continuous assignments of a program are evaluated with the design. Nested and anonymous programs, the implicit program top, and final and clocking blocks in programs are not supported yet. The shared suite's program cases are checked against xezim 0.11.3. Icarus 13.0 and Verilator 5.052 run program procedures in the active region set and do not end the simulation when the programs end; their exclusions record the pre-exclusion results. Change-Id: I3e6817bd4b94f04465a90ecb9ef4155ee3e582da --- crates/celox-frontend-sv/src/lowering.rs | 23 +- crates/celox-frontend-sv/src/lowering/ff.rs | 9 +- .../src/lowering/procedural.rs | 4 + crates/celox-frontend-sv/src/warning.rs | 4 +- crates/celox-sv-analyzer/src/analyze.rs | 1 + crates/celox-sv-analyzer/src/ast.rs | 22 +- .../celox-sv-analyzer/src/ast/module_index.rs | 6 +- .../celox-sv-analyzer/src/ast/validation.rs | 76 +++++- crates/celox-sv-analyzer/src/ast/warnings.rs | 4 +- crates/celox-sv-analyzer/src/ir.rs | 13 + crates/celox-sv-analyzer/src/lib.rs | 5 + crates/celox-sv-analyzer/src/parsed.rs | 8 +- .../src/source_tree/scopes.rs | 2 +- .../source_tree/tests/subroutine_ownership.rs | 11 +- .../celox-sv-analyzer/src/system_functions.rs | 3 +- crates/celox-sv-syntax/src/nodes.rs | 8 + crates/celox-test-suite/LIMITATIONS.md | 5 + crates/celox-test-suite/src/icarus.rs | 3 + crates/celox-test-suite/src/sv/cases/mod.rs | 4 + .../src/sv/cases/program.vtest | 240 ++++++++++++++++++ .../verification/sv/limitations.json | 102 ++++++++ .../sv/limitations/programs-icarus.json | 126 +++++++++ .../sv/limitations/programs-verilator.json | 154 +++++++++++ .../tests/frontends/systemverilog/timing.rs | 14 + docs/guide/systemverilog-reference.md | 34 ++- docs/guide/systemverilog.md | 2 +- docs/ja/guide/systemverilog-reference.md | 24 +- 27 files changed, 872 insertions(+), 35 deletions(-) create mode 100644 crates/celox-test-suite/src/sv/cases/program.vtest create mode 100644 crates/celox-test-suite/verification/sv/limitations/programs-icarus.json create mode 100644 crates/celox-test-suite/verification/sv/limitations/programs-verilator.json diff --git a/crates/celox-frontend-sv/src/lowering.rs b/crates/celox-frontend-sv/src/lowering.rs index 69a5dfd22ad..e9eab318956 100644 --- a/crates/celox-frontend-sv/src/lowering.rs +++ b/crates/celox-frontend-sv/src/lowering.rs @@ -4108,8 +4108,10 @@ fn lower_initial_processes( .copied() .unwrap_or(0x4d59_5df4); // An `always` process never finishes, so it is never folded into - // initial state. - if process.repeats() { + // initial state. A program's `initial` procedure runs in the reactive + // region set (IEEE 1800-2023 24.3.1), after the design's processes of + // time zero, so it is not folded either. + if process.repeats() || (module.is_program() && !process.is_initializer()) { runtime.push((process, random_seed)); continue; } @@ -4154,7 +4156,7 @@ fn lower_initial_processes( .max() .unwrap_or_default(), ); - let slots: Vec<_> = runtime + let mut slots: Vec<_> = runtime .iter() .enumerate() .map(|(index, (process, _))| { @@ -4195,6 +4197,21 @@ fn lower_initial_processes( } }) .collect(); + // Every `initial` kernel of a program instance names the status slot of + // the first, the instance's identity for `$exit` and its end (IEEE + // 1800-2023 24.3, 24.7). + if module.is_program() { + let program = slots + .iter() + .zip(&runtime) + .find(|(_, (process, _))| !process.is_initializer()) + .map(|(slots, _)| slots.status); + for (slots, (process, _)) in slots.iter_mut().zip(&runtime) { + if !process.is_initializer() { + slots.program = program; + } + } + } // The kernels add their event sites, runtime errors and extern functions // after those of the sequential processes. Their hidden variables are // state the kernels keep. diff --git a/crates/celox-frontend-sv/src/lowering/ff.rs b/crates/celox-frontend-sv/src/lowering/ff.rs index b77cd77394d..8c41deb2eb7 100644 --- a/crates/celox-frontend-sv/src/lowering/ff.rs +++ b/crates/celox-frontend-sv/src/lowering/ff.rs @@ -2869,9 +2869,14 @@ impl<'p, 'a> Ff<'p, 'a> { } return Ok(true); } - // IEEE 1800-2023 24.7: `$exit` outside a program block is ignored, - // with a warning. + // IEEE 1800-2023 24.7: `$exit` in a thread of a program's `initial` + // procedure ends the program's initial procedures; elsewhere it is + // ignored, with a warning. if task == sv::ir::SystemTask::Exit { + if self.m.program && self.kernel.is_some() { + self.with_kernel(ProcessKernelBuilder::exit); + return Ok(false); + } self.m .warn(crate::FrontendWarningKind::ExitOutsideProgram, site); return Ok(true); diff --git a/crates/celox-frontend-sv/src/lowering/procedural.rs b/crates/celox-frontend-sv/src/lowering/procedural.rs index aabc641beca..1da137b3222 100644 --- a/crates/celox-frontend-sv/src/lowering/procedural.rs +++ b/crates/celox-frontend-sv/src/lowering/procedural.rs @@ -53,6 +53,9 @@ pub(super) struct ProcModule<'a> { /// Number of extern calls lowered so far. pub extern_calls: usize, pub four_state: bool, + /// The module is a program (IEEE 1800-2023 24.3): its kernels honour + /// `$exit` (24.7). + pub program: bool, pub forceable: HashMap, /// Hidden variables created while lowering, for the caller to publish. pub created: Vec, @@ -244,6 +247,7 @@ impl<'a> ProcModule<'a> { extern_functions: Vec::new(), extern_calls: 0, four_state, + program: module.is_program(), created: Vec::new(), temp_counter: 0, capture_counter: 0, diff --git a/crates/celox-frontend-sv/src/warning.rs b/crates/celox-frontend-sv/src/warning.rs index 851b993d562..4d5752d35ce 100644 --- a/crates/celox-frontend-sv/src/warning.rs +++ b/crates/celox-frontend-sv/src/warning.rs @@ -57,8 +57,8 @@ pub enum FrontendWarningKind { file: String, detail: String, }, - /// `$exit` outside a program block, which IEEE 1800-2023 24.7 ignores. - /// Celox has no program blocks, so every call is ignored. + /// `$exit` outside a thread of a program's `initial` procedure, which + /// IEEE 1800-2023 24.7 ignores. #[error("`$exit` outside a program block is ignored")] ExitOutsideProgram, } diff --git a/crates/celox-sv-analyzer/src/analyze.rs b/crates/celox-sv-analyzer/src/analyze.rs index 6173365ce53..a9c7a6e64a7 100644 --- a/crates/celox-sv-analyzer/src/analyze.rs +++ b/crates/celox-sv-analyzer/src/analyze.rs @@ -230,6 +230,7 @@ fn analyze_source_impl(source: ast::Source) -> Result { .collect(), ) .with_dpi_imports(module.dpi_imports().to_vec()) + .with_program(module.is_program()) .with_imported_parameters(imported_parameters) .with_declaration_subroutines(module.declaration_subroutines().to_vec()), ); diff --git a/crates/celox-sv-analyzer/src/ast.rs b/crates/celox-sv-analyzer/src/ast.rs index 7b61af634ae..0476cad5c0e 100644 --- a/crates/celox-sv-analyzer/src/ast.rs +++ b/crates/celox-sv-analyzer/src/ast.rs @@ -288,7 +288,7 @@ impl Source { let scoped_context = context.child(&scoped, ¶meter_overrides); let syntax_tree = &scoped_context; let unit = syntax_tree.root_unit()?; - if unit.kind != celox_sv_syntax::UnitKind::Module { + if !unit.kind.is_module_like() { continue; } match unit.port_style { @@ -477,8 +477,7 @@ impl Source { let scoped = syntax_tree.scoped(handle)?; let syntax_tree = &scoped; let unit = syntax_tree.root_unit()?; - if unit.kind == celox_sv_syntax::UnitKind::Module - && unit.port_style != celox_sv_syntax::PortStyle::Wildcard + if unit.kind.is_module_like() && unit.port_style != celox_sv_syntax::PortStyle::Wildcard { names.push(syntax_tree.root_name()?); } @@ -495,9 +494,7 @@ impl Source { let scoped = syntax_tree.scoped(handle)?; let syntax_tree = &scoped; let unit = syntax_tree.root_unit()?; - if unit.kind == celox_sv_syntax::UnitKind::Module - && unit.port_style == celox_sv_syntax::PortStyle::Ansi - { + if unit.kind.is_module_like() && unit.port_style == celox_sv_syntax::PortStyle::Ansi { let name = syntax_tree.root_name()?; let context = AnalysisContext::unspecialized(syntax_tree); interfaces.insert(name, module_interface_from_scope(&context)?); @@ -512,7 +509,8 @@ impl Source { pub(crate) fn warnings(&self) -> Vec { let mut warnings = self.diagnostics.clone(); - for module in &self.modules { + // A program's functions run in its threads, where `$exit` applies. + for module in self.modules.iter().filter(|module| !module.program) { warnings::append(&mut warnings, module.constant_functions.warnings()); } warnings @@ -522,6 +520,9 @@ impl Source { #[derive(Debug, Clone, PartialEq, Eq)] pub struct Module { name: String, + /// A program (IEEE 1800-2023 24.3), whose processes run in the reactive + /// region set. + program: bool, parameters: Vec, ports: Vec, signals: Vec, @@ -625,6 +626,7 @@ impl Module { ) -> Result { let unit = syntax_tree.scope_handle().unit(syntax_tree.declarations()); let is_package = unit.is_none_or(|unit| unit.kind == celox_sv_syntax::UnitKind::Package); + let program = unit.is_some_and(|unit| unit.kind == celox_sv_syntax::UnitKind::Program); let name = unit.map_or_else( || packages::UNIT.to_string(), |unit| instances::normalize_identifier(&unit.name.spelling), @@ -1417,6 +1419,7 @@ impl Module { Ok(Self { name, + program, parameters, ports, signals, @@ -1488,6 +1491,11 @@ impl Module { pub fn dpi_imports(&self) -> &[crate::ir::DpiImport] { &self.dpi_imports } + + /// Whether this is a program (IEEE 1800-2023 24.3). + pub fn is_program(&self) -> bool { + self.program + } } pub(crate) fn generated_time_declarations( diff --git a/crates/celox-sv-analyzer/src/ast/module_index.rs b/crates/celox-sv-analyzer/src/ast/module_index.rs index 3bf284410b6..085570d03e7 100644 --- a/crates/celox-sv-analyzer/src/ast/module_index.rs +++ b/crates/celox-sv-analyzer/src/ast/module_index.rs @@ -30,7 +30,7 @@ impl ModuleIndex { for handle in tree.root_scopes()? { let scoped = tree.scoped(handle)?; let unit = scoped.root_unit()?; - if unit.kind != celox_sv_syntax::UnitKind::Module + if !unit.kind.is_module_like() || unit.port_style == celox_sv_syntax::PortStyle::Wildcard { continue; @@ -51,11 +51,11 @@ impl ModuleIndex { } } if let Some(declarations) = declarations { - use celox_sv_syntax::{PortKind, PortStyle, UnitKind}; + use celox_sv_syntax::{PortKind, PortStyle}; let units = declarations .all_units() .filter(|unit| { - unit.kind == UnitKind::Module + unit.kind.is_module_like() && !unit.external && unit.port_style != PortStyle::Wildcard }) diff --git a/crates/celox-sv-analyzer/src/ast/validation.rs b/crates/celox-sv-analyzer/src/ast/validation.rs index 629730d0fc0..f432f47c197 100644 --- a/crates/celox-sv-analyzer/src/ast/validation.rs +++ b/crates/celox-sv-analyzer/src/ast/validation.rs @@ -215,13 +215,16 @@ pub(super) fn reject_silently_ignored_constructs( parameter_dimensions: &ScopedMap, parameter_values: &HashMap, ) -> Converted<()> { + if let Ok(unit) = syntax_tree.root_unit() { + validate_program_structure(unit, syntax_tree.declarations())?; + } let mut dimensions = PackedDimensions::new(parameter_dimensions.clone(), const_env, type_aliases); dimensions.parameter_values = parameter_values.clone().into(); validate_scope(None, syntax_tree, &dimensions)?; if syntax_tree .root_unit() - .is_ok_and(|unit| unit.kind == celox_sv_syntax::UnitKind::Module) + .is_ok_and(|unit| unit.kind.is_module_like()) { let active = generate::items(syntax_tree, const_env, type_aliases)?; let mut views = generate::ScopeViews::new(&dimensions); @@ -236,6 +239,75 @@ pub(super) fn reject_silently_ignored_constructs( Ok(()) } +/// A program is a leaf: it shall not contain `always` procedures or +/// declarations or instances of modules, interfaces or other programs (IEEE +/// 1800-2023 24.3), generate blocks included. A program nested in a module +/// is implicitly instantiated there, which Celox does not do. +fn validate_program_structure( + unit: &celox_sv_syntax::Unit, + native: &celox_sv_syntax::Declarations, +) -> Converted<()> { + use celox_sv_syntax::{BodyItemKind, ProcedureKind, UnitKind}; + let program = unit.kind == UnitKind::Program; + let illegal = |item: String| { + Err(AnalyzerError::IllegalProgramItem { + program: instances::normalize_identifier(&unit.name.spelling), + item, + }) + }; + let mut pending = unit.items.iter().rev().copied().collect::>(); + while let Some(id) = pending.pop() { + match &native.body_item(id).kind { + BodyItemKind::Procedure(procedure) if program => { + let keyword = match native.procedure(*procedure).kind { + ProcedureKind::Always => "always", + ProcedureKind::AlwaysComb => "always_comb", + ProcedureKind::AlwaysLatch => "always_latch", + ProcedureKind::AlwaysFf => "always_ff", + ProcedureKind::Initial | ProcedureKind::Final => continue, + }; + return illegal(format!("`{keyword}` procedure")); + } + BodyItemKind::Instance(_) if program => return illegal("an instance".into()), + BodyItemKind::Unit(nested) => { + let nested = native.unit(*nested); + if program { + let kind = match nested.kind { + UnitKind::Module => "module", + UnitKind::Interface => "interface", + UnitKind::Package => "package", + UnitKind::Program => "program", + }; + return illegal(format!( + "{kind} declaration `{}`", + instances::normalize_identifier(&nested.name.spelling) + )); + } + if nested.kind == UnitKind::Program { + return Err(unsupported("program declaration nested in a module")); + } + } + BodyItemKind::Region(items) | BodyItemKind::Block { items, .. } => { + pending.extend(items.iter().rev().copied()); + } + BodyItemKind::Conditional { + then_body, + otherwise, + .. + } => { + pending.extend(otherwise); + pending.push(*then_body); + } + BodyItemKind::Case { arms, .. } => { + pending.extend(arms.iter().rev().map(|arm| arm.body)); + } + BodyItemKind::Loop { body, .. } => pending.push(*body), + _ => {} + } + } + Ok(()) +} + fn contains(outer: celox_sv_syntax::TokenRange, inner: celox_sv_syntax::TokenRange) -> bool { outer.start <= inner.start && inner.end <= outer.end } @@ -253,7 +325,7 @@ fn validate_scope( let is_module = owner.is_none() && syntax_tree .root_unit() - .is_ok_and(|unit| unit.kind == celox_sv_syntax::UnitKind::Module); + .is_ok_and(|unit| unit.kind.is_module_like()); let items = owner.map_or_else(|| syntax_tree.body_membership(), |id| Ok(vec![id]))?; validate_body_items(&items, native, !is_module)?; parameters::skipped::validate_scope(owner, is_module, syntax_tree, indexed_dimensions)?; diff --git a/crates/celox-sv-analyzer/src/ast/warnings.rs b/crates/celox-sv-analyzer/src/ast/warnings.rs index 0b8994bb6e2..354e287cdf5 100644 --- a/crates/celox-sv-analyzer/src/ast/warnings.rs +++ b/crates/celox-sv-analyzer/src/ast/warnings.rs @@ -98,7 +98,9 @@ pub(crate) fn components(tree: &SyntaxTree) -> HashMap, dpi_imports: Vec, imported_parameters: Vec, + program: bool, } impl Module { @@ -194,9 +195,21 @@ impl Module { declaration_subroutines: Vec::new(), dpi_imports: Vec::new(), imported_parameters: Vec::new(), + program: false, } } + pub(crate) fn with_program(mut self, program: bool) -> Self { + self.program = program; + self + } + + /// Whether this is a program (IEEE 1800-2023 24.3): its `initial` + /// procedures run in the reactive region set (24.3.1). + pub fn is_program(&self) -> bool { + self.program + } + pub(crate) fn with_imported_parameters(mut self, parameters: Vec) -> Self { self.imported_parameters = parameters; self diff --git a/crates/celox-sv-analyzer/src/lib.rs b/crates/celox-sv-analyzer/src/lib.rs index 58712e85d28..d6d6b598f7a 100644 --- a/crates/celox-sv-analyzer/src/lib.rs +++ b/crates/celox-sv-analyzer/src/lib.rs @@ -162,6 +162,11 @@ pub enum AnalyzerError { /// least significant bit to judge the edge on (IEEE 1800-2023 6.12). #[error("`{edge}` of a real expression: edge event controls do not apply to reals")] RealEdgeEvent { edge: &'static str }, + /// An item a program shall not contain: an `always` procedure, or a + /// declaration or instance of a module, interface or program (IEEE + /// 1800-2023 24.3). + #[error("{item} in program `{program}`: a program shall not contain it (IEEE 1800-2023 24.3)")] + IllegalProgramItem { program: String, item: String }, } impl miette::Diagnostic for AnalyzerError {} diff --git a/crates/celox-sv-analyzer/src/parsed.rs b/crates/celox-sv-analyzer/src/parsed.rs index 300a5369603..d3706a66139 100644 --- a/crates/celox-sv-analyzer/src/parsed.rs +++ b/crates/celox-sv-analyzer/src/parsed.rs @@ -53,7 +53,9 @@ impl ParsedSource { .filter(|unit| { matches!( unit.kind, - celox_sv_syntax::UnitKind::Module | celox_sv_syntax::UnitKind::Interface + celox_sv_syntax::UnitKind::Module + | celox_sv_syntax::UnitKind::Program + | celox_sv_syntax::UnitKind::Interface ) && !unit.external && unit.port_style != celox_sv_syntax::PortStyle::Wildcard }) @@ -118,7 +120,9 @@ impl ParsedSource { }; if !matches!( unit.kind, - celox_sv_syntax::UnitKind::Module | celox_sv_syntax::UnitKind::Interface + celox_sv_syntax::UnitKind::Module + | celox_sv_syntax::UnitKind::Program + | celox_sv_syntax::UnitKind::Interface ) { continue; } diff --git a/crates/celox-sv-analyzer/src/source_tree/scopes.rs b/crates/celox-sv-analyzer/src/source_tree/scopes.rs index 00ae5ae22d5..75629f44ecb 100644 --- a/crates/celox-sv-analyzer/src/source_tree/scopes.rs +++ b/crates/celox-sv-analyzer/src/source_tree/scopes.rs @@ -65,7 +65,7 @@ pub(super) fn handles(native: &Declarations) -> Converted> { if !unit.external && matches!( unit.kind, - UnitKind::Module | UnitKind::Interface | UnitKind::Package + UnitKind::Module | UnitKind::Program | UnitKind::Interface | UnitKind::Package ) { result.push(handle); diff --git a/crates/celox-sv-analyzer/src/source_tree/tests/subroutine_ownership.rs b/crates/celox-sv-analyzer/src/source_tree/tests/subroutine_ownership.rs index 22dfb95df73..923e8ae1f93 100644 --- a/crates/celox-sv-analyzer/src/source_tree/tests/subroutine_ownership.rs +++ b/crates/celox-sv-analyzer/src/source_tree/tests/subroutine_ownership.rs @@ -128,7 +128,7 @@ fn captured_class_method_identity_is_required_by_existing_module_consumers() { } #[test] -fn ignored_program_methods_do_not_corrupt_native_function_query_scope() { +fn program_functions_do_not_corrupt_native_function_query_scope() { let tree=parse_source("program unused;function int helper();return 1;endfunction endprogram module M(input logic[1:0]a);function automatic int g(input logic[3:0]a);logic[$bits(a)-1:0]x;return $bits(x);endfunction localparam N=g(0);endmodule",Path::new("native-subroutine-query-scopes.sv")).unwrap(); let ir = crate::analyze::analyze_source(crate::ast::Source::from_syntax(&tree).unwrap()).unwrap(); @@ -136,13 +136,18 @@ fn ignored_program_methods_do_not_corrupt_native_function_query_scope() { // N using the module's shadowed width (2), also on immutable81ebab922. // This retirement preserves that pre-existing behavior; runtime formals // and body-local type discovery still use the proper native scope. - let g = ir.modules()[0] + let module = ir + .modules() + .iter() + .find(|module| module.name() == "M") + .unwrap(); + let g = module .subroutines() .iter() .find(|routine| routine.name == "g") .unwrap(); assert_eq!(g.params[0].r#type.resolved_width(), Some(4)); - let local = ir.modules()[0] + let local = module .locals() .iter() .find(|local| local.name.starts_with("x@")) diff --git a/crates/celox-sv-analyzer/src/system_functions.rs b/crates/celox-sv-analyzer/src/system_functions.rs index 3fda772fed5..bd9fb3c891c 100644 --- a/crates/celox-sv-analyzer/src/system_functions.rs +++ b/crates/celox-sv-analyzer/src/system_functions.rs @@ -457,8 +457,7 @@ pub enum SystemTask { /// `$stop` (20.2). Stop, /// `$exit` (20.2, 24.7), which ends the initial procedures of the - /// program block that calls it. Celox has no program blocks, and a call - /// from outside one is ignored. + /// program block that calls it; a call from outside one is ignored. Exit, /// `$printtimescale` (20.4.1); the analyzer rewrites it into a /// [`SystemTask::Display`] of the message. diff --git a/crates/celox-sv-syntax/src/nodes.rs b/crates/celox-sv-syntax/src/nodes.rs index b52c7abebc2..efb4f75075d 100644 --- a/crates/celox-sv-syntax/src/nodes.rs +++ b/crates/celox-sv-syntax/src/nodes.rs @@ -330,6 +330,14 @@ pub enum UnitKind { Program, } +impl UnitKind { + /// A module or a program, which IEEE 1800-2023 24.3 makes a leaf module + /// with its own scheduling: both elaborate as design units. + pub fn is_module_like(self) -> bool { + matches!(self, Self::Module | Self::Program) + } +} + #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum PortStyle { Ansi, diff --git a/crates/celox-test-suite/LIMITATIONS.md b/crates/celox-test-suite/LIMITATIONS.md index 518436a082f..7128178485c 100644 --- a/crates/celox-test-suite/LIMITATIONS.md +++ b/crates/celox-test-suite/LIMITATIONS.md @@ -587,6 +587,11 @@ four-state expectations; the assertions remain enabled on all Celox variants. | `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. | +| `sv_program_reactive_region` | execute | icarus, verilator | 10 | Verilator 5.052 and Icarus 13.0 run program procedures in the active region set: a program reads design values before the design's nonblocking updates of the time slot, its `#0` resumes before the design's, and a flip-flop clocked by a program write runs before the program's `#0` and its nonblocking update of the flip-flop's input. IEEE 1800-2023 24.3.1 schedules program procedures in the Reactive region, `#0` in the Re-Inactive region and their nonblocking updates in the Re-NBA region, and 4.5 empties the reactive region set before the active region set runs again. xezim 0.11.3 agrees with every case except the flip-flop one, where it also runs the flip-flop first. The pre-exclusion results are [Icarus](verification/sv/limitations/programs-icarus.json) and [Verilator](verification/sv/limitations/programs-verilator.json). | +| `sv_program_completion` | execute | icarus, verilator | 2 | Neither tool ends the simulation when every program `initial` procedure has ended; IEEE 1800-2023 24.3 calls `$finish` then. xezim 0.11.3 does not either. | +| `sv_icarus_program_exit` | execute | icarus | 1 | Icarus 13.0 does not define `$exit`; IEEE 1800-2023 24.7 ends the calling program's `initial` procedures. | +| `sv_verilator_program_exit` | execute | verilator | 1 | Verilator 5.052 continues after `$exit` and keeps the program's other `initial` procedures (24.7). Icarus does not run the case; xezim 0.11.3 agrees with the expected output. | +| `sv_verilator_program_items` | compile | verilator | 2 | Verilator 5.052 rejects an `always` procedure and a module instance in a program with syntax errors, which the run cannot count as language rejections. Both are illegal (IEEE 1800-2023 24.3); Icarus 13.0 rejects them with diagnostics that name the rule. | 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/icarus.rs b/crates/celox-test-suite/src/icarus.rs index bbc0228d82e..166342b579b 100644 --- a/crates/celox-test-suite/src/icarus.rs +++ b/crates/celox-test-suite/src/icarus.rs @@ -154,6 +154,9 @@ pub fn is_source_rejection(code: Option, log: &str, sources: &[PathBuf]) -> // nonequivalent element types or different sizes. | "Element types are not compatible in array assignment." | "Unpacked dimensions are not compatible in array assignment." + // IEEE 1800-2023 24.3: what a program shall not contain. + | "Always statements are not allowed in program blocks." + | "Module instantiations are not allowed in program blocks." ) || (error.starts_with("A reference to a net or variable (`") && error.ends_with("') is not allowed in a constant expression.")) || (error.starts_with("Array ") && error.ends_with(" needs an array index here.")) diff --git a/crates/celox-test-suite/src/sv/cases/mod.rs b/crates/celox-test-suite/src/sv/cases/mod.rs index 8adeddf9279..88a5c66434b 100644 --- a/crates/celox-test-suite/src/sv/cases/mod.rs +++ b/crates/celox-test-suite/src/sv/cases/mod.rs @@ -51,6 +51,10 @@ pub(super) const GROUPS: &[Group] = &[ file: "src/sv/cases/random_plusargs.vtest", text: include_str!("random_plusargs.vtest"), }, + Group { + file: "src/sv/cases/program.vtest", + text: include_str!("program.vtest"), + }, Group { file: "src/sv/cases/latch.vtest", text: include_str!("latch.vtest"), diff --git a/crates/celox-test-suite/src/sv/cases/program.vtest b/crates/celox-test-suite/src/sv/cases/program.vtest new file mode 100644 index 00000000000..033758f52d1 --- /dev/null +++ b/crates/celox-test-suite/src/sv/cases/program.vtest @@ -0,0 +1,240 @@ +(group program (category sequential)) + +; Program blocks (IEEE 1800-2023 Clause 24). Their `initial` procedures, +; event controls and delays run in the reactive region set, which runs after +; the active region set of a time slot is empty (4.5, 24.3.1). Expected +; outputs follow the reference algorithm of 4.5. + +; A program reads the design after its nonblocking updates and the +; combinational logic they feed have settled in the same time slot. +(case program_reads_design_after_nonblocking_updates_settle + (source "program_settled.sv" #" + module Top; + logic clk = 0; + logic [7:0] d = 0, q = 0, qq; + always @(posedge clk) q <= d; + assign qq = q + 8'd1; + initial begin + #1 d = 5; clk = 1; + $display("design q=%0d qq=%0d", q, qq); + end + Probe p(clk, q, qq); + endmodule + program Probe(input logic clk, input logic [7:0] q, qq); + initial begin + $display("start q=%0d qq=%0d", q, qq); + @(posedge clk) $display("edge q=%0d qq=%0d", q, qq); + $finish; + end + endprogram + "#) + (top Top) + (run_to_finish) + (expect_output "start q=0 qq=1\ndesign q=0 qq=1\nedge q=5 qq=6\n")) + +; A program's nonblocking assignment updates in the Re-NBA region, after the +; design's NBA region; the design processes the update wakes run in the +; active region set that follows. +(case program_nonblocking_updates_follow_design_updates + (source "program_renba.sv" #" + module Top; + logic clk = 0; + logic [7:0] count = 0, r; + always @(posedge clk) count <= count + 8'd1; + always @(r) $display("design sees r=%0d count=%0d", r, count); + initial begin #1 clk = 1; #1 clk = 0; #1 clk = 1; end + Tb t(clk, count, r); + endmodule + program Tb(input logic clk, input logic [7:0] count, output logic [7:0] r); + initial begin + r = 1; + repeat (2) begin + @(posedge clk); + r <= count + 8'd10; + $display("program count=%0d r=%0d", count, r); + end + #1 $finish; + end + endprogram + "#) + (top Top) + (run_to_finish) + (expect_output "design sees r=1 count=0\nprogram count=1 r=1\ndesign sees r=11 count=1\nprogram count=2 r=11\ndesign sees r=12 count=2\n")) + +; `#0` in a program resumes in the Re-Inactive region (4.4.2.7), after the +; design's Inactive region of the same time. +(case program_zero_delay_resumes_in_the_re_inactive_region + (source "program_zero.sv" #" + module Top; + logic [7:0] a = 0; + initial begin + #1 a = 1; + #0 $display("design inactive a=%0d", a); + end + Tb t(a); + endmodule + program Tb(input logic [7:0] a); + logic [7:0] seen; + initial begin + #1 seen = a; + $display("reactive a=%0d", a); + #0 $display("re-inactive a=%0d seen=%0d", a, seen); + $finish; + end + endprogram + "#) + (top Top) + (run_to_finish) + (expect_output "design inactive a=1\nreactive a=1\nre-inactive a=1 seen=1\n")) + +; Design activity a program creates waits until the reactive region set is +; empty (4.5): a flip-flop clocked by the program's blocking write runs after +; the program's `#0` and its Re-NBA update of the flip-flop's input. +(case design_activity_of_a_program_waits_for_the_reactive_region_set + (source "program_drain.sv" #" + module Top; + logic clk; + logic [7:0] d, q = 0; + always @(posedge clk) begin + q <= d; + $display("design edge d=%0d", d); + end + Tb t(clk, d, q); + endmodule + program Tb(output logic clk, output logic [7:0] d, input logic [7:0] q); + initial begin + clk = 0; + d = 0; + #1 clk = 1; + d <= 7; + #0 $display("re-inactive q=%0d d=%0d", q, d); + #1 $display("q=%0d", q); + $finish; + end + endprogram + "#) + (top Top) + (run_to_finish) + (expect_output "re-inactive q=0 d=0\ndesign edge d=7\nq=7\n")) + +; An input the host drives wakes a program in the Reactive region, after +; the nonblocking updates the same edge causes in the design. +(case program_waits_see_host_driven_edges_after_design_updates + (source "program_host_edge.sv" #" + module Top(input logic go, output logic [7:0] seen); + logic [7:0] count = 0; + always @(posedge go) count <= count + 8'd1; + Tb t(go, count, seen); + endmodule + program Tb(input logic go, input logic [7:0] count, output logic [7:0] seen); + initial begin + seen = 0; + forever @(posedge go) seen = count; + end + endprogram + "#) + (top Top) + (run_until 1) + (assert_eq seen 0) + (modify (set go 1)) + (run_until 2) + (assert_eq seen 1 "the program reads the count after its update") + (modify (set go 0)) + (run_until 3) + (modify (set go 1)) + (run_until 4) + (assert_eq seen 2)) + +; `$exit` ends every `initial` procedure of its program (24.7). With them the +; last program procedures have ended, which ends the simulation (24.3). +(case program_exit_ends_its_initial_procedures + (source "program_exit.sv" #" + module Top; + initial #20 $display("design at 20"); + Tb t(); + endmodule + program Tb; + initial begin + #2 $display("first"); + #2 $exit; + $display("not reached"); + end + initial begin + #3 $display("second"); + #10 $display("second not reached"); + end + endprogram + "#) + (top Top) + (run_to_finish) + (expect_output "first\nsecond\n")) + +; The simulation ends with an implicit `$finish` once every `initial` +; procedure of every program has ended, while design processes still run +; (24.3). +(case simulation_ends_when_program_procedures_end + (source "program_complete.sv" #" + module Top(output int n); + initial begin + n = 0; + forever #1 n++; + end + Tb t(); + endmodule + program Tb; + initial #3 $display("done"); + endprogram + "#) + (top Top) + (run_to_finish) + (expect_output "done\n") + (assert_eq n 3)) + +; Program ports connect like module ports (24.3). +(case program_ports_connect_to_design_nets + (source "program_ports.sv" #" + program Check(input wire a, input wire b, output logic same); + initial begin + same = a == b; + #1 $finish; + end + endprogram + module Top(output logic same); + wire a = 1; + wire b = 1; + Check c(a, b, same); + endmodule + "#) + (top Top) + (run_to_finish) + (assert_eq same 1)) + +; A program shall not contain `always` procedures or instances (24.3). +(case programs_reject_always_procedures + (source "program_always.sv" #" + program Tb(input logic clk); + logic seen; + always @(posedge clk) seen = 1; + endprogram + module Top(input logic clk); + Tb t(clk); + endmodule + "#) + (top Top) + (expect reject)) + +(case programs_reject_instances + (source "program_instance.sv" #" + module Leaf(output logic y); + assign y = 1; + endmodule + program Tb; + logic y; + Leaf l(y); + endprogram + module Top; + Tb t(); + endmodule + "#) + (top Top) + (expect reject)) diff --git a/crates/celox-test-suite/verification/sv/limitations.json b/crates/celox-test-suite/verification/sv/limitations.json index a026da63bf5..92ccf06a1e1 100644 --- a/crates/celox-test-suite/verification/sv/limitations.json +++ b/crates/celox-test-suite/verification/sv/limitations.json @@ -2210,5 +2210,107 @@ "timing::always_star_timed_task_calls_wait_on_folded_branch_reads" ] } + }, + { + "id": "sv_program_reactive_region", + "category": "simulator_disagreement", + "phase": "execute", + "observed_version": "Verilator 5.052 2026-09-05 rev v5.052; Icarus 13.0", + "reason": "Verilator 5.052 and Icarus 13.0 run the initial procedures of a program in the active region set: a program reads design values before the design's nonblocking updates of the same time slot, its #0 resumes before the design's, and a flip-flop clocked by a program write runs before the program's #0 and its nonblocking update of the flip-flop's input. IEEE 1800-2023 24.3.1 schedules program procedures, their delays and event controls in the Reactive region, #0 in the Re-Inactive region and their nonblocking updates in the Re-NBA region, and 4.5 runs the reactive region set only once the active region set is empty and empties it before the active region set runs again.", + "upstream": [], + "evidence": [ + "verification/sv/limitations/programs-icarus.json", + "verification/sv/limitations/programs-verilator.json", + "LIMITATIONS.md#systemverilog-suite" + ], + "cases": { + "icarus": [ + "program::program_reads_design_after_nonblocking_updates_settle", + "program::program_nonblocking_updates_follow_design_updates", + "program::program_zero_delay_resumes_in_the_re_inactive_region", + "program::design_activity_of_a_program_waits_for_the_reactive_region_set", + "program::program_waits_see_host_driven_edges_after_design_updates" + ], + "verilator": [ + "program::program_reads_design_after_nonblocking_updates_settle", + "program::program_nonblocking_updates_follow_design_updates", + "program::program_zero_delay_resumes_in_the_re_inactive_region", + "program::design_activity_of_a_program_waits_for_the_reactive_region_set", + "program::program_waits_see_host_driven_edges_after_design_updates" + ] + } + }, + { + "id": "sv_program_completion", + "category": "simulator_disagreement", + "phase": "execute", + "observed_version": "Verilator 5.052 2026-09-05 rev v5.052; Icarus 13.0", + "reason": "Verilator 5.052 and Icarus 13.0 do not end the simulation when every initial procedure of every program has ended: Icarus keeps running the design's forever loop and Verilator runs until the testbench times out. IEEE 1800-2023 24.3 ends the simulation then with an implicit $finish.", + "upstream": [], + "evidence": [ + "verification/sv/limitations/programs-icarus.json", + "verification/sv/limitations/programs-verilator.json", + "LIMITATIONS.md#systemverilog-suite" + ], + "cases": { + "icarus": [ + "program::simulation_ends_when_program_procedures_end" + ], + "verilator": [ + "program::simulation_ends_when_program_procedures_end" + ] + } + }, + { + "id": "sv_icarus_program_exit", + "category": "simulator_unsupported", + "phase": "execute", + "observed_version": "Icarus 13.0", + "reason": "Icarus 13.0 does not define $exit (\"System task/function $exit() is not defined by any module\"). IEEE 1800-2023 24.7 ends every initial procedure of the calling program.", + "upstream": [], + "evidence": [ + "verification/sv/limitations/programs-icarus.json", + "LIMITATIONS.md#systemverilog-suite" + ], + "cases": { + "icarus": [ + "program::program_exit_ends_its_initial_procedures" + ] + } + }, + { + "id": "sv_verilator_program_exit", + "category": "simulator_disagreement", + "phase": "execute", + "observed_version": "Verilator 5.052 2026-09-05 rev v5.052", + "reason": "Verilator 5.052 continues a program initial procedure after $exit and does not end the program's other initial procedures. IEEE 1800-2023 24.7 ends every initial procedure of the calling program.", + "upstream": [], + "evidence": [ + "verification/sv/limitations/programs-verilator.json", + "LIMITATIONS.md#systemverilog-suite" + ], + "cases": { + "verilator": [ + "program::program_exit_ends_its_initial_procedures" + ] + } + }, + { + "id": "sv_verilator_program_items", + "category": "simulator_compile_limitation", + "phase": "compile", + "observed_version": "Verilator 5.052 2026-09-05 rev v5.052", + "reason": "Verilator 5.052 rejects an always procedure and a module instance in a program with syntax errors, which the run cannot count as language rejections. Both are illegal in a program (IEEE 1800-2023 24.3), and Icarus rejects them with diagnostics that name the rule.", + "upstream": [], + "evidence": [ + "verification/sv/limitations/programs-verilator.json", + "LIMITATIONS.md#systemverilog-suite" + ], + "cases": { + "verilator": [ + "program::programs_reject_always_procedures", + "program::programs_reject_instances" + ] + } } ] diff --git a/crates/celox-test-suite/verification/sv/limitations/programs-icarus.json b/crates/celox-test-suite/verification/sv/limitations/programs-icarus.json new file mode 100644 index 00000000000..07d9f83315a --- /dev/null +++ b/crates/celox-test-suite/verification/sv/limitations/programs-icarus.json @@ -0,0 +1,126 @@ +{ + "cases": [ + { + "case_fingerprint": "98d78b162572ed7fca59bfc2c9f2e37137962de997f7460cd9dfc234e7718177", + "category": "Sequential", + "detail": "program::design_activity_of_a_program_waits_for_the_reactive_region_set at 118:3: expect_output: expected [114, 101, 45, 105, 110, 97, 99, 116, 105, 118, 101, 32, 113, 61, 48, 32, 100, 61, 48, 10, 100, 101, 115, 105, 103, 110, 32, 101, 100, 103, 101, 32, 100, 61, 55, 10, 113, 61, 55, 10], got [100, 101, 115, 105, 103, 110, 32, 101, 100, 103, 101, 32, 100, 61, 48, 10, 114, 101, 45, 105, 110, 97, 99, 116, 105, 118, 101, 32, 113, 61, 48, 32, 100, 61, 48, 10, 113, 61, 48, 10]", + "expectation": "Simulation", + "name": "program::design_activity_of_a_program_waits_for_the_reactive_region_set", + "phase": "execute", + "reused": false, + "status": "mismatch", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [], + "verified_at_unix": 1792831670 + }, + { + "case_fingerprint": "41e828824fc2bc36cabe1b6dbf16395623a3e6ab2206cff419d19bfbcffb6ebc", + "category": "Sequential", + "detail": "native testbench failed: exit status: 1; see runtime.log and protocol.log\n/source_0.sv:9: Error: System task/function $exit() is not defined by any module.\n", + "expectation": "Simulation", + "name": "program::program_exit_ends_its_initial_procedures", + "phase": "execute", + "reused": false, + "status": "runtime_error", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [], + "verified_at_unix": 1792831671 + }, + { + "case_fingerprint": "e8bec7bc06fc8cbb0d30f66d98b13643fca063f1f39ad1dfbc155f9862f0b32a", + "category": "Sequential", + "detail": "program::program_nonblocking_updates_follow_design_updates at 62:3: expect_output: expected [100, 101, 115, 105, 103, 110, 32, 115, 101, 101, 115, 32, 114, 61, 49, 32, 99, 111, 117, 110, 116, 61, 48, 10, 112, 114, 111, 103, 114, 97, 109, 32, 99, 111, 117, 110, 116, 61, 49, 32, 114, 61, 49, 10, 100, 101, 115, 105, 103, 110, 32, 115, 101, 101, 115, 32, 114, 61, 49, 49, 32, 99, 111, 117, 110, 116, 61, 49, 10, 112, 114, 111, 103, 114, 97, 109, 32, 99, 111, 117, 110, 116, 61, 50, 32, 114, 61, 49, 49, 10, 100, 101, 115, 105, 103, 110, 32, 115, 101, 101, 115, 32, 114, 61, 49, 50, 32, 99, 111, 117, 110, 116, 61, 50, 10], got [100, 101, 115, 105, 103, 110, 32, 115, 101, 101, 115, 32, 114, 61, 49, 32, 99, 111, 117, 110, 116, 61, 48, 10, 112, 114, 111, 103, 114, 97, 109, 32, 99, 111, 117, 110, 116, 61, 48, 32, 114, 61, 49, 10, 100, 101, 115, 105, 103, 110, 32, 115, 101, 101, 115, 32, 114, 61, 49, 48, 32, 99, 111, 117, 110, 116, 61, 49, 10, 112, 114, 111, 103, 114, 97, 109, 32, 99, 111, 117, 110, 116, 61, 49, 32, 114, 61, 49, 48, 10, 100, 101, 115, 105, 103, 110, 32, 115, 101, 101, 115, 32, 114, 61, 49, 49, 32, 99, 111, 117, 110, 116, 61, 50, 10]", + "expectation": "Simulation", + "name": "program::program_nonblocking_updates_follow_design_updates", + "phase": "execute", + "reused": false, + "status": "mismatch", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [], + "verified_at_unix": 1792831670 + }, + { + "case_fingerprint": "b2c9d6e02e663d5d87fcc72893fc4826435ea647ec065fa3e5d1a634df2595c7", + "category": "Sequential", + "detail": "program::program_reads_design_after_nonblocking_updates_settle at 33:3: expect_output: expected [115, 116, 97, 114, 116, 32, 113, 61, 48, 32, 113, 113, 61, 49, 10, 100, 101, 115, 105, 103, 110, 32, 113, 61, 48, 32, 113, 113, 61, 49, 10, 101, 100, 103, 101, 32, 113, 61, 53, 32, 113, 113, 61, 54, 10], got [115, 116, 97, 114, 116, 32, 113, 61, 48, 32, 113, 113, 61, 49, 10, 100, 101, 115, 105, 103, 110, 32, 113, 61, 48, 32, 113, 113, 61, 49, 10, 101, 100, 103, 101, 32, 113, 61, 48, 32, 113, 113, 61, 49, 10]", + "expectation": "Simulation", + "name": "program::program_reads_design_after_nonblocking_updates_settle", + "phase": "execute", + "reused": false, + "status": "mismatch", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [], + "verified_at_unix": 1792831670 + }, + { + "case_fingerprint": "1a3d0280a6f0d27dcdaa7ba200e64d226eb39bcf6eedd9956a64402dd30bb192", + "category": "Sequential", + "detail": "program::program_waits_see_host_driven_edges_after_design_updates at 141:3: assert_eq seen: expected 0x1, got 0x0 (the program reads the count after its update)", + "expectation": "Simulation", + "name": "program::program_waits_see_host_driven_edges_after_design_updates", + "phase": "execute", + "reused": false, + "status": "mismatch", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [], + "verified_at_unix": 1792837870 + }, + { + "case_fingerprint": "e1ea6e05b0ce14a9a9cd02ef9e69bf39339d91a8419056906f9438fce72fdb10", + "category": "Sequential", + "detail": "program::program_zero_delay_resumes_in_the_re_inactive_region at 88:3: expect_output: expected [100, 101, 115, 105, 103, 110, 32, 105, 110, 97, 99, 116, 105, 118, 101, 32, 97, 61, 49, 10, 114, 101, 97, 99, 116, 105, 118, 101, 32, 97, 61, 49, 10, 114, 101, 45, 105, 110, 97, 99, 116, 105, 118, 101, 32, 97, 61, 49, 32, 115, 101, 101, 110, 61, 49, 10], got [114, 101, 97, 99, 116, 105, 118, 101, 32, 97, 61, 48, 10, 114, 101, 45, 105, 110, 97, 99, 116, 105, 118, 101, 32, 97, 61, 49, 32, 115, 101, 101, 110, 61, 48, 10, 100, 101, 115, 105, 103, 110, 32, 105, 110, 97, 99, 116, 105, 118, 101, 32, 97, 61, 49, 10]", + "expectation": "Simulation", + "name": "program::program_zero_delay_resumes_in_the_re_inactive_region", + "phase": "execute", + "reused": false, + "status": "mismatch", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [], + "verified_at_unix": 1792831670 + }, + { + "case_fingerprint": "5e1d053a0c7b01273adbe8317ffc74f4daa1926606adb5cb0e508360115f5b26", + "category": "Sequential", + "detail": "program::simulation_ends_when_program_procedures_end at 163:3: assert_eq n: expected 0x3, got 0xb0d64f9", + "expectation": "Simulation", + "name": "program::simulation_ends_when_program_procedures_end", + "phase": "execute", + "reused": false, + "status": "mismatch", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [], + "verified_at_unix": 1792831701 + } + ], + "context_fingerprint": "d7cdfa72597b3b332e474a5926d6172cf630908f1bfb0e7c4466829d2bb64e44", + "counts": { + "compile_error": 0, + "emission_error": 0, + "ignored": 0, + "mismatch": 6, + "passed": 0, + "rejected": 0, + "runtime_error": 1, + "unexpected_accept": 0, + "unsupported": 0 + }, + "exclude_stronger_than_sv": false, + "include_ignored": false, + "incremental": true, + "run_counts": { + "fresh": 10, + "reused": 0 + }, + "schema_version": 3, + "suite": "sv", + "suite_version": "0.11.0", + "tool": "icarus", + "version": "Icarus Verilog version 13.0 (stable) (v13_0)" +} diff --git a/crates/celox-test-suite/verification/sv/limitations/programs-verilator.json b/crates/celox-test-suite/verification/sv/limitations/programs-verilator.json new file mode 100644 index 00000000000..8ae5f4463ea --- /dev/null +++ b/crates/celox-test-suite/verification/sv/limitations/programs-verilator.json @@ -0,0 +1,154 @@ +{ + "cases": [ + { + "case_fingerprint": "98d78b162572ed7fca59bfc2c9f2e37137962de997f7460cd9dfc234e7718177", + "category": "Sequential", + "detail": "program::design_activity_of_a_program_waits_for_the_reactive_region_set at 118:3: expect_output: expected [114, 101, 45, 105, 110, 97, 99, 116, 105, 118, 101, 32, 113, 61, 48, 32, 100, 61, 48, 10, 100, 101, 115, 105, 103, 110, 32, 101, 100, 103, 101, 32, 100, 61, 55, 10, 113, 61, 55, 10], got [100, 101, 115, 105, 103, 110, 32, 101, 100, 103, 101, 32, 100, 61, 48, 10, 114, 101, 45, 105, 110, 97, 99, 116, 105, 118, 101, 32, 113, 61, 48, 32, 100, 61, 48, 10, 113, 61, 48, 10]", + "expectation": "Simulation", + "name": "program::design_activity_of_a_program_waits_for_the_reactive_region_set", + "phase": "execute", + "reused": false, + "status": "mismatch", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [], + "verified_at_unix": 1792831897 + }, + { + "case_fingerprint": "41e828824fc2bc36cabe1b6dbf16395623a3e6ab2206cff419d19bfbcffb6ebc", + "category": "Sequential", + "detail": "program::program_exit_ends_its_initial_procedures at 142:3: expect_output: expected [102, 105, 114, 115, 116, 10, 115, 101, 99, 111, 110, 100, 10], got [102, 105, 114, 115, 116, 10, 115, 101, 99, 111, 110, 100, 10, 110, 111, 116, 32, 114, 101, 97, 99, 104, 101, 100, 10]", + "expectation": "Simulation", + "name": "program::program_exit_ends_its_initial_procedures", + "phase": "execute", + "reused": false, + "status": "mismatch", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [], + "verified_at_unix": 1792831909 + }, + { + "case_fingerprint": "e8bec7bc06fc8cbb0d30f66d98b13643fca063f1f39ad1dfbc155f9862f0b32a", + "category": "Sequential", + "detail": "program::program_nonblocking_updates_follow_design_updates at 62:3: expect_output: expected [100, 101, 115, 105, 103, 110, 32, 115, 101, 101, 115, 32, 114, 61, 49, 32, 99, 111, 117, 110, 116, 61, 48, 10, 112, 114, 111, 103, 114, 97, 109, 32, 99, 111, 117, 110, 116, 61, 49, 32, 114, 61, 49, 10, 100, 101, 115, 105, 103, 110, 32, 115, 101, 101, 115, 32, 114, 61, 49, 49, 32, 99, 111, 117, 110, 116, 61, 49, 10, 112, 114, 111, 103, 114, 97, 109, 32, 99, 111, 117, 110, 116, 61, 50, 32, 114, 61, 49, 49, 10, 100, 101, 115, 105, 103, 110, 32, 115, 101, 101, 115, 32, 114, 61, 49, 50, 32, 99, 111, 117, 110, 116, 61, 50, 10], got [100, 101, 115, 105, 103, 110, 32, 115, 101, 101, 115, 32, 114, 61, 49, 32, 99, 111, 117, 110, 116, 61, 48, 10, 112, 114, 111, 103, 114, 97, 109, 32, 99, 111, 117, 110, 116, 61, 48, 32, 114, 61, 49, 10, 100, 101, 115, 105, 103, 110, 32, 115, 101, 101, 115, 32, 114, 61, 49, 48, 32, 99, 111, 117, 110, 116, 61, 49, 10, 112, 114, 111, 103, 114, 97, 109, 32, 99, 111, 117, 110, 116, 61, 49, 32, 114, 61, 49, 48, 10, 100, 101, 115, 105, 103, 110, 32, 115, 101, 101, 115, 32, 114, 61, 49, 49, 32, 99, 111, 117, 110, 116, 61, 50, 10]", + "expectation": "Simulation", + "name": "program::program_nonblocking_updates_follow_design_updates", + "phase": "execute", + "reused": false, + "status": "mismatch", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [], + "verified_at_unix": 1792831897 + }, + { + "case_fingerprint": "b2c9d6e02e663d5d87fcc72893fc4826435ea647ec065fa3e5d1a634df2595c7", + "category": "Sequential", + "detail": "program::program_reads_design_after_nonblocking_updates_settle at 33:3: expect_output: expected [115, 116, 97, 114, 116, 32, 113, 61, 48, 32, 113, 113, 61, 49, 10, 100, 101, 115, 105, 103, 110, 32, 113, 61, 48, 32, 113, 113, 61, 49, 10, 101, 100, 103, 101, 32, 113, 61, 53, 32, 113, 113, 61, 54, 10], got [115, 116, 97, 114, 116, 32, 113, 61, 48, 32, 113, 113, 61, 48, 10, 100, 101, 115, 105, 103, 110, 32, 113, 61, 48, 32, 113, 113, 61, 49, 10, 101, 100, 103, 101, 32, 113, 61, 48, 32, 113, 113, 61, 49, 10]", + "expectation": "Simulation", + "name": "program::program_reads_design_after_nonblocking_updates_settle", + "phase": "execute", + "reused": false, + "status": "mismatch", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [], + "verified_at_unix": 1792831897 + }, + { + "case_fingerprint": "1a3d0280a6f0d27dcdaa7ba200e64d226eb39bcf6eedd9956a64402dd30bb192", + "category": "Sequential", + "detail": "program::program_waits_see_host_driven_edges_after_design_updates at 141:3: assert_eq seen: expected 0x1, got 0x0 (the program reads the count after its update)", + "expectation": "Simulation", + "name": "program::program_waits_see_host_driven_edges_after_design_updates", + "phase": "execute", + "reused": false, + "status": "mismatch", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [], + "verified_at_unix": 1792838391 + }, + { + "case_fingerprint": "e1ea6e05b0ce14a9a9cd02ef9e69bf39339d91a8419056906f9438fce72fdb10", + "category": "Sequential", + "detail": "program::program_zero_delay_resumes_in_the_re_inactive_region at 88:3: expect_output: expected [100, 101, 115, 105, 103, 110, 32, 105, 110, 97, 99, 116, 105, 118, 101, 32, 97, 61, 49, 10, 114, 101, 97, 99, 116, 105, 118, 101, 32, 97, 61, 49, 10, 114, 101, 45, 105, 110, 97, 99, 116, 105, 118, 101, 32, 97, 61, 49, 32, 115, 101, 101, 110, 61, 49, 10], got [114, 101, 97, 99, 116, 105, 118, 101, 32, 97, 61, 48, 10, 100, 101, 115, 105, 103, 110, 32, 105, 110, 97, 99, 116, 105, 118, 101, 32, 97, 61, 49, 10, 114, 101, 45, 105, 110, 97, 99, 116, 105, 118, 101, 32, 97, 61, 49, 32, 115, 101, 101, 110, 61, 48, 10]", + "expectation": "Simulation", + "name": "program::program_zero_delay_resumes_in_the_re_inactive_region", + "phase": "execute", + "reused": false, + "status": "mismatch", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [], + "verified_at_unix": 1792831897 + }, + { + "case_fingerprint": "cd0097ccbf44d7e034bdbabdbddc680dbf4b6a3670fe175986c6650389d9ab56", + "category": "Sequential", + "detail": "Verilator build exit status: 1; see /build.log\n%Error: /source_0.sv:4:7: syntax error, unexpected always\n 4 | always @(posedge clk) seen = 1;\n | ^~~~~~\n ... See the manual at https://verilator.org/verilator_doc.html?v=5.052 for more assistance.\n%Error: Exiting due to 1 error(s)\n", + "expectation": "CompilationError", + "name": "program::programs_reject_always_procedures", + "phase": "compile", + "reused": false, + "status": "compile_error", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [], + "verified_at_unix": 1792831897 + }, + { + "case_fingerprint": "47c8dbcf8ce4ca92c6b7e653ffa4b7bfcf85e1ba8820a11ab4d7ea329eb0bc19", + "category": "Sequential", + "detail": "Verilator build exit status: 1; see /build.log\n%Error: /source_0.sv:7:7: syntax error, unexpected IDENTIFIER-for-instance\n 7 | Leaf l(y);\n | ^~~~\n ... See the manual at https://verilator.org/verilator_doc.html?v=5.052 for more assistance.\n%Error: Exiting due to 1 error(s)\n", + "expectation": "CompilationError", + "name": "program::programs_reject_instances", + "phase": "compile", + "reused": false, + "status": "compile_error", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [], + "verified_at_unix": 1792831897 + }, + { + "case_fingerprint": "5e1d053a0c7b01273adbe8317ffc74f4daa1926606adb5cb0e508360115f5b26", + "category": "Sequential", + "detail": "native testbench failed: exit status: 124; see runtime.log and protocol.log\n", + "expectation": "Simulation", + "name": "program::simulation_ends_when_program_procedures_end", + "phase": "execute", + "reused": false, + "status": "runtime_error", + "stronger_than_sv": false, + "tag_reasons": {}, + "tags": [], + "verified_at_unix": 1792831939 + } + ], + "context_fingerprint": "beab703827e726cd3a44bda4eb8c7b9a41ee8465f115c88842d25373082bebb3", + "counts": { + "compile_error": 2, + "emission_error": 0, + "ignored": 0, + "mismatch": 6, + "passed": 0, + "rejected": 0, + "runtime_error": 1, + "unexpected_accept": 0, + "unsupported": 0 + }, + "exclude_stronger_than_sv": false, + "include_ignored": false, + "incremental": true, + "run_counts": { + "fresh": 10, + "reused": 0 + }, + "schema_version": 3, + "suite": "sv", + "suite_version": "0.11.0", + "tool": "verilator", + "version": "Verilator 5.052 2026-09-05 rev v5.052" +} diff --git a/crates/celox/tests/frontends/systemverilog/timing.rs b/crates/celox/tests/frontends/systemverilog/timing.rs index 83bcaca0f58..2a47cf7fd4f 100644 --- a/crates/celox/tests/frontends/systemverilog/timing.rs +++ b/crates/celox/tests/frontends/systemverilog/timing.rs @@ -48,6 +48,20 @@ macro_rules! timed_cases { }; } +// Program blocks (IEEE 1800-2023 Clause 24) and the reactive region set. +timed_cases! { + fn program_reads_design_after_nonblocking_updates_settle = "program::program_reads_design_after_nonblocking_updates_settle"; + fn program_nonblocking_updates_follow_design_updates = "program::program_nonblocking_updates_follow_design_updates"; + fn program_zero_delay_resumes_in_the_re_inactive_region = "program::program_zero_delay_resumes_in_the_re_inactive_region"; + fn design_activity_of_a_program_waits_for_the_reactive_region_set = "program::design_activity_of_a_program_waits_for_the_reactive_region_set"; + fn program_exit_ends_its_initial_procedures = "program::program_exit_ends_its_initial_procedures"; + fn simulation_ends_when_program_procedures_end = "program::simulation_ends_when_program_procedures_end"; + fn program_ports_connect_to_design_nets = "program::program_ports_connect_to_design_nets"; + fn programs_reject_always_procedures = "program::programs_reject_always_procedures"; + fn programs_reject_instances = "program::programs_reject_instances"; + fn program_waits_see_host_driven_edges_after_design_updates = "program::program_waits_see_host_driven_edges_after_design_updates"; +} + timed_cases! { fn timed_task_automatic_scalar_calls_keep_separate_storage = "timing::timed_task_automatic_scalar_calls_keep_separate_storage"; fn timed_task_four_state_storage_survives_suspension = "timing::timed_task_four_state_storage_survives_suspension"; diff --git a/docs/guide/systemverilog-reference.md b/docs/guide/systemverilog-reference.md index 5aa24a66073..b0a32f9096d 100644 --- a/docs/guide/systemverilog-reference.md +++ b/docs/guide/systemverilog-reference.md @@ -17,6 +17,7 @@ synthesis and is tested at the design level. | Area | Supported | | --- | --- | | Modules | ANSI ports, `parameter` / `localparam`, type parameters (`parameter type`), named and positional port and parameter connections, hierarchy, instance arrays (a connection is broadcast, or split between the elements when it is as wide as all of them) | +| Programs | `program` declarations with ports and parameters, instantiated from modules like a module; their `initial` procedures run in the reactive region set (see [Programs](#programs)) | | Generate | `for` (with `genvar`), `if`, `case`; constant expressions including `**` | | Packages | types, parameters, enums and functions, through `import p::*;`, `import p::x;` and `p::x` | | Interfaces | interfaces with value and type parameters, header package imports, `localparam`s, `typedef`s, variables and nets, logic (`assign`, `always_comb`, `always_ff`, generate) and functions; interface instances and instance arrays, also inside generate blocks and other interfaces; interface ports with or without a modport, generic `interface` and `interface.mp` ports, and arrays of interface ports; explicit and implicit named interface connections (`.bus(bus)` and `.bus`); modport `input` / `output` members and imported functions; access to members, parameters and functions through an instance or port (`h[i].m`, `h.P`, `h.f(...)`, `h[i].f(...)` with a constant element index) | @@ -513,13 +514,40 @@ simulation is suspended (`CheckpointError::Stopped`). In an `always` process, `$ Its diagnostic level argument prints nothing. Batch hosts without an interactive mode, such as the VPI host, end the run at `$stop`. +### Programs {#programs} + +A `program` (IEEE 1800-2023 Clause 24) is declared and instantiated like a +leaf module. Its `initial` procedures run in the reactive region set: they +start, resume after `#` delays and wake on event controls in the Reactive +region, `#0` resumes them in the Re-Inactive region, and their nonblocking +assignments update in the Re-NBA region (4.4.2.6–4.4.2.8, 24.3.1). The +reactive region set runs once the active region set of a time slot is empty, +and the design activity it creates runs in the active region set again +(4.5), so a program reads the design after its nonblocking updates and the +logic they feed have settled. Subroutines a program procedure calls run in +its region (24.5). Design processes keep the compiled scheduler; program +procedures run in the reference scheduler. + +When every `initial` procedure of every program has ended, the simulation +ends with an implicit `$finish` (24.3). A program shall not contain `always` +procedures or instances or declarations of modules, interfaces and programs; +Celox rejects them by name. + +Not supported: continuous assignments in a program are evaluated with the +design's combinational logic rather than in the Reactive region; programs +nested in modules and anonymous programs are rejected; a program that no +module instantiates is not an implicit top level next to the design's top +module; `final` procedures and clocking blocks are unsupported in programs as +everywhere. + ### `$exit` -`$exit` ends the initial procedures of the program block that calls it -(24.7). Celox has no program blocks, and a call from anywhere else is +`$exit` in a thread of a program's `initial` procedure ends every `initial` +procedure of that program instance (24.7), and with them the simulation once +no program procedure is left. A call from anywhere else is ignored, as 24.7 specifies, with a compilation warning (`Simulator::warnings()`) at the call. This includes a call in a constant -function. Calls in function declarations are diagnosed even when the function +function of a module. Calls in function declarations are diagnosed even when the function is unused; the diagnostic does not depend on constant evaluation executing it. ### Immediate cover diff --git a/docs/guide/systemverilog.md b/docs/guide/systemverilog.md index 7460ee2e29b..49f40d7e00d 100644 --- a/docs/guide/systemverilog.md +++ b/docs/guide/systemverilog.md @@ -79,7 +79,7 @@ The main supported areas are: | Area | Examples | | --- | --- | -| Design structure | Modules, hierarchy, parameters, generate blocks, packages, interfaces and modports | +| Design structure | Modules, hierarchy, parameters, generate blocks, packages, interfaces and modports, and programs, whose procedures run in the reactive region set | | 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 | diff --git a/docs/ja/guide/systemverilog-reference.md b/docs/ja/guide/systemverilog-reference.md index f49f8709926..84095da4942 100644 --- a/docs/ja/guide/systemverilog-reference.md +++ b/docs/ja/guide/systemverilog-reference.md @@ -16,6 +16,7 @@ outline: [2, 3] | 領域 | サポート内容 | | --- | --- | | モジュール | ANSI ポート、`parameter` / `localparam`、型パラメータ(`parameter type`)、名前付き・位置指定のポート/パラメータ接続、階層、インスタンス配列(接続は全要素に共通、または全要素の幅に等しければ要素ごとに分割) | +| プログラム | ポートとパラメータを持つ `program` 宣言。モジュールと同じくモジュールからインスタンス化し、その `initial` 手続きはリアクティブ領域セットで実行します(下記の「プログラム」を参照) | | generate | `for`(`genvar`)、`if`、`case`。`**` を含む定数式 | | package | 型、パラメータ、enum、関数。`import p::*;`、`import p::x;`、`p::x` で参照 | | interface | 値・型パラメータ、ヘッダーの package import、メンバー、内部ロジックと関数、入れ子の interface インスタンス、インスタンス配列、定数添字で選ぶ配列要素の関数呼び出し、modport 付き・なしのポート、generic interface ポート、interface ポート配列。明示・暗黙の名前付き接続(`.bus(bus)` / `.bus`)、`input` / `output` modport と imported function。詳しい制約は下記参照 | @@ -446,9 +447,26 @@ offset を使い、先頭(0)、現在位置(1)、末尾(2)を基準 `always` 系のプロセスでは、`$finish` が `RuntimeEvent::Finish` に なるのと同様に `RuntimeEvent::Stop` として報告します。診断レベルの引数では何も 表示しません。VPI ホストのように対話モードを持たないバッチ実行では `$stop` で終了します。 -`$exit` は呼び出した program ブロックの initial 手続きを終了します(24.7)。 -Celox は program ブロックを持たず、それ以外からの呼び出しは 24.7 の規定どおり無視し、 -その呼び出し位置でコンパイル警告(`Simulator::warnings()`)を出します。定数関数内の呼び出しも同様です。 +`program`(IEEE 1800-2023 24 章)はリーフモジュールと同じく宣言・インスタンス化します。 +その `initial` 手続きはリアクティブ領域セットで実行します。開始、`#` 遅延後の再開、 +イベント制御での起床は Reactive 領域、`#0` からの再開は Re-Inactive 領域で行い、 +ノンブロッキング代入は Re-NBA 領域で更新します(4.4.2.6〜4.4.2.8、24.3.1)。 +リアクティブ領域セットはタイムスロットのアクティブ領域セットが空になってから実行し、 +それが起こした設計側の動作は再びアクティブ領域セットで実行します(4.5)。そのため +program は、ノンブロッキング更新とそれを受ける論理が落ち着いた後の設計を読みます。 +program の手続きが呼ぶサブルーチンはその手続きの領域で実行します(24.5)。 +設計のプロセスはコンパイル済みスケジューラのまま、program の手続きは参照スケジューラで実行します。 +すべての program のすべての `initial` 手続きが終わると、暗黙の `$finish` でシミュレーションを +終了します(24.3)。program 内の `always` 手続きと、モジュール・インターフェース・program の +宣言やインスタンスは名前を示して拒否します。 +未対応: program 内の連続代入は Reactive 領域ではなく設計の組み合わせ論理として評価します。 +モジュール内に入れ子の program と無名 program は拒否します。どのモジュールからも +インスタンス化されない program は、設計のトップモジュールと並ぶ暗黙のトップにはなりません。 + +`$exit` は、program の `initial` 手続きのスレッドから呼ぶと、その program インスタンスの +すべての `initial` 手続きを終了し(24.7)、program の手続きが残らなければシミュレーションも +終了します。それ以外からの呼び出しは 24.7 の規定どおり無視し、 +その呼び出し位置でコンパイル警告(`Simulator::warnings()`)を出します。モジュールの定数関数内の呼び出しも同様です。 関数宣言内の呼び出しは、その関数を使わなくても警告します。定数評価が実行したかどうかには依存しません。 ### 実行時 I/O と plusargs From 3913becc5647fc25f4bb4790a6f8d0b032f51187 Mon Sep 17 00:00:00 2001 From: tignear Date: Sat, 24 Oct 2026 19:41:46 +0900 Subject: [PATCH 2/2] test(sv): expect sv-tests program_construct to pass Change-Id: I510de44b7d695ee363e6ed51ef971ed9721252a4 --- conformance/sv-tests/expected.tsv | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conformance/sv-tests/expected.tsv b/conformance/sv-tests/expected.tsv index 535226b7930..6f06f0c9c4c 100644 --- a/conformance/sv-tests/expected.tsv +++ b/conformance/sv-tests/expected.tsv @@ -979,8 +979,8 @@ chapter-23/23.2--module-definition.sv 2state pass chapter-23/23.2--module-definition.sv 4state pass chapter-23/23.2--module-label.sv 2state pass chapter-23/23.2--module-label.sv 4state pass -chapter-24/24.3--program.sv 2state fail -chapter-24/24.3--program.sv 4state fail +chapter-24/24.3--program.sv 2state pass +chapter-24/24.3--program.sv 4state pass chapter-25/25.3-interface.sv 2state pass chapter-25/25.3-interface.sv 4state pass chapter-26/26.2--package-decl.sv 2state pass