diff --git a/Cargo.toml b/Cargo.toml index 50099796..1ede30bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ track_caller = [] voladdress = "1.3.0" bitfrob = "1" bracer = "0.1.2" +critical-section = { version = "1.1.2", features = ["restore-state-bool"], optional = true } [profile.dev] opt-level = 3 diff --git a/src/critical_section.rs b/src/critical_section.rs new file mode 100644 index 00000000..9bcb7b1a --- /dev/null +++ b/src/critical_section.rs @@ -0,0 +1,18 @@ +use critical_section::{set_impl, Impl, RawRestoreState}; + +use crate::mmio::IME; + +struct GbaCriticalSection; +set_impl!(GbaCriticalSection); + +unsafe impl Impl for GbaCriticalSection { + unsafe fn acquire() -> RawRestoreState { + let restore = IME.read(); + IME.write(false); + restore + } + + unsafe fn release(restore: RawRestoreState) { + IME.write(restore); + } +} diff --git a/src/lib.rs b/src/lib.rs index 5a2c1094..4cd643c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,6 +91,8 @@ mod macros; pub mod asm_runtime; pub mod bios; pub mod builtin_art; +#[cfg(feature = "critical-section")] +mod critical_section; pub mod dma; pub mod fixed; pub mod gba_cell;