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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ jobs:
case "$feature" in celox/*) args+=(--features "$feature");; esac
done
CELOX_COCOTB_PYTHON="$(command -v python)" \
cargo test --locked -p celox-vpi "${args[@]}" --test cocotb_e2e \
cocotb_drives_an_attached_native_flip_flop -- --exact --ignored
cargo test --locked -p celox-vpi --features systemverilog "${args[@]}" --test cocotb_e2e \
cocotb_ -- --ignored

external-suites:
name: External suite (${{ matrix.suite }}, ${{ matrix.tool }})
Expand Down
2 changes: 2 additions & 0 deletions crates/celox-frontend-sv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ pub use lowering::{
interface_signatures_for_with_options,
prepare_external_hierarchy_with_foreign_resolver_with_options, schedule_sources_with_options,
};

pub use lowering::schedule_sources_with_force_signals;
146 changes: 143 additions & 3 deletions crates/celox-frontend-sv/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ pub enum FrontendError {
Lowering(#[from] ParserError),
}

// These names cannot be authored as unescaped SV identifiers and are hidden
// from the source-facing hierarchy. The runtime image records their addresses.
const FORCE_ENABLE: &str = "@force-enable:";
const FORCE_VALUE: &str = "@force-value:";

#[derive(Clone)]
struct SvVariable {
path: Vec<String>,
Expand All @@ -69,6 +74,8 @@ struct SvVariable {
source: Option<SourceLocation>,
/// A procedural local or a lowering temporary: not addressable by name.
hidden: bool,
/// Compiler-generated external override slot: (target, is_enable).
force_control: Option<(SourceVarId, bool)>,
}

impl SvVariable {
Expand Down Expand Up @@ -97,7 +104,7 @@ impl SvVariable {
/// A hidden variable other than the time slot, whose value the lowered
/// code computes. The time slot is state the scheduler writes.
fn is_scratch(&self) -> bool {
self.hidden && self.path != [TIME_SLOT]
self.hidden && self.path != [TIME_SLOT] && self.force_control.is_none()
}
}

Expand Down Expand Up @@ -135,6 +142,7 @@ struct AnalyzedSvModule {
pub(crate) struct LoweredSvInstance {
pub module_name: String,
pub instance_name: String,
pub force_signals: Vec<String>,
pub parameter_overrides: Vec<LoweredSvParameterOverride>,
pub port_connections: Vec<LoweredSvPortConnection>,
/// For an element of an instance array: its position in declaration order
Expand Down Expand Up @@ -163,13 +171,15 @@ pub(crate) struct LoweredSvPortConnection {
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) struct LoweredSvModuleKey {
pub name: String,
pub force_signals: Vec<String>,
pub parameter_overrides: Vec<LoweredSvParameterOverride>,
}

impl LoweredSvModuleKey {
pub fn base(name: String) -> Self {
Self {
name,
force_signals: Vec::new(),
parameter_overrides: Vec::new(),
}
}
Expand All @@ -179,6 +189,7 @@ impl LoweredSvModuleKey {
parameter_overrides.sort_by(|left, right| left.name.cmp(&right.name));
Self {
name: instance.module_name.clone(),
force_signals: instance.force_signals.clone(),
parameter_overrides,
}
}
Expand Down Expand Up @@ -267,7 +278,7 @@ fn interface_signatures_collecting(
.into_iter()
.map(|prototype| {
warnings.extend(crate::warning::analyzer_warnings(&prototype.warnings));
let lowered = lower_module(&prototype.module, false, true, false, warnings)?;
let lowered = lower_module(&prototype.module, false, true, false, warnings, &[])?;
Ok(InterfaceSignature {
name: prototype.name,
module: lowered.source,
Expand Down Expand Up @@ -774,7 +785,7 @@ fn lower_package_states(
.into_iter()
.enumerate()
.map(|(index, module)| {
let mut lowered = lower_module(module, four_state, true, ff_parts, warnings)?;
let mut lowered = lower_module(module, four_state, true, ff_parts, warnings, &[])?;
lowered.sim_module.time_precision = Some(
celox_design::TimePrecision::try_from(packages.global_time_precision())
.map_err(|reason| sv::AnalyzerError::Unsupported(reason.to_string()))?,
Expand Down Expand Up @@ -1360,6 +1371,7 @@ fn prepare_external_hierarchy_collecting(
type_kind: variable.metadata.type_kind,
source: variable.source.clone(),
hidden: !variable.module_affiliated,
force_control: None,
},
)
})
Expand Down Expand Up @@ -1725,6 +1737,46 @@ pub fn schedule_sources_with_options(
trace: Option<&mut FrontendTrace>,
warnings: &mut Vec<crate::FrontendWarning>,
analysis_options: sv::AnalysisOptions,
) -> Result<ScheduledRtlOutput, FrontendError> {
schedule_sources_with_force_signals(
sources,
top,
parameter_overrides,
ignored_loops,
true_loops,
four_state,
parallel,
trace_options,
trace,
warnings,
analysis_options,
&[],
)
}

/// Schedule with explicitly forceable signal names. Only these objects receive
/// external override inputs; unrelated logic retains its normal optimization.
#[allow(clippy::too_many_arguments)]
pub fn schedule_sources_with_force_signals(
sources: &[(&str, &Path)],
top: &str,
parameter_overrides: &[(String, u64)],
ignored_loops: &[(
(Vec<(String, usize)>, Vec<String>),
(Vec<(String, usize)>, Vec<String>),
)],
true_loops: &[(
(Vec<(String, usize)>, Vec<String>),
(Vec<(String, usize)>, Vec<String>),
usize,
)],
four_state: bool,
parallel: &celox_frontend_core::ParallelScheduleOptions,
trace_options: &FrontendTraceOptions,
trace: Option<&mut FrontendTrace>,
warnings: &mut Vec<crate::FrontendWarning>,
analysis_options: sv::AnalysisOptions,
force_signals: &[String],
) -> Result<ScheduledRtlOutput, FrontendError> {
let mut found = Vec::new();
let scheduled = schedule_sources_collecting(
Expand All @@ -1739,6 +1791,7 @@ pub fn schedule_sources_with_options(
trace,
&mut found,
analysis_options,
force_signals,
);
append_error_warnings(&scheduled, &mut found);
crate::warning::append_unique_warnings(sources, warnings, found);
Expand All @@ -1765,12 +1818,18 @@ fn schedule_sources_collecting(
trace: Option<&mut FrontendTrace>,
warnings: &mut Vec<crate::warning::PendingWarning>,
analysis_options: sv::AnalysisOptions,
force_signals: &[String],
) -> Result<ScheduledRtlOutput, FrontendError> {
let analyzed = analyze_sources(sources, &[top], analysis_options)?;
package_warnings(&analyzed, warnings);
let top = top.to_string();
let root_key = LoweredSvModuleKey {
name: top.clone(),
force_signals: force_signals
.iter()
.filter_map(|path| path.strip_prefix(&format!("{top}.")))
.map(str::to_string)
.collect(),
parameter_overrides: parameter_overrides
.iter()
.map(|(name, value)| LoweredSvParameterOverride {
Expand Down Expand Up @@ -2014,13 +2073,42 @@ fn specialize_module(
.iter()
.find(|candidate| candidate.name() == module.name)
.ok_or_else(|| sv::AnalyzerError::Unsupported(format!("module `{}`", module.name)))?;
let local_forces = key
.force_signals
.iter()
.map(|path| {
module
.interface_member_paths
.iter()
.find(|(_, segments)| segments.join(".") == *path)
.map_or_else(|| path.clone(), |(flat, _)| flat.clone())
})
.collect::<Vec<_>>();
let mut lowered = lower_module(
specialized,
four_state,
module.implicit_nets_allowed,
ff_parts,
warnings,
&local_forces,
)?;
for instance in &mut lowered.instances {
let name = if let Some((index, _)) = instance.array_element {
format!(
"{}[{}]",
instance.instance_name,
instance.array_index_base + index
)
} else {
instance.instance_name.clone()
};
let prefix = format!("{name}.");
instance.force_signals = key
.force_signals
.iter()
.filter_map(|path| path.strip_prefix(&prefix).map(str::to_string))
.collect();
}
lowered.sim_module.time_precision = Some(
celox_design::TimePrecision::try_from(module.packages.global_time_precision())
.map_err(|reason| sv::AnalyzerError::Unsupported(reason.to_string()))?,
Expand Down Expand Up @@ -2052,6 +2140,7 @@ fn lower_module(
implicit_nets_allowed: bool,
ff_parts: bool,
warnings: &mut Vec<crate::warning::PendingWarning>,
force_signals: &[String],
) -> Result<LoweredSvModule, sv::AnalyzerError> {
lower_module_with_overrides(
module,
Expand All @@ -2060,6 +2149,7 @@ fn lower_module(
implicit_nets_allowed,
ff_parts,
warnings,
force_signals,
)
}

Expand All @@ -2072,6 +2162,7 @@ fn lower_module_with_overrides(
implicit_nets_allowed: bool,
ff_parts: bool,
warnings: &mut Vec<crate::warning::PendingWarning>,
force_signals: &[String],
) -> Result<LoweredSvModule, sv::AnalyzerError> {
let mut package_variables = Vec::new();
let name = module.name().to_string();
Expand Down Expand Up @@ -2120,6 +2211,7 @@ fn lower_module_with_overrides(
type_kind: type_info.type_kind,
source: None,
hidden: false,
force_control: None,
};
name_to_id.insert(port.name().to_string(), id);
port_order.push(id);
Expand Down Expand Up @@ -2168,6 +2260,7 @@ fn lower_module_with_overrides(
type_kind: type_info.type_kind,
source: None,
hidden: false,
force_control: None,
};
name_to_id.insert(signal.name().to_string(), id);
// The package's object has its own default value.
Expand All @@ -2190,6 +2283,50 @@ fn lower_module_with_overrides(
variables.insert(id, variable);
}

let forceable = variables
.iter()
.filter_map(|(&id, var)| {
let name = &var.path[0];
force_signals.iter().any(|path| name == path).then_some(id)
})
.collect::<Vec<_>>();
for id in forceable {
let target = variables[&id].clone();
// Whole packed variables only. Array elements require a separately
// specified addressing contract rather than a guessed flat offset.
if !target.array_dims.is_empty() {
continue;
}
for (prefix, width, is_4state) in [
(FORCE_ENABLE, 1, false),
(FORCE_VALUE, target.width, target.is_4state),
] {
let control_name = format!("{prefix}{}", target.path[0]);
if name_to_id.contains_key(&control_name) {
return Err(sv::AnalyzerError::Unsupported(format!(
"signal `{control_name}` conflicts with a compiler force control"
)));
}
let mut control = target.clone();
control.force_control = Some((id, prefix == FORCE_ENABLE));
control.path = vec![control_name.clone()];
control.width = width;
control.signed = false;
control.is_4state = is_4state;
control.kind = VariableKind::Variable;
control.hidden = true;
control.packed_ranges = vec![(width as i128 - 1, 0)];
control.type_kind = if is_4state {
PortTypeKind::Logic
} else {
PortTypeKind::Bit
};
let control_id = next_var_id(&mut next_id);
variables.insert(control_id, control);
name_to_id.insert(control_name, control_id);
}
}

if !cfg!(feature = "dpi") && !module.dpi_imports().is_empty() {
return Err(sv::AnalyzerError::Unsupported(
"DPI-C import (enable the `sv-dpi` feature of `celox`)".to_string(),
Expand Down Expand Up @@ -2295,6 +2432,7 @@ fn lower_module_with_overrides(
instances.push(LoweredSvInstance {
module_name: instance.module_name().to_string(),
instance_name: instance.name().to_string(),
force_signals: Vec::new(),
parameter_overrides: lower_parameter_overrides(
instance,
&constants,
Expand Down Expand Up @@ -3166,6 +3304,7 @@ fn lower_initial_processes(
source: None,
// Control slots are not signals of the design.
hidden: true,
force_control: None,
},
);
id
Expand Down Expand Up @@ -3497,6 +3636,7 @@ fn ensure_parent_output_signals(
type_kind: PortTypeKind::Logic,
source: None,
hidden: false,
force_control: None,
};
parent
.variables
Expand Down
Loading
Loading