remove some dead code in comments and add querying values on timeline back to vcd example

This commit is contained in:
Yehowshua Immanuel 2022-10-26 02:19:59 -04:00
parent 9f18b166a5
commit b4cbbf29cc
4 changed files with 51 additions and 80 deletions

View file

@ -4,7 +4,7 @@
// the root of the folder containing the sources for this program.
use std::fs::File;
use fastwave_backend::{ScopeIdx, VCD, parse_vcd};
use fastwave_backend::{ScopeIdx, VCD, parse_vcd, SignalIdx};
fn indented_print(indent : u8, name : &String) {
for _ in 0..indent {print!(" |");}
@ -12,29 +12,26 @@ fn indented_print(indent : u8, name : &String) {
println!("{name}");
}
fn print_root_scope_tree(root_idx: ScopeIdx, vcd: &VCD, indent : u8) {
if vcd.child_scopes_by_idx(root_idx).is_empty() {
} else {
for child_scope_idx in vcd.child_scopes_by_idx(root_idx) {
indented_print(indent, vcd.scope_name_by_idx(child_scope_idx));
// for signal_idx in vcd.get_children_signal_idxs(child_scope_idx) {
// let signal = vcd.try_signal_idx_to_signal(signal_idx).unwrap();
// match signal {
// Signal::Data {..} => {}
// Signal::Alias {..} => {}
// }
// // let to_print = format!("{},{}", signal.name(), )
// }
// vcd.try_signal_idx_to_signal(idx)
print_root_scope_tree(child_scope_idx, vcd.clone(), indent + 1);
// TODO: refactor into more general visitor pattern that takes a
// function as an argument.
fn visit_all_scopes(vcd: &VCD) {
fn visit_all_scope_children(root_idx: ScopeIdx, vcd: &VCD, indent : u8) {
if vcd.child_scopes_by_idx(root_idx).is_empty() {
} else {
for child_scope_idx in vcd.child_scopes_by_idx(root_idx) {
indented_print(indent, vcd.scope_name_by_idx(child_scope_idx));
for signal_idx in vcd.get_children_signal_idxs(child_scope_idx) {
let signal = vcd.signal_from_signal_idx(signal_idx);
let SignalIdx(idx) = signal_idx;
indented_print(indent + 1, &format!("{},{}", signal.name(), idx));
}
visit_all_scope_children(child_scope_idx, vcd.clone(), indent + 1);
}
}
}
}
fn ui_all_scopes(vcd: &VCD) {
for root_scope_idx in vcd.root_scopes_by_idx() {
indented_print(0, vcd.scope_name_by_idx(root_scope_idx));
print_root_scope_tree(root_scope_idx, vcd, 1u8);
visit_all_scope_children(root_scope_idx, vcd, 1u8);
}
}
@ -42,6 +39,8 @@ fn main() -> std::io::Result<()> {
use std::time::Instant;
// we start by printing out the entire signal tree of
// a parsed VCD
let now = Instant::now();
let file_path = "tests/vcd-files/icarus/CPU.vcd";
let file = File::open(file_path)?;
@ -50,25 +49,36 @@ fn main() -> std::io::Result<()> {
println!("Parsed VCD file {} : {:.2?}", file_path, elapsed);
println!("Printing Scopes");
ui_all_scopes(&vcd);
visit_all_scopes(&vcd);
println!("Done Printing Scopes");
println!();
// we then parse another VCD, print its signal tree and
// query some values on its timeline
let now = Instant::now();
let file_path = "tests/vcd-files/amaranth/up_counter.vcd";
let file = File::open(file_path)?;
let vcd = parse_vcd(file).unwrap();
// let state_signal = vcd.all_si
// for signal_idx in vcd.si
// let name = state_signal.name();
// let time = BigUint::from(57760000u32);
// let val = state_signal
// .query_string_val_on_tmln(
// &time,
// &vcd.tmstmps_encoded_as_u8s,
// &vcd.all_signals,
// )
// .unwrap();
// println!("Signal `{name}` has value `{val}` at time `{time}`");
let elapsed = now.elapsed();
println!("Parsed VCD file {} : {:.2?}", file_path, elapsed);
println!("Printing Scopes");
visit_all_scopes(&vcd);
println!("Done Printing Scopes");
let state_signal = vcd.signal_from_signal_idx(SignalIdx(4));
let name = state_signal.name();
let timestamps = vec![31499_000u32, 31500_000u32, 57760_000u32];
for timestamp in timestamps {
let time = num::BigUint::from(timestamp);
let val = state_signal
.query_string_val_on_tmln(&time, &vcd)
.unwrap();
println!("Signal `{name}` has value `{val}` at time `{time}`");
}
Ok(())
}