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
use super::{BuildError, Endian, SvdError, ValidateLevel};
/// CPU describes the processor included in the microcontroller device.
#[cfg_attr(
    feature = "serde",
    derive(serde::Deserialize, serde::Serialize),
    serde(rename_all = "camelCase")
)]
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Cpu {
    /// Processor architecture
    pub name: String,

    /// Define the HW revision of the processor
    pub revision: String,

    /// Define the endianness of the processor
    pub endian: Endian,

    /// Indicate whether the processor is equipped with a memory protection unit (MPU)
    pub mpu_present: bool,

    /// Indicate whether the processor is equipped with a hardware floating point unit (FPU)
    pub fpu_present: bool,

    /// Indicate whether the processor is equipped with a double precision floating point unit.
    /// This element is valid only when `fpu_present` is set to `true`
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none", rename = "fpuDP")
    )]
    pub fpu_double_precision: Option<bool>,

    /// Indicates whether the processor implements the optional SIMD DSP extensions (DSP)
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub dsp_present: Option<bool>,

    /// Indicate whether the processor has an instruction cache
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub icache_present: Option<bool>,

    /// Indicate whether the processor has a data cache
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub dcache_present: Option<bool>,

    /// Indicate whether the processor has an instruction tightly coupled memory
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub itcm_present: Option<bool>,

    /// Indicate whether the processor has a data tightly coupled memory
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub dtcm_present: Option<bool>,

    /// Indicate whether the Vector Table Offset Register (VTOR) is implemented.
    /// If not specified, then VTOR is assumed to be present
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub vtor_present: Option<bool>,

    /// Define the number of bits available in the Nested Vectored Interrupt Controller (NVIC) for configuring priority
    #[cfg_attr(feature = "serde", serde(rename = "nvicPrioBits"))]
    pub nvic_priority_bits: u32,

    /// Indicate whether the processor implements a vendor-specific System Tick Timer
    #[cfg_attr(feature = "serde", serde(rename = "vendorSystickConfig"))]
    pub has_vendor_systick: bool,

    /// Add 1 to the highest interrupt number and specify this number in here
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub device_num_interrupts: Option<u32>,

    /// Indicate the amount of regions in the Security Attribution Unit (SAU)
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub sau_num_regions: Option<u32>,
    // sauRegionsConfig
}

/// Builder for [`Cpu`]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CpuBuilder {
    name: Option<String>,
    revision: Option<String>,
    endian: Option<Endian>,
    mpu_present: Option<bool>,
    fpu_present: Option<bool>,
    fpu_double_precision: Option<bool>,
    dsp_present: Option<bool>,
    icache_present: Option<bool>,
    dcache_present: Option<bool>,
    itcm_present: Option<bool>,
    dtcm_present: Option<bool>,
    vtor_present: Option<bool>,
    nvic_priority_bits: Option<u32>,
    has_vendor_systick: Option<bool>,
    device_num_interrupts: Option<u32>,
    sau_num_regions: Option<u32>,
}

impl From<Cpu> for CpuBuilder {
    fn from(c: Cpu) -> Self {
        Self {
            name: Some(c.name),
            revision: Some(c.revision),
            endian: Some(c.endian),
            mpu_present: Some(c.mpu_present),
            fpu_present: Some(c.fpu_present),
            fpu_double_precision: c.fpu_double_precision,
            dsp_present: c.dsp_present,
            icache_present: c.icache_present,
            dcache_present: c.dcache_present,
            itcm_present: c.itcm_present,
            dtcm_present: c.dtcm_present,
            vtor_present: c.vtor_present,
            nvic_priority_bits: Some(c.nvic_priority_bits),
            has_vendor_systick: Some(c.has_vendor_systick),
            device_num_interrupts: c.device_num_interrupts,
            sau_num_regions: c.sau_num_regions,
        }
    }
}

