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
use super::AccessType;
use super::AccessType::Unimplemented;
use crate::{Error, Result};
use std::collections::HashMap;
use std::sync::RwLock;

// State values for common initialization cases
pub const ZERO: u8 = 0;
pub const ONE: u8 = 1;
pub const UNDEFINED: u8 = 0b10;

// TODO: Would one RwLock wrapping a BitInner struct instantiate faster?
#[derive(Debug)]
pub struct Bit {
    pub id: usize,
    pub register_id: usize,
    pub overlay: RwLock<Option<String>>,
    pub overlay_snapshots: RwLock<HashMap<String, Option<String>>>,
    /// The individual bits mean the following:
    /// 0 - Data value
    /// 1 - Value is X
    /// 2 - Value is Z
    /// 3 - Bit is to be verified
    /// 4 - Bit is to be captured
    /// 5 - Modified, sets if bits \[2:0\ have been changed since the last reset
    pub state: RwLock<u8>,
    /// The state we think the device has, only bits \[2:0\] are applicable.
    /// This is updated by resetting the register or executing a transaction.
    pub device_state: RwLock<u8>,
    /// The state of the bit at the last reset
    pub state_snapshots: RwLock<HashMap<String, u8>>,
    pub access: AccessType,
    pub position: usize,
}

impl Bit {
    pub fn snapshot(&self, name: &str) {
        let state = *self.state.read().unwrap();
        let overlay = match &*self.overlay.read().unwrap() {
            Some(x) => Some(x.to_string()),
            None => None,
        };
        let mut state_snapshots = self.state_snapshots.write().unwrap();
        let mut overlay_snapshots = self.overlay_snapshots.write().unwrap();
        state_snapshots.insert(name.to_string(), state);
        overlay_snapshots.insert(name.to_string(), overlay);
    }

    pub fn is_changed(&self, name: &str) -> Result<bool> {
        let state = *self.state.read().unwrap();
        let overlay = match &*self.overlay.read().unwrap() {
            Some(x) => Some(x.to_string()),
            None => None,
        };
        match self.state_snapshots.read().unwrap().get(name) {
            None => {
                return Err(Error::new(&format!(
                    "No snapshot named '{}' has been taken",
                    name
                )))
            }
            Some(x) => {
                if *x != state {
                    return Ok(true);
                }
            }
        };
        match self.overlay_snapshots.read().unwrap().get(name) {
            None => {
                return Err(Error::new(&format!(
                    "No snapshot named '{}' has been taken",
                    name
                )))
            }
            Some(x) => {
                if *x != overlay {
                    return Ok(true);
                }
            }
        };
        Ok(false)
    }

    pub fn rollback(&self, name: &str) -> Result<()> {
        match self.state_snapshots.read().unwrap().get(name) {
            None => {
                return Err(Error::new(&format!(
                    "No snapshot named '{}' has been taken",
                    name
                )))
            }
            Some(x) => {
                let mut state = self.state.write().unwrap();
                *state = *x;
            }
        };
        match self.overlay_snapshots.read().unwrap().get(name) {
            None => {
                return Err(Error::new(&format!(
                    "No snapshot named '{}' has been taken",
                    name
                )))
            }
            Some(x) => match x {
                None => self.set_overlay(None),
                Some(y) => self.set_overlay(Some(&*y.as_str())),
            },
        };
        Ok(())
    }

    /// Copies the state (data and flags) and overlay attributes of the given bit to self
    pub fn copy_state(&self, source: &Bit) {
        let mut state = self.state.write().unwrap();
        *state = *source.state.read().unwrap();
        let mut overlay = self.overlay.write().unwrap();
        match &*source.overlay.read().unwrap() {
            Some(x) => *overlay = Some(x.to_string()),
            None => *overlay = None,
        }
    }

    pub fn clear_flags(&self) {
        let state_val;
        {
            state_val = *self.state.read().unwrap();
        }
        let mut state = self.state.write().unwrap();
        *state = state_val & 0b0010_0111;
    }

    pub fn clear_verify_flag(&self) {
        let state_val;
        {
            state_val = *self.state.read().unwrap();
        }
        let mut state = self.state.write().unwrap();
        *state = state_val & 0b1111_0111;
    }

    pub fn capture(&self) {
        let state_val;
        {
            state_val = *self.state.read().unwrap();
        }
        let mut state = self.state.write().unwrap();
        *state = state_val | 0b1_0000;
    }

    pub fn clear_capture(&self) {
        let state_val;
        {
            state_val = *self.state.read().unwrap();
        }
        let mut state = self.state.write().unwrap();
        *state = state_val & 0b0_1111;
    }

    /// Sets the bit's data value to X
    pub fn set_undefined(&self) {
        let state_val;
        {
            state_val = *self.state.read().unwrap();
        }
        let mut state = self.state.write().unwrap();
        *state = state_val | 0b0010_0010;
    }

