fstapi 0.0.3

API for manipulating Fast Signal Trace (FST) format waveforms.
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
458
459
460
461
462
463
464
465
466
467
468
use crate::consts::{AttrType, FileType, ScopeType, VarDir, VarType};
use crate::types::Handle;
use crate::utils::*;
use crate::{Error, Result, capi};
use std::marker::PhantomData;
use std::num::NonZeroU32;
use std::os::raw;
use std::path::Path;
use std::{ptr, slice};

/// FST waveform reader.
#[derive(Debug)]
pub struct Reader {
  /// Non-null context pointer.
  ctx: *mut raw::c_void,
}

impl Reader {
  /// Opens an FST waveform from the given path.
  pub fn open<P>(path: P) -> Result<Self>
  where
    P: AsRef<Path>,
  {
    let path = path.to_str()?.into_cstring()?;
    let ctx = unsafe { capi::fstReaderOpen(path.as_ptr()) };
    if ctx.is_null() {
      Err(Error::ContextCreate)
    } else {
      Ok(Self { ctx })
    }
  }

  /// Returns date.
  pub fn date(&self) -> Result<&str> {
    unsafe { capi::fstReaderGetDateString(self.ctx).to_str() }
  }

  /// Returns date as raw C string.
  pub fn date_raw(&self) -> *const raw::c_char {
    unsafe { capi::fstReaderGetDateString(self.ctx) }
  }

  /// Returns version.
  pub fn version(&self) -> Result<&str> {
    unsafe { capi::fstReaderGetVersionString(self.ctx).to_str() }
  }

  /// Returns version as raw C string.
  pub fn version_raw(&self) -> *const raw::c_char {
    unsafe { capi::fstReaderGetVersionString(self.ctx) }
  }

  /// Returns start time.
  pub fn start_time(&self) -> u64 {
    unsafe { capi::fstReaderGetStartTime(self.ctx) }
  }

  /// Returns end time.
  pub fn end_time(&self) -> u64 {
    unsafe { capi::fstReaderGetEndTime(self.ctx) }
  }

  /// Returns file type.
  pub fn file_type(&self) -> FileType {
    unsafe { capi::fstReaderGetFileType(self.ctx) as FileType }
  }

  /// Returns alias count.
  pub fn alias_count(&self) -> u64 {
    unsafe { capi::fstReaderGetAliasCount(self.ctx) }
  }

  /// Returns scope count.
  pub fn scope_count(&self) -> u64 {
    unsafe { capi::fstReaderGetScopeCount(self.ctx) }
  }

  /// Returns variable count.
  pub fn var_count(&self) -> u64 {
    unsafe { capi::fstReaderGetVarCount(self.ctx) }
  }

  /// Returns timescale.
  pub fn timescale(&self) -> i32 {
    unsafe { capi::fstReaderGetTimescale(self.ctx) as i32 }
  }

