implemented println! macro

This commit is contained in:
clizia 2025-08-27 01:05:47 +02:00
parent f94b8537cf
commit 16e1bc308f
2 changed files with 29 additions and 11 deletions

View file

@ -11,9 +11,7 @@ static HELLO: &[u8] = b"Hello toto :3";
// looks for a function named `_start` by default // looks for a function named `_start` by default
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
pub extern "C" fn _start() -> ! { pub extern "C" fn _start() -> ! {
use core::fmt::Write; println!("smoking cigarettes in the shower\nwhen they get wet i just light another\n:{}", 3);
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 {} loop {}
} }

View file

@ -3,14 +3,6 @@ use volatile::Volatile;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use spin::Mutex; use spin::Mutex;
lazy_static! {
pub static ref WRITER: Mutex<Writer> = Mutex::new(Writer {
column_position: 0,
color_code: ColorCode::new(Color::Pink, Color::Black),
buffer: unsafe { &mut *(0xB8000 as *mut Buffer) },
});
}
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)] #[repr(u8)]
@ -72,6 +64,15 @@ pub struct Writer {
buffer: &'static mut Buffer, buffer: &'static mut Buffer,
} }
lazy_static! {
pub static ref WRITER: Mutex<Writer> = Mutex::new(Writer {
column_position: 0,
color_code: ColorCode::new(Color::Pink, Color::Black),
buffer: unsafe { &mut *(0xB8000 as *mut Buffer) },
});
}
impl fmt::Write for Writer { impl fmt::Write for Writer {
fn write_str(&mut self, s: &str) -> fmt::Result { fn write_str(&mut self, s: &str) -> fmt::Result {
self.write_string(s); self.write_string(s);
@ -79,6 +80,24 @@ impl fmt::Write for Writer {
} }
} }
#[macro_export]
macro_rules! print {
($($arg:tt)*) => ($crate::vga_buffer::_print(format_args!($($arg)*)));
}
#[macro_export]
macro_rules! println {
() => ($crate::print!("\n"));
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
}
#[doc(hidden)]
pub fn _print(args: fmt::Arguments) {
use core::fmt::Write;
WRITER.lock().write_fmt(args).unwrap();
}
impl Writer { impl Writer {
pub fn write_byte(&mut self, byte: u8) { pub fn write_byte(&mut self, byte: u8) {
match byte { match byte {
@ -136,3 +155,4 @@ impl Writer {
} }
} }
} }