Run cargo fmt

This commit is contained in:
Greg Shuflin 2022-02-19 01:00:56 -08:00
parent 5c0ea91e65
commit 1cfd133cb2
2 changed files with 82 additions and 89 deletions

View File

@ -17,5 +17,3 @@ pub extern "C" fn _start() -> ! {
println!("Gamarjoba, munde!"); println!("Gamarjoba, munde!");
loop {} loop {}
} }

View File

@ -1,7 +1,6 @@
use core::fmt; use core::fmt;
use spin::Mutex;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use spin::Mutex;
#[macro_export] #[macro_export]
macro_rules! print { macro_rules! print {
@ -48,22 +47,21 @@ pub enum Color {
White = 15, White = 15,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(transparent)] #[repr(transparent)]
pub struct ColorCode(u8); pub struct ColorCode(u8);
impl ColorCode { impl ColorCode {
pub fn new(foreground: Color, background: Color) -> ColorCode { pub fn new(foreground: Color, background: Color) -> ColorCode {
ColorCode((background as u8) << 4 | (foreground as u8)) ColorCode((background as u8) << 4 | (foreground as u8))
} }
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)] #[repr(C)]
struct ScreenChar { struct ScreenChar {
ascii_char: u8, ascii_char: u8,
color_code: ColorCode, color_code: ColorCode,
} }
const BUFFER_WIDTH: usize = 80; const BUFFER_WIDTH: usize = 80;
@ -71,100 +69,97 @@ const BUFFER_HEIGHT: usize = 25;
#[repr(transparent)] #[repr(transparent)]
pub struct Buffer { pub struct Buffer {
chars: [[ScreenChar; BUFFER_WIDTH]; BUFFER_HEIGHT], chars: [[ScreenChar; BUFFER_WIDTH]; BUFFER_HEIGHT],
} }
pub struct Writer { pub struct Writer {
column_position: usize, column_position: usize,
color_code: ColorCode, color_code: ColorCode,
buffer: &'static mut Buffer, buffer: &'static mut Buffer,
} }
impl Writer { impl Writer {
pub fn new(color_code: ColorCode) -> Self { pub fn new(color_code: ColorCode) -> Self {
Writer { Writer {
column_position: 0, column_position: 0,
color_code, color_code,
buffer: unsafe { &mut *(0xb8000 as *mut Buffer) }, buffer: unsafe { &mut *(0xb8000 as *mut Buffer) },
}
}
pub fn write_string(&mut self, s: &str) {
for byte in s.bytes() {
match byte {
0x20..=0x7e | b'\n' => self.write_byte(byte),
_ => self.write_byte(0xfe), // 0xfe is the character ■
}
}
}
pub fn write_byte(&mut self, byte: u8) {
match byte {
b'\n' => self.new_line(),
byte => {
if self.column_position >= BUFFER_WIDTH {
self.new_line();
} }
let row = BUFFER_HEIGHT - 1; }
let col = self.column_position; pub fn write_string(&mut self, s: &str) {
let color_code = self.color_code; for byte in s.bytes() {
match byte {
0x20..=0x7e | b'\n' => self.write_byte(byte),
_ => self.write_byte(0xfe), // 0xfe is the character ■
}
}
}
let ptr = &mut self.buffer.chars[row][col] as *mut ScreenChar; pub fn write_byte(&mut self, byte: u8) {
let screenchar = ScreenChar { match byte {
ascii_char: byte, color_code, b'\n' => self.new_line(),
byte => {
if self.column_position >= BUFFER_WIDTH {
self.new_line();
}
let row = BUFFER_HEIGHT - 1;
let col = self.column_position;
let color_code = self.color_code;
let ptr = &mut self.buffer.chars[row][col] as *mut ScreenChar;
let screenchar = ScreenChar {
ascii_char: byte,
color_code,
};
unsafe {
core::ptr::write_volatile(ptr, screenchar);
}
self.column_position += 1;
}
}
}
fn new_line(&mut self) {
for row in 1..BUFFER_HEIGHT {
for col in 0..BUFFER_WIDTH {
let character = unsafe {
core::ptr::read_volatile(&self.buffer.chars[row][col] as *const ScreenChar)
};
let ptr = &mut self.buffer.chars[row - 1][col] as *mut ScreenChar;
unsafe {
core::ptr::write_volatile(ptr, character);
}
}
}
self.clear_row(BUFFER_HEIGHT - 1);
self.column_position = 0;
}
fn clear_row(&mut self, row: usize) {
let blank = ScreenChar {
ascii_char: b' ',
color_code: self.color_code,
}; };
for col in 0..BUFFER_WIDTH {
unsafe { let ptr = &mut self.buffer.chars[row][col] as *mut ScreenChar;
core::ptr::write_volatile(ptr, screenchar); unsafe {
} core::ptr::write_volatile(ptr, blank);
}
self.column_position += 1;
}
}
}
fn new_line(&mut self) {
for row in 1..BUFFER_HEIGHT {
for col in 0..BUFFER_WIDTH {
let character = unsafe {
core::ptr::read_volatile(&self.buffer.chars[row][col] as *const ScreenChar)
};
let ptr = &mut self.buffer.chars[row - 1][col] as *mut ScreenChar;
unsafe {
core::ptr::write_volatile(ptr, character);
}
}
}
self.clear_row(BUFFER_HEIGHT - 1);
self.column_position = 0;
}
fn clear_row(&mut self, row: usize) {
let blank = ScreenChar {
ascii_char: b' ',
color_code: self.color_code,
};
for col in 0..BUFFER_WIDTH {
let ptr = &mut self.buffer.chars[row][col] as *mut ScreenChar;
unsafe {
core::ptr::write_volatile(ptr, blank);
} }
} }
}
} }
impl fmt::Write for Writer { impl fmt::Write for Writer {
fn write_str(&mut self, s: &str) -> fmt::Result { fn write_str(&mut self, s: &str) -> fmt::Result {
self.write_string(s); self.write_string(s);
Ok(()) Ok(())
} }
} }
lazy_static! { lazy_static! {
pub static ref WRITER: Mutex<Writer> = Mutex::new(Writer::new( pub static ref WRITER: Mutex<Writer> =
ColorCode::new(Color::Yellow, Color::Black) Mutex::new(Writer::new(ColorCode::new(Color::Yellow, Color::Black)));
));
} }