  /// Returns timescale as string.
  ///
  /// Returns [`None`] if the timescale is not valid.
  pub fn timescale_str(&self) -> Option<&'static str> {
    match self.timescale() + 21 {
      t @ 0..=23 => Some(
        [
          "1zs", "10zs", "100zs", "1as", "10as", "100as", "1fs", "10fs", "100fs", "1ps", "10ps",
          "100ps", "1ns", "10ns", "100ns", "1us", "10us", "100us", "1ms", "10ms", "100ms", "1s",
          "10s", "100s",
        ][t as usize],
      ),
      _ => None,
    }
  }

  /// Returns timezero.
  pub fn timezero(&self) -> i64 {
    unsafe { capi::fstReaderGetTimezero(self.ctx) }
  }

  /// Returns process mask for the facility of the given handle.
  pub fn mask(&self, handle: Handle) -> bool {
    unsafe { capi::fstReaderGetFacProcessMask(self.ctx, handle.into()) != 0 }
  }

  /// Clears process mask for the facility of the given handle.
  pub fn clear_mask(&mut self, handle: Handle) {
    unsafe { capi::fstReaderClrFacProcessMask(self.ctx, handle.into()) }
  }

  /// Clears process mask for all facilities.
  pub fn clear_mask_all(&mut self) {
    unsafe { capi::fstReaderClrFacProcessMaskAll(self.ctx) }
  }

  /// Sets process mask for the facility of the given handle.
  pub fn set_mask(&mut self, handle: Handle) {
    unsafe { capi::fstReaderSetFacProcessMask(self.ctx, handle.into()) }
  }

  /// Sets process mask for all facilities.
  pub fn set_mask_all(&mut self) {
    unsafe { capi::fstReaderSetFacProcessMaskAll(self.ctx) }
  }

  /// Sets time range limit.
  pub fn set_time_range_limit(&mut self, start_time: u64, end_time: u64) {
    unsafe { capi::fstReaderSetLimitTimeRange(self.ctx, start_time, end_time) }
  }

  /// Resets time range limit.
  pub fn reset_time_range_limit(&mut self) {
    unsafe { capi::fstReaderSetUnlimitedTimeRange(self.ctx) }
  }

  /// Sets whether to use native doubles in callback when iterating over blocks.
  pub fn set_native_doubles_on_callback(&mut self, enable: bool) {
    unsafe { capi::fstReaderIterBlocksSetNativeDoublesOnCallback(self.ctx, enable as i32) }
  }

  /// Returns an iterator over the hierarchies of the waveform.
  pub fn hiers(&mut self) -> Hiers<'_> {
    unsafe { capi::fstReaderIterateHierRewind(self.ctx) };
    Hiers {
      ctx: self.ctx,
      phantom: PhantomData,
    }
  }

  /// Returns an iterator over the variables of the waveform.
  pub fn vars(&mut self) -> Vars<'_> {
    unsafe { capi::fstReaderIterateHierRewind(self.ctx) };
    Vars {
      hiers: self.hiers(),
      scopes: Vec::new(),
    }
  }

  /// Runs the given callback on each block of the waveform.
  ///
  /// The callback will be called when value changes, and is defined as:
  ///
  /// ```
  /// fn callback(time: u64, handle: fstapi::Handle, value: &[u8], var_len: bool) {
  ///   // ...
  /// }
  /// ```
  pub fn for_each_block<F>(&mut self, mut callback: F) -> Result<()>
  where
    F: FnMut(u64, Handle, &[u8], bool),
  {
    extern "C" fn c_callback<F>(
      data: *mut raw::c_void,
      time: u64,
      handle: capi::fstHandle,
      value: *const raw::c_uchar,
      len: u32,
    ) where
      F: FnMut(u64, Handle, &[u8], bool),
    {
      let data: &mut F = unsafe { &mut *(data as *mut F) };
      let handle = unsafe { Handle(NonZeroU32::new_unchecked(handle)) };
      let value = unsafe { slice::from_raw_parts(value, len as usize) };
      data(time, handle, value, false);
    }

    extern "C" fn c_callback_var_len<F>(
      data: *mut raw::c_void,
      time: u64,
      handle: capi::fstHandle,
      value: *const raw::c_uchar,
      len: u32,
    ) where
      F: FnMut(u64, Handle, &[u8], bool),
    {
      let data: &mut F = unsafe { &mut *(data as *mut F) };
      let handle = unsafe { Handle(NonZeroU32::new_unchecked(handle)) };
      let value = unsafe { slice::from_raw_parts(value, len as usize) };
      data(time, handle, value, true);
    }

    let ret = unsafe {
      capi::fstReaderIterBlocks2(
        self.ctx,
        Some(c_callback::<F>),
        Some(c_callback_var_len::<F>),
        (&mut callback) as *mut _ as *mut raw::c_void,
        ptr::null_mut(),
      )
    };
    match ret {
      0 => Err(Error::InvalidOperation),
      _ => Ok(()),
    }
  }

  /// Dumps the content of waveform as VCD format to the given file
  /// ([Some(path)]) or the standard output ([None]).
  pub fn dump_as_vcd<P>(&mut self, path: Option<P>) -> Result<()>
  where
    P: AsRef<Path>,
  {
    let ret = if let Some(path) = path {
      let path = path.to_str()?.into_cstring()?;
      unsafe { capi::fstReaderDumpToVcdFile(self.ctx, path.as_ptr()) }
    } else {
      unsafe { capi::fstReaderDumpToVcdFile(self.ctx, ptr::null()) }
    };
    match ret {
      0 => Ok(()),
      _ => Err(Error::InvalidOperation),
    }
  }
}

