veryl 0.19.1

A modern hardware description language
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
use crate::cmd_build::CmdBuild;
use crate::runner::{Cocotb, CocotbSource, Dsim, Vcs, Verilator, Vivado};
use crate::{OptBuild, OptTest};
use log::{error, info, warn};
use miette::Result;
use std::path::PathBuf;
use veryl_analyzer::symbol::TestType;
use veryl_analyzer::symbol_table;
use veryl_metadata::WaveFormFormat;
use veryl_metadata::{FilelistType, Metadata, SimType, WaveFormTarget};
use veryl_parser::resource_table::{self, PathId};
use veryl_simulator::ir::{Config, Ir, ProtoModuleCache, build_ir_cached};
use veryl_simulator::output_buffer;
use veryl_simulator::simulator::Simulator;
use veryl_simulator::simulator_error::SimulatorError;
use veryl_simulator::testbench::{TestResult, run_native_testbench};
use veryl_simulator::wave_dumper::WaveDumper;
use veryl_simulator::wavedrom::{self, SignalKind, classify_signals, parse_wavedrom};

pub struct CmdTest {
    opt: OptTest,
}

struct NativeTestJob {
    test_name: String,
    module_name: String,
    sim_ir: Ir,
    dump: Option<WaveDumper>,
}

fn wave_output_path(name: &str, test_path: PathId, metadata: &Metadata) -> PathBuf {
    let target_name = format!("{}.{}", name, metadata.test.waveform_format.extension());
    match &metadata.test.waveform_target {
        WaveFormTarget::Target => PathBuf::from(test_path.to_string())
            .parent()
            .unwrap()
            .join(target_name),
        WaveFormTarget::Directory { path } => path.join(target_name),
    }
}

fn create_wave_dumper(
    name: &str,
    test_path: PathId,
    metadata: &Metadata,
) -> std::result::Result<WaveDumper, SimulatorError> {
    let path = wave_output_path(name, test_path, metadata);
    if let Some(parent) = path.parent()
        && !parent.exists()
    {
        std::fs::create_dir_all(parent).map_err(|e| SimulatorError::IoError {
            message: format!("failed to create directory {}: {e}", parent.display()),
        })?;
    }
    let path_str = path.to_string_lossy().to_string();
    info!("  Dumping waveform to {}", path_str);
    let dumper = match metadata.test.waveform_format {
        WaveFormFormat::Vcd => {
            let file = std::fs::File::create(&path).map_err(|e| SimulatorError::IoError {
                message: format!("failed to create waveform file {}: {e}", path.display()),
            })?;
            WaveDumper::new_vcd(Box::new(file))
        }
        WaveFormFormat::Fst => WaveDumper::new_fst(&path_str),
    };
    Ok(dumper.with_path(path))
}

impl CmdTest {
    pub fn new(opt: OptTest) -> Self {
        Self { opt }
    }

    pub fn exec(&self, metadata: &mut Metadata) -> Result<bool> {
        // force filelist_type to absolute which can be refered from temporary directory
        metadata.build.filelist_type = FilelistType::Absolute;

        let build = CmdBuild::new(OptBuild {
            files: self.opt.files.clone(),
            check: false,
        });

        let mut ir = veryl_analyzer::ir::Ir::default();
        build.exec(metadata, true, false, Some(&mut ir))?;

        let tests = symbol_table::get_tests(&metadata.project.name);
        let doc_tests = symbol_table::get_doc_tests(&metadata.project.name);

        let total_tests = tests.len();
        let tests: Vec<_> = if self.opt.include_ignored {
            tests
        } else if self.opt.ignored {
            tests
                .into_iter()
                .filter(|(_, property)| property.ignored)
                .collect()
        } else {
            tests
                .into_iter()
                .filter(|(_, property)| !property.ignored)
                .collect()
        };
        let ignored_count = total_tests - tests.len();

        if ignored_count > 0 {
            info!("{ignored_count} test(s) ignored");
        }

        let (tests, doc_tests) = if let Some(ref filter) = self.opt.test {
            let tests: Vec<_> = tests
                .into_iter()
                .filter(|(test, _)| {
                    let name = test.to_string();
                    name.contains(filter.as_str())
                })
                .collect();

            let doc_tests: Vec<_> = doc_tests
                .into_iter()
                .filter(|dt| {
                    let name = dt.module_name.to_string();
                    name.contains(filter.as_str())
                })
                .collect();

            if tests.is_empty() && doc_tests.is_empty() {
                warn!("No tests matched filter '{filter}'");
            }

            (tests, doc_tests)
        } else {
            (tests, doc_tests)
        };

        let sim_type = if let Some(x) = self.opt.sim {
            x.into()
        } else {
            metadata.test.simulator
        };

        let config = Config {
            use_jit: !self.opt.disable_jit,
            disable_ff_opt: self.opt.disable_ff_opt,
            ..Config::default()
        };
        let mut proto_cache = ProtoModuleCache::default();

        let mut success = 0;
        let mut failure = 0;

        let mut native_jobs: Vec<NativeTestJob> = Vec::new();
        let mut non_native_tests = Vec::new();

        for (test, property) in &tests {
            match property.r#type {
                TestType::Native => {
                    let test_name = test.to_string();
                    info!("Building IR for test ({test_name})");

                    match prepare_native_test(
                        &ir,
                        &test_name,
                        &property.top,
                        &self.opt,
                        property.path,
                        metadata,
                        &config,
                        &mut proto_cache,
                    ) {
                        Ok(job) => native_jobs.push(job),
                        Err(e) => {
                            error!("Failed to build IR for test ({test_name}): {e}");
                            failure += 1;
                        }
                    }
                }
                _ => {
                    non_native_tests.push((test, property));
                }
            }
        }

