stvec.rs 891 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //! stvec register
  2. /// stvec register
  3. #[derive(Clone, Copy, Debug)]
  4. pub struct Stvec {
  5. bits: usize,
  6. }
  7. /// Trap mode
  8. pub enum TrapMode {
  9. Direct = 0,
  10. Vectored = 1,
  11. }
  12. impl Stvec {
  13. /// Returns the contents of the register as raw bits
  14. pub fn bits(&self) -> usize {
  15. self.bits
  16. }
  17. /// Returns the trap-vector base-address
  18. pub fn address(&self) -> usize {
  19. self.bits - (self.bits & 0b11)
  20. }
  21. /// Returns the trap-vector mode
  22. pub fn trap_mode(&self) -> TrapMode {
  23. let mode = self.bits & 0b11;
  24. match mode {
  25. 0 => TrapMode::Direct,
  26. 1 => TrapMode::Vectored,
  27. _ => unimplemented!()
  28. }
  29. }
  30. }
  31. read_csr_as!(Stvec, 0x105, __read_stvec);
  32. write_csr!(0x105, __write_stvec);
  33. /// Writes the CSR
  34. #[inline]
  35. pub unsafe fn write(addr: usize, mode: TrapMode) {
  36. _write(addr + mode as usize);
  37. }