Run cargo-fmt

This commit is contained in:
Oscar Gustafsson 2023-09-26 13:16:14 +02:00
parent 22eaf8da15
commit 5758e77371
12 changed files with 81 additions and 70 deletions

View file

@ -18,14 +18,17 @@ fn main() -> std::io::Result<()> {
let args = Cli::parse(); let args = Cli::parse();
use std::time::Instant; use std::time::Instant;
let now = Instant::now(); let now = Instant::now();
let file = File::open(&args.path)?; let file = File::open(&args.path)?;
parse_vcd(file).unwrap(); parse_vcd(file).unwrap();
let elapsed = now.elapsed(); let elapsed = now.elapsed();
println!("Parsed VCD file {} : {:.2?}", &args.path.as_os_str().to_str().unwrap(), elapsed); println!(
"Parsed VCD file {} : {:.2?}",
&args.path.as_os_str().to_str().unwrap(),
elapsed
);
Ok(()) Ok(())
} }

View file

@ -4,18 +4,20 @@
// the root of the folder containing the sources for this program. // the root of the folder containing the sources for this program.
use std::fs::File; use std::fs::File;
use fastwave_backend::{ScopeIdx, VCD, parse_vcd, SignalIdx}; use fastwave_backend::{parse_vcd, ScopeIdx, SignalIdx, VCD};
fn indented_print(indent : u8, name : &String) { fn indented_print(indent: u8, name: &String) {
for _ in 0..indent {print!(" |");} for _ in 0..indent {
print!(" |");
}
print!("---"); print!("---");
println!("{name}"); println!("{name}");
} }
// TODO: refactor into more general visitor pattern that takes a // TODO: refactor into more general visitor pattern that takes a
// function as an argument. // function as an argument.
fn visit_all_scopes(vcd: &VCD) { fn visit_all_scopes(vcd: &VCD) {
fn visit_all_scope_children(root_idx: ScopeIdx, vcd: &VCD, indent : u8) { fn visit_all_scope_children(root_idx: ScopeIdx, vcd: &VCD, indent: u8) {
if vcd.child_scopes_by_idx(root_idx).is_empty() { if vcd.child_scopes_by_idx(root_idx).is_empty() {
} else { } else {
for child_scope_idx in vcd.child_scopes_by_idx(root_idx) { for child_scope_idx in vcd.child_scopes_by_idx(root_idx) {
@ -36,9 +38,8 @@ fn visit_all_scopes(vcd: &VCD) {
} }
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
use std::time::Instant; use std::time::Instant;
// we start by printing out the entire signal tree of // we start by printing out the entire signal tree of
// a parsed VCD // a parsed VCD
let now = Instant::now(); let now = Instant::now();
@ -53,8 +54,7 @@ fn main() -> std::io::Result<()> {
println!("Done Printing Scopes"); println!("Done Printing Scopes");
println!(); println!();
// we then parse another VCD, print its signal tree and
// we then parse another VCD, print its signal tree and
// query some values on its timeline // query some values on its timeline
let now = Instant::now(); let now = Instant::now();
let file_path = "tests/vcd-files/amaranth/up_counter.vcd"; let file_path = "tests/vcd-files/amaranth/up_counter.vcd";
@ -73,11 +73,8 @@ fn main() -> std::io::Result<()> {
let timestamps = vec![31499_000u32, 31500_000u32, 57760_000u32]; let timestamps = vec![31499_000u32, 31500_000u32, 57760_000u32];
for timestamp in timestamps { for timestamp in timestamps {
let time = num::BigUint::from(timestamp); let time = num::BigUint::from(timestamp);
let val = state_signal let val = state_signal.query_string_val_on_tmln(&time, &vcd).unwrap();
.query_string_val_on_tmln(&time, &vcd)
.unwrap();
println!("Signal `{name}` has value `{val}` at time `{time}`"); println!("Signal `{name}` has value `{val}` at time `{time}`");
} }
Ok(()) Ok(())

View file

@ -5,8 +5,8 @@
mod vcd; mod vcd;
pub use vcd::parse::parse_vcd; pub use vcd::parse::parse_vcd;
pub use vcd::types::{ScopeIdx, SignalIdx, VCD};
pub use vcd::types::{Metadata, Timescale, Version};
pub use vcd::signal::{Signal, SignalValue}; pub use vcd::signal::{Signal, SignalValue};
pub use vcd::types::{Metadata, Timescale, Version};
pub use vcd::types::{ScopeIdx, SignalIdx, VCD};
pub use num::BigUint; pub use num::BigUint;

View file

@ -3,8 +3,8 @@
// and the YEHOWSHUA license, both of which can be found at // and the YEHOWSHUA license, both of which can be found at
// the root of the folder containing the sources for this program. // the root of the folder containing the sources for this program.
mod reader;
pub(crate) mod types;
pub(crate) mod parse; pub(crate) mod parse;
mod reader;
pub(crate) mod signal; pub(crate) mod signal;
mod utilities; pub(crate) mod types;
mod utilities;

View file

@ -4,11 +4,10 @@
// the root of the folder containing the sources for this program. // the root of the folder containing the sources for this program.
mod combinator_atoms; mod combinator_atoms;
mod types; mod events;
mod metadata; mod metadata;
mod scopes; mod scopes;
mod events; mod types;
pub fn parse_vcd(file: impl std::io::Read) -> Result<super::types::VCD, String> { pub fn parse_vcd(file: impl std::io::Read) -> Result<super::types::VCD, String> {
let mut word_gen = super::reader::WordReader::new(file); let mut word_gen = super::reader::WordReader::new(file);
@ -26,7 +25,7 @@ pub fn parse_vcd(file: impl std::io::Read) -> Result<super::types::VCD, String>
all_signals: vec![], all_signals: vec![],
all_scopes: vec![], all_scopes: vec![],
root_scopes: vec![], root_scopes: vec![],
largest_timestamp: None largest_timestamp: None,
}; };
scopes::parse_scopes(&mut word_gen, &mut vcd, &mut signal_map)?; scopes::parse_scopes(&mut word_gen, &mut vcd, &mut signal_map)?;

View file

@ -3,14 +3,13 @@
// and the YEHOWSHUA license, both of which can be found at // and the YEHOWSHUA license, both of which can be found at
// the root of the folder containing the sources for this program. // the root of the folder containing the sources for this program.
use std::collections::HashMap;
use num::BigUint; use num::BigUint;
use std::collections::HashMap;
use super::super::utilities::{BinaryParserErrTypes, binary_str_to_vec_u8}; use super::super::reader::{next_word, Cursor, Line, Word, WordReader};
use super::super::signal::{SignalEnum, LsbIdxOfTmstmpValOnTmln}; use super::super::signal::{LsbIdxOfTmstmpValOnTmln, SignalEnum};
use super::super::reader::{WordReader, Cursor, Line, Word, next_word};
use super::super::types::{SignalIdx, VCD}; use super::super::types::{SignalIdx, VCD};
use super::super::utilities::{binary_str_to_vec_u8, BinaryParserErrTypes};
pub(super) fn parse_events<'a, R: std::io::Read>( pub(super) fn parse_events<'a, R: std::io::Read>(
word_reader: &mut WordReader<R>, word_reader: &mut WordReader<R>,

View file

@ -5,11 +5,11 @@
use chrono::prelude::{DateTime, Utc}; use chrono::prelude::{DateTime, Utc};
use itertools::Itertools; use itertools::Itertools;
use super::super::reader::{Cursor, WordReader, next_word}; use super::super::reader::{next_word, Cursor, WordReader};
use super::super::types::{Timescale, Version, Metadata}; use super::super::types::{Metadata, Timescale, Version};
use super::combinator_atoms::{take_until, take_while, digit, tag}; use super::combinator_atoms::{digit, tag, take_until, take_while};
use super::types::{ParseResult}; use super::types::ParseResult;
pub(super) fn parse_date( pub(super) fn parse_date(
word_and_ctx1: (&str, &Cursor), word_and_ctx1: (&str, &Cursor),
@ -145,7 +145,9 @@ pub(super) fn parse_date(
)) ))
} }
pub(super) fn parse_version<R: std::io::Read>(word_reader: &mut WordReader<R>) -> Result<Version, String> { pub(super) fn parse_version<R: std::io::Read>(
word_reader: &mut WordReader<R>,
) -> Result<Version, String> {
let mut version = String::new(); let mut version = String::new();
loop { loop {
@ -220,7 +222,9 @@ pub(super) fn parse_timescale<R: std::io::Read>(
return Ok(timescale); return Ok(timescale);
} }
pub(super) fn parse_metadata<R: std::io::Read>(word_reader: &mut WordReader<R>) -> Result<Metadata, String> { pub(super) fn parse_metadata<R: std::io::Read>(
word_reader: &mut WordReader<R>,
) -> Result<Metadata, String> {
let mut metadata = Metadata { let mut metadata = Metadata {
date: None, date: None,
version: None, version: None,

View file

@ -5,15 +5,14 @@
/// part of the vcd parser that handles parsing the signal tree and /// part of the vcd parser that handles parsing the signal tree and
/// building the resulting signal tree /// building the resulting signal tree
use std::collections::HashMap; use std::collections::HashMap;
use super::super::reader::{WordReader, next_word, curr_word}; use super::super::reader::{curr_word, next_word, WordReader};
use super::super::types::{VCD, Scope, ScopeIdx, SignalIdx};
use super::super::signal::{SigType, SignalEnum}; use super::super::signal::{SigType, SignalEnum};
use super::super::types::{Scope, ScopeIdx, SignalIdx, VCD};
use super::combinator_atoms::{tag, ident}; use super::combinator_atoms::{ident, tag};
use super::types::{ParseResult}; use super::types::ParseResult;
pub(super) fn parse_var<'a, R: std::io::Read>( pub(super) fn parse_var<'a, R: std::io::Read>(
word_reader: &mut WordReader<R>, word_reader: &mut WordReader<R>,
@ -98,7 +97,7 @@ pub(super) fn parse_var<'a, R: std::io::Read>(
let (word, _) = next_word!(word_reader)?; let (word, _) = next_word!(word_reader)?;
match word { match word {
"$end" => break, "$end" => break,
_ => full_signal_name.push(word.to_string()) _ => full_signal_name.push(word.to_string()),
} }
} }
let full_signal_name = full_signal_name.join(" "); let full_signal_name = full_signal_name.join(" ");
@ -118,7 +117,11 @@ pub(super) fn parse_var<'a, R: std::io::Read>(
let signal_idx = SignalIdx(vcd.all_signals.len()); let signal_idx = SignalIdx(vcd.all_signals.len());
let signal = SignalEnum::Alias { let signal = SignalEnum::Alias {
name: full_signal_name.clone(), name: full_signal_name.clone(),
path: path.iter().cloned().chain([full_signal_name]).collect::<Vec<String>>(), path: path
.iter()
.cloned()
.chain([full_signal_name])
.collect::<Vec<String>>(),
signal_alias: *ref_signal_idx, signal_alias: *ref_signal_idx,
}; };
(signal, signal_idx) (signal, signal_idx)
@ -128,7 +131,11 @@ pub(super) fn parse_var<'a, R: std::io::Read>(
signal_map.insert(signal_alias.to_string(), signal_idx); signal_map.insert(signal_alias.to_string(), signal_idx);
let signal = SignalEnum::Data { let signal = SignalEnum::Data {
name: full_signal_name.clone(), name: full_signal_name.clone(),
path: path.iter().cloned().chain([full_signal_name]).collect::<Vec<String>>(), path: path
.iter()
.cloned()
.chain([full_signal_name])
.collect::<Vec<String>>(),
sig_type: var_type, sig_type: var_type,
signal_error: None, signal_error: None,
num_bits, num_bits,
@ -223,7 +230,7 @@ fn parse_scopes_inner<'a, R: std::io::Read>(
parent_scope_idx: Option<ScopeIdx>, parent_scope_idx: Option<ScopeIdx>,
vcd: &'a mut VCD, vcd: &'a mut VCD,
signal_map: &mut HashMap<String, SignalIdx>, signal_map: &mut HashMap<String, SignalIdx>,
path: &Vec<String> path: &Vec<String>,
) -> Result<(), String> { ) -> Result<(), String> {
// $scope module reg_mag_i $end // $scope module reg_mag_i $end
// ^^^^^^ - module keyword // ^^^^^^ - module keyword
@ -275,8 +282,6 @@ fn parse_scopes_inner<'a, R: std::io::Read>(
// ^^^^ - end keyword // ^^^^ - end keyword
ident(word_reader, "$end")?; ident(word_reader, "$end")?;
loop { loop {
let (word, cursor) = next_word!(word_reader)?; let (word, cursor) = next_word!(word_reader)?;
let ParseResult { matched, residual } = tag(word, "$"); let ParseResult { matched, residual } = tag(word, "$");
@ -286,7 +291,13 @@ fn parse_scopes_inner<'a, R: std::io::Read>(
match residual { match residual {
"scope" => { "scope" => {
// recursive - parse inside of current scope tree // recursive - parse inside of current scope tree
parse_scopes_inner(word_reader, Some(curr_scope_idx), vcd, signal_map, &path)?; parse_scopes_inner(
word_reader,
Some(curr_scope_idx),
vcd,
signal_map,
&path,
)?;
} }
"var" => { "var" => {
parse_var(word_reader, curr_scope_idx, vcd, signal_map, &path)?; parse_var(word_reader, curr_scope_idx, vcd, signal_map, &path)?;

View file

@ -4,26 +4,24 @@
// the root of the folder containing the sources for this program. // the root of the folder containing the sources for this program.
#[derive(Debug)] #[derive(Debug)]
pub(super) struct ParseResult<'a> { pub(super) struct ParseResult<'a> {
pub(super) matched : &'a str, pub(super) matched: &'a str,
pub(super) residual : &'a str} pub(super) residual: &'a str,
}
impl<'a> ParseResult<'a> { impl<'a> ParseResult<'a> {
pub(super) fn assert_match(&self) -> Result<&str, String> {
pub(super) fn assert_match(& self) -> Result<&str, String> {
if self.matched == "" { if self.matched == "" {
return Err("no match".to_string()) return Err("no match".to_string());
} } else {
else { return Ok(self.matched);
return Ok(self.matched)
} }
} }
pub(super) fn assert_residual(& self) -> Result<&str, String> { pub(super) fn assert_residual(&self) -> Result<&str, String> {
if self.residual == "" { if self.residual == "" {
return Err("no residual".to_string()) return Err("no residual".to_string());
} } else {
else { return Ok(self.residual);
return Ok(self.residual)
} }
} }
} }

View file

@ -4,9 +4,9 @@
// This program is distributed under both the GPLV3 license // This program is distributed under both the GPLV3 license
// and the YEHOWSHUA license, both of which can be found at // and the YEHOWSHUA license, both of which can be found at
// the root of the folder containing the sources for this program. // the root of the folder containing the sources for this program.
use super::signal::{Signal, SignalEnum};
use chrono::prelude::{DateTime, Utc}; use chrono::prelude::{DateTime, Utc};
use num::BigUint; use num::BigUint;
use super::signal::{Signal, SignalEnum};
#[derive(Debug)] #[derive(Debug)]
pub struct Version(pub String); pub struct Version(pub String);
@ -63,7 +63,7 @@ pub struct VCD {
pub(super) all_signals: Vec<SignalEnum>, pub(super) all_signals: Vec<SignalEnum>,
pub(super) all_scopes: Vec<Scope>, pub(super) all_scopes: Vec<Scope>,
pub(super) root_scopes: Vec<ScopeIdx>, pub(super) root_scopes: Vec<ScopeIdx>,
pub(super) largest_timestamp: Option<BigUint> pub(super) largest_timestamp: Option<BigUint>,
} }
impl VCD { impl VCD {
@ -106,7 +106,7 @@ impl VCD {
// dereference signal if Signal::Alias, or keep idx if Signal::Data // dereference signal if Signal::Alias, or keep idx if Signal::Data
let signal_idx = match signal { let signal_idx = match signal {
SignalEnum::Data { self_idx, .. } => *self_idx, SignalEnum::Data { self_idx, .. } => *self_idx,
SignalEnum::Alias {signal_alias, .. } => *signal_alias, SignalEnum::Alias { signal_alias, .. } => *signal_alias,
}; };
// Should now point to Signal::Data variant, or else there's an error // Should now point to Signal::Data variant, or else there's an error

View file

@ -5,7 +5,7 @@
// TODO: we should eventually be able to only test on just // TODO: we should eventually be able to only test on just
// the files const // the files const
pub const FILES : [&str; 31] = [ pub const FILES: [&str; 31] = [
"./tests/vcd-files/aldec/SPI_Write.vcd", "./tests/vcd-files/aldec/SPI_Write.vcd",
"./tests/vcd-files/ghdl/alu.vcd", "./tests/vcd-files/ghdl/alu.vcd",
"./tests/vcd-files/ghdl/idea.vcd", "./tests/vcd-files/ghdl/idea.vcd",
@ -40,7 +40,7 @@ pub const FILES : [&str; 31] = [
"./tests/vcd-files/scope_with_comment.vcd", "./tests/vcd-files/scope_with_comment.vcd",
]; ];
pub const GOOD_DATE_FILES : [&str; 24] = [ pub const GOOD_DATE_FILES: [&str; 24] = [
"./test-vcd-files/aldec/SPI_Write.vcd", "./test-vcd-files/aldec/SPI_Write.vcd",
"./test-vcd-files/ghdl/alu.vcd", "./test-vcd-files/ghdl/alu.vcd",
"./test-vcd-files/ghdl/idea.vcd", "./test-vcd-files/ghdl/idea.vcd",
@ -64,10 +64,10 @@ pub const GOOD_DATE_FILES : [&str; 24] = [
"./test-vcd-files/verilator/vlt_dump.vcd", "./test-vcd-files/verilator/vlt_dump.vcd",
"./test-vcd-files/xilinx_isim/test.vcd", "./test-vcd-files/xilinx_isim/test.vcd",
"./test-vcd-files/xilinx_isim/test1.vcd", "./test-vcd-files/xilinx_isim/test1.vcd",
"./test-vcd-files/xilinx_isim/test2x2_regex22_string1.vcd" "./test-vcd-files/xilinx_isim/test2x2_regex22_string1.vcd",
]; ];
pub const BAD_DATE_FILES : [&str; 6] = [ pub const BAD_DATE_FILES: [&str; 6] = [
"./test-vcd-files/ncsim/ffdiv_32bit_tb.vcd", "./test-vcd-files/ncsim/ffdiv_32bit_tb.vcd",
"./test-vcd-files/quartus/mipsHardware.vcd", "./test-vcd-files/quartus/mipsHardware.vcd",
"./test-vcd-files/quartus/wave_registradores.vcd", "./test-vcd-files/quartus/wave_registradores.vcd",

View file

@ -19,4 +19,4 @@ fn parse_all_VCDs() {
vcd.unwrap(); vcd.unwrap();
} }
} }
} }