From d8596b224731c0b9d98c195bfcfb249c328eba36 Mon Sep 17 00:00:00 2001 From: Albin Chaboissier Date: Fri, 14 Nov 2025 09:57:42 +0100 Subject: [PATCH] Rust character drawing --- bench/link.ld | 3 + bench/programs/pixel.s | 6 ++ bench/programs/test.s | 10 +++ bench/programs_rust/fpga-lib/src/vga.rs | 86 +++++++++++++++++++--- bench/programs_rust/hello-rust/src/main.rs | 51 ++++++++++--- bench/tests/op/bltu.s | 54 +++++++++++--- src/main/scala/projet/Alu.scala | 70 +++++++++--------- 7 files changed, 212 insertions(+), 68 deletions(-) create mode 100644 bench/programs/pixel.s create mode 100644 bench/programs/test.s diff --git a/bench/link.ld b/bench/link.ld index ff98fb3..3bbc33b 100644 --- a/bench/link.ld +++ b/bench/link.ld @@ -44,4 +44,7 @@ SECTIONS PROVIDE(_memory_start = ORIGIN(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); } diff --git a/bench/programs/pixel.s b/bench/programs/pixel.s new file mode 100644 index 0000000..6adfc7f --- /dev/null +++ b/bench/programs/pixel.s @@ -0,0 +1,6 @@ +_start: + li x1, 0x80000000 + li x2, 0xFFFFFFFF + sw x2, 0(x1) +stop: + jal x0, stop diff --git a/bench/programs/test.s b/bench/programs/test.s new file mode 100644 index 0000000..de80682 --- /dev/null +++ b/bench/programs/test.s @@ -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 diff --git a/bench/programs_rust/fpga-lib/src/vga.rs b/bench/programs_rust/fpga-lib/src/vga.rs index 1957f2d..121c3cc 100644 --- a/bench/programs_rust/fpga-lib/src/vga.rs +++ b/bench/programs_rust/fpga-lib/src/vga.rs @@ -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; diff --git a/bench/programs_rust/hello-rust/src/main.rs b/bench/programs_rust/hello-rust/src/main.rs index 2e71d73..139f3c1 100644 --- a/bench/programs_rust/hello-rust/src/main.rs +++ b/bench/programs_rust/hello-rust/src/main.rs @@ -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 + {} } diff --git a/bench/tests/op/bltu.s b/bench/tests/op/bltu.s index e17e957..8d2893c 100644 --- a/bench/tests/op/bltu.s +++ b/bench/tests/op/bltu.s @@ -1,15 +1,47 @@ -# expected: 000001BC, 000002BC +# expected: 0000000000000001, 0000000000000004, 0000000000000008, 000000000000000D, 0000000000000014 .text _start: - add x1, x0, 28 - add x2, x0, 28 - bltu x1, x2, finish # Should not jump - add x31, x0, 0x1BC + # Test 1: Equal values (should NOT branch) + li x1, 5 + li x2, 5 + bltu x1, x2, label1 + addi x31, x0, 1 # expected path (not taken) +label1: + + # Test 2: x1 < x2 unsigned (should branch) + li x1, 5 + li x2, 10 + 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: - add x1, x0, 12 - add x2, x0, 24 - bltu x1, x2, finish # Should jump to finish - add x31, x0, 0x3BC + # 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 diff --git a/src/main/scala/projet/Alu.scala b/src/main/scala/projet/Alu.scala index 0091f10..8461253 100644 --- a/src/main/scala/projet/Alu.scala +++ b/src/main/scala/projet/Alu.scala @@ -72,41 +72,41 @@ class Alu extends Module { out := (io.a * io.b) >> 64.U; } - is(Div) { - when(io.b === 0.U) { - when(io.word_mode) { - out := (-1).S(32.W).asUInt - }.otherwise { - out := (-1).S(64.W).asUInt - } - }.otherwise { - out := (io.a.asSInt / io.b.asSInt).asUInt; - } - } - - is(Divu) { - when(io.b === 0.U) { - out := "xFFFFFFFFFFFFFFFF".U - }.otherwise { - out := io.a / io.b; - } - } - - is(Rem) { - when(io.b === 0.U) { - out := io.a - }.otherwise { - out := (io.a.asSInt % io.b.asSInt).asUInt; - } - } - - is(Remu) { - when(io.b === 0.U) { - out := io.a - }.otherwise { - out := io.a % io.b; - } - } + // is(Div) { + // when(io.b === 0.U) { + // when(io.word_mode) { + // out := (-1).S(32.W).asUInt + // }.otherwise { + // out := (-1).S(64.W).asUInt + // } + // }.otherwise { + // out := (io.a.asSInt / io.b.asSInt).asUInt; + // } + // } + // + // is(Divu) { + // when(io.b === 0.U) { + // out := "xFFFFFFFFFFFFFFFF".U + // }.otherwise { + // out := io.a / io.b; + // } + // } + // + // is(Rem) { + // when(io.b === 0.U) { + // out := io.a + // }.otherwise { + // out := (io.a.asSInt % io.b.asSInt).asUInt; + // } + // } + // + // is(Remu) { + // when(io.b === 0.U) { + // out := io.a + // }.otherwise { + // out := io.a % io.b; + // } + // } is(And) { out := op_a & op_b;