Rust character drawing

This commit is contained in:
2025-11-14 09:57:42 +01:00
parent 6bbecc252e
commit d8596b2247
7 changed files with 212 additions and 68 deletions

View File

@@ -5,10 +5,13 @@ pub const WIDTH: usize = 320;
pub const HEIGHT: usize = 240;
#[repr(transparent)]
#[derive(Clone, Copy)]
pub struct Color(pub(crate) u8);
impl Color {
pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self {
impl Color
{
pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self
{
Self(((r >> 6) << 6) | ((g >> 5) << 3) | (b >> 5))
}
}
@@ -23,21 +26,84 @@ pub const BLUE: Color = Color::from_rgb(0, 0, 255);
pub struct VGA {}
impl VGA {
impl VGA
{
/// # Safety
/// `x` must be less than `WIDTH` and `y` must be less than `HEIGHT`
pub unsafe fn write_pixel_unsafe(x: u16, y: u16, color: Color) {
unsafe { *VGA_ADDRESS.add(x as usize + y as usize * WIDTH) = color }
pub unsafe fn write_pixel_unsafe(x: u16, y: u16, color: Color)
{
let pixel_index = x as usize + y as usize * WIDTH;
let word_index = pixel_index / 4; // Get index of the word in which the pixel is
let byte_index = pixel_index % 4; // Get index of the byte within the word for the pixel
unsafe { *VGA_ADDRESS.add(word_index * 4 + (3 - byte_index)) = color }
}
pub unsafe fn draw_char(x: u16, y: u16, c: char) {
let c = c as u8 - b' ';
for i in 0..FONT_HEIGHT {
for j in 0..FONT_WIDTH {
/// # 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' ')
{
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)
&& 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) }
{
unsafe { Self::write_pixel_unsafe(xx, yy, color) }
}
}
}
}
/// # Safety
/// 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.chars().enumerate().for_each(|(i, c)| unsafe {
//Self::draw_char(x + i as u16 * (FONT_WIDTH as u16), y, c, color)
//Self::draw_char(x + (i as u16) << 1, y, c, color)
//Self::draw_char(x, y, c, color)
//});
for i in 0..2
{
Self::draw_char(
x + (2 - i) as u16 * (FONT_WIDTH as u16),
y,
str.as_bytes()[i] as char,
color,
);
}
}
unsafe fn font_plate_index(x: u16, y: u16) -> bool
{
let pixel_index = (y as usize) * FONTPLATE_WIDTH + (x as usize);
let byte_index = pixel_index / 8;
let bit_index = pixel_index % 8;
(FONTPLATE[byte_index] >> bit_index) & 0b1 == 0b1
}
}
pub const FONT_WIDTH: usize = 6;

View File

@@ -6,23 +6,50 @@ use core::arch::asm;
use fpga_lib::{
panic_handler,
register::X31,
vga::{VGA, WHITE},
vga::{BLUE, GREEN, RED, VGA, WHITE},
};
panic_handler! {}
#[unsafe(no_mangle)]
pub extern "C" fn _start() -> ! {
X31::write_const::<0xdead1ffffffff>();
let test = X31::read() + 10;
unsafe {
asm!(
"add x31, x0, x0",
"add x31, x0, {}", in(reg) test
);
pub extern "C" fn _start() -> !
{
unsafe { asm!("la sp, _stack_start") }
// X31::write_const::<0xdead1ffffffff>();
// let test = X31::read() + 10;
//unsafe {
// asm!(
// "add x31, x0, x0",
// "add x31, x0, {}", in(reg) test
// );
//
// for i in 0..100
// {
// VGA::write_pixel_unsafe(i, i, WHITE);
// }
//unsafe { VGA::draw_char(9, 3, 'S', RED) };
unsafe { VGA::draw_string(3, 3, "{Skibidi toilet !!}", GREEN) };
//}
VGA::write_pixel_unsafe(0, 0, WHITE);
}
// let mut a = core::hint::black_box(true);
// let mut x = core::hint::black_box(121);
// let mut y = core::hint::black_box(221);
// for i in 0..100
// {
// if i < x && i < y && a
// {
// unsafe {
// //VGA::write_pixel_unsafe(i, i, WHITE);
// }
// }
// }
loop {}
// let mut a = core::hint::black_box(true);
// if a
// {
// X31::write_const::<0xdead1ffffffff>();
// }
loop
{}
}