Skip to content

Commit

Permalink
Merge pull request #242 from rmsyn/riscv/mip-csr-macro
Browse files Browse the repository at this point in the history
riscv: define mip using CSR macros
  • Loading branch information
romancardenas authored Nov 27, 2024
2 parents 517d52b + 803a68c commit ca4850a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 36 deletions.
1 change: 1 addition & 0 deletions riscv/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Use CSR helper macros to define `mie` register
- Use CSR helper macros to define `mimpid` register
- Use CSR helper macros to define `misa` register
- Use CSR helper macros to define `mip` register

## [v0.12.1] - 2024-10-20

Expand Down
85 changes: 49 additions & 36 deletions riscv/src/register/mip.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,47 @@
//! mip register
/// mip register
#[derive(Clone, Copy, Debug)]
pub struct Mip {
bits: usize,
read_write_csr! {
/// `mip` register
Mip: 0x344,
mask: 0xaaa,
}

impl Mip {
/// Returns the contents of the register as raw bits
#[inline]
pub fn bits(&self) -> usize {
self.bits
}

read_write_csr_field! {
Mip,
/// Supervisor Software Interrupt Pending
#[inline]
pub fn ssoft(&self) -> bool {
self.bits & (1 << 1) != 0
}
ssoft: 1,
}

read_only_csr_field! {
Mip,
/// Machine Software Interrupt Pending
#[inline]
pub fn msoft(&self) -> bool {
self.bits & (1 << 3) != 0
}
msoft: 3,
}

read_write_csr_field! {
Mip,
/// Supervisor Timer Interrupt Pending
#[inline]
pub fn stimer(&self) -> bool {
self.bits & (1 << 5) != 0
}
stimer: 5,
}

read_only_csr_field! {
Mip,
/// Machine Timer Interrupt Pending
#[inline]
pub fn mtimer(&self) -> bool {
self.bits & (1 << 7) != 0
}
mtimer: 7,
}

read_write_csr_field! {
Mip,
/// Supervisor External Interrupt Pending
#[inline]
pub fn sext(&self) -> bool {
self.bits & (1 << 9) != 0
}
sext: 9,
}

read_only_csr_field! {
Mip,
/// Machine External Interrupt Pending
#[inline]
pub fn mext(&self) -> bool {
self.bits & (1 << 11) != 0
}
mext: 11,
}

read_csr_as!(Mip, 0x344);
set!(0x344);
clear!(0x344);

Expand All @@ -63,3 +54,25 @@ set_clear_csr!(
set_clear_csr!(
/// Supervisor External Interrupt Pending
, set_sext, clear_sext, 1 << 9);

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_mip() {
let mut m = Mip::from_bits(0);

test_csr_field!(m, ssoft);
test_csr_field!(m, stimer);
test_csr_field!(m, sext);

assert!(!m.msoft());
assert!(!m.mtimer());
assert!(!m.mext());

assert!(Mip::from_bits(1 << 3).msoft());
assert!(Mip::from_bits(1 << 7).mtimer());
assert!(Mip::from_bits(1 << 11).mext());
}
}

0 comments on commit ca4850a

Please sign in to comment.