        if !native_jobs.is_empty() {
            let num_threads = std::thread::available_parallelism()
                .map(|n| n.get())
                .unwrap_or(1)
                .min(native_jobs.len());
            let job_queue = std::sync::Mutex::new(native_jobs.into_iter());
            let table_snapshot = resource_table::export_tables();

            type JobResult = (
                String,
                std::result::Result<(TestResult, Option<PathBuf>), SimulatorError>,
                String,
            );
            let results: Vec<Vec<JobResult>> = std::thread::scope(|s| {
                let queue = &job_queue;
                let snapshot = &table_snapshot;
                let handles: Vec<_> = (0..num_threads)
                    .map(|_| {
                        s.spawn(move || {
                            resource_table::import_tables(snapshot);
                            let mut thread_results = Vec::new();
                            loop {
                                let job = queue.lock().unwrap().next();
                                let Some(job) = job else { break };
                                output_buffer::enable();
                                let wave_path = job.dump.as_ref().and_then(|d| d.path().cloned());
                                let result =
                                    run_native_testbench(job.sim_ir, job.dump, job.module_name);
                                let result = result.map(|r| (r, wave_path));
                                let output = output_buffer::take();
                                thread_results.push((job.test_name, result, output));
                            }
                            thread_results
                        })
                    })
                    .collect();
                handles.into_iter().map(|h| h.join().unwrap()).collect()
            });

            for (test_name, result, output) in results.into_iter().flatten() {
                info!("Executing test ({test_name})");
                if !output.is_empty() {
                    print!("{output}");
                }
                match result {
                    Ok((TestResult::Pass, wave_path)) => {
                        info!("Succeeded test ({test_name})");
                        success += 1;
                        if let Some(path) = wave_path {
                            metadata.add_generated_file(path);
                        }
                    }
                    Ok((TestResult::Fail(msg), _)) => {
                        error!("Failed test ({test_name}): {msg}");
                        failure += 1;
                    }
                    Err(e) => {
                        error!("Failed test ({test_name}): {e}");
                        failure += 1;
                    }
                }
            }
        }

        for (test, property) in non_native_tests {
            let mut runner = match property.r#type {
                TestType::Inline => match sim_type {
                    SimType::Verilator => Verilator::new().runner(),
                    SimType::Vcs => Vcs::new().runner(),
                    SimType::Dsim => Dsim::new().runner(),
                    SimType::Vivado => Vivado::new().runner(),
                },
                TestType::CocotbEmbed(ref x) => {
                    Cocotb::new(CocotbSource::Embed(x.clone())).runner()
                }
                TestType::CocotbInclude(x) => Cocotb::new(CocotbSource::Include(x)).runner(),
                TestType::Native => unreachable!(),
            };

