use core::fmt;
use lazy_static::lazy_static;
use spin::Mutex;
lazy_static! {
pub static ref WRITER: Mutex<Writer> = Mutex::new(Writer::new());
}
pub struct Writer {
column_position: usize,
}
impl Writer {
pub const fn new() -> Self {
Writer { column_position: 0 }
}
pub fn write_byte(&mut self, byte: u8) {
match byte {
b'\n' => {
self.new_line();
}
byte => {
self.write_to_serial(byte);
self.column_position += 1;
}
}
}
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),
}
}
}
fn new_line(&mut self) {
self.write_to_serial(b'\n');
self.column_position = 0;
}
fn write_to_serial(&mut self, byte: u8) {
use crate::serial::SERIAL1;
use core::fmt::Write;
let mut serial = SERIAL1.lock();
let _ = serial.write_char(byte as char);
}
}
impl Default for Writer {
fn default() -> Self {
Self::new()
}
}
impl fmt::Write for Writer {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.write_string(s);
Ok(())
}
}
#[doc(hidden)]
pub fn _print(args: fmt::Arguments) {
use crate::interrupts;
use core::fmt::Write;
interrupts::without_interrupts(|| {
WRITER.lock().write_fmt(args).unwrap();
});
}
#[macro_export]
macro_rules! print {
($($arg:tt)*) => ($crate::console::_print(format_args!($($arg)*)));
}
#[macro_export]
macro_rules! println {
() => ($crate::print!("\n"));
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
}