vhpi 0.6.0

Rust wrapper around the IEEE 1076 VHPI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#![cfg_attr(not(windows), allow(clippy::unnecessary_cast))]

use crate::{check_error, Error, Handle, Time};
use num_derive::{FromPrimitive, ToPrimitive};
use std::mem::ManuallyDrop;

bitflags::bitflags! {
    /// Bitmask of callback flags.
    pub struct CallbackFlag: i32 {
        const Disable = vhpi_sys::vhpiDisableCb as i32;
        const Return = vhpi_sys::vhpiReturnCb as i32;
    }
}

#[repr(u32)]
/// Callback reasons.
#[derive(Debug, Clone, PartialEq, Eq, FromPrimitive, ToPrimitive)]
pub enum CbReason {
    /// Triggered at the start of simulation.
    StartOfSimulation = vhpi_sys::vhpiCbStartOfSimulation as u32,
    /// Triggered at the end of simulation.
    EndOfSimulation = vhpi_sys::vhpiCbEndOfSimulation as u32,
    /// Triggered on the next simulation time step.
    NextTimeStep = vhpi_sys::vhpiCbNextTimeStep as u32,
    /// Repeating trigger for each next simulation time step.
    RepNextTimeStep = vhpi_sys::vhpiCbRepNextTimeStep as u32,
    /// Triggered when a watched value changes.
    ValueChange = vhpi_sys::vhpiCbValueChange as u32,
    /// Triggered when a force operation is applied.
    Force = vhpi_sys::vhpiCbForce as u32,
    /// Triggered when a force operation is released.
    Release = vhpi_sys::vhpiCbRelease as u32,
    /// Triggered when a transaction is scheduled.
    Transaction = vhpi_sys::vhpiCbTransaction as u32,
    /// Triggered on statement execution.
    Stmt = vhpi_sys::vhpiCbStmt as u32,
    /// Triggered when simulation resumes.
    Resume = vhpi_sys::vhpiCbResume as u32,
    /// Triggered when simulation suspends.
    Suspend = vhpi_sys::vhpiCbSuspend as u32,
    /// Triggered at the start of a subprogram call.
    StartOfSubpCall = vhpi_sys::vhpiCbStartOfSubpCall as u32,
    /// Triggered at the end of a subprogram call.
    EndOfSubpCall = vhpi_sys::vhpiCbEndOfSubpCall as u32,
    /// Triggered once after a specified delay.
    AfterDelay = vhpi_sys::vhpiCbAfterDelay as u32,
    /// Repeating trigger after each specified delay interval.
    RepAfterDelay = vhpi_sys::vhpiCbRepAfterDelay as u32,
    /// Triggered at the start of the next simulation cycle.
    StartOfNextCycle = vhpi_sys::vhpiCbStartOfNextCycle as u32,
    /// Repeating trigger at the start of each next cycle.
    RepStartOfNextCycle = vhpi_sys::vhpiCbRepStartOfNextCycle as u32,
    /// Triggered at the start of process execution.
    StartOfProcesses = vhpi_sys::vhpiCbStartOfProcesses as u32,
    /// Repeating trigger at the start of process execution.
    RepStartOfProcesses = vhpi_sys::vhpiCbRepStartOfProcesses as u32,
    /// Triggered at the end of process execution.
    EndOfProcesses = vhpi_sys::vhpiCbEndOfProcesses as u32,
    /// Repeating trigger at the end of process execution.
    RepEndOfProcesses = vhpi_sys::vhpiCbRepEndOfProcesses as u32,
    /// Triggered at the last known delta cycle in a time step.
    LastKnownDeltaCycle = vhpi_sys::vhpiCbLastKnownDeltaCycle as u32,
    /// Repeating trigger at the last known delta cycle.
    RepLastKnownDeltaCycle = vhpi_sys::vhpiCbRepLastKnownDeltaCycle as u32,
    /// Triggered at the start of postponed process execution.
    StartOfPostponed = vhpi_sys::vhpiCbStartOfPostponed as u32,
    /// Repeating trigger at the start of postponed execution.
    RepStartOfPostponed = vhpi_sys::vhpiCbRepStartOfPostponed as u32,
    /// Triggered at the end of the current time step.
    EndOfTimeStep = vhpi_sys::vhpiCbEndOfTimeStep as u32,
    /// Repeating trigger at the end of each time step.
    RepEndOfTimeStep = vhpi_sys::vhpiCbRepEndOfTimeStep as u32,
    /// Triggered when tool-specific execution starts.
    StartOfTool = vhpi_sys::vhpiCbStartOfTool as u32,
    /// Triggered when tool-specific execution ends.
    EndOfTool = vhpi_sys::vhpiCbEndOfTool as u32,
    /// Triggered when analysis starts.
    StartOfAnalysis = vhpi_sys::vhpiCbStartOfAnalysis as u32,
    /// Triggered when analysis ends.
    EndOfAnalysis = vhpi_sys::vhpiCbEndOfAnalysis as u32,
    /// Triggered when elaboration starts.
    StartOfElaboration = vhpi_sys::vhpiCbStartOfElaboration as u32,
    /// Triggered when elaboration ends.
    EndOfElaboration = vhpi_sys::vhpiCbEndOfElaboration as u32,
    /// Triggered when initialization starts.
    StartOfInitialization = vhpi_sys::vhpiCbStartOfInitialization as u32,
    /// Triggered when initialization ends.
    EndOfInitialization = vhpi_sys::vhpiCbEndOfInitialization as u32,
    /// Triggered when the simulator reaches quiescence.
    Quiescense = vhpi_sys::vhpiCbQuiescense as u32,
    /// Triggered when a PLI error is reported.
    PLIError = vhpi_sys::vhpiCbPLIError as u32,
    /// Triggered when save operation starts.
    StartOfSave = vhpi_sys::vhpiCbStartOfSave as u32,
    /// Triggered when save operation ends.
    EndOfSave = vhpi_sys::vhpiCbEndOfSave as u32,
    /// Triggered when restart operation starts.
    StartOfRestart = vhpi_sys::vhpiCbStartOfRestart as u32,
    /// Triggered when restart operation ends.
    EndOfRestart = vhpi_sys::vhpiCbEndOfRestart as u32,
    /// Triggered when reset operation starts.
    StartOfReset = vhpi_sys::vhpiCbStartOfReset as u32,
    /// Triggered when reset operation ends.
    EndOfReset = vhpi_sys::vhpiCbEndOfReset as u32,
    /// Triggered when entering interactive mode.
    EnterInteractive = vhpi_sys::vhpiCbEnterInteractive as u32,
    /// Triggered when leaving interactive mode.
    ExitInteractive = vhpi_sys::vhpiCbExitInteractive as u32,
    /// Triggered on signal interrupt.
    SigInterrupt = vhpi_sys::vhpiCbSigInterrupt as u32,
    /// Triggered when a timeout expires.
    TimeOut = vhpi_sys::vhpiCbTimeOut as u32,
    /// Repeating trigger for timeout expiry.
    RepTimeOut = vhpi_sys::vhpiCbRepTimeOut as u32,
    /// Triggered when sensitivity conditions are met.
    Sensitivity = vhpi_sys::vhpiCbSensitivity as u32,
    Unknown,
}

