Skip to content
This repository was archived by the owner on Dec 30, 2024. It is now read-only.

Commit b685bf8

Browse files
handle case of signal events with bitwidths shorter than nominal signal length
1 parent d70a09e commit b685bf8

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

src/vcd/parse.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,12 @@ fn parse_events<'a>(
273273
// Also account for the error case of a bitwidth of `None`
274274
match num_bits {
275275
Some(ref num_bits) => {
276-
if *num_bits > observed_num_bits {
276+
if observed_num_bits > *num_bits {
277277
let (f, l) = (file!(), line!());
278278
let msg = format!("\
279279
Error near {f}:{l}. The bitwidth for signal {name} \
280-
of sig_type {sig_type:?} is expected to be `1` not \
281-
`{num_bits}`. \
280+
of sig_type {sig_type:?} is expected to be `{num_bits}` not \
281+
`{observed_num_bits}`. \
282282
This error occurred while parsing the vcd file at \
283283
{cursor:?}");
284284
*signal_error = Some(msg);
@@ -309,7 +309,31 @@ fn parse_events<'a>(
309309
}
310310
else {
311311
u8_timeline_markers.push(timeline_idx);
312+
313+
let mut curr_num_bytes = value_u8.len();
312314
u8_timeline.append(&mut value_u8);
315+
316+
// we may need to zero extend values
317+
// so that we end up storing all values
318+
// of a particular signal in a consistent
319+
// amount of bytes
320+
let num_bits = num_bits.unwrap();
321+
let bytes_required = (num_bits / 8) +
322+
if (num_bits % 8) > 0 {1} else {0};
323+
324+
while curr_num_bytes < bytes_required {
325+
// useful for debugging
326+
// let err = format!("Error at {cursor:?}.\
327+
// num_bits = {num_bits}, \
328+
// observed_bits = {observed_num_bits}, \
329+
// curr_num_bytes = {curr_num_bytes}, \
330+
// bytes_required = {bytes_required} \
331+
// for signal {name}");
332+
// Err(err)?;
333+
334+
u8_timeline.push(0u8);
335+
curr_num_bytes += 1;
336+
}
313337
Ok(())
314338
}
315339
}
@@ -402,9 +426,7 @@ fn parse_events<'a>(
402426
}?;
403427
}
404428

405-
// other one bit cases
406-
"x" | "X" | "z" | "Z" | "u" | "U" => {
407-
let val = word.to_string();
429+
"1" => {
408430
// lokup signal idx
409431
let hash = &word[1..];
410432
let (f, l )= (file!(), line!());
@@ -431,8 +453,7 @@ fn parse_events<'a>(
431453
let signal = vcd.all_signals.get_mut(signal_idx).unwrap();
432454
match signal {
433455
Signal::Data {name, sig_type, ref mut signal_error, num_bits,
434-
self_idx, u8_timeline, u8_timeline_markers, string_timeline,
435-
string_timeline_markers, ..} => {
456+
self_idx, u8_timeline, u8_timeline_markers, scope_parent, ..} => {
436457

437458
// if this is a bad signal, go ahead and skip it
438459
if signal_error.is_some() {continue;}
@@ -469,8 +490,8 @@ fn parse_events<'a>(
469490
|e| format!("Error near {f}:{l}. Failed to convert from usize to u32."))?;
470491
let timeline_idx = TimelineIdx(timeline_idx);
471492

472-
string_timeline_markers.push(timeline_idx);
473-
string_timeline.push(val);
493+
u8_timeline_markers.push(timeline_idx);
494+
u8_timeline.push(1u8);
474495
Ok(())
475496
}
476497
Signal::Alias {..} => {
@@ -482,7 +503,9 @@ fn parse_events<'a>(
482503
}
483504
}?;
484505
}
485-
"1" => {
506+
// other one bit cases
507+
"x" | "X" | "z" | "Z" | "u" | "U" => {
508+
let val = word.to_string();
486509
// lokup signal idx
487510
let hash = &word[1..];
488511
let (f, l )= (file!(), line!());
@@ -509,7 +532,8 @@ fn parse_events<'a>(
509532
let signal = vcd.all_signals.get_mut(signal_idx).unwrap();
510533
match signal {
511534
Signal::Data {name, sig_type, ref mut signal_error, num_bits,
512-
self_idx, u8_timeline, u8_timeline_markers, scope_parent, ..} => {
535+
self_idx, u8_timeline, u8_timeline_markers, string_timeline,
536+
string_timeline_markers, ..} => {
513537

514538
// if this is a bad signal, go ahead and skip it
515539
if signal_error.is_some() {continue;}
@@ -546,8 +570,8 @@ fn parse_events<'a>(
546570
|e| format!("Error near {f}:{l}. Failed to convert from usize to u32."))?;
547571
let timeline_idx = TimelineIdx(timeline_idx);
548572

549-
u8_timeline_markers.push(timeline_idx);
550-
u8_timeline.push(1u8);
573+
string_timeline_markers.push(timeline_idx);
574+
string_timeline.push(val);
551575
Ok(())
552576
}
553577
Signal::Alias {..} => {

0 commit comments

Comments
 (0)