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
4 changes: 2 additions & 2 deletions conformance/sv-tests/expected.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,9 @@ chapter-20/20.15--dist_poisson.sv fail
chapter-20/20.15--dist_t.sv fail
chapter-20/20.15--dist_uniform.sv fail
chapter-20/20.15--random.sv pass
chapter-20/20.2--exit.sv fail
chapter-20/20.2--exit.sv pass
chapter-20/20.2--finish.sv pass
chapter-20/20.2--stop.sv fail
chapter-20/20.2--stop.sv pass
chapter-20/20.3--realtime.sv fail
chapter-20/20.3--stime.sv fail
chapter-20/20.3--time.sv fail
Expand Down
8 changes: 8 additions & 0 deletions crates/celox-design/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ pub enum RuntimeEventKind {
AssertFatal,
/// `$finish` executed by the design (IEEE 1800-2023 20.2).
Finish,
/// `$stop` executed by the design (IEEE 1800-2023 20.2).
Stop,
}

/// How a runtime event site sizes its formatted arguments.
Expand Down Expand Up @@ -244,6 +246,10 @@ pub enum ProcessStatus {
/// The process returned so the runtime can charge its time-slot budget.
/// It resumes at once.
Yield,
/// The process suspended the simulation (`$stop`, IEEE 1800-2023 20.2).
/// It resumes at the point stored in [`ProcessSlots::resume`] when the
/// simulation continues.
Stop,
}

impl ProcessStatus {
Expand All @@ -259,6 +265,7 @@ impl ProcessStatus {
Self::Pending => 6,
Self::Nba => 7,
Self::Yield => 8,
Self::Stop => 9,
}
}

