summaryrefslogtreecommitdiff
path: root/src/exceptions.rs
diff options
context:
space:
mode:
authortaitep <taitep@taitep.se>2025-12-27 21:33:39 +0100
committertaitep <taitep@taitep.se>2025-12-27 21:33:39 +0100
commit5c008bfc0446e4631dbab64be61159af04f78dd1 (patch)
tree852f7ee883f675b4c67cf424b8f7d17357e7742d /src/exceptions.rs
parentb5d36b7969f2759147d58a80e0e5b62c215d2998 (diff)
Add exception values (what will go in mtval/stval)
Diffstat (limited to 'src/exceptions.rs')
-rw-r--r--src/exceptions.rs67
1 files changed, 51 insertions, 16 deletions
diff --git a/src/exceptions.rs b/src/exceptions.rs
index 9a84edc..5e3f02f 100644
--- a/src/exceptions.rs
+++ b/src/exceptions.rs
@@ -6,6 +6,8 @@
use int_enum::IntEnum;
+use crate::consts::{Addr, DWord};
+
#[repr(u8)]
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, IntEnum)]
@@ -29,6 +31,12 @@ pub enum ExceptionType {
HardwareError = 19,
}
+#[derive(Debug, Clone, Copy)]
+pub struct Exception {
+ pub type_: ExceptionType,
+ pub value: DWord,
+}
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemoryExceptionType {
AddressMisaligned,
@@ -36,28 +44,55 @@ pub enum MemoryExceptionType {
PageFault,
}
-impl MemoryExceptionType {
- pub(crate) fn to_exception_store(&self) -> ExceptionType {
- match self {
- Self::AddressMisaligned => ExceptionType::StoreAmoAddressMisaligned,
- Self::AccessFault => ExceptionType::StoreAmoAccessFault,
- Self::PageFault => ExceptionType::StoreAmoPageFault,
+#[derive(Debug, Clone, Copy)]
+pub struct MemoryException {
+ pub type_: MemoryExceptionType,
+ pub addr: Addr,
+}
+
+impl MemoryException {
+ pub(crate) fn to_exception_store(&self) -> Exception {
+ Exception {
+ type_: match self.type_ {
+ MemoryExceptionType::AddressMisaligned => ExceptionType::StoreAmoAddressMisaligned,
+ MemoryExceptionType::AccessFault => ExceptionType::StoreAmoAccessFault,
+ MemoryExceptionType::PageFault => ExceptionType::StoreAmoPageFault,
+ },
+ value: self.addr,
}
}
- pub(crate) fn to_exception_instr(&self) -> ExceptionType {
- match self {
- Self::AddressMisaligned => ExceptionType::InstructionAddressMisaligned,
- Self::AccessFault => ExceptionType::InstructionAccessFault,
- Self::PageFault => ExceptionType::InstructionPageFault,
+ pub(crate) fn to_exception_instr(&self) -> Exception {
+ Exception {
+ type_: match self.type_ {
+ MemoryExceptionType::AddressMisaligned => ExceptionType::InstructionAccessFault,
+ MemoryExceptionType::AccessFault => ExceptionType::InstructionAccessFault,
+ MemoryExceptionType::PageFault => ExceptionType::InstructionPageFault,
+ },
+ value: self.addr,
}
}
- pub(crate) fn to_exception_load(&self) -> ExceptionType {
- match self {
- Self::AddressMisaligned => ExceptionType::LoadAddressMisaligned,
- Self::AccessFault => ExceptionType::LoadAccessFault,
- Self::PageFault => ExceptionType::LoadPageFault,
+ pub(crate) fn to_exception_load(&self) -> Exception {
+ Exception {
+ type_: match self.type_ {
+ MemoryExceptionType::AddressMisaligned => ExceptionType::LoadAddressMisaligned,
+ MemoryExceptionType::AccessFault => ExceptionType::LoadAccessFault,
+ MemoryExceptionType::PageFault => ExceptionType::LoadPageFault,
+ },
+ value: self.addr,
}
}
}
+
+impl Into<MemoryExceptionType> for MemoryException {
+ fn into(self) -> MemoryExceptionType {
+ self.type_
+ }
+}
+
+impl Into<ExceptionType> for Exception {
+ fn into(self) -> ExceptionType {
+ self.type_
+ }
+}