From daec1c75bf4e9dc58104aa76b183d8ac728c3367 Mon Sep 17 00:00:00 2001 From: clizia Date: Tue, 26 Aug 2025 23:44:17 +0200 Subject: [PATCH] added a static Writer to reuse in the program --- Cargo.toml | 5 +++++ src/main.rs | 6 ++++-- src/vga_buffer.rs | 46 +++++++++++++++++++++++++++++++--------------- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5ebddac..6cf02d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,8 @@ panic = "abort" # disable stack unwinding on panic [dependencies] bootloader = "0.9" volatile = "0.2.6" +spin = "0.5.2" + +[dependencies.lazy_static] +version = "1.0" +features = ["spin_no_std"] diff --git a/src/main.rs b/src/main.rs index 22929da..83bce51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,8 +11,10 @@ static HELLO: &[u8] = b"Hello toto :3"; // looks for a function named `_start` by default #[unsafe(no_mangle)] pub extern "C" fn _start() -> ! { - vga_buffer::print_something(); - + use core::fmt::Write; + vga_buffer::WRITER.lock().write_str("TOTOTO !!!! :3").unwrap(); + write!(vga_buffer::WRITER.lock(), ", smoking cigarette in the shower when they get wet i just light another :{}", 3).unwrap(); + loop {} } diff --git a/src/vga_buffer.rs b/src/vga_buffer.rs index 21ac5af..8f71171 100644 --- a/src/vga_buffer.rs +++ b/src/vga_buffer.rs @@ -1,5 +1,15 @@ use core::fmt; use volatile::Volatile; +use lazy_static::lazy_static; +use spin::Mutex; + +lazy_static! { + pub static ref WRITER: Mutex = Mutex::new(Writer { + column_position: 0, + color_code: ColorCode::new(Color::Pink, Color::Black), + buffer: unsafe { &mut *(0xB8000 as *mut Buffer) }, + }); +} #[allow(dead_code)] #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -103,20 +113,26 @@ impl Writer { } fn new_line(&mut self) { - todo!() + for row in 1..BUFFER_HEIGHT { + for col in 0..BUFFER_WIDTH { + let character = self.buffer.chars[row][col].read(); + self.buffer.chars[row - 1][col].write(character); + } + } + self.clear_row(BUFFER_HEIGHT - 1); + self.column_position = 0; + } + + // this method clears a row by overwriting all of its + // characters with a space character. + fn clear_row(&mut self, row: usize) { + let blank = ScreenChar { + ascii_carachter: b' ', + color_code: self.color_code, + }; + + for col in 0..BUFFER_WIDTH { + self.buffer.chars[row][col].write(blank); + } } } - -// function to test things: to be removed -pub fn print_something() { - use core::fmt::Write; - let mut writer = Writer { - column_position: 0, - color_code: ColorCode::new(Color::Pink, Color::Black), - buffer: unsafe { &mut *(0xB8000 as *mut Buffer) }, - }; - - writer.write_byte(b'H'); - writer.write_string("ello "); - write!(writer, "The numbers are {} and {}", 42, 1.0/3.0).unwrap(); -}