use core::fmt::{self, Write};
mod serial;
pub use serial::Serial;
use serial::COM1;
pub mod cga;
use cga::{Attribute, Color, Rect, Window};
mod keyboard;
pub use keyboard::Keyboard;
use crate::arch::cpu;
use crate::interrupts::guard::GUARD;
use crate::util::Spin;
pub static mut KEYBOARD: Keyboard = Keyboard::new();
pub static SERIAL: Spin<Serial> = Spin::new(Serial::new(COM1));
pub static KOUT: Spin<Window> = Spin::new(
Window::new(Rect::new(0..40, 10..25))
.with_hw_cursor()
.with_style(Attribute::with(Color::Black, Color::Brown)),
);
pub static DBG: Spin<Window> = Spin::new(
Window::new(Rect::new(40..80, 0..25)).with_style(Attribute::with(Color::Red, Color::Blue)),
);
macro_rules! print {
(dbg: $($arg:tt)*) => ({
use core::fmt::Write;
$crate::arch::int::suppress(|| {
let mut out = $crate::device::DBG.lock();
let _ = write!(out, $($arg)*);
})
});
($($arg:tt)*) => ({
use core::fmt::Write;
$crate::arch::int::suppress(|| {
let mut out = $crate::device::KOUT.lock();
let _ = write!(out, $($arg)*);
})
});
}
macro_rules! println {
(dbg: $($arg:tt)*) => ({
use core::fmt::Write;
$crate::arch::int::suppress(|| {
let mut out = $crate::device::DBG.lock();
let _ = writeln!(out, $($arg)*);
})
});
($($arg:tt)*) => ({
use core::fmt::Write;
$crate::arch::int::suppress(|| {
let mut out = $crate::device::KOUT.lock();
let _ = writeln!(out, $($arg)*);
})
});
}
macro_rules! serial {
(force: $($arg:tt)*) => ({
#[allow(unused)]
$crate::arch::int::suppress(|| {
let serial = unsafe { $crate::device::SERIAL.raw() };
$crate::device::_serial(serial, format_args!($($arg)*), core::file!(), core::line!());
})
});
($($arg:tt)*) => ({
#[allow(unused)]
$crate::arch::int::suppress(|| {
let mut serial = $crate::device::SERIAL.lock();
$crate::device::_serial(&mut serial, format_args!($($arg)*), core::file!(), core::line!());
})
});
}
pub fn _serial(serial: &mut Serial, args: fmt::Arguments, file: &str, line: u32) {
let core = cpu::id();
let tid = unsafe { GUARD.read().scheduler.active().unwrap_or(0) };
let _ = write!(serial, "\x1b[0;90m[{core}@{tid} {file}:{line}]\x1b[0m ");
let _ = serial.write_fmt(args);
let _ = serial.write_char('\n');
}