impl CbReason {
    #[must_use]
    pub fn from_u32(value: u32) -> Self {
        num_traits::FromPrimitive::from_u32(value).unwrap_or(CbReason::Unknown)
    }
}

/// Data passed to callback functions.
pub struct CbData {
    obj: ManuallyDrop<Handle>,
}

impl CbData {
    #[inline]
    unsafe fn from_raw(raw: *const vhpi_sys::vhpiCbDataS) -> Self {
        Self {
            // vhpi_sys::vhpiCbDataS::obj is the callback trigger object handle.
            // Keep it borrowed by avoiding Drop with ManuallyDrop.
            obj: ManuallyDrop::new(unsafe { Handle::from_raw((*raw).obj) }),
        }
    }

    #[must_use]
    pub fn obj(&self) -> &Handle {
        // ManuallyDrop has the same layout as Handle; only Drop behavior differs.
        unsafe { &*(&raw const self.obj).cast::<Handle>() }
    }
}

/// Information about a registered callback returned by [`get_cb_info`] and
/// [`Handle::get_cb_info`].
#[derive(Debug, PartialEq)]
pub struct CbInfo {
    /// Raw callback reason as returned by the simulator. Compare against
    /// [`CbReason`] discriminant values to identify the reason.
    pub reason: CbReason,
    /// The trigger object this callback is attached to.
    ///
    /// The handle is borrowed from the simulator and must not be released;
    /// it is valid only as long as the callback handle is alive.
    obj: ManuallyDrop<Handle>,
    /// The scheduled simulation time for time-based callbacks.
    pub time: Option<Time>,
}

impl Clone for CbInfo {
    fn clone(&self) -> Self {
        Self {
            reason: self.reason.clone(),
            // SAFETY: This callback object is borrowed, and ManuallyDrop
            // prevents the wrapper from releasing it.
            obj: ManuallyDrop::new(unsafe { Handle::from_raw(self.obj().as_raw()) }),
            time: self.time.clone(),
        }
    }
}

