From 39c48bc46225eb79943f42c1189d6c5d26721b2e Mon Sep 17 00:00:00 2001 From: Lokathor Date: Tue, 18 Oct 2022 09:44:23 -0600 Subject: [PATCH] aligned bytes, Closes: https://github.com/rust-console/gba/issues/79 --- CHANGELOG.md | 2 ++ examples/foo.txt | 1 + examples/hello.rs | 8 ++++---- src/lib.rs | 21 +++++++++++++++++++++ src/prelude.rs | 3 ++- src/random.rs | 1 + 6 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 examples/foo.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index 5884e62..d635dd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ * **0.9.1:** * Adds some randomization support directly into the crate. * Added more methods to the `Fixed` type. + * Adds an `include_aligned_bytes!` macro to pull in compile time data that's + aligned to 4. * **0.9.0:** * **MSRV:** The crate now requires `compiler_builtins-0.1.81` to build. You will need a Nightly from 2022-10-15 or later. diff --git a/examples/foo.txt b/examples/foo.txt new file mode 100644 index 0000000..c246d3c --- /dev/null +++ b/examples/foo.txt @@ -0,0 +1 @@ +foo_ \ No newline at end of file diff --git a/examples/hello.rs b/examples/hello.rs index 5fc8807..bdeace5 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -2,10 +2,7 @@ #![no_main] use core::fmt::Write; -use gba::{ - mgba::{MgbaBufferedLogger, MgbaMessageLevel}, - prelude::*, -}; +use gba::prelude::*; #[panic_handler] fn panic_handler(info: &core::panic::PanicInfo) -> ! { @@ -15,6 +12,9 @@ fn panic_handler(info: &core::panic::PanicInfo) -> ! { loop {} } +#[allow(dead_code)] +const FOO_: Align4<[u8; 4]> = include_aligned_bytes!("foo.txt"); + #[link_section = ".ewram"] static FRAME_KEYS: GbaCell = GbaCell::new(KeyInput::new()); diff --git a/src/lib.rs b/src/lib.rs index f5eccf7..2a4fd6e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,3 +101,24 @@ pub mod random; 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(pub T); + +/// As [`include_bytes!`] but the value is wrapped in [`Align4`] +/// +/// ## Panics +/// * The included number of bytes must be a multiple of 4. +#[macro_export] +macro_rules! include_aligned_bytes { + ($file:expr $(,)?) => {{ + let LONG_NAME_THAT_DOES_NOT_CLASH = *include_bytes!($file); + assert!(LONG_NAME_THAT_DOES_NOT_CLASH.len() % 4 == 0); + Align4(LONG_NAME_THAT_DOES_NOT_CLASH) + }}; +} diff --git a/src/prelude.rs b/src/prelude.rs index d70b2a9..93ccee1 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -2,5 +2,6 @@ pub use crate::{ asm_runtime::*, bios::*, builtin_art::*, dma::*, fixed::*, gba_cell::*, - interrupts::*, keys::*, mmio::*, sound::*, timers::*, video::*, + include_aligned_bytes, interrupts::*, keys::*, mgba::*, mmio::*, sound::*, + timers::*, video::*, Align4, }; diff --git a/src/random.rs b/src/random.rs index a75f4cb..af490da 100644 --- a/src/random.rs +++ b/src/random.rs @@ -124,6 +124,7 @@ pub trait Gen32 { } impl Gen32 for Lcg32 { + #[inline] fn next_u32(&mut self) -> u32 { Lcg32::next_u32(self) }