aboutsummaryrefslogtreecommitdiff
path: root/src/io.rs
diff options
context:
space:
mode:
authorTianhao Wang <wth@riseup.net>2024-02-01 05:18:20 +0100
committerTianhao Wang <wth@riseup.net>2024-02-01 05:18:25 +0100
commitf8c1ef0109a8177cf4747a891e6c462c4ef59c92 (patch)
tree1d608c0b41b98304973e1d53bc82be848d83d022 /src/io.rs
parent2bdafab229439dcd9968114ab232ed0a3a218bfb (diff)
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.
Diffstat (limited to 'src/io.rs')
-rw-r--r--src/io.rs34
1 files changed, 34 insertions, 0 deletions
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<CGAScreen> = 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);
+}