base print function
This commit is contained in:
parent
dbc49f92ec
commit
32258e2397
2 changed files with 28 additions and 13 deletions
|
@ -13,14 +13,7 @@ static HELLO: &[u8] = b"Hello World!";
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn _start() -> ! {
|
pub extern "C" fn _start() -> ! {
|
||||||
let vga_buffer = 0xb8000 as *mut u8;
|
vga_buffer::print_something();
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
loop {}
|
loop {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,10 +24,10 @@ pub enum Color {
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
struct ColorCode(u8)
|
struct ColorCode(u8);
|
||||||
|
|
||||||
impl ColorCode {
|
impl ColorCode {
|
||||||
fn new(foreground: Color, background = Color) -> ColorCode {
|
fn new(foreground: Color, background: Color) -> ColorCode {
|
||||||
ColorCode((background as u8) << 4 | (foreground as u8))
|
ColorCode((background as u8) << 4 | (foreground as u8))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,7 @@ struct ScreenChar {
|
||||||
const BUFFER_HEIGHT: usize = 25;
|
const BUFFER_HEIGHT: usize = 25;
|
||||||
const BUFFER_WIDTH: usize = 80;
|
const BUFFER_WIDTH: usize = 80;
|
||||||
|
|
||||||
#repr(transparent)
|
#[repr(transparent)]
|
||||||
struct Buffer {
|
struct Buffer {
|
||||||
chars: [[ScreenChar; BUFFER_WIDTH]; BUFFER_HEIGHT],
|
chars: [[ScreenChar; BUFFER_WIDTH]; BUFFER_HEIGHT],
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,7 @@ pub struct Writer {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Writer {
|
impl Writer {
|
||||||
pub fn write_byte(&mut self, byteL u8) {
|
pub fn write_byte(&mut self, byte: u8) {
|
||||||
match byte {
|
match byte {
|
||||||
b'\n' => self.new_line(),
|
b'\n' => self.new_line(),
|
||||||
byte => {
|
byte => {
|
||||||
|
@ -63,7 +63,7 @@ impl Writer {
|
||||||
}
|
}
|
||||||
|
|
||||||
let row = BUFFER_HEIGHT - 1;
|
let row = BUFFER_HEIGHT - 1;
|
||||||
let col self.column_position;
|
let col = self.column_position;
|
||||||
|
|
||||||
let color_code = self.color_code;
|
let color_code = self.color_code;
|
||||||
self.buffer.chars[row][col] = ScreenChar {
|
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 */}
|
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!")
|
||||||
|
}
|
Loading…
Reference in a new issue