wellen 0.21.1

Fast VCD and FST library for waveform viewers written in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright 2023-2024 The Regents of the University of California
// Copyright 2024-2026 Cornell University
// released under BSD 3-Clause License
// author: Kevin Laeufer <laeufer@cornell.edu>

use super::{Bit, FixedWidthEncoding};
use crate::fst::push_zeros;
use crate::fst::{get_bytes_per_entry, get_len_and_meta};
use crate::signal::value::BitVecValue;
use crate::wavemem::check_if_changed_and_truncate;
use crate::{BitVecRef, Signal, SignalEncoding, SignalRef, States, TimeTableIdx};
use std::num::NonZeroU32;

pub fn transform_signal(transform: &DerivedBitVecSignal, inputs: &[&Signal]) -> Signal {
    match transform.output_encoding() {
        SignalEncoding::BitVector(width) => transform_bv_signal(transform, width.get(), inputs),
        other => todo!("Add support for generating a {:?} signal.", other),
    }
}

fn transform_bv_signal(
    transform: &DerivedBitVecSignal,
    out_width: u32,
    inputs: &[&Signal],
) -> Signal {
    let max_states = inputs
        .iter()
        .map(|i| {
            i.max_states()
                .expect("inputs to a bit-vec transform mut be bit-vec")
        })
        .reduce(States::join)
        .unwrap();
    let mut out = BitVectorBuilder::new(max_states, out_width);
    // iteration - todo: split into its own function
    let mut time = inputs
        .iter()
        .map(|i| i.time_indices.first().cloned().unwrap_or(0))
        .min()
        .unwrap_or(0);
    let mut offsets: Vec<Option<u32>> = inputs
        .iter()
        .map(|i| {
            for (ii, i_time) in i.time_indices.iter().enumerate() {
                if *i_time > time {
                    return if ii > 0 {
                        Some(i.time_indices[ii - 1])
                    } else {
                        None
                    };
                }
            }
            i.time_indices
                .first()
                .and_then(|first| if *first == time { Some(0) } else { None })
        })
        .collect();
    let mut values: Vec<Option<BitVecRef>> = inputs
        .iter()
        .zip(offsets.iter())
        .map(|(i, offset)| {
            offset.map(|o| {
                let value = i.data.get_value_at(o as usize);

                value.as_bit_vec().unwrap()
            })
        })
        .collect();

    while !offsets.iter().all(|o| o.is_none()) {
        out.add_change(time, (&transform.on_change(&values)).into());

        // more iteration logic
        let maybe_next_time = inputs
            .iter()
            .zip(offsets.iter())
            // find the next time index
            .flat_map(|(i, offset)| offset.and_then(|o| i.time_indices.get(o as usize + 1)))
            // pick the smallest
            .min();
        if let Some(next_time) = maybe_next_time {
            for ((i, offset), value) in inputs.iter().zip(offsets.iter_mut()).zip(values.iter_mut())
            {
                // we only advance the signals which have a matching next time
                if let Some(o) = *offset {
                    let next_o = o + 1;
                    if i.time_indices.get(next_o as usize) == Some(next_time) {
                        *offset = Some(next_o);
                        let any_value = i.data.get_value_at(next_o as usize);
                        *value = Some(any_value.as_bit_vec().unwrap());
                    }
                }
            }
            time = *next_time;
        } else {
            break; // done
        }
    }

    out.finish(SignalRef::derived_max())
}

/// Captures a signal which is derived from other bit-vector signals by slice and concat operations.
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct DerivedBitVecSignal {
    width: u32,
    inputs: Vec<SignalRef>,
    bits: Vec<u64>,
}

impl DerivedBitVecSignal {
    /// Creates a new signal by slicing an existing one.
    pub fn new_slice(signal: SignalRef, enc: SignalEncoding, msb: u32, lsb: u32) -> Self {
        let mut out = Self {
            width: 0,
            inputs: vec![],
            bits: vec![],
        };
        out.concat_left(signal, enc, msb, lsb);
        out
    }

    pub fn width(&self) -> u32 {
        self.width
    }

    /// Creates a new signal that is just the identity (full slice) of an existing one.
    pub fn new_identity(signal: SignalRef, enc: SignalEncoding) -> Self {
        if let SignalEncoding::BitVector(width) = enc {
            Self::new_slice(signal, enc, width.get() - 1, 0)
        } else {
            unreachable!("This function only works for bit vector signals")
        }
    }

