Add debug! macro. Modify VGA & Writer

master
WangRunji 7 years ago
parent 936c485ab9
commit 83684cbfd7

@ -7,7 +7,7 @@ use self::structs::*;
pub fn find_rdsp() -> Option<*const rsdp> { pub fn find_rdsp() -> Option<*const rsdp> {
use util::{Checkable, find_in_memory}; use util::{Checkable, find_in_memory};
let ebda = unsafe { *(0x40E as *const u16) as usize } << 4; let ebda = unsafe { *(0x40E as *const u16) as usize } << 4;
println!("EBDA at {:#x}", ebda); debug!("EBDA at {:#x}", ebda);
if ebda != 0 { if ebda != 0 {
if let Some(addr) = unsafe{ find_in_memory::<rsdp>(ebda as usize, 1024, 4) } { if let Some(addr) = unsafe{ find_in_memory::<rsdp>(ebda as usize, 1024, 4) } {
return Some(addr as *const rsdp); return Some(addr as *const rsdp);

@ -40,11 +40,20 @@ impl ColorCode {
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
#[repr(C)] #[repr(C)]
struct ScreenChar { pub struct ScreenChar {
ascii_char: u8, ascii_char: u8,
color_code: ColorCode, 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_HEIGHT: usize = 25;
pub const BUFFER_WIDTH: usize = 80; pub const BUFFER_WIDTH: usize = 80;
@ -53,30 +62,19 @@ pub struct VgaBuffer {
} }
impl 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) { pub fn clear(&mut self) {
for i in 0 .. BUFFER_HEIGHT { let blank = ScreenChar::new(b' ', Color::LightGray, Color::Black);
self.clear_row(i); 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) { pub fn write(&mut self, row: usize, col: usize, screen_char: ScreenChar) {
let screen_char = &mut self.chars[row][col]; self.chars[row][col].write(screen_char);
let color_code = screen_char.read().color_code;
screen_char.write(ScreenChar {
ascii_char: byte,
color_code: color_code,
});
} }
pub fn byte_at(&self, row: usize, col: usize) -> u8 { pub fn read(&self, row: usize, col: usize) -> ScreenChar {
self.chars[row][col].read().ascii_char self.chars[row][col].read()
} }
pub fn set_cursor_at(&self, row: usize, col: usize) { pub fn set_cursor_at(&self, row: usize, col: usize) {
assert!(row < BUFFER_HEIGHT && col < BUFFER_WIDTH); assert!(row < BUFFER_HEIGHT && col < BUFFER_WIDTH);
@ -97,6 +95,6 @@ mod test {
fn print_something() { fn print_something() {
let vga = unsafe {&mut *VGA_BUFFER}; let vga = unsafe {&mut *VGA_BUFFER};
vga.clear(); vga.clear();
vga.write(0, 0, b'a'); vga.write(0, 0, ScreenChar::new('h', Color::LightGray, Color::Black));
} }
} }

@ -2,6 +2,17 @@ use core::fmt;
mod vga_writer; 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 { macro_rules! print {
($($arg:tt)*) => ({ ($($arg:tt)*) => ({
$crate::io::print(format_args!($($arg)*)); $crate::io::print(format_args!($($arg)*));
@ -13,9 +24,22 @@ macro_rules! println {
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*)); ($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; 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() { pub fn init() {

@ -20,11 +20,15 @@ impl VgaWriter {
buffer.clear(); buffer.clear();
VgaWriter { VgaWriter {
column_position: 0, column_position: 0,
color: Color::White, color: Color::LightGray,
buffer: buffer, buffer: buffer,
} }
} }
pub fn set_color(&mut self, color: Color) {
self.color = color;
}
pub fn write_byte(&mut self, byte: u8) { pub fn write_byte(&mut self, byte: u8) {
match byte { match byte {
b'\n' => self.new_line(), b'\n' => self.new_line(),
@ -36,7 +40,7 @@ impl VgaWriter {
let row = BUFFER_HEIGHT - 1; let row = BUFFER_HEIGHT - 1;
let col = self.column_position; 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.column_position += 1;
self.buffer.set_cursor_at(row, col); self.buffer.set_cursor_at(row, col);
} }
@ -46,14 +50,16 @@ impl VgaWriter {
fn new_line(&mut self) { fn new_line(&mut self) {
for row in 1..BUFFER_HEIGHT { for row in 1..BUFFER_HEIGHT {
for col in 0..BUFFER_WIDTH { for col in 0..BUFFER_WIDTH {
let byte = self.buffer.byte_at(row, col); let screen_char = self.buffer.read(row, col);
self.buffer.write(row-1, col, byte); self.buffer.write(row-1, col, screen_char);
} }
} }
let blank = ScreenChar::new(b' ', self.color, Color::Black);
for col in 0..BUFFER_WIDTH { 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.column_position = 0;
self.buffer.set_cursor_at(BUFFER_HEIGHT-1, 0);
} }
} }

Loading…
Cancel
Save