Skip to main content

sparreal_kernel/os/
console.rs

1use core::fmt::{self, Write};
2
3use crate::hal;
4
5pub trait Console {
6    fn write_fmt(&self, args: fmt::Arguments<'_>);
7    fn read(&self) -> Option<u8>;
8}
9
10static mut CON: &dyn Console = &EarlyConsole;
11
12fn con() -> &'static dyn Console {
13    unsafe { CON }
14}
15
16struct EarlyConsole;
17
18impl Console for EarlyConsole {
19    fn write_fmt(&self, args: fmt::Arguments<'_>) {
20        let mut writer = EarlyConsole;
21        Write::write_fmt(&mut writer, args).unwrap();
22    }
23    fn read(&self) -> Option<u8> {
24        hal::al::console::early_read()
25    }
26}
27
28impl Write for EarlyConsole {
29    fn write_str(&mut self, s: &str) -> fmt::Result {
30        let mut bytes = s.as_bytes();
31        while !bytes.is_empty() {
32            let n = hal::al::console::early_write(bytes);
33            bytes = &bytes[n..];
34        }
35        Ok(())
36    }
37}
38
39pub fn _write_fmt(args: fmt::Arguments<'_>) {
40    con().write_fmt(args);
41}