Skip to content

Commit

Permalink
diff amaranth VCD
Browse files Browse the repository at this point in the history
  • Loading branch information
ekiwi committed Nov 9, 2023
1 parent be2bf10 commit 578bb9e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
11 changes: 9 additions & 2 deletions src/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ pub enum ScopeType {
#[derive(Debug, Clone, Copy)]
pub enum VarType {
Wire,
String,
Todo, // placeholder tpe
}

Expand Down Expand Up @@ -628,7 +629,7 @@ impl HierarchyBuilder {
name: String,
tpe: VarType,
direction: VarDirection,
length: u32,
raw_length: u32,
signal_idx: SignalRef,
) {
let node_id = self.vars.len();
Expand All @@ -647,13 +648,19 @@ impl HierarchyBuilder {
}
self.handle_to_node[handle_idx] = Some(var_id);

// for strings, the length is always flexible
let length = match tpe {
VarType::String => SignalLength::Variable,
_ => SignalLength::from_uint(raw_length),
};

// now we can build the node data structure and store it
let node = Var {
parent: parent.unwrap(),
name: self.add_string(name),
tpe,
direction,
length: SignalLength::from_uint(length),
length,
signal_idx,
next: None,
};
Expand Down
3 changes: 2 additions & 1 deletion src/vcd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ fn convert_scope_tpe(tpe: &[u8]) -> ScopeType {
fn convert_var_tpe(tpe: &[u8]) -> VarType {
match tpe {
b"wire" => VarType::Wire,
_ => VarType::Todo,
b"string" => VarType::String,
_ => panic!("TODO: convert {}", String::from_utf8_lossy(tpe)),
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/wavemem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,8 +630,8 @@ impl SignalEncoder {
);
// string: var-length time index + var-len length + content
leb128::write::unsigned(&mut self.data, time_idx_delta as u64).unwrap();
leb128::write::unsigned(&mut self.data, value.len() as u64).unwrap();
self.data.extend_from_slice(value);
leb128::write::unsigned(&mut self.data, (value.len() - 1) as u64).unwrap();
self.data.extend_from_slice(&value[1..]);
}
}
self.prev_time_idx = time_index;
Expand Down
48 changes: 43 additions & 5 deletions tests/diff_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ fn waveform_scope_type_to_string(tpe: ScopeType) -> &'static str {
fn waveform_var_type_to_string(tpe: VarType) -> &'static str {
match tpe {
VarType::Wire => "wire",
VarType::String => "string",
VarType::Todo => "todo",
}
}
Expand Down Expand Up @@ -108,7 +109,7 @@ fn diff_hierarchy_item(ref_item: &vcd::ScopeItem, our_item: HierarchyItem, our_h
waveform_var_type_to_string(our_var.var_type())
);
match our_var.length() {
SignalLength::Variable => panic!("TODO: check for varlen!"),
SignalLength::Variable => {} // nothing to check
SignalLength::Fixed(size) => assert_eq!(ref_var.size, size.get()),
}
assert!(ref_var.index.is_none(), "TODO: expose index");
Expand Down Expand Up @@ -140,7 +141,9 @@ fn diff_signals<R: BufRead>(ref_reader: &mut vcd::Parser<R>, our: &mut Waveform)
match cmd_res.unwrap() {
vcd::Command::Timestamp(new_time) => {
current_time = new_time;
time_table_idx += 1;
if new_time > 0 {
time_table_idx += 1;
}
assert_eq!(current_time, time_table[time_table_idx]);
}
vcd::Command::ChangeScalar(id, value) => {
Expand All @@ -153,13 +156,31 @@ fn diff_signals<R: BufRead>(ref_reader: &mut vcd::Parser<R>, our: &mut Waveform)
let signal_ref = vcd_lib_id_to_signal_ref(id);
let our_value = our.get_signal_value_at(signal_ref, time_table_idx as u32);
let our_value_str = our_value.to_bit_string().unwrap();
assert_eq!(our_value_str, value.to_string());
if value.len() < our_value_str.len() {
let prefix_len = our_value_str.len() - value.len();
// we are zero / x extending, so our string might be longer
let suffix: String = our_value_str.chars().skip(prefix_len).collect();
assert_eq!(suffix, value.to_string());
let is_x_extended = suffix.chars().next().unwrap() == 'x';
for c in our_value_str.chars().take(prefix_len) {
if is_x_extended {
assert_eq!(c, 'x');
} else {
assert_eq!(c, '0');
}
}
} else {
assert_eq!(our_value_str, value.to_string());
}
}
vcd::Command::ChangeReal(_, _) => {
todo!("compare real")
}
vcd::Command::ChangeString(_, _) => {
todo!("compare string")
vcd::Command::ChangeString(id, value) => {
let signal_ref = vcd_lib_id_to_signal_ref(id);
let our_value = our.get_signal_value_at(signal_ref, time_table_idx as u32);
let our_value_str = our_value.to_string();
assert_eq!(our_value_str, value);
}
vcd::Command::Begin(_) => {} // ignore
vcd::Command::End(_) => {} // ignore
Expand Down Expand Up @@ -199,6 +220,23 @@ fn id_to_int(id: &[u8]) -> Option<u64> {
Some(result - 1)
}

#[test]
#[ignore]
fn diff_aldec_SPI_Write() {
run_diff_test(
"inputs/aldec/SPI_Write.vcd",
"inputs/aldec/SPI_Write.vcd.fst",
);
}

#[test]
fn diff_amaranth_up_counter() {
run_diff_test(
"inputs/amaranth/up_counter.vcd",
"inputs/amaranth/up_counter.vcd.fst",
);
}

#[test]
fn diff_treadle_gcd() {
run_diff_test("inputs/treadle/GCD.vcd", "inputs/treadle/GCD.vcd.fst");
Expand Down

0 comments on commit 578bb9e

Please sign in to comment.