            if runner.run(metadata, *test, property.top, property.path, self.opt.wave)? {
                success += 1;
                if self.opt.wave {
                    let test_name = test.to_string();
                    let path = wave_output_path(&test_name, property.path, metadata);
                    metadata.add_generated_file(path);
                }
            } else {
                failure += 1;
            }
        }

        for dt in &doc_tests {
            let module_name = dt.module_name.to_string();
            info!("Executing doc test ({module_name})");

            match run_doc_test(
                &ir,
                &module_name,
                &dt.wavedrom_json,
                &dt.ports,
                self.opt.wave,
                dt.path,
                metadata,
                &config,
                &mut proto_cache,
            ) {
                Ok(wave_path) => {
                    info!("Succeeded doc test ({module_name})");
                    success += 1;
                    if let Some(path) = wave_path {
                        metadata.add_generated_file(path);
                    }
                }
                Err(e) => {
                    error!("Failed doc test ({module_name}): {e}");
                    failure += 1;
                }
            }
        }

        let ignored_msg = if ignored_count > 0 {
            format!(", {ignored_count} ignored")
        } else {
            String::new()
        };
        let summary = format!("Completed tests : {success} passed, {failure} failed{ignored_msg}");

        if self.opt.wave {
            metadata
                .save_build_info()
                .map_err(|e| miette::miette!("{e}"))?;
        }

        if failure == 0 {
            info!("{summary}");
            Ok(true)
        } else {
            error!("{summary}");
            Ok(false)
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn prepare_native_test(
    ir: &veryl_analyzer::ir::Ir,
    test_name: &str,
    top: &Option<resource_table::StrId>,
    opt: &OptTest,
    test_path: PathId,
    metadata: &Metadata,
    config: &Config,
    cache: &mut ProtoModuleCache,
) -> std::result::Result<NativeTestJob, SimulatorError> {
    let top_name = if let Some(top_str) = top {
        top_str.to_string()
    } else {
        test_name.to_string()
    };

    let top_str_id = resource_table::get_str_id(top_name.clone()).ok_or_else(|| {
        SimulatorError::TopModuleNotFound {
            module_name: top_name.clone(),
        }
    })?;
    let sim_ir = build_ir_cached(ir, top_str_id, config, cache)?;

    let module_name = sim_ir.name.to_string();

    let dump = if opt.wave {
        Some(create_wave_dumper(test_name, test_path, metadata)?)
    } else {
        None
    };

    Ok(NativeTestJob {
        test_name: test_name.to_string(),
        module_name,
        sim_ir,
        dump,
    })
}

#[allow(clippy::too_many_arguments)]
fn run_doc_test(
    ir: &veryl_analyzer::ir::Ir,
    module_name: &str,
    wavedrom_json: &str,
    ports: &[(String, String)],
    wave: bool,
    source_path: PathId,
    metadata: &Metadata,
    config: &Config,
    cache: &mut ProtoModuleCache,
) -> std::result::Result<Option<PathBuf>, SimulatorError> {
    let mut scenario = parse_wavedrom(wavedrom_json).map_err(|e| SimulatorError::TestFailed {
        message: format!("WaveDrom parse error in {module_name}: {e}"),
    })?;

    classify_signals(&mut scenario, ports);
    let top_str_id = resource_table::get_str_id(module_name.to_string()).ok_or_else(|| {
        SimulatorError::TopModuleNotFound {
            module_name: module_name.to_string(),
        }
    })?;
    let sim_ir = build_ir_cached(ir, top_str_id, config, cache)?;

    let dump = if wave {
        let doc_name = format!("{}_doc", module_name);
        Some(create_wave_dumper(&doc_name, source_path, metadata)?)
    } else {
        None
    };

    let mut sim = Simulator::new(sim_ir, dump);

    // Resolve clock event
    let fallback_clock = || {
        sim.ir
            .event_statements
            .keys()
            .find(|e| matches!(e, veryl_simulator::ir::Event::Clock(_)))
            .cloned()
            .unwrap_or(veryl_simulator::ir::Event::Initial)
    };
    let clock_event = scenario
        .signals
        .iter()
        .find(|s| s.kind == SignalKind::Clock)
        .and_then(|s| {
            sim.get_clock(&s.name)
                .or_else(|| sim.get_clock(&format!("i_{}", s.name)))
        })
        .unwrap_or_else(fallback_clock);

    // Resolve reset event
    let reset_event = sim
        .ir
        .event_statements
        .keys()
        .find(|e| matches!(e, veryl_simulator::ir::Event::Reset(_)))
        .cloned();

    remap_signal_names(&mut scenario, ports);

    // Build port width map (after remap so signal names match port names)
    let mut port_widths = std::collections::HashMap::new();
    for (port_name, _) in ports {
        let var_path: veryl_simulator::ir::VarPath = port_name.parse().unwrap();
        if let Some(var_id) = sim.ir.ports.get(&var_path)
            && let Some(var) = sim.ir.module_variables.variables.get(var_id)
        {
            port_widths.insert(port_name.clone(), var.width);
        }
    }

    let result = wavedrom::run_wavedrom_test(
        &mut sim,
        &scenario,
        &clock_event,
        &reset_event,
        3,
        &port_widths,
    );

    match result {
        TestResult::Pass => {
            let wave_path = sim.dump.and_then(|d| d.into_path());
            Ok(wave_path)
        }
        TestResult::Fail(msg) => Err(SimulatorError::TestFailed { message: msg }),
    }
}

/// Remap WaveDrom signal names to actual port names (e.g. "clk" -> "i_clk").
fn remap_signal_names(scenario: &mut wavedrom::WaveScenario, ports: &[(String, String)]) {
    for signal in &mut scenario.signals {
        if ports.iter().any(|(name, _)| name == &signal.name) {
            continue;
        }
        if let Some((port_name, _)) = ports
            .iter()
            .find(|(port_name, _)| wavedrom::strip_port_prefix(port_name) == signal.name)
        {
            signal.name = port_name.clone();
        }
    }
}