gba/src/lib.rs

151 lines
5 KiB
Rust
Raw Normal View History

#![no_std]
v0.7.0-remake (#168) * Literally clear the repo to nothing for starters * The screen color changes with key presses There's enough code here that the screen color can change when you press the keys. Right now it's most all unsafe code directly in the example file. The strange thing is that llvm is making us define atomic helper functions when it's not supposed to do that, so we need to investigate more before proceeding too much. * can't tell if this is broken or if mgba is busted on mac * unix dumper * don't panic * oops * GbaCell into its own file, also i had broken the rt irq handler. * closer to an acceptable demo. * make IrqFn into just an alias. * wrap most current mmio types. * more stuff * Use paste to remove a whole bunch of repetition (#166) * gba cells are "unwind safe", they can't be in a bad intermediate state, that's the point * Once again we must thank Yandros for saving us from ourselves. * assembly updates. * oops * finish video MMIO basics, no docs yet. * small note. * simplify asm irq handler stack usage. * ewram support. * more cell stuff. * Remove the non-Zlib dependency, sorry Sp00ph :( * add proper dma support. * small docs improvement. * bitmap and oam declarations * more macros * single bit constants will be extremely useful * add `IntrWait` * rename for clarity * improvements * use the new constant * improve sound support * timers * key support * add a prelude module. * fix doc * more and more assembly (mem fns will need global_asm probably) * more comments. * comment updates. * readme * more video support, and put IrqFn alias into interrupts module. * finish working in the EABI code. * CI config. * funding file. * rustc plz inline these functions when you can :3 * "rt0" is unnecessarily cryptic * overhaul the readme. * phrasing Co-authored-by: Sp00ph <61327188+Sp00ph@users.noreply.github.com>
2022-09-28 02:40:05 +10:00
#![feature(asm_sym)]
#![feature(asm_const)]
#![feature(isa_attribute)]
v0.7.0-remake (#168) * Literally clear the repo to nothing for starters * The screen color changes with key presses There's enough code here that the screen color can change when you press the keys. Right now it's most all unsafe code directly in the example file. The strange thing is that llvm is making us define atomic helper functions when it's not supposed to do that, so we need to investigate more before proceeding too much. * can't tell if this is broken or if mgba is busted on mac * unix dumper * don't panic * oops * GbaCell into its own file, also i had broken the rt irq handler. * closer to an acceptable demo. * make IrqFn into just an alias. * wrap most current mmio types. * more stuff * Use paste to remove a whole bunch of repetition (#166) * gba cells are "unwind safe", they can't be in a bad intermediate state, that's the point * Once again we must thank Yandros for saving us from ourselves. * assembly updates. * oops * finish video MMIO basics, no docs yet. * small note. * simplify asm irq handler stack usage. * ewram support. * more cell stuff. * Remove the non-Zlib dependency, sorry Sp00ph :( * add proper dma support. * small docs improvement. * bitmap and oam declarations * more macros * single bit constants will be extremely useful * add `IntrWait` * rename for clarity * improvements * use the new constant * improve sound support * timers * key support * add a prelude module. * fix doc * more and more assembly (mem fns will need global_asm probably) * more comments. * comment updates. * readme * more video support, and put IrqFn alias into interrupts module. * finish working in the EABI code. * CI config. * funding file. * rustc plz inline these functions when you can :3 * "rt0" is unnecessarily cryptic * overhaul the readme. * phrasing Co-authored-by: Sp00ph <61327188+Sp00ph@users.noreply.github.com>
2022-09-28 02:40:05 +10:00
#![feature(naked_functions)]
2022-10-07 17:28:30 +11:00
//#![warn(missing_docs)]
//! A crate for GBA development.
//!
//! ## How To Make Your Own GBA Project Using This Crate
//!
//! This will require the use of Nightly Rust. Any recent-ish version of Nightly
//! should be fine.
//!
//! [arm-download]:
//! https://developer.arm.com/Tools%20and%20Software/GNU%20Toolchain
//!
//! * **Get The ARM Binutils:** You'll need the ARM version of the GNU binutils
//! in your path, specifically the linker (`arm-none-eabi-ld`). Linux folks
//! can use the package manager. Mac and Windows folks can use the [ARM
//! Website][arm-download].
//! * **Run `rustup component add rust-src`:** This makes rustup keep the
//! standard library source code on hand, which is necessary for `build-std`
//! to work.
//! * **Create A `.cargo/config.toml`:** You'll want to set up a file to provide
//! all the right default settings so that a basic `cargo build` and `cargo
//! run` will "just work". Something like the following is what you probably
//! want.
//!
//! ```toml
//! [build]
//! target = "thumbv4t-none-eabi"
//!
//! [unstable]
//! build-std = ["core"]
//!
//! [target.thumbv4t-none-eabi]
//! runner = "mgba-qt"
//! rustflags = ["-Clink-arg=-Tlinker_scripts/mono_boot.ld"]
//! ```
//!
//! * **Make Your Executables:** At this point you can make a `bin` or an
//! `example` file. Every executable will need to be `#![no_std]` and
//! `#![no_main]`. They will also need a `#[panic_handler]` defined, as well
2022-10-05 17:09:14 +11:00
//! as a `#[no_mangle] extern "C" fn main() -> ! {}` function, which is what
//! the assembly runtime will call to start your Rust program after it fully
//! initializes the system. The C ABI must be used because Rust's own ABI is
//! not stable.
//!
//! ```rust
//! #![no_std]
//! #![no_main]
//!
//! #[panic_handler]
//! fn panic_handler(_: &core::panic::PanicInfo) -> ! {
//! loop {}
//! }
//!
//! #[no_mangle]
//! extern "C" fn main() -> ! {
//! loop {}
//! }
//! ```
//!
//! * **Optional: Use `objcopy` and `gbafix`:** The `cargo build` will produce
//! ELF files, which mGBA can run directly. If you want to run your program on
//! real hardware you'll need to first `objcopy` the raw binary out of the ELF
//! into its own file, then Use `gbafix` to give an appropriate header to the
//! file. `objcopy` is part of the ARM binutils you already installed, it
//! should be named `arm-none-eabi-objcopy`. You can get `gbafix` through
//! cargo: `cargo install gbafix`.
//!
//! ## Other GBA-related Crates
//!
//! This crate provides a largely "unmanaged" interaction with the GBA's
//! hardware. If you would like an API that use the borrow checker to guide you
//! more, the [agb](https://docs.rs/agb) crate might be what you want.
//!
//! ## Safety
//!
//! All safety considerations for the crate assume that you're building for the
//! `thumbv4t-none-eabi` or `armv4t-none-eabi` targets, using the provided
//! linker script, and then running the code on a GBA. While it's possible to
//! break any of these assumptions, if you do that some or all of the code
//! provided by this crate may become unsound.
v0.7.0-remake (#168) * Literally clear the repo to nothing for starters * The screen color changes with key presses There's enough code here that the screen color can change when you press the keys. Right now it's most all unsafe code directly in the example file. The strange thing is that llvm is making us define atomic helper functions when it's not supposed to do that, so we need to investigate more before proceeding too much. * can't tell if this is broken or if mgba is busted on mac * unix dumper * don't panic * oops * GbaCell into its own file, also i had broken the rt irq handler. * closer to an acceptable demo. * make IrqFn into just an alias. * wrap most current mmio types. * more stuff * Use paste to remove a whole bunch of repetition (#166) * gba cells are "unwind safe", they can't be in a bad intermediate state, that's the point * Once again we must thank Yandros for saving us from ourselves. * assembly updates. * oops * finish video MMIO basics, no docs yet. * small note. * simplify asm irq handler stack usage. * ewram support. * more cell stuff. * Remove the non-Zlib dependency, sorry Sp00ph :( * add proper dma support. * small docs improvement. * bitmap and oam declarations * more macros * single bit constants will be extremely useful * add `IntrWait` * rename for clarity * improvements * use the new constant * improve sound support * timers * key support * add a prelude module. * fix doc * more and more assembly (mem fns will need global_asm probably) * more comments. * comment updates. * readme * more video support, and put IrqFn alias into interrupts module. * finish working in the EABI code. * CI config. * funding file. * rustc plz inline these functions when you can :3 * "rt0" is unnecessarily cryptic * overhaul the readme. * phrasing Co-authored-by: Sp00ph <61327188+Sp00ph@users.noreply.github.com>
2022-09-28 02:40:05 +10:00
mod macros;
2018-12-30 14:17:48 +11:00
v0.7.0-remake (#168) * Literally clear the repo to nothing for starters * The screen color changes with key presses There's enough code here that the screen color can change when you press the keys. Right now it's most all unsafe code directly in the example file. The strange thing is that llvm is making us define atomic helper functions when it's not supposed to do that, so we need to investigate more before proceeding too much. * can't tell if this is broken or if mgba is busted on mac * unix dumper * don't panic * oops * GbaCell into its own file, also i had broken the rt irq handler. * closer to an acceptable demo. * make IrqFn into just an alias. * wrap most current mmio types. * more stuff * Use paste to remove a whole bunch of repetition (#166) * gba cells are "unwind safe", they can't be in a bad intermediate state, that's the point * Once again we must thank Yandros for saving us from ourselves. * assembly updates. * oops * finish video MMIO basics, no docs yet. * small note. * simplify asm irq handler stack usage. * ewram support. * more cell stuff. * Remove the non-Zlib dependency, sorry Sp00ph :( * add proper dma support. * small docs improvement. * bitmap and oam declarations * more macros * single bit constants will be extremely useful * add `IntrWait` * rename for clarity * improvements * use the new constant * improve sound support * timers * key support * add a prelude module. * fix doc * more and more assembly (mem fns will need global_asm probably) * more comments. * comment updates. * readme * more video support, and put IrqFn alias into interrupts module. * finish working in the EABI code. * CI config. * funding file. * rustc plz inline these functions when you can :3 * "rt0" is unnecessarily cryptic * overhaul the readme. * phrasing Co-authored-by: Sp00ph <61327188+Sp00ph@users.noreply.github.com>
2022-09-28 02:40:05 +10:00
pub mod asm_runtime;
pub mod bios;
2022-10-05 12:50:40 +11:00
pub mod builtin_art;
v0.7.0-remake (#168) * Literally clear the repo to nothing for starters * The screen color changes with key presses There's enough code here that the screen color can change when you press the keys. Right now it's most all unsafe code directly in the example file. The strange thing is that llvm is making us define atomic helper functions when it's not supposed to do that, so we need to investigate more before proceeding too much. * can't tell if this is broken or if mgba is busted on mac * unix dumper * don't panic * oops * GbaCell into its own file, also i had broken the rt irq handler. * closer to an acceptable demo. * make IrqFn into just an alias. * wrap most current mmio types. * more stuff * Use paste to remove a whole bunch of repetition (#166) * gba cells are "unwind safe", they can't be in a bad intermediate state, that's the point * Once again we must thank Yandros for saving us from ourselves. * assembly updates. * oops * finish video MMIO basics, no docs yet. * small note. * simplify asm irq handler stack usage. * ewram support. * more cell stuff. * Remove the non-Zlib dependency, sorry Sp00ph :( * add proper dma support. * small docs improvement. * bitmap and oam declarations * more macros * single bit constants will be extremely useful * add `IntrWait` * rename for clarity * improvements * use the new constant * improve sound support * timers * key support * add a prelude module. * fix doc * more and more assembly (mem fns will need global_asm probably) * more comments. * comment updates. * readme * more video support, and put IrqFn alias into interrupts module. * finish working in the EABI code. * CI config. * funding file. * rustc plz inline these functions when you can :3 * "rt0" is unnecessarily cryptic * overhaul the readme. * phrasing Co-authored-by: Sp00ph <61327188+Sp00ph@users.noreply.github.com>
2022-09-28 02:40:05 +10:00
pub mod dma;
2022-10-15 17:34:18 +11:00
pub mod fixed;
v0.7.0-remake (#168) * Literally clear the repo to nothing for starters * The screen color changes with key presses There's enough code here that the screen color can change when you press the keys. Right now it's most all unsafe code directly in the example file. The strange thing is that llvm is making us define atomic helper functions when it's not supposed to do that, so we need to investigate more before proceeding too much. * can't tell if this is broken or if mgba is busted on mac * unix dumper * don't panic * oops * GbaCell into its own file, also i had broken the rt irq handler. * closer to an acceptable demo. * make IrqFn into just an alias. * wrap most current mmio types. * more stuff * Use paste to remove a whole bunch of repetition (#166) * gba cells are "unwind safe", they can't be in a bad intermediate state, that's the point * Once again we must thank Yandros for saving us from ourselves. * assembly updates. * oops * finish video MMIO basics, no docs yet. * small note. * simplify asm irq handler stack usage. * ewram support. * more cell stuff. * Remove the non-Zlib dependency, sorry Sp00ph :( * add proper dma support. * small docs improvement. * bitmap and oam declarations * more macros * single bit constants will be extremely useful * add `IntrWait` * rename for clarity * improvements * use the new constant * improve sound support * timers * key support * add a prelude module. * fix doc * more and more assembly (mem fns will need global_asm probably) * more comments. * comment updates. * readme * more video support, and put IrqFn alias into interrupts module. * finish working in the EABI code. * CI config. * funding file. * rustc plz inline these functions when you can :3 * "rt0" is unnecessarily cryptic * overhaul the readme. * phrasing Co-authored-by: Sp00ph <61327188+Sp00ph@users.noreply.github.com>
2022-09-28 02:40:05 +10:00
pub mod gba_cell;
pub mod interrupts;
pub mod keys;
2022-09-28 09:57:09 +10:00
pub mod mgba;
v0.7.0-remake (#168) * Literally clear the repo to nothing for starters * The screen color changes with key presses There's enough code here that the screen color can change when you press the keys. Right now it's most all unsafe code directly in the example file. The strange thing is that llvm is making us define atomic helper functions when it's not supposed to do that, so we need to investigate more before proceeding too much. * can't tell if this is broken or if mgba is busted on mac * unix dumper * don't panic * oops * GbaCell into its own file, also i had broken the rt irq handler. * closer to an acceptable demo. * make IrqFn into just an alias. * wrap most current mmio types. * more stuff * Use paste to remove a whole bunch of repetition (#166) * gba cells are "unwind safe", they can't be in a bad intermediate state, that's the point * Once again we must thank Yandros for saving us from ourselves. * assembly updates. * oops * finish video MMIO basics, no docs yet. * small note. * simplify asm irq handler stack usage. * ewram support. * more cell stuff. * Remove the non-Zlib dependency, sorry Sp00ph :( * add proper dma support. * small docs improvement. * bitmap and oam declarations * more macros * single bit constants will be extremely useful * add `IntrWait` * rename for clarity * improvements * use the new constant * improve sound support * timers * key support * add a prelude module. * fix doc * more and more assembly (mem fns will need global_asm probably) * more comments. * comment updates. * readme * more video support, and put IrqFn alias into interrupts module. * finish working in the EABI code. * CI config. * funding file. * rustc plz inline these functions when you can :3 * "rt0" is unnecessarily cryptic * overhaul the readme. * phrasing Co-authored-by: Sp00ph <61327188+Sp00ph@users.noreply.github.com>
2022-09-28 02:40:05 +10:00
pub mod mmio;
pub mod prelude;
pub mod random;
v0.7.0-remake (#168) * Literally clear the repo to nothing for starters * The screen color changes with key presses There's enough code here that the screen color can change when you press the keys. Right now it's most all unsafe code directly in the example file. The strange thing is that llvm is making us define atomic helper functions when it's not supposed to do that, so we need to investigate more before proceeding too much. * can't tell if this is broken or if mgba is busted on mac * unix dumper * don't panic * oops * GbaCell into its own file, also i had broken the rt irq handler. * closer to an acceptable demo. * make IrqFn into just an alias. * wrap most current mmio types. * more stuff * Use paste to remove a whole bunch of repetition (#166) * gba cells are "unwind safe", they can't be in a bad intermediate state, that's the point * Once again we must thank Yandros for saving us from ourselves. * assembly updates. * oops * finish video MMIO basics, no docs yet. * small note. * simplify asm irq handler stack usage. * ewram support. * more cell stuff. * Remove the non-Zlib dependency, sorry Sp00ph :( * add proper dma support. * small docs improvement. * bitmap and oam declarations * more macros * single bit constants will be extremely useful * add `IntrWait` * rename for clarity * improvements * use the new constant * improve sound support * timers * key support * add a prelude module. * fix doc * more and more assembly (mem fns will need global_asm probably) * more comments. * comment updates. * readme * more video support, and put IrqFn alias into interrupts module. * finish working in the EABI code. * CI config. * funding file. * rustc plz inline these functions when you can :3 * "rt0" is unnecessarily cryptic * overhaul the readme. * phrasing Co-authored-by: Sp00ph <61327188+Sp00ph@users.noreply.github.com>
2022-09-28 02:40:05 +10:00
pub mod sound;
pub mod timers;
pub mod video;
/// Wraps a value to be aligned to a minimum of 4.
///
/// If the size of the value held is already a multiple of 4 then this will be
/// the same size as the wrapped value. Otherwise the compiler will add
/// sufficient padding bytes on the end to make the size a multiple of 4.
#[repr(C, align(4))]
pub struct Align4<T>(pub T);
2022-10-25 03:54:55 +11:00
impl<const N: usize> Align4<[u8; N]> {
/// Views these bytes as a slice of `u32`
/// ## Panics
/// * If the number of bytes isn't a multiple of 4
pub fn as_u32_slice(&self) -> &[u32] {
assert!(self.0.len() % 4 == 0);
// Safety: our struct is aligned to 4, so the pointer will already be
// aligned, we only need to check the length
unsafe {
let data: *const u8 = self.0.as_ptr();
let len: usize = self.0.len();
core::slice::from_raw_parts(data.cast::<u32>(), len / 4)
}
}
/// Views these bytes as a slice of `u16`
/// ## Panics
/// * If the number of bytes isn't a multiple of 2
pub fn as_u16_slice(&self) -> &[u16] {
assert!(self.0.len() % 2 == 0);
// Safety: our struct is aligned to 4, so the pointer will already be
// aligned, we only need to check the length
unsafe {
let data: *const u8 = self.0.as_ptr();
let len: usize = self.0.len();
core::slice::from_raw_parts(data.cast::<u16>(), len / 2)
}
}
}
/// Works like [`include_bytes!`], but the value is wrapped in [`Align4`].
#[macro_export]
macro_rules! include_aligned_bytes {
($file:expr $(,)?) => {{
let LONG_NAME_THAT_DOES_NOT_CLASH = *include_bytes!($file);
Align4(LONG_NAME_THAT_DOES_NOT_CLASH)
}};
}