impl CpuBuilder {
    /// Set the name of the cpu.
    pub fn name(mut self, value: String) -> Self {
        self.name = Some(value);
        self
    }
    /// Set the revision of the cpu.
    pub fn revision(mut self, value: String) -> Self {
        self.revision = Some(value);
        self
    }
    /// Set the endian of the cpu.
    pub fn endian(mut self, value: Endian) -> Self {
        self.endian = Some(value);
        self
    }
    /// Set the mpu_present of the cpu.
    pub fn mpu_present(mut self, value: bool) -> Self {
        self.mpu_present = Some(value);
        self
    }
    /// Set the fpu_present of the cpu.
    pub fn fpu_present(mut self, value: bool) -> Self {
        self.fpu_present = Some(value);
        self
    }
    /// Set the fpu_double_precision of the cpu.
    pub fn fpu_double_precision(mut self, value: Option<bool>) -> Self {
        self.fpu_double_precision = value;
        self
    }
    /// Set the dsp_present of the cpu.
    pub fn dsp_present(mut self, value: Option<bool>) -> Self {
        self.dsp_present = value;
        self
    }
    /// Set the icache_present of the cpu.
    pub fn icache_present(mut self, value: Option<bool>) -> Self {
        self.icache_present = value;
        self
    }
    /// Set the dcache_present of the cpu.
    pub fn dcache_present(mut self, value: Option<bool>) -> Self {
        self.dcache_present = value;
        self
    }
    /// Set the itcm_present of the cpu.
    pub fn itcm_present(mut self, value: Option<bool>) -> Self {
        self.itcm_present = value;
        self
    }
    /// Set the dtcm_present of the cpu.
    pub fn dtcm_present(mut self, value: Option<bool>) -> Self {
        self.dtcm_present = value;
        self
    }
    /// Set the vtor_present of the cpu.
    pub fn vtor_present(mut self, value: Option<bool>) -> Self {
        self.vtor_present = value;
        self
    }
    /// Set the nvic_priority_bits of the cpu.
    pub fn nvic_priority_bits(mut self, value: u32) -> Self {
        self.nvic_priority_bits = Some(value);
        self
    }
    /// Set the has_vendor_systick of the cpu.
    pub fn has_vendor_systick(mut self, value: bool) -> Self {
        self.has_vendor_systick = Some(value);
        self
    }
    /// Set the device_num_interrupts of the cpu.
    pub fn device_num_interrupts(mut self, value: Option<u32>) -> Self {
        self.device_num_interrupts = value;
        self
    }
    /// Set the sau_num_regions of the cpu.
    pub fn sau_num_regions(mut self, value: Option<u32>) -> Self {
        self.sau_num_regions = value;
        self
    }
    /// Validate and build a [`Cpu`].
    pub fn build(self, lvl: ValidateLevel) -> Result<Cpu, SvdError> {
        let cpu = Cpu {
            name: self
                .name
                .ok_or_else(|| BuildError::Uninitialized("name".to_string()))?,
            revision: self
                .revision
                .ok_or_else(|| BuildError::Uninitialized("revision".to_string()))?,
            endian: self
                .endian
                .ok_or_else(|| BuildError::Uninitialized("endian".to_string()))?,
            mpu_present: self
                .mpu_present
                .ok_or_else(|| BuildError::Uninitialized("mpu_present".to_string()))?,
            fpu_present: self
                .fpu_present
                .ok_or_else(|| BuildError::Uninitialized("fpu_present".to_string()))?,
            fpu_double_precision: self.fpu_double_precision,
            dsp_present: self.dsp_present,
            icache_present: self.icache_present,
            dcache_present: self.dcache_present,
            itcm_present: self.itcm_present,
            dtcm_present: self.dtcm_present,
            vtor_present: self.vtor_present,
            nvic_priority_bits: self
                .nvic_priority_bits
                .ok_or_else(|| BuildError::Uninitialized("nvic_priority_bits".to_string()))?,
            has_vendor_systick: self
                .has_vendor_systick
                .ok_or_else(|| BuildError::Uninitialized("has_vendor_systick".to_string()))?,
            device_num_interrupts: self.device_num_interrupts,
            sau_num_regions: self.sau_num_regions,
        };
        cpu.validate(lvl)?;
        Ok(cpu)
    }
}

impl Cpu {
    /// Make a builder for [`Cpu`]
    pub fn builder() -> CpuBuilder {
        CpuBuilder::default()
    }
    /// Modify an existing [`Cpu`] based on a [builder](CpuBuilder).
    pub fn modify_from(&mut self, builder: CpuBuilder, lvl: ValidateLevel) -> Result<(), SvdError> {
        if let Some(name) = builder.name {
            self.name = name;
        }
        if let Some(revision) = builder.revision {
            self.revision = revision;
        }
        if let Some(endian) = builder.endian {
            self.endian = endian;
        }
        if let Some(mpu_present) = builder.mpu_present {
            self.mpu_present = mpu_present;
        }
        if let Some(fpu_present) = builder.fpu_present {
            self.fpu_present = fpu_present;
        }
        if builder.fpu_double_precision.is_some() {
            self.fpu_double_precision = builder.fpu_double_precision;
        }
        if builder.dsp_present.is_some() {
            self.dsp_present = builder.dsp_present;
        }
        if builder.icache_present.is_some() {
            self.icache_present = builder.icache_present;
        }
        if builder.dcache_present.is_some() {
            self.dcache_present = builder.dcache_present;
        }
        if builder.itcm_present.is_some() {
            self.itcm_present = builder.itcm_present;
        }
        if builder.dtcm_present.is_some() {
            self.dtcm_present = builder.dtcm_present;
        }
        if builder.vtor_present.is_some() {
            self.vtor_present = builder.vtor_present;
        }
        if let Some(nvic_priority_bits) = builder.nvic_priority_bits {
            self.nvic_priority_bits = nvic_priority_bits;
        }
        if let Some(has_vendor_systick) = builder.has_vendor_systick {
            self.has_vendor_systick = has_vendor_systick;
        }
        if builder.device_num_interrupts.is_some() {
            self.device_num_interrupts = builder.device_num_interrupts;
        }
        if builder.sau_num_regions.is_some() {
            self.sau_num_regions = builder.sau_num_regions;
        }
        self.validate(lvl)
    }
    /// Validate the [`Cpu`]
    pub fn validate(&self, _lvl: ValidateLevel) -> Result<(), SvdError> {
        // TODO
        Ok(())
    }
    /// Check if the [`Cpu`] is a Cortex-M
    pub fn is_cortex_m(&self) -> bool {
        self.name.starts_with("CM")
    }
}