1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- pub use super::HartIdNumber;
- use crate::common::safe_peripheral;
- #[derive(Clone, Copy, Debug, Eq, PartialEq)]
- pub struct MTIMER {
-
-
- pub mtimecmp0: MTIMECMP,
-
- pub mtime: MTIME,
- }
- impl MTIMER {
-
-
-
-
-
- #[inline]
- pub const unsafe fn new(mtimecmp: usize, mtime: usize) -> Self {
- Self {
- mtimecmp0: MTIMECMP::new(mtimecmp),
- mtime: MTIME::new(mtime),
- }
- }
-
-
-
-
-
- #[inline]
- pub fn mtimecmp<H: HartIdNumber>(&self, hart_id: H) -> MTIMECMP {
-
- unsafe { MTIMECMP::new(self.mtimecmp0.get_ptr().offset(hart_id.number() as _) as _) }
- }
-
-
-
-
-
-
- #[inline]
- pub fn mtimecmp_mhartid(&self) -> MTIMECMP {
- let hart_id = riscv::register::mhartid::read();
-
- unsafe { MTIMECMP::new(self.mtimecmp0.get_ptr().add(hart_id) as _) }
- }
- }
- safe_peripheral!(MTIMECMP, u64, RW);
- safe_peripheral!(MTIME, u64, RW);
- #[cfg(test)]
- mod test {
- use super::super::test::HartId;
- use super::*;
- #[test]
- fn check_mtimer() {
-
- let raw_mtimecmp = [0u64; HartId::MAX_HART_ID_NUMBER as usize + 1];
- let raw_mtime = 0u64;
-
- let mtimer =
- unsafe { MTIMER::new(raw_mtimecmp.as_ptr() as _, &raw_mtime as *const u64 as _) };
- assert_eq!(
- mtimer.mtimecmp(HartId::H0).get_ptr() as usize,
- raw_mtimecmp.as_ptr() as usize
- );
- assert_eq!(mtimer.mtimecmp(HartId::H1).get_ptr() as usize, unsafe {
- raw_mtimecmp.as_ptr().offset(1)
- }
- as usize);
- assert_eq!(mtimer.mtimecmp(HartId::H2).get_ptr() as usize, unsafe {
- raw_mtimecmp.as_ptr().offset(2)
- }
- as usize);
- assert_eq!(
- mtimer.mtime.get_ptr() as usize,
- &raw_mtime as *const u64 as _
- );
- }
- }
|