2022-08-01 20:52:42 +00:00
|
|
|
use num::BigInt;
|
2022-08-07 01:40:33 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::fs::File;
|
2022-06-04 01:06:46 +00:00
|
|
|
|
2022-06-19 13:44:57 +00:00
|
|
|
use super::*;
|
2022-06-13 02:52:24 +00:00
|
|
|
|
2022-06-19 13:44:57 +00:00
|
|
|
mod combinator_atoms;
|
|
|
|
use combinator_atoms::*;
|
2022-06-04 01:06:46 +00:00
|
|
|
|
2022-06-19 13:44:57 +00:00
|
|
|
mod types;
|
|
|
|
use types::*;
|
2022-06-17 22:16:51 +00:00
|
|
|
|
2022-06-22 01:06:51 +00:00
|
|
|
mod metadata;
|
|
|
|
use metadata::*;
|
2022-06-04 01:06:46 +00:00
|
|
|
|
2022-07-18 16:53:44 +00:00
|
|
|
mod scopes;
|
|
|
|
use scopes::*;
|
2022-07-16 00:48:02 +00:00
|
|
|
|
2022-08-02 20:40:40 +00:00
|
|
|
mod events;
|
|
|
|
use events::*;
|
2022-07-26 01:16:15 +00:00
|
|
|
|
2022-08-07 01:40:33 +00:00
|
|
|
use std::cmp::Ordering;
|
|
|
|
|
|
|
|
fn compare_strs(a: &str, b: &str) -> Ordering {
|
|
|
|
let last_idx = if a.len() > b.len() { a.len() } else { b.len() };
|
|
|
|
// let last_idx += -1;
|
|
|
|
Ordering::Less
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ordered_binary_lookup(map: &Vec<(String, SignalIdx)>, key: &str) -> Result<SignalIdx, String> {
|
|
|
|
let mut upper_idx = map.len() - 1;
|
|
|
|
let mut lower_idx = 0usize;
|
|
|
|
|
|
|
|
while lower_idx <= upper_idx {
|
|
|
|
let mid_idx = lower_idx + ((upper_idx - lower_idx) / 2);
|
|
|
|
let (str_val, signal_idx) = map.get(mid_idx).unwrap();
|
|
|
|
let ordering = key.partial_cmp(str_val.as_str()).unwrap();
|
|
|
|
|
|
|
|
match ordering {
|
|
|
|
Ordering::Less => {
|
|
|
|
upper_idx = mid_idx - 1;
|
|
|
|
}
|
|
|
|
Ordering::Equal => {
|
|
|
|
return Ok(*signal_idx);
|
|
|
|
}
|
|
|
|
Ordering::Greater => {
|
|
|
|
lower_idx = mid_idx + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Err(format!(
|
|
|
|
"Error near {}:{}. Unable to find key: `{key}` in the map.",
|
|
|
|
file!(),
|
|
|
|
line!()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_vcd(file: File) -> Result<VCD, String> {
|
2022-06-04 01:06:46 +00:00
|
|
|
let mut word_gen = WordReader::new(file);
|
|
|
|
|
2022-07-14 20:46:11 +00:00
|
|
|
let header = parse_metadata(&mut word_gen)?;
|
2022-07-13 00:02:45 +00:00
|
|
|
|
2022-08-07 01:40:33 +00:00
|
|
|
// later, we'll need to map parsed ascii symbols to their
|
2022-07-26 01:16:15 +00:00
|
|
|
// respective signal indexes
|
2022-07-14 20:46:11 +00:00
|
|
|
let mut signal_map = std::collections::HashMap::new();
|
2022-07-13 00:02:45 +00:00
|
|
|
|
2022-08-02 23:31:35 +00:00
|
|
|
// after we parse metadata, we form the VCD object
|
2022-08-07 01:40:33 +00:00
|
|
|
let mut vcd = VCD {
|
|
|
|
metadata: header,
|
|
|
|
timeline: vec![],
|
|
|
|
timeline_markers: vec![],
|
|
|
|
all_signals: vec![],
|
|
|
|
all_scopes: vec![],
|
|
|
|
scope_roots: vec![],
|
2022-07-13 00:02:45 +00:00
|
|
|
};
|
2022-07-14 20:46:11 +00:00
|
|
|
|
2022-08-02 23:31:35 +00:00
|
|
|
parse_scopes(&mut word_gen, &mut vcd, &mut signal_map)?;
|
2022-08-07 01:40:33 +00:00
|
|
|
|
|
|
|
// the signal map should not contain any empty string
|
|
|
|
for (k, v) in &signal_map {
|
|
|
|
if k.len() == 0 {
|
|
|
|
return Err(format!("Critical error near {}:{}. There should be no empty strings in vcd string -> SignalIdx hashmap.", file!(), line!()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now that we've parsed all scopes and filled the hashmap
|
|
|
|
// with signals, we convert hashmap to an ordered vector
|
|
|
|
let mut signal_map1: Vec<(String, SignalIdx)> = signal_map
|
|
|
|
.iter()
|
|
|
|
.map(|(string, idx)| (string.clone(), idx.clone()))
|
|
|
|
.collect();
|
|
|
|
signal_map1.sort_by(|a: &(String, SignalIdx), b: &(String, SignalIdx)| {
|
|
|
|
let a = &a.0;
|
|
|
|
let b = &b.0;
|
|
|
|
a.partial_cmp(&b).unwrap()
|
|
|
|
});
|
|
|
|
|
|
|
|
let now = std::time::Instant::now();
|
|
|
|
for (k, v) in &signal_map1 {
|
|
|
|
let signal_idx = ordered_binary_lookup(&signal_map1, k.as_str())?;
|
|
|
|
assert!(*v == signal_idx);
|
|
|
|
}
|
|
|
|
let ordered_binary_search_elapsed = now.elapsed();
|
|
|
|
println!(
|
|
|
|
"ordered_binary_search_elapsed: {:.2?}",
|
|
|
|
ordered_binary_search_elapsed
|
|
|
|
);
|
|
|
|
|
|
|
|
// parse_events(&mut wosrd_gen, &mut vcd, &mut signal_map)?;
|
2022-07-16 00:48:02 +00:00
|
|
|
|
|
|
|
Ok(vcd)
|
2022-06-18 05:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2022-06-24 00:54:27 +00:00
|
|
|
use crate::test;
|
2022-06-18 05:00:01 +00:00
|
|
|
use std::fs::File;
|
|
|
|
#[test]
|
|
|
|
fn headers() {
|
2022-06-25 02:22:55 +00:00
|
|
|
// TODO: eventually, once all dates pass, merge the following
|
|
|
|
// two loops
|
|
|
|
// testing dates
|
2022-08-02 23:31:35 +00:00
|
|
|
for file in test::GOOD_DATE_FILES {
|
2022-08-07 01:40:33 +00:00
|
|
|
let metadata = parse_metadata(&mut WordReader::new(File::open(file).unwrap()));
|
2022-06-18 05:00:01 +00:00
|
|
|
assert!(metadata.is_ok());
|
|
|
|
assert!(metadata.unwrap().date.is_some());
|
|
|
|
}
|
|
|
|
|
2022-08-02 23:31:35 +00:00
|
|
|
for file in test::FILES {
|
2022-08-07 01:40:33 +00:00
|
|
|
let metadata = parse_metadata(&mut WordReader::new(File::open(file).unwrap()));
|
2022-06-25 02:22:55 +00:00
|
|
|
assert!(metadata.is_ok());
|
|
|
|
|
2022-08-02 23:31:35 +00:00
|
|
|
let (scalar, _timescale) = metadata.unwrap().timescale;
|
2022-06-25 02:22:55 +00:00
|
|
|
assert!(scalar.is_some());
|
|
|
|
}
|
2022-06-18 05:00:01 +00:00
|
|
|
}
|
2022-07-16 00:48:02 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn scopes() {
|
2022-07-18 16:53:44 +00:00
|
|
|
// see if we can parse all signal trees successfully
|
2022-08-02 23:31:35 +00:00
|
|
|
for file_name in test::FILES {
|
2022-07-16 00:48:02 +00:00
|
|
|
let file = File::open(file_name).unwrap();
|
|
|
|
let vcd = parse_vcd(file);
|
|
|
|
|
|
|
|
if !vcd.is_ok() {
|
|
|
|
dbg!(file_name);
|
|
|
|
vcd.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// assert!(vcd.is_ok());
|
|
|
|
}
|
|
|
|
}
|
2022-08-07 01:40:33 +00:00
|
|
|
}
|