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

@@ -44,4 +44,7 @@ SECTIONS
PROVIDE(_memory_start = ORIGIN(datamem)); PROVIDE(_memory_start = ORIGIN(datamem));
PROVIDE(_memory_end = ORIGIN(datamem) + LENGTH(datamem)); PROVIDE(_memory_end = ORIGIN(datamem) + LENGTH(datamem));
PROVIDE(_stack_size = 1024); /* 1 KiB */
PROVIDE(_stack_start = ORIGIN(datamem) + LENGTH(datamem));
PROVIDE(_stack_end = _stack_start - _stack_size);
} }

6
bench/programs/pixel.s Normal file
View File

@@ -0,0 +1,6 @@
_start:
li x1, 0x80000000
li x2, 0xFFFFFFFF
sw x2, 0(x1)
stop:
jal x0, stop

10
bench/programs/test.s Normal file
View File

@@ -0,0 +1,10 @@
.text
_start:
addi sp,sp,-16
li a0,1
sb a0,15(sp)
addi a0,sp,15
lui t6,0x6f569
slli t6,t6,0x15
addi t6,t6,-1 # 0x6f568fff
j 0x1c

View File

@@ -5,10 +5,13 @@ pub const WIDTH: usize = 320;
pub const HEIGHT: usize = 240; pub const HEIGHT: usize = 240;
#[repr(transparent)] #[repr(transparent)]
#[derive(Clone, Copy)]
pub struct Color(pub(crate) u8); pub struct Color(pub(crate) u8);
impl Color { impl Color
pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self { {
pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self
{
Self(((r >> 6) << 6) | ((g >> 5) << 3) | (b >> 5)) Self(((r >> 6) << 6) | ((g >> 5) << 3) | (b >> 5))
} }
} }
@@ -23,23 +26,86 @@ pub const BLUE: Color = Color::from_rgb(0, 0, 255);
pub struct VGA {} pub struct VGA {}
impl VGA { impl VGA
{
/// # Safety /// # Safety
/// `x` must be less than `WIDTH` and `y` must be less than `HEIGHT` /// `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) { pub unsafe fn write_pixel_unsafe(x: u16, y: u16, color: Color)
unsafe { *VGA_ADDRESS.add(x as usize + y as usize * WIDTH) = 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) { /// # Safety
let c = c as u8 - b' '; /// `x` must be less than `WIDTH` and `y` must be less than `HEIGHT`
for i in 0..FONT_HEIGHT { #[inline(never)]
for j in 0..FONT_WIDTH { 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; pub const FONT_WIDTH: usize = 6;
pub const FONT_HEIGHT: usize = 13; pub const FONT_HEIGHT: usize = 13;
pub const FONTPLATE_WIDTH: usize = 32 * FONT_WIDTH; pub const FONTPLATE_WIDTH: usize = 32 * FONT_WIDTH;

View File

@@ -6,23 +6,50 @@ use core::arch::asm;
use fpga_lib::{ use fpga_lib::{
panic_handler, panic_handler,
register::X31, register::X31,
vga::{VGA, WHITE}, vga::{BLUE, GREEN, RED, VGA, WHITE},
}; };
panic_handler! {} panic_handler! {}
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
pub extern "C" fn _start() -> ! { pub extern "C" fn _start() -> !
X31::write_const::<0xdead1ffffffff>(); {
let test = X31::read() + 10; unsafe { asm!("la sp, _stack_start") }
unsafe { // X31::write_const::<0xdead1ffffffff>();
asm!( // let test = X31::read() + 10;
"add x31, x0, x0", //unsafe {
"add x31, x0, {}", in(reg) test // 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
{}
} }

View File

@@ -1,15 +1,47 @@
# expected: 000001BC, 000002BC # expected: 0000000000000001, 0000000000000004, 0000000000000008, 000000000000000D, 0000000000000014
.text .text
_start: _start:
add x1, x0, 28 # Test 1: Equal values (should NOT branch)
add x2, x0, 28 li x1, 5
bltu x1, x2, finish # Should not jump li x2, 5
add x31, x0, 0x1BC bltu x1, x2, label1
addi x31, x0, 1 # expected path (not taken)
label1:
add x1, x0, 12 # Test 2: x1 < x2 unsigned (should branch)
add x2, x0, 24 li x1, 5
bltu x1, x2, finish # Should jump to finish li x2, 10
add x31, x0, 0x3BC bltu x1, x2, label2
addi x31, x31, 2 # skipped if branch taken
j label2_end
label2:
addi x31, x31, 3 # expected path (branch taken)
label2_end:
# Test 3: x1 > x2 unsigned (should NOT branch)
li x1, 10
li x2, 5
bltu x1, x2, label3
addi x31, x31, 4 # expected path (not taken)
label3:
# Test 4: x1 = 0xFFFFFFFFFFFFFFFF, x2 = 0 (max unsigned vs min unsigned)
li x1, -1 # 0xFFFFFFFFFFFFFFFF
li x2, 0
bltu x1, x2, label4
addi x31, x31, 5 # expected path (not taken)
label4:
# Test 5: x1 = 0, x2 = 0xFFFFFFFFFFFFFFFF (min unsigned vs max unsigned)
li x1, 0
li x2, -1
bltu x1, x2, label5
addi x31, x31, 6 # skipped if branch taken
j label5_end
label5:
addi x31, x31, 7 # expected path (branch taken)
label5_end:
# Done
nop
finish:
add x31, x0, 0x2BC

View File

@@ -72,41 +72,41 @@ class Alu extends Module {
out := (io.a * io.b) >> 64.U; out := (io.a * io.b) >> 64.U;
} }
is(Div) { // is(Div) {
when(io.b === 0.U) { // when(io.b === 0.U) {
when(io.word_mode) { // when(io.word_mode) {
out := (-1).S(32.W).asUInt // out := (-1).S(32.W).asUInt
}.otherwise { // }.otherwise {
out := (-1).S(64.W).asUInt // out := (-1).S(64.W).asUInt
} // }
}.otherwise { // }.otherwise {
out := (io.a.asSInt / io.b.asSInt).asUInt; // out := (io.a.asSInt / io.b.asSInt).asUInt;
} // }
} // }
//
is(Divu) { // is(Divu) {
when(io.b === 0.U) { // when(io.b === 0.U) {
out := "xFFFFFFFFFFFFFFFF".U // out := "xFFFFFFFFFFFFFFFF".U
}.otherwise { // }.otherwise {
out := io.a / io.b; // out := io.a / io.b;
} // }
} // }
//
is(Rem) { // is(Rem) {
when(io.b === 0.U) { // when(io.b === 0.U) {
out := io.a // out := io.a
}.otherwise { // }.otherwise {
out := (io.a.asSInt % io.b.asSInt).asUInt; // out := (io.a.asSInt % io.b.asSInt).asUInt;
} // }
} // }
//
is(Remu) { // is(Remu) {
when(io.b === 0.U) { // when(io.b === 0.U) {
out := io.a // out := io.a
}.otherwise { // }.otherwise {
out := io.a % io.b; // out := io.a % io.b;
} // }
} // }
is(And) { is(And) {
out := op_a & op_b; out := op_a & op_b;