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

Add options to asm #12

Merged
merged 1 commit into from
Sep 11, 2022
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
10 changes: 8 additions & 2 deletions src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
use crate::asm;

/// A no-operation. Useful to prevent delay loops from being optimized away.
///
/// Unlike [barrier], this does not prevent reordering of memory access.
#[inline(always)]
pub fn nop() {
unsafe {
asm!("nop");
// Do not use pure because prevent nop from being removed.
asm!("nop", options(nomem, nostack, preserves_flags));
}
}

/// A compiler fence, prevents instruction reordering.
///
/// Unlike [nop], this does not emit machine code.
#[inline(always)]
pub fn barrier() {
unsafe {
asm!("");
// Do not use `nomem` and `readonly` because prevent preceding and subsequent memory accesses from being reordered.
asm!("", options(nostack, preserves_flags));
}
}
8 changes: 6 additions & 2 deletions src/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ pub fn disable() {
match () {
#[cfg(target_arch = "msp430")]
() => unsafe {
asm!("dint {{ nop");
// Do not use `nomem` and `readonly` because prevent subsequent memory accesses from being reordered before interrupts are disabled.
// Do not use `preserves_flags` because DINT modifies the GIE (global interrupt enable) bit of the status register.
asm!("dint {{ nop", options(nostack));
},
#[cfg(not(target_arch = "msp430"))]
() => {}
Expand All @@ -29,7 +31,9 @@ pub unsafe fn enable() {
match () {
#[cfg(target_arch = "msp430")]
() => {
asm!("nop {{ eint {{ nop");
// Do not use `nomem` and `readonly` because prevent preceding memory accesses from being reordered after interrupts are enabled.
// Do not use `preserves_flags` because EINT modifies the GIE (global interrupt enable) bit of the status register.
asm!("nop {{ eint {{ nop", options(nostack));
}
#[cfg(not(target_arch = "msp430"))]
() => {}
Expand Down
2 changes: 1 addition & 1 deletion src/register/pc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::asm;
pub fn read() -> u16 {
let r;
unsafe {
asm!("mov R0, {0}", out(reg) r);
asm!("mov R0, {0}", out(reg) r, options(nomem, nostack, preserves_flags));
}
r
}
2 changes: 1 addition & 1 deletion src/register/sp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::asm;
pub fn read() -> u16 {
let r;
unsafe {
asm!("mov R1, {0}", out(reg) r);
asm!("mov R1, {0}", out(reg) r, options(nomem, nostack, preserves_flags));
}
r
}
2 changes: 1 addition & 1 deletion src/register/sr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Sr {
pub fn read() -> Sr {
let r: u16;
unsafe {
asm!("mov R2, {0}", out(reg) r);
asm!("mov R2, {0}", out(reg) r, options(nomem, nostack, preserves_flags));
}
Sr { bits: r }
}