Adds sample rust program

This commit is contained in:
2025-11-15 02:11:00 +01:00
parent ae0e33132e
commit 674c399ef6
4 changed files with 100 additions and 84 deletions

View File

@@ -0,0 +1,28 @@
pub struct Buttons {}
impl Buttons
{
pub unsafe fn get_button(button_id: u8) -> bool
{
unsafe { *LED_IP_ADDRESS >> (button_id) & 0b1 == 0b1 }
}
}
pub struct Leds {}
impl Leds
{
pub unsafe fn set_led(led_id: u8, state: bool)
{
if state
{
unsafe { *LED_IP_ADDRESS |= (state as u32) << (led_id + 4) }
}
else
{
unsafe { *LED_IP_ADDRESS &= !((state as u32) << (led_id + 4)) }
}
}
}
pub const LED_IP_ADDRESS: *mut u32 = 0x30000000 as *mut u32;

View File

@@ -1,5 +1,6 @@
#![no_std]
pub mod interface;
pub mod register;
pub mod vga;
@@ -7,8 +8,10 @@ pub mod vga;
macro_rules! panic_handler {
() => {
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
fn panic(_info: &core::panic::PanicInfo) -> !
{
loop
{}
}
};
}

View File

@@ -41,17 +41,14 @@ impl VGA
/// # Safety
/// `x` must be less than `WIDTH` and `y` must be less than `HEIGHT`
#[inline(never)]
pub unsafe fn draw_char(x: u16, y: u16, c: char, color: Color)
{
let c = if (c as u8 > b'~') || ((c as u8) < b' ')
{
unsafe { Self::write_pixel_unsafe(x, y, RED) };
b'/' - b' '
}
else
{
unsafe { Self::write_pixel_unsafe(x, y, GREEN) };
c as u8 - b' '
};
@@ -66,12 +63,48 @@ impl VGA
let xx = x + i;
let yy = y + j;
// if xx < (WIDTH as u16)
// && yy < (HEIGHT as u16)
// && unsafe { Self::font_plate_index(char_x as u16 + i, char_y as u16 + j) }
if unsafe { Self::font_plate_index(char_x as u16 + i, char_y as u16 + j) }
if xx < (WIDTH as u16)
&& yy < (HEIGHT as u16)
&& unsafe { Self::font_plate_index(char_x as u16 + i, char_y as u16 + j) }
{
// //unsafe { Self::write_pixel_unsafe(xx, yy, color) }
unsafe { Self::write_pixel_unsafe(xx, yy, color) }
}
}
}
}
pub unsafe fn draw_char_bg(x: u16, y: u16, c: char, color: Color, bg_color: Color)
{
let c = if (c as u8 > b'~') || ((c as u8) < b' ')
{
b'/' - b' '
}
else
{
c as u8 - b' '
};
// Get char position within font plate
let char_x = (c as usize % 32) * FONT_WIDTH;
let char_y = (c as usize / 32) * FONT_HEIGHT;
for i in 0..(FONT_WIDTH as u16)
{
for j in 0..(FONT_HEIGHT as u16)
{
let xx = x + i;
let yy = y + j;
if xx < (WIDTH as u16) && yy < (HEIGHT as u16)
{
if unsafe { Self::font_plate_index(char_x as u16 + i, char_y as u16 + j) }
{
unsafe { Self::write_pixel_unsafe(xx, yy, color) }
}
else
{
unsafe { Self::write_pixel_unsafe(xx, yy, bg_color) }
}
}
}
}
@@ -81,7 +114,7 @@ impl VGA
/// The text must have a length that can fit within a `u16`
pub unsafe fn draw_string(x: u16, y: u16, str: &str, color: Color)
{
str.bytes().enumerate().for_each(|(i, c)| unsafe {
str.chars().enumerate().for_each(|(i, c)| unsafe {
Self::draw_char(x + i as u16 * (FONT_WIDTH as u16), y, c as char, color)
});
}