diff --git a/crates/celox-design/src/lib.rs b/crates/celox-design/src/lib.rs index 979efe9ce3e..a8e65014c28 100644 --- a/crates/celox-design/src/lib.rs +++ b/crates/celox-design/src/lib.rs @@ -542,6 +542,9 @@ pub enum HostRequestKind { Scan, Distribution, Process, + // Append new variants: native images persist Serde variant indices. + /// Services owned by an explicitly invoked testbench kernel. + Testbench, } impl HostRequestKind { @@ -554,6 +557,7 @@ impl HostRequestKind { Self::Scan => 4, Self::Distribution => 5, Self::Process => 6, + Self::Testbench => 7, }; (family << 32) | site as u64 } @@ -567,6 +571,7 @@ impl HostRequestKind { 4 => Self::Scan, 5 => Self::Distribution, 6 => Self::Process, + 7 => Self::Testbench, _ => return None, }; Some((family, request as u32)) @@ -707,6 +712,8 @@ pub enum ProcessKind { Regular, /// A final procedure runs only when simulation ends (IEEE 1800-2023 9.2.3). Final, + /// Invoked explicitly by its caller; never enqueued as an initial procedure. + Callable, } /// A pure expression evaluated with the storage of one waiting activation. @@ -2263,6 +2270,7 @@ mod tests { HostRequestKind::Scan, HostRequestKind::Distribution, HostRequestKind::Process, + HostRequestKind::Testbench, ] { for site in [0, 1, u32::MAX] { assert_eq!( @@ -2271,7 +2279,7 @@ mod tests { ); } } - for invalid in [0, u32::MAX as u64, 7u64 << 32, 1u64 << 63] { + for invalid in [0, u32::MAX as u64, 8u64 << 32, 1u64 << 63] { assert_eq!(HostRequestKind::decode(invalid), None); } assert_eq!( diff --git a/crates/celox-design/src/schedule.rs b/crates/celox-design/src/schedule.rs index 611a6215caf..7673f3d18f9 100644 --- a/crates/celox-design/src/schedule.rs +++ b/crates/celox-design/src/schedule.rs @@ -153,7 +153,9 @@ impl ScheduleModel { Self { events: design.events.clone(), kinds: design.event_kinds(), - timed: !processes.is_empty(), + timed: processes + .iter() + .any(|process| process.slots.kind != crate::ProcessKind::Callable), counted, probes: schema.occurrence_probes.clone(), processes, diff --git a/crates/celox-frontend-core/src/process.rs b/crates/celox-frontend-core/src/process.rs index c3d907872be..980d1953c86 100644 --- a/crates/celox-frontend-core/src/process.rs +++ b/crates/celox-frontend-core/src/process.rs @@ -32,7 +32,7 @@ use thiserror::Error; use crate::SourceVarId; -type RegionedSourceAddr = RegionedVarAddrBase; +type RegionedSourceAddr = RegionedVarAddrBase; /// Widest selector of a single `Switch`. const SWITCH_BITS: usize = 8; @@ -55,9 +55,9 @@ pub enum ProcessKernelError { pub const NBA_POSITION_WIDTH: usize = 64; /// Builder of one process kernel. -pub struct ProcessKernelBuilder { - builder: SIRBuilder, - slots: ProcessSlots, +pub struct ProcessKernelBuilder { + builder: SIRBuilder>, + slots: ProcessSlots, /// The block each resume point continues in; point 0 is the start. resume_blocks: Vec, /// The wait site and predicate entry resume point being lowered. @@ -67,10 +67,10 @@ pub struct ProcessKernelBuilder { flush_site: Option, } -impl ProcessKernelBuilder { +impl ProcessKernelBuilder { /// Start a kernel. The builder is positioned at the start of the /// process body. - pub fn new(slots: ProcessSlots) -> Self { + pub fn new(slots: ProcessSlots) -> Self { let builder = SIRBuilder::new(); let start = builder.current_block(); Self { @@ -99,24 +99,24 @@ impl ProcessKernelBuilder { /// The kernel's control slots, with [`ProcessSlots::const_delays`] set /// for the delays lowered so far. - pub fn slots(&self) -> ProcessSlots { + pub fn slots(&self) -> ProcessSlots { self.slots } /// The SIR builder positioned where the process continues. After /// [`Self::finish`] no block is open until the caller switches to one. - pub fn builder(&mut self) -> &mut SIRBuilder { + pub fn builder(&mut self) -> &mut SIRBuilder> { &mut self.builder } - fn store_constant(&mut self, slot: SourceVarId, width: usize, value: u64) { + fn store_constant(&mut self, slot: A, width: usize, value: u64) { let register = self.builder.alloc_bit(width, false); self.builder .emit(SIRInstruction::Imm(register, SIRValue::new(value))); self.store(slot, width, register); } - fn store(&mut self, slot: SourceVarId, width: usize, value: RegisterId) { + fn store(&mut self, slot: A, width: usize, value: RegisterId) { self.builder.emit(SIRInstruction::Store( stable(slot), SIROffset::Static(0), @@ -343,7 +343,7 @@ impl ProcessKernelBuilder { pub fn wait_level( &mut self, site: u32, - mut predicate: impl FnMut(&mut SIRBuilder) -> RegisterId, + mut predicate: impl FnMut(&mut SIRBuilder>) -> RegisterId, ) -> Result<(), ProcessKernelError> { let holds = predicate(&mut self.builder); let join = self.builder.new_block(); @@ -379,9 +379,9 @@ impl ProcessKernelBuilder { pub fn nonblocking>( &mut self, index: u32, - site: &NbaSite, + site: &NbaSite, lower: impl FnOnce( - &mut SIRBuilder, + &mut SIRBuilder>, ) -> Result<(RegisterId, Option>), E>, ) -> Result<(), E> { if let Some(pending) = site.pending { @@ -432,10 +432,10 @@ impl ProcessKernelBuilder { pub fn nonblocking_switched>( &mut self, index: u32, - site: &NbaSite, - switch: SourceVarId, + site: &NbaSite, + switch: A, lower: impl FnOnce( - &mut SIRBuilder, + &mut SIRBuilder>, ) -> Result<(RegisterId, Option>), E>, ) -> Result<(), E> { assert!( @@ -489,7 +489,7 @@ impl ProcessKernelBuilder { } /// Complete the kernel. If a block is still open, the process ends there. - pub fn build(mut self) -> ExecutionUnit { + pub fn build(mut self) -> ExecutionUnit> { if self.builder.has_open_block() { self.end(ProcessStatus::Done); } @@ -513,7 +513,7 @@ impl ProcessKernelBuilder { .chunks(1 << SWITCH_BITS) .map(<[BlockId]>::to_vec) .collect::>(); - let byte = |builder: &mut SIRBuilder, lsb: usize| { + let byte = |builder: &mut SIRBuilder>, lsb: usize| { let byte = builder.alloc_bit(SWITCH_BITS, false); builder.emit(SIRInstruction::Slice(byte, resume, lsb, SWITCH_BITS)); byte @@ -565,8 +565,8 @@ impl ProcessKernelBuilder { } } -fn stable(var_id: SourceVarId) -> RegionedSourceAddr { - RegionedSourceAddr { +fn stable(var_id: A) -> RegionedSourceAddr { + RegionedVarAddrBase { region: STABLE_REGION, var_id, } diff --git a/crates/celox-runtime/src/simulation/mod.rs b/crates/celox-runtime/src/simulation/mod.rs index caf433462af..17198395437 100644 --- a/crates/celox-runtime/src/simulation/mod.rs +++ b/crates/celox-runtime/src/simulation/mod.rs @@ -211,7 +211,12 @@ impl SimulationState { last_clock_unknown: BitSet::new(), four_state: backend.layout().four_state, process_wakeups: (0..model.processes.len()) - .filter(|&index| model.processes[index].kind != celox_design::ProcessKind::Final) + .filter(|&index| { + matches!( + model.processes[index].kind, + celox_design::ProcessKind::Regular | celox_design::ProcessKind::Initializer + ) + }) .map(|process| Reverse((0, process))) .collect(), kernel_count: model.processes.len(), diff --git a/crates/celox/src/backend/native/backend.rs b/crates/celox/src/backend/native/backend.rs index ae3d4b70502..47c377bc29a 100644 --- a/crates/celox/src/backend/native/backend.rs +++ b/crates/celox/src/backend/native/backend.rs @@ -564,7 +564,10 @@ impl NativeProgramImage { /// Whether the design has process kernels, which only a timed /// [`crate::Simulation`] runs. pub fn has_processes(&self) -> bool { - !self.runtime_schema.processes.is_empty() + self.runtime_schema + .processes + .iter() + .any(|process| process.kind != celox_design::ProcessKind::Callable) } /// The compiler-inserted override slots of a reflected signal, as diff --git a/crates/celox/src/backend/native/image_file.rs b/crates/celox/src/backend/native/image_file.rs index a06226bb4e1..c4f37b4f026 100644 --- a/crates/celox/src/backend/native/image_file.rs +++ b/crates/celox/src/backend/native/image_file.rs @@ -377,6 +377,33 @@ mod tests { .program_image() .clone() } + #[test] + fn host_request_variants_preserve_existing_image_encoding() { + use celox_design::HostRequestKind; + + // These bytes are the enum indices stored in pre-Callable images. + // Round trips alone would miss a simultaneous encoder/decoder change. + for (index, kind) in [ + HostRequestKind::Plusargs, + HostRequestKind::FileIo, + HostRequestKind::Vcd, + HostRequestKind::Scan, + HostRequestKind::Distribution, + HostRequestKind::Process, + HostRequestKind::Testbench, + ] + .into_iter() + .enumerate() + { + let bytes = [index as u8]; + assert_eq!( + postcard::from_bytes::(&bytes).unwrap(), + kind + ); + assert_eq!(postcard::to_allocvec(&kind).unwrap(), bytes); + } + } + #[test] fn reader_ignores_runtime_prefix_and_preserves_image_and_offset() { let image = image(); diff --git a/crates/celox/src/scheduler.rs b/crates/celox/src/scheduler.rs index ccd3d23a348..e500e574b56 100644 --- a/crates/celox/src/scheduler.rs +++ b/crates/celox/src/scheduler.rs @@ -425,7 +425,9 @@ impl Plan { for (index, slots) in schema.processes.iter().enumerate() { let process = &model.processes[index]; let unit = &sir.processes[index]; - let reason = if slots.kind == celox_design::ProcessKind::Initializer { + let reason = if slots.kind == celox_design::ProcessKind::Callable { + Some("an explicitly invoked kernel") + } else if slots.kind == celox_design::ProcessKind::Initializer { Some("a declaration initializer") } else if slots.kind == celox_design::ProcessKind::Final { Some("a final procedure") diff --git a/crates/celox/src/simulation.rs b/crates/celox/src/simulation.rs index 404b6434266..3db664b6964 100644 --- a/crates/celox/src/simulation.rs +++ b/crates/celox/src/simulation.rs @@ -202,7 +202,7 @@ impl UnitExecutor for Simulator { timestamp: u64, ) -> Result<(), Halt> { match request.kind { - celox_design::HostRequestKind::Process => { + celox_design::HostRequestKind::Process | celox_design::HostRequestKind::Testbench => { Err(celox_runtime::SimulatorErrorCode::InternalError.into()) } celox_design::HostRequestKind::Plusargs => { diff --git a/crates/celox/src/simulator/builder.rs b/crates/celox/src/simulator/builder.rs index 1d199c50b5e..597f5dd9bc0 100644 --- a/crates/celox/src/simulator/builder.rs +++ b/crates/celox/src/simulator/builder.rs @@ -1767,7 +1767,11 @@ mod host { program: &crate::ir::RuntimeProgram, ) -> Result<(), SimulatorError> { let schema = &program.runtime_schema; - if schema.processes.is_empty() { + if schema + .processes + .iter() + .all(|process| process.kind == celox_design::ProcessKind::Callable) + { return Ok(()); } let origin = schema.process_origins.first().cloned().unwrap_or_default();