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
//! Write VCD files based on pin states.
//!
//!

use crate::pins::*;
use embedded_time::duration::*;
use std::convert::TryInto;
use std::io::Result as IOResult;
use std::sync::atomic::Ordering;
use std::sync::Arc;

/// A builder for a [VcdWriter].
pub struct VcdWriterBuilder<W>
where
    W: std::io::Write,
{
    writer: vcd::Writer<W>,
    pins: Vec<(vcd::IdCode, Arc<AtomicPinState>)>,
}

impl<W> VcdWriterBuilder<W>
where
    W: std::io::Write,
{
    /// Create a new builder from a writer that implements [std::io::Write].
    pub fn new(writer: W) -> IOResult<Self> {
        Self::new_with_module(writer, "top")
    }

    /// Create a new builder from a writer that implements [std::io::Write] with
    /// an explicit module name.
    pub fn new_with_module(writer: W, module: &str) -> IOResult<Self> {
        let mut writer = vcd::Writer::new(writer);
        writer.timescale(1, vcd::TimescaleUnit::NS)?;
        writer.add_module(module)?;
        Ok(VcdWriterBuilder {
            writer,
            pins: vec![],
        })
    }

    /// Add a push pull pin with a corresponding named VCD variable.
    ///
    /// The pin state will be written to the VCD file according to the
    /// state of the pin:
    ///
    /// | Pin state | VCD value |
    /// |-----------|-----------|
    /// | high      | 1         |
    /// | low       | 0         |
    ///
    /// The initial pin state is low.
    pub fn add_push_pull_pin(&mut self, reference: &str) -> IOResult<PushPullPin> {
        let code = self.writer.add_wire(1, reference)?;
        let pin = Arc::new(AtomicPinState::new_with_state(PinState::Low));
        self.pins.push((code, pin.clone()));
        Ok(PushPullPin::new(pin))
    }

    /// Add an open drain pin with a corresponding named VCD variable.
    ///
    /// The pin state will be written to the VCD file according to the
    /// state of the pin:
    ///
    /// | Pin state | VCD value |
    /// |-----------|-----------|
    /// | high      | 0         |
    /// | low       | Z         |
    ///
    /// The initial pin state is floating.
    pub fn add_open_drain_pin(&mut self, reference: &str) -> IOResult<OpenDrainPin> {
        let code = self.writer.add_wire(1, reference)?;
        let pin = Arc::new(AtomicPinState::new_with_state(PinState::Floating));
        self.pins.push((code, pin.clone()));
        Ok(OpenDrainPin::new(pin))
    }

    /// Change the module used for wires added hereafter.
    pub fn add_module(&mut self, identifier: &str) -> IOResult<()> {
        self.writer.add_module(identifier)
    }

    /// Build a VCD writer.
    ///
    /// This consumes the builder.
    pub fn build(mut self) -> IOResult<VcdWriter<W>> {
        self.writer.upscope()?;
        self.writer.enddefinitions()?;
        Ok(VcdWriter {
            writer: self.writer,
            pins: self.pins,
        })
    }
}

/// A writer for VCD files.
///
/// Write VCD files based on pin states.
pub struct VcdWriter<W>
where
    W: std::io::Write,
{
    writer: vcd::Writer<W>,
    pins: Vec<(vcd::IdCode, Arc<AtomicPinState>)>,
}

impl<W> VcdWriter<W>
where
    W: std::io::Write,
{
    /// Write a timestamp to the VCD file.
    ///
    /// A timestamp represents a point in time that is used for the following
    /// pin states.
    pub fn timestamp<D: TryInto<Nanoseconds<u64>>>(&mut self, timestamp: D) -> IOResult<()> {
        let ts: Nanoseconds<u64> = timestamp.try_into().map_err(|_e| {
            std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "can't convert timestamp to nanoseconds",
            )
        })?;
        self.writer.timestamp(ts.0)
    }

    /// Sample all pins and write their state to the VCD file.
    ///
    /// All assigned pins will be sampled and their state is written
    /// according to the variable configuration.
    pub fn sample(&mut self) -> IOResult<()> {
        for (id, pin) in self.pins.iter() {
            let state: PinState = pin.load(Ordering::SeqCst);
            self.writer.change_scalar(*id, vcd::Value::from(state))?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use embedded_hal::digital::OutputPin;
    use pretty_assertions::assert_eq;
    use std::fmt;
    use std::sync::{Arc, Mutex};
    use synchronized_writer::SynchronizedWriter;

    #[derive(PartialEq, Eq)]
    #[doc(hidden)]
    pub struct PrettyString<'a>(pub &'a str);

    /// Make diff to display string as multi-line string
    impl<'a> fmt::Debug for PrettyString<'a> {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            f.write_str(self.0)
        }
    }

    macro_rules! assert_eq {
        ($left:expr, $right:expr) => {
            pretty_assertions::assert_eq!(PrettyString($left), PrettyString($right));
        };
    }

    #[test]
    fn write_simple() {
        let vcd = "$timescale 1 ns $end
$scope module logic $end
$var wire 1 ! test $end
$upscope $end
$enddefinitions $end
#0
0!
#100
1!
#200
1!
#300
0!
#400
#500
"
        .to_string();
        let buf = Arc::new(Mutex::new(Vec::new()));
        let writer = SynchronizedWriter::new(buf.clone());
        let mut writer = VcdWriterBuilder::new_with_module(writer, "logic").unwrap();

        let mut out_pin = writer.add_push_pull_pin("test").unwrap();
        let mut writer = writer.build().unwrap();
        writer.timestamp(0.nanoseconds()).unwrap();
        out_pin.set_low().unwrap();
        writer.sample().unwrap();
        writer.timestamp(100.nanoseconds()).unwrap();
        out_pin.set_high().unwrap();
        writer.sample().unwrap();
        writer.timestamp(200.nanoseconds()).unwrap();
        writer.sample().unwrap();
        writer.timestamp(300.nanoseconds()).unwrap();
        out_pin.set_low().unwrap();
        writer.sample().unwrap();
        writer.timestamp(400.nanoseconds()).unwrap();
        writer.timestamp(500.nanoseconds()).unwrap();

        let writer_vcd = String::from_utf8((*buf.lock().unwrap()).clone()).unwrap();
        assert_eq!(&writer_vcd, &vcd);
    }
}