rust-console / gba

A crate that helps you make GBA games
https://docs.rs/gba
Apache License 2.0
659 stars 50 forks source link

Add optional backend for `critical-section` #191

Closed ids1024 closed 9 months ago

ids1024 commented 10 months ago

This is similar to the single-threaded bare metal implementations in cortex-m and riscv.

In theory there could be a race condition in acquire if an interrupt occurs between the read and write, and the interrupt disables IME. But it's probably not sensible to have an interrupt disable interrupts, so I don't think it's necessarily to complicate things with inline assembly using swp.

Lokathor commented 10 months ago

I don't object to this, but I also have never heard of this crate before. I guess this is just a commonly used utility crate?

Interrupts actually happen with interrupts disabled (in the program status register) by default, and then our asm interrupt handler re-enabled interrupts as part of switching to System mode for the Rust interrupt handler to run.

I coulda sworn we had a utility fn to cleanly do a swp instruction, but I don't see it anywhere, if we did have it.

ids1024 commented 10 months ago

Yeah, it seems to be commonly provided by basically all the hal crates (or one of their dependencies, anyway).

It sounds useful for creating portable abstractions, and in particular I see embassy-executor and embassy-time seem to use it, so presumably a backend like this would be needed to use those with gba.

Looking at git log -G swp, there were a couple swp related functions before bf853b71d6628bc659270a4b2ea317bf4f103b2f.