impl Drop for Reader {
  fn drop(&mut self) {
    unsafe { capi::fstReaderClose(self.ctx) }
  }
}

/// An iterator over the hierarchies of an FST waveform.
///
/// This struct is created by the [`hiers`](Reader::hiers)
/// method on [`Reader`].
#[derive(Debug)]
pub struct Hiers<'a> {
  ctx: *mut raw::c_void,
  phantom: PhantomData<&'a ()>,
}

impl<'a> Iterator for Hiers<'a> {
  type Item = Hier<'a>;

  fn next(&mut self) -> Option<Self::Item> {
    unsafe { capi::fstReaderIterateHier(self.ctx).as_ref() }.map(Hier::new)
  }
}

/// Hierarchy of FST waveform.
#[derive(Debug)]
pub enum Hier<'a> {
  /// Begin of a scope.
  Scope(Scope<'a>),
  /// End of a scope.
  Upscope,
  /// Variable.
  Var(Var<'a>),
  /// Begin of an attribute.
  AttrBegin(Attr<'a>),
  /// End of an attribute.
  AttrEnd,
}

impl<'a> Hier<'a> {
  /// Creates a new hierarchy.
  fn new(hier: &'a capi::fstHier) -> Self {
    match hier.htyp as capi::fstHierType {
      capi::fstHierType_FST_HT_SCOPE => Self::Scope(Scope(unsafe { &hier.u.scope })),
      capi::fstHierType_FST_HT_UPSCOPE => Self::Upscope,
      capi::fstHierType_FST_HT_VAR => Self::Var(Var(unsafe { &hier.u.var })),
      capi::fstHierType_FST_HT_ATTRBEGIN => Self::AttrBegin(Attr(unsafe { &hier.u.attr })),
      capi::fstHierType_FST_HT_ATTREND => Self::AttrEnd,
      _ => unreachable!(),
    }
  }
}

/// A scope in FST hierarchy.
#[derive(Debug)]
pub struct Scope<'a>(&'a capi::fstHier__bindgen_ty_1_fstHierScope);

impl<'a> Scope<'a> {
  /// Returns scope type.
  pub fn ty(&self) -> ScopeType {
    self.0.typ as ScopeType
  }

  /// Returns scope name.
  pub fn name(&self) -> Result<&str> {
    unsafe { (self.0.name, self.0.name_length + 1).to_str() }
  }

  /// Returns scope name as raw C string.
  pub fn name_raw(&self) -> *const raw::c_char {
    self.0.name
  }

  /// Returns scope component.
  pub fn component(&self) -> Result<&str> {
    unsafe { (self.0.component, self.0.component_length + 1).to_str() }
  }

  /// Returns scope component as raw C string.
  pub fn component_raw(&self) -> *const raw::c_char {
    self.0.component
  }
}

/// A variable in FST hierarchy.
#[derive(Debug)]
pub struct Var<'a>(&'a capi::fstHier__bindgen_ty_1_fstHierVar);

impl<'a> Var<'a> {
  /// Returns variable type.
  pub fn ty(&self) -> VarType {
    self.0.typ as VarType
  }

