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
// Copyright 2021 Adam Greig
// Licensed under the MIT license.

#![allow(clippy::write_with_newline)]

use std::fmt;

use super::{
    Command,
    PIOMapDirection,
    Pattern,
    RunClock,
    RunTestForm,
    RunTestTime,
    State,
    TRSTMode,
    VectorChar
};

impl fmt::Display for State {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl fmt::Display for VectorChar {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl fmt::Display for Pattern {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.length)?;
        // Since the patterns are often long, and SVF requires lines do not exceed 256
        // characters, go to some effort to wrap long lines and format them neatly.
        const BYTES_PER_LINE: usize = 36;
        let mut break_next = false;
        for (name, data) in [
            ("TDI", &self.tdi),
            ("TDO", &self.tdo),
            ("MASK", &self.mask),
            ("SMASK", &self.smask)
        ].iter() {
            if let Some(data) = data {
                if break_next {
                    write!(f, "\n ")?;
                    break_next = false;
                }
                let skip = data.iter().rev().position(|&x| x != 0).unwrap_or(data.len() - 1);
                let data = &data[..data.len() - skip];
                write!(f, " {} (", name)?;
                for (idx, byte) in data.iter().rev().enumerate() {
                    if data.len() > BYTES_PER_LINE && idx % BYTES_PER_LINE == 0 {
                        write!(f, "\n    ")?;
                    }
                    write!(f, "{:02X}", byte)?;
                }
                write!(f, ")")?;
                if data.len() > BYTES_PER_LINE {
                    break_next = true;
                }
            }
        }
        Ok(())
    }
}

impl fmt::Display for PIOMapDirection {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", format!("{:?}", self).to_ascii_uppercase())
    }
}

impl fmt::Display for RunClock {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl fmt::Display for TRSTMode {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", format!("{:?}", self).to_ascii_uppercase())
    }
}

impl fmt::Display for RunTestTime {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:E} SEC", self.min)?;
        if let Some(max) = self.max {
            write!(f, " MAXIMUM {:E} SEC", max)?;
        }
        Ok(())
    }
}

impl fmt::Display for RunTestForm {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            RunTestForm::Clocked { run_count, run_clk, time } => {
                write!(f, "{} {}", run_count, run_clk)?;
                if let Some(t) = time {
                    write!(f, " {}", t)?;
                }
            },
            RunTestForm::Timed(t) => {
                write!(f, "{}", t)?;
            },
        }
        Ok(())
    }
}

impl fmt::Display for Command {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Command::EndDR(state) => write!(f, "ENDDR {};", state),
            Command::EndIR(state) => write!(f, "ENDIR {};", state),
            Command::Frequency(Some(freq)) => write!(f, "FREQUENCY {:E} HZ;", freq),
            Command::Frequency(None)       => write!(f, "FREQUENCY;"),
            Command::HDR(pattern) => write!(f, "HDR {};", pattern),
            Command::HIR(pattern) => write!(f, "HIR {};", pattern),
            Command::PIO(v) => {
                write!(f, "PIO (")?;
                for c in v.iter() {
                    c.fmt(f)?;
                }
                write!(f, ");")
            },
            Command::PIOMap(v) => {
                write!(f, "PIOMAP (\n")?;
                for (dir, name) in v.iter() {
                    write!(f, "    {} {}\n", dir, name)?;
                }
                write!(f, ");")
            },
            Command::RunTest { run_state, form, end_state } => {
                write!(f, "RUNTEST")?;
                if let Some(s) = run_state {
                    write!(f, " {}", s)?;
                }
                write!(f, " {}", form)?;
                if let Some(s) = end_state {
                    write!(f, " ENDSTATE {}", s)?;
                }
                write!(f, ";")
            },
            Command::SDR(pattern) => write!(f, "SDR {};", pattern),
            Command::SIR(pattern) => write!(f, "SIR {};", pattern),
            Command::State { path, end } => {
                write!(f, "STATE")?;
                if let Some(path) = path {
                    for state in path.iter() {
                        write!(f, " {}", state)?;
                    }
                }
                write!(f, " {};", end)
            },
            Command::TDR(pattern) => write!(f, "TDR {};", pattern),
            Command::TIR(pattern) => write!(f, "TIR {};", pattern),
            Command::TRST(mode) => write!(f, "TRST {};", mode),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::parse_complete;

    #[test]
    fn test_display_roundtrip() {
        let svf = "\
ENDDR IDLE;
ENDIR IDLE;
FREQUENCY;
FREQUENCY 1E3 HZ;
HDR 0;
HIR 8 TDI (AA);
PIO (HLZUDX);
PIOMAP (
    IN A
    OUT B
    INOUT C
    IN D
    OUT E
    INOUT F
);
RUNTEST IDLE 100 SCK 1E0 SEC MAXIMUM 1E0 SEC ENDSTATE IDLE;
RUNTEST IDLE 1E0 SEC;
SDR 16 TDI (AAAA) TDO (5555) MASK (1234) SMASK (ABCD);
SIR 0;
STATE IDLE RESET DRPAUSE;
TDR 0;
TIR 0;
TRST ON;
";
        let commands = parse_complete(svf).unwrap();
        let display = commands.iter().map(|c| format!("{}\n", c)).collect::<String>();
        assert_eq!(svf, display);
    }
}