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
/*
 * File: fpga_info.rs
 * Project: defined
 * Created Date: 05/10/2023
 * Author: Shun Suzuki
 * -----
 * Last Modified: 05/10/2023
 * Modified By: Shun Suzuki (suzuki@hapis.k.u-tokyo.ac.jp)
 * -----
 * Copyright (c) 2023 Shun Suzuki. All rights reserved.
 *
 */

use crate::cpu::RxMessage;

/// FPGA information
#[repr(C)]
pub struct FPGAInfo {
    info: u8,
}

impl FPGAInfo {
    pub const fn new(info: u8) -> Self {
        Self { info }
    }

    /// Check if thermal sensor is asserted
    pub const fn is_thermal_assert(&self) -> bool {
        (self.info & 0x01) != 0
    }

    pub const fn info(&self) -> u8 {
        self.info
    }
}

impl From<&RxMessage> for FPGAInfo {
    fn from(msg: &RxMessage) -> Self {
        Self { info: msg.data }
    }
}

#[cfg(test)]
mod tests {
    use std::mem::size_of;

    use super::*;

    #[test]
    fn fpga_info() {
        assert_eq!(size_of::<FPGAInfo>(), 1);

        let info = FPGAInfo::new(0x00);
        assert!(!info.is_thermal_assert());
        assert_eq!(info.info(), 0x00);

        let info = FPGAInfo::new(0x01);
        assert!(info.is_thermal_assert());
        assert_eq!(info.info(), 0x01);

        let info = FPGAInfo::new(0x02);
        assert!(!info.is_thermal_assert());
        assert_eq!(info.info(), 0x02);

        let rx = RxMessage { ack: 0, data: 0x01 };
        let info = FPGAInfo::from(&rx);
        assert!(info.is_thermal_assert());
        assert_eq!(info.info(), 0x01);
    }
}