    /// Concatenates a full signal to the left, i.e., on the most significant side.
    pub fn concat_left_full(&mut self, signal: SignalRef, enc: SignalEncoding) {
        if let SignalEncoding::BitVector(width) = enc {
            self.concat_left(signal, enc, width.get() - 1, 0)
        } else {
            unreachable!("This function only works for bit vector signals")
        }
    }

    /// Concatenates a signal to the left, i.e., on the most significant side.
    pub fn concat_left(&mut self, signal: SignalRef, enc: SignalEncoding, msb: u32, lsb: u32) {
        let mut extract = self.make_extract(signal, enc, msb, lsb);
        self.width += extract.width();
        if let Some(other_num) = self.bits.first_mut() {
            let other: Extract = (*other_num).into();
            // can the two extracts be merged?
            if other.signal == extract.signal && extract.lsb == other.msb + 1 {
                extract.lsb = other.lsb;
                *other_num = extract.into();
            } else {
                self.bits.insert(0, extract.into());
            }
        } else {
            debug_assert!(self.bits.is_empty());
            self.bits.push(extract.into());
        }
    }

    /// Concatenates a full signal to the right, i.e., on the least significant side.
    pub fn concat_right_full(&mut self, signal: SignalRef, enc: SignalEncoding) {
        if let SignalEncoding::BitVector(width) = enc {
            self.concat_right(signal, enc, width.get() - 1, 0)
        } else {
            unreachable!("This function only works for bit vector signals")
        }
    }

    /// Concatenates a signal to the right, i.e., on the least significant side.
    pub fn concat_right(&mut self, signal: SignalRef, enc: SignalEncoding, msb: u32, lsb: u32) {
        let mut extract = self.make_extract(signal, enc, msb, lsb);
        self.width += extract.width();
        if let Some(other_num) = self.bits.last_mut() {
            let other: Extract = (*other_num).into();
            // can the two extracts be merged?
            if other.signal == extract.signal && extract.msb + 1 == other.lsb {
                extract.msb = other.msb;
                *other_num = extract.into();
            } else {
                self.bits.push(extract.into());
            }
        } else {
            debug_assert!(self.bits.is_empty());
            self.bits.push(extract.into());
        }
    }

    /// Converts a signal description into an [[Extract]] op.
    fn make_extract(
        &mut self,
        signal: SignalRef,
        enc: SignalEncoding,
        msb: u32,
        lsb: u32,
    ) -> Extract {
        debug_assert!(msb >= lsb);
        if let SignalEncoding::BitVector(len) = enc {
            debug_assert!(msb < len.get());
            // check to see if we already have this signal as an input
            let signal = if let Some(ii) = self.inputs.iter().position(|i| *i == signal) {
                ii as u16
            } else {
                let ii = self.inputs.len() as u16;
                self.inputs.push(signal);
                ii
            };
            Extract { signal, msb, lsb }
        } else {
            unreachable!("Can only derive from a bit-vector signal!");
        }
    }
}

/// TODO: turn this into more of a trait!
impl DerivedBitVecSignal {
    pub fn output_encoding(&self) -> SignalEncoding {
        SignalEncoding::BitVector(NonZeroU32::new(self.width).unwrap())
    }

    pub fn inputs(&self) -> &[SignalRef] {
        &self.inputs
    }

