Merge pull request #18 from oscargus/cleanup

Cleanup
This commit is contained in:
Frans Skarman 2023-09-27 07:40:33 +00:00 committed by GitHub
commit fccd65ef8f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 118 additions and 107 deletions

View file

@ -18,14 +18,17 @@ fn main() -> std::io::Result<()> {
let args = Cli::parse();
use std::time::Instant;
let now = Instant::now();
let file = File::open(&args.path)?;
parse_vcd(file).unwrap();
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(())
}

View file

@ -4,18 +4,20 @@
// the root of the folder containing the sources for this program.
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) {
for _ in 0..indent {print!(" |");}
fn indented_print(indent: u8, name: &String) {
for _ in 0..indent {
print!(" |");
}
print!("---");
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.
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() {
} else {
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<()> {
use std::time::Instant;
// we start by printing out the entire signal tree of
// a parsed VCD
let now = Instant::now();
@ -53,8 +54,7 @@ fn main() -> std::io::Result<()> {
println!("Done Printing Scopes");
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
let now = Instant::now();
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];
for timestamp in timestamps {
let time = num::BigUint::from(timestamp);
let val = state_signal
.query_string_val_on_tmln(&time, &vcd)
.unwrap();
let val = state_signal.query_string_val_on_tmln(&time, &vcd).unwrap();
println!("Signal `{name}` has value `{val}` at time `{time}`");
}
Ok(())

View file

@ -5,8 +5,8 @@
mod 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::types::{Metadata, Timescale, Version};
pub use vcd::types::{ScopeIdx, SignalIdx, VCD};
pub use num::BigUint;

View file

@ -3,8 +3,8 @@
// and the YEHOWSHUA license, both of which can be found at
// the root of the folder containing the sources for this program.
mod reader;
pub(crate) mod types;
pub(crate) mod parse;
mod reader;
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.
mod combinator_atoms;
mod types;
mod events;
mod metadata;
mod scopes;
mod events;
mod types;
pub fn parse_vcd(file: impl std::io::Read) -> Result<super::types::VCD, String> {
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_scopes: vec![],
root_scopes: vec![],
largest_timestamp: None
largest_timestamp: None,
};
scopes::parse_scopes(&mut word_gen, &mut vcd, &mut signal_map)?;

View file

@ -6,15 +6,13 @@ use super::super::reader::{next_word, WordReader};
use super::types::ParseResult;
pub(super) fn digit(chr: u8) -> bool {
let zero = b'0' as u8;
let nine = b'9' as u8;
let zero = b'0';
let nine = b'9';
let between_zero_and_nine = (chr >= zero) && (nine >= chr);
return between_zero_and_nine;
(chr >= zero) && (nine >= chr)
}
pub(super) fn take_until<'a>(word: &'a str, pattern: u8) -> ParseResult<'a> {
pub(super) fn take_until(word: &str, pattern: u8) -> ParseResult<'_> {
let mut new_start = 0;
for chr in word.as_bytes() {
@ -25,13 +23,13 @@ pub(super) fn take_until<'a>(word: &'a str, pattern: u8) -> ParseResult<'a> {
}
}
return ParseResult {
ParseResult {
matched: &word[0..new_start],
residual: &word[new_start..],
};
}
}
pub(super) fn take_while<'a>(word: &'a str, cond: fn(u8) -> bool) -> ParseResult<'a> {
pub(super) fn take_while(word: &str, cond: fn(u8) -> bool) -> ParseResult<'_> {
let mut new_start = 0;
for chr in word.as_bytes() {
@ -42,10 +40,10 @@ pub(super) fn take_while<'a>(word: &'a str, cond: fn(u8) -> bool) -> ParseResult
}
}
return ParseResult {
ParseResult {
matched: &word[0..new_start],
residual: &word[new_start..],
};
}
}
pub(super) fn tag<'a>(word: &'a str, pattern: &'a str) -> ParseResult<'a> {
@ -63,10 +61,10 @@ pub(super) fn tag<'a>(word: &'a str, pattern: &'a str) -> ParseResult<'a> {
new_start += 1;
}
return ParseResult {
ParseResult {
matched: &word[0..new_start],
residual: &word[new_start..],
};
}
}
pub(super) fn ident<R: std::io::Read>(
@ -77,9 +75,9 @@ pub(super) fn ident<R: std::io::Read>(
let (word, cursor) = next_word!(word_reader)?;
if word == keyword {
return Ok(());
Ok(())
} else {
let err = format!("found keyword `{word}` but expected `{keyword}` on {cursor:?}");
return Err(err);
Err(err)
}
}

View file

@ -3,18 +3,17 @@
// and the YEHOWSHUA license, both of which can be found at
// the root of the folder containing the sources for this program.
use std::collections::HashMap;
use num::BigUint;
use std::collections::HashMap;
use super::super::utilities::{BinaryParserErrTypes, binary_str_to_vec_u8};
use super::super::signal::{SignalEnum, LsbIdxOfTmstmpValOnTmln};
use super::super::reader::{WordReader, Cursor, Line, Word, next_word};
use super::super::reader::{next_word, Cursor, Line, Word, WordReader};
use super::super::signal::{LsbIdxOfTmstmpValOnTmln, SignalEnum};
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<R: std::io::Read>(
word_reader: &mut WordReader<R>,
vcd: &'a mut VCD,
vcd: &mut VCD,
signal_map: &mut HashMap<String, SignalIdx>,
) -> Result<(), String> {
let mut curr_tmstmp_lsb_idx = 0u32;

View file

@ -5,11 +5,11 @@
use chrono::prelude::{DateTime, Utc};
use itertools::Itertools;
use super::super::reader::{Cursor, WordReader, next_word};
use super::super::types::{Timescale, Version, Metadata};
use super::super::reader::{next_word, Cursor, WordReader};
use super::super::types::{Metadata, Timescale, Version};
use super::combinator_atoms::{take_until, take_while, digit, tag};
use super::types::{ParseResult};
use super::combinator_atoms::{digit, tag, take_until, take_while};
use super::types::ParseResult;
pub(super) fn parse_date(
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();
loop {
@ -157,7 +159,7 @@ pub(super) fn parse_version<R: std::io::Read>(word_reader: &mut WordReader<R>) -
return Ok(Version(version));
} else {
version.push_str(word);
version.push_str(" ");
version.push(' ');
}
}
}
@ -177,7 +179,7 @@ pub(super) fn parse_timescale<R: std::io::Read>(
.map_err(|e| format!("Error near {}:{}. {e}", file!(), line!()))?;
let timescale = {
if residual == "" {
if residual.is_empty() {
let (word, _) = next_word!(word_reader)?;
let unit = match word {
"fs" => Ok(Timescale::Fs),
@ -217,10 +219,12 @@ pub(super) fn parse_timescale<R: std::io::Read>(
let (word, _) = next_word!(word_reader)?;
tag(word, "$end").assert_match()?;
return Ok(timescale);
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 {
date: None,
version: None,
@ -327,5 +331,5 @@ pub(super) fn parse_metadata<R: std::io::Read>(word_reader: &mut WordReader<R>)
_ => {}
}
}
return Ok(metadata);
Ok(metadata)
}

View file

@ -5,20 +5,19 @@
/// part of the vcd parser that handles parsing the signal tree and
/// building the resulting signal tree
use std::collections::HashMap;
use super::super::reader::{WordReader, next_word, curr_word};
use super::super::types::{VCD, Scope, ScopeIdx, SignalIdx};
use super::super::reader::{curr_word, next_word, WordReader};
use super::super::signal::{SigType, SignalEnum};
use super::super::types::{Scope, ScopeIdx, SignalIdx, VCD};
use super::combinator_atoms::{tag, ident};
use super::types::{ParseResult};
use super::combinator_atoms::{ident, tag};
use super::types::ParseResult;
pub(super) fn parse_var<'a, R: std::io::Read>(
pub(super) fn parse_var<R: std::io::Read>(
word_reader: &mut WordReader<R>,
parent_scope_idx: ScopeIdx,
vcd: &'a mut VCD,
vcd: &mut VCD,
signal_map: &mut HashMap<String, SignalIdx>,
path: &Vec<String>,
) -> Result<(), String> {
@ -71,7 +70,9 @@ pub(super) fn parse_var<'a, R: std::io::Read>(
| SigType::Wire
| SigType::Tri1
| SigType::Time => {
let num_bits = word.parse::<usize>().expect(parse_err.as_str());
let num_bits = word
.parse::<usize>()
.unwrap_or_else(|_| panic!("{}", parse_err));
let num_bits = u16::try_from(num_bits).map_err(|_| {
format!(
"Error near {}:{} while parsing vcd file at {cursor:?}. \
@ -98,7 +99,7 @@ pub(super) fn parse_var<'a, R: std::io::Read>(
let (word, _) = next_word!(word_reader)?;
match word {
"$end" => break,
_ => full_signal_name.push(word.to_string())
_ => full_signal_name.push(word.to_string()),
}
}
let full_signal_name = full_signal_name.join(" ");
@ -118,7 +119,11 @@ pub(super) fn parse_var<'a, R: std::io::Read>(
let signal_idx = SignalIdx(vcd.all_signals.len());
let signal = SignalEnum::Alias {
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, signal_idx)
@ -128,7 +133,11 @@ pub(super) fn parse_var<'a, R: std::io::Read>(
signal_map.insert(signal_alias.to_string(), signal_idx);
let signal = SignalEnum::Data {
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,
signal_error: None,
num_bits,
@ -155,9 +164,9 @@ pub(super) fn parse_var<'a, R: std::io::Read>(
/// Sometimes, variables can be listed outside of scopes.
/// We call these orphaned vars.
fn parse_orphaned_vars<'a, R: std::io::Read>(
fn parse_orphaned_vars<R: std::io::Read>(
word_reader: &mut WordReader<R>,
vcd: &'a mut VCD,
vcd: &mut VCD,
signal_map: &mut HashMap<String, SignalIdx>,
) -> Result<(), String> {
// create scope for unscoped signals if such a scope does not
@ -218,12 +227,12 @@ fn parse_orphaned_vars<'a, R: std::io::Read>(
Ok(())
}
fn parse_scopes_inner<'a, R: std::io::Read>(
fn parse_scopes_inner<R: std::io::Read>(
word_reader: &mut WordReader<R>,
parent_scope_idx: Option<ScopeIdx>,
vcd: &'a mut VCD,
vcd: &mut VCD,
signal_map: &mut HashMap<String, SignalIdx>,
path: &Vec<String>
path: &Vec<String>,
) -> Result<(), String> {
// $scope module reg_mag_i $end
// ^^^^^^ - module keyword
@ -275,8 +284,6 @@ fn parse_scopes_inner<'a, R: std::io::Read>(
// ^^^^ - end keyword
ident(word_reader, "$end")?;
loop {
let (word, cursor) = next_word!(word_reader)?;
let ParseResult { matched, residual } = tag(word, "$");
@ -286,7 +293,13 @@ fn parse_scopes_inner<'a, R: std::io::Read>(
match residual {
"scope" => {
// 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" => {
parse_var(word_reader, curr_scope_idx, vcd, signal_map, &path)?;
@ -330,9 +343,9 @@ fn parse_scopes_inner<'a, R: std::io::Read>(
Ok(())
}
pub(super) fn parse_scopes<'a, R: std::io::Read>(
pub(super) fn parse_scopes<R: std::io::Read>(
word_reader: &mut WordReader<R>,
vcd: &'a mut VCD,
vcd: &mut VCD,
signal_map: &mut HashMap<String, SignalIdx>,
) -> Result<(), String> {
// get the current word

View file

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

View file

@ -319,7 +319,7 @@ impl SignalEnum {
fn bits_required(&self) -> Option<u16> {
match self {
SignalEnum::Data { num_bits, .. } => num_bits.clone(),
SignalEnum::Data { num_bits, .. } => *num_bits,
// TODO: Follow aliases?
SignalEnum::Alias { .. } => None,
}

View file

@ -4,9 +4,9 @@
// This program is distributed under both the GPLV3 license
// and the YEHOWSHUA license, both of which can be found at
// the root of the folder containing the sources for this program.
use super::signal::{Signal, SignalEnum};
use chrono::prelude::{DateTime, Utc};
use num::BigUint;
use super::signal::{Signal, SignalEnum};
#[derive(Debug)]
pub struct Version(pub String);
@ -63,7 +63,7 @@ pub struct VCD {
pub(super) all_signals: Vec<SignalEnum>,
pub(super) all_scopes: Vec<Scope>,
pub(super) root_scopes: Vec<ScopeIdx>,
pub(super) largest_timestamp: Option<BigUint>
pub(super) largest_timestamp: Option<BigUint>,
}
impl VCD {
@ -85,7 +85,7 @@ impl VCD {
let scope = &self.all_scopes[idx];
&scope.name
}
pub fn signal_from_signal_idx<'a>(&'a self, idx: SignalIdx) -> Signal<'a> {
pub fn signal_from_signal_idx(&self, idx: SignalIdx) -> Signal<'_> {
let SignalIdx(idx) = idx;
let signal_enum = &self.all_signals[idx];
return Signal(signal_enum);
@ -106,7 +106,7 @@ impl VCD {
// dereference signal if Signal::Alias, or keep idx if Signal::Data
let signal_idx = match signal {
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

View file

@ -38,7 +38,7 @@ fn base2_str_to_byte(word: &[u8]) -> Result<u8, BinaryParserErrTypes> {
for (idx, chr) in word.iter().rev().enumerate() {
match chr {
b'1' => val = bit_lut[idx] | val,
b'1' => val |= bit_lut[idx],
b'0' => {}
b'x' | b'X' => return Err(BinaryParserErrTypes::XValue),
b'z' | b'Z' => return Err(BinaryParserErrTypes::ZValue),
@ -73,13 +73,13 @@ pub(super) fn binary_str_to_vec_u8(binary_str: &str) -> Result<Vec<u8>, BinaryPa
if head_idx < 8 {
head_idx = 0
} else {
head_idx = head_idx - 8;
head_idx -= 8;
}
if tail_idx < 8 {
tail_idx = 0
} else {
tail_idx = tail_idx - 8;
tail_idx -= 8;
}
}
Ok(vec_u8)

View file

@ -5,7 +5,7 @@
// TODO: we should eventually be able to only test on just
// the files const
pub const FILES : [&str; 31] = [
pub const FILES: [&str; 31] = [
"./tests/vcd-files/aldec/SPI_Write.vcd",
"./tests/vcd-files/ghdl/alu.vcd",
"./tests/vcd-files/ghdl/idea.vcd",
@ -40,7 +40,7 @@ pub const FILES : [&str; 31] = [
"./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/ghdl/alu.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/xilinx_isim/test.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/quartus/mipsHardware.vcd",
"./test-vcd-files/quartus/wave_registradores.vcd",

View file

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