  /// Returns variable direction.
  pub fn direction(&self) -> VarDir {
    self.0.direction as VarDir
  }

  /// Returns variable name.
  pub fn name(&self) -> Result<&str> {
    unsafe { (self.0.name, self.0.name_length + 1).to_str() }
  }

  /// Returns variable name as raw C string.
  pub fn name_raw(&self) -> *const raw::c_char {
    self.0.name
  }

  /// Returns variable length in bits.
  pub fn length(&self) -> u32 {
    self.0.length
  }

  /// Returns variable handle.
  pub fn handle(&self) -> Handle {
    unsafe { Handle(NonZeroU32::new_unchecked(self.0.handle)) }
  }

  /// Returns `true` if variable is an alias.
  pub fn is_alias(&self) -> bool {
    self.0.is_alias() != 0
  }
}

/// An attribute in FST hierarchy.
#[derive(Debug)]
pub struct Attr<'a>(&'a capi::fstHier__bindgen_ty_1_fstHierAttr);

impl<'a> Attr<'a> {
  /// Returns attribute type.
  pub fn ty(&self) -> AttrType {
    self.0.typ as AttrType
  }

  /// Returns attribute subtype.
  ///
  /// The subtype may be one of [`MiscType`](crate::consts::MiscType),
  /// [`ArrayType`](crate::consts::ArrayType),
  /// [`EnumValueType`](crate::consts::EnumValueType) or
  /// [`PackType`](crate::consts::PackType).
  pub fn subtype(&self) -> u32 {
    self.0.subtype as u32
  }

  /// Returns attribute name.
  pub fn name(&self) -> Result<&str> {
    unsafe { (self.0.name, self.0.name_length + 1).to_str() }
  }

  /// Returns attribute name as raw C string.
  pub fn name_raw(&self) -> *const raw::c_char {
    self.0.name
  }

  /// Returns attribute argument.
  ///
  /// Argument may be number of array elements, struct members,
  /// or some other payload (possibly ignored).
  pub fn arg(&self) -> u64 {
    self.0.arg
  }

  /// Returns attribute argument generated by the attribute name.
  ///
  /// For when name is overloaded as a variable-length integer,
  /// i.e. `ty` is [`attr_type::MISC`](crate::consts::attr_type::MISC)
  /// and `subtype` is
  /// [`misc_type::SOURCESTEM`](crate::consts::misc_type::SOURCESTEM) or
  /// [`misc_type::SOURCEISTEM`](crate::consts::misc_type::SOURCEISTEM).
  pub fn arg_from_name(&self) -> u64 {
    self.0.arg_from_name
  }
}

/// An iterator over the variables of an FST waveform.
///
/// This struct is created by the [`vars`](Reader::vars)
/// method on [`Reader`].
#[derive(Debug)]
pub struct Vars<'a> {
  hiers: Hiers<'a>,
  scopes: Vec<String>,
}

impl<'a> Iterator for Vars<'a> {
  type Item = Result<(String, Var<'a>)>;

  fn next(&mut self) -> Option<Self::Item> {
    macro_rules! unwrap_or_return {
      ($e:expr) => {
        match $e {
          Ok(v) => v,
          Err(e) => return Some(Err(e)),
        }
      };
    }

    for hier in self.hiers.by_ref() {
      match hier {
        Hier::Scope(s) => {
          let name = unwrap_or_return!(s.name());
          match self.scopes.last() {
            Some(last) => self.scopes.push(format!("{last}.{name}")),
            None => self.scopes.push(name.into()),
          }
        }
        Hier::Upscope => {
          self.scopes.pop();
        }
        Hier::Var(v) => {
          let name = unwrap_or_return!(v.name());
          let name = match self.scopes.last() {
            Some(last) => format!("{last}.{name}"),
            None => name.into(),
          };
          return Some(Ok((name, v)));
        }
        _ => {}
      }
    }

    None
  }
}