From ac8097715146f5f3886310114c438e98081a6504 Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Sun, 21 Jan 2024 16:35:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=9B=A0rust=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E4=B8=8D=E4=BB=A50=E7=BB=93=E5=B0=BE?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E7=9A=84=E5=86=85=E5=AD=98=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Makefile | 1 + src/kernel/main.rs | 6 +++++- src/kernel/tty/tty.rs | 18 +++++++++++++++--- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Makefile b/src/Makefile index d300cdd..cbfd441 100644 --- a/src/Makefile +++ b/src/Makefile @@ -76,6 +76,7 @@ libk: @echo -e "\e[33m-------------------------\e[0m" rust: + @echo -e "\e[1m\e[33mrustc\e[0m \e[34m-->\e[0m \e[1m\e[32m$@.o\e[0m" @rustc ${RSCFLAGS} lib.rs -o rust.o clear: diff --git a/src/kernel/main.rs b/src/kernel/main.rs index 4c90f07..099a7ee 100644 --- a/src/kernel/main.rs +++ b/src/kernel/main.rs @@ -1,14 +1,18 @@ +use std::panic; + use crate::kernel::tty::tty::{Color, MessageBuilder, Tty}; #[no_mangle] extern "C" fn kmain_rust() { + let tty = Tty::from_id(0).unwrap(); + // let ttyptr = unsafe { tty.get_c_tty_t() }; + let hello = MessageBuilder::new() .message("Hello, ".to_string()) .message("Metaverse".to_string()) .foreground_color(Color(0xa, 0xee, 0xa)) .message("!\n".to_string()) .build(); - let tty = Tty::from_id(0).unwrap(); tty.print(hello); loop {} } diff --git a/src/kernel/tty/tty.rs b/src/kernel/tty/tty.rs index 76d9982..2d6aa59 100644 --- a/src/kernel/tty/tty.rs +++ b/src/kernel/tty/tty.rs @@ -1,9 +1,12 @@ -use std::ptr::null_mut; +use std::{ + io::{BufWriter, Write}, + ptr::null_mut, +}; extern "C" { fn tty_new(tty_type: u8, mode: u8) -> *mut u8; fn tty_get(id: usize) -> *mut *mut u8; - fn tty_text_print(ttyx: *mut u8, string: *mut u8, color: u32, bgcolor: u32); + pub fn tty_text_print(ttyx: *mut u8, string: *mut u8, color: u32, bgcolor: u32); fn tty_get_id(tty: *mut u8) -> usize; } @@ -39,6 +42,10 @@ impl Tty { } } + pub unsafe fn get_c_tty_t(&self) -> *mut u8 { + self.tty_pointer + } + pub fn id(&self) -> usize { unsafe { tty_get_id(self.tty_pointer) } } @@ -51,12 +58,17 @@ impl Tty { } in msg.into_iter() { unsafe { + let string = msg.as_bytes_mut() as *mut [u8] as *mut u8; + let string = string.offset(msg.len() as isize); + let swp = *string; + *string = 0; tty_text_print( self.tty_pointer, msg.as_bytes_mut() as *mut [u8] as *mut u8, u32::from(fgcolor), u32::from(bgcolor), - ) + ); + *string = swp; }; } }