Browse Source

Fix Comments on user accesses: cycle.rs, cycleh.rs, instret.rs, and instreth.rs

Reformat mod.rs

Update hpm method to use an index from enum

Move mcounteren macros outside of Mcounteren type declaration
dkhayes117 4 years ago
parent
commit
282345f3de
6 changed files with 316 additions and 286 deletions
  1. 3 2
      src/register/cycle.rs
  2. 3 2
      src/register/cycleh.rs
  3. 3 2
      src/register/instret.rs
  4. 3 2
      src/register/instreth.rs
  5. 297 274
      src/register/mcounteren.rs
  6. 7 4
      src/register/mod.rs

+ 3 - 2
src/register/cycle.rs

@@ -1,6 +1,7 @@
 //! cycle register
-//! Shadow of mcycle register that can be read in user mode
-//! must have mcounteren::cy bit enabled for user mode access
+//! 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
 
 read_csr_as_usize!(0xC00, __read_cycle);
 read_composite_csr!(super::cycleh::read(), read());

+ 3 - 2
src/register/cycleh.rs

@@ -1,5 +1,6 @@
 //! cycleh register
-//! Shadow of mcycleh register that can be read in user mode
-//! must have mcounteren::cy bit enabled for user mode access
+//! 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
 
 read_csr_as_usize_rv32!(0xC80, __read_cycleh);

+ 3 - 2
src/register/instret.rs

@@ -1,6 +1,7 @@
 //! instret register
-//! shadow of minstret register for user mode
-//! mcounteren::ir must be enabled to use in user mode
+//! 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
 
 read_csr_as_usize!(0xC02, __read_instret);
 read_composite_csr!(super::instreth::read(), read());

+ 3 - 2
src/register/instreth.rs

@@ -1,5 +1,6 @@
 //! instreth register
-//! shadow of minstreth register for user mode
-//! mcounteren::ir must be enabled to use in user mode
+//! 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
 
 read_csr_as_usize!(0xC82, __read_instreth);

+ 297 - 274
src/register/mcounteren.rs

@@ -2,7 +2,7 @@
 
 
 use bit_field::BitField;
-use core::mem::size_of;
+//use core::mem::size_of;
 
 /// mcounteren register
 #[derive(Clone, Copy, Debug)]
@@ -11,38 +11,48 @@ pub struct Mcounteren {
 }
 
 
-/*
-/// Cycle enable/disable in mcounteren
-#[derive(Copy, Clone, Debug, Eq, PartialEq)]
-pub enum CY {
-    Enabled = 1,
-    Disabled = 0,
-}
-
-
-/// Time enable/disable in mcounteren
+/// Enable/disable in mcounteren bits
 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
-pub enum Time {
+pub enum State {
     Enabled = 1,
     Disabled = 0,
 }
 
-
-/// Instret enable/disable in mcounteren
+/// Enable/disable in mcounteren bits
 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
-pub enum Instret {
-    Enabled = 1,
-    Disabled = 0,
+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,
 }
 
-*/
 
-/// Enable/disable in mcounteren
-#[derive(Copy, Clone, Debug, Eq, PartialEq)]
-pub enum State {
-    Enabled = 1,
-    Disabled = 0,
-}
 impl Mcounteren {
     /// User "cycle[h]" Enable
     #[inline]
@@ -71,287 +81,298 @@ impl Mcounteren {
         }
     }
 
+    /// 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 "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 "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,
-            }
+    /// User "hpm5" Enable
+    #[inline]
+    pub fn hpm5(&self) -> State {
+        match self.bits.get_bit(5) {
+            true => State::Enabled,
+            false => State::Disabled,
         }
+    }
 
-        /// User "hpm6" Enable
-        #[inline]
-        pub fn hpm6(&self) -> State {
-            match self.bits.get_bit(6){
-                true => State::Enabled,
-                false => State::Disabled,
-            }
+    /// 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 "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 "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 "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 "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 "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 "hpm12" Enable
+    #[inline]
+    pub fn hpm12(&self) -> State {
+        match self.bits.get_bit(12) {
+            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 "hpm13" Enable
+    #[inline]
+    pub fn hpm13(&self) -> State {
+        match self.bits.get_bit(13) {
+            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 "hpm14" Enable
+    #[inline]
+    pub fn hpm14(&self) -> State {
+        match self.bits.get_bit(14) {
+            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,
-            }
+    /// 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,
+        }
+    }
 
-    read_csr_as!(Mcounteren, 0x306, __read_mcounteren);
-    write_csr!(0x306, __write_mcounteren);
-    set!(0x306, __set_mcounteren);
-    clear!(0x306, __clear_mcounteren);
-
-    set_clear_csr!(
-    /// User cycle Enable
-    , set_cy, clear_cy, 1 << 0);
+    /// User "hpm18" Enable
+    #[inline]
+    pub fn hpm18(&self) -> State {
+        match self.bits.get_bit(18) {
+            true => State::Enabled,
+            false => State::Disabled,
+        }
+    }
 
-    set_clear_csr!(
-    /// User time Enable
-    , set_tm, clear_tm, 1 << 1);
+    /// User "hpm19" Enable
+    #[inline]
+    pub fn hpm19(&self) -> State {
+        match self.bits.get_bit(19) {
+            true => State::Enabled,
+            false => State::Disabled,
+        }
+    }
 
-    set_clear_csr!(
-    /// User instret Enable
-    , set_ir, clear_ir, 1 << 2);
+    /// 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);
+write_csr!(0x306, __write_mcounteren);
+set!(0x306, __set_mcounteren);
+clear!(0x306, __clear_mcounteren);
+
+
+
+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);
+
+/*
     set_clear_csr!(
     /// User hpmcounter3 Enable
     , set_hpm3, clear_hpm3, 1 << 3);
@@ -468,4 +489,6 @@ impl Mcounteren {
     set_clear_csr!(
     /// User hpmcounter31 Enable
     , set_hpm31, clear_hpm31, 1 << 31);
-}
+
+ */
+

+ 7 - 4
src/register/mod.rs

@@ -35,14 +35,14 @@ pub mod cycle;
 pub mod instret;
 pub mod time;
 mod hpmcounterx;
+
 pub use self::hpmcounterx::*;
+
 pub mod cycleh;
 pub mod instreth;
 pub mod timeh;
 
 
-
-
 // Supervisor Trap Setup
 // TODO: sedeleg, sideleg
 pub mod sstatus;
@@ -89,8 +89,11 @@ pub mod mip;
 
 // Machine Protection and Translation
 mod pmpcfgx;
+
 pub use self::pmpcfgx::*;
+
 mod pmpaddrx;
+
 pub use self::pmpaddrx::*;
 
 
@@ -98,7 +101,9 @@ pub use self::pmpaddrx::*;
 pub mod mcycle;
 pub mod minstret;
 mod mhpmcounterx;
+
 pub use self::mhpmcounterx::*;
+
 pub mod mcycleh;
 pub mod minstreth;
 
@@ -107,8 +112,6 @@ pub mod minstreth;
 mod mhpmeventx;
 
 
-
-
 pub use self::mhpmeventx::*;