diff --git a/agb/src/lib.rs b/agb/src/lib.rs index 57cb1b64..28bfc684 100644 --- a/agb/src/lib.rs +++ b/agb/src/lib.rs @@ -12,6 +12,7 @@ )] #![feature(alloc_error_handler)] #![feature(allocator_api)] +#![feature(asm_const)] #![warn(clippy::all)] #![deny(clippy::must_use_candidate)] #![deny(clippy::trivially_copy_pass_by_ref)] diff --git a/agb/src/syscall.rs b/agb/src/syscall.rs index cf28c71a..69ff353f 100644 --- a/agb/src/syscall.rs +++ b/agb/src/syscall.rs @@ -4,10 +4,19 @@ use core::arch::asm; #[allow(non_snake_case)] +const fn swi_map(thumb_id: u32) -> u32 { + if cfg!(target_feature="thumb-mode") { + thumb_id + } else { + thumb_id << 16 + } +} + pub fn halt() { unsafe { asm!( - "swi 0x02", + "swi {SWI}", + SWI = const { swi_map(0x02) }, lateout("r0") _, lateout("r1") _, lateout("r2") _, @@ -19,7 +28,8 @@ pub fn halt() { pub fn stop() { unsafe { asm!( - "swi 0x03", + "swi {SWI}", + SWI = const { swi_map(0x03) }, lateout("r0") _, lateout("r1") _, lateout("r2") _, @@ -31,7 +41,8 @@ pub fn stop() { pub fn wait_for_interrupt() { unsafe { asm!( - "swi 0x04", + "swi {SWI}", + SWI = const { swi_map(0x04) }, lateout("r0") _, lateout("r1") _, lateout("r2") _, @@ -45,7 +56,8 @@ pub fn wait_for_interrupt() { pub fn wait_for_vblank() { unsafe { asm!( - "swi 0x05", + "swi {SWI}", + SWI = const { swi_map(0x05) }, lateout("r0") _, lateout("r1") _, lateout("r2") _, @@ -61,7 +73,8 @@ pub fn div(numerator: i32, denominator: i32) -> (i32, i32, i32) { let abs_divide: i32; unsafe { asm!( - "swi 0x06", + "swi {SWI}", + SWI = const { swi_map(0x06) }, in("r0") numerator, in("r1") denominator, lateout("r0") divide, @@ -77,7 +90,8 @@ pub fn sqrt(n: i32) -> i32 { let result: i32; unsafe { asm!( - "swi 0x08", + "swi {SWI}", + SWI = const { swi_map(0x08) }, in("r0") n, lateout("r0") result, lateout("r1") _, @@ -93,7 +107,8 @@ pub fn arc_tan(n: i16) -> i16 { let result: i16; unsafe { asm!( - "swi 0x09", + "swi {SWI}", + SWI = const { swi_map(0x09) }, in("r0") n, lateout("r0") result, lateout("r1") _, @@ -109,7 +124,8 @@ pub fn arc_tan2(x: i16, y: i32) -> i16 { let result: i16; unsafe { asm!( - "swi 0x09", + "swi {SWI}", + SWI = const { swi_map(0x09) }, in("r0") x, in("r1") y, lateout("r0") result,