wavepeek 1.0.1

Command-line tool for RTL waveform inspection with deterministic machine-friendly output.
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
use serde::Serialize;

use crate::cli::property::{CaptureMode, PropertyArgs};
use crate::diagnostic::{Diagnostic, WarningDiagnosticCode};
use crate::engine::expr_runtime::{
    bind_waveform_event_expr, bind_waveform_logical_expr, candidate_sources_for_handles,
    eval_bound_logical_truth, event_candidate_handles, event_expr_contains_wildcard,
    event_expr_is_any_tracked_only, event_expr_matches, open_shared_waveform,
    referenced_signal_handles,
};
use crate::engine::time::{
    DumpTimeContext, TimeValidationError, format_raw_timestamp, parse_dump_time_context,
    validate_time_token_to_raw,
};
use crate::engine::{CommandData, CommandName, CommandResult, HumanRenderOptions};
use crate::error::WavepeekError;
use crate::expr::EventEvalFrame;
use crate::waveform::{ChangeCandidateCollectionMode, Waveform};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum PropertyResultKind {
    Match,
    Assert,
    Deassert,
}

impl std::fmt::Display for PropertyResultKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Match => f.write_str("match"),
            Self::Assert => f.write_str("assert"),
            Self::Deassert => f.write_str("deassert"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct PropertyCaptureRow {
    pub time: String,
    pub kind: PropertyResultKind,
}

pub fn run(args: PropertyArgs) -> Result<CommandResult, WavepeekError> {
    let waveform = open_shared_waveform(args.waves.as_path())?;
    let metadata = waveform.borrow().metadata()?;
    let dump_time = parse_dump_time_context(&metadata)?;
    let dump_tick = dump_time.dump_tick;

    let from_raw = match args.from.as_deref() {
        Some(token) => parse_bound_time(token, "--from", dump_time, &metadata)?,
        None => u64::try_from(dump_time.dump_start_zs / dump_time.dump_tick_zs).map_err(|_| {
            WavepeekError::Internal("dump start timestamp exceeds supported range".to_string())
        })?,
    };
    let to_raw = match args.to.as_deref() {
        Some(token) => parse_bound_time(token, "--to", dump_time, &metadata)?,
        None => u64::try_from(dump_time.dump_end_zs / dump_time.dump_tick_zs).map_err(|_| {
            WavepeekError::Internal("dump end timestamp exceeds supported range".to_string())
        })?,
    };

    if from_raw > to_raw {
        return Err(WavepeekError::Args(
            "--from must be less than or equal to --to".to_string(),
        ));
    }

    let event_expr_source = args.on.as_deref().unwrap_or("*");
    let (host, bound_event) =
        bind_waveform_event_expr(waveform.clone(), args.scope.as_deref(), event_expr_source)?;
    let bound_eval = bind_waveform_logical_expr(&host, args.scope.as_deref(), args.eval.as_str())?;
    let eval_signal_handles = referenced_signal_handles(&bound_eval);
    let eval_sources = candidate_sources_for_handles(&host, eval_signal_handles.as_slice())?;
    waveform
        .borrow()
        .validate_expr_values_supported(eval_sources.as_slice())?;

    let tracked_signal_handles = if event_expr_contains_wildcard(&bound_event) {
        if eval_signal_handles.is_empty() && event_expr_is_any_tracked_only(&bound_event) {
            return Err(WavepeekError::Args(
                "wildcard trigger cannot infer tracked signals from --eval; pass --on explicitly"
                    .to_string(),
            ));
        }
        eval_signal_handles.clone()
    } else {
        Vec::new()
    };

    let mut candidate_sources = if tracked_signal_handles.is_empty() {
        Vec::new()
    } else {
        eval_sources
    };
    candidate_sources.extend(candidate_sources_for_handles(
        &host,
        &event_candidate_handles(&bound_event),
    )?);
    let mut seen = std::collections::HashSet::new();
    candidate_sources.retain(|signal| seen.insert(signal.id));

    let candidate_times = waveform
        .borrow_mut()
        .collect_expr_candidate_times_with_mode(
            candidate_sources.as_slice(),
            from_raw,
            to_raw,
            ChangeCandidateCollectionMode::Auto,
        )?;
    let candidate_schedule = {
        let waveform_ref = waveform.borrow();
        build_candidate_schedule(&waveform_ref, candidate_times.as_slice())?
    };

    let mut rows = Vec::new();
    let mut previous_state = match args.capture {
        CaptureMode::Match => None,
        CaptureMode::Switch | CaptureMode::Assert | CaptureMode::Deassert => Some(
            eval_bound_logical_truth(args.eval.as_str(), &bound_eval, &host, from_raw)?,
        ),
    };

    for (timestamp, previous_timestamp) in candidate_schedule {
        let frame = EventEvalFrame {
            timestamp,
            previous_timestamp,
            tracked_signals: tracked_signal_handles.as_slice(),
        };
        if !event_expr_matches(event_expr_source, &bound_event, &host, &frame)? {
            continue;
        }

        let decision = eval_bound_logical_truth(args.eval.as_str(), &bound_eval, &host, timestamp)?;
        match args.capture {
            CaptureMode::Match => {
                if decision {
                    rows.push(PropertyCaptureRow {
                        time: format_raw_timestamp(timestamp, dump_tick)?,
                        kind: PropertyResultKind::Match,
                    });
                }
            }
            CaptureMode::Switch | CaptureMode::Assert | CaptureMode::Deassert => {
                if timestamp == from_raw {
                    previous_state = Some(decision);
                    continue;
                }

                let prior = previous_state.unwrap_or(false);
                let transition = match (prior, decision) {
                    (false, true) => Some(PropertyResultKind::Assert),
                    (true, false) => Some(PropertyResultKind::Deassert),
                    _ => None,
                };
                previous_state = Some(decision);

                let Some(kind) = transition else {
                    continue;
                };
                if !capture_allows_kind(args.capture, kind) {
                    continue;
                }

                rows.push(PropertyCaptureRow {
                    time: format_raw_timestamp(timestamp, dump_tick)?,
                    kind,
                });
            }
        }
    }

    let diagnostics = if rows.is_empty() {
        vec![Diagnostic::warning(
            WarningDiagnosticCode::EmptyResult,
            "no property matches found in selected time range",
        )]
    } else {
        Vec::new()
    };

    Ok(CommandResult {
        command: CommandName::Property,
        json: args.json,
        human_options: HumanRenderOptions::default(),
        data: CommandData::Property(rows),
        diagnostics,
    })
}

fn capture_allows_kind(capture: CaptureMode, kind: PropertyResultKind) -> bool {
    match capture {
        CaptureMode::Match | CaptureMode::Switch => true,
        CaptureMode::Assert => kind == PropertyResultKind::Assert,
        CaptureMode::Deassert => kind == PropertyResultKind::Deassert,
    }
}

fn build_candidate_schedule(
    waveform: &Waveform,
    candidate_times: &[u64],
) -> Result<Vec<(u64, Option<u64>)>, WavepeekError> {
    candidate_times
        .iter()
        .map(|timestamp| Ok((*timestamp, waveform.previous_sample_time(*timestamp))))
        .collect()
}

fn parse_bound_time(
    token: &str,
    arg_name: &str,
    dump_time: DumpTimeContext,
    metadata: &crate::waveform::WaveformMetadata,
) -> Result<u64, WavepeekError> {
    match validate_time_token_to_raw(token, dump_time, true) {
        Ok(raw) => Ok(raw),
        Err(TimeValidationError::RequiresUnits) => Err(WavepeekError::Args(format!(
            "time token '{token}' requires units. See 'wavepeek property --help'."
        ))),
        Err(TimeValidationError::InvalidToken) => Err(WavepeekError::Args(format!(
            "invalid time token '{token}': expected <integer><unit> (for example 10ns). See 'wavepeek property --help'."
        ))),
        Err(TimeValidationError::TooLarge) => Err(WavepeekError::Args(format!(
            "time '{token}' is too large to process safely. See 'wavepeek property --help'."
        ))),
        Err(TimeValidationError::OutOfBounds) => Err(WavepeekError::Args(format!(
            "time '{}' for {} is outside dump bounds [{}, {}]. See 'wavepeek property --help'.",
            token, arg_name, metadata.time_start, metadata.time_end
        ))),
        Err(TimeValidationError::NotAligned) => {
            let dump_precision = format_raw_timestamp(1, dump_time.dump_tick)?;
            Err(WavepeekError::Args(format!(
                "time '{token}' cannot be represented exactly in dump precision '{}'. See 'wavepeek property --help'.",
                dump_precision
            )))
        }
        Err(TimeValidationError::RawOutOfRange) => Err(WavepeekError::Args(format!(
            "time '{token}' exceeds supported raw timestamp range. See 'wavepeek property --help'."
        ))),
    }
}

#[cfg(test)]
#[path = "../tests/property_capture_types.rs"]
mod property_capture_types;

#[cfg(test)]
mod tests {
    use std::fs;
    use std::path::PathBuf;

    use tempfile::NamedTempFile;

    use super::{
        DumpTimeContext, PropertyResultKind, build_candidate_schedule, capture_allows_kind,
        parse_bound_time, run,
    };
    use crate::cli::property::{CaptureMode, PropertyArgs};
    use crate::engine::CommandData;
    use crate::engine::time::{ParsedTime, TimeUnit, as_zeptoseconds};
    use crate::waveform::{Waveform, WaveformMetadata};

    const TEST_VCD: &str = concat!(
        "$date\n  today\n$end\n",
        "$version\n  property-test\n$end\n",
        "$timescale 1ns $end\n",
        "$scope module top $end\n",
        "$var wire 1 ! sig $end\n",
        "$upscope $end\n",
        "$enddefinitions $end\n",
        "#0\n",
        "0!\n",
        "#5\n",
        "1!\n",
        "#10\n",
        "0!\n"
    );

    fn metadata() -> WaveformMetadata {
        WaveformMetadata {
            time_unit: "1ns".to_string(),
            time_start: "0ns".to_string(),
            time_end: "10ns".to_string(),
        }
    }

    fn dump_time() -> DumpTimeContext {
        DumpTimeContext {
            dump_tick: ParsedTime {
                value: 1,
                unit: TimeUnit::Ns,
            },
            dump_tick_zs: 1_000_000_000_000,
            dump_start_zs: 0,
            dump_end_zs: 10_000_000_000_000,
        }
    }

    #[test]
    fn property_result_kind_display_is_stable() {
        assert_eq!(PropertyResultKind::Match.to_string(), "match");
        assert_eq!(PropertyResultKind::Assert.to_string(), "assert");
        assert_eq!(PropertyResultKind::Deassert.to_string(), "deassert");
    }

    #[test]
    fn capture_mode_filtering_matches_expected_transition_kinds() {
        assert!(capture_allows_kind(
            CaptureMode::Match,
            PropertyResultKind::Assert
        ));
        assert!(capture_allows_kind(
            CaptureMode::Switch,
            PropertyResultKind::Deassert
        ));
        assert!(capture_allows_kind(
            CaptureMode::Assert,
            PropertyResultKind::Assert
        ));
        assert!(!capture_allows_kind(
            CaptureMode::Assert,
            PropertyResultKind::Deassert
        ));
        assert!(capture_allows_kind(
            CaptureMode::Deassert,
            PropertyResultKind::Deassert
        ));
        assert!(!capture_allows_kind(
            CaptureMode::Deassert,
            PropertyResultKind::Assert
        ));
    }

    #[test]
    fn candidate_schedule_tracks_previous_timestamp_for_indexed_and_between_times() {
        let fixture = write_fixture(TEST_VCD, "property-schedule.vcd");
        let waveform = Waveform::open(fixture.path()).expect("waveform should open");

        let schedule = build_candidate_schedule(&waveform, &[0, 7, 10]).expect("schedule");
        assert_eq!(schedule, vec![(0, None), (7, Some(5)), (10, Some(5))]);
    }

    #[test]
    fn parse_bound_time_maps_validation_failures_to_argument_errors() {
        let md = metadata();
        let context = dump_time();

        assert_eq!(
            parse_bound_time("10ns", "--from", context, &md).expect("valid time"),
            10
        );

        for (token, expected) in [
            ("10", "requires units"),
            ("1.5ns", "expected <integer><unit>"),
            ("11ns", "outside dump bounds [0ns, 10ns]"),
            (
                "15ps",
                "cannot be represented exactly in dump precision '1ns'",
            ),
        ] {
            let error = parse_bound_time(token, "--from", context, &md).expect_err("bad time");
            assert!(error.to_string().contains(expected), "{token}: {error}");
        }

        let huge_context = DumpTimeContext {
            dump_tick: ParsedTime {
                value: 1,
                unit: TimeUnit::Zs,
            },
            dump_tick_zs: 1,
            dump_start_zs: 0,
            dump_end_zs: as_zeptoseconds(ParsedTime {
                value: u64::MAX,
                unit: TimeUnit::Ns,
            })
            .expect("end should convert"),
        };
        let error = parse_bound_time(&format!("{}ns", u64::MAX), "--to", huge_context, &md)
            .expect_err("raw out-of-range should fail");
        assert!(error.to_string().contains("supported raw timestamp range"));

        let error = parse_bound_time(&format!("{}s", u64::MAX), "--to", context, &md)
            .expect_err("too-large time should fail");
        assert!(error.to_string().contains("too large to process safely"));
    }

    #[test]
    fn property_run_captures_match_and_switch_rows_through_public_entrypoint() {
        let fixture = write_fixture(TEST_VCD, ".property-run.vcd");

        let matched = run(PropertyArgs {
            waves: PathBuf::from(fixture.path()),
            from: None,
            to: None,
            scope: Some("top".to_string()),
            on: Some("posedge sig".to_string()),
            eval: "sig".to_string(),
            capture: CaptureMode::Match,
            json: false,
        })
        .expect("match capture should succeed");
        let CommandData::Property(rows) = matched.data else {
            panic!("property command should return property rows");
        };
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].time, "5ns");
        assert_eq!(rows[0].kind, PropertyResultKind::Match);

        let switched = run(PropertyArgs {
            waves: PathBuf::from(fixture.path()),
            from: None,
            to: None,
            scope: Some("top".to_string()),
            on: None,
            eval: "sig".to_string(),
            capture: CaptureMode::Switch,
            json: true,
        })
        .expect("switch capture should succeed");
        let CommandData::Property(rows) = switched.data else {
            panic!("property command should return property rows");
        };
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0].kind, PropertyResultKind::Assert);
        assert_eq!(rows[0].time, "5ns");
        assert_eq!(rows[1].kind, PropertyResultKind::Deassert);
        assert_eq!(rows[1].time, "10ns");

        let assert_only = run(PropertyArgs {
            waves: PathBuf::from(fixture.path()),
            from: Some("0ns".to_string()),
            to: Some("10ns".to_string()),
            scope: Some("top".to_string()),
            on: None,
            eval: "sig".to_string(),
            capture: CaptureMode::Assert,
            json: false,
        })
        .expect("assert capture should succeed");
        let CommandData::Property(rows) = assert_only.data else {
            panic!("property command should return property rows");
        };
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].kind, PropertyResultKind::Assert);

        let error = run(PropertyArgs {
            waves: PathBuf::from(fixture.path()),
            from: Some("10ns".to_string()),
            to: Some("0ns".to_string()),
            scope: Some("top".to_string()),
            on: Some("posedge sig".to_string()),
            eval: "sig".to_string(),
            capture: CaptureMode::Match,
            json: false,
        })
        .expect_err("reversed time bounds should fail");
        assert!(
            error
                .to_string()
                .contains("--from must be less than or equal to --to")
        );
    }

    #[test]
    fn property_run_rejects_signal_free_wildcard_inference() {
        let fixture = write_fixture(TEST_VCD, ".property-run-error.vcd");
        let error = run(PropertyArgs {
            waves: PathBuf::from(fixture.path()),
            from: None,
            to: None,
            scope: Some("top".to_string()),
            on: None,
            eval: "1'b1".to_string(),
            capture: CaptureMode::Match,
            json: false,
        })
        .expect_err("signal-free wildcard trigger should fail");
        assert!(
            error
                .to_string()
                .contains("wildcard trigger cannot infer tracked signals")
        );
    }

    fn write_fixture(contents: &str, suffix: &str) -> NamedTempFile {
        let fixture = NamedTempFile::with_suffix(suffix).expect("fixture should create");
        fs::write(fixture.path(), contents).expect("fixture should write");
        fixture
    }
}