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
7 changes: 4 additions & 3 deletions crates/celox-vpi/src/bin/celox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn run_sv(arguments: RunConfig) -> Result<(), String> {
.map(|(text, path)| (text.as_str(), path.as_path()))
.collect();
let builder = celox::Simulation::from_sv_sources(source_refs, &arguments.design.top)
.four_state(arguments.execution.four_state)
.four_state(arguments.design.four_state.unwrap_or(false))
.threads(arguments.execution.threads as usize)
.opt_level(arguments.opt_level)
.diagnostic_policy(arguments.design.diagnostics.clone())
Expand Down Expand Up @@ -403,7 +403,7 @@ fn compile_native_design(design: &Design) -> Result<NativeProgramImage, String>
.map(|(text, path)| (text.as_str(), path.as_path()))
.collect();
let builder = celox::Simulator::from_sv_sources(refs, &design.top)
.four_state(true)
.four_state(design.four_state.unwrap_or(false))
.diagnostic_policy(design.diagnostics.clone())
.signal_access(design.signal_access.clone())
.opt_level(design.opt_level.unwrap_or(celox::OptLevel::O1));
Expand All @@ -427,13 +427,14 @@ fn compile_native_design(design: &Design) -> Result<NativeProgramImage, String>
{
return Err("pass .sv/.v sources, or one .veryl project entry file".to_string());
}
let (image, warnings) = celox_vpi::driver::compile_native_image_with_parameters(
let (image, warnings) = celox_vpi::driver::compile_native_image_with_state(
&design.sources[0],
&design.top,
design.diagnostics.clone(),
design.opt_level.unwrap_or(celox::OptLevel::O1),
design.signal_access.clone(),
&design.parameters,
design.four_state.unwrap_or(false),
)?;
report_warnings(&warnings)?;
Ok(image)
Expand Down
104 changes: 94 additions & 10 deletions crates/celox-vpi/src/bin/celox/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ pub(super) struct DesignArgs {
/// Override a compilation diagnostic: CODE=allow|warn|error (repeatable).
#[arg(long = "diagnostic", value_name = "CODE=LEVEL")]
diagnostics: Vec<String>,
/// Use two-state simulation, resolving X/Z to zero.
#[arg(long, conflicts_with = "four_state")]
two_state: bool,
/// Use four-state simulation, overriding the project (default: two-state).
#[arg(long)]
four_state: bool,
/// Optimization level: O0, O1, O2 (default: project setting or O2 for run, O1 for vpi build).
#[arg(short = 'O', long, value_parser = super::parse_opt_level)]
opt_level: Option<celox::OptLevel>,
Expand Down Expand Up @@ -56,7 +62,7 @@ pub(super) struct DesignArgs {
/// SystemVerilog macro, NAME or NAME=VALUE (repeatable; last value wins).
#[arg(short = 'D', long = "define", value_name = "NAME[=VALUE]")]
defines: Vec<String>,
/// Override a numeric top parameter (repeatable; last value wins).
/// Override a top parameter with an integer or SV literal (repeatable; last value wins).
#[arg(
short = 'G',
long = "parameter",
Expand Down Expand Up @@ -95,6 +101,7 @@ struct Manifest {
parameters: BTreeMap<String, ParameterValue>,
top: Option<String>,
opt_level: Option<String>,
four_state: Option<bool>,
live_signals: Option<Vec<String>>,
force_signals: Option<Vec<String>>,
max_steps: Option<u64>,
Expand Down Expand Up @@ -127,6 +134,7 @@ struct Target {
parameters: BTreeMap<String, ParameterValue>,
top: Option<String>,
opt_level: Option<String>,
four_state: Option<bool>,
live_signals: Option<Vec<String>>,
force_signals: Option<Vec<String>>,
max_steps: Option<u64>,
Expand All @@ -140,8 +148,10 @@ pub(super) struct Design {
pub include_dirs: Vec<PathBuf>,
pub defines: BTreeMap<String, String>,
pub parameters: BTreeMap<String, u64>,
sv_parameters: BTreeMap<String, String>,
pub top: String,
pub opt_level: Option<celox::OptLevel>,
pub four_state: Option<bool>,
pub signal_access: celox::SignalAccessOptions,
pub max_steps: Option<u64>,
}
Expand Down Expand Up @@ -275,6 +285,7 @@ impl DesignArgs {
design.signal_access.force_signals =
config.force_signals.clone().unwrap_or_default();
design.max_steps = config.max_steps;
design.four_state = config.four_state;
if let Some(target) = selected {
design.extend_parameters(&target.parameters)?;
design.diagnostics.0.extend(target.diagnostics.0.clone());
Expand All @@ -296,6 +307,7 @@ impl DesignArgs {
design.signal_access.force_signals.clone_from(paths);
}
design.max_steps = target.max_steps.or(design.max_steps);
design.four_state = target.four_state.or(design.four_state);
}
design.opt_level = opt_level
.map(|value| {
Expand All @@ -322,6 +334,12 @@ impl DesignArgs {
}
}));
design.opt_level = self.opt_level.or(design.opt_level);
if self.two_state {
design.four_state = Some(false);
}
if self.four_state {
design.four_state = Some(true);
}
if self.no_live_signals {
design.signal_access.live_signals.clear();
} else if !self.live_signals.is_empty() {
Expand Down Expand Up @@ -359,7 +377,7 @@ impl DesignArgs {
let (name, value) = parameter
.split_once('=')
.ok_or_else(|| format!("invalid parameter `{parameter}`: expected NAME=VALUE"))?;
design.set_parameter(name, parse_parameter_value(value)?)?;
design.set_parameter_text(name, value, false)?;
}
if let Some(top) = self.top {
design.top = top;
Expand All @@ -372,6 +390,9 @@ impl DesignArgs {
if design.top.trim().is_empty() {
return Err("provide --top MODULE or set top in celox.toml".to_string());
}
if !design.is_sv() && !design.sv_parameters.is_empty() {
return Err("SV parameter literals require .sv/.v sources".to_string());
}
if !design.is_sv() && (!design.include_dirs.is_empty() || !design.defines.is_empty()) {
return Err(
"include directories and defines require SystemVerilog sources".to_string(),
Expand All @@ -381,12 +402,13 @@ impl DesignArgs {
}
}

/// TOML integers cover ordinary values; strings also permit radix prefixes
/// and the full unsigned 64-bit range supported by SimulatorBuilder::param.
/// Preserve ordinary integer settings while admitting signed SV literals and
/// textual parameters without losing their width or signedness.
#[derive(Deserialize)]
#[serde(untagged)]
enum ParameterValue {
Integer(u64),
Signed(i64),
String(String),
}

Expand Down Expand Up @@ -424,6 +446,9 @@ impl Design {
for (name, value) in &self.parameters {
builder = builder.param(name, *value);
}
for (name, literal) in &self.sv_parameters {
builder = builder.param_literal(name, literal);
}
builder
}

Expand All @@ -432,16 +457,25 @@ impl Design {
parameters: &BTreeMap<String, ParameterValue>,
) -> Result<(), String> {
for (name, value) in parameters {
let value = match value {
ParameterValue::Integer(value) => *value,
ParameterValue::String(value) => parse_parameter_value(value)?,
};
self.set_parameter(name, value)?;
match value {
ParameterValue::Integer(value) => self.set_parameter(name, *value)?,
ParameterValue::Signed(value) => {
self.set_parameter_text(name, &value.to_string(), false)?
}
ParameterValue::String(value) => self.set_parameter_text(name, value, true)?,
}
}
Ok(())
}

fn set_parameter(&mut self, name: &str, value: u64) -> Result<(), String> {
Self::validate_parameter_name(name)?;
self.sv_parameters.remove(name);
self.parameters.insert(name.to_string(), value);
Ok(())
}

fn validate_parameter_name(name: &str) -> Result<(), String> {
if name.is_empty()
|| !name.bytes().enumerate().all(|(index, byte)| {
byte == b'_'
Expand All @@ -451,10 +485,60 @@ impl Design {
{
return Err(format!("invalid top parameter name `{name}`"));
}
self.parameters.insert(name.to_string(), value);
Ok(())
}

fn set_parameter_text(
&mut self,
name: &str,
text: &str,
bare_string: bool,
) -> Result<(), String> {
if let Ok(value) = parse_parameter_value(text) {
return self.set_parameter(name, value);
}
#[cfg(feature = "systemverilog")]
{
let literal = if celox_sv_analyzer::typecheck::parse_parameter_literal(text).is_some() {
text.to_string()
} else if bare_string {
// TOML strings are ordinary string values unless they spell a
// numeric literal. Encode bytes using SV's escape rules.
let mut quoted = String::from("\"");
for byte in text.bytes() {
match byte {
b'"' => quoted.push_str("\\\""),
b'\\' => quoted.push_str("\\\\"),
b'\n' => quoted.push_str("\\n"),
b'\t' => quoted.push_str("\\t"),
32..=126 => quoted.push(char::from(byte)),
0..=127 => quoted.push_str(&format!("\\{byte:03o}")),
_ => return Err(format!("non-ASCII string parameter `{name}`")),
}
}
quoted.push('"');
quoted
} else {
return Err(format!(
"invalid parameter value `{text}`: expected an unsigned 64-bit integer or SV literal"
));
};
// Reuse the name validation and remove whichever representation a
// lower-precedence project or target setting used.
Self::validate_parameter_name(name)?;
self.parameters.remove(name);
self.sv_parameters.insert(name.to_string(), literal);
Ok(())
}
#[cfg(not(feature = "systemverilog"))]
{
let _ = (name, bare_string);
Err(format!(
"invalid parameter value `{text}`: SV literals require the systemverilog feature"
))
}
}

pub fn is_sv(&self) -> bool {
!self.sources.is_empty()
&& self.sources.iter().all(|path| {
Expand Down
4 changes: 1 addition & 3 deletions crates/celox-vpi/src/bin/celox/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ pub(super) struct ExecutionArgs {
#[arg(long, value_enum, default_value = "native")]
pub backend: Backend,
#[arg(long)]
pub four_state: bool,
#[arg(long)]
compile_only: bool,
/// Write a pointer-free native image during compile-only mode.
#[arg(long, value_name = "PATH")]
Expand Down Expand Up @@ -197,7 +195,7 @@ pub(super) fn run(
test: design.top.clone(),
opt_level: design.opt_level.unwrap_or(OptLevel::O2),
backend: cli.backend,
four_state: cli.four_state,
four_state: design.four_state.unwrap_or(false),
compile_only: cli.compile_only,
native_image_output: cli.native_image_output,
native_image_input: cli.native_image_input,
Expand Down
46 changes: 39 additions & 7 deletions crates/celox-vpi/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ pub fn compile_native_image_with_diagnostics(
policy,
celox::OptLevel::O0,
celox::SignalAccessOptions::default(),
true,
CompileMode {
legacy_force: true,
four_state: true,
},
&BTreeMap::new(),
)
}
Expand All @@ -85,16 +88,45 @@ pub fn compile_native_image_with_parameters(
access: celox::SignalAccessOptions,
parameters: &BTreeMap<String, u64>,
) -> Result<(NativeProgramImage, Vec<CompilationWarning>), String> {
compile_native_image_configured(source, top, policy, level, access, false, parameters)
compile_native_image_with_state(source, top, policy, level, access, parameters, true)
}

fn compile_native_image_configured(
/// Compile numeric parameters with an explicit four-state simulation mode.
pub fn compile_native_image_with_state(
source: &Path,
top: &str,
policy: DiagnosticPolicy,
level: celox::OptLevel,
access: celox::SignalAccessOptions,
parameters: &BTreeMap<String, u64>,
four_state: bool,
) -> Result<(NativeProgramImage, Vec<CompilationWarning>), String> {
compile_native_image_configured(
source,
top,
policy,
level,
access,
CompileMode {
legacy_force: false,
four_state,
},
parameters,
)
}

struct CompileMode {
legacy_force: bool,
four_state: bool,
}

fn compile_native_image_configured(
source: &Path,
top: &str,
policy: DiagnosticPolicy,
level: celox::OptLevel,
access: celox::SignalAccessOptions,
mode: CompileMode,
parameters: &BTreeMap<String, u64>,
) -> Result<(NativeProgramImage, Vec<CompilationWarning>), String> {
let compiled = if let Ok(metadata_path) = Metadata::search_from(source) {
Expand All @@ -118,8 +150,8 @@ fn compile_native_image_configured(
let builder = Simulator::from_sources(source_refs, top)
.with_metadata(metadata)
.diagnostic_policy(policy.clone())
.four_state(true)
.native_force_support(legacy_force)
.four_state(mode.four_state)
.native_force_support(mode.legacy_force)
.signal_access(access.clone())
.opt_level(level);
parameters
Expand All @@ -133,8 +165,8 @@ fn compile_native_image_configured(
.map_err(|error| format!("failed to read {}: {error}", source.display()))?;
let builder = Simulator::from_sources(vec![(contents.as_str(), source)], top)
.diagnostic_policy(policy.clone())
.four_state(true)
.native_force_support(legacy_force)
.four_state(mode.four_state)
.native_force_support(mode.legacy_force)
.signal_access(access.clone())
.opt_level(level);
parameters
Expand Down
10 changes: 6 additions & 4 deletions crates/celox-vpi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1312,10 +1312,12 @@ pub unsafe extern "C" fn vpi_put_value(
}

let signal_info = with_runtime(|runtime| {
runtime
.reflection()
.signal(id)
.map(|signal| (signal.signal.width, signal.signal.is_4state))
runtime.reflection().signal(id).map(|signal| {
(
signal.signal.width,
signal.signal.is_4state && runtime.four_state(),
)
})
})
.flatten();
let Some((width, is_4state)) = signal_info else {
Expand Down
Loading
Loading