    /// Sets the bit's data value to Z
    pub fn set_hiz(&self) {
        let state_val;
        {
            state_val = *self.state.read().unwrap();
        }
        let mut state = self.state.write().unwrap();
        *state = state_val | 0b0010_0100;
    }

    /// Returns true if not in X or Z state
    pub fn has_known_value(&self) -> bool {
        self.access == Unimplemented || *self.state.read().unwrap() & 0b110 == 0
    }

    pub fn is_x(&self) -> bool {
        self.access != Unimplemented && *self.state.read().unwrap() & 0b10 != 0
    }

    pub fn is_z(&self) -> bool {
        self.access != Unimplemented && *self.state.read().unwrap() & 0b100 != 0
    }

    pub fn is_to_be_verified(&self) -> bool {
        *self.state.read().unwrap() & 0b1000 != 0
    }

    pub fn is_to_be_captured(&self) -> bool {
        *self.state.read().unwrap() & 0b1_0000 != 0
    }

    pub fn has_overlay(&self) -> bool {
        (*self.overlay.read().unwrap()).is_some()
    }

    pub fn is_readable(&self) -> bool {
        self.access.is_readable()
    }

    pub fn is_writeable(&self) -> bool {
        self.access.is_writeable()
    }

    pub fn is_writable(&self) -> bool {
        self.access.is_writable()
    }

    pub fn is_modified_since_reset(&self) -> bool {
        *self.state.read().unwrap() & 0b10_0000 != 0
    }

    pub fn state_char(&self) -> char {
        if self.has_known_value() {
            if *self.state.read().unwrap() & 0b1 == 0 {
                '0'
            } else {
                '1'
            }
        } else {
            if self.is_x() {
                'x'
            } else {
                'z'
            }
        }
    }

    pub fn verify(&self) -> Result<()> {
        if self.has_known_value() {
            let mut state = self.state.write().unwrap();
            *state = *state | 0b1000;
            Ok(())
        } else {
            return Err(Error::new(&format!(
                "Attempt to verify a bit which has an undefined data value, bit state is: {}",
                self.state_char()
            )));
        }
    }

    /// Returns true if the current bit state differs from the device state.
    /// Note that for the purposes of this comparison, X and a 1/0 are considered different.
    pub fn is_update_required(&self) -> bool {
        *self.state.read().unwrap() & 0b111 != *self.device_state.read().unwrap() & 0b111
    }

    pub fn update_device_state(&self) -> Result<()> {
        let s = self.state.read().unwrap();
        let mut d = self.device_state.write().unwrap();
        *d = *s;
        Ok(())
    }

    pub fn data(&self) -> Result<u8> {
        if self.has_known_value() {
            Ok(*self.state.read().unwrap() & 0b1)
        } else {
            return Err(Error::new(&format!(
                "Bit data value is unknown, bit state is: {}",
                self.state_char()
            )));
        }
    }

    pub fn set_data(&self, val: u8) {
        // Let's make set_data ignore bit behaviour and can introduce an additional
        // behavioral-aware method in future
        //if self.is_writeable() {
        if !self.access.is_unimplemented() {
            self.force_data(val);
        }
    }

    /// Like set_data(), but will force the data value in the event of the bit being unimplemented or
    /// otherwise unwritable
    pub fn force_data(&self, val: u8) {
        let state_val;
        {
            // Clear X and Z flags
            state_val = *self.state.read().unwrap() & 0b1111_1000;
        }
        let mut state = self.state.write().unwrap();
        *state = state_val | (val & 0b1) | 0b0010_0000;
    }

    pub fn get_overlay(&self) -> Option<String> {
        match &*self.overlay.read().unwrap() {
            Some(x) => Some(x.to_string()),
            None => None,
        }
    }

    pub fn set_overlay(&self, val: Option<&str>) {
        let mut overlay = self.overlay.write().unwrap();
        match val {
            Some(x) => *overlay = Some(x.to_string()),
            None => *overlay = None,
        }
    }

    pub fn verify_enable_flag(&self) -> u8 {
        if self.is_to_be_verified() {
            1
        } else {
            0
        }
    }

    pub fn capture_enable_flag(&self) -> u8 {
        if self.is_to_be_captured() {
            1
        } else {
            0
        }
    }

    pub fn overlay_enable_flag(&self) -> u8 {
        if self.has_overlay() {
            1
        } else {
            0
        }
    }

    /// Applies the given state value, making it the new reset baseline and
    /// the device_state
    pub fn reset(&self, val: u8) {
        let mut state = self.state.write().unwrap();
        *state = val;
        let mut device_state = self.device_state.write().unwrap();
        *device_state = val;
    }
}