From 83684cbfd79e9e2d7b9ffa9a1d9351340b9ff486 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Sat, 7 Apr 2018 20:57:14 +0800 Subject: [PATCH] Add debug! macro. Modify VGA & Writer --- src/arch/x86_64/driver/acpi/mod.rs | 2 +- src/arch/x86_64/driver/vga.rs | 42 ++++++++++++++---------------- src/io/mod.rs | 28 ++++++++++++++++++-- src/io/vga_writer.rs | 16 ++++++++---- 4 files changed, 58 insertions(+), 30 deletions(-) diff --git a/src/arch/x86_64/driver/acpi/mod.rs b/src/arch/x86_64/driver/acpi/mod.rs index 90381fe..e065a95 100644 --- a/src/arch/x86_64/driver/acpi/mod.rs +++ b/src/arch/x86_64/driver/acpi/mod.rs @@ -7,7 +7,7 @@ use self::structs::*; pub fn find_rdsp() -> Option<*const rsdp> { use util::{Checkable, find_in_memory}; let ebda = unsafe { *(0x40E as *const u16) as usize } << 4; - println!("EBDA at {:#x}", ebda); + debug!("EBDA at {:#x}", ebda); if ebda != 0 { if let Some(addr) = unsafe{ find_in_memory::(ebda as usize, 1024, 4) } { return Some(addr as *const rsdp); diff --git a/src/arch/x86_64/driver/vga.rs b/src/arch/x86_64/driver/vga.rs index 76b383b..197307e 100644 --- a/src/arch/x86_64/driver/vga.rs +++ b/src/arch/x86_64/driver/vga.rs @@ -40,11 +40,20 @@ impl ColorCode { #[derive(Debug, Clone, Copy)] #[repr(C)] -struct ScreenChar { +pub struct ScreenChar { ascii_char: u8, color_code: ColorCode, } +impl ScreenChar { + pub const fn new(ascii_char: u8, foreground_color: Color, background_color: Color) -> Self { + ScreenChar { + ascii_char, + color_code: ColorCode::new(foreground_color, background_color) + } + } +} + pub const BUFFER_HEIGHT: usize = 25; pub const BUFFER_WIDTH: usize = 80; @@ -53,30 +62,19 @@ pub struct VgaBuffer { } impl VgaBuffer { - fn clear_row(&mut self, row: usize) { - let blank = ScreenChar { - ascii_char: b' ', - color_code: ColorCode::new(Color::White, Color::Black), - }; - for col in 0..BUFFER_WIDTH { - self.chars[row][col].write(blank); - } - } pub fn clear(&mut self) { - for i in 0 .. BUFFER_HEIGHT { - self.clear_row(i); + let blank = ScreenChar::new(b' ', Color::LightGray, Color::Black); + for row in 0 .. BUFFER_HEIGHT { + for col in 0..BUFFER_WIDTH { + self.chars[row][col].write(blank); + } } } - pub fn write(&mut self, row: usize, col: usize, byte: u8) { - let screen_char = &mut self.chars[row][col]; - let color_code = screen_char.read().color_code; - screen_char.write(ScreenChar { - ascii_char: byte, - color_code: color_code, - }); + pub fn write(&mut self, row: usize, col: usize, screen_char: ScreenChar) { + self.chars[row][col].write(screen_char); } - pub fn byte_at(&self, row: usize, col: usize) -> u8 { - self.chars[row][col].read().ascii_char + pub fn read(&self, row: usize, col: usize) -> ScreenChar { + self.chars[row][col].read() } pub fn set_cursor_at(&self, row: usize, col: usize) { assert!(row < BUFFER_HEIGHT && col < BUFFER_WIDTH); @@ -97,6 +95,6 @@ mod test { fn print_something() { let vga = unsafe {&mut *VGA_BUFFER}; vga.clear(); - vga.write(0, 0, b'a'); + vga.write(0, 0, ScreenChar::new('h', Color::LightGray, Color::Black)); } } \ No newline at end of file diff --git a/src/io/mod.rs b/src/io/mod.rs index 413a637..ba05279 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -2,6 +2,17 @@ use core::fmt; mod vga_writer; +macro_rules! _debug { + ($($arg:tt)*) => ({ + $crate::io::debug(format_args!($($arg)*)); + }); +} + +macro_rules! debug { + ($fmt:expr) => (_debug!(concat!($fmt, "\n"))); + ($fmt:expr, $($arg:tt)*) => (_debug!(concat!($fmt, "\n"), $($arg)*)); +} + macro_rules! print { ($($arg:tt)*) => ({ $crate::io::print(format_args!($($arg)*)); @@ -13,9 +24,22 @@ macro_rules! println { ($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*)); } -pub fn print(args: fmt::Arguments) { +use arch::driver::vga::Color; + +fn print_in_color(args: fmt::Arguments, color: Color) { use core::fmt::Write; - vga_writer::VGA_WRITER.lock().write_fmt(args).unwrap(); + use arch::driver::vga::*; + let mut writer = vga_writer::VGA_WRITER.lock(); + writer.set_color(color); + writer.write_fmt(args).unwrap(); +} + +pub fn print(args: fmt::Arguments) { + print_in_color(args, Color::LightGray); +} + +pub fn debug(args: fmt::Arguments) { + print_in_color(args, Color::LightRed); } pub fn init() { diff --git a/src/io/vga_writer.rs b/src/io/vga_writer.rs index df603c6..034b994 100644 --- a/src/io/vga_writer.rs +++ b/src/io/vga_writer.rs @@ -20,11 +20,15 @@ impl VgaWriter { buffer.clear(); VgaWriter { column_position: 0, - color: Color::White, + color: Color::LightGray, buffer: buffer, } } + pub fn set_color(&mut self, color: Color) { + self.color = color; + } + pub fn write_byte(&mut self, byte: u8) { match byte { b'\n' => self.new_line(), @@ -36,7 +40,7 @@ impl VgaWriter { let row = BUFFER_HEIGHT - 1; let col = self.column_position; - self.buffer.write(row, col, byte); + self.buffer.write(row, col, ScreenChar::new(byte, self.color, Color::Black)); self.column_position += 1; self.buffer.set_cursor_at(row, col); } @@ -46,14 +50,16 @@ impl VgaWriter { fn new_line(&mut self) { for row in 1..BUFFER_HEIGHT { for col in 0..BUFFER_WIDTH { - let byte = self.buffer.byte_at(row, col); - self.buffer.write(row-1, col, byte); + let screen_char = self.buffer.read(row, col); + self.buffer.write(row-1, col, screen_char); } } + let blank = ScreenChar::new(b' ', self.color, Color::Black); for col in 0..BUFFER_WIDTH { - self.buffer.write(BUFFER_HEIGHT-1, col, b' '); + self.buffer.write(BUFFER_HEIGHT-1, col, blank); } self.column_position = 0; + self.buffer.set_cursor_at(BUFFER_HEIGHT-1, 0); } }