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
use crate::{SvdExpanderError, SvdExpanderResult};
use svd_parser::{
  writeconstraint::WriteConstraintRange, EnumeratedValue, EnumeratedValues, ModifiedWriteValues,
  Usage, WriteConstraint,
};

/// Describes the manipulation of data written to a field. If not specified, the values written to
/// the field is the value stored in the field.
#[derive(Debug, Clone, PartialEq)]
pub enum ModifiedWriteValuesSpec {
  /// Write data bit of one shall clear (set to zero) the corresponding bit in the field.
  OneToClear,

  /// Write data bit of one shall set (set to one) the corresponding bit in the field.
  OneToSet,

  /// Write data bit of one shall toggle (invert) the corresponding bit in the field.
  OneToToggle,

  /// Write data bit of zero shall clear (set to zero) the corresponding bit in the field.
  ZeroToClear,

  /// Write data bit of zero shall set (set to one) the corresponding bit in the field.
  ZeroToSet,

  /// Write data bit of zero shall toggle (invert) the corresponding bit in the field.
  ZeroToToggle,

  /// After a write operation, all the bits in the field are cleared (set to zero).
  Clear,

  /// After a write operation, all the bits in the field are set (set to one).
  Set,

  /// After a write operation, all the bits in the field may be modified (default).
  Modify,
}
impl ModifiedWriteValuesSpec {
  pub(crate) fn new(mwv: &ModifiedWriteValues) -> Self {
    match mwv {
      ModifiedWriteValues::OneToClear => ModifiedWriteValuesSpec::OneToClear,
      ModifiedWriteValues::OneToSet => ModifiedWriteValuesSpec::OneToSet,
      ModifiedWriteValues::OneToToggle => ModifiedWriteValuesSpec::OneToToggle,

      ModifiedWriteValues::ZeroToClear => ModifiedWriteValuesSpec::ZeroToClear,
      ModifiedWriteValues::ZeroToSet => ModifiedWriteValuesSpec::ZeroToSet,
      ModifiedWriteValues::ZeroToToggle => ModifiedWriteValuesSpec::ZeroToToggle,

      ModifiedWriteValues::Clear => ModifiedWriteValuesSpec::Clear,
      ModifiedWriteValues::Set => ModifiedWriteValuesSpec::Set,
      ModifiedWriteValues::Modify => ModifiedWriteValuesSpec::Modify,
    }
  }
}

/// Defines constraints for writing values to a field.
#[derive(Debug, Clone, PartialEq)]
pub enum WriteConstraintSpec {
  /// Only the values in the enumerated values list(s) of the field may be written.
  UseEnumeratedValues,

  /// Only integers within the bounds of the range (inclusive) may be written.
  Range(WriteConstraintRangeSpec),

  /// Only the last-read value can be written.
  WriteAsRead,

  /// There are no constraints on writing to the field. This variant is only constructed in cases
  /// where the SVD XML is illogical, for example like this:
  ///
  /// ```xml
  /// <writeConstraint>
  ///   <useEnumeratedValues>false</useEnumeratedValues>
  /// </writeConstraint>
  /// ```
  ///
  /// The XML is supposed to contain one of three mutually exclusive options, so it doesn't make
  /// sense if the option that it contains is set to `false`.
  Unconstrained,
}
impl WriteConstraintSpec {
  pub(crate) fn new(wc: &WriteConstraint) -> Self {
    match wc {
      WriteConstraint::WriteAsRead(true) => WriteConstraintSpec::WriteAsRead,
      WriteConstraint::UseEnumeratedValues(true) => WriteConstraintSpec::UseEnumeratedValues,
      WriteConstraint::Range(ref wcr) => {
        WriteConstraintSpec::Range(WriteConstraintRangeSpec::new(wcr))
      }
      _ => WriteConstraintSpec::Unconstrained,
    }
  }
}

/// A range of values that can be written to a field. Writable values are integers that fall within
/// the `min` and `max` bounds (inclusive).
#[derive(Debug, Clone, PartialEq)]
pub struct WriteConstraintRangeSpec {
  /// Lowest value that can be written to a field.
  pub min: u32,

  /// Highest value that can be written to a field.
  pub max: u32,
}
impl WriteConstraintRangeSpec {
  pub(crate) fn new(wcr: &WriteConstraintRange) -> Self {
    Self {
      min: wcr.min,
      max: wcr.max,
    }
  }
}

/// Defines whether an enumerated value may be read from a field, written to a field, or both.
#[derive(Debug, Clone, PartialEq)]
pub enum EnumeratedValueUsageSpec {
  /// The enumerated value may be read from the field, but not written to it.
  Read,

  /// The enumerated value may be written to the field, but not read from it.
  Write,

