From 779770a187d1d88bedf4171210ae04741f447b00 Mon Sep 17 00:00:00 2001 From: Lokathor Date: Sun, 16 Dec 2018 20:55:53 -0700 Subject: [PATCH] cleanup and cleanup designations --- examples/hello_world.rs | 99 ++++++++++++++++++++++++----------------- src/core_extras.rs | 3 ++ src/lib.rs | 5 --- src/video_ram.rs | 2 + 4 files changed, 62 insertions(+), 47 deletions(-) diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 87e6a1c..3866d46 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -20,50 +20,17 @@ macro_rules! const_assert { }; } -#[panic_handler] -fn panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -newtype! { - #[derive(Debug, Clone, Copy, PartialEq, Eq)] - Color, u16 -} - -pub const fn rgb(red: u16, green: u16, blue: u16) -> Color { - Color(blue << 10 | green << 5 | red) -} - -newtype! { - #[derive(Debug, Clone, Copy, PartialEq, Eq)] - DisplayControlSetting, u16 -} - -pub const DISPLAY_CONTROL: VolatilePtr = VolatilePtr(0x04000000 as *mut DisplayControlSetting); -pub const JUST_MODE3_AND_BG2: DisplayControlSetting = DisplayControlSetting(3 + 0b100_0000_0000); - -pub struct Mode3; - -impl Mode3 { - const SCREEN_WIDTH: isize = 240; - const PIXELS: VolatilePtr = VolatilePtr(0x600_0000 as *mut Color); - - pub unsafe fn draw_pixel_unchecked(col: isize, row: isize, color: Color) { - Self::PIXELS.offset(col + row * Self::SCREEN_WIDTH).write(color); - } -} - -#[start] -fn main(_argc: isize, _argv: *const *const u8) -> isize { - unsafe { - DISPLAY_CONTROL.write(JUST_MODE3_AND_BG2); - Mode3::draw_pixel_unchecked(120, 80, rgb(31, 0, 0)); - Mode3::draw_pixel_unchecked(136, 80, rgb(0, 31, 0)); - Mode3::draw_pixel_unchecked(120, 96, rgb(0, 0, 31)); - loop {} - } +#[macro_export] +macro_rules! const_rgb { + ($r:expr, $g:expr, $b:expr) => {{ + const_assert!($r); + const_assert!($g); + const_assert!($b); + Color::new($r, $g, $b) + }}; } +// TODO: kill this #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] #[repr(transparent)] pub struct VolatilePtr(pub *mut T); @@ -78,3 +45,51 @@ impl VolatilePtr { VolatilePtr(self.0.wrapping_offset(count)) } } + +newtype! { + #[derive(Debug, Clone, Copy, PartialEq, Eq)] + Color, u16 +} + +impl Color { + /// Combines the Red, Blue, and Green provided into a single color value. + pub const fn new(red: u16, green: u16, blue: u16) -> Color { + Color(blue << 10 | green << 5 | red) + } +} + +newtype! { + #[derive(Debug, Clone, Copy, PartialEq, Eq)] + DisplayControlSetting, u16 +} + +pub const DISPLAY_CONTROL: VolatilePtr = VolatilePtr(0x0400_0000 as *mut DisplayControlSetting); +pub const JUST_MODE3: DisplayControlSetting = DisplayControlSetting(3); +pub const JUST_BG2: DisplayControlSetting = DisplayControlSetting(0b100_0000_0000); +pub const JUST_MODE3_AND_BG2: DisplayControlSetting = DisplayControlSetting(JUST_MODE3.0 | JUST_BG2.0); + +pub struct Mode3; +impl Mode3 { + const SCREEN_WIDTH: isize = 240; + const PIXELS: VolatilePtr = VolatilePtr(0x600_0000 as *mut Color); + + pub unsafe fn draw_pixel_unchecked(col: isize, row: isize, color: Color) { + Self::PIXELS.offset(col + row * Self::SCREEN_WIDTH).write(color); + } +} + +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + loop {} +} + +#[start] +fn main(_argc: isize, _argv: *const *const u8) -> isize { + unsafe { + DISPLAY_CONTROL.write(JUST_MODE3_AND_BG2); + Mode3::draw_pixel_unchecked(120, 80, const_rgb!(31, 0, 0)); + Mode3::draw_pixel_unchecked(136, 80, const_rgb!(0, 31, 0)); + Mode3::draw_pixel_unchecked(120, 96, const_rgb!(0, 0, 31)); + loop {} + } +} diff --git a/src/core_extras.rs b/src/core_extras.rs index fe82440..1eb96dd 100644 --- a/src/core_extras.rs +++ b/src/core_extras.rs @@ -37,3 +37,6 @@ impl VolatilePtr { VolatilePtr(self.0 as *mut Z) } } + +// TODO: kill all this with fire + diff --git a/src/lib.rs b/src/lib.rs index 7b53307..f00981f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,8 +35,3 @@ pub mod io_registers; pub mod video_ram; pub(crate) use crate::video_ram::*; - -/// Combines the Red, Blue, and Green provided into a single color value. -pub const fn rgb16(red: u16, green: u16, blue: u16) -> u16 { - blue << 10 | green << 5 | red -} diff --git a/src/video_ram.rs b/src/video_ram.rs index 6cb223a..fd47355 100644 --- a/src/video_ram.rs +++ b/src/video_ram.rs @@ -15,6 +15,8 @@ pub use super::*; +// TODO: kill all this too + /// The physical width in pixels of the GBA screen. pub const SCREEN_WIDTH: isize = 240;