浏览代码

Fix with Cargo fmt
Add scounteren.rs
Implemented set_hpm and clear_hpm functions
Removed State enum, replaced with bool
Update comments to be more concise

dkhayes117 4 年之前
父节点
当前提交
42fa92f501
共有 7 个文件被更改,包括 97 次插入458 次删除
  1. 2 2
      src/register/cycle.rs
  2. 2 2
      src/register/cycleh.rs
  3. 2 2
      src/register/instret.rs
  4. 2 2
      src/register/instreth.rs
  5. 22 449
      src/register/mcounteren.rs
  6. 1 1
      src/register/mod.rs
  7. 66 0
      src/register/scounteren.rs

+ 2 - 2
src/register/cycle.rs

@@ -1,7 +1,7 @@
 //! cycle register
 //! Shadow of mcycle register
-//! must have mcounteren::cy bit enabled for use in supervisor mode (if implemented)
-//! if supervisor mode is not implemented this register will control user mode access
+//! must have `scounter::cy` or `mcounteren::cy` bit enabled depending on whether
+//! S-mode is implemented or not
 
 read_csr_as_usize!(0xC00, __read_cycle);
 read_composite_csr!(super::cycleh::read(), read());

+ 2 - 2
src/register/cycleh.rs

@@ -1,6 +1,6 @@
 //! cycleh register
 //! Shadow of mcycleh register (rv32)
-//! must have mcounteren::cy bit enabled for use in supervisor mode (if implemented)
-//! if supervisor mode is not implemented this register will control user mode access
+//! must have `scounter::cy` or `mcounteren::cy` bit enabled depending on whether
+//! S-mode is implemented or not
 
 read_csr_as_usize_rv32!(0xC80, __read_cycleh);

+ 2 - 2
src/register/instret.rs

@@ -1,7 +1,7 @@
 //! instret register
 //! Shadow of minstret register
-//! must have mcounteren::ir bit enabled for use in supervisor mode (if implemented)
-//! if supervisor mode is not implemented this register will control user mode access
+//! must have `scounter::ir` or `mcounteren::ir` bit enabled depending on whether
+//! S-mode is implemented or not
 
 read_csr_as_usize!(0xC02, __read_instret);
 read_composite_csr!(super::instreth::read(), read());

+ 2 - 2
src/register/instreth.rs

@@ -1,6 +1,6 @@
 //! instreth register
 //! Shadow of minstreth register (rv32)
-//! must have mcounteren::ir bit enabled for use in supervisor mode (if implemented)
-//! if supervisor mode is not implemented this register will control user mode access
+//! must have `scounter::ir` or `mcounteren::ir` bit enabled depending on whether
+//! S-mode is implemented or not
 
 read_csr_as_usize!(0xC82, __read_instreth);

+ 22 - 449
src/register/mcounteren.rs

@@ -1,8 +1,6 @@
 //! mcounteren register
 
-
 use bit_field::BitField;
-//use core::mem::size_of;
 
 /// mcounteren register
 #[derive(Clone, Copy, Debug)]
@@ -10,347 +8,31 @@ pub struct Mcounteren {
     bits: usize,
 }
 
