Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

riscv: define mip using CSR macros #242

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}