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
6 changes: 6 additions & 0 deletions crates/celox-design/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ pub struct RuntimeEventSite {
/// deferred assertions.
#[serde(default)]
pub deferred: Option<DeferredSite>,
/// The last argument is the simulation time at which the site issued
/// the event, which a report carries (IEEE 1800-2023 20.10). It is not
/// formatted.
#[serde(default)]
pub timestamp_arg: bool,
pub arg_widths: Vec<usize>,
pub arg_signed: Vec<bool>,
pub arg_is_string: Vec<bool>,
Expand Down Expand Up @@ -2226,6 +2231,7 @@ mod tests {
location: None,
assertion: None,
deferred: None,
timestamp_arg: false,
arg_widths: Vec::new(),
arg_signed: Vec::new(),
arg_is_string: Vec::new(),
Expand Down
14 changes: 14 additions & 0 deletions crates/celox-frontend-sv/src/lowering/comb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3578,6 +3578,19 @@ impl<'p, 'a> Comb<'p, 'a> {
arg_widths.push(self.width(node));
captured.push(self.capture(node)?);
}
// A report carries the time it is issued at (IEEE 1800-2023 20.10).
// The time does not run the process.
let timestamp_arg = matches!(kind, SystemTaskKind::Message(_) | SystemTaskKind::Fatal);
if timestamp_arg {
let time = sv::ir::Expr::Ident(self.m.time_slot());
let (node, _) = self.eval(store, frames, &time, None)?;
arg_signed.push(false);
arg_is_string.push(false);
arg_float_precision.push(None);
arg_is_handle.push(false);
arg_widths.push(self.width(node));
captured.push(self.capture(node)?);
}
let event_kind = kind.event_kind();
let condition = self.path_condition()?;
let guard = if let Some(passed) = assertion {
Expand Down Expand Up @@ -3649,6 +3662,7 @@ impl<'p, 'a> Comb<'p, 'a> {
location: report_location(site),
assertion: assertion_site.cloned(),
deferred,
timestamp_arg,
arg_widths,
arg_signed,
arg_is_string,
Expand Down
14 changes: 14 additions & 0 deletions crates/celox-frontend-sv/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ impl<'p, 'a> Ff<'p, 'a> {
location: None,
assertion: None,
deferred: Some(DeferredSite::FlushPoint),
timestamp_arg: false,
arg_widths: Vec::new(),
arg_signed: Vec::new(),
arg_is_string: Vec::new(),
Expand Down Expand Up @@ -3761,6 +3762,18 @@ impl<'p, 'a> Ff<'p, 'a> {
}
}
}
// A report carries the time it is issued at (IEEE 1800-2023 20.10).
let timestamp_arg = matches!(kind, SystemTaskKind::Message(_) | SystemTaskKind::Fatal);
if timestamp_arg {
let time = sv::ir::Expr::Ident(self.m.time_slot());
let reg = self.eval(&time, None)?;
arg_widths.push(self.b.register(&reg).width());
arg_signed.push(false);
arg_is_string.push(false);
arg_float_precision.push(None);
arg_is_handle.push(false);
regs.push(reg);
}
// Argument effects happen before branching, and the condition register
// retains the value it had before those effects.
let resume = match assertion {
Expand Down Expand Up @@ -3794,6 +3807,7 @@ impl<'p, 'a> Ff<'p, 'a> {
location: report_location(site),
assertion: assertion_site.cloned(),
deferred,
timestamp_arg,
arg_widths,
arg_signed,
arg_is_string,
Expand Down
1 change: 1 addition & 0 deletions crates/celox-frontend-veryl/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ impl<'a> FfParser<'a> {
location: None,
assertion: None,
deferred: None,
timestamp_arg: false,
arg_widths: value_args
.iter()
.map(|arg| self.get_expression_width(&arg.0))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ fn register_comb_runtime_event_site<'a>(
location: None,
assertion: None,
deferred: None,
timestamp_arg: false,
arg_widths: value_args
.iter()
.map(|arg| arg.0.comptime().r#type.total_width().unwrap_or(1))
Expand Down
1 change: 1 addition & 0 deletions crates/celox-frontend-veryl/src/testbench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ fn runtime_event_site_for_assert(
location: None,
assertion: None,
deferred: None,
timestamp_arg: false,
arg_widths: value_args.iter().map(assert_arg_width).collect(),
arg_signed: value_args
.iter()
Expand Down
8 changes: 8 additions & 0 deletions crates/celox-runtime/src/simulation/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ pub trait HostBoundary: UnitExecutor {
fn try_end_time_slot(&mut self, _timestamp: u64) -> Result<(), Halt> {
Ok(())
}

/// Called before a run or a step moves from `timestamp` to a later time:
/// before the step of a later slot, and at the end of a run to a time
/// without work. An error ends the run there, as an error of a step
/// does, and so does a `$finish` the host ran there.
fn try_leave_time(&mut self, _timestamp: u64) -> Result<(), Halt> {
Ok(())
}
}

/// How many time slots a compiled scheduler may run at once (see
Expand Down
43 changes: 42 additions & 1 deletion crates/celox-runtime/src/simulation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,10 @@ impl<B: SimBackend> SimulationState<B> {
Ok(Some(self.scheduler.time))
}
// The time slot goes on when the simulation continues.
Err(Halt::Stop) => Ok(Some(self.scheduler.time)),
Err(Halt::Stop) => {
self.stopped = true;
Ok(Some(self.scheduler.time))
}
Err(Halt::Error(error)) => {
if matches!(&error, SimulatorErrorCode::Fatal { .. }) {
let _ = self.finalize_terminal(executor);
Expand Down Expand Up @@ -342,6 +345,12 @@ impl<B: SimBackend> SimulationState<B> {
} else {
None
};
if compiled.is_some()
&& next_time > self.scheduler.time
&& !self.leave_time(executor)?
{
return Ok(false);
}
observer.before_step(self, executor);
let result = match compiled {
Some(limit) => self.run_compiled(executor, limit),
Expand All @@ -356,10 +365,42 @@ impl<B: SimBackend> SimulationState<B> {
if self.finished {
return Ok(false);
}
if end > self.scheduler.time && !self.leave_time(executor)? {
return Ok(false);
}
self.advance_time(executor.backend_mut(), end);
Ok(true)
}

/// The host's work before a run leaves the current time
/// ([`HostBoundary::try_leave_time`]). Returns `false` when it stopped
/// or finished the simulation.
fn leave_time<E>(&mut self, executor: &mut E) -> Result<bool, SimulatorErrorCode>
where
E: SimulationExecutor<Backend = B>,
{
match executor.try_leave_time(self.scheduler.time) {
Ok(()) => Ok(true),
Err(Halt::Stop) => {
self.stopped = true;
Ok(false)
}
Err(Halt::Finish) => {
self.finalize_terminal(executor)?;
Ok(false)
}
Err(Halt::Error(error)) => {
if matches!(&error, SimulatorErrorCode::Fatal { .. }) {
let _ = self.finalize_terminal(executor);
return Err(error);
}
let _ = self.finish_timed_boundary(executor, self.scheduler.time);
let _ = executor.flush_output();
Err(error)
}
}
}

/// Select the loop that runs time slots.
pub fn set_scheduler_mode(&mut self, mode: SchedulerMode) {
self.stats.mode = mode;
Expand Down
3 changes: 3 additions & 0 deletions crates/celox-runtime/src/simulation/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ impl<B: SimBackend> SimulationState<B> {
if current_time > limit {
return Ok(settled);
}
if current_time > self.scheduler.time {
executor.try_leave_time(self.scheduler.time)?;
}
// Host writes alone: settle them, then go on to the next time.
let host_only = self.host_pending && self.next_work_time() != Some(current_time);
self.run_time(executor, current_time)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/celox/src/backend/native/image_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{fmt, path::Path};
use super::backend::NativeProgramImage;

const TRAILER_MAGIC: &[u8; 8] = b"CELOXNPI";
const CONTAINER_VERSION: u16 = 43;
const CONTAINER_VERSION: u16 = 44;
const TRAILER_SIZE: usize = 32;
const FNV_OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325;
const FNV_PRIME: u64 = 0x0000_0100_0000_01b3;
Expand Down
43 changes: 37 additions & 6 deletions crates/celox/src/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,17 @@ impl<B: SimBackend> HostBoundary for Simulator<B> {
self.dump_unless_rewound(timestamp);
}

/// Combinational logic still to settle belongs to `timestamp`, so its
/// reports read that time: settle it before the run moves on.
fn try_leave_time(&mut self, _timestamp: u64) -> Result<(), Halt> {
if self.timestamped_reports && self.dirty {
self.eval_comb_checked().map_err(Halt::Error)?;
self.dirty = false;
self.runtime_finish()?;
}
Ok(())
}

/// Deferred assertions mature in the Observed and Postponed regions of
/// the slot (IEEE 1800-2023 16.4).
fn try_end_time_slot(&mut self, _timestamp: u64) -> Result<(), Halt> {
Expand Down Expand Up @@ -748,7 +759,11 @@ impl<B: SimBackend> Simulation<B> {
return self.watched_step();
}
// With nothing scheduled, the step ends the slot too.
let was_stopped = self.is_stopped();
self.end_idle_slot(self.state.next_event_time().unwrap_or(u64::MAX))?;
if !was_stopped && self.is_stopped() {
return Ok(Some(self.time()));
}
self.step_until(u64::MAX)
}

Expand All @@ -759,7 +774,7 @@ impl<B: SimBackend> Simulation<B> {
/// run at that time.
#[inline]
fn end_idle_slot(&mut self, end_time: u64) -> Result<(), RuntimeErrorCode> {
if !self.simulator.host_slot_work
if (!self.simulator.host_slot_work && !self.simulator.timestamped_reports)
|| end_time <= self.state.time()
|| self.state.next_event_time() == Some(self.state.time())
{
Expand All @@ -774,7 +789,8 @@ impl<B: SimBackend> Simulation<B> {
let result = if self.simulator.dirty {
let result = self.simulator.eval_comb_checked();
self.simulator.dirty = false;
result
// A `$finish` of the settle finishes the simulation here.
result.and_then(|()| self.sync_finish_checked())
} else {
Ok(())
};
Expand All @@ -796,7 +812,7 @@ impl<B: SimBackend> Simulation<B> {
while let Some(next_time) = self.state.next_event_time() {
if next_time > self.state.time() {
self.watch_settle()?;
if self.reports.failure.is_some() {
if self.is_finished() || self.is_stopped() || self.reports.failure.is_some() {
return Ok(Some(self.state.time()));
}
}
Expand Down Expand Up @@ -844,7 +860,7 @@ impl<B: SimBackend> Simulation<B> {
}
if next_time > self.state.time() {
self.watch_settle()?;
if self.reports.failure.is_some() {
if self.is_finished() || self.is_stopped() || self.reports.failure.is_some() {
return Ok(false);
}
}
Expand All @@ -858,7 +874,7 @@ impl<B: SimBackend> Simulation<B> {
}
}
self.watch_settle()?;
Ok(self.reports.failure.is_none())
Ok(!self.is_stopped() && self.reports.failure.is_none())
}

/// Settle combinational logic that no step settled, as after host writes
Expand All @@ -877,7 +893,11 @@ impl<B: SimBackend> Simulation<B> {
return Ok(());
}
if self.simulator.dirty {
if let Err(error) = self.simulator.eval_comb_checked() {
if let Err(error) = self
.simulator
.eval_comb_checked()
.and_then(|()| self.sync_finish_checked())
{
self.finalize_fatal(&error);
self.flush_error_output();
return Err(error);
Expand Down Expand Up @@ -959,7 +979,11 @@ impl<B: SimBackend> Simulation<B> {
return Ok(());
}
} else {
let was_stopped = self.is_stopped();
self.end_idle_slot(end_time)?;
if !was_stopped && self.is_stopped() {
return Ok(());
}
}
let reached = match self.shadow.take() {
None => self.state.run_until(&mut self.simulator, end_time)?,
Expand Down Expand Up @@ -1008,6 +1032,9 @@ impl<B: SimBackend> Simulation<B> {
} else {
self.end_idle_slot(time)?;
}
if self.is_finished() || self.is_stopped() {
return Ok(());
}
self.state.advance_time(&mut self.simulator.backend, time);
if let Some(shadow) = &mut self.shadow {
shadow
Expand Down Expand Up @@ -1037,7 +1064,11 @@ impl<B: SimBackend> Simulation<B> {
self.run_until(end_time)?;
return Ok(!self.state.is_stopped());
}
let was_stopped = self.is_stopped();
self.end_idle_slot(end_time)?;
if !was_stopped && self.is_stopped() {
return Ok(false);
}
let mut steps = 0;
while let Some(next_time) = self.state.next_event_time() {
if next_time > end_time {
Expand Down
Loading
Loading