impl CbInfo {
    #[must_use]
    /// Return a reference to the trigger object handle.
    pub fn obj(&self) -> &Handle {
        unsafe { &*(&raw const self.obj).cast::<Handle>() }
    }
}

struct AfterDelayCbState<F>
where
    F: Fn(&CbData),
{
    callback: F,
    time: vhpi_sys::vhpiTimeT,
}

#[derive(Debug, Clone, PartialEq)]
/// Errors returned when registering a VHPI callback.
pub enum RegisterCbError {
    /// A callback reason value was not recognized.
    UnknownReason,
    /// The simulator reported an error while registering the callback.
    Error(Error),
}

unsafe extern "C" fn trampoline<F>(cb_data: *const vhpi_sys::vhpiCbDataS)
where
    F: Fn(&CbData),
{
    if cb_data.is_null() {
        return;
    }

    let user_data = (*cb_data).user_data.cast::<F>();
    if user_data.is_null() {
        return;
    }

    let data = CbData::from_raw(cb_data);

    let callback = &*user_data;
    callback(&data);
}

unsafe extern "C" fn after_delay_trampoline<F>(cb_data: *const vhpi_sys::vhpiCbDataS)
where
    F: Fn(&CbData),
{
    if cb_data.is_null() {
        return;
    }

    let user_data = (*cb_data).user_data.cast::<AfterDelayCbState<F>>();
    if user_data.is_null() {
        return;
    }

    let data = CbData::from_raw(cb_data);

    ((*user_data).callback)(&data);

    drop(Box::from_raw(user_data));
}

/// Register a global callback for a simulator event.
///
/// The callback is retained by the simulator and can be removed later using
/// [`remove_cb`]. The returned handle represents the registered callback.
///
/// # Errors
///
/// Returns [`RegisterCbError::Error`] when the simulator reports an error while
/// registering the callback.
pub fn register_cb<F>(reason: CbReason, callback: F) -> Result<Handle, RegisterCbError>
where
    F: Fn(&CbData) + 'static,
{
    let boxed: Box<F> = Box::new(callback);
    let user_data = Box::into_raw(boxed).cast::<std::os::raw::c_void>();

    let mut cb_data = vhpi_sys::vhpiCbDataS {
        reason: reason as i32,
        cb_rtn: Some(trampoline::<F>),
        obj: std::ptr::null_mut(),
        time: std::ptr::null_mut(),
        value: std::ptr::null_mut(),
        user_data,
    };
    let ret = unsafe { vhpi_sys::vhpi_register_cb(&raw mut cb_data, CallbackFlag::Return.bits()) };
    match check_error() {
        Some(err) => {
            unsafe {
                drop(Box::from_raw(user_data.cast::<F>()));
            }
            Err(RegisterCbError::Error(err))
        }
        // SAFETY: VHPI returns a callback handle owned by the caller.
        None => Ok(unsafe { Handle::from_raw(ret) }),
    }
}

/// Register a callback that fires once after the specified simulation delay.
///
/// The callback state is released after the callback runs. If you need to
/// cancel it before it fires, call [`remove_cb`] with the returned handle.
///
/// # Errors
///
/// Returns [`RegisterCbError::Error`] when the simulator reports an error while
/// registering the callback.
pub fn register_cb_after_delay<F>(delay: Time, callback: F) -> Result<Handle, RegisterCbError>
where
    F: Fn(&CbData) + 'static,
{
    let boxed = Box::new(AfterDelayCbState {
        callback,
        time: delay.into(),
    });
    let user_data = Box::into_raw(boxed);
    let mut cb_data = vhpi_sys::vhpiCbDataS {
        reason: CbReason::AfterDelay as i32,
        cb_rtn: Some(after_delay_trampoline::<F>),
        obj: std::ptr::null_mut(),
        time: unsafe { &raw mut (*user_data).time },
        value: std::ptr::null_mut(),
        user_data: user_data.cast::<std::os::raw::c_void>(),
    };
    let ret = unsafe { vhpi_sys::vhpi_register_cb(&raw mut cb_data, CallbackFlag::Return.bits()) };
    match check_error() {
        Some(err) => {
            unsafe {
                drop(Box::from_raw(user_data.cast::<AfterDelayCbState<F>>()));
            }
            Err(RegisterCbError::Error(err))
        }
        // SAFETY: VHPI returns a callback handle owned by the caller.
        None => Ok(unsafe { Handle::from_raw(ret) }),
    }
}

