diff --git a/Cargo.lock b/Cargo.lock
index ae3aff84d83..47908e1d40d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -657,6 +657,7 @@ name = "celox-heap"
version = "0.11.0"
dependencies = [
"fxhash",
+ "serde",
]
[[package]]
diff --git a/crates/celox-design/src/lib.rs b/crates/celox-design/src/lib.rs
index 3f34f2b61fc..a85be9c3be6 100644
--- a/crates/celox-design/src/lib.rs
+++ b/crates/celox-design/src/lib.rs
@@ -1105,6 +1105,11 @@ pub struct RuntimeSchema {
/// most one per module instance. Before each round a timed scheduler
/// stores the current time into each, as [`TIME_SLOT_WIDTH`] bits.
pub time_slots: Vec,
+ /// Variables of `$sampled` operands and their copies, which hold the
+ /// value of the variable in the Preponed region (IEEE 1800-2023
+ /// 16.5.1): a host copies each variable into its copy when a time slot
+ /// is over, and once before the first.
+ pub sample_shadows: Vec<(A, A)>,
/// The state a compiled scheduler exchanges with the runtime, and the
/// scheduler itself once one is generated. Present when the design has
/// process kernels.
@@ -1359,6 +1364,7 @@ impl Default for RuntimeSchema {
host_request_sites: Vec::new(),
active_commits: Vec::new(),
time_slots: Vec::new(),
+ sample_shadows: Vec::new(),
scheduler: None,
derived_edges: Vec::new(),
settle_partitions: None,
diff --git a/crates/celox-design/src/schedule.rs b/crates/celox-design/src/schedule.rs
index bbe9a4a74fe..a8b28da4739 100644
--- a/crates/celox-design/src/schedule.rs
+++ b/crates/celox-design/src/schedule.rs
@@ -46,6 +46,9 @@ pub struct ScheduleModel {
/// Objects the scheduler stores the current time into
/// ([`RuntimeSchema::time_slots`]).
pub time_slots: Vec,
+ /// Sampled variables and their copies
+ /// ([`RuntimeSchema::sample_shadows`]).
+ pub sample_shadows: Vec<(A, A)>,
/// The compiled scheduler, if one was generated
/// ([`RuntimeSchema::scheduler`]).
pub scheduler: Option>,
@@ -152,6 +155,7 @@ impl ScheduleModel {
.any(|site| matches!(site.kind, RuntimeEventKind::Finish)),
active_commits: schema.active_commits.clone(),
time_slots: schema.time_slots.clone(),
+ sample_shadows: schema.sample_shadows.clone(),
scheduler: schema.scheduler.clone(),
}
}
diff --git a/crates/celox-frontend-core/src/sdk.rs b/crates/celox-frontend-core/src/sdk.rs
index 978966f9d2f..b17909e54cd 100644
--- a/crates/celox-frontend-core/src/sdk.rs
+++ b/crates/celox-frontend-core/src/sdk.rs
@@ -1972,6 +1972,7 @@ pub fn lower_frontend_artifact(
nba_switches: Vec::new(),
host_request_sites: Vec::new(),
time_slot: None,
+ sample_shadows: Vec::new(),
};
let symbolic = SymbolicRtl {
modules: [(module_id, sim_module.clone())].into_iter().collect(),
diff --git a/crates/celox-frontend-core/src/symbolic/artifact.rs b/crates/celox-frontend-core/src/symbolic/artifact.rs
index 13d9d614ced..6f6acef4356 100644
--- a/crates/celox-frontend-core/src/symbolic/artifact.rs
+++ b/crates/celox-frontend-core/src/symbolic/artifact.rs
@@ -159,6 +159,9 @@ pub struct SimModule {
/// The variable through which the module's code reads the simulation
/// time; see `RuntimeSchema::time_slots`.
pub time_slot: Option,
+ /// Variables and their sampled copies; see
+ /// `RuntimeSchema::sample_shadows`.
+ pub sample_shadows: Vec<(SourceVarId, SourceVarId)>,
}
/// A nonblocking site whose kernel has two paths, as
diff --git a/crates/celox-frontend-core/src/symbolic/assembly.rs b/crates/celox-frontend-core/src/symbolic/assembly.rs
index 288fda6029b..766715c6749 100644
--- a/crates/celox-frontend-core/src/symbolic/assembly.rs
+++ b/crates/celox-frontend-core/src/symbolic/assembly.rs
@@ -2135,6 +2135,7 @@ pub fn schedule_symbolic_rtl_reporting_loops(
nba_sites,
host_request_sites,
time_slots,
+ sample_shadows,
..
} = timing;
let source_sir = SirProgram {
@@ -2386,6 +2387,10 @@ pub fn schedule_symbolic_rtl_reporting_loops(
.collect();
let active_commits: Vec = active_commits.into_iter().map(project).collect();
let time_slots: Vec = time_slots.into_iter().map(project).collect();
+ let sample_shadows: Vec<(StateAddr, StateAddr)> = sample_shadows
+ .into_iter()
+ .map(|(variable, copy)| (project(variable), project(copy)))
+ .collect();
let root_instance = expanded[&InstancePath(Vec::new())];
let settle_partitions = settle_flags.map(|settle| {
let mut marks: HashMap> = HashMap::default();
@@ -2509,6 +2514,12 @@ pub fn schedule_symbolic_rtl_reporting_loops(
.flat_map(|(source, counter)| [source.id, *counter]),
);
}
+ // The host copies each sampled variable into its copy.
+ timing_read_roots.extend(
+ sample_shadows
+ .iter()
+ .flat_map(|&(variable, copy)| [variable, copy]),
+ );
// A package variable's state object maps back to the package's own
// variable, not to one of its aliases.
let state_to_source = source_to_state
@@ -2561,6 +2572,7 @@ pub fn schedule_symbolic_rtl_reporting_loops(
host_request_sites,
active_commits,
time_slots,
+ sample_shadows,
scheduler,
derived_edges: Vec::new(),
settle_partitions,
@@ -4113,6 +4125,7 @@ struct TimingMetadata {
nba_sites: Vec>,
host_request_sites: Vec>,
time_slots: Vec,
+ sample_shadows: Vec<(AbsoluteAddr, AbsoluteAddr)>,
nba_switches: Vec>,
}
@@ -4435,6 +4448,7 @@ fn relocate_timing(
nba_sites: Vec::new(),
host_request_sites: Vec::new(),
time_slots: Vec::new(),
+ sample_shadows: Vec::new(),
nba_switches: Vec::new(),
};
for (&instance, module) in instances {
@@ -4470,6 +4484,12 @@ fn relocate_timing(
}),
);
metadata.time_slots.extend(module.time_slot.map(locate));
+ metadata.sample_shadows.extend(
+ module
+ .sample_shadows
+ .iter()
+ .map(|&(variable, copy)| (locate(variable), locate(copy))),
+ );
metadata.nba_switches.extend(
module
.nba_switches
diff --git a/crates/celox-frontend-sv/src/lowering.rs b/crates/celox-frontend-sv/src/lowering.rs
index 763987247b7..98a11f435f2 100644
--- a/crates/celox-frontend-sv/src/lowering.rs
+++ b/crates/celox-frontend-sv/src/lowering.rs
@@ -87,6 +87,9 @@ struct SvVariable {
hidden: bool,
/// Compiler-generated external override slot: (target, is_enable).
force_control: Option<(SourceVarId, bool)>,
+ /// The variable whose sampled value this hidden copy holds
+ /// (`SimModule::sample_shadows`).
+ sampled_from: Option,
}
/// Whether `expr` reads a string variable: after its calls are hoisted, a
@@ -126,13 +129,14 @@ impl SvVariable {
}
/// A hidden variable other than the time slot, whose value the lowered
- /// code computes. The time slot is state the scheduler writes, and an
- /// event expression's variables are state its process and its
- /// continuous assignment share.
+ /// code computes. The time slot and the sampled copies are state the
+ /// scheduler writes, and an event expression's variables are state its
+ /// process and its continuous assignment share.
fn is_scratch(&self) -> bool {
self.hidden
&& self.path != [TIME_SLOT]
&& self.force_control.is_none()
+ && self.sampled_from.is_none()
&& !self.path[0].starts_with(EVENT_PREFIX)
}
}
@@ -2198,6 +2202,7 @@ fn prepare_external_hierarchy_collecting(
source: variable.source.clone(),
hidden: !variable.module_affiliated,
force_control: None,
+ sampled_from: None,
},
)
})
@@ -3629,6 +3634,7 @@ fn lower_module_with_overrides(
source: None,
hidden: false,
force_control: None,
+ sampled_from: None,
};
name_to_id.insert(port.name().to_string(), id);
port_order.push(id);
@@ -3713,6 +3719,7 @@ fn lower_module_with_overrides(
source: None,
hidden: false,
force_control: None,
+ sampled_from: None,
};
name_to_id.insert(signal.name().to_string(), id);
// The package's object, or the other instance's, has its own
@@ -3793,6 +3800,7 @@ fn lower_module_with_overrides(
source: None,
hidden: false,
force_control: None,
+ sampled_from: None,
},
);
}
@@ -4005,6 +4013,7 @@ fn lower_module_with_overrides(
nba_switches,
host_request_sites,
time_slot: name_to_id.get(TIME_SLOT).copied(),
+ sample_shadows: Vec::new(),
},
variables,
port_order,
@@ -4793,6 +4802,12 @@ pub(crate) fn attach_instance_glue(
}
// Combinational processes may read the time too.
module.time_slot = signal_names.get(TIME_SLOT).copied();
+ let mut sample_shadows: Vec<_> = parent_variables
+ .iter()
+ .filter_map(|(©, variable)| variable.sampled_from.map(|variable| (variable, copy)))
+ .collect();
+ sample_shadows.sort_by_key(|&(variable, copy)| (variable.0, copy.0));
+ module.sample_shadows = sample_shadows;
for (instance, child_id, child, connections) in resolved_instances {
let glue = build_instance_glue(
&parent_variables,
@@ -5187,6 +5202,7 @@ fn lower_initial_processes(
// Control slots are not signals of the design.
hidden: true,
force_control: None,
+ sampled_from: None,
},
);
id
@@ -5831,6 +5847,7 @@ fn ensure_parent_output_signals(
source: None,
hidden: false,
force_control: None,
+ sampled_from: None,
};
parent
.variables
@@ -7713,6 +7730,7 @@ fn lower_bit_vector_function_slt(
| SystemFunction::Isunbounded
| SystemFunction::Signed
| SystemFunction::Unsigned
+ | SystemFunction::Sampled
| SystemFunction::Bits
| SystemFunction::Size
| SystemFunction::Dimensions
@@ -9077,6 +9095,15 @@ fn runtime_select_as_shift(
}
fn expr_from_const_expr(expr: &sv::ir::ConstExpr) -> Option {
+ expr_from_const_expr_with_calls(expr, false)
+}
+
+/// Reconstruct a select operand, optionally retaining nested runtime calls.
+fn expr_from_const_expr_with_calls(
+ expr: &sv::ir::ConstExpr,
+ allow_calls: bool,
+) -> Option {
+ let convert = |expr| expr_from_const_expr_with_calls(expr, allow_calls);
Some(match expr {
sv::ir::ConstExpr::Literal(value) => sv::ir::Expr::Literal(value.clone()),
sv::ir::ConstExpr::Ident(name) => sv::ir::Expr::Ident(name.clone()),
@@ -9086,13 +9113,13 @@ fn expr_from_const_expr(expr: &sv::ir::ConstExpr) -> Option {
lsb,
signed,
} => sv::ir::Expr::Select {
- expr: Box::new(expr_from_const_expr(expr)?),
+ expr: Box::new(convert(expr)?),
msb: (**msb).clone(),
lsb: (**lsb).clone(),
signed: *signed,
},
sv::ir::ConstExpr::Select { expr, bit } => sv::ir::Expr::Select {
- expr: Box::new(expr_from_const_expr(expr)?),
+ expr: Box::new(convert(expr)?),
msb: (**bit).clone(),
lsb: (**bit).clone(),
signed: false,
@@ -9102,28 +9129,36 @@ fn expr_from_const_expr(expr: &sv::ir::ConstExpr) -> Option {
width,
signed,
} => sv::ir::Expr::Resize {
- expr: Box::new(expr_from_const_expr(expr)?),
+ expr: Box::new(convert(expr)?),
width: *width,
signed: *signed,
},
+ sv::ir::ConstExpr::Function { callee, args, .. } if allow_calls => sv::ir::Expr::Call {
+ callee: callee.clone(),
+ arg_order: Vec::new(),
+ args: args
+ .iter()
+ .map(|arg| convert(arg).map(Some))
+ .collect::