    pub fn on_change(&self, values: &[Option<BitVecRef<'_>>]) -> BitVecValue {
        let max_states = values
            .iter()
            .flat_map(|v| v.map(|v| v.states()))
            .reduce(States::join)
            .unwrap();
        let mut out = BitVecValue::zero(max_states, self.width);

        let mut bit = self.width;
        for extract in self.bits.iter().map(|b| Extract::from(*b)) {
            if let Some(value) = values[extract.signal as usize] {
                for other_bit in (extract.lsb..(extract.msb + 1)).rev() {
                    bit -= 1;
                    let other_value = value.get_bit(other_bit);
                    out.set_bit(bit, other_value);
                }
            } else {
                // set bits to X if not value is available
                for _ in 0..extract.width() {
                    bit -= 1;
                    let other_value = Bit::X;
                    out.set_bit(bit, other_value);
                }
            }
        }
        debug_assert_eq!(bit, 0);

        out
    }
}

#[derive(Debug, Copy, Clone, PartialEq)]
struct Extract {
    signal: u16,
    msb: u32,
    lsb: u32,
}

impl Extract {
    fn width(&self) -> u32 {
        self.msb - self.lsb + 1
    }
}

impl From<u64> for Extract {
    fn from(value: u64) -> Self {
        let signal = (value >> (64 - 16)) as u16;
        let lsb = (value & (u32::MAX as u64)) as u32;
        let width = ((value >> 32) & (u16::MAX as u64)) as u16;
        let msb = (width as u32) - 1 + lsb;
        Self { signal, msb, lsb }
    }
}

impl From<Extract> for u64 {
    fn from(value: Extract) -> Self {
        let width = (value.msb - value.lsb + 1) as u16;
        ((value.signal as u64) << (64 - 16)) | ((width as u64) << 32) | (value.lsb as u64)
    }
}

struct BitVectorBuilder {
    max_states: States,
    width: u32,
    len: usize,
    has_meta: bool,
    bytes_per_entry: usize,
    data: Vec<u8>,
    time_indices: Vec<TimeTableIdx>,
}

impl BitVectorBuilder {
    fn new(max_states: States, bits: u32) -> Self {
        assert!(bits > 0);
        let (len, has_meta) = get_len_and_meta(max_states, bits);
        let bytes_per_entry = get_bytes_per_entry(len, has_meta);
        let data = vec![];
        let time_indices = vec![];
        Self {
            max_states,
            width: bits,
            len,
            has_meta,
            bytes_per_entry,
            data,
            time_indices,
        }
    }

    fn add_change(&mut self, time_idx: TimeTableIdx, value: BitVecRef) {
        debug_assert_eq!(value.width(), self.width);
        let local_encoding = value.states();
        debug_assert!(
            self.max_states.bits() >= local_encoding.bits(),
            "Cannot append a {:?} value to a {:?} signal.",
            local_encoding,
            self.max_states
        );
        if self.width == 1 {
            let value = u8::from(value.get_bit(0));
            let meta_data = (local_encoding as u8) << 6;
            self.data.push(value | meta_data);
        } else {
            let (local_len, local_has_meta) = get_len_and_meta(local_encoding, self.width);

            // append data
            let meta_data = (local_encoding as u8) << 6;
            if local_len == self.len && local_has_meta == self.has_meta {
                // same meta-data location and length as the maximum
                if self.has_meta {
                    self.data.push(meta_data);
                    value.append_to_vec(&mut self.data);
                } else {
                    let meta_data_index = self.data.len();
                    value.append_to_vec(&mut self.data);
                    self.data[meta_data_index] |= meta_data;
                }
            } else {
                // smaller encoding than the maximum
                self.data.push(meta_data);
                if self.has_meta {
                    push_zeros(&mut self.data, self.len - local_len);
                } else {
                    push_zeros(&mut self.data, self.len - local_len - 1);
                }
                value.append_to_vec(&mut self.data);
            }
        }
        // see if there actually was a change and revert if there was not
        if check_if_changed_and_truncate(self.bytes_per_entry, &mut self.data) {
            self.time_indices.push(time_idx);
        }
    }

    fn finish(self, id: SignalRef) -> Signal {
        debug_assert_eq!(
            self.data.len(),
            self.time_indices.len() * self.bytes_per_entry
        );
        let encoding = FixedWidthEncoding::BitVector {
            max_states: self.max_states,
            width: self.width,
            meta_byte: self.has_meta,
        };
        Signal::new_fixed_len(
            id,
            self.time_indices,
            encoding,
            self.bytes_per_entry as u32,
            self.data,
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::prelude::*;

    fn do_test_extract_conversion(signal: u16, width: u16, lsb: u32) {
        let msb = lsb + width as u32 - 1;
        assert_eq!(width as u32, msb - lsb + 1);
        let extract = Extract { signal, msb, lsb };
        let as_num: u64 = extract.into();
        let and_back: Extract = as_num.into();
        assert_eq!(extract, and_back);
    }

    proptest! {
        #[test]
        fn test_extract_conversion(signal: u16, width: u16, lsb: u32) {
            do_test_extract_conversion(signal, width, lsb);
        }
    }
}