misa.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //! misa register
  2. use core::num::NonZeroUsize;
  3. /// misa register
  4. #[derive(Clone, Copy, Debug)]
  5. pub struct Misa {
  6. bits: NonZeroUsize,
  7. }
  8. /// Machine XLEN
  9. #[derive(Copy, Clone, Debug, Eq, PartialEq)]
  10. pub enum MXL {
  11. XLEN32,
  12. XLEN64,
  13. XLEN128,
  14. }
  15. impl Misa {
  16. /// Returns the contents of the register as raw bits
  17. pub fn bits(&self) -> usize {
  18. self.bits.get()
  19. }
  20. /// Returns the machine xlen.
  21. pub fn mxl(&self) -> MXL {
  22. let value = match () {
  23. #[cfg(target_pointer_width = "32")]
  24. () => (self.bits() >> 30) as u8,
  25. #[cfg(target_pointer_width = "64")]
  26. () => (self.bits() >> 62) as u8,
  27. };
  28. match value {
  29. 1 => MXL::XLEN32,
  30. 2 => MXL::XLEN64,
  31. 3 => MXL::XLEN128,
  32. _ => unreachable!(),
  33. }
  34. }
  35. /// Returns true when the atomic extension is implemented.
  36. pub fn has_extension(&self, extension: char) -> bool {
  37. let bit = extension as u8 - 65;
  38. if bit > 25 {
  39. return false;
  40. }
  41. self.bits() & (1 << bit) == (1 << bit)
  42. }
  43. }
  44. read_csr!(0x301, __read_misa);
  45. /// Reads the CSR
  46. #[inline]
  47. pub fn read() -> Option<Misa> {
  48. let r = unsafe { _read() };
  49. // When misa is hardwired to zero it means that the misa csr
  50. // isn't implemented.
  51. NonZeroUsize::new(r).map(|bits| Misa { bits })
  52. }