Add signal type query

This commit is contained in:
Oscar Gustafsson 2023-10-09 16:07:23 +02:00
parent 0df20db383
commit dba5773ebe
4 changed files with 68 additions and 45 deletions

View file

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

View file

@ -130,7 +130,7 @@ pub(super) fn parse_events<R: std::io::Read>(
match signal {
SignalEnum::Data {
name,
sig_type,
signal_type,
ref mut signal_error,
num_bits,
num_bytes,
@ -158,7 +158,7 @@ pub(super) fn parse_events<R: std::io::Read>(
let (f, l) = (file!(), line!());
let msg = format!("\
Error near {f}:{l}. The bitwidth for signal {name} \
of sig_type {sig_type:?} is expected to be `{num_bits}` not \
of signal_type {signal_type:?} is expected to be `{num_bits}` not \
`{observed_num_bits}`. \
This error occurred while parsing the vcd file at \
{cursor:?}");
@ -171,7 +171,7 @@ pub(super) fn parse_events<R: std::io::Read>(
let msg = format!(
"\
Error near {f}:{l}. The bitwidth for signal {name} \
must be specified for a signal of type {sig_type:?}. \
must be specified for a signal of type {signal_type:?}. \
This error occurred while parsing the vcd file at \
{cursor:?}"
);
@ -245,7 +245,7 @@ pub(super) fn parse_events<R: std::io::Read>(
match signal {
SignalEnum::Data {
name,
sig_type,
signal_type,
ref mut signal_error,
num_bits,
num_bytes,
@ -268,7 +268,7 @@ pub(super) fn parse_events<R: std::io::Read>(
let msg = format!(
"\
Error near {f}:{l}. The bitwidth for signal {name} \
of sig_type {sig_type:?} is expected to be `1` not \
of signal_type {signal_type:?} is expected to be `1` not \
`{num_bits}`. \
This error occurred while parsing the vcd file at \
{cursor:?}"
@ -282,7 +282,7 @@ pub(super) fn parse_events<R: std::io::Read>(
let msg = format!(
"\
Error near {f}:{l}. The bitwidth for signal {name} \
must be specified for a signal of type {sig_type:?}. \
must be specified for a signal of type {signal_type:?}. \
This error occurred while parsing the vcd file at \
{cursor:?}"
);
@ -336,7 +336,7 @@ pub(super) fn parse_events<R: std::io::Read>(
match signal {
SignalEnum::Data {
name,
sig_type,
signal_type,
ref mut signal_error,
num_bits,
num_bytes,
@ -359,7 +359,7 @@ pub(super) fn parse_events<R: std::io::Read>(
let msg = format!(
"\
Error near {f}:{l}. The bitwidth for signal {name} \
of sig_type {sig_type:?} is expected to be `1` not \
of signal_type {signal_type:?} is expected to be `1` not \
`{num_bits}`. \
This error occurred while parsing the vcd file at \
{cursor:?}"
@ -373,7 +373,7 @@ pub(super) fn parse_events<R: std::io::Read>(
let msg = format!(
"\
Error near {f}:{l}. The bitwidth for signal {name} \
must be specified for a signal of type {sig_type:?}. \
must be specified for a signal of type {signal_type:?}. \
This error occurred while parsing the vcd file at \
{cursor:?}"
);
@ -430,7 +430,7 @@ pub(super) fn parse_events<R: std::io::Read>(
match signal {
SignalEnum::Data {
name,
sig_type,
signal_type,
ref mut signal_error,
num_bits,
string_vals,
@ -452,7 +452,7 @@ pub(super) fn parse_events<R: std::io::Read>(
let msg = format!(
"\
Error near {f}:{l}. The bitwidth for signal {name} \
of sig_type {sig_type:?} is expected to be `1` not \
of signal_type {signal_type:?} is expected to be `1` not \
`{num_bits}`. \
This error occurred while parsing the vcd file at \
{cursor:?}"
@ -466,7 +466,7 @@ pub(super) fn parse_events<R: std::io::Read>(
let msg = format!(
"\
Error near {f}:{l}. The bitwidth for signal {name} \
must be specified for a signal of type {sig_type:?}. \
must be specified for a signal of type {signal_type:?}. \
This error occurred while parsing the vcd file at \
{cursor:?}"
);

View file

@ -8,7 +8,7 @@
use std::collections::HashMap;
use super::super::reader::{curr_word, next_word, WordReader};
use super::super::signal::{SigType, SignalEnum};
use super::super::signal::{SignalEnum, SignalType};
use super::super::types::{Scope, ScopeIdx, SignalIdx, VCD};
use super::combinator_atoms::{ident, tag};
@ -47,25 +47,25 @@ pub(super) fn parse_var<R: std::io::Read>(
// $var parameter 3 a IDLE $end
// ^^^^^^^^^ - var_type
let var_type = match word {
"event" => Ok(SigType::Event),
"integer" => Ok(SigType::Integer),
"parameter" => Ok(SigType::Parameter),
"real" => Ok(SigType::Real),
"realtime" => Ok(SigType::RealTime),
"reg" => Ok(SigType::Reg),
"string" => Ok(SigType::Str),
"supply0" => Ok(SigType::Supply0),
"supply1" => Ok(SigType::Supply1),
"tri" => Ok(SigType::Tri),
"triand" => Ok(SigType::TriAnd),
"trior" => Ok(SigType::TriOr),
"trireg" => Ok(SigType::TriReg),
"tri0" => Ok(SigType::Tri0),
"tri1" => Ok(SigType::Tri1),
"time" => Ok(SigType::Time),
"wand" => Ok(SigType::WAnd),
"wire" => Ok(SigType::Wire),
"wor" => Ok(SigType::WOr),
"event" => Ok(SignalType::Event),
"integer" => Ok(SignalType::Integer),
"parameter" => Ok(SignalType::Parameter),
"real" => Ok(SignalType::Real),
"realtime" => Ok(SignalType::RealTime),
"reg" => Ok(SignalType::Reg),
"string" => Ok(SignalType::Str),
"supply0" => Ok(SignalType::Supply0),
"supply1" => Ok(SignalType::Supply1),
"tri" => Ok(SignalType::Tri),
"triand" => Ok(SignalType::TriAnd),
"trior" => Ok(SignalType::TriOr),
"trireg" => Ok(SignalType::TriReg),
"tri0" => Ok(SignalType::Tri0),
"tri1" => Ok(SignalType::Tri1),
"time" => Ok(SignalType::Time),
"wand" => Ok(SignalType::WAnd),
"wire" => Ok(SignalType::Wire),
"wor" => Ok(SignalType::WOr),
_ => {
let err = format!(
"Error near {}:{} \
@ -85,13 +85,22 @@ pub(super) fn parse_var<R: std::io::Read>(
// $var parameter 3 a IDLE $end
// ^ - num_bits
let num_bits = match var_type {
SigType::Integer
| SigType::Parameter
| SigType::Real
| SigType::Reg
| SigType::Wire
| SigType::Tri1
| SigType::Time => {
SignalType::Event
| SignalType::Integer
| SignalType::Parameter
| SignalType::Reg
| SignalType::Supply0
| SignalType::Supply1
| SignalType::Tri
| SignalType::TriAnd
| SignalType::TriOr
| SignalType::TriReg
| SignalType::Tri0
| SignalType::Tri1
| SignalType::Time
| SignalType::WAnd
| SignalType::Wire
| SignalType::WOr => {
let num_bits = word
.parse::<usize>()
.unwrap_or_else(|_| panic!("{}", parse_err));
@ -105,7 +114,7 @@ pub(super) fn parse_var<R: std::io::Read>(
})?;
Some(num_bits)
}
// for strings, we don't really care what the number of bits is
// for strings, reals, and realtimes we don't really care what the number of bits is
_ => None,
};
@ -160,7 +169,7 @@ pub(super) fn parse_var<R: std::io::Read>(
.cloned()
.chain([full_signal_name])
.collect::<Vec<String>>(),
sig_type: var_type,
signal_type: var_type,
signal_error: None,
num_bits,
num_bytes,

View file

@ -11,8 +11,8 @@ use num::BigUint;
#[derive(Debug, Copy, Clone)]
pub struct LsbIdxOfTmstmpValOnTmln(pub(super) u32);
#[derive(Debug)]
pub enum SigType {
#[derive(Debug, Eq, PartialEq)]
pub enum SignalType {
Event,
Integer,
Parameter,
@ -55,6 +55,11 @@ impl<'a> Signal<'a> {
}
}
pub fn signal_type(&self) -> Option<&SignalType> {
let Signal(signal_enum) = &self;
signal_enum.signal_type()
}
pub fn real_idx(&self) -> SignalIdx {
match self.0 {
SignalEnum::Data { self_idx, .. } => *self_idx,
@ -129,7 +134,7 @@ pub(super) enum SignalEnum {
Data {
name: String,
path: Vec<String>,
sig_type: SigType,
signal_type: SignalType,
/// I've seen a 0 bit signal parameter in a xilinx
/// simulation before that gets assigned 1 bit values.
/// I consider this to be bad behavior. We capture such
@ -204,6 +209,15 @@ impl SignalEnum {
}
.clone()
}
pub fn signal_type(&self) -> Option<&SignalType> {
match self {
SignalEnum::Data { signal_type, .. } => Some(signal_type),
// TODO: Follow aliases?
SignalEnum::Alias { .. } => None,
}
.clone()
}
}
// helper functions ultimately used by Signal's query functions later on