  /// The enumerated value may be both written to the field and read from it.
  ReadWrite,
}
impl EnumeratedValueUsageSpec {
  pub(crate) fn new(u: &Usage) -> Self {
    match u {
      Usage::Read => EnumeratedValueUsageSpec::Read,
      Usage::Write => EnumeratedValueUsageSpec::Write,
      Usage::ReadWrite => EnumeratedValueUsageSpec::ReadWrite,
    }
  }
}

/// A set of values that can be written to and/or read from a field.
#[derive(Debug, Clone, PartialEq)]
pub struct EnumeratedValueSetSpec {
  preceding_path: String,
  register_path: String,
  derived_from: Option<String>,

  /// The name of the set of enumerated values.
  pub name: Option<String>,

  /// Whether the values in this set can be read from the field, written to it, or both.
  pub usage: Option<EnumeratedValueUsageSpec>,

  /// The list of values.
  pub values: Vec<EnumeratedValueSpec>,
}
impl EnumeratedValueSetSpec {
  pub(crate) fn new(
    ev: &EnumeratedValues,
    preceding_path: &str,
    register_path: &str,
  ) -> SvdExpanderResult<Self> {
    Ok(Self {
      preceding_path: preceding_path.to_owned(),
      register_path: register_path.to_owned(),
      derived_from: ev.derived_from.clone(),
      name: ev.name.clone(),
      usage: match ev.usage {
        Some(ref u) => Some(EnumeratedValueUsageSpec::new(u)),
        None => None,
      },
      values: ev
        .values
        .iter()
        .map(|v| EnumeratedValueSpec::new(&v))
        .collect(),
    })
  }

  /// The full path to the enumerated value set that this set inherits from (if any).
  pub(crate) fn derived_from_path(&self) -> SvdExpanderResult<Option<String>> {
    match self.derived_from {
      Some(ref df) => match df.contains(".") {
        true => {
          return Err(SvdExpanderError::new(
            "Qualified paths to enumerated value sets are not currently supported.",
          ))
        }
        false => Ok(Some(format!("{}.{}", self.register_path, df))),
      },
      None => Ok(None),
    }
  }

  /// The full path to this enumerated value set. Will be `None` if this set does not
  /// have a `name` (`name` is `None`).
  pub(crate) fn path(&self) -> Option<String> {
    match self.name {
      Some(ref n) => Some(format!("{}.{}", self.register_path, n)),
      None => None,
    }
  }

  pub(crate) fn clone_with_preceding_paths(
    &self,
    preceding_path: &str,
    register_path: &str,
  ) -> Self {
    Self {
      preceding_path: preceding_path.to_owned(),
      register_path: register_path.to_owned(),
      derived_from: None,
      name: self.name.clone(),
      usage: self.usage.clone(),
      values: self.values.clone(),
    }
  }

  pub(crate) fn inherit_from(&mut self, ss: &EnumeratedValueSetSpec) -> bool {
    let mut changed = false;

    if self.usage.is_none() && ss.usage.is_some() {
      self.usage = ss.usage.clone();
      changed = true;
    }

    for ancestor in ss.values.iter() {
      if let Some(ref mut descendant) = self.values.iter_mut().find(|f| f.name == ancestor.name) {
        if descendant.inherit_from(ancestor) {
          changed = true;
        }
      } else {
        self.values.push(ancestor.clone());
        changed = true;
      }
    }

    changed
  }
}

/// A value that can be written to or read from a field (or both).
#[derive(Debug, Clone, PartialEq)]
pub struct EnumeratedValueSpec {
  /// The name of the value. Must be unique within the enumerated value set.
  pub name: String,

  /// A description of the value's meaning.
  pub description: Option<String>,

  /// The actual value to be written and/or read.
  pub value: Option<EnumeratedValueValueSpec>,
}
impl EnumeratedValueSpec {
  pub(crate) fn new(ev: &EnumeratedValue) -> Self {
    Self {
      name: ev.name.clone(),
      description: ev.description.clone(),
      value: match (ev.value, ev.is_default) {
        (Some(v), _) => Some(EnumeratedValueValueSpec::Value(v)),
        (None, Some(true)) => Some(EnumeratedValueValueSpec::Default),
        (None, None) => None,
        (None, Some(false)) => None,
      },
    }
  }

  pub(crate) fn inherit_from(&mut self, vs: &EnumeratedValueSpec) -> bool {
    let mut changed = false;

    if self.description.is_none() && vs.description.is_some() {
      self.description = vs.description.clone();
      changed = true;
    }

    if self.value.is_none() && vs.value.is_some() {
      self.value = vs.value.clone();
      changed = true;
    }

    changed
  }
}

/// The actual content that can be written to or read from a field as part of
/// an enumerated value set.
#[derive(Debug, Clone, PartialEq)]
pub enum EnumeratedValueValueSpec {
  /// The bit value to be read or written.
  Value(u32),

  /// This value is a catch-all for any other values that are not explicitly listed.
  Default,
}