diff --git a/src/main.rs b/src/main.rs index d252cee..6bd4c83 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,14 +13,7 @@ static HELLO: &[u8] = b"Hello World!"; #[no_mangle] pub extern "C" fn _start() -> ! { - let vga_buffer = 0xb8000 as *mut u8; - - for (i, &byte) in HELLO.iter().enumerate() { - unsafe { - *vga_buffer.offset(i as isize * 2) = byte; - *vga_buffer.offset(i as isize * 2 + 1) = 0xb; - } - } + vga_buffer::print_something(); loop {} } diff --git a/src/vga_buffer.rs b/src/vga_buffer.rs index c77e9c3..be682f0 100644 --- a/src/vga_buffer.rs +++ b/src/vga_buffer.rs @@ -24,10 +24,10 @@ pub enum Color { #[derive(Debug, Clone, Copy, PartialEq, Eq)] #[repr(transparent)] -struct ColorCode(u8) +struct ColorCode(u8); impl ColorCode { - fn new(foreground: Color, background = Color) -> ColorCode { + fn new(foreground: Color, background: Color) -> ColorCode { ColorCode((background as u8) << 4 | (foreground as u8)) } } @@ -42,7 +42,7 @@ struct ScreenChar { const BUFFER_HEIGHT: usize = 25; const BUFFER_WIDTH: usize = 80; -#repr(transparent) +#[repr(transparent)] struct Buffer { chars: [[ScreenChar; BUFFER_WIDTH]; BUFFER_HEIGHT], } @@ -54,7 +54,7 @@ pub struct Writer { } impl Writer { - pub fn write_byte(&mut self, byteL u8) { + pub fn write_byte(&mut self, byte: u8) { match byte { b'\n' => self.new_line(), byte => { @@ -63,7 +63,7 @@ impl Writer { } let row = BUFFER_HEIGHT - 1; - let col self.column_position; + let col = self.column_position; let color_code = self.color_code; self.buffer.chars[row][col] = ScreenChar { @@ -74,6 +74,28 @@ impl Writer { } } } + pub fn write_string(&mut self, s: &str) { + for byte in s.bytes() { + match byte { + // printable ASCII byte or newline + 0x20..=0x7e | b'\n' => self.write_byte(byte), + // not part of printable ascii range + _ => self.write_byte(0xfe), + } + } + } fn new_line(&mut self) {/*TODO */} +} + +pub fn print_something() { + let mut writer = Writer { + column_position: 0, + color_code: ColorCode::new(Color::Yellow, Color::Black), + buffer: unsafe { &mut *(0xb8000 as *mut Buffer) }, + }; + + writer.write_byte(b'H'); + writer.write_string("ello "); + writer.write_string("World!") } \ No newline at end of file