Expand All @@ -272,6 +279,7 @@ impl ProcessStatus {
6 => Some(Self::Pending),
7 => Some(Self::Nba),
8 => Some(Self::Yield),
9 => Some(Self::Stop),
_ => None,
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/celox-frontend-core/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ impl ProcessKernelBuilder {
self.suspend(ProcessStatus::Yield)
}

/// Suspend the simulation (`$stop`), and continue at a new resume point
/// when it continues.
pub fn stop(&mut self) -> Result<(), ProcessKernelError> {
self.suspend(ProcessStatus::Stop)
}

/// Suspend at wait site `site` of this process. The open block
/// afterwards is the site's resume point: the caller lowers the wait's
/// condition there and ends it with [`Self::wake_if`].
Expand Down
10 changes: 8 additions & 2 deletions crates/celox-frontend-core/src/symbolic/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2976,7 +2976,10 @@ fn build_comb_observer_capture_paths(
};
let emit_on_true = matches!(
sites[observer.site_id as usize].kind,
RuntimeEventKind::Display | RuntimeEventKind::Write | RuntimeEventKind::Finish
RuntimeEventKind::Display
| RuntimeEventKind::Write
| RuntimeEventKind::Finish
| RuntimeEventKind::Stop
);
let fatal_error_code = matches!(
sites[observer.site_id as usize].kind,
Expand Down Expand Up @@ -3029,7 +3032,10 @@ fn build_comb_observer_capture_paths(
let member = &observers[member_idx];
let member_emit_on_true = matches!(
sites[member.site_id as usize].kind,
RuntimeEventKind::Display | RuntimeEventKind::Write | RuntimeEventKind::Finish
RuntimeEventKind::Display
| RuntimeEventKind::Write
| RuntimeEventKind::Finish
| RuntimeEventKind::Stop
);
let member_fatal_error_code = matches!(
sites[member.site_id as usize].kind,
Expand Down
17 changes: 13 additions & 4 deletions crates/celox-frontend-sv/src/lowering/comb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,12 @@ impl<'p, 'a> Comb<'p, 'a> {
self.readmem(&mut store, frames, *format, args)?;
Ok(store)
}
// IEEE 1800-2023 24.7: `$exit` outside a program block is
// ignored.
sv::ir::Stmt::SystemTask {
task: sv::ir::SystemTask::Exit,
..
} => Ok(store),
sv::ir::Stmt::SystemTask { task, .. } if self.initial => Err(unsupported(format!(
"system task `{task}` inside an initial block"
))),
Expand Down Expand Up @@ -3152,7 +3158,7 @@ impl<'p, 'a> Comb<'p, 'a> {
}
let event_kind = match &kind {
SystemTaskKind::Print(kind, _) => *kind,
SystemTaskKind::Finish => RuntimeEventKind::Finish,
SystemTaskKind::Control(kind) => *kind,
SystemTaskKind::Message => RuntimeEventKind::AssertContinue,
SystemTaskKind::Fatal => RuntimeEventKind::AssertFatal,
SystemTaskKind::Assertion(kind) => *kind,
Expand All @@ -3169,8 +3175,8 @@ impl<'p, 'a> Comb<'p, 'a> {
})
} else {
match (&kind, condition) {
(SystemTaskKind::Print(..) | SystemTaskKind::Finish, None) => None,
(SystemTaskKind::Print(..) | SystemTaskKind::Finish, Some((node, sources))) => {
(SystemTaskKind::Print(..) | SystemTaskKind::Control(_), None) => None,
(SystemTaskKind::Print(..) | SystemTaskKind::Control(_), Some((node, sources))) => {
observed.extend(sources);
Some(node)
}
Expand Down Expand Up @@ -3199,7 +3205,10 @@ impl<'p, 'a> Comb<'p, 'a> {
effects.push(SLTForEffect::Event {
site_id,
guard,
emit_on_true: matches!(kind, SystemTaskKind::Print(..) | SystemTaskKind::Finish),
emit_on_true: matches!(
kind,
SystemTaskKind::Print(..) | SystemTaskKind::Control(_)
),
args: captured.clone(),
fatal_error_code: kind.is_fatal().then_some(i64::from(site_id)),
});
Expand Down
23 changes: 18 additions & 5 deletions crates/celox-frontend-sv/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1974,13 +1974,26 @@ impl<'p, 'a> Ff<'p, 'a> {
}
return Ok(true);
}
// IEEE 1800-2023 24.7: `$exit` outside a program block is ignored.
if task == sv::ir::SystemTask::Exit {
return Ok(true);
}
let kind =
system_task_kind(task).ok_or_else(|| unsupported(format!("system task `{task}`")))?;
// A process ends the simulation itself; the rest of its body does not
// run.
if matches!(kind, SystemTaskKind::Finish) && self.kernel.is_some() {
self.with_kernel(ProcessKernelBuilder::finish);
return Ok(false);
// run. A stop suspends the process with the simulation, which then
// continues after the call.
match kind {
SystemTaskKind::Control(RuntimeEventKind::Finish) if self.kernel.is_some() => {
self.with_kernel(ProcessKernelBuilder::finish);
return Ok(false);
}
SystemTaskKind::Control(RuntimeEventKind::Stop) if self.kernel.is_some() => {
self.with_kernel(ProcessKernelBuilder::stop)
.map_err(|error| unsupported(error.to_string()))?;
return Ok(true);
}
_ => {}
}
let assertion = if matches!(kind, SystemTaskKind::Assertion(_)) {
let Some(sv::ir::SystemTaskArg::Expr(condition)) = args.first() else {
Expand Down Expand Up @@ -2042,7 +2055,7 @@ impl<'p, 'a> Ff<'p, 'a> {
};
let event_kind = match &kind {
SystemTaskKind::Print(kind, _) => *kind,
SystemTaskKind::Finish => RuntimeEventKind::Finish,
SystemTaskKind::Control(kind) => *kind,
SystemTaskKind::Message => RuntimeEventKind::AssertContinue,
SystemTaskKind::Fatal => RuntimeEventKind::AssertFatal,
SystemTaskKind::Assertion(kind) => *kind,
Expand Down
15 changes: 8 additions & 7 deletions crates/celox-frontend-sv/src/lowering/procedural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,8 +790,8 @@ pub(super) enum SystemTaskKind {
/// `$display` and `$write`, with the radix of their `b`, `o` and `h`
/// variants (`d` otherwise).
Print(RuntimeEventKind, char),
/// `$finish`.
Finish,
/// `$finish` and `$stop`, with their event kind.
Control(RuntimeEventKind),
/// `$error`, `$warning`, `$info`: a message, then execution continues.
Message,
/// `$fatal`: a message, then the simulation ends with an error.
Expand All @@ -811,21 +811,22 @@ impl SystemTaskKind {
}

/// How a task reports, for the tasks that report: `$readmemh`/`$readmemb`
/// load memory instead, and the analyzer rewrites `$printtimescale` into a
/// `$display`.
/// load memory instead, the analyzer rewrites `$printtimescale` into a
/// `$display`, and `$exit` outside a program block does nothing.
pub(super) fn system_task_kind(task: sv::ir::SystemTask) -> Option<SystemTaskKind> {
use sv::ir::SystemTask;
Some(match task {
SystemTask::Display(radix) => {
SystemTaskKind::Print(RuntimeEventKind::Display, radix.letter())
}
SystemTask::Write(radix) => SystemTaskKind::Print(RuntimeEventKind::Write, radix.letter()),
SystemTask::Finish => SystemTaskKind::Finish,
SystemTask::Finish => SystemTaskKind::Control(RuntimeEventKind::Finish),
SystemTask::Stop => SystemTaskKind::Control(RuntimeEventKind::Stop),
SystemTask::Severity(_) => SystemTaskKind::Message,
SystemTask::Fatal => SystemTaskKind::Fatal,
SystemTask::Assert => SystemTaskKind::Assertion(RuntimeEventKind::AssertFatal),
SystemTask::AssertContinue => SystemTaskKind::Assertion(RuntimeEventKind::AssertContinue),
SystemTask::Readmem(_) | SystemTask::Printtimescale => return None,
SystemTask::Readmem(_) | SystemTask::Printtimescale | SystemTask::Exit => return None,
})
}

Expand Down Expand Up @@ -863,7 +864,7 @@ pub(super) fn system_task_template(
&args[1..]
}
SystemTaskKind::Assertion(_) => &args[1..],
SystemTaskKind::Finish => &[][..],
SystemTaskKind::Control(_) => &[][..],
_ => args,
};
let (mut template, values) = match args.first() {
Expand Down
17 changes: 13 additions & 4 deletions crates/celox-frontend-veryl/src/lowering/logic_tree/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ fn collect_system_function_effect(
observed_inputs.extend(collector.active_guard_sources.iter().copied());
let guard = match (kind, collector.active_guard, explicit_guard) {
(
RuntimeEventKind::Display | RuntimeEventKind::Write | RuntimeEventKind::Finish,
RuntimeEventKind::Display
| RuntimeEventKind::Write
| RuntimeEventKind::Finish
| RuntimeEventKind::Stop,
active,
None,
) => active,
Expand All @@ -227,11 +230,14 @@ fn collect_system_function_effect(
Some(active)
}
(
RuntimeEventKind::Display | RuntimeEventKind::Write | RuntimeEventKind::Finish,
RuntimeEventKind::Display
| RuntimeEventKind::Write
| RuntimeEventKind::Finish
| RuntimeEventKind::Stop,
_,
Some(_),
) => {
unreachable!("display/write/finish has no explicit guard")
unreachable!("display/write/finish/stop has no explicit guard")
}
}
.map(|node| -> Result<NodeId, ParserError> {
Expand All @@ -249,7 +255,10 @@ fn collect_system_function_effect(
guard,
emit_on_true: matches!(
kind,
RuntimeEventKind::Display | RuntimeEventKind::Write | RuntimeEventKind::Finish
RuntimeEventKind::Display
| RuntimeEventKind::Write
| RuntimeEventKind::Finish
| RuntimeEventKind::Stop
),
args: observer_args.clone(),
fatal_error_code: matches!(kind, RuntimeEventKind::AssertFatal)
Expand Down
16 changes: 14 additions & 2 deletions crates/celox-napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -957,8 +957,9 @@ impl HandleBackend {
)
.map_or_else(
|halt| match halt {
// A bare backend reports no `$finish`; settle next time.
celox::Halt::Finish => Ok(false),
// A bare backend reports no `$finish` or `$stop`; settle
// next time.
celox::Halt::Finish | celox::Halt::Stop => Ok(false),
celox::Halt::Error(error) => Err(error),
},
|outcome| {
Expand Down Expand Up @@ -2097,6 +2098,17 @@ impl NativeSimulationHandle {
Ok(sim.is_finished())
}

/// Returns whether the last step or run returned early because a process
/// of the design executed `$stop`. The next step continues it.
#[napi]
pub fn is_stopped(&self) -> Result<bool> {
let sim = self
.sim
.as_ref()
.ok_or_else(|| Error::from_reason("Simulation has been disposed"))?;
Ok(sim.is_stopped())
}

/// Evaluate combinational logic.
#[napi]
pub fn eval_comb(&mut self) -> Result<()> {
Expand Down
6 changes: 3 additions & 3 deletions crates/celox-runtime/src/derived_tick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,9 @@ impl<E: EventHandle> DerivedTickPlan<E> {
}
};
match result {
// An RTL `$finish` ends only a timed simulation, which does not
// tick; a tick leaves it to the host.
Ok(_) | Err(Halt::Finish) => Ok(()),
// An RTL `$finish` or `$stop` ends or suspends only a timed
// simulation, which does not tick; a tick leaves it to the host.
Ok(_) | Err(Halt::Finish | Halt::Stop) => Ok(()),
Err(Halt::Error(error)) => Err(error),
}
}
Expand Down
33 changes: 32 additions & 1 deletion crates/celox-runtime/src/simulation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
SimulatorErrorCode,
backend::SimBackend,
schedule::ResolvedSchedule,
scheduler::Scheduler,
scheduler::{Scheduler, SimEvent},
timing::{Halt, ProcessState},
};

Expand Down Expand Up @@ -73,6 +73,13 @@ impl<B: SimBackend> std::fmt::Debug for EventInfo<B> {
}
}

/// A round suspended by `$stop` in its process phase: the round and the
/// scheduled events it processes, whose periodic clocks re-arm at its end.
struct Suspended<B: SimBackend> {
round: Box<Round>,
events: Vec<SimEvent<B>>,
}

/// Backend-independent state and execution rules for timed simulation.
pub struct SimulationState<B: SimBackend> {
scheduler: Scheduler<B>,
Expand All @@ -86,6 +93,10 @@ pub struct SimulationState<B: SimBackend> {
host_pending: bool,
/// A process requested the end of the simulation.
finished: bool,
/// A process suspended the simulation (`$stop`) in the last step.
stopped: bool,
/// The round a `$stop` suspended, which the next step continues.
suspended: Option<Suspended<B>>,
/// Buffers of the round in progress, kept between rounds.
scratch: Option<Box<Round>>,
/// Rounds and process-phase passes one time slot may run, and kernel
Expand Down Expand Up @@ -135,6 +146,8 @@ impl<B: SimBackend> SimulationState<B> {
process_state,
host_pending: false,
finished: false,
stopped: false,
suspended: None,
scratch: None,
round_budget: SLOT_ROUND_BUDGET,
return_budget: SLOT_RETURN_BUDGET,
Expand Down Expand Up @@ -164,6 +177,7 @@ impl<B: SimBackend> SimulationState<B> {
where
E: SimulationExecutor<Backend = B>,
{
self.stopped = false;
match self.step_time(executor, limit) {
Ok(time) => Ok(time),
Err(Halt::Finish) => {
Expand All @@ -172,6 +186,8 @@ impl<B: SimBackend> SimulationState<B> {
self.finish(executor);
Ok(Some(self.scheduler.time))
}
// The time slot goes on when the simulation continues.
Err(Halt::Stop) => Ok(Some(self.scheduler.time)),
Err(Halt::Error(error)) => Err(error),
}
}
Expand All @@ -190,6 +206,20 @@ impl<B: SimBackend> SimulationState<B> {
self.finished
}

/// Whether the last step returned because a process suspended the
/// simulation with `$stop` (IEEE 1800-2023 20.2). The simulation is
/// suspended in the middle of a time slot, and the next step continues it
/// after the `$stop`.
pub fn is_stopped(&self) -> bool {
self.stopped
}

/// Whether the simulation is suspended in the middle of a time slot, so
/// that its scheduling state cannot be captured.
pub fn is_suspended(&self) -> bool {
self.suspended.is_some()
}

/// Settle externally driven state against the previous edge baseline even
/// when no clock/reset signal is explicitly scheduled at this timestamp.
pub fn settle_at<E>(
Expand All @@ -211,6 +241,7 @@ impl<B: SimBackend> SimulationState<B> {
self.finish(executor);
Ok(Some(self.scheduler.time))
}
Err(Halt::Stop) => Ok(Some(self.scheduler.time)),
Err(Halt::Error(error)) => Err(error),
}
}
Expand Down
Loading
Loading