Skip to content

Commit

Permalink
load timescale from fst
Browse files Browse the repository at this point in the history
  • Loading branch information
ekiwi committed Nov 9, 2023
1 parent 9691a67 commit e710f1e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
34 changes: 30 additions & 4 deletions src/fst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,43 @@ 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<F: BufRead + Seek>(reader: &mut FstReader<F>) -> Hierarchy {
let mut h = HierarchyBuilder::default();
// load meta-data
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();

Expand Down
9 changes: 7 additions & 2 deletions src/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {:?}",
Expand Down
3 changes: 2 additions & 1 deletion src/vcd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand Down

0 comments on commit e710f1e

Please sign in to comment.