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 @@ -819,8 +819,8 @@ chapter-21/21.6--test.sv 2state pass
chapter-21/21.6--test.sv 4state pass
chapter-21/21.6--value.sv 2state pass
chapter-21/21.6--value.sv 4state pass
chapter-21/21.7--dumpfile.sv 2state fail
chapter-21/21.7--dumpfile.sv 4state fail
chapter-21/21.7--dumpfile.sv 2state pass
chapter-21/21.7--dumpfile.sv 4state pass
chapter-21/21.7--dumpports.sv 2state fail
chapter-21/21.7--dumpports.sv 4state fail
chapter-22/22.10--celldefine-basic-1.sv 2state skip
Expand Down
80 changes: 80 additions & 0 deletions crates/celox-frontend-sv/src/lowering/ff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,82 @@ impl<'p, 'a> Ff<'p, 'a> {
self.random.clone().expect("a process RNG")
}

fn vcd_task(
&mut self,
operation: sv::ir::VcdTask,
args: &[sv::ir::SystemTaskArg<sv::ir::Expr>],
) -> Result<(), sv::AnalyzerError> {
use celox_design::{HostRequestKind, HostRequestSite, HostRequestValue};
use sv::ir::VcdTask;
if self.kernel.is_none() {
return Err(unsupported("VCD tasks require a timed process"));
}
let mut values = Vec::new();
let mut strings = Vec::new();
for (index, arg) in args.iter().enumerate() {
if operation == VcdTask::Vars
&& index == 0
&& matches!(arg, sv::ir::SystemTaskArg::Empty)
{
continue;
}
if operation == VcdTask::Vars && index > 0 {
let sv::ir::SystemTaskArg::Str(name) = arg else {
return Err(unsupported("$dumpvars module or variable selection"));
};
strings.push(name.clone());
continue;
}
let (reg, signed, is_string) = match arg {
sv::ir::SystemTaskArg::Expr(expr) => {
(self.eval(expr, None)?, self.expr_signed(expr), false)
}
sv::ir::SystemTaskArg::Str(text) if operation == VcdTask::File => {
let bytes = unescape(text).into_bytes();
let width = (bytes.len() * 8).max(8);
Comment thread
tignear marked this conversation as resolved.
Resolved
let value = bytes.iter().fold(BigUint::zero(), |value, byte| {
(value << 8u32) | BigUint::from(*byte)
});
let reg = self.b.alloc_bit(width, false);
self.b.emit(SIRInstruction::Imm(reg, SIRValue::new(value)));
(reg, false, true)
}
_ => {
return Err(unsupported(
"VCD argument must be an integral value or filename string",
));
Comment thread
tignear marked this conversation as resolved.
Resolved
}
};
let width = self.b.register(&reg).width();
let is_4state = matches!(self.b.register(&reg), RegisterType::Logic { .. });
let (id, _) = self.m.temp("vcd_argument", width, signed, is_4state);
self.store(id, SIROffset::Static(0), width, reg);
values.push(HostRequestValue {
id,
width,
signed,
is_string,
});
}
let local_site = self
.host_request_sites
.iter()
.filter(|site| site.kind == HostRequestKind::Vcd)
.count() as u32;
self.host_request_sites.push(HostRequestSite {
process: self.process,
kind: HostRequestKind::Vcd,
requires_timed_simulation: false,
operation: operation as u32,
args: values,
results: Vec::new(),
strings,
});
self.with_kernel(|kernel| kernel.host_request(HostRequestKind::Vcd, local_site))
.map_err(|error| unsupported(error.to_string()))?;
Ok(())
}

/// The seed of this process's `$random` calls without a seed argument.
/// It starts at 0, as the seed variable of `$random(seed)` typically
/// does, so a design whose one process calls `$random` gets the same
Expand Down Expand Up @@ -2408,6 +2484,10 @@ impl<'p, 'a> Ff<'p, 'a> {
args: &[sv::ir::SystemTaskArg<sv::ir::Expr>],
site: Option<&sv::ir::SourceSpan>,
) -> Result<bool, sv::AnalyzerError> {
if let sv::ir::SystemTask::Vcd(operation) = task {
self.vcd_task(operation, args)?;
return Ok(true);
}
if matches!(
task,
sv::ir::SystemTask::Fclose
Expand Down
3 changes: 2 additions & 1 deletion crates/celox-frontend-sv/src/lowering/procedural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,8 @@ pub(super) fn system_task_kind(task: sv::ir::SystemTask) -> Option<SystemTaskKin
| SystemTask::Fdisplay(_)
| SystemTask::Fwrite(_)
| SystemTask::Printtimescale
| SystemTask::Exit => return None,
| SystemTask::Exit
| SystemTask::Vcd(_) => return None,
})
}