-
-/// Enable/disable in mcounteren bits
-#[derive(Copy, Clone, Debug, Eq, PartialEq)]
-pub enum State {
-    Enabled = 1,
-    Disabled = 0,
-}
-
-/// Enable/disable in mcounteren bits
-#[derive(Copy, Clone, Debug, Eq, PartialEq)]
-pub enum Index {
-    Hpm3 = 3,
-    Hpm4 = 4,
-    Hpm5 = 5,
-    Hpm6 = 6,
-    Hpm7 = 7,
-    Hpm8 = 8,
-    Hpm9 = 9,
-    Hpm10 = 10,
-    Hpm11 = 11,
-    Hpm12 = 12,
-    Hpm13 = 13,
-    Hpm14 = 14,
-    Hpm15 = 15,
-    Hpm16 = 16,
-    Hpm17 = 17,
-    Hpm18 = 18,
-    Hpm19 = 19,
-    Hpm20 = 20,
-    Hpm21 = 21,
-    Hpm22 = 22,
-    Hpm23 = 23,
-    Hpm24 = 24,
-    Hpm25 = 25,
-    Hpm26 = 26,
-    Hpm27 = 27,
-    Hpm28 = 28,
-    Hpm29 = 29,
-    Hpm30 = 30,
-    Hpm31 = 31,
-}
-
-
 impl Mcounteren {
     /// User "cycle[h]" Enable
     #[inline]
-    pub fn cy(&self) -> State {
-        match self.bits.get_bit(0) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
+    pub fn cy(&self) -> bool {
+        self.bits.get_bit(0)
     }
 
     /// User "time[h]" Enable
     #[inline]
-    pub fn tm(&self) -> State {
-        match self.bits.get_bit(1) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
+    pub fn tm(&self) -> bool {
+        self.bits.get_bit(1)
     }
 
     /// User "instret[h]" Enable
     #[inline]
-    pub fn ir(&self) -> State {
-        match self.bits.get_bit(2) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
+    pub fn ir(&self) -> bool {
+        self.bits.get_bit(2)
     }
 
     /// User "hpm[x]" Enable (bits 3-31)
     #[inline]
-    pub fn hpm(&self, index: Index) -> State {
-        match self.bits.get_bit(index as usize) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /*
-    /// User "hpm3" Enable
-    #[inline]
-    pub fn hpm3(&self) -> State {
-        match self.bits.get_bit(3) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm4" Enable
-    #[inline]
-    pub fn hpm4(&self) -> State {
-        match self.bits.get_bit(4) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm5" Enable
-    #[inline]
-    pub fn hpm5(&self) -> State {
-        match self.bits.get_bit(5) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
+    pub fn hpm(&self, index: usize) -> bool {
+        assert!(((3..32).contains(&index)));
+        self.bits.get_bit(index)
     }
-
-    /// User "hpm6" Enable
-    #[inline]
-    pub fn hpm6(&self) -> State {
-        match self.bits.get_bit(6) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm7" Enable
-    #[inline]
-    pub fn hpm7(&self) -> State {
-        match self.bits.get_bit(7) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm8" Enable
-    #[inline]
-    pub fn hpm8(&self) -> State {
-        match self.bits.get_bit(8) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm9" Enable
-    #[inline]
-    pub fn hpm9(&self) -> State {
-        match self.bits.get_bit(9) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm10" Enable
-    #[inline]
-    pub fn hpm10(&self) -> State {
-        match self.bits.get_bit(10) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm11" Enable
-    #[inline]
-    pub fn hpm11(&self) -> State {
-        match self.bits.get_bit(11) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm12" Enable
-    #[inline]
-    pub fn hpm12(&self) -> State {
-        match self.bits.get_bit(12) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm13" Enable
-    #[inline]
-    pub fn hpm13(&self) -> State {
-        match self.bits.get_bit(13) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm14" Enable
-    #[inline]
-    pub fn hpm14(&self) -> State {
-        match self.bits.get_bit(14) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm15" Enable
-    #[inline]
-    pub fn hpm15(&self) -> State {
-        match self.bits.get_bit(15) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm16" Enable
-    #[inline]
-    pub fn hpm16(&self) -> State {
-        match self.bits.get_bit(16) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm17" Enable
-    #[inline]
-    pub fn hpm17(&self) -> State {
-        match self.bits.get_bit(17) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm18" Enable
-    #[inline]
-    pub fn hpm18(&self) -> State {
-        match self.bits.get_bit(18) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm19" Enable
-    #[inline]
-    pub fn hpm19(&self) -> State {
-        match self.bits.get_bit(19) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm20" Enable
-    #[inline]
-    pub fn hpm20(&self) -> State {
-        match self.bits.get_bit(20) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm21" Enable
-    #[inline]
-    pub fn hpm21(&self) -> State {
-        match self.bits.get_bit(21) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm22" Enable
-    #[inline]
-    pub fn hpm22(&self) -> State {
-        match self.bits.get_bit(22) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm23" Enable
-    #[inline]
-    pub fn hpm23(&self) -> State {
-        match self.bits.get_bit(23) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm24" Enable
-    #[inline]
-    pub fn hpm24(&self) -> State {
-        match self.bits.get_bit(24) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm25" Enable
-    #[inline]
-    pub fn hpm25(&self) -> State {
-        match self.bits.get_bit(25) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm26" Enable
-    #[inline]
-    pub fn hpm26(&self) -> State {
-        match self.bits.get_bit(26) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm27" Enable
-    #[inline]
-    pub fn hpm27(&self) -> State {
-        match self.bits.get_bit(27) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm24" Enable
-    #[inline]
-    pub fn hpm28(&self) -> State {
-        match self.bits.get_bit(28) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm29" Enable
-    #[inline]
-    pub fn hpm29(&self) -> State {
-        match self.bits.get_bit(29) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm30" Enable
-    #[inline]
-    pub fn hpm30(&self) -> State {
-        match self.bits.get_bit(30) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }
-
-    /// User "hpm31" Enable
-    #[inline]
-    pub fn hpm31(&self) -> State {
-        match self.bits.get_bit(31) {
-            true => State::Enabled,
-            false => State::Disabled,
-        }
-    }*/
 }
 
 read_csr_as!(Mcounteren, 0x306, __read_mcounteren);
@@ -362,133 +44,24 @@ clear!(0x306, __clear_mcounteren);
 
 set_clear_csr!(
 /// User cycle Enable
-, set_cy, clear_cy, 1 << 0);
+    , set_cy, clear_cy, 1 << 0);
 
 set_clear_csr!(
 /// User time Enable
-, set_tm, clear_tm, 1 << 1);
+    , set_tm, clear_tm, 1 << 1);
 
 set_clear_csr!(
 /// User instret Enable
-, set_ir, clear_ir, 1 << 2);
-
-/*
-    set_clear_csr!(
-    /// User hpmcounter3 Enable
-    , set_hpm3, clear_hpm3, 1 << 3);
+    , set_ir, clear_ir, 1 << 2);
 
-    set_clear_csr!(
-    /// User hpmcounter4 Enable
-    , set_hpm4, clear_hpm4, 1 << 4);
-
-    set_clear_csr!(
-    /// User hpmcounter5 Enable
-    , set_hpm5, clear_hpm5, 1 << 5);
-
-    set_clear_csr!(
-    /// User hpmcounter6 Enable
-    , set_hpm6, clear_hpm6, 1 << 6);
-
-    set_clear_csr!(
-    /// User hpmcounter7 Enable
-    , set_hpm7, clear_hpm7, 1 << 7);
-
-    set_clear_csr!(
-    /// User hpmcounter8 Enable
-    , set_hpm8, clear_hpm8, 1 << 8);
-
-    set_clear_csr!(
-    /// User hpmcounter9 Enable
-    , set_hpm9, clear_hpm9, 1 << 9);
-
-    set_clear_csr!(
-    /// User hpmcounter10 Enable
-    , set_hpm10, clear_hpm10, 1 << 10);
-
-    set_clear_csr!(
-    /// User hpmcounter11 Enable
-    , set_hpm11, clear_hpm11, 1 << 11);
-
-    set_clear_csr!(
-    /// User hpmcounter12 Enable
-    , set_hpm12, clear_hpm12, 1 << 12);
-
-    set_clear_csr!(
-    /// User hpmcounter13 Enable
-    , set_hpm13, clear_hpm13, 1 << 13);
-
-
-    set_clear_csr!(
-    /// User hpmcounter14 Enable
-    , set_hpm14, clear_hpm14, 1 << 14);
-
-    set_clear_csr!(
-    /// User hpmcounter15 Enable
-    , set_hpm15, clear_hpm15, 1 << 15);
-
-    set_clear_csr!(
-    /// User hpmcounter16 Enable
-    , set_hpm16, clear_hpm16, 1 << 16);
-
-    set_clear_csr!(
-    /// User hpmcounter17 Enable
-    , set_hpm17, clear_hpm17, 1 << 17);
-
-    set_clear_csr!(
-    /// User hpmcounter18 Enable
-    , set_hpm18, clear_hpm18, 1 << 18);
-
-    set_clear_csr!(
-    /// User hpmcounter19 Enable
-    , set_hpm19, clear_hpm19, 1 << 19);
-
-    set_clear_csr!(
-    /// User hpmcounter20 Enable
-    , set_hpm20, clear_hpm20, 1 << 20);
-
-    set_clear_csr!(
-    /// User hpmcounter21 Enable
-    , set_hpm21, clear_hpm21, 1 << 21);
-
-    set_clear_csr!(
-    /// User hpmcounter22 Enable
-    , set_hpm22, clear_hpm22, 1 << 22);
-
-    set_clear_csr!(
-    /// User hpmcounter23 Enable
-    , set_hpm23, clear_hpm23, 1 << 23);
-
-    set_clear_csr!(
-    /// User hpmcounter24 Enable
-    , set_hpm24, clear_hpm24, 1 << 24);
-
-    set_clear_csr!(
-    /// User hpmcounter25 Enable
-    , set_hpm25, clear_hpm25, 1 << 25);
-
-    set_clear_csr!(
-    /// User hpmcounter26 Enable
-    , set_hpm26, clear_hpm26, 1 << 26);
-
-    set_clear_csr!(
-    /// User hpmcounter27 Enable
-    , set_hpm27, clear_hpm27, 1 << 27);
-
-    set_clear_csr!(
-    /// User hpmcounter28 Enable
-    , set_hpm28, clear_hpm28, 1 << 28);
-
-    set_clear_csr!(
-    /// User hpmcounter29 Enable
-    , set_hpm29, clear_hpm29, 1 << 29);
-
-    set_clear_csr!(
-    /// User hpmcounter30 Enable
-    , set_hpm30, clear_hpm30, 1 << 30);
-
-    set_clear_csr!(
-    /// User hpmcounter31 Enable
-    , set_hpm31, clear_hpm31, 1 << 31);
-
- */
+#[inline]
+pub unsafe fn set_hpm(index: usize) {
+    assert!(((3..32).contains(&index)));
+    _set(1 << index);
+}
 
+#[inline]
+pub unsafe fn clear_hpm(index: usize) {
+    assert!(((3..32).contains(&index)));
+    _clear(1 << index);
+}

+ 1 - 1
src/register/mod.rs

@@ -48,7 +48,7 @@ pub mod timeh;
 pub mod sstatus;
 pub mod sie;
 pub mod stvec;
-// TODO: scounteren
+pub mod scounteren;
 
 
 // Supervisor Trap Handling

+ 66 - 0
src/register/scounteren.rs

@@ -0,0 +1,66 @@
+//! scounteren register
+
+use bit_field::BitField;
+
+/// scounteren register
+#[derive(Clone, Copy, Debug)]
+pub struct Scounteren {
+    bits: usize,
+}
+
+impl Scounteren {
+    /// User "cycle[h]" Enable
+    #[inline]
+    pub fn cy(&self) -> bool {
+        self.bits.get_bit(0)
+    }
+
+
+    /// User "time[h]" Enable
+    #[inline]
+    pub fn tm(&self) -> bool {
+        self.bits.get_bit(1)
+    }
+
+    /// User "instret[h]" Enable
+    #[inline]
+    pub fn ir(&self) -> bool {
+        self.bits.get_bit(2)
+    }
+
+    /// User "hpm[x]" Enable (bits 3-31)
+    #[inline]
+    pub fn hpm(&self, index: usize) -> bool {
+        assert!(((3..32).contains(&index)));
+        self.bits.get_bit(index)
+    }
+}
+
+read_csr_as!(Scounteren, 0x106, __read_scounteren);
+write_csr!(0x106, __write_scounteren);
+set!(0x106, __set_scounteren);
+clear!(0x106, __clear_scounteren);
+
+set_clear_csr!(
+/// User cycle Enable
+    , set_cy, clear_cy, 1 << 0);
+
+set_clear_csr!(
+/// User time Enable
+    , set_tm, clear_tm, 1 << 1);
+
+set_clear_csr!(
+/// User instret Enable
+    , set_ir, clear_ir, 1 << 2);
+
+#[inline]
+pub unsafe fn set_hpm(index: usize) {
+    assert!(((3..32).contains(&index)));
+    _set(1 << index);
+}
+
+#[inline]
+pub unsafe fn clear_hpm(index: usize) {
+    assert!(((3..32).contains(&index)));
+    _clear(1 << index);
+}