/// Remove a previously registered callback.
///
/// # Errors
///
/// Returns an [`Error`] if the simulator reports a failure.
pub fn remove_cb(handle: &Handle) -> Result<(), Error> {
    let rc = unsafe { vhpi_sys::vhpi_remove_cb(handle.as_raw()) };
    if rc != 0 {
        Err(check_error().unwrap_or_else(|| "vhpi_remove_cb failed".into()))
    } else {
        Ok(())
    }
}

/// Disable a previously registered callback without removing it.
///
/// The callback remains registered but will not fire until re-enabled with
/// [`enable_cb`]. Returns an error if the simulator reports a failure.
///
/// # Errors
///
/// Returns an [`Error`] if the simulator reports a failure.
pub fn disable_cb(handle: &Handle) -> Result<(), Error> {
    let rc = unsafe { vhpi_sys::vhpi_disable_cb(handle.as_raw()) };
    if rc != 0 {
        Err(check_error().unwrap_or_else(|| "vhpi_disable_cb failed".into()))
    } else {
        Ok(())
    }
}

/// Re-enable a callback that was previously disabled with [`disable_cb`].
///
/// # Errors
///
/// Returns an [`Error`] if the simulator reports a failure.
pub fn enable_cb(handle: &Handle) -> Result<(), Error> {
    let rc = unsafe { vhpi_sys::vhpi_enable_cb(handle.as_raw()) };
    if rc != 0 {
        Err(check_error().unwrap_or_else(|| "vhpi_enable_cb failed".into()))
    } else {
        Ok(())
    }
}

/// Retrieve information about a registered callback.
///
/// # Errors
///
/// Returns an [`Error`] if the simulator reports a failure.
pub fn get_cb_info(handle: &Handle) -> Result<CbInfo, Error> {
    let mut raw: vhpi_sys::vhpiCbDataT = unsafe { std::mem::zeroed() };
    let rc = unsafe { vhpi_sys::vhpi_get_cb_info(handle.as_raw(), &raw mut raw) };
    if rc != 0 {
        return Err(check_error().unwrap_or_else(|| "vhpi_get_cb_info failed".into()));
    }
    let time = if raw.time.is_null() {
        None
    } else {
        Some(Time::from(unsafe { *raw.time }))
    };
    Ok(CbInfo {
        reason: CbReason::from_u32(raw.reason as u32),
        // SAFETY: The callback object is borrowed; ManuallyDrop prevents release.
        obj: ManuallyDrop::new(unsafe { Handle::from_raw(raw.obj) }),
        time,
    })
}

impl Handle {
    /// Register a callback scoped to this object handle.
    ///
    /// # Errors
    ///
    /// Returns [`RegisterCbError::Error`] when the simulator reports an error
    /// while registering the callback.
    pub fn register_cb<F>(&self, reason: CbReason, callback: F) -> Result<Handle, RegisterCbError>
    where
        F: Fn(&CbData) + 'static,
    {
        let boxed: Box<F> = Box::new(callback);
        let user_data = Box::into_raw(boxed).cast::<std::os::raw::c_void>();

        let mut cb_data = vhpi_sys::vhpiCbDataS {
            reason: reason as i32,
            cb_rtn: Some(trampoline::<F>),
            obj: self.as_raw(),
            time: std::ptr::null_mut(),
            value: std::ptr::null_mut(),
            user_data,
        };
        let ret =
            unsafe { vhpi_sys::vhpi_register_cb(&raw mut cb_data, CallbackFlag::Return.bits()) };
        match check_error() {
            Some(err) => {
                unsafe {
                    drop(Box::from_raw(user_data.cast::<F>()));
                }
                Err(RegisterCbError::Error(err))
            }
            // SAFETY: VHPI returns a callback handle owned by the caller.
            None => Ok(unsafe { Handle::from_raw(ret) }),
        }
    }

    /// Remove the callback represented by this handle.
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if the simulator reports a failure.
    pub fn remove_cb(&self) -> Result<(), Error> {
        remove_cb(self)
    }

    /// Disable this callback without removing it.
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if the simulator reports a failure.
    pub fn disable_cb(&self) -> Result<(), Error> {
        disable_cb(self)
    }

    /// Re-enable this callback after it was disabled.
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if the simulator reports a failure.
    pub fn enable_cb(&self) -> Result<(), Error> {
        enable_cb(self)
    }

    /// Retrieve information about this callback.
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if the simulator reports a failure.
    pub fn get_cb_info(&self) -> Result<CbInfo, Error> {
        get_cb_info(self)
    }
}