Expand Down
19 changes: 19 additions & 0 deletions crates/celox-runtime/src/simulation/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ pub trait HostBoundary: UnitExecutor {
/// change its state.
fn freeze(&mut self) {}

/// Publish buffered diagnostic output without ending or freezing execution.
fn flush_output(&mut self) -> Result<(), SimulatorErrorCode> {
Ok(())
}

/// Freeze after a design-requested finish, reporting output failures.
fn try_freeze(&mut self) -> Result<(), SimulatorErrorCode> {
let output = self.flush_output();
self.freeze();
output
}

/// Snapshot external-component inputs immediately before an event domain
/// evaluates its sequential logic.
fn stage_external_event(
Expand All @@ -78,6 +90,13 @@ pub trait HostBoundary: UnitExecutor {

/// Called after the state for a simulation timestamp has stabilized.
fn finish_timed_step(&mut self, _timestamp: u64) {}

/// Fallible timestamp boundary. The default preserves the original
/// infallible hook for existing executor implementations.
fn try_finish_timed_step(&mut self, timestamp: u64) -> Result<(), Halt> {
self.finish_timed_step(timestamp);
Ok(())
}
}

/// Backend execution hooks needed by the timed simulation engine: its
Expand Down
66 changes: 59 additions & 7 deletions crates/celox-runtime/src/simulation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,64 @@ impl<B: SimBackend> SimulationState<B> {
match self.step_time(executor, limit) {
Ok(time) => Ok(time),
Err(Halt::Finish) => {
// The waveform records the state at the finish.
executor.finish_timed_step(self.scheduler.time);
self.finish(executor);
self.finalize_terminal(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)) => {
// Preserve the primary runtime failure even if diagnostic
// waveform output also fails. This does not resume processes.
let _ = self.finish_timed_boundary(executor, self.scheduler.time);
let _ = executor.flush_output();
Err(error)
}
}
}

/// Complete a boundary reached from an entry point that returns runtime
/// errors rather than scheduler control halts.
fn finish_timed_boundary<E>(
&mut self,
executor: &mut E,
time: u64,
) -> Result<(), SimulatorErrorCode>
where
E: SimulationExecutor<Backend = B>,
{
match executor.try_finish_timed_step(time) {
Ok(()) => Ok(()),
Err(Halt::Error(error)) => Err(error),
Err(Halt::Finish) => {
self.finish_checked(executor)?;
Ok(())
}
Err(Halt::Stop) => {
self.stopped = true;
Ok(())
}
}
}

fn finish_checked<E>(&mut self, executor: &mut E) -> Result<(), SimulatorErrorCode>
where
E: SimulationExecutor<Backend = B>,
{
self.finished = true;
executor.try_freeze()
}

/// Once $finish is consumed, output errors must not make execution resumable.
pub fn finalize_terminal<E>(&mut self, executor: &mut E) -> Result<(), SimulatorErrorCode>
where
E: SimulationExecutor<Backend = B>,
{
self.finished = true;
let output = self.finish_timed_boundary(executor, self.scheduler.time);
let frozen = executor.try_freeze();
output.and(frozen)
}

/// End the simulation where it is.
pub fn finish<E>(&mut self, executor: &mut E)
where
Expand Down Expand Up @@ -241,16 +288,21 @@ impl<B: SimBackend> SimulationState<B> {
self.slot_passes = 0;
match self.step_round(executor, time, Vec::new(), Vec::new(), true) {
Ok(()) => {
executor.finish_timed_step(time);
self.finish_timed_boundary(executor, time)?;
Ok(Some(time))
}
Err(Halt::Finish) => {
executor.finish_timed_step(time);
self.finish(executor);
self.finalize_terminal(executor)?;
Ok(Some(self.scheduler.time))
}
Err(Halt::Stop) => Ok(Some(self.scheduler.time)),
Err(Halt::Error(error)) => Err(error),
Err(Halt::Error(error)) => {
// Preserve the primary runtime failure even if diagnostic
// waveform output also fails. This does not resume processes.
let _ = self.finish_timed_boundary(executor, self.scheduler.time);
let _ = executor.flush_output();
Err(error)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/celox-runtime/src/simulation/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl<B: SimBackend> SimulationState<B> {
self.charge_pass()?;
self.step_round(executor, current_time, Vec::new(), ready, false)?;
}
executor.finish_timed_step(current_time);
executor.try_finish_timed_step(current_time)?;
Ok(())
}

Expand Down
Loading
Loading