From f8c1ef0109a8177cf4747a891e6c462c4ef59c92 Mon Sep 17 00:00:00 2001 From: Tianhao Wang Date: Thu, 1 Feb 2024 05:18:20 +0100 Subject: add println! and panic! macro The rust lazy_static requires interior mutability. I have to include a Mutex impl (spin::Mutex). But I'd like to implement my own primitives. --- src/io.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/io.rs (limited to 'src/io.rs') diff --git a/src/io.rs b/src/io.rs new file mode 100644 index 0000000..988db09 --- /dev/null +++ b/src/io.rs @@ -0,0 +1,34 @@ +use crate::machine::cgascr::CGAScreen; +use core::fmt; +use lazy_static::lazy_static; +use spin::Mutex; + +// TODO I want my own locking primitive for practice, instead of stock spin lock +lazy_static! { + // TODO perhaps remove the 'a lifetime from the struc defs + pub static ref CGASCREEN_GLOBAL: Mutex = Mutex::new(CGAScreen::new()); +} + +#[macro_export] +macro_rules! print { + ($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*))); +} + +#[macro_export] +macro_rules! println { + () => ($crate::print!("\n")); + ($($arg:tt)*) => (print!("{}\n", format_args!($($arg)*))); +} + +pub fn _print(args: fmt::Arguments) { + use core::fmt::Write; + CGASCREEN_GLOBAL.lock().write_fmt(args).unwrap(); +} + +pub fn clear() { + CGASCREEN_GLOBAL.lock().clear(); +} + +pub fn set_attr(attr: u8) { + CGASCREEN_GLOBAL.lock().setattr(attr); +} -- cgit v1.2.3-70-g09d2