From e710f1e5e2faafa9684d335e233592dbc277bf35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20L=C3=A4ufer?= Date: Thu, 9 Nov 2023 09:48:40 -0500 Subject: [PATCH] load timescale from fst --- Cargo.toml | 2 +- src/fst.rs | 34 ++++++++++++++++++++++++++++++---- src/hierarchy.rs | 9 +++++++-- src/vcd.rs | 3 ++- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d5c90ed..af95f79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ edition = "2021" [dependencies] bytesize = "1.3.0" clap = { version = "4.4.6", features = ["derive"] } -fst-native = "0.6.0" +fst-native = "0.6.3" leb128 = "0.2.5" lz4_flex = "0.11.1" memmap2 = "0.9.0" diff --git a/src/fst.rs b/src/fst.rs index 4df4783..8c98a65 100644 --- a/src/fst.rs +++ b/src/fst.rs @@ -90,9 +90,35 @@ fn convert_var_direction(tpe: FstVarDirection) -> VarDirection { } } -// fn convert_timescale_unit() -> TimescaleUnit { -// -// } +fn convert_timescale(exponent: i8) -> Timescale { + if exponent >= 0 { + Timescale::new(10u32.pow(exponent as u32), TimescaleUnit::Seconds) + } else if exponent >= -3 { + Timescale::new( + 10u32.pow((exponent + 3) as u32), + TimescaleUnit::MilliSeconds, + ) + } else if exponent >= -6 { + Timescale::new( + 10u32.pow((exponent + 6) as u32), + TimescaleUnit::MicroSeconds, + ) + } else if exponent >= -9 { + Timescale::new(10u32.pow((exponent + 9) as u32), TimescaleUnit::NanoSeconds) + } else if exponent >= -12 { + Timescale::new( + 10u32.pow((exponent + 12) as u32), + TimescaleUnit::PicoSeconds, + ) + } else if exponent >= -15 { + Timescale::new( + 10u32.pow((exponent + 15) as u32), + TimescaleUnit::FemtoSeconds, + ) + } else { + panic!("Unexpected timescale exponent: {}", exponent); + } +} fn read_hierarchy(reader: &mut FstReader) -> Hierarchy { let mut h = HierarchyBuilder::default(); @@ -100,7 +126,7 @@ fn read_hierarchy(reader: &mut FstReader) -> Hierarchy { let fst_header = reader.get_header(); h.set_version(fst_header.version.trim().to_string()); h.set_date(fst_header.date.trim().to_string()); - //TODO: h.set_timescale( Timescale { factor: 0, unit: convert_timescale_unit(fst_header.) } ); + h.set_timescale(convert_timescale(fst_header.timescale_exponent)); let mut path_names = HashMap::new(); diff --git a/src/hierarchy.rs b/src/hierarchy.rs index 76c59d1..01cf1f0 100644 --- a/src/hierarchy.rs +++ b/src/hierarchy.rs @@ -14,6 +14,12 @@ pub struct Timescale { pub unit: TimescaleUnit, } +impl Timescale { + pub fn new(factor: u32, unit: TimescaleUnit) -> Self { + Timescale { factor, unit } + } +} + #[derive(Debug, Clone, Copy, PartialEq)] pub enum TimescaleUnit { FemtoSeconds, @@ -682,8 +688,7 @@ impl HierarchyBuilder { self.meta.version = value; } - pub fn set_timescale(&mut self, factor: u32, unit: TimescaleUnit) { - let value = Timescale { factor, unit }; + pub fn set_timescale(&mut self, value: Timescale) { assert!( self.meta.timescale.is_none(), "Duplicate timescales: {:?} vs {:?}", diff --git a/src/vcd.rs b/src/vcd.rs index b1bd874..0d752ef 100644 --- a/src/vcd.rs +++ b/src/vcd.rs @@ -104,7 +104,8 @@ fn read_hierarchy(input: &mut (impl BufRead + Seek)) -> (usize, Hierarchy) { } HeaderCmd::Timescale(factor, unit) => { let factor_int = u32::from_str_radix(std::str::from_utf8(factor).unwrap(), 10).unwrap(); - h.set_timescale(factor_int, convert_timescale_unit(unit)); + let value = Timescale::new(factor_int, convert_timescale_unit(unit)); + h.set_timescale(value); } };