diff --git a/moon/src/main.rs b/moon/src/main.rs index c09d2071..ece63c74 100644 --- a/moon/src/main.rs +++ b/moon/src/main.rs @@ -1,6 +1,8 @@ #![no_std] #![no_main] +#![feature(core_intrinsics)] + mod video; mod bootboot; mod panic; @@ -17,7 +19,7 @@ pub extern "C" fn _start() -> ! { init::halt_other_cores(); init::check_magic(); - + video::clear(Color::White); cpu::halt(); diff --git a/moon/src/video.rs b/moon/src/video.rs index 8a34247e..2570416e 100644 --- a/moon/src/video.rs +++ b/moon/src/video.rs @@ -1,6 +1,6 @@ #![allow(dead_code)] -use crate::bootboot::{BOOTBOOT, BOOTBOOT_FB}; +use crate::bootboot::{BOOTBOOT, BOOTBOOT_FB, FB_ARGB, FB_BGRA}; use crate::util::get_bootboot; use BOOTBOOT_FB as FB; @@ -27,10 +27,31 @@ pub enum Color { White = 0xffffffff, } +use core::intrinsics::bswap; + +impl Color { + + fn to_argb(self) -> u32 { + self as u32 + } + + fn to_bgra(self) -> u32 { + bswap(self as u32) + } + + fn to_layout(self, layout: u32) -> u32 { + match layout { + FB_ARGB => self.to_argb(), + FB_BGRA => self.to_bgra(), + _ => panic!("unsupported framebuffer data layout"), + } + } +} + pub fn put_pixel(x: u32, y: u32, color: Color) -> () { let boot: BOOTBOOT = get_bootboot(); let ptr: u64 = FB + (boot.fb_scanline * y) as u64 + (x * 4) as u64; - unsafe { *(ptr as *mut u32) = color as u32 }; + unsafe { *(ptr as *mut u32) = color.to_layout(boot.fb_type as u32) }; } pub fn draw_rect(x: u32, y: u32, width: u32, height: u32, color: Color) -> () { @@ -40,7 +61,7 @@ pub fn draw_rect(x: u32, y: u32, width: u32, height: u32, color: Color) -> () { let addr: u64 = FB + (boot.fb_scanline * i) as u64 + (x * 4) as u64; for ptr in (addr..(addr + (width*4) as u64)).step_by(4) { - unsafe { *(ptr as *mut u32) = color as u32 }; + unsafe { *(ptr as *mut u32) = color.to_layout(boot.fb_type as u32) }; } } }