use crate::register::{CpuMode, MemoryAccessType};
use bit_field::BitField;
impl_define_csr!(
Crmd,
"Current Mode Information (CRMD)
The information in this register is used to determine the the processor core’s privilege level,
global interrupt enable bit, watchpoint enable bit, and address translation mode at that time.
"
);
impl_read_csr!(0x0, Crmd);
impl Crmd {
pub fn plv(&self) -> CpuMode {
self.bits.get_bits(0..2).into()
}
pub fn ie(&self) -> bool {
self.bits.get_bit(2)
}
pub fn da(&self) -> bool {
self.bits.get_bit(3)
}
pub fn pg(&self) -> bool {
self.bits.get_bit(4)
}
pub fn datf(&self) -> MemoryAccessType {
self.bits.get_bits(5..=6).into()
}
pub fn datm(&self) -> MemoryAccessType {
self.bits.get_bits(7..=8).into()
}
pub fn we(&self) -> bool {
self.bits.get_bit(9)
}
}
impl core::fmt::Debug for Crmd {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("CrMd")
.field("plv", &self.plv())
.field("ie", &self.ie())
.field("we", &self.we())
.field("is_paging_md", &self.pg())
.field("is_dir_acc", &self.da())
.field("datf", &self.datf())
.field("datm", &self.datm())
.finish()
}
}
pub fn set_plv(mode: CpuMode) {
let mode = mode as usize;
debug_assert!(mode < 4);
set_csr_loong_bits!(0x0, 0..=1, mode);
}
pub fn set_ie(enable: bool) {
set_csr_loong_bit!(0x0, 2, enable);
}
pub fn set_da(da: bool) {
set_csr_loong_bit!(0x0, 3, da);
}
pub fn set_pg(pg: bool) {
set_csr_loong_bit!(0x0, 4, pg);
}
pub fn set_datf(datf: MemoryAccessType) {
set_csr_loong_bits!(0x0, 5..=6, datf as usize);
}
pub fn set_datm(datm: MemoryAccessType) {
set_csr_loong_bits!(0x0, 7..=8, datm as usize);
}
pub fn set_we(we: bool) {
set_csr_loong_